==[调试前的准备]=====================================================
1、程序运行参数。
set args 可指定运行时参数。(如:set args -f 20 -t 40)
show args 命令可以查看设置好的运行参数。
2、运行环境。
path 可设定程序的运行路径。
show paths 查看程序的运行路径。
set environment varname [=value] 设置环境变量。如:set env USER=user
show environment [varname] 查看环境变量。
3、工作目录。
cd 相当于shell的cd命令。
pwd 显示当前的所在目录。
4、程序的输入输出。
info terminal 显示你程序用到的终端的模式。
使用重定向控制程序输出。如:run > outfile
tty命令可以指写输入输出的终端设备。如:tty /dev/ttyb
==[断点]==============================================================
break 在进入指定函数时停住
break 在指定行号停住。
break filename:linenum 在源文件filename的linenum行处停住。
break filename:function 在源文件filename的function函数的入口处停住。
break *address 在程序运行的内存地址处停住。
break ... if 条件成立时停住. 例如: break 337 if i==0
info b[n] : 列出所有断点信息
d : 删除所有断点
==[设置观察点(WatchPoint)]=========================================
watch 一量表达式值有变化时,马上停住程序。
rwatch 当表达式(变量)expr被读时,停住程序。
awatch 当表达式(变量)的值被读或被写时,停住程序。
info watchpoints 列出当前所设置了的所有观察点。
==[为停止点设定运行命令(很实用的强大的功能.)] =======================
commands [bnum]
... command-list ...
end
为断点号bnum指写一个命令列表。当程序被该断点停住时,gdb会依次运
行命令列表中的命令。
==[停止条件维护]=====================================================
condition 修改断点号为bnum的停止条件为expression。
condition 清除断点号为bnum的停止条件。
==[调试子进程]=======================================================
方法一:
GDB对使用fork/vfork创建子进程的程序提供了follow-fork-mode选项来支持多进程调试。
set follow-fork-mode [parent|child]
* parent: fork之后继续调试父进程,子进程不受影响。
* child: fork之后调试子进程,父进程不受影响
(gdb) set follow-fork-mode child
(gdb) break 子进程行号
方法二:
使用GDB的attach命令.
attach命令可以绑定一个外部程序进行调试(可参考(gdb) help attach).
假设调试名为test_proc进程的子进程.先使用
$ ps -ef | grep test_proc
找出其子进程的PID号.然后
(gdb) attach <子进程的PID>
进行调试.之后,用detach命令终止调试.
可以把follow-fork-mode和attach混合使用,用来调试正在运行的程序的子进程.
更详尽的解释.
http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/
==[自定义命令]========================================================
GDB可自定义宏命令来简化方便调试过程.命令定义在文件
.gdbinit文件中.自定义命令的格式为
define <command>
<code>
end
document <command>
<help text>
end
更详尽的解释.
http://www.ibm.com/developerworks/cn/aix/library/au-gdb.html
==[查看C/C++中的宏定义]===================================================
首先在编译时加入参数 ‘-gdwarf-2’ ‘-g3’ :
$ gcc -gdwarf-2 -g3 sample.c -o sample
用于通知gcc编译器在编译时加入扩展信息.
* info macro <macro name> : 显示宏的信息
* macro expand expression : 展开表达式expression. 但是不会显示表达式结果.
假设有定义 #define ADD(x,y) (x)+(y) 那么
(gdb) macro expand ADD(7,8)
expands to: (7)+(8)
==[使用GDB文本界面]======================================================
gdb Text User Interface(TUI) GDB 文本用户界面
(1) 打开TUI模式
方法一: 使用‘gdbtui’ or ‘gdb-tui’开始一个调试
$ gdbtui -q sample
(gdb) ....
方法二: 使用切换键 `ctrl+x ctrl+a` or `ctrl+x A`
(2) TUI模式下有4个窗口,
command 命令窗口. 可以键入调试命令
source 源代码窗口. 显示当前行,断点等信息
assembly 汇编代码窗口
register 寄存器窗口
除command 窗口外,其他三个窗口不可同时显示.其可用 layout 命令来进行选择
自己需要的窗口. 可参见 `help layout` .
(3) 设置TUI
set tui border-kind kind
Select the border appearance for the source, assembly and register windows.
The possible values are the following:
space: Use a space character to draw the border.
ascii: Use ascii characters ‘+’, ‘-’ and ‘|’ to draw the border.
acs Use the Alternate Character Set to draw the border. The
border is
drawn: using character line graphics if the terminal supports them.
(4) 更详尽的说明
http://sourceware.org/gdb/current/onlinedocs/gdb_23.html#SEC235.
另:
ctrl+x再ctrl+a: 在classic和layout两种方式之间切换gdb的显示方式。(layout方式的gdb很有用,但是layout方式的gdb有bug,你会用到这种快速切换的)
再另:GDB Text User Interface
The GDB Text User Interface, TUI in short, is a terminal interface which uses the curses
library to show the source file, the assembly output, the program registers and GDB commands in separate text windows. The TUI is available only when GDB is configured with the --enable-tui
configure option (see section B.3 configure
options).
1 TUI overview
The TUI has two display modes that can be switched while GDB runs:
In the TUI mode, GDB can display several text window on the terminal:
The source, assembly and register windows are attached to the thread and the frame position. They are updated when the current thread changes, when the frame changes or when the program counter changes. These three windows are arranged by the TUI according to several layouts. The layout defines which of these three windows are visible. The following layouts are available:
2 TUI Key Bindings
The TUI installs several key bindings in the readline keymaps (see section 25. Command Line Editing). They allow to leave or enter in the TUI mode or they operate directly on the TUI layout and windows. The following key bindings are installed for both TUI mode and the GDB standard mode.
Think of this key binding as the Emacs C-x 1 binding.
Think of it as the Emacs C-x 2 binding.
The following key bindings are handled only by the TUI mode:
In the TUI mode, the arrow keys are used by the active window for scrolling. This means they are not available for readline. It is necessary to use other readline key bindings such as C-p, C-n, C-b and C-f.
3 TUI specific commands
The TUI has specific commands to control the text windows. These commands are always available, that is they do not depend on the current terminal mode in which GDB runs. When GDB is in the standard mode, using these commands will automatically switch in the TUI mode.
layout next
layout prev
layout src
layout asm
layout split
layout regs
focus next | prev | src | asm | regs | split
refresh
update
winheight name +count
winheight name -count
4 TUI configuration variables
The TUI has several configuration variables that control the appearance of windows on the terminal.
set tui border-kind kind
space
ascii
acs
set tui active-border-mode mode
normal
,
standout
,
reverse
,
half
,
half-standout
,
bold
and
bold-standout
.
set tui border-mode mode
normal
standout
reverse
half
half-standout
bold
bold-standout