如何使用信号调试程序? [英] How to debug programs using signals?

查看:75
本文介绍了如何使用信号调试程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

静态易失性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(不,我不想为你服务。);
while(1){
/ *无限循环* /;
}
}
puts(Yes,real routines go here。);
返回0;
}

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

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

如果你只是继续

c $ c>来自GDB,信号将被吞噬,这不是你想要的。



你可以要求GDB继续程序发送一个包含信号的信号SIGTRAP



您也可以让GDB直接传递指定的信号而不是停止处理SIGTRAP nostop noprint pass GDB命令。您需要在 之前完成 命中第一个 SIGTRAP


#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;
}

The above will give different output when run inside/outside gdb,because gdb captures the sigtrap signal.

How to make my program behaves the same in gdb?

解决方案

GDB will stop the inferior (being debugged) program when the inferior receives any signal.

If you simply continue from GDB, the signal will be "swallowed", which is not what you want.

You can ask GDB to continue the program and send it a signal with signal SIGTRAP.

You can also ask GDB to pass a given signal directly to the inferior, and not stop at all with handle SIGTRAP nostop noprint pass GDB command. You'll need to do that before you hit the first SIGTRAP.

这篇关于如何使用信号调试程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