一篇不错的帖子,讲的是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)相关调试技巧相关推荐

  1. 【转贴】gdb中的信号(signal)相关调试技巧

    一篇不错的帖子,讲的是gdb中的信号(signal)相关调试技巧 转自Magic C++论坛  http://www.magicunix.com/index_ch.html  http://www.m ...

  2. 开发中总结的dart相关的技巧

    特意给大家带来我在开发中总结的dart相关的技巧 1. 你知道吗?Dart 支持字符串乘法. 这是一个简单的程序,显示如何使用字符串乘法打印圣诞树: void main() {for (var i = ...

  3. Visual Studio中11个强大的调试技巧和方法

    调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...

  4. alert不会影响到页面中其他代码执行_JavaScript调试技巧合集——为什么不推荐使用alert调试代码?...

    导读 本文是<JavaScript调试技巧合集>系列分享中的一篇,笔者希望在每篇文章中介绍一个关于JS调试的小知识点,希望你在读完这个系列后,在调试技巧上能够更加运用自如.下面是知识点概览 ...

  5. 利用vstar捕获FPGA设计中的信号进行逻辑调试

    1. 调试窗口 完成FPGA板的执行,并对结果进行分析.VSTAR支持将事件图和波形查看器作为调试窗口. 1.1 启动调试窗口 FGPA板供电连接USB端口时,点击"调试窗口"图标 ...

  6. ACM/OI中C++常用优化(实用/调试/技巧)代码(语法)

    一.C++万能编译头文件 #include<bits/stdc++.h> 从 #include <iostream> #include <cstdio> #incl ...

  7. GPRS模块中CSQ信号值相关

    我们一般用AT指令去查询模块的信号强度 如: 命令:AT+CSQ 返回:+CSQ: **, ## 其中: **应在 0 到 31 之间(99表示无信号),数值越大表明信号质量越好: ##为误码率,值在 ...

  8. chrome控制台如何把vw显示成px_你可能不知道的chrome调试技巧

    本文是对常用的chrome调试技巧进行总结整理,如果你没有深入了解过chrome调试工具,此处总有你不知道的惊喜! 从 Chrome 说起 对于大部分人来说,Chrome 可能只是个浏览器,但是对于开 ...

  9. [转载]qt信号signal和槽slot机制

    好东西! 原文地址:qt信号signal和槽slot机制作者:fox1987 信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工具以及在实际使用过程中应注 ...

最新文章

  1. 区块链是互联网未来十年中举足轻重的技术
  2. Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析
  3. String是一个很普通的类 - Java那些事儿
  4. django 多对多表的创建,级联删除,手动创建第三张表
  5. 微信小程序之通过Canvas生成图片保存到手机相册
  6. [js高手之路]使用原型对象(prototype)需要注意的地方
  7. ubuntu18.0.4 不能下载 libgd2-dev(ubuntu 20.04 安装perl 中GD 模块失败的解决办法)
  8. 异常单据锁定涉及的数据库表
  9. 强化学习原理与python实现原理pdf_纯Python实现!Facebook发布PyTorch分布式强化学习库...
  10. 第十一章 Helm-kubernetes的包管理器(上)
  11. 再品Resnet残差网络
  12. emacs coding UTF-8 保存报错
  13. 信息论与编码_学术动态 | “中大网络信息理论与编码研讨会”成功举办
  14. OSX更新后JRE6被删除引发了问题
  15. Windows、Linux下安装Redis图文教程
  16. php是什么电器元件,常见电子元件识别
  17. NOTEXPRESS 链接文件夹——让题录在文件夹中共享
  18. 为磁盘更换好看的ico图标
  19. JavaEE企业级实战项目 智牛股第一天 概要分析和环境搭建
  20. youtube下载利器

热门文章

  1. 【动态规划】拆分词句
  2. 等额本息java实现
  3. Spark中组件Mllib的学习41之保序回归(Isotonic regression)
  4. 苹果即将停售 iMac Pro,新品要来了?
  5. 电脑开不开机 且开且珍惜
  6. node.js----util.inherits详解
  7. LED智能照明灯具常见调光曲线及其点亮现象简述
  8. (fpga)用verilog写积分函数
  9. 0x80 的具体解释
  10. vue 学习笔记第无弹