GDB
Introduction
GDB is a powerful user-mode program debugging tool for UNIX and UNIX-like environments released by GNU. It is a mainstream tool for C/C++ program debugging.
Installing GDB
In CentOS, use Yum to install GDB.
yum install gdb //CentOS
Usage
- Debug an application.
gdb program
program indicates the executable file to be debugged.
- Debug the core file of an application.
gdb program core
core indicates the file generated after a core dump occurs due to illegal program execution.
- Use GDB to debug a running program.
gdb program $PID
PID indicates the process ID of the program to be debugged.
- GDB debugging command
(gdb) command *args
(gdb) is displayed when accessing the GDB debugging page. command indicates the debugging command to be executed (some commands are abbreviated). *args indicates the parameters required by some debugging commands.
Common parameters are as follows:
Parameter
Description
r
Runs the program until a breakpoint. The program stops running at the breakpoint and waits for the user to enter the next command.
c
Resumes the program running until the next breakpoint.
n
Traces the program in a single step. A function is directly called without stepping into the function body.
s
Debugs the program. It steps into the function if a function is called.
until
Runs the program until it exits the loop.
finish
Runs the program until the current function returns.
call
Calls a function that is visible in the program and passes parameters.
l
Views the source code. list n is used to view the 10 lines before and after n. list func is used to view the function source code.
b n
Adds a breakpoint at line n.
b func
Sets a breakpoint at the beginning of the func() function.
clear n
Clears the breakpoint in line n.