-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.cpp
More file actions
executable file
·79 lines (64 loc) · 1.58 KB
/
trace.cpp
File metadata and controls
executable file
·79 lines (64 loc) · 1.58 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
#include <iostream>
#include <stdio.h>
#include <ucontext.h>
#include <bits/wordsize.h>
//
#if __WORDSIZE == 64
typedef uint64_t regType;
#define FRAME_REG REG_RBP
#define IP_REG REG_RIP
#else
typedef uint32_t regType;
#define FRAME_REG REG_EBP
#define IP_REG REG_EIP
#endif
class PrintTrace {
public:
PrintTrace(ucontext_t * u) : _uc(u) {}
void dumpTrace(regType *fp,regType *ip,int frameNumber);
void analyzeContext();
private:
PrintTrace(const PrintTrace &) = delete;
PrintTrace & operator = (const PrintTrace &) = delete;
ucontext_t * _uc;
};
void PrintTrace::analyzeContext() {
int frame = 0;
mcontext_t mc = _uc->uc_mcontext;
regType* cfp = reinterpret_cast<regType*>(mc.gregs[FRAME_REG]);
regType* cip = reinterpret_cast<regType*>(mc.gregs[IP_REG]);
dumpTrace(cfp,cip,frame);
}
void PrintTrace::dumpTrace(regType *fp,regType* ip,int frame) {
if(!fp) {
return;
}
fprintf(stderr,"[%d] Current IP : %p , Current FP : %p .. \n",
frame,ip,fp);
dumpTrace(reinterpret_cast<regType*>(fp[0]),
reinterpret_cast<regType*>((fp+1)[0]),frame+1);
}
#define TRACE \
do { \
ucontext_t uc; \
getcontext(&uc); \
PrintTrace t(&uc); \
t.analyzeContext();\
} while (0)
int func3() {
int i = 10;
int j = 20;
TRACE;
return i+j;
}
int func2() {
int i = 10;
return i+func3();
}
int func1() {
int i = 10;
return i+func2();
}
int main() {
return func1();
}