-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cpp
More file actions
263 lines (205 loc) · 4.91 KB
/
Debug.cpp
File metadata and controls
263 lines (205 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include "WinUtils.h"
#endif
#include "Debug.h"
#include "stdio.h"
//#include "windows.h"
#ifdef SIMULATE_NO_WIN32
#define SIMULATE_NO_WIN32_ENABLED
#undef _WIN32
#endif
const char * errorFileName(const char * c) {
if (!c) return nullptr;
size_t len = strlen(c);
for (size_t i=len; i <= len; --i) {
if (i < len && c[i] == '\\') return &c[i+1];
}
return c;
}
const char * errorFilePath(const char * c) {
if (!c) return nullptr;
const size_t bufLen = 500;
static char buffer[bufLen];
for (size_t i=0; i<bufLen; ++i) {
buffer[i] = c[i];
if (!c[i]) break;
}
buffer[bufLen-1] = 0;
size_t len = strlen(buffer);
for (size_t i=len; i <= len; --i) {
if (buffer[i] == '\\') {
buffer[i] = 0;
break;
}
}
return buffer;
}
void Dbg_DoNothing(...) {}
#ifdef _DEBUG
long g_Dbg_Options = DBG_OPT_NONE;
void Dbg_SetOpt(long dbgOpt) {
g_Dbg_Options |= dbgOpt;
}
void Dbg_UnsetOpt(long dbgOpt) {
g_Dbg_Options &= ~dbgOpt;
}
void Dbg_Print(long dbgOpt, const char * fStr, ...) {
//static int lvl = 0;
int lvl = 0;
if (!(dbgOpt & g_Dbg_Options)) return;
if (dbgOpt & DBG_OPT_TRACEOUT) lvl--;
va_list args;
va_start(args, fStr);
#ifdef _MSC_VER
#define OUT_FUNC OutputDebugStringA
#else
#define OUT_FUNC printf
#endif
#define OUT_PREFIX(opt) if (dbgOpt & DBG_OPT_##opt) OUT_FUNC(DBG_STR_##opt)
for (int i=0; i<lvl; ++i) OUT_FUNC(" ");
OUT_PREFIX(TRACEIN);
OUT_PREFIX(TRACEOUT);
OUT_PREFIX(INFO);
OUT_PREFIX(ALLOC);
OUT_PREFIX(FREE);
OUT_PREFIX(CTOR);
OUT_PREFIX(CPTOR);
OUT_PREFIX(DTOR);
OUT_PREFIX(REF);
OUT_PREFIX(MOVE);
#undef OUT_FUNC
#undef OUT_PREFIX
#ifdef _MSC_VER
long n = vsnprintf(NULL, 0, fStr, args);
char buffer[1000];
//static long last_n = 0;
long last_n = 0;
vsprintf(buffer, fStr, args);
OutputDebugStringA(buffer);
#else
vfprintf(stdout, fStr, args);
#endif // _MSC_VER
va_end(args);
if (dbgOpt & DBG_OPT_TRACEIN) lvl++;
}
#endif // _DEBUG
void(*g_MyErrorCore_ptr)(char *, char *, char *, size_t, void *, bool, long);
void SetErrorFunction(void(*funcPtr)(char *, char *, char *, size_t, void *, bool, long)) {
g_MyErrorCore_ptr = funcPtr;
}
__declspec(noinline) void MY_ERROR_CORE(
wchar_t * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue,
long errCode)
{
MY_ERROR_CORE(aVect<char>(errorMsg), funcStr, fileStr, line, exceptPtr, allowContinue, errCode);
}
__declspec(noinline) void MY_ERROR_CORE(
char * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue,
long errCode)
{
#ifdef _WINUTILS_
static WinCriticalSection cs;
Critical_Section(cs) {
#endif
if (g_MyErrorCore_ptr) {
g_MyErrorCore_ptr(errorMsg, funcStr, fileStr, line, exceptPtr, allowContinue, errCode);
}
else {
printf("\n### Error ###\n\n%s\n - Function: %s\n - File: %s\n - Path: %s\n - Line: %Id\n\n",
errorMsg, funcStr, errorFileName(fileStr), errorFilePath(fileStr), line);
#ifdef _WINUTILS_
StopAllOtherThreads();
#endif
if (manualDbgBreak(errorMsg, funcStr, fileStr, line, exceptPtr, allowContinue, errCode)) {
getchar();
//exit(-1);
TerminateProcess(GetCurrentProcess(), -1);
}
StopAllOtherThreads(true);
}
#ifdef _WINUTILS_
}
#endif
}
void MyErrorFunc_(
char * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue,
long errCode)
{
throw MyException(errorMsg, funcStr, fileStr, line, exceptPtr, allowContinue, errCode);
}
__declspec(noinline)
void MY_ERROR_CORE(
wchar_t * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue)
{
MY_ERROR_CORE(aVect<char>(errorMsg), funcStr, fileStr, line, exceptPtr, allowContinue, -1);
}
__declspec(noinline)
void MY_ERROR_CORE(
char * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue)
{
MY_ERROR_CORE(errorMsg, funcStr, fileStr, line, exceptPtr, allowContinue, -1);
}
void MY_ASSERT_CORE(bool cond) {
if (!cond) MY_ERROR("Assertion failed");
}
void EnableMyException() {
SetErrorFunction(MyErrorFunc_);
};
#pragma optimize("", off)
bool manualDbgBreak(
char * errorMsg,
char * funcStr,
char * fileStr,
size_t line,
void * exceptPtr,
bool allowContinue,
long errCode)
{
char msg[5000];
size_t strLen = strlen(errorMsg);
if (strLen > 5000-1) errorMsg[5000-2] = 0;
sprintf(msg, "Error :\n\n%s\n%s", errorMsg, allowContinue ? "\nTerminate program (recommanded) ?" : "");
int result = MessageBoxA(nullptr, msg, "DbgBreak", MB_ICONERROR | (allowContinue ? MB_YESNO : 0) | MB_SERVICE_NOTIFICATION);
#ifdef _WIN32
if (IsDebuggerPresent()) {
__debugbreak();
} else {
CoreDump(errorMsg, exceptPtr, GetCurrentThreadId(), fileStr, line, funcStr);
}
#endif
if (!allowContinue || result == IDYES) {
printf("_STOP_\n");
return true;
}
else {
printf("_Continue_\n");
return false;
}
}
#pragma optimize("", on)