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

后端 未结 10 1043
野性不改
野性不改 2020-12-01 08:49

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

10条回答
  •  有刺的猬
    2020-12-01 09:39

    I just added a module to portable-snippets (a collection of public domain snippets of portable code) to do this. It's not 100% portable, but it should be pretty robust:

    • __builtin_debugtrap for some versions of clang (identified with __has_builtin(__builtin_debugtrap))
    • On MSVC and Intel C/C++ Compiler: __debugbreak
    • For ARM C/C++ Compiler: __breakpoint(42)
    • For x86/x86_64, assembly: int3
    • For ARM Thumb, assembly: .inst 0xde01
    • For ARM AArch64, assembly: .inst 0xd4200000
    • For other ARM, assembly: .inst 0xe7f001f0
    • For Alpha, assembly: bpt
    • For non-hosted C with GCC (or something which masquerades as it), __builtin_trap
    • Otherwise, include signal.h and
      • If defined(SIGTRAP) (i.e., POSIX), raise(SIGTRAP)
      • Otherwise, raise(SIGABRT)

    In the future the module in portable-snippets may expand to include other logic and I'll probably forget to update this answer, so you should look there for updates. It's public domain (CC0), so feel free to steal the code.

提交回复
热议问题