c++cross-platformbreakpointsportabilitydebugbreak

Is there a portable equivalent to DebugBreak()/__debugbreak?


In MSVC, DebugBreak() or __debugbreak cause a debugger to break. On x86 it is equivalent to writing _asm int 3, on x64 it is something different. When compiling with gcc (or any other standard compiler) I want to do a break into debugger, too. Is there a platform independent function or intrinsic? I saw the XCode question about that, but it doesn't seem portable enough.

Sidenote: I mainly want to implement ASSERT with that, and I understand I can use assert() for that, but I also want to write DEBUG_BREAK or something into the code.


Solution

  • What about defining a conditional macro based on #ifdef that expands to different constructs based on the current architecture or platform.

    Something like:

    #ifdef _MSC_VER
    #define DEBUG_BREAK __debugbreak()
    #else
    ...
    #endif
    

    This would be expanded by the preprocessor the correct debugger break instruction based on the platform where the code is compiled. This way you always use DEBUG_BREAK in your code.