viv*_*dos 51 c++ portability debugbreak
在MSVC中,DebugBreak()或__debugbreak导致调试器中断.在x86上它相当于写"_asm int 3",在x64上它是不同的东西.在使用gcc(或任何其他标准编译器)进行编译时,我也想进入调试器.是否存在平台无关功能或内在功能?我看到了XCode的问题,但它似乎不够便携.
旁注:我主要想用它来实现ASSERT,我知道我可以使用assert(),但我也想在代码中编写DEBUG_BREAK或其他东西.
caf*_*caf 40
可移植到大多数POSIX系统的方法是:
raise(SIGTRAP);
Run Code Online (Sandbox Code Playgroud)
Jor*_*ira 14
如何基于#ifdef定义一个基于当前架构或平台扩展到不同构造的条件宏.
就像是:
#ifdef _MSC_VER
#define DEBUG_BREAK __debugbreak()
#else
...
#endif
Run Code Online (Sandbox Code Playgroud)
这将由预处理器根据编译代码的平台扩展正确的调试器中断指令.这样您就可以DEBUG_BREAK在代码中使用.
nem*_*equ 10
我只是向可移植代码段(可移植代码的公共领域代码段的集合)添加了一个模块来执行此操作。它不是100%可移植的,但是应该非常健壮:
__builtin_debugtrap对于某些版本的clang(以标识__has_builtin(__builtin_debugtrap))__debugbreak__breakpoint(42)int $03.inst 0xde01.inst 0xd4200000.inst 0xe7f001f0bpt__builtin_trapsignal.h and
defined(SIGTRAP) (i.e., POSIX), raise(SIGTRAP)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.
这似乎是这个问题的一个非常好的、可移植的解决方案: https: //github.com/scottt/debugbreak
引用的存储库 (debugbreak.h) 中提供的标头封装了 MSVC
__debugbreak,
Run Code Online (Sandbox Code Playgroud)
和
__asm__ volatile("int $0x03");
Run Code Online (Sandbox Code Playgroud)
在 i386 和 x86_64 上,以及在 ARM 上它实现
__asm__ volatile(".inst 0xe7f001f0");
Run Code Online (Sandbox Code Playgroud)
以及记录标题中指出的问题的一些解决方法,用于单步执行 GDB 中的断点,以及用于在stepi或cont遇到问题的平台上扩展 GDB 的 Python 脚本。该脚本将debugbreak-step和debugbreak-continue添加到 GDB。