首页 > GDB调试技巧

GDB调试技巧

1. GDB 调试程序

1.Run a program without any argument.

     gdb program

2. Run a program with arguments

    gdb --args program arg1 arg2 ... argN

    or

    gdb program 

    (gdb) r arg1 arg2 ... argN   

3. start with both an executable program and a core file specified

     gdb program core

     (gcore file: Produce a Core File from Your Program)

4. debug a running process,

    specify a process ID as a second argument or use option –p

    the following would attach gdb to process 1234:

    gdb program 1234

    gdb –p 1234

2. 常用命令

r - runRuns the program until a breakpoint or error

c -  (continue) Continues running the program until the next breakpoint or error

fin – (finish) Continue running until just after function in the selected stack frame returns . 直接运行至当前函数结束,比如误按s进入函数A,在函数A中输入fin会直接跳出函数A.          

ret – (returnReturns from current function; 直接退出当前函数,函数内尚未运行的code不会被执行。

j location – (jump) Resume execution at location; it does not change the current stack frame, like run(set $pc = location)

s  - (step) Runs the next line of the program (step into)

s N - Continue running as in step, but do so N times

n – (next) Like s, but it does not step into functions

n N - Continue to the next source N line in the current (innermost) stack frame

p expr - (print) Prints the current value of the variable “expr“

p/f expr - you can choose a different format by specifying ‘/f’, where f is a letter specifying the format。比如p/x expr: 会以16进制的格式打印出expr

bt - (backtrace) Prints a stack trace

bt full N - Print the values of the local variables also of the innermost N frames.  加上full会打印出每个frame的局部变量。N指示打印出几级frame.

- (until) This command is used to avoid single stepping through a loop more than once. u 经常用于跳出当前循环。

u N - Continue running your program until either the specified location is reached,    u 123: 会直接运行到123行code.

         or the current stack frame returns

f - (frame) print current stack frame info.

up n -   Move n frames up the stack; n defaults to 1.        显示上面的第几级的frame的code,注意:只是显示code, pc并没有发送变化,即程序并没有执行。

down n -Move n frames down the stack; n defaults to 1.

q - (quit) Quits gdb

b – (breakpoint) Set breakpoint (see next page)

3. 设置断点-breakpoint

Break into a Function

(gdb) b function

Break into a Function in a given file

(gdb) b filename:function

Break on to a line in a given file

(gdb) b filename:linenum

Break upon matching memory address

(gdb) b *(memory address)

Set a breakpoint with condition cond

(gdb) b <...> if condition

          condition bnum experssion

# Note:

#1: The symbol <...> implies that you can use any form of breakpoints.

Example: break linenum if variable==1

Others:

b N - Puts a breakpoint at line N

b     - Puts a breakpoint at the current line ,当前行设置断点。

b+N - Puts a breakpoint N lines down from the current line

# Note: Break will take place at line in the current source file.

# The current file is the last file whose code appeared in the debug console.

i b     – show all breakpoints

d       – delete all breakpoints, 删除所有断点(不用担心,会有二次确认删除提示)。

d n    – delete specified(n- breakpoints are numbered when created) breakpoint

dis n  – disable specified(n) breakpoint , 去能断点

ena n – enable  specified(n) breakpoint, 使能断点。

tb    - set temporary breakpoint   ,设置临时断点,只作用一次,自动删除。

rb regex - set breakpoints on all functions matching the regular expression regex(grep rule), 设置一类函数断点,函数名符合一定的正则表达式即可。



commands n: You can give any breakpoint a series of commands to execute

when your program stops due to that breakpoint, 每一个断点可以设置对应的命令,断点触发后,这些命令会被自动运行。

save b [filename] : saves all current breakpoint to a file.  在gdb退出之前,可以存储目前断点的信息,以便下次调试时重新加载。

source [filename]: restore breakpoints

 

4. 设置观察点-watchpoint

Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes. The simplest (and the most popular) use of this command is to watch the value of a single variable:

    (gdb) watch drbId

If you watch for a change in a numerically entered address you need to dereference it, as the address itself is just a constant number which will never change. gdb refuses to create a watchpoint that watches a never-changing value:

(gdb) watch 0x600850

Cannot watch constant value 0x600850.

(gdb) watch *(int *) 0x600850

Watchpoint 1: *(int *) 6293584

watch   – write watchpoint ,       写观察点, 当观察点的值被修改时触发。

rwatch  – read watchpoint,      读观察点, 当观察点的值被读取时触发。

awatch  – access watchpoint, 访问(读写)观察点, 被修改或被读取时均会触发。

备注,观察点与硬件中断有关,不能设置的太多。

比如一个struct A, 里面有200个不同类型的数据, 有全局变量 A a, 如果设置 watch a, 会报错,提示: 观察点太多。不同的硬件平台,最多设置观察点的个数不尽相同。另外,如果给局部变量设置 b 一个观察点,则在b作用域失效后,其对应的观察点自动失效。

5. 设置检查点-checkpoint

checkpoint:

Save a snapshot of the debugged program’s current execution state

restart checkpoint-id

Restore the program state that was saved as checkpoint number checkpoint-id

checkpoint 可以保存程序某一时刻的状态信息,用于后续恢复这一时刻。比如,一个很难复现的bug,你费了九牛二虎之力终于复现了,你就可以保存这一检查点,继续调试,如果调试期间错过了某些信息,需要重新调试,你就可以恢复之前保存的观察点,反复调试,犹如时光倒流。

6. 检查变量和设置变量

p x :             Prints current value of variable x.

display x:     Constantly displays the value of variable x, which is shown after every step or pause. 程序没运行一次,都会打印此变量的值。

undispaly x: Removes the constant display

whatis x:      Print the data type of x, 查看变量的类型

info locals:  Print the local variables of the selected frame, 打印当前的堆栈局部变量。

info variables: All global and static variable names, or those matching REGEXP,显示全局变量和静态变量名。也可以模糊匹配,比如:info variables g*: 会显示g开头的全局变量和静态变量名

set x=3 : sets x to a set value (3) , 程序运行时,可以修改变量的值。

x/FMT ADDRESS: Examine memory, 检查内存的值,非常有用。

    FMT is a repeat count followed by a format letter and a size letter.

      Format letters are : o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), 

                                      i(instruction), c(char)  and s(string). 

      Size letters are:       b(byte), h(halfword), w(word), g(giant, 8 bytes)

 

7. 调用程序和CRT

在gdb 调试时,可以在cmd窗口直接调用工程中过的函数和c 语言标准库。

call function() 

call function(x)

call strlen(x)

call sizeof(x)

8. 调试log输出

如果我们要打印的变量结构体非常庞大,这时我们可以将此变量的值保存到文件中,单独打开保存的文件以便更容易查看、。

show logging

        Show the current values of the logging settings.

set logging on

        Enable logging.

set logging off

        Disable logging.

set logging file file

        Change the name of the current logfile. The default logfile is gdb.txt.

set logging overwrite [on|off]

        By default, gdb will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead

 

9.生成Core File

A core file or core dump is a file that records the memory image of a running process and its process status (register values etc.).

generate-core-file [file]   : 生成core file. note: 缩写:gcore [file]

gdb exe_name core.file  : gdb 加载core file, exe_name 为可执行文件的名字。

更多相关:

  • 上篇笔记中梳理了一把 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下即可