-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThread.cpp
121 lines (109 loc) · 4.04 KB
/
Thread.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
#include "Thread.h"
#include "InAppDebugger.h"
#include "syscall_abi.h"
#include <sys/user.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <errno.h>
using namespace std;
Thread::Thread(InAppDebugger*dbg, pid_t tid) : dbg(dbg), tid(tid) {
}
Thread::Thread(InAppDebugger*dbg, pid_t tid, shared_ptr<Process> owningProcess) : dbg(dbg), tid(tid), process(owningProcess) {
}
void Thread::setOwningProcess(shared_ptr<Process> process) {
this->process = process;
}
void Thread::processEvent(int status) {
int pass_sig = 0;
dbg->log("%d: processing event 0x%04x (%d)", tid, status, status);
if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_EXIT << 8))) {
dbg->log("%d: thread exited", tid);
dbg->removeThread(tid);
handleClone();
return;
}
if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
handleClone();
return;
}
if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_FORK << 8))) {
handleFork();
return;
}
if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_FORK << 8))) {
handleFork();
return;
}
if (status >> 8 == (SIGTRAP | 0x80)) {
user_regs_struct regs;
ptrace(PTRACE_GETREGS, tid, 0, ®s);
switch (state) {
case State::WAITING_SYSCALL_ENTER:
if (dbg->needLogSyscall(regs.REG_SYSCALL_NO)) {
dbg->log("%d: enter syscall_%ld(%ld, %ld, %ld, %ld, %ld, %ld)",
tid,
(long) regs.REG_SYSCALL_NO,
(long) regs.REG_SYSCALL_ARG1,
(long) regs.REG_SYSCALL_ARG2,
(long) regs.REG_SYSCALL_ARG3,
(long) regs.REG_SYSCALL_ARG4,
(long) regs.REG_SYSCALL_ARG5,
(long) regs.REG_SYSCALL_ARG6);
}
state = State::WAITING_SYSCALL_LEAVE;
break;
case State::WAITING_SYSCALL_LEAVE:
if (dbg->needLogSyscall(regs.REG_SYSCALL_NO)) {
dbg->log("%d: leave syscall_%ld() -> %ld", tid,
(long) regs.REG_SYSCALL_NO,
(long) regs.REG_SYSCALL_RETVAL);
}
state = State::WAITING_SYSCALL_ENTER;
break;
}
} else if (status == 0) {
dbg->log("%d: thread exited", tid);
dbg->removeThread(tid);
return;
} else {
dbg->log("%d: received signal %d, passing to app", tid, status >> 8);
pass_sig = status >> 8;
}
ptraceResume(pass_sig);
}
void Thread::ptraceResume(int signal) {
dbg->log("%d: resuming with signal %d", tid, signal);
if (0 != ptrace(PTRACE_SYSCALL, tid, 0, signal)) {
dbg->log("%d: ERROR: failed to resume thread with signal=%d: errno=%d", tid, signal, errno);
}
}
void Thread::handleClone() {
unsigned long event_message = 0;
ptrace(PTRACE_GETEVENTMSG, tid, nullptr, &event_message);
dbg->log("%d: started new thread %d", tid, event_message);
//
// if (0 != ptrace(PTRACE_CONT, tid, 0, 0)) {
// dbg->log("%d: ERROR: failed to resume thread with signal=%d: errno=%d", tid, signal, 0);
// }
//
// if (0 != ptrace(PTRACE_CONT, event_message, 0, 0)) {
// dbg->log("%d: ERROR: failed to resume thread with signal=%d: errno=%d", event_message, 0, errno);
// }
//
// dbg->onNewThread(process.lock(), event_message, 1);
// Registering new thread with the debugger
dbg->onNewThread(process.lock(), event_message, (SIGTRAP|0x80)<<8);
// resuming this thread immediately
ptraceResume(0);
}
void Thread::handleFork() {
unsigned long event_message = 0;
ptrace(PTRACE_GETEVENTMSG, tid, nullptr, &event_message);
dbg->log("%d: forked new process %d", tid, event_message);
// Registering new thread with the debugger
dbg->onNewProcess(process.lock(), event_message, (SIGTRAP|0x80)<<8);
// resuming this thread immediately
ptraceResume(0);
}