Linux: gdb debugging

gdb

gdb (GNU debugger) is a powerful program debugging tool under Linux released by the GNU open source organization. It can:

  1. Start your program, you can run the program as you want according to your customized requirements
  2. Allows the debugged program to stop at the breakpoint you specify. (The breakpoint can be a conditional expression)
  3. When the program is stopped, you can check what happened in your program at this time
  4. You can change your program to correct the impact of a bug to test other bugs

Debug与Release

What gdb debugs is the Debug version of the executable file, here is a brief mention of the Debug version and the Release version

The executable file generated can have a Debug version and a Release version, and the default version generated by gcc is the Release version

Debug version : the debuggable version, it contains debugging information, and does not make any optimization, which is convenient for developers to debug the program

Release version : The release version, which is often optimized in various ways, so that the program is optimal in terms of code size and running speed, so that users can use it well, and it cannot be executed in a single step.

The release version generated by gcc by default, the Debug version can be generated by adding the -g option when compiling

gcc -g  源文件.c  -o  指定的文件名

The -g option and the -o option are indispensable. If the compilation and linking are completed step by step, the -g option must be added to the compilation stage

Insert picture description here

Use of gdb

1. Use gdb to open the file

gdb  debug版本的可执行文件  #gdb后面跟的必须是debug版本的可执行文件,否则会报错

Insert picture description here

2. View the source code

list                #list可以简写为l,用list默认查看main函数所在的代码
list 源代码文件      #查看指定文件的代码
list 源代码文件:行数 #查看指定文件指定行数周围10行的代码
list 文件名:函数名   #查看指定文件指定函数周围10行的代码
#命令一次只能显示十行,若想查看后面的源码可一直按回车键,直到所有源码显示完

Insert picture description here

Breakpoint management

1. Add a breakpoint

b 行号  #将断点添加到最近一次显示的文件的指定行
b filename:行号  #将断点添加到filename的指定行
b functionname  #将断点添加到指定函数的第一行
#b全称breakpoint

2. View the information of existing breakpoints

info break  #break可以简写为b

Insert picture description here

3. Delete the breakpoint

d 断点号  #删除指定的断点

4. Add conditional breakpoints

This kind of breakpoint is triggered when certain conditions are met, which is more suitable for exception investigation

break line-or-function if (condition)
b src/main.cpp:127 if cnt==10

5. Set the breakpoint to invalid

Setting a breakpoint as invalid will not delete the breakpoint, you can continue to enable it when needed

disable [breakpoints] [range...]  #disable可以简写为dis

breakpoints is the breakpoint number. If nothing is specified, it means all stopping points are disabled. The shorthand command is dis

6, restore the breakpoint

Corresponding to disable is to enable breakpoints that are set as invalid, that is, restore breakpoints

enable [breakpoints] [range...]
enable once [breakpoints]  #仅仅恢复断点一次,当程序停止后, 该断点马上被GDB自动删除

Execution process control

1. Start debugging and start execution

run  #可以简写为r

2. Single step execution

next  #逐过程,简写为n    调用函数的语句中:n直接执行完函数继续执行
step  #逐语句,简写为s    s进入函数一句一句执行
finish  #直接将当前函数执行完成,退出到调用函数的下一条指令的位置

3. Continue execution until the next breakpoint

c

4. Exit the loop body

until   #简写为u

5. Exit debugging

q

Program variable

View the value of a variable

p valname
p &valname
p 数组名  #显示数组中所有元素的值
p 数组指针  #显示的是指针的值

Execute once p will be displayed once, if p is not executed, it will not be displayed

View variable type

ptype valname

Automatically display the value of the variable

display valname  #每执行一次next或step,就显示一次

View the stored value of the memory according to the memory address

x <addr>  #<addr>表示一个内存地址

Display function call stack

bt  #打印当前的函数调用栈的所有信息

Insert picture description here

Guess you like

Origin blog.csdn.net/huifaguangdemao/article/details/108428969