breakpoint.c 729 B

1234567891011121314151617181920212223242526272829303132
  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. bool breakpoint_initialized;
  8. bool breakpoint_under_debug;
  9. /* This doesn't get called if we're under GDB. */
  10. static void trap(int signum)
  11. {
  12. breakpoint_initialized = true;
  13. }
  14. void breakpoint_init(void)
  15. {
  16. struct sigaction old, new;
  17. new.sa_handler = trap;
  18. new.sa_flags = 0;
  19. sigemptyset(&new.sa_mask);
  20. sigaction(SIGTRAP, &new, &old);
  21. kill(getpid(), SIGTRAP);
  22. sigaction(SIGTRAP, &old, NULL);
  23. if (!breakpoint_initialized) {
  24. breakpoint_initialized = true;
  25. breakpoint_under_debug = true;
  26. }
  27. }