当前位置:网站首页>GDB command basic parameters

GDB command basic parameters

2022-08-10 03:20:00 Once_day

GDBCommand base parameters

Author:Onceday date2022年7月29日

漫漫长路,才刚刚开始!


主要参考文档:

1.引言

使用gcc编译c/c++时,需要使用-g来生成调试信息.

启动gdb有以下几种,programThat is, the executable file that needs to be debugged:

  • gdb <program>

  • gdb <program> core,配合coredump进行调试.

  • gdb <program> <PID>,Specifies the runtime process of the service programID,gdb会自动attach上去.

Some commonly used parameters can be added at startup:

  • -s,Read the symbol table from the specified file

  • -se,Read symbol table information from the specified file,并把他用在可执行文件中

  • -c,调试时,coredump的core文件

  • -d,加入一个源文件的搜索路径,The default search path is in the environment variablePATH所定义的路径

gdbrun in the environmentshell命令以及make命令:

(gdb) shell ls 
(gdb) make [args]

2. 程序运行时,Settable environment and variables

2.1 程序运行参数

setSet the input parameters when the program starts running,showDisplays the current parameter list.

(gdb) set args 15 "145" 155 145
(gdb) show args
Argument list ...  is "15 "145" 155 145".

用于main(int,char*)的参数输入.

2.2 运行环境

path设定程序的运行路径,show查看程序的运行路径,set environment 设置环境变量,show environment查看环境变量.

(gdb) path .
Executable and object file path: /home/onceday/new:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
(gdb) show path
Executable and object file path: /home/onceday/new:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
(gdb) set environment ONCE=day
(gdb) show environment ONCE
ONCE = day

环境变量用于main(argc,argv,env)中的env.

2.3 工作目录

cd切换目录, pwd显示当前目录.

2.4 程序的输入输出

info terminal Displays the terminal mode used by your program

run > outfile 使用重定向控制程序输出

tty /dev/ttyb Can refer to a terminal device that writes input and output

3.暂停程序

当程序暂停时,可用info program来查看程序信息.

There are several ways to pause as follows:

  • 断点(Breakpoint)

  • 观察点(WatchPoint)

  • 捕捉点(CatchPoint)

  • 信号(Signals)

  • 线程停止(Thread Stops)

可使用c或者continuecommand to resume program operation.

3.1 设置断点
  1. break <function>,在进入指定函数时停住, Class and type names can be added

  2. break <linenum>,在指定行号停住

  3. break +offset /break -offset,在当前行号的前面或后面offset行停住

  4. break filename:linenum,在源文件filename的linenum行处停住

  5. break filename:function,在源文件filename的function函数的入口处停住

  6. break *address,在程序运行的内存地址处停住

  7. break ,Stop at the next command

  8. break ... if <condition>,...Indicates the above parameters,This can stop when the conditions are met.

3.2 设置观察点

An observation point is generally to observe an expression(变量也是一种表达式)的值是否有变化,如果有变化,Stop the program now.

  1. watch <expr>,为表达式(变量),设置观察点,一旦表达式值有变化,Stop the program now.

  2. rwatch <expr>,当表达式(变量),被读时,停住程序.

  3. awatch <expr>,当表达式(变量)when the value is read or written,停住程序.

  4. info watchpoints,列出当前所设置了的所有观察点.

3.3 设置捕捉点

Snap points can be set to capture something while the program is running,如:载入共享库(动态链接库)或是C++的异常.

catch <event>
catch assert -- Catch failed Ada assertions, when raised.
catch catch -- Catch an exception, when caught.
catch exception -- Catch Ada exceptions, when raised.
catch exec -- Catch calls to exec.
catch fork -- Catch calls to fork.
catch handlers -- Catch Ada exceptions, when handled.
catch load -- Catch loads of shared libraries.
catch rethrow -- Catch an exception, when rethrown.
catch signal -- Catch signals by their names and/or numbers.
catch syscall -- Catch system calls by their names, groups and/or numbers.
catch throw -- Catch an exception, when thrown.
catch unload -- Catch unloads of shared libraries.
catch vfork -- Catch calls to vfork.

4.Clear the stop point

Clear the stop point:

  1. clear,清除所有的已定义的停止点.

  2. clear <[filename:]function>,清除所有设置在函数上的停止点.

  3. clear <[filename:]function>,清除所有设置在指定行上的停止点.

  4. delete breakpoints,删除指定的断点,breakpoints为断点号.

  5. delete range...,Delete breakpoints in the specified range.

  6. delete,删除所有断点.

  7. disable breakpoints,Stop at the specified breakpoint.

  8. disable range...,Stops on breakpoints within the specified range.

  9. disable,Stop all breakpoints

  10. enable [breakpoints] [range...],使能断点

  11. enable [breakpoints] once range...,使能一次,程序停止后 ,会被GDB自动disable.

  12. enable [breakpoints] delete range...,使能一次,程序停止后,会被GDB自动删除.

可以用conditionModify the stop condition of the breakpoint,That is, the following expression is satisfied:

