The Art of Debbugging 笔记 (gdb)

来源:互联网 发布:mysql查询结果中文乱码 编辑:程序博客网 时间:2024/06/10 03:15
 gdb 选项:

 -tui,打开TUI模式,Ctrl-X-A返回non-TUI模式。

 在TUI模式下,上下箭头,PgUp/PgDn 用于滚动代码,Crtl+P/N 用于查看历史命令;

 断点显示在代码右侧,B+表示当前有效,B-表示disable了:



gdb 命令:

断点

break: 2.4节;

tbreak: temporary break,单次有效;

delete/clear/disable/enable: 2.7节;

info breakpoints: 输出格式 2.8.1;

 

条件断点:

break break-args if (condition): (2.10.1)

 

 

 

单步

step: step into a function

next: step over a function 2.9.1.1


finish: 结束当前stack frame,即从当前函数返回,比如:step 进入一个函数,finish可以从这个函数返回;2.9.1.3

continue [n]: 继续执行,可选参数参数n,表示忽略接下来遇到的n个断点;2.9.1.2

 

until: 参数和break一样,可以执行到希望的位置;没有参数,默认是调出循环;2.9.1.4

 

查看堆栈信息:1.5.4

backtrace (bt): 打印所有调用函数堆栈信息;

bt n: 打印栈顶n层信息;

bt -n: 打印栈底n层信息;

frame: 打印当前栈顶;(每一层称为一个frame)

frame n: n从0开始,0表示栈顶,依次类推;

up/down n: 向栈底/顶移动n层;

info fram/f: 打印更详细的当前栈的信息;

 

 

info命令:

info breakpoints

info args

 

attatching to a running process:

$gdb /path/to/the/executable/of/running/process pid-of-running-process

进入gdb之后,可以用attatch命令

(gdb) attatch pid

参考:Using GNU's GDB Debugger, chapter 6 Debugging A Running Process


watch 命令:

(gdb) watch 表达式         当表达式的值发生改变时,程序中止。注意:只有在程序运行过程中才可以设置观察点,因此只有在设置断点之后运行程序再设置观察点才是有意义的

(gdb)awatch 表达式       当表达式被读取或者写入时,程序终止。

可以配合display应用。

display 命令:

(gdb) display 表达式             每运行到断点时显示该表达式的值,程序运行后起作用

(gdb) info display 显示所有display的表达式


 查看内存信息:

x (examine)命令

x/nfu addr

x addr

n, the repeat count

The repeat count is a decimal integer; the default is 1. It specifieshow much memory (counting by unitsu) to display.
f, the display format
The display format is one of the formats used by print,`s' (null-terminated string), or`i' (machine instruction).The default is`x' (hexadecimal) initially.The default changes each time you use eitherx orprint.
u, the unit size
The unit size is any of
b
Bytes.
h
Halfwords (two bytes).
w
Words (four bytes). This is the initial default.
g
Giant words (eight bytes).

Each time you specify a unit size with x, that size becomes thedefault unit the next time you usex. (For the`s' and`i' formats, the unit size is ignored and is normally not written.)

addr, starting display address
addr is the address where you want GDB to begin displayingmemory. The expression need not have a pointer value (though it may);it is always interpreted as an integer address of a byte of memory.See sectionExpressions, for more information on expressions. The default foraddr is usually just after the last address examined--but severalother commands also set the default address:info breakpoints (tothe address of the last breakpoint listed),info line (to thestarting address of a line), and print (if you use it to displaya value from memory).

For example, `x/3uh 0x54320'addr也可以使用程序中的符号)is a request to display three halfwords(h) of memory, formatted as unsigned decimal integers (`u'),starting at address 0x54320. `x/4xw $sp' prints the fourwords (`w') of memory above the stack pointer in hexadecimal (`x').

具体查看 Debug with GDB, 8.5 Examining Memory


打印指针指向的数组

p *array@len:array 是指针,len希望打印的元素个数,元素类型为*array的类型;


 

调试 core

$ gdb executable-file core // executable-file 为可执行文件,core 是可执行文件运行崩溃时dump的core文件;

进入gdb后,也可以用core-file命令指定core file

(gdb) core-file core



参考资料:

1. GNU GDB Debugger Command Cheat Sheet


原创粉丝点击