#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/