GDB (GNU Debuger) is a powerful debugger in Linux. GDB allows you to view the internal structure of the program, print variable values, set breakpoints, single-step debugging source code, run-time modification of variable values, path tracking, thread switching, display assembly code and so on.
Compile
To debug with GDB, you need to include the-G option when compiling the program. When this option is set, GCC will add the debug information "wedge" to the program, and GDB can use these wedges to interact with the program.
Gcc-g Myfile.c-o Myfile_debug
Debugging
- Loading program
You can use the GDB program method, or start with the file program after GDB starts.
- Set input parameters and display
the Set args command sets the parameters sent to the program, and the show args command displays its default parameters.
- Print Code Content
list Line1,line2 Prints the code, and does not enter parameters to print from the current line. Printing data with Print
- Breakpoint
Set Breakpoint: 1. Break line number; 2. break function name; 3. Break line number or function if condition: This is a conditional breakpoint setting command, and if the condition is true, the program stops when it reaches the specified line or function. such as B if i==2
Show Breakpoint Information: info break
Deletes the specified breakpoint: The Delete breakpoint breakpoint number. If no number argument is taken, all breakpoints are deleted
Forbidden Breakpoint: Disable Breakpoint breakpoint number
Allow breakpoints: Enable breakpoint breakpoint number
Clear breakpoint: Clear source code line number. Disposable Clear Direct
- Run the program
run the prepared program using the Run command, followed by the parameters passed to the program. If you use the Run command without parameters, GDB uses the parameters of the previous Run command again.
- Detecting variable types
Print the type of an array or variable: Whatis variable name
View a detailed definition of the structure with the PTYPE variable name
- Single-Step debugging
a single-step trial of next or Step commands will enter the inside of the function body. If you have entered inside the function and want to exit the function to return to the calling function, you can use the command finish.
- Setting the detection point
the command display can show the value of a variable, and the value of the set variable is displayed (scoped) at the end or when a breakpoint is encountered.
- Call path (stack information)
The BackTrace command can print the function's call path, providing forward tracking functionality. Abbreviation BT
- Multithreading thread
info thread lists the thread number in the current process and enters the thread that needs to be debugged with the thread ID
- Compilation disassemble
disassemble function name, printing the assembly code of the specified function
Common commands
Using the GDB Debug program