-
Notifications
You must be signed in to change notification settings - Fork 1
/
unwindmonitor.cpp
172 lines (149 loc) · 3.5 KB
/
unwindmonitor.cpp
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <unistd.h>
#include <QDebug>
#include <iostream>
#include <cxxabi.h>
#include <unistd.h>
#include <inttypes.h>
#include <libunwind.h>
#include <libunwind-ptrace.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "unwindmonitor.h"
using namespace std;
UnwindMonitor::UnwindMonitor(QObject *parent) :
QObject(parent),
out(stdout)
{
as = unw_create_addr_space(&_UPT_accessors, 0);
}
UnwindMonitor::~UnwindMonitor()
{
unw_destroy_addr_space(as);
}
void UnwindMonitor::execChild(const QStringList args)
{
// setup arguments, impedence between C and C++ is obvious
char *prog = ::strdup(args.first().toLocal8Bit().constData());
char **argv = new char*[args.size() + 1];
argv[args.size()] = 0;
for(int i = 0; i < args.size(); i++) {
argv[i] = ::strdup(args.at(i).toLocal8Bit().constData());
}
// setup ptrace
ptrace(PTRACE_TRACEME, 0, 0, 0);
kill(getpid(), SIGINT);
int ret = execvp(prog, argv);
// handle error
if (ret < 0) {
free(prog);
for (int i = 0; i < args.size(); i++) {
free(argv[i]);
}
delete argv;
out << "execvp error" << endl;
}
}
void UnwindMonitor::execute(const QStringList args)
{
if (args.isEmpty()) {
emit done();
return;
}
pid_t pid = fork();
if (pid == 0) {
execChild(args);
} else if (pid > 0) {
traceProcess(pid);
}
emit done();
return;
}
bool UnwindMonitor::waitSyscall(pid_t pid)
{
int status;
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, &status, 0);
if (WIFEXITED(status))
return false;
return true;
}
void UnwindMonitor::traceProcess(pid_t pid)
{
int status;
struct UPT_info *ui;
ui = (UPT_info*) _UPT_create(pid);
// Initialisation
waitpid(pid, &status, 0);
if (WIFEXITED(status))
goto out;
while(1) {
if (!waitSyscall(pid)) // sys_exit
goto out;
if (!waitSyscall(pid)) // sys_entry
break;
int rax = ptrace(PTRACE_PEEKUSER, pid, 8 * ORIG_RAX, 0);
cout << rax << endl;
doBacktrace(ui);
}
out:
_UPT_destroy(ui);
}
QString demangle(char *orig)
{
QString name;
int status;
char *demang = abi::__cxa_demangle(orig, 0, 0, &status);
switch(status) {
case 0:
name.append(demang);
free(demang);
break;
case -1:
break;
case -2:
case -3:
name.append(orig);
break;
default:
name.append("demangle error");
break;
}
return name;
}
void UnwindMonitor::doBacktrace(struct UPT_info *ui)
{
unw_cursor_t cursor;
unw_word_t ip, prev_ip = 0;
unw_word_t offp;
int ret;
char buf[512];
ret = unw_init_remote(&cursor, as, ui);
if (ret < 0) {
cerr << "unw_init_remote() failed" << endl;
return;
}
do {
ret = unw_get_reg(&cursor, UNW_REG_IP, &ip);
if (ret < 0) {
cerr << "unw_init_remote() failed" << endl;
return;
}
buf[0] = '\0';
if (ip == prev_ip)
break;
prev_ip = ip;
unw_get_proc_name(&cursor, buf, sizeof(buf), &offp);
QString funcName = demangle(buf);
out << QString("ip = %1 %2").arg((long) ip, 0, 16).arg(funcName) << endl;
ret = unw_step(&cursor);
} while (ret > 0);
}