-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_me.c
58 lines (52 loc) · 1.26 KB
/
debug_me.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
/* Start up debugger on current process.
*
* Usage is something like:
*
* #include "debug_me.h"
* ...
* int func() {
* if (case_number == interesting_case)
* debug_me();
* }
*
* When func() gets to interesting_case, an xterm window with gdb will
* be fired up, and the process will wait for the debugger to attach.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
static volatile int wait;
/* To continue execution:
* (gdb) call debug_me_continue()
*/
void debug_me_continue(void) {
fprintf(stderr, " connected\n");
wait = 0;
}
void debug_me(void) {
char pidbuffer[BUFSIZ];
char pathbuffer[BUFSIZ];
int forkval;
wait = 1;
sprintf(pidbuffer, "%d", getpid());
sprintf(pathbuffer, "/proc/%d/exe", getpid());
forkval = fork();
if (forkval == 0) {
/* Child. Start xterm. */
execlp("xterm", "xterm", "-fg", "orange", "-bg", "black",
"-e", "gdb",
"-ex", "call debug_me_continue()",
pathbuffer, pidbuffer, NULL);
perror("debug_me");
exit(2);
} else {
/* Parent process. */
fprintf(stderr, "Waiting for debugger...");
while (wait) {
/* fprintf(stderr, ".");
sleep(1); */
}
fprintf(stderr, "Continuing...\n");
}
}