【转贴】gdb中的信号(signal)相关调试技巧
一篇不错的帖子,讲的是gdb中的信号(signal)相关调试技巧
转自Magic C++论坛
http://www.magicunix.com/index_ch.html
http://www.magicunix.com/cgi-bin1/forum_cn/ultimatebb.cgi?ubb=get_topic&f=1&t=000060#000003
引用:
--------------------------------------------------------------------------------
原发贴者 Couger:
我写了一个INT信号的处理函数,在处理函数里设置断点后go,但是在console下按Ctrl-C后MC并没有进入处理函数,而console下的程序也直接退出,没有给出希望的输出。
--------------------------------------------------------------------------------
在console下按Ctrl-C后确实发送了SIGINT信号,但是gdb里的缺省设置将会导致由GDB截获的该信息,调试的应用程序无法接受到该信号。
有两种方法可以使调试的应用程序接受到信号:
(1)改变gdb信号处理的设置
比如,以下设置会告诉gdb在接收到SIGINT时不要停止、打印出来、传递给调试目标程序
=====================================
(gdb) handle SIGINT nostop print pass
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal Stop Print Pass to program Description
SIGINT No Yes Yes Interrupt
(gdb)
=====================================
(2)使用gdb命令直接向调试的应用程序发送信号
首先在你希望发送信号的语句处设置断点,然后运行程序,当停止到断点所在位置后,用gdb的signal命令发送信号给调试目标程序
====================================
(gdb) signal SIGINT
Continuing with signal SIGINT.
Breakpoint 1, handler (signal=2) at main.cpp:15
15 printf("Signal handler...\n");
====================================
;-( 但是这两种方法目前MC都还不支持,所以需要等新版本的MC才可以方便的支持你这种调试情况,呵呵。临时先手工调试一下吧。
新版本将会增加
(1)调试器的信号处理设置
(2)支持发送信号命令
调试用例:
============
/*
* This program is uninterruptable with
* Ctrl+C, uses signal handler
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
/* The signal handler function */
void handler( int signal ) {
printf("Signal handler...\n");
psignal( signal, "Signal: ");
} /*handler*/
main() {
/* Registering the handler, catching
SIGINT signals */
signal( SIGINT, handler );
/* Do nothing */
while( 1 ) {
printf("Running...\n");
sleep(10);
} /*while*/
} /*main*/
============
改变gdb的信号处理设置
============
5.3 Signals
A signal is an asynchronous event that can happen in a program. The operating system defines the possible kinds of signals, and gives each kind a name and a number. For example, in Unix SIGINT is the signal a program gets when you type an interrupt character (often C-c); SIGSEGV is the signal a program gets from referencing a place in memory far away from all the areas in use; SIGALRM occurs when the alarm clock timer goes off (which happens only if your program has requested an alarm).
Some signals, including SIGALRM, are a normal part of the functioning of your program. Others, such as SIGSEGV, indicate errors; these signals are fatal (they kill your program immediately) if the program has not specified in advance some other way to handle the signal. SIGINT does not indicate an error in your program, but it is normally fatal so it can carry out the purpose of the interrupt: to kill the program.
GDB has the ability to detect any occurrence of a signal in your program. You can tell GDB in advance what to do for each kind of signal.
Normally, GDB is set up to let the non-erroneous signals like SIGALRM be silently passed to your program (so as not to interfere with their role in the program's functioning) but to stop your program immediately whenever an error signal happens. You can change these settings with the handle command.
info signals
info handle
Print a table of all the kinds of signals and how GDB has been told to handle each one. You can use this to see the signal numbers of all the defined types of signals.
info handle is an alias for info signals.
handle signal keywords...
Change the way GDB handles signal signal. signal can be the number of a signal or its name (with or without the `SIG' at the beginning); a list of signal numbers of the form `low-high'; or the word `all', meaning all the known signals. The keywords say what change to make.
The keywords allowed by the handle command can be abbreviated. Their full names are:
nostop
GDB should not stop your program when this signal happens. It may still print a message telling you that the signal has come in.
stop
GDB should stop your program when this signal happens. This implies the print keyword as well.
print
GDB should print a message when this signal happens.
noprint
GDB should not mention the occurrence of the signal at all. This implies the nostop keyword as well.
pass
noignore
GDB should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled. pass and noignore are synonyms.
nopass
ignore
GDB should not allow your program to see this signal. nopass and ignore are synonyms.
When a signal stops your program, the signal is not visible to the program until you continue. Your program sees the signal then, if pass is in effect for the signal in question at that time. In other words, after GDB reports a signal, you can use the handle command with pass or nopass to control whether your program sees that signal when you continue.
The default is set to nostop, noprint, pass for non-erroneous signals such as SIGALRM, SIGWINCH and SIGCHLD, and to stop, print, pass for the erroneous signals.
You can also use the signal command to prevent your program from seeing a signal, or cause it to see a signal it normally would not see, or to give it any signal at any time. For example, if your program stopped due to some sort of memory reference error, you might store correct values into the erroneous variables and continue, hoping to see more execution; but your program would probably terminate immediately as a result of the fatal signal once it saw the signal. To prevent this, you can continue with `signal 0'. See section Giving your program a signal.
============
直接使用gdb signal命令发送信号给调试目标程序
================
三、产生信号
使用singal命令,可以产生一个信号给被调试的程序。如:中断信号Ctrl+C。这非常方便于程序的调试,可以在程序运行的任意位置设置断点,并在该断点用GDB产生一个信号,这种精确地在某处产生信号非常有利程序的调试。
语法是:signal <singal>,UNIX的系统信号通常从1到15。所以<singal>取值也在这个范围。
single命令和shell的kill命令不同,系统的kill命令发信号给被调试程序时,是由GDB截获的,而single命令所发出一信号则是直接发给被调试程序的。
【转贴】gdb中的信号(signal)相关调试技巧的更多相关文章
- [No0000194]聊聊 Chrome DevTools 中你可能不知道的调试技巧
对于前端开发者来说,ChromeDevTools 绝对是不可或缺的调试工具,我们常用的调试方法包含一些console等,而ChromeDevTools 其实很强大,下面来聊聊一些你可能不知道的debu ...
- [转] gdb中忽略信号处理
信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...
- qt信号signal和槽slot机制
内容: 一.概述 二.信号 三.槽 四.信号与槽的关联 五.元对象工具 六.程序样例 七.应注意的问题 信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工 ...
- pwn 题GDB调试技巧和exp模板
GDB分析ELF文件常用的调试技巧 gdb常用命令 首先是gbd+文件名 静态调试 ,gdb attach +文件名 动态调试 为了方便查看堆栈和寄存器 最好是安装peda插件 安装 可以通过pip直 ...
- gdb中信号
信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...
- 第15.16节 PyQt(Python+Qt)入门学习:PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用
老猿Python博文目录 老猿Python博客地址 一.引言 前面一些章节其实已经在使用信号和槽了,但是作为Qt中最重要的机制也是Qt区别与其他开发平台的重要核心特性,还是非常有必要单独介绍. 二.信 ...
- Qt中信号(signal)和槽(slot)的几种关联方法
声明,个人总结,不一定正确! 1.最常见的,使用connect语句.比如:connect(btnSend,SIGNAL(clicked()),this,SLOT(clear()); 2.在 .ui设计 ...
- Django中信号signal针对model的使用
Django中实现对数据库操作的记录除了使用[开源插件]还可以使用信号signal独立实现 信号机制-观察者模式-发布与订阅:signal - 配置 # 文件路径:Django/myapps/__in ...
- 第四章 、PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 前面章节其实已经在使用信号和槽了,但是作为Qt中最重要的机制也是Qt区别与其他开发平台的重 ...
随机推荐
- bzoj4028
一眼分块题…… 分块,维护每个块的总的gcd和xor和 先思考我们应该怎么查询,考虑到gcd是一个神奇的东西,因为它最多变化logX次 于是我们从前往后扫描每个块,如果一个块内总的gcd是当前扫描的前 ...
- 流行的MySql版本
简介 MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这 ...
- [信息OJ 2467] Asakura的难题
2467: G Asakura的难题 Time Limit:2000MS Memory Limit:65536KB Description Asakura是班里有名的捣蛋鬼,所以经常有同学到老师那里 ...
- python20151125
python exit() python test.py 输入输出 print('2\n2') print(1, 2, 3) name = input() print(':', name) 数据类型和 ...
- -_-#【Dom Ready / Dom Load】
Dom Ready和Dom Load DOM Ready 详解 javascript的domReady 域名解析 - 加载html - 加载js和css - Dom Ready - 加载图片等其他信息 ...
- 【 D3.js 高级系列 — 3.0 】 堆栈图
堆栈图布局(Stack Layout)能够计算二维数组每一数据层的基线,以方便将各数据层叠加起来.本文讲解堆栈图的制作方法. 先说说什么是堆栈图. 例如,有如下情况: 某公司,销售三种产品:个人电脑. ...
- data guard折腾记一
终于有空闲的机器腾出来了,生产环境上的一套Oracle环境终于可以鸟枪换炮了,生产环境有Data Guard,为了减少停机时间,而且避免重新构建Data Guard的麻烦(其实也不麻烦,就是浪费时间) ...
- android 打包签名
1.Eclipse工程中右键工程,弹出选项中选择 android工具 → 生成签名应用包 2.选择需要打包的android项目工程 3.如果已有私钥文件,选择私钥文件 输入密码,如果没有私钥文件见第6 ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- (一)NUnit单元测试心得
由于各种缘由,一本<.Net单元测试艺术>突然出现在了我的办公桌上,于是我的单元测试之路就此开始.通过一两个月不间断的学习,以及不断结合具体的项目做开发,再结合书上的知识对单元测试有了一些 ...