X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fbreakpoint%2Fbreakpoint.c;fp=ccan%2Fbreakpoint%2Fbreakpoint.c;h=279e29a1dfd1414cd3567766803482c9892972cc;hb=9552c972f725f70ab024e0c9d0487ff078322fd8;hp=0000000000000000000000000000000000000000;hpb=75786a7313a8ac224b7501d5cde841dbda16392e;p=ccan diff --git a/ccan/breakpoint/breakpoint.c b/ccan/breakpoint/breakpoint.c new file mode 100644 index 00000000..279e29a1 --- /dev/null +++ b/ccan/breakpoint/breakpoint.c @@ -0,0 +1,32 @@ +/* CC0 (Public domain) - see LICENSE file for details + * + * Idea for implementation thanks to stackoverflow.com: + * http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running + */ +#include + +bool breakpoint_initialized; +bool breakpoint_under_debug; + +/* This doesn't get called if we're under GDB. */ +static void trap(int signum) +{ + breakpoint_initialized = true; +} + +void breakpoint_init(void) +{ + struct sigaction old, new; + + new.sa_handler = trap; + new.sa_flags = 0; + sigemptyset(&new.sa_mask); + sigaction(SIGTRAP, &new, &old); + kill(getpid(), SIGTRAP); + sigaction(SIGTRAP, &old, NULL); + + if (!breakpoint_initialized) { + breakpoint_initialized = true; + breakpoint_under_debug = true; + } +}