如何使用信号调试程序?

com*_*fan 11 gdb signals

#include <stdio.h> 
#include <signal.h>

static volatile sig_atomic_t being_debugged = 1;
static void int3_handler(int signo) { being_debugged = 0; }

int main()
{
        signal(SIGTRAP, int3_handler);
        __asm__ __volatile__("int3");
        if (being_debugged) {
        puts("No, I don't want to serve you.");
                while (1) {
            /* endless loop */ ;
        }
        }
        puts("Yes, real routines go here.");
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

当在gdb内部/外部运行时,上面将给出不同的输出,因为gdb捕获sigtrap信号.

如何让我的程序在gdb中表现相同?

Emp*_*ian 19

当下级收到任何信号时,GDB将停止下级(被调试)程序.

如果您只是continue来自GDB,信号将被"吞噬",这不是您想要的.

您可以要求GDB继续该程序并向其发送信号signal SIGTRAP.

您还可以要求GDB将给定信号直接传递给下级,而不是使用handle SIGTRAP nostop noprint passGDB命令完全停止.你击中第一个之前你需要这样做SIGTRAP.