-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqdbp_enable.c
133 lines (120 loc) · 2.93 KB
/
qdbp_enable.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
enum state {
start_up , // program is starting
wait_segv , // offending instruction triggered
single_step , // run until offending instruction is executed
signal_trap , // send signal
wait_trap , // action completed, re-arm for next iteration
};
static void ptrace_traceme() {
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL)) {
perror("ptrace traceme");
exit(1);
}
}
static void ptrace_cont(pid_t pid, int signo) {
if (ptrace(PTRACE_CONT, pid, NULL, signo)) {
perror("ptrace cont");
kill(pid, SIGTERM);
exit(1);
}
}
static void ptrace_singlestep(pid_t pid, int signo) {
if (ptrace(PTRACE_SINGLESTEP, pid, NULL, signo)) {
perror("ptrace singlestep");
kill(pid, SIGTERM);
exit(1);
}
}
static void ptrace_getregs(pid_t pid, struct user_regs_struct *regs) {
if (ptrace(PTRACE_GETREGS, pid, NULL, regs)) {
perror("ptrace getregs");
kill(pid, SIGTERM);
exit(1);
}
}
static void expect_sigtrap(pid_t pid, int signo) {
if (signo != SIGTRAP) {
fprintf(stderr, "Unexpected signal %u\n", signo);
kill(pid, SIGTERM);
exit(1);
}
}
static void send_signal(pid_t pid) {
if (kill(pid, SIGUSR1)) {
perror("kill");
exit(1);
}
}
int main(int argc, char **argv) {
enum state state = start_up;
unsigned long long int inst;
pid_t child_pid;
int wait_status;
if (argc < 2) {
fprintf(stderr, "Expected a program name as argument\n");
return 1;
}
child_pid = fork();
if (child_pid < 0) {
perror("fork");
return 1;
}
if (!child_pid) {
ptrace_traceme();
execvp(argv[1], &argv[1]);
perror("execvp");
return 1;
}
while (wait(&wait_status), WIFSTOPPED(wait_status)) {
struct user_regs_struct regs;
unsigned sig = WSTOPSIG(wait_status);
switch (state) {
case start_up:
expect_sigtrap(child_pid, sig);
ptrace_cont(child_pid, 0);
state = wait_segv;
break;
case wait_segv:
if (sig != SIGSEGV) {
ptrace_cont(child_pid, sig);
break;
}
ptrace_getregs(child_pid, ®s);
ptrace_singlestep(child_pid, sig);
inst = regs.rip;
state = single_step;
break;
case single_step:
expect_sigtrap(child_pid, sig);
ptrace_getregs(child_pid, ®s);
ptrace_singlestep(child_pid, 0);
if (inst == regs.rip)
state = signal_trap;
break;
case signal_trap:
expect_sigtrap(child_pid, sig);
send_signal(child_pid);
ptrace_cont(child_pid, 0);
state = wait_trap;
break;
case wait_trap:
ptrace_cont(child_pid, sig);
if (sig == SIGUSR1)
state = wait_segv;
break;
}
}
if (WIFEXITED(wait_status))
return WEXITSTATUS(wait_status);
if (WIFSIGNALED(wait_status)) {
printf("Target killed by %u\n", WTERMSIG(wait_status));
return 1;
}
return 0;
}