gdb quick start (with test cases)

It is more complicated to use gdb to debug the program in the terminal. This article aims to help Xiaobai get started with gdb quickly, so only some important commands are introduced!

The case code is at the end of the article!

1. gdb debugging

1. Compile the source file

gcc -g test.c -o test

2. Start the program

gdb ./test

  The result is as follows:

Explanation: It appears in the result: Reading symbols from ./test03... That means the debugger is started successfully! 

 3. Set breakpoints

  Method 1: Execute the command

b main           //在入口函数出设置一个断点

  Method 2: Execute the command

b .c文件路径:行数

//在test03.c文件的123行设置一个断点
b /root/sql_test/test03.c:123          

  The result is as follows:

4. View breakpoints

  Excuting an order:

info b             //查看断点情况及断点对应编号

  The result is as follows:

5. Delete the breakpoint

  Excuting an order:

//删除指定编号(Num)的断点
d num1 num2…          

//删除1号断点
d 1        

  The result is as follows:

6. Enter debugging

  Excuting an order:

r              //进入调试,并到达第一个断点

  The result is as follows:

7. Single-step debugging

  Method 1: Execute the command

n             //单步调试但不进入具体函数内部

  The result is as follows:

  Description: debugging syntactic sugar --- press Enter directly to execute the previous debugging operation;

  Method 2: Execute the command

s              //单步调试且进入具体函数内部

  The result is as follows: 

8. Jump from one breakpoint to the next

  Excuting an order:

c             //从一个断点跳到下一个断点

  The result is as follows: 

9. Monitor variables

  Excuting an order:

watch 变量名             //用于监视某个变量,一旦变量发生变化就会立即停止

  The result is as follows:

Note: Before monitoring a variable, try to close all breakpoints. When debugging with GDB, when you use the watch command to monitor a variable, it is best to turn off or delete the breakpoint. This is because the watch command triggers a breakpoint every time the value of a variable changes so that you can catch the change of the variable. If you use the watch command with a breakpoint set, GDB will first trigger the breakpoint and then the watch command whenever the value of the variable changes. This can cause unnecessary interruptions in the debugging process, making debugging difficult. Therefore, in order to use the watch command better, it is recommended to turn off or delete the breakpoint before using it.

10. View variables

  Excuting an order:

p 变量名             //查看变量的值

  The result is as follows:

Explanation: As can be seen from the figure above, before viewing the value of variable i, two breakpoints are set, which are lines 124 and 141 of the sample code respectively. The first pi checks the defined value of the global variable i in the code. The second pi looks at the value of i after executing the code on line 141. 

11. View source code

  Excuting an order:

layout src             //弹出源码框且光标停留在第一个断点处,特别说明:快捷键Ctrl+X+A同样可以打开/关闭代码框

  The result is as follows:

Note: At this time, if you use the keyboard scroll key or the mouse, you cannot move the cursor in the code to the next line. Correct way: Enter: n on the command line to move the cursor to the next line, and continue to execute command n through syntactic sugar (Enter key).    

  The result is as follows: 

12. View assembly code

  Excuting an order:

layout asm            //打开汇编意义上的代码且光标停留在第一个断点处,特别说明:快捷键Ctrl+X+A同样可以打开/关闭代码框

  The result is as follows:

Note: At this time, if you use the keyboard scroll key or the mouse, you cannot move the cursor in the code to the next line. Correct way: Enter: si on the command line to move the cursor to the next line, and continue to execute the command si through syntactic sugar (Enter key).

  The result is as follows:

13. From the execution of the entry function mian to the breakpoint, the functions experienced in the middle

  Excuting an order:

bt            //从入口函数mian执行到断点,中间经历的函数(逆序看经历的函数)

  The result is as follows:

14. View registers

  Excuting an order:

info r             //查看寄存器的值

  The result is as follows:

 15. End the current program

  Excuting an order:

k             //结束当前程序,但不会结束调试

  The result is as follows:

16. Exit debugging

  Excuting an order:

quit         //退出调试

  The result is as follows:

 2. Test code

The test code will be posted tomorrow! !

Guess you like

Origin blog.csdn.net/weixin_47156401/article/details/132611696