condition <bnum> <expression> #Modify the breakpoint numberbnum的停止条件为expression
condition <bnum>  #清除断点号为bnum的停止条件

More special maintenance commandsignore,Program can be specified to run,忽略停止条件几次.

ignore <bnum> <count>

You can also set run commands for stop points:

When the running program is stopped,可以让其自动运行一些别的命令.

commands [bnum]
... command-list ...
end

实例如下:

break foo if x>0
commands
printf "x is %d/n",x
continue
end

5. 恢复程序运行和单步调试

when the program is stopped了,可以用continue命令恢复程序的运行直到程序结束,Or the arrival of the next breakpoint.The following three commands:

continue [ignore-count]
c [ignore-count]
fg [ignore-count]

ignore-count表示忽略其后的断点次数.

单步跟踪step,如果有函数调用,会进入该函数,The premise is that this function is compiled withdebug信息.

step <count> #countIndicates the number of executed instructions,不加则为1

单步跟踪next,如果有函数调用,不会进入该函数.

next <count> #countIndicates the number of executed instructions,不加则为1

可以打开step-mode模式,在进行单步跟踪时,程序不会因为没有debuginformation is unreliable.

set step-mode on
set step-mode off 

finish运行程序,直到当前函数完成返回,And print the stack address and the return value, that is, the parameter value and other information when the function returns.

until 或 u Can be single-stepped within a loop body,这个命令可以运行程序直到退出循环体.

stepi或si,nexti或ni:单步跟踪一条机器指令,一条程序代码有可能由数条机器指令完成.

6.信号处理

GDBYou can handle any kind of signal while debugging your program.When the specified signal is received,Take it and stop the running program,以供你进行调试.

handle <signal> <keywords ...>

Signals can be written as :SIGIO-SIGKILL.

Keywords are as follows:

  • nostop,收到信号时,GDBwon't stop,But the message can be printed out.

  • stop,当被调试的程序收到信号时,GDB会停住你的程序.

  • print,当被调试的程序收到信号时,GDB会显示出一条信息.

  • noprint,当被调试的程序收到信号时,GDB不会告诉你收到信号的信息.

  • passnoignore,当被调试的程序收到信号时,GDB不处理信号.这表示,GDB会把这个信号交给被调试程序会处理.

  • nopassignore,当被调试的程序收到信号时,GDB不会让被调试程序来处理这个信号.

可以使用info signalsinfo handle查看有哪些信号在被GDB检测中.

7. 多线程

Define breakpoints on multiple threads:

break <linespec> thread <threadno>
break <linespec> thread <threadno> if ...

linespec指定了断点设置在的源程序的行号.threadno指定了线程的ID.

ID由GDB分配的,可以通过“info threads”命令来查看正在运行程序中的线程信息.

如果不指定thread <threadno>,则表示断点设在所有线程上面.

还可以为某线程指定断点条件.如

(gdb) break frik.c:13 thread 28 if bartab > lim

当程序被GDB停住时,所有的运行线程都会被停住.

And when the resume program is running,所有的线程也会被恢复运行.那怕是主进程在被单步调试时.

8. 查看栈信息

when the program is stopped,The execution can be confirmed by viewing the stack information:

backtrace
bt    #函数调用栈
bt <n> #n是正整数,表示只打印栈顶上n层的栈信息
bt <-n> #-nRepresents a negative integer,表示只打印栈底下n层的栈信息

View information about a stack at a certain level:

frame <n>
f  <n>

n是一个从0开始的整数,是栈中的层编号,比如,frame 0表示栈顶,frame 1表示栈的第二层.

up <n>  #表示向栈的上面移动n层,可以不打n,表示向上移动一层.
down <n> #表示向下移动n层.

The following commands do not print out the moved stack level information:

select-frame <n> 对应于 frame 命令.
up-silently <n> 对应于 up 命令.
down-silently <n> 对应于 down 命令.

Command to output current stack layer information:

  • 可以使用info frameinfo fPrint out more detailed current stack layer information.

  • info argsPrint the parameter names and their values ​​for the current function.

  • info locals打印出当前函数中所有局部变量及其值.

  • info catch 打印出当前的函数中的异常处理信息.

9.显示源代码

编译时需要加上-g参数.

list <[function:]linenum>    #显示程序第linenum行的周围的源程序
list <[filename:]function>    #显示函数名为function的函数的源程序.
list               #显示当前行后面的源程序.
list -            #显示当前行前面的源程序

设置一次显示源代码的行数:

set listsize <count>

show listsize    #查看当前listsize的设置

The following line selections are also displayed

list <first> ,<last>    #显示从first行到last行之间的源代码
list , <last>·        #显示从当前行到last行之间的源代码
list +                #往后显示源代码
9.1 源码搜索

Source code search can be performed with regular expressions:

forward-search <regexp>
search <regexp>    #向前面搜索

reverse-search <regexp>  #全部搜索
9.2 指定源文件的路径
directory <dirname [: dirname2 ....]>
dir <dirname [: dirname2 ....]>


