首页 > gdb高级调试技巧

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 阻塞

上例中在断点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

更多相关:

  • 上篇笔记中梳理了一把 resolver 和 balancer,这里顺着前面的流程走一遍入口的 ClientConn 对象。ClientConn// ClientConn represents a virtual connection to a conceptual endpoint, to // perform RPCs. // //...

  • 我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做。。。 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑,算是搞成功了。 实验平台:Windows、VS2015、Tensorflow1.8 api、Python3.6 具体的流程为:...

  • Path Tracing 懒得翻译了,相信搞图形学的人都能看得懂,2333 Path Tracing is a rendering algorithm similar to ray tracing in which rays are cast from a virtual camera and traced through a s...

  • configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY][NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 我遇到的是 configure_file(config/config.in ${CMAKE_SOURCE_DIR}/...

  •     直接复制以下代码创建一个名为settings.xml的文件,放到C:UsersAdministrator.m2下即可