Article directory
- 1. Background
- 2. Use gdb
-
- 2.1 Generate debug version
- 2.2 Enter gdb
- 2.3 Show source code
- 2.4 Run the program/start debugging
- 2.5 Breakpoint, view breakpoint information
- 2.6 Delete breakpoints
- 2.7 Disabling and enabling power outage
- 2.8 Statement-by-statement execution
- 2.9 Process-by-process execution
- 2.10 Tracking variables
- 2.11 Jump to line X
- 2.12 Jump to next breakpoint
- 2.13 Run the entire specified function
- 2.14 View call stack
- 2.15 Setting the value of a variable
1. Background
1. There are two ways to release programs, debug mode and release mode
2. The default for binary programs produced by Linux gcc/g++ is release mode
3. To use gdb debugging, you must add the -g option when generating a binary program from the source code
2. Use gdb
2.1 Generate debug version
Let’s first write a piece of C language code:
#include <stdio.h>
int main()
{
int i = 0;
for(i = 0; i < 5; i++)
{
printf("hello linux!\n");
}
return 0;
}
Here we write a Makefile normally for automated construction. The executable file generated is the release version if we write it normally, but add the -g option when compiling the code with gcc, so that it is generated It is the debug version of the executable program.
At this time, what we generate is the debug version of the executable file.
2.2 Enter gdb
gdb test_debug //gdb 可执行程序名
2.3 Show source code
list/l line number: Display the binFile source code, and then column down from the last position, 10 lines each time. list / l
Function name: List the source code of a certain function.
list n // 将第n行放在输出的十行中间输出出来
list 函数名 // 从函数名开始打印10行出来
Here list can be used abbreviated as l.
2.4 Run the program/start debugging
r or run: run the program.
If a breakpoint exists, it will jump to the breakpoint line. This is debugging.
2.5 Breakpoint, view breakpoint information
break(b) line number: set a breakpoint on a certain line
break function name: set a breakpoint at the beginning of a certain function
b 行号 // 在某一行打断点
info break :查看断点信息
2.6 Delete breakpoints
delete breakpoints: delete all breakpoints
delete breakpoints n: delete the breakpoint with serial number n
d // delete简写,直接d是删除所有断点
d Num // 删除编号为Num的断点
Here we can find that in a debugging cycle, breakpoints are created and then deleted, and then breakpoints are created again. The breakpoint serial numbers increase linearly, instead of being deleted and then sorted in consecutive ascending order.
2.7 Disabling and enabling power outage
disable breakpoints: disable breakpoints
enable breakpoints: enable breakpoints
2.8 Statement-by-statement execution
s or step: Enter function call
2.9 Process-by-process execution
n or next: single execution (without entering function call)
2.10 Tracking variables
display variable name: track a variable and display its value every time you stop
undisplay: Cancel tracking of all variables previously set
undisplay n: Cancel tracking of the previously set variable numbered n
2.11 Jump to line X
until X line number: jump to X line
2.12 Jump to next breakpoint
continue (or c): execute the program continuously rather than single-step from the current position
2.13 Run the entire specified function
finish: Execute until the current function returns, then wait for the command
2.14 View call stack
breaktrace (or bt): View function calls and parameters at all levels
When other functions are called in our main function, we can use the bt command to see the called stack:
We wrote another func function this time and entered In the func function, check the call stack, then run the func function, and check the callback stack again. The bt command can completely see the stack call situation.
2.15 Setting the value of a variable
set var variable name = val // Set the value of a variable to val
Use:Used when dealing with problems in the judgment logic.