使用GDB/Emacs debug C++程序

使用GDB/Emacs debug C++程序

Date: 2006-09-26
Version: 1.0
Authors: 张乐

gdb使用技巧

注意事项

  1. gdb使用 Readline
  1. 可以使用bash快捷键,tab补齐
  2. 清除inputrc里有冲突的设置

准备工作

  1. 重新编译安装程序。系统里现有的程序一般不适合直接拿来跟踪。需要使用合理的CFLAGS重新编译安装后再跟踪。
./configure
find -name "Makefile" | xargs sed -i -e 's/^CFLAGS = /CFLAGS = -g3 -ggdb3 -gdwarf-2 /'
make && make install

运行

  1. 启动gdb
a. gdb bayonne
b. gdb --args bayonne -vvv script.scr
  1. 利用帮助
a. help 命令类别名
b. help 命令名
  1. 运行程序
a. r[un]
b. r[un] -vvv script.scr

设置断点

  1. 按照函数名设置断点
b[reak] main
b[reak] 'namespace::Class::func()'
  1. 按照文件名和行数设置断点
b[reak] methods.cpp:523

检查状态

  1. 检查堆栈
b[ack]t[race]
up
down
  1. 检查当前函数的源代码
list
  1. 检查/改变变量值
p[rint] var
p[rint] ptr->mem
p[rint] *ptr
p[rint] ('namespace::Class') *this
p[rint] (('namespace::Class') *this)->a.b.c

p[rint] var=value
  1. 检查类
ptype ptr

set print object on
whatis ptr
  1. 检查并启用/禁用断点
set breakpoint pending on
info b[reakpoints]
enable [N]
disable [N]
  1. 检查并切换线程
info th[reads]
thread N
  1. 检查寄存器
info reg
info reg esp
  1. 检查内存
x/10s *0xaabbccdd
x/5x var

继续运行

  1. 运行到某一行即停止,当前函数返回时也停止
advance source.cpp:123
  1. 运行到当前函数返回为止
fin[ish]
  1. 继续运行直到程序结束或下一个断点
c[ontinue]

信号

  1. 检查当前的信号处理策略
info signals
  1. 改变信号处理策略
handle SIGINT pass
handle SIGINT nostop

与Emacs配合使用

  1. 建议使用Emacs-23。设定别名
alias emacs='emacs --enable-font-backend -fn "Dejavu Sans Mono-12"'
  1. 修改~/.emacs,把下面这一行加入
(defvar gdb-many-windows t)
  1. 在Emacs里启动gdb
M-x gdb
  1. 开启多窗口模式
M-x gdb-many-windows
  1. 进入gud-tooltip-mode
M-x gud-tooltip-mode

Comments

Popular posts from this blog

The diff between the original SEC complaint against Ripple and the amended one

Send $SGB (Songbird) to another wallet using a script

Rippled thread cpu usage