Table of contents
We usually debug C/C++ code mostly in VS under Windows platform. In LInux, we usually use
gdb
it to debug code. Although we rarely debug code on LInux, gdb is rarely used in actual use. But we must understand it in order to face various environments.The purpose of this article: to be able to carry out basic debugging of single-process programs, that is, to use the basic debugging functions of VS 2019 and VS 2022, all of which are implemented using gdb on Linux.
This article will use the vim editor , make/Makefile , and gcc/g++ in the Linux environment . If you are not familiar with these three tools, you can click the link below to learn together with this article.
vim editor
make/Makefile
gcc/g++
1.debug and release
Before learning to use gdb debugging under Linux, we need to master the following three foreshadowing knowledge about debug and release modes
-
There are two ways to release the program, debug mode and release mode
Debug mode : This mode is a mode for programmers to write programs and debug codes. The executable program generated in this mode contains debugging information that is larger than the executable program generated in release mode .
Release mode : There is no debugging information, the program is optimized, the size of the program is reduced, and it is more suitable for users to use. The executable program generated under this mode is what the company will eventually push to the market. The testers in the company will test whether it is perfect, and then push it to the market after passing the test.
- Note: You can debug in debug mode, but not in release mode.
The picture below is the selection diagram of the two modes under VS 2019. You can choose which mode you need.
Then how to convert the two modes under Linux? -
Under Linux, the binary executable program produced by gcc/g++ is in release mode by default
- That is, under Linux, the executable program generated by using conventional instructions is in release mode , and cannot be debugged
//常规生成 gcc -o 可执行文件名 源代码文件 g++ -o 可执行文件名 源代码文件
-
-g
To use gdb debugging, you must add options when using gcc/g++ to generate binary programsgcc -o 可执行文件名 源代码文件 -g g++ -o 可执行文件名 源代码文件 -g
Next, we edit the test code under the following test.c file to test.
Note: the 7th line of code defines variables in the for loop i
. This writing method is supported in c99, and the gcc/g++ editor I use does not support it. This way of writing needs to be added after executing the command -std=c99
,
The Makefile is as follows:
If you do not know how to use the Makefile, you can directly use the following two instructions to test
gcc -o MyTest-debug test.c -g -std=c99 //生成debug模式的可执行文件
gcc -o MyTest-release test.c -std=c99 //生成release模式的可执行文件
First , test whether debugging is possible in release mode.
As shown in the above figure, the red box indicates that no debugging symbols have been found and debugging cannot be performed.
Second , test whether debugging is possible in debug mode
No error message is displayed, and it can be debugged normally
Finally: Observe the volume of the two executable files, it is obvious that debug is larger than release
expand
We can use the readlef
command to view the information under the two executable files, as follows:
We can use this command to view the debug information under the two files, as follows (the grep command is a search string command):
The debug information is found in the MyTest-debug file, but there is nothing in MyTest-release.
2. How to use gdb to debug
The core job of the debugger: locating the problem
Next, we briefly introduce how to use gdb to debug under Linux. This paragraph is explained step by step, and the summary of commands under gdb is placed in the next paragraph.
If you don’t have properties for gdb, just follow the steps
-
enter debug
Use the
gdb file
command to enter the debug mode, as shown below
-
show code to debug
l/list 行号
: Display the source code of the file, starting from the input line number, and list ten lines downward. Generally, when we start debugging, we start from thel 1
beginning to view, and then enter thel
orEnter
key to get the code from the last search position.
Note: After entering the gdb command, theEnter
key will be recorded. That is, after inputting the command, inputting the Enter key will repeat the above action. The following directives also follow this rule.- After entering the gdb mode, use it alone
l
to view, the displayed code is random
-
set breakpoint
-
break/b 行号
: Set a breakpoint on a certain lineAs shown below, set a breakpoint on line 19
-
break/b 函数名
: Set a breakpoint at the beginning of a functionAs shown in the figure below, set a breakpoint at the addTestTop function
5 is the line number of the first line of code inside the addTestTop function.
-
break/b 文件名:函数名
: When there are multiple files, you can use this method to set breakpoints -
info break/b
: View breakpoint informationWhen the program is running, it will stop at the breakpoint, and when you view the breakpoint for the next time, it will display the number of times it stopped at the breakpoint.
Note: The serial number of the gdb breakpoint is increasing, and it will continue to increase after each use, until it is turned off and turned on, and starts from 1 again
-
-
run the program
-
r/run: run the program
- If there is a breakpoint, run to the breakpoint and stop
- If there is no breakpoint, run directly
- If there is a breakpoint, run to the breakpoint and stop
-
Note: A run ends after all the codes in all main functions run. After stopping at a breakpoint, performing other viewing instructions will not affect this debugging.
-
-
There are breakpoints, after running, execute statement by statement
-
s/step
: When a function is encountered, enter the function
Note: After entering other functions, a call chain will be generated, that is, one function calls another function, and then calls other functions.
Can be viewed by
bt
command -
-
There are breakpoints, after running, run process by process
n/next
That is, when a function is encountered, the next instruction is directly executed without entering the function
- Remember the
Enter
button execute the command executed last time? If there are too many contents to be debugged, just press onces
orn
thenEnter
press
-
delete breakpoint
-
delete /d breakpoints: delete all breakpoints
-
delete/dn: delete the breakpoint with sequence number n
-
-
Turn breakpoints on and off
Used when we don't want to use a breakpoint and don't want to delete the breakpoint
-
disable breakpoint breakpoint number: turn off the breakpoint
-
enable breakpoint breakpoint number: enable breakpoint
-
-
view variables
-
p 变量名
: This instruction is input when it needs to be checked, and it will not affect debugging. As follows, after I enter the addTestTop function in debugging, I execute a few statements and check the result of the sum variable. At this time, the sum is 6. -
The efficiency of viewing in this way is too low. When we use debugging in VS, we execute the corresponding variable once and change accordingly. You can view it directly. This method needs to be called by the user itself, which is inconvenient. We can use the following method
-
-
Track View Variables
-
display 变量名
: After execution, the variable will be displayed after each statement is executed -
Can display custom types such as built-in types and structures, stl
-
-
untrace on breakpoint
-
undisplay 序号
: Use when you don't want to track a variable
-
-
Jump to the specified line
until 行号
: Jump to line X, there is a loop in our code above, we can use this instruction to jump out of the loop
-
run the whole function
finash
: Execute after entering a function, only execute this function, and stop after execution
-
From one breakpoint to the next
-
continue/c
: run directly to the next breakpoint -
After combining
util、finish、continue
, the basic debugging function can be realized
-
-
exit gdb
q/quit
: If you are debugging, you will be prompted whether you need to exit
3. Instruction set
gdb binFile
Exit: ctrl + d
or quit/q
Debug command:
- list/l line number: display the source code of binFile, and then go to the following position from the last position, and list 10 lines each time.
- list/l function name: List the source code of a function.
- r or run: Run the program.
- n or next: single execution.
- s or step: enter the function call
- break(b) line number: set a breakpoint on a certain line
- break function name: set a breakpoint at the beginning of a function
- info break : View breakpoint information.
- finish: Execute until the current function returns, then stand down and wait for the command
- print§: print the value of the expression, the value of the variable can be modified or the function can be called through the expression
- p variable: Print variable value.
- set var: modify the value of a variable
- continue (or c): continuous rather than single-step execution of the program from the current position
- run (or r): Execute the program continuously from the beginning instead of single-step
- delete breakpoints: delete all breakpoints
- delete breakpoints n: delete the breakpoint with sequence number n
- disable breakpoints: disable breakpoints
- enable breakpoints: enable breakpoints
- info (or i) breakpoints: See which breakpoints are currently set
- display variable name: track and view a variable, and display its value every time you stop
- undisplay: undisplay those variables that were previously set
- until X line number: jump to X line
- breaktrace (or bt): View function calls and parameters at all levels
- info(i) locals: View the value of local variables in the current stack frame
- quit: quit gdb