-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace.c
141 lines (115 loc) · 2.44 KB
/
trace.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
134
135
136
137
138
139
140
141
/*
* itrace
*
* Trace specific routines
*
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "trace.h"
#include "ptrace.h"
#include "resolv.h"
#include "elfsym.h"
#include "disas.h"
pid_t trace_pid()
{
iprintf("[+] Attaching to pid %d\n", tracee.pid);
if (ptrace_attach(tracee.pid) < 0) {
iprintf("[!] ptrace_attach failed!\n");
return 0;
}
return tracee.pid;
}
pid_t trace_program()
{
pid_t child;
int nargs;
assert(tracee.prog != NULL);
iprintf("[+] Starting and tracing `%s'\n", tracee.prog);
for (nargs = 0; tracee.prog_args[nargs]; ++nargs) {
iprintf("Arg[%d]: %s\n", nargs, tracee.prog_args[nargs]);
}
child = fork();
if (child == 0) {
/* child process */
if (ptrace_traceme() < 0) {
iprintf("[!] ptrace_traceme failed!\n");
exit(1);
}
if (execv(tracee.prog, tracee.prog_args) == -1) {
iprintf("[!] execv() failed (%s)\n", strerror(errno));
}
exit(1);
} else if (child < 0) {
iprintf("[!] fork() failed (%s)\n", strerror(errno));
exit(1);
}
return child;
}
static void _abort_execution()
{
iprintf("[!] Detaching...\n");
ptrace_detach(tracee.pid);
kill(tracee.pid, SIGINT);
exit(1);
}
void trace_loop()
{
int status, signo = 0, active = (tracee.offset == 0);
unsigned int counter = 0, total = 0;
struct user_regs_struct regs;
signal(SIGINT, _abort_execution);
resolv_startup();
wait(&status);
while (1) {
if (ptrace(PTRACE_SINGLESTEP, tracee.pid, NULL, signo) != 0) {
iprintf("Error: ptrace() failed on single-stepping!\n");
exit(1);
}
wait(&status);
if (!WIFSTOPPED(status)) {
break;
}
signo = WSTOPSIG(status);
if (signo == SIGTRAP) {
signo = 0;
} else {
switch (signo) {
case SIGHUP:
case SIGINT:
case SIGSEGV:
ptrace(PTRACE_CONT, tracee.pid, 0, signo);
goto out;
default:
break;
}
}
++total;
ptrace(PTRACE_GETREGS, tracee.pid, NULL, ®s);
if (!active) {
active = (tracee.offset == regs.reg_eip);
}
if (active) {
++counter;
if ((tracee.flags & IGNORE_LIBS)
&& resolv_is_dynamic(regs.reg_eip)) {
continue;
}
if (tracee.num_inst == 0 || counter <= tracee.num_inst) {
disas_instr(®s);
}
}
}
out:
iprintf("[!] Program exited with status %d\n", WEXITSTATUS(status));
if (tracee.flags & SHOW_MAPS) {
resolv_show_maps();
}
if (tracee.flags & SHOW_COUNT) {
printf("Instructions executed=%u\n", total);
}
resolv_shutdown();
}