-
Notifications
You must be signed in to change notification settings - Fork 8
/
trace-repl.cpp
225 lines (216 loc) · 7.81 KB
/
trace-repl.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
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
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <string_view>
#include <sys/ptrace.h>
#include <cstring>
#include <string>
#include "utils.h"
using namespace std;
static bool is_signaled = false;
static void sig_handler(int s) {
if (s == SIGCHLD) {
is_signaled = true;
}
}
void parse_signal(int status, int si_code) {
cout << "received status " << hex << status;
if (WIFEXITED(status)) {
cout << "(exited)" << endl;
cout << "return=" << dec << WEXITSTATUS(status) << endl;
} else if (WIFSIGNALED(status)) {
cout << "(signaled)" << endl;
cout << "signal=" << sigabbrev_np(WTERMSIG(status)) << dec << "(" << WTERMSIG(status) << ")" << endl;
} else if (WIFSTOPPED(status)) {
cout << "(stopped)" << endl;
auto stop_sig = WSTOPSIG(status);
cout << "signal=" << sigabbrev_np(stop_sig) << dec << "(" << stop_sig << ")" << endl;
cout << "event=" << parse_ptrace_event(status) << endl;
cout << "si_code=" << si_code << "(" << parse_si_code(si_code) << ")" << endl;
}
}
int trace_main(int pid) {
struct sigaction act{};
act.sa_handler = sig_handler;
if (sigaction(SIGINT, &act, 0) == -1) {
perror("sigaction SIGINT");
}
act.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &act, 0) == -1) {
perror("sigaction SIGCHLD");
}
if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_TRACEEXEC) == -1) {
perror("seize");
return 1;
}
cout << "attach to " << pid << endl;
for (;;) {
string cmd;
cout << pid << "> ";
cin >> cmd;
if (cmd == "q") {
cout << "exit" << endl;
return 0;
} else if (cmd == "d") {
int sig;
cin >> sig;
cout << "detach with " << sig << endl;
if (ptrace(PTRACE_DETACH, pid, 0, sig) == -1) {
perror("detach");
}
cout << "exit" << endl;
return 0;
} else if (cmd == "i") {
if (ptrace(PTRACE_INTERRUPT, pid, 0, 0) == -1) {
perror("interrupt");
} else {
cout << pid << " has been interrupted" << endl;
}
} else if (cmd == "l") {
if (ptrace(PTRACE_LISTEN, pid, 0, 0) == -1) {
perror("listen");
} else {
cout << pid << " has been listened" << endl;
}
} else if (cmd == "c") {
int sig;
cin >> sig;
cout << "continue with signal " << sig << endl;
if (ptrace(PTRACE_CONT, pid, 0, sig) == -1) {
perror("cont");
} else {
cout << pid << " has been continued" << endl;
}
} else if (cmd == "w") {
cout << "waiting ..." << endl;
int status;
for (;;) {
if (waitpid(pid, &status, __WALL) == -1) {
if (errno == EINTR) {
cout << "waiting has been interrupted" << endl;
break;
} else {
perror("wait");
}
} else {
siginfo_t siginfo;
int si_code = 0;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo) == -1) {
perror("get siginfo");
} else si_code = siginfo.si_code;
parse_signal(status, si_code);
break;
}
}
} else if (cmd == "W") {
int status;
auto wait_pid = waitpid(pid, &status, __WALL | WNOHANG);
if (wait_pid == -1) {
perror("wait");
} else if (wait_pid == 0) {
cout << "nothing to wait" << endl;
} else {
siginfo_t siginfo;
int si_code = 0;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo) == -1) {
perror("get siginfo");
} else si_code = siginfo.si_code;
parse_signal(status, si_code);
}
} else if (cmd == "p") {
cout << (is_signaled ? "true" : "false") << endl;
is_signaled = false;
} else if (cmd == "k") {
int sig;
cin >> sig;
cout << "kill with signal " << sig << endl;
if (tgkill(pid, pid, sig) == -1) perror("tgkill");
} else if (cmd == "C") {
cout << "continue if stopped" << endl;
int status;
auto wait_pid = waitpid(pid, &status, __WALL | WNOHANG);
if (wait_pid == -1) {
perror("wait");
} else if (wait_pid == 0) {
cout << "nothing to wait, maybe process is not stopped" << endl;
} else {
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && (status>>16 == PTRACE_EVENT_STOP)) {
cout << "inject SIGCONT" << endl;
if (tgkill(pid, pid, SIGCONT)) perror("tgkill cont");
ptrace(PTRACE_CONT, pid, 0, 0);
wait_pid = waitpid(pid, &status, __WALL);
if (wait_pid == -1) perror("wait");
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP && (status>>16 == PTRACE_EVENT_STOP)) {
cout << "trap c 0" << endl;
ptrace(PTRACE_CONT, pid, 0, 0);
} else {
cout << "trap not received:" << WSTOPSIG(status) << endl;
continue;
}
wait_pid = waitpid(pid, &status, __WALL);
if (wait_pid == -1) perror("wait");
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGCONT) {
cout << "cont c 0" << endl;
ptrace(PTRACE_CONT, pid, 0, 0);
}
} else {
cout << "process is not stopped" << endl;
siginfo_t siginfo;
int si_code = 0;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo) == -1) {
perror("get siginfo");
} else si_code = siginfo.si_code;
parse_signal(status, si_code);
}
}
} else {
cout << "invalid command " << cmd << endl;
}
}
}
int main(int argc, char **argv) {
if (argc <= 1) {
cerr << "usage: " << argv[0] << " <pid>" << endl;
return 1;
}
cout << "Command:" << endl;
cout << " d <sig>: detach with signal and exit" << endl;
cout << " i: interrupt" << endl;
cout << " l: listen" << endl;
cout << " w: wait (Ctrl-C to stop)" << endl;
cout << " c <sig>: continue with signal <sig>" << endl;
cout << " W: wait not hang" << endl;
cout << " q: exit" << endl;
cout << " p: peek SIGCHLD value" << endl;
cout << " k <sig>: kill with signal" << endl;
cout << " C: continue process if stopped" << endl;
auto pid = (int) strtol(argv[1], nullptr, 0);
return trace_main(pid);
}
/**
* stop-before-seize:
* ./sleeper 0
* ./trace-repl pid
* w -> SIGSTOP + PTRACE_EVENT_STOP
* c 0 -> process continued during tracing (stop after detaching)
* k 18 -> send SIGCONT
* c 0, w -> SIGTRAP + PTRACE_EVENT_STOP
* (if SIGCONT is not blocked) c 0, w -> SIGCONT
* c 0, w -> process continued correctly
*
* stop-after-seize:
* ./sleeper 5
* ./trace-repl pid
* <waiting for SIGSTOP>
* w -> SIGSTOP + SI_TKILL
* c 0 -> process continued correctly (SIGSTOP was suppressed)
* c 19 -> stop-before-seize
*
* stop and detach after exec:
* ./sleeper 0 --exec
* ./trace-repl pid
* <waiting for SIGTRAP + PTRACE_EVENT_EXEC>
* k 19
* c 0, w -> SIGSTOP + SI_TKILL
* d 19 -> process is stopped and detached
*/