-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.h
171 lines (120 loc) · 8.46 KB
/
debug.h
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
#pragma once
#if CHECK_ASSERTS
#ifdef NDEBUG
#undef NDEBUG
#endif
#else
#ifndef NDEBUG
#define NDEBUG
#endif
#endif
#define __dcheck (globalSharedContext->showDebug)
#if DEBUG && DEBUG_SINGLE_INST
#define dbg_single_inst_have_to_jmp() if (__dcheck) cerr << "INS: FORCED LONGJMP! [cod0]\n";
#define dbg_single_inst_false_jmp() if (__dcheck) cerr << "INS: FALSE longjmp found, after: " << ctx->subcallcount << endl;
#define dbg_single_inst_reg_jmp() if (__dcheck) cerr << "\nINS: longjmp [cod1]: addr != last jump addr!!\n";\
if (__dcheck) cerr << "------------------ curr RTN: " << RTN_FindNameByAddress(currFuncAddr) << endl;\
if (__dcheck) cerr << "------------------ last target addr: " << RTN_FindNameByAddress(ctx->jumpTargetFuncAddr) << endl;
#else
#define dbg_single_inst_have_to_jmp()
#define dbg_single_inst_false_jmp()
#define dbg_single_inst_reg_jmp()
#endif
#if DEBUG && DEBUG_BRCALL
#define dbg_brcall_jmp() if (__dcheck) cerr << "\nINS [cod2]: PROBABLE longjmp from: " << currentFuncName << " to " << targetFuncName << endl;\
if (__dcheck) cerr << "INS: insCat: " << CATEGORY_StringShort(insCat) << endl;
#define dbg_brcall_cantpop() if (__dcheck) cerr << "INS: CAN'T pop()!!!\n";
#define dbg_brcall_ifret() if (__dcheck) if (insCat == XED_CATEGORY_RET) cerr << "INS: branchOrCall, RET INSTRUCTION" << endl;
#define dbg_brcall_keepoldjumpaddr() if (__dcheck) cerr << "INS: I'm in .text, but DON'T SET jump target on: " << targetFuncName;\
if (__dcheck) cerr << ", but on: " << RTN_FindNameByAddress(ctx->lastJumpTargetFuncAddr) << endl;
#define dbg_brcall_subcall() if (__dcheck) cerr << "INS: subfunction internal CALL, by: " << currentFuncName << endl; \
if (__dcheck) cerr << "INS: brachOrCall, CALL: " << hex << (void*)targetFuncAddr << dec << endl; \
if (__dcheck) cerr << "INS: exact target addr: " << hex << (void*)targetAddr << dec << endl; \
if (__dcheck) cerr << "INS: current function addr: " << hex << (void*)currentFuncAddr << dec << endl; \
if (__dcheck) cerr << "INS: traceTarget: " << traceTarget << endl;
#define dbg_brcall_tracejmp() if (__dcheck) if (ctx->jumpTargetFuncAddr != targetFuncAddr) \
cerr << "INS: current func: " << currentFuncName << ", setting jump to: " << targetFuncName << endl;
#define dbg_brcall_fjmp_found1() if (__dcheck) cerr << "INS: [cod9] found FORWARD jmp" << endl;
#define dbg_brcall_fjmp_found2() if (__dcheck) cerr << "INS: [cod9] can't pop, but record FORWARD jmp" << endl;
#else
#define dbg_brcall_jmp()
#define dbg_brcall_cantpop()
#define dbg_brcall_ifret()
#define dbg_brcall_keepoldjumpaddr()
#define dbg_brcall_subcall()
#define dbg_brcall_tracejmp()
#define dbg_brcall_fjmp_found1()
#define dbg_brcall_fjmp_found2()
#endif
#if DEBUG && DEBUG_IMGLOAD
#define dbg_imgload_imgname() if (__dcheck) cerr << "Image: " << IMG_Name(img) << endl;
#define dbg_imgload_sectionname() if (__dcheck) cerr << "Section: " << SEC_Name(sec) << endl;
#define dbg_imgload_funcname() if (__dcheck) cerr << "Function: " << funcName << "(0x" << hex \
<< (void*)funcAddr << dec << (trace?"): TRACING\n": "): SKIPPED\n");
#else
#define dbg_imgload_imgname()
#define dbg_imgload_sectionname()
#define dbg_imgload_funcname()
#endif
#if DEBUG && DEBUG_FUNC_TRACE
#define dbg_functr_funcname() if (__dcheck) cerr << "\nFUNC_MODE trace: " << fc->functionName() << ", stacksize: " << ctx->shadowStack.size() << endl;
#define dbg_functr_stackptr() if (__dcheck) cerr << "FUNC_MODE: reg_sp = " << reg_sp << endl << "FUNC_MODE: top sp = " << FUNCMODE_TOP_STACKPTR() << endl;
#define dbg_functr_regsp_gt() if (__dcheck) cerr << "FUNC_MODE: reg_sp >= topstackptr [cod4]\n";
#define dbg_functr_pop() if (__dcheck) cerr << "FUNC_MODE: pop()\n";
#define dbg_functr_fjmps_set() if (__dcheck) cerr << "FUNC_MODE: current func has " << ctx->lastfjmps+1 << " fjmps!\n";
#define dbg_functr_ssize_after() if (__dcheck) cerr << "FUNC MODE: after trace, stack size: " << ctx->shadowStack.size() << endl;
#else
#define dbg_functr_funcname()
#define dbg_functr_stackptr()
#define dbg_functr_regsp_gt()
#define dbg_functr_pop()
#define dbg_functr_fjmps_set()
#define dbg_functr_ssize_after()
#endif
#if DEBUG && DEBUG_FUNC_RET
#define dbg_funcret_name() if (__dcheck) cerr << "\nFUNC_MODE: func ret: " << globalCtx->allFuncs[ctx->getCurrentFunction()]->functionName() << endl;
#define dbg_funcret_pop_err() if (__dcheck) cerr << "FUNC_MODE: Can't pop(): stack size() == 1\n";
#define dbg_funcret_pop() if (__dcheck) cerr << "FUNC_MODE: RETURN -> pop()\n";
#define dbg_funcret_stack_after_pop() if (__dcheck) cerr << "FUNC_MODE: stack size after pop() = " << ctx->shadowStack.size() << endl;
#define dbg_funcret_fjmps() if (__dcheck) if (ctx->shadowStack.top().fjmps) cerr << "FUNC RET: [cod10], fjmps: " << ctx->shadowStack.top().fjmps << endl;
#else
#define dbg_funcret_name()
#define dbg_funcret_pop_err()
#define dbg_funcret_pop()
#define dbg_funcret_stack_after_pop()
#define dbg_funcret_fjmps()
#endif
#if DEBUG && DEBUG_INTRA_TRACE
#define dbg_intratr_begin() if (__dcheck) cerr << "\nINTRA_MODE: tracing block: " << *bb << ", addr: 0x" << hex << (void*)bb->blockAddress() << dec << endl; \
if (__dcheck) cerr << "INTRA_MODE: reg_sp = " << reg_sp << endl;
#define dbg_intratr_nlog_skip() if (__dcheck) cerr << "\nINTRA_MODE: SKIPPING block: " << *bb << endl;
#define dbg_intratr_longjmp() if (__dcheck) cerr << "INTRA_MODE: probable longjmp [cod3]\n"; \
if (__dcheck) cerr << "currFunc: " << RTN_FindNameByAddress( ctx->getCurrentFunction() ) << endl; \
if (__dcheck) cerr << "blockFunc: " << RTN_FindNameByAddress( bb->functionAddr() ) << endl; \
if (__dcheck) cerr << "Calling intramode return procedure..\n";
#define dbg_intratr_begin_sp() if (__dcheck) cerr << "begin stack size: " << intraCtx->shadowStack.size() << endl;
#define dbg_intratr_normal_trace() if (__dcheck) cerr << "NORMAL traceObject\n";
#define dbg_intratr_end_sp() if (__dcheck) cerr << "end stack size: " << intraCtx->shadowStack.size() << endl;
#define dbg_intratr_first_block() if (__dcheck) cerr << "FIRST BLOCK\n";
#define dbg_intratr_first_call() if (__dcheck) cerr << "FIRST FUNCTION CALL\n";
#else
#define dbg_intratr_begin()
#define dbg_intratr_nlog_skip()
#define dbg_intratr_longjmp()
#define dbg_intratr_begin_sp()
#define dbg_intratr_normal_trace()
#define dbg_intratr_end_sp()
#define dbg_intratr_first_block()
#define dbg_intratr_first_call()
#endif
#if DEBUG && DEBUG_INTRA_RET
#define dbg_intraret_begin() if (__dcheck) cerr << "INTRA_MODE ret: " << ctx->getCurrentFunctionName() << endl;
#define dbg_intraret_cantpop() if (__dcheck) cerr << "INTRA_MODE ret: can't pop(), stack size() == 0\n";
#define dbg_intraret_stackpop() if (__dcheck) cerr << "INTRA_MODE shadowStack.pop()\n";
#define dbg_intraret_lastret() if (__dcheck) cerr << "INTRA_MODE: LAST RETURN, next call will be a NEW CALL\n";
#else
#define dbg_intraret_begin()
#define dbg_intraret_cantpop()
#define dbg_intraret_stackpop()
#define dbg_intraret_lastret()
#endif