gdb - 如何使用信号调试程序?

标签 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;
}

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

如何使我的程序在 gdb 中的行为相同?

最佳答案

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

如果您只是 continue来自GDB,信号会被“吞噬”,这不是你想要的。

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

您也可以要求 GDB 将给定的信号直接传递给下级,而不用 handle SIGTRAP nostop noprint pass 停止。 GDB 命令。您需要在点击第一个 SIGTRAP 之前执行此操作.

关于gdb - 如何使用信号调试程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6008140/

相关文章:

c++ pthreads - 在尝试锁定互斥锁以进行读取时崩溃

c++ - 输出函数调用到日志文件

c - 不要在 epoll_wait 中终止程序

c++ - 如果没有线程阻塞信号,并且一个线程在 `sigwait()` 中,是否可以保证被阻塞的线程将接收到下一个信号?

linux - HUP 何时停止发送?我该怎么办?

c - 一个文件中的信号接收器 (C)

c++ - GDB:[无可用资源]

c++ - 在 GDB 中查看 GCC 优化中间体?

c - 如何重复SIGALRM?

c++ - GDB 不喜欢我的 typedef