2

I've written some code using __debugbreak() on Windows, and I'd like to support other compilers, so I'm looking to create a more portable version of this function (Clang and GCC).

I've rejected using inline assembler for now because it does not provide a target-independent means of specifying a debug break. I've also looked at __builtin_trap() but apparently it doesn't really permit for example step in/step over afterwards and that kind of thing. I've also looked at this question but the accepted answer doesn't actually specify the bit that I'm looking for- the bit that goes in the "...".

I've also seen some patches for Clang about supporting this, but apparently Clang still rejected code containing __debugbreak().

Any suggestions?

4

1 回答 1

4

我可以确认这__builtin_trap()不会为您提供正确类型的断点以在 gcc 中继续。它给出了一个illegal opcode trap,这根本不允许代码继续 - 如果你next在 gdb 中这样做,它只会以 a 退出,SIGILL如果你真的希望能够继续,这并不是特别有益。

据我所知,唯一的解决方案是使用内联汇编程序。我尝试int 3在“hello, world”程序中使用 an,虽然gdb没有识别断点,但它确实在指令处停止,并且我能够在中断后继续。

#include <stdio.h>

#define BREAKPOINT \
    asm("int $3")

int main()
{

    BREAKPOINT;

    printf("Hello, world!\n");
    return 0;
}

(在 Linux x86_64 上使用 gcc 和 clang 编译,32 位和 64 位模式)

我确实找到了这个博客: http: //mainisnormallyafunction.blogspot.co.uk/2012/01/embedding-gdb-breakpoints-in-c-source.html

但它需要一个脚本来解析可执行文件(并且可能根本无法在 Windows 下工作)。

于 2013-07-07T22:28:14.397 回答