[Linux] Linux code debugger -- gdb

Insert image description here

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;
}

Insert image description here

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.
Insert image description here
Insert image description here
At this time, what we generate is the debug version of the executable file.

2.2 Enter gdb

gdb test_debug //gdb 可执行程序名

Insert image description here

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.
Insert image description here

2.4 Run the program/start debugging

r or run: run the program.

Insert image description here

If a breakpoint exists, it will jump to the breakpoint line. This is debugging.

Insert image description here

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 行号 // 在某一行打断点

Insert image description here

info break :查看断点信息

Insert image description here

2.6 Delete breakpoints

delete breakpoints: delete all breakpoints
delete breakpoints n: delete the breakpoint with serial number n

d // delete简写,直接d是删除所有断点

Insert image description here

d Num // 删除编号为Num的断点

Insert image description here

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

Insert image description here

enable breakpoints: enable breakpoints

Insert image description here

2.8 Statement-by-statement execution

s or step: Enter function call

Insert image description here

2.9 Process-by-process execution

n or next: single execution (without entering function call)

Insert image description here

2.10 Tracking variables

display variable name: track a variable and display its value every time you stop

Insert image description here

undisplay: Cancel tracking of all variables previously set

undisplay n: Cancel tracking of the previously set variable numbered n

Insert image description here
Insert image description here

2.11 Jump to line X

until X line number: jump to X line

Insert image description here

2.12 Jump to next breakpoint

continue (or c): execute the program continuously rather than single-step from the current position

Insert image description here

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.
Insert image description here

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.

Guess you like

Origin blog.csdn.net/Ljy_cx_21_4_3/article/details/134191255