gdb高级调试技巧

1. 反向调试

gdb支持程序反向执行。

record  让程序开始记录反向调试所必要的信息

rn : reverse next

rc: reverse continue ,Continue program being debugged but run it in reverse

record stop: 停止记录

2. 格式化(pretty print)打印结构体内容

GDB调试C程序时,默认情况下输出很不直观,看起来很费劲.

set print pretty on

3.AddressSanitizerAndDebugger

https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger

b __asan::ReportGenericError

p __asan_describe_address(0x7ffff73c3f80)

4. 条件断点不生效

设置条件断点:

cond 1 $rdi==0x0   //在断点一设置条件断点

b 57 if $rdi==0x0   //在当前文件的57行设置条件断点

有时候你会发现条件断点没有生效,我的解决方案是,先单步运行(n)几行,在运行c.

Greg 在他的blog中也提到过这个问题:http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html

5. Command 阻塞

gdb高级调试技巧_第1张图片

上例中在断点1设置command,  先打印堆栈,再continue, 你会发现gdb 还是会被自动阻塞住。

你需要在gdb的配置文件.gdbinit中添加 set height 0, 会自动避免阻塞问题

6.ptype

Print definition of type TYPE. 可以显示类型的定义。

有个重要参数: \o

/o       print offsets and sizes  of fields in a struct (like pahole)

struct tuv
{
int a1;
char *a2;
int a3;
};
(gdb) ptype /o struct tuv
/* offset | size */ type = struct tuv {
/* 0 | 4 */ int a1;
/* XXX 4-byte hole */
/* 8 | 8 */ char *a2;
/* 16 | 4 */ int a3;
/* total size (bytes): 24 */
}

Notice the format of the first column of comments. There, you
can find two parts separated by the ‘|’ character: the offset, which
indicates where the field is located inside the struct, in bytes, and
the size of the field. Another interesting line is the marker of a hole
in the struct, indicating that it may be possible to pack the struct
and make it use less space by reorganizing its fields.

7 File filename

修改code后,重新build。

gdb无须退出,在gdb中运行 file a.out 命令,新编译的a.out会自动加载到gdb。

8. 调试多线程

info threads

thread 1

set scheduler-locking on/off/step

set schedule-multiple on/off

thead apply all bt

thead apply 1 bt

你可能感兴趣的:(Linux/HPUX,gdb)