Before debugging
If you want to debug a program in GDB, you need to add debugging information when compiling.
The method added in GCC
GCC -g a.c -o a.exe
Or provide debugging information more in line with GDB below
GCC -ggdb a.c -o a.exe
Run process
command |
effect |
-start |
Start executing the program and stop at the first sentence of the main function |
-continue、-c |
Continue execution from current location |
-run、-r |
Execute the program to the next breakpoint or end of the program |
-next、-n |
Under an execution, do not enter other functions |
-step、-s |
The next step, will enter other functions |
-set args <parameters> |
Specify running parameters |
-show arge |
View the parameters that have been set |
-path <path> |
Set the path to run the program |
-show path |
View the set path |
Variables and stack operations
command |
effect |
-info <variable name>, -i <variable name> |
View local variable values |
-info locals、-i locals |
View the values of all local variables |
-set var <variable name>=<value> |
Modify the value of the current variable |
-print <expression>, -p <expression> |
View the value of an expression |
-display <variable name> |
Track a variable, display this variable every time you stop |
-x /<integer> <variable name> |
View the contents of integer bytes starting from the memory unit pointed to by the variable name |
-backtrace、bt |
View functions and their parameters |
-frame <stack level (integer)>, -f <stack level> |
Switch to the specified stack, and then you can view variables in other locations, such as global variables and another local variable |
Breakpoint related operations
command |
effect |
-break <number of lines, function name>, -b <number of lines, function name> |
Set a breakpoint on a line or function |
-break <number of lines, function name> if <expression> |
Set a breakpoint when the expression is established in a certain line or function |
-info breakpoints |
View all breakpoints and their breakpoint numbers |
-delete breakpoints <breakpoint number> |
Remove breakpoint |
-disable breakpoints <breakpoint number> |
Disable breakpoint |
-enable breakpoints <breakpoint number> |
Enable breakpoint |
-watch <variable name> |
Interrupt when the variable changes |
-info watchpoints、i watchpoints |
View all watch variables |