show directories #显示定义了的源文件搜索路径
9.3 查看源代码在内存中的地址

Print out the memory address of the specified source code at runtime:

info line 行业/函数名/文件名:行号/文件名:函数名

查看源程序的当前执行时的机器码,这个命令会把目前内存中的指令dump出来:

disassemble 函数名....

10. 查看运行时数据

print <expr>
print /<f> <expr>

<expr>是表达式,是你所调试的程序的语言的表达式(GDB可以调试多种编程语言),<f>是输出的格式.

Expressions can support the following operators:

  1. ::,Specify a variable in a file or in a function.

  2. @,An operator related to arrays

  3. {<type>} <addr>,表示一个指向内存地址的类型为type的一个对象.

示例如下:

p file::variable
p function::variable
p "f2.c"::x
p *[email protected]  #int *array = (int *) malloc (len * sizeof (int));

Static arrays directlyprintThe array name can output the contents of the array.

</f>格式有以下几种:

  • x 按十六进制格式显示变量.

  • d 按十进制格式显示变量.

  • u 按十六进制格式显示无符号整型.

  • o 按八进制格式显示变量.

  • t 按二进制格式显示变量.

  • a 按十六进制格式显示变量.

  • c 按字符格式显示变量.

  • f 按浮点数格式显示变量.

示例如下:

(gdb) p /f i
$24 = 1.41531145e-43
10.1 使用examine(x)查看内存地址的值
x [/<n/f/u>] <addr>

可选参数:

  • n,是一个正整数,表示显示内存的长度.

  • f,表示显示的格式,which is shown above.

  • u,表示从当前地址往后请求的字节数.默认是4个bytes.The following are some preset characters:

    1. b表示单字节

    2. h表示双字节

    3. w表示四字节

    4. g表示八字节

示例:

x/3uh 0x54320

表示从内存地址0x54320读取内容,h表示以双字节为一个单位,3表示三个单位,u表示按十六进制显示.

10.2 自动显示

当程序停住时,These variables can be displayed automatically.

display <expr>
display /<fmt> <expr>

示例:

display /i $pc  

$pc是GDB的环境变量,表示着指令的地址,/i 输出汇编指令.

删除自动显示:

undisplay <dnums...>
delete display <dnums...>

<dnums>is the serial number of the variable that needs to be displayed automatically.

使用info displayAll currently automatically displayed variables can be observed,and their serial numbers.

使用示例:

delete display 2,3  #Also delete the serial number2和3The corresponding automatic display variable
disable display 2,3  #Also invalidate the serial number2和3The corresponding automatic display variable
enable display 2-5   #Also enable serial number2-5All corresponding variables in the scope are automatically displayed

11.设置显示(print)选项

1.打开地址输出,当程序显示函数信息时,GDB会显出函数的参数地址.系统默认为打开的

set print address on
set print address off

2.打开数组显示,打开后当数组显示时,每个元素占一行,如果不打开的话,每个元素则以逗号分隔.这个选项默认是关闭的.

set print array
set print array on

3.Set the maximum length of the array display and display currentprint elements的选项信息.

set print elements <number-of-elements>
show print elements

4.when displaying strings,遇到结束符则停止显示,这个选项默认为off.

set print null-stop <on/off>

5.Displays things like structs in a pretty format.

set print pretty on
set print pretty off

6.设置字符显示,是否按“/nnn”的格式显示

set print sevenbit-strings <on/off>

7.设置显示结构体时,是否显式其内的联合体数据.

set print union <on/off>

8.对象、成员、虚函数等C++类设置

set print object <on/off>   #How to handle object pointers

在C++中,如果一个对象指针指向其派生类,如果打开这个选项,GDB会自动按照虚方法调用的规则显示输出,如果关闭这个选项的话,GDB就不管虚函数表了.这个选项默认是off.

set print static-members <on/off>

这个选项表示,当显示一个C++对象中的内容是,是否显示其中的静态数据成员.默认是on.

set print vtbl <on/off>

当此选项打开时,GDB将用比较规整的格式来显示虚函数表时.其默认是关闭的.

11.2 print历史记录

GDBWill record the current running process generatedprint记录,并以$1$2$3…numbered this way.

那么就可以使用$1This way to re-enter the previous oneprint表达式.

11.3 GDB环境变量

可以在GDB的调试环境中定义自己的变量,用来保存一些调试程序中的运行数据.

Use the following command to define oneGDB变量:

set $foo = *object_ptr

Use the following command to view all currently set environment variables.

show convenience

Environment variables can be used interactively with program variables:

set $i = 0
print bar[$i++]->contents
11.4 查看寄存器

Use the following command to view the register status(Floating point registers are not included):

info registers

The following commands can include floating point registers:

info all-registers

查看所指定的寄存器的情况:

info registers <regname ...>

也可以使用print命令访问($+寄存器名):

print $sp

本文收集整理于互联网,仅供交流学习之用!

原网站

版权声明
本文为[Once_day]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100156450121.html