GDB的使用,重点讲解图像化 gdb -tui 方式

==[调试前的准备]=====================================================
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:

 

  • A curses (or TUI) mode in which it displays several text windows on the terminal.

     

  • A standard mode which corresponds to the GDB configured without the TUI.

In the TUI mode, GDB can display several text window on the terminal:

 

command
This window is the GDB command window with the GDB prompt and the GDB outputs. The GDB input is still managed using readline but through the TUI. The command window is always visible.

 

source
The source window shows the source file of the program. The current line as well as active breakpoints are displayed in this window. The current program position is shown with the `>' marker and active breakpoints are shown with `*' markers.

 

assembly
The assembly window shows the disassembly output of the program.

 

register
This window shows the processor registers. It detects when a register is changed and when this is the case, registers that have changed are highlighted.

 

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:

 

  • source

     

  • assembly

     

  • source and assembly

     

  • source and registers

     

  • assembly and registers

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.

 

C-x C-a
C-x a
C-x A
Enter or leave the TUI mode. When the TUI mode is left, the curses window management is left and GDB operates using its standard mode writing on the terminal directly. When the TUI mode is entered, the control is given back to the curses windows. The screen is then refreshed.

C-x 1
Use a TUI layout with only one window. The layout will either be `source' or `assembly'. When the TUI mode is not active, it will switch to the TUI mode.

Think of this key binding as the Emacs C-x 1 binding.

C-x 2
Use a TUI layout with at least two windows. When the current layout shows already two windows, a next layout with two windows is used. When a new layout is chosen, one window will always be common to the previous layout and the new one.

Think of it as the Emacs C-x 2 binding.

 

The following key bindings are handled only by the TUI mode:

 

PgUp
Scroll the active window one page up.

PgDn
Scroll the active window one page down.

Up
Scroll the active window one line up.

Down
Scroll the active window one line down.

Left
Scroll the active window one column left.

Right
Scroll the active window one column right.

C-L
Refresh the screen.

 

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
Display the next layout.

 

layout prev
Display the previous layout.

 

layout src
Display the source window only.

 

layout asm
Display the assembly window only.

 

layout split
Display the source and assembly window.

 

layout regs
Display the register window together with the source or assembly window.

 

focus next | prev | src | asm | regs | split
Set the focus to the named window. This command allows to change the active window so that scrolling keys can be affected to another window.

 

refresh
Refresh the screen. This is similar to using C-L key.

 

update
Update the source window and the current execution point.

 

winheight name +count
winheight name -count
Change the height of the window name by count lines. Positive counts increase the height, while negative counts decrease it.

4 TUI configuration variables

The TUI has several configuration variables that control the appearance of windows on the terminal.

 

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.

 

 

set tui active-border-mode mode
Select the attributes to display the border of the active window. The possible values are normal, standout, reverse, half, half-standout, bold and bold-standout.

 

set tui border-mode mode
Select the attributes to display the border of other windows. The mode can be one of the following:
normal
Use normal attributes to display the border.

 

standout
Use standout mode.

 

reverse
Use reverse video mode.

 

half
Use half bright mode.

 

half-standout
Use half bright and standout mode.

 

bold
Use extra bright or bold mode.

 

bold-standout
Use extra bright or bold and standout mode.

 

 

你可能感兴趣的:(linux,gdb,gdb,-tui)