1

How do people trigger a breakpoint on gdb (for Cygwin, specifically) from the very source code?

Like when a JS script has the debugger word in it and Chromium dev tools trigger stop for debugging?

cigien
  • 57,834
  • 11
  • 73
  • 112
1737973
  • 159
  • 18
  • 42
  • remember, when compiling/linking that the necessary information for debugging needs to be kept so the information will be available to the debugger, when using `gcc`, that information can be saved via the option: `-ggdb3`. Note other compilers use different options to have the max debug info available to the debugger. Note the option used may be different depending on which debugger is to be used. – user3629249 May 14 '20 at 17:41
  • [I'm using `-g` to no hassle. Why should, or should not, and for which compilers, on which platforms, switch to `-ggdb3`?](https://stackoverflow.com/questions/61813929/) – 1737973 May 15 '20 at 07:26

1 Answers1

1

Here's how SDL2 implements this feature:

#if defined(_MSC_VER)
/* Don't include intrin.h here because it contains C++ code */
    extern void __cdecl __debugbreak(void);
    #define SDL_TriggerBreakpoint() __debugbreak()
#elif ( (!defined(__NACL__)) && ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))) )
    #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
#elif defined(__386__) && defined(__WATCOMC__)
    #define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
#elif defined(HAVE_SIGNAL_H) && !defined(__WATCOMC__)
    #include <signal.h>
    #define SDL_TriggerBreakpoint() raise(SIGTRAP)
#else
    /* How do we trigger breakpoints on this platform? */
    #define SDL_TriggerBreakpoint()
#endif

The conditionals should probably resolve to __asm__ __volatile__ ( "int $3\n\t" ) on Cygwin.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Yes! `#include \n[...] raise(SIGTRAP);` saves my day. – 1737973 May 14 '20 at 18:45
  • And I don't really get why now, but yes `__asm__ __volatile__ ("int $3\n\t");` effectively saves it definitely even better. – 1737973 May 14 '20 at 18:49
  • [On `__asm__ __volatile__`](https://stackoverflow.com/a/26456845) and [on `"int $3\n\t"` (I guess)](https://stackoverflow.com/q/22379105/1737973) – 1737973 May 15 '20 at 07:22