gdb调试


1. gdb基础命令

gdb ./myExecutor

(gdb) break main.c:45
(gdb) break func1              断点设置在该函数的开始处
(gdb) info b
(gdb) disable 1
(gdb) enable 1
(gdb) delete 1
(gdb) break test.c:6 if num>0
(gdb) condition 1 num>5     修改断点 1 的条件
(gdb) condition 1               清除断点上的判断条件,但断点本身依然保留
(gdb) list
(gdb) list 6,8                       列出6到21行之间的源码

(gdb) handle 2 nostop pass             gdb将SIGINT信号传递给gdb的程序


handle <signal> <keywords>
keywords: 
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.

(gdb) r config.yaml
(gdb) n
(gdb) c
(gdb) s
(gdb) bt
(gdb) p x
(gdb) info threads

(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set var width=47

(gdb) return

(gdb) signal SIGPIPE

(gdb) call x%10

call expr/func

Reference: Debugging with GDB - Table of Contents

2. gdb调试调试运行中的程序

正常可debug程序运行后仍然可以gdb调试,方法如下:

ps -ef | grep [exec]

sudo gdb attach [process-number]

> info thread

> thread 3

> bt

> layout src

3. 调出 GDB TUI, 查看源代码

// 有两种方式:

// 1. Ctrl + X + A

// 2. layout src

默认情况下,GDB TUI 模式会显示 command 窗口和 source 窗口,还有其他窗口,如下列举的四个常用的窗口:

  • (cmd)command 命令窗口,可以输入调试命令
  • (src)source 源代码窗口, 显示当前行、断点等信息
  • (asm)assembly 汇编代码窗口
  • (reg)register 寄存器窗口

可以通过“layout + 窗口类型”命令来选择自己需要的窗口,例如,在 cmd 窗口输入 layout asm 则可以切换到汇编代码窗口。

4. cgdb 边调试边查看源码

cgdb安装

sudo apt-get install cgdb

cgdb 使用

启动cgdb的时候默认光标在gdb窗口上

  • Esc键  :进入cgdb模式
  • 按键:回到gdb窗口
  • 鼠标滚轮:滚动浏览源代码
  • 按键: 鼠标滚轮可以滚动浏览gdb的调试窗口
  • 按键-/:按25%的比例来缩小放大源代码窗口
  • 按键Ctrl+W : 切换源代码窗口和调试窗口的布局(上下/左右)
  • 按键空格 :当前行打断点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值