-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger_utils.c
270 lines (218 loc) · 7.75 KB
/
debugger_utils.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// Created by gnowacki on 10.03.18.
//
#include <libdwarf.h>
#include "debugger_utils.h"
int stepi(pid_t child_pid, int* wait_status, unsigned int* counter, breakpoint_struct** breakpoint_array, int* insert_elem)
{
if(WIFSTOPPED(*wait_status))
{
breakpoint_struct* breakpoint = NULL;
breakpoint = check_if_breakpoint(child_pid, breakpoint_array, insert_elem);
(*counter)++;
struct user_regs_struct registers;
ptrace(PTRACE_GETREGS, child_pid, 0, ®isters);
unsigned int eip = registers.eip;
if((breakpoint != NULL) && ((ptrace(PTRACE_PEEKTEXT, child_pid, breakpoint->address, 0) & 0xFF) == 0xCC))
{
disable_breakpoint(child_pid, breakpoint);
}
if (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {
perror("ptrace");
return -1;
}
wait(wait_status); //NOTE that wait_status is passed through pointer
ptrace(PTRACE_GETREGS, child_pid, 0, ®isters);
unsigned int next_eip = registers.eip;
printf("Executed instruction [%d]: ", *counter);
print_instruction_opcode(child_pid, eip, next_eip);
if(breakpoint != NULL)
{
enable_breakpoint(child_pid, breakpoint);
}
return 0;
}
else if(WIFEXITED(*wait_status))
{
return 1;
}
}
void info_registers(pid_t child_pid)
{
struct user_regs_struct registers;
ptrace(PTRACE_GETREGS, child_pid, 0, ®isters);
print_registers(®isters);
}
void print_registers(const struct user_regs_struct* regs) {
printf("EIP = 0x%08x\n", regs->eip);
printf("EBX = 0x%08x ECX = 0x%08x EDX = 0x%08x EAX = 0x%08x\n", regs->ebx, regs->ecx, regs->edx, regs->eax);
printf("ESI = 0x%08x EDI = 0x%08x EBP = 0x%08x ESP = 0x%08x\n", regs->esi, regs->edi, regs->ebp, regs->esp);
}
void print_instruction_opcode(pid_t pid, unsigned int from_addr, unsigned int to_addr)
{
printf("EIP: 0x%08X OPCODE: ", from_addr);
for(unsigned int addr = from_addr; addr < to_addr; ++addr)
{
unsigned int word = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
printf("%02x", word & 0xFF);
}
printf("\n");
}
extern void run_debuggee_proc(const char* prog_name);
extern void run_debugger_proc(pid_t child_pid, const char* child_prog_name);
void run_new(const char* child_prog_name)
{
pid_t child_pid = fork();
if(child_pid == 0)
run_debuggee_proc(child_prog_name);
else if(child_pid > 0)
run_debugger_proc(child_pid, child_prog_name);
else
{
perror("Fork error");
return;
}
}
int run(pid_t child_pid, int* wait_status, breakpoint_struct** breakpoint_array, int* insert_elem)
{
ptrace(PTRACE_CONT, child_pid, 0, 0);
wait(wait_status);
if (WIFEXITED(*wait_status))
return 0;
clean_breakpoint_and_stepback(child_pid, wait_status, breakpoint_array, insert_elem);
if (WIFSTOPPED(*wait_status))
return 1;
}
breakpoint_struct* check_if_breakpoint(pid_t child_pid, breakpoint_struct** breakpoint_array, int* insert_elem)
{
breakpoint_struct* breakpoint = NULL;
struct user_regs_struct registers;
ptrace(PTRACE_GETREGS, child_pid, 0, ®isters);
int i;
for(i = 0; i < MAX_BREAKPOINTS; ++i)
{
if((breakpoint_array[i] != NULL) && ((long)breakpoint_array[i]->address == registers.eip)) //IMPORTANT - here is no address + 1 as in clean_breakpoint_and_stepback()
{
breakpoint = breakpoint_array[i];
return breakpoint;
}
}
}
int continue_debugging(pid_t child_pid, int* wait_status, breakpoint_struct** breakpoint_array, int* insert_elem)
{
breakpoint_struct* breakpoint = NULL;
breakpoint = check_if_breakpoint(child_pid, breakpoint_array, insert_elem);
if (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
{
perror("ptrace");
return -1;
}
wait(wait_status);
if (WIFEXITED(*wait_status))
return 0;
if(breakpoint != NULL) //if was not found in the array - means that someone has deleted breakpoint in the meanwhile
enable_breakpoint(child_pid, breakpoint);
ptrace(PTRACE_CONT, child_pid, 0, 0);
wait(wait_status);
if (WIFEXITED(*wait_status))
return 0;
clean_breakpoint_and_stepback(child_pid, wait_status, breakpoint_array, insert_elem);
if (WIFSTOPPED(*wait_status))
return 1;
}
void enable_breakpoint(pid_t pid, breakpoint_struct* breakpoint)
{
assert(breakpoint);
breakpoint->original_data = ptrace(PTRACE_PEEKTEXT, pid, breakpoint->address, 0);
ptrace(PTRACE_POKETEXT, pid, breakpoint->address, (breakpoint->original_data & 0xFFFFFF00) | 0xCC);
}
void disable_breakpoint(pid_t pid, breakpoint_struct* breakpoint)
{
assert(breakpoint);
unsigned int data = ptrace(PTRACE_PEEKTEXT, pid, breakpoint->address, 0);
assert((data & 0xFF) == 0xCC);
ptrace(PTRACE_POKETEXT, pid, breakpoint->address, (data & 0xFFFFFF00) | (breakpoint->original_data & 0xFF));
}
breakpoint_struct* create_breakpoint(pid_t pid, void* addr)
{
breakpoint_struct* breakpoint = malloc(sizeof(breakpoint_struct));
breakpoint->address = addr;
enable_breakpoint(pid, breakpoint);
return breakpoint;
}
void free_breakpoint_array(breakpoint_struct** breakpoint_array)
{
int i;
for(i = 0; i < MAX_BREAKPOINTS; ++i)
{
free(breakpoint_array[i]);
}
}
void break_at_address(pid_t child_pid, const char* command_name, breakpoint_struct** breakpoint_array, int* insert_elem)
{
char address_array[8];
strncpy(address_array, command_name + 8, 8);
unsigned int address = (unsigned int)strtol(address_array, NULL, 16);
breakpoint_struct* breakpoint = create_breakpoint(child_pid, (void*)address);
breakpoint_array[*insert_elem] = breakpoint;
(*insert_elem)++;
}
void break_at_address_dwarf(pid_t child_pid, Dwarf_Unsigned address, breakpoint_struct** breakpoint_array, int* insert_elem)
{
breakpoint_struct* breakpoint = create_breakpoint(child_pid, (void*)address);
breakpoint_array[*insert_elem] = breakpoint;
(*insert_elem)++;
}
int clean_breakpoint_and_stepback(pid_t pid, int* wait_status, breakpoint_struct** breakpoint_array, int* insert_elem)
{
breakpoint_struct* breakpoint;
struct user_regs_struct registers;
ptrace(PTRACE_GETREGS, pid, 0, ®isters);
int i;
for(i = 0; i < MAX_BREAKPOINTS; ++i)
{
if((breakpoint_array[i] != NULL) && ((long)breakpoint_array[i]->address + 1 == registers.eip))
{
breakpoint = breakpoint_array[i];
break;
}
}
registers.eip = (long) breakpoint->address;
ptrace(PTRACE_SETREGS, pid, 0, ®isters);
disable_breakpoint(pid, breakpoint);
}
void info_break(pid_t child_pid, breakpoint_struct** breakpoint_array, int* insert_elem)
{
int is_any = 0;
int i;
printf("Enabled breakpoints: \n");
for(i = 0; i < MAX_BREAKPOINTS; ++i)
{
if(breakpoint_array[i] != NULL)
{
printf("[%d] 0x%08X\n", i, breakpoint_array[i]->address);
is_any = 1;
}
}
if(is_any == 0)
printf("%s\n", "No breakpoints are set");
}
int del_breakpoint(pid_t child_pid, int *wait_status, const char* command_name, breakpoint_struct **breakpoint_array, int *insert_elem)
{
char break_num_array[BREAK_ARG_LEN];
strncpy(break_num_array, command_name + 4, BREAK_ARG_LEN);
char* new_line_ptr = strchr(break_num_array, '\n');
*new_line_ptr = '\0';
int break_num = (int)strtol(break_num_array, NULL, 10);
if(breakpoint_array[break_num] != NULL)
{
disable_breakpoint(child_pid, breakpoint_array[break_num]);
free(breakpoint_array[break_num]);
breakpoint_array[break_num] = NULL;
return break_num;
}
else
{
return -1;
}
}