breakpoint: new module.
[ccan] / ccan / breakpoint / breakpoint.c
1 /* CC0 (Public domain) - see LICENSE file for details
2  *
3  * Idea for implementation thanks to stackoverflow.com:
4  *      http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running
5  */
6 #include <ccan/breakpoint/breakpoint.h>
7
8 bool breakpoint_initialized;
9 bool breakpoint_under_debug;
10
11 /* This doesn't get called if we're under GDB. */
12 static void trap(int signum)
13 {
14         breakpoint_initialized = true;
15 }
16
17 void breakpoint_init(void)
18 {
19         struct sigaction old, new;
20
21         new.sa_handler = trap;
22         new.sa_flags = 0;
23         sigemptyset(&new.sa_mask);
24         sigaction(SIGTRAP, &new, &old);
25         kill(getpid(), SIGTRAP);
26         sigaction(SIGTRAP, &old, NULL);
27
28         if (!breakpoint_initialized) {
29                 breakpoint_initialized = true;
30                 breakpoint_under_debug = true;
31         }
32 }