GDB command basic parameters
Author: Onceday date July 29, 2022
The long road has only just begun!
Main reference documents:
1 Introduction
When compiling c/c++ with gcc, you need to use -g to generate debug information.
There are the following types of starting gdb, and program is the executable file that needs to be debugged:
-
gdb <program>
-
gdb <program> core, debug with coredump.
-
gdb <program> <PID>, specify the process ID of the service program when it is running, and gdb will automatically attach it.
Some common parameters can be added at startup:
-
-s, read the symbol table from the specified file
-
-se, read the symbol table information from the specified file and use it in the executable file
-
-c, when debugging, the core file of coredump
-
-d, add a search path for source files, the default search path is the path defined by PATH in the environment variable
You can run shell commands and make commands in the gdb environment:
(gdb) shell ls (gdb) make [args]
2. The environment and variables that can be set when the program is running
2.1 Program running parameters
set sets the input parameters when the program starts running, and show displays the current parameter list.
(gdb) set args 15 "145" 155 145 (gdb) show args Argument list ... is "15 "145" 155 145".
Parameter input for main(int,char*).
2.2 Operating Environment
path sets the running path of the program, show views the running path of the program, set environment sets the environment variables, and show environment views the environment variables.
(gdb) path . Executable and object file path: /home/onceday/new:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin (gdb) show path Executable and object file path: /home/onceday/new:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin (gdb) set environment ONCE=day (gdb) show environment ONCE ONCE = day
Environment variables are used for env in main(argc,argv,env).
2.3 Working Directory
cd switches directories, pwd displays the current directory.
2.4 Program input and output
info terminal shows the terminal mode your program is using
run > outfile uses redirection to control program output
tty /dev/ttyb can refer to the terminal device for writing input and output
3. Pause the program
When the program is suspended, you can use info program to view program information.
There are several ways to pause:
-
Breakpoint
-
WatchPoint
-
CatchPoint
-
Signals
-
Thread Stops
The program can be resumed using the c or continue command.
3.1 Set breakpoints
-
break <function>, stop when entering the specified function, you can add class name and type name
-
break <linenum>, stop at the specified line number
-
break +offset /break -offset, stop at the offset line before or after the current line number
-
break filename:linenum, stop at the linenum line of the source file filename
-
break filename:function, stop at the entry of the function function of the source file filename
-
break *address, stop at the memory address where the program is running
-
break , stop at the next instruction
-
break ... if <condition>, ... indicates the above parameters, which can stop when the condition is established.
3.2 Setting up observation points
The observation point is generally to observe whether the value of an expression (variable is also an expression) changes, and if there is a change, stop the program immediately.
-
watch <expr>, set a watch point for an expression (variable), and stop the program as soon as the value of the expression changes.
-
rwatch <expr>, when the expression (variable), is read, stop the program.
-
awatch <expr>, stop the program when the value of the expression (variable) is read or written.
-
info watchpoints, lists all watchpoints currently set.
3.3 Set Snap Points
You can set up capture points to capture some things when the program is running, such as: loading shared libraries (dynamic link libraries) or C++ exceptions.
catch <event> catch assert -- Catch failed Ada assertions, when raised. catch catch -- Catch an exception, when caught. catch exception -- Catch Ada exceptions, when raised. catch exec -- Catch calls to exec. catch fork -- Catch calls to fork. catch handlers -- Catch Ada exceptions, when handled. catch load -- Catch loads of shared libraries. catch rethrow -- Catch an exception, when rethrown. catch signal -- Catch signals by their names and/or numbers. catch syscall -- Catch system calls by their names, groups and/or numbers. catch throw -- Catch an exception, when thrown. catch unload -- Catch unloads of shared libraries. catch vfork -- Catch calls to vfork.
4. Clear the stop point
Clear stop point:
-
clear, clears all defined stops.
-
clear <[filename:]function>, clears all stop points set on the function.
-
clear <[filename:]function>, clears all stops set on the specified line.
-
delete breakpoints, delete the specified breakpoint, breakpoints is the breakpoint number.
-
delete range..., delete breakpoints in the specified range.
-
delete, deletes all breakpoints.
-
disable breakpoints, stop the specified breakpoints.
-
disable range..., stop breakpoints in the specified range.
-
disable, stop all breakpoints
-
enable [breakpoints] [range...], enable breakpoints
-
enable [breakpoints] once range..., enable it once, after the program stops, it will be automatically disable d by GDB.
-
enable [breakpoints] delete range..., enable it once, after the program stops, it will be automatically deleted by GDB.
You can use condition to modify the stop condition of the breakpoint, that is, to satisfy the following expression:
condition <bnum> <expression> #Modify the stop condition of breakpoint number bnum to expression condition <bnum> #Clear the stop condition with breakpoint number bnum
There is a special maintenance command ignore, which can specify the program to run, ignoring the stop condition several times.
ignore <bnum> <count>
You can also set run commands for stop points:
When the running program is stopped, it can automatically run some other commands.
commands [bnum] ... command-list ... end
Examples are as follows:
break foo if x>0 commands printf "x is %d/n",x continue end
5. Resume program execution and single-step debugging
When the program is stopped, you can use the continue command to resume the program until the program ends, or the next breakpoint arrives. The following three commands:
continue [ignore-count] c [ignore-count] fg [ignore-count]
ignore-count indicates the number of subsequent breakpoints to be ignored.
Single-step tracking step, if there is a function call, it will enter the function, provided that the function is compiled with debug information.
step <count> #count indicates the number of executed instructions, or 1 if not added
Single-step tracking next, if there is a function call, it will not enter the function.
next <count> #count indicates the number of executed instructions, or 1 if not added
The step-mode mode can be turned on, and the program will not be unreliable because there is no debug information when single-step tracking is performed.
set step-mode on set step-mode off
finish runs the program until the current function completes the return, and prints the stack address and return value, that is, the parameter value, and other information when the function returns.
until or u can be single-stepped within a loop body, and this command runs the program until it exits the loop body.
stepi or si, nexti or ni: single-step trace a machine instruction, a program code may be completed by several machine instructions.
6. Signal processing
GDB can handle any kind of signal when debugging a program. When the specified signal is received, stop the running program for you to debug.
handle <signal> <keywords ...>
Signals can be written as: SIGIO-SIGKILL.
Keywords are as follows:
-
nostop, GDB will not stop when a signal is received, but can print out messages.
-
stop, when the program being debugged receives a signal, GDB will stop your program.
-
print, GDB will display a message when the program being debugged receives a signal.
-
noprint, when the program being debugged receives a signal, GDB will not tell you about the received signal.
-
pass noignore, when the program being debugged receives a signal, GDB does not handle the signal. This means that GDB will pass this signal to the debugged program for processing.
-
nopass ignore, when the debugged program receives a signal, GDB will not let the debugged program handle the signal.
You can use info signals and info handle to see which signals are being detected by GDB.
7. Multithreading
Define breakpoints on multiple threads:
break <linespec> thread <threadno> break <linespec> thread <threadno> if ...
linespec specifies the line number of the source program where the breakpoint is set. threadno specifies the ID of the thread.
The ID is assigned by GDB, and the thread information in the running program can be viewed through the "info threads" command.
If thread <threadno> is not specified, it means that the breakpoint is set on all threads.
You can also specify breakpoint conditions for a thread. like
(gdb) break frik.c:13 thread 28 if bartab > lim
When a program is stalled by GDB, all running threads are stalled.
When the program is resumed, all threads are resumed. Even when the main process is being single-stepped.
8. View stack information
When the program is halted, the execution can be confirmed by viewing the stack information:
backtrace bt #function call stack bt <n> #n is a positive integer, indicating that only the stack information of the n layers on the top of the stack is printed bt <-n> #-n represents a negative integer, which means that only the stack information of the n layers below the stack is printed.
View the information of a layer stack:
frame <n> f <n>
n is an integer starting from 0, which is the layer number in the stack. For example, frame 0 represents the top of the stack, and frame 1 represents the second layer of the stack.
up <n> #It means to move n layers to the top of the stack, you can skip n, which means to move up one layer. down <n> #Indicates to move down n layers.
The following commands do not print out the moved stack layer information:
select-frame <n> corresponds to frame Order. up-silently <n> corresponds to up Order. down-silently <n> corresponds to down Order.
The command to output the current stack layer information:
-
You can use info frame or info f to print out more detailed current stack layer information.
-
info args prints the parameter names and their values for the current function.
-
info locals prints out all local variables and their values in the current function.
-
info catch prints out the exception handling information in the current function.
9. Display source code
You need to add the -g parameter when compiling.
list <[function:]linenum> #show the source program around linenum of the program
list <[filename:]function> #Displays the source program of the function whose function name is function.
list #Display the source program following the current line.
list - #Display the source program before the current line
Set the number of lines of source code to display at a time:
set listsize <count> show listsize #View the current listsize setting
The following line selections are also displayed:
list <first> ,<last> #Display the source code from the first line to the last line list , <last>· #Display the source code from the current line to the last line list + #show source code
9.1 Source code search
Source code search can be performed with regular expressions:
forward-search <regexp> search <regexp> #search forward reverse-search <regexp> #Search all
9.2 Specify the path to the source file
directory <dirname [: dirname2 ....]> dir <dirname [: dirname2 ....]> show directories #Displays the defined source file search paths
9.3 View the address of the source code in memory
Print out the memory address of the specified source code at runtime:
info line industry/Function name/filename: line number/filename: function name
To view the machine code of the current execution of the source program, this command will dump the current instructions in memory:
disassemble Function name....
10. View runtime data
print <expr> print /<f> <expr>
<expr> is the expression, which is the language of the program you are debugging (GDB can debug multiple programming languages), and <f> is the output format.
Expressions support the following operators:
-
::, specifies a variable in a file or in a function.
-
@, an operator related to arrays
-
{<type>} <addr>, indicating an object of type type that points to a memory address.
An example is as follows:
p file::variable p function::variable p "f2.c"::x p *array@len #int *array = (int *) malloc (len * sizeof (int));
For a static array, simply print the array name to output the contents of the array.
</f> The formats are as follows:
-
x Displays the variable in hexadecimal format.
-
d Displays variables in decimal format.
-
u Displays unsigned integers in hexadecimal format.
-
o Display variables in octal format.
-
t Display variables in binary format.
-
a Displays the variable in hexadecimal format.
-
c Displays variables in character format.
-
f Displays variables in floating-point format.
An example is as follows:
(gdb) p /f i $24 = 1.41531145e-43
10.1 Use examine(x) to view the value of a memory address
x [/<n/f/u>] <addr>
Optional parameters:
-
n, is a positive integer, indicating the length of the display memory.
-
f, indicates the displayed format, which is shown above.
-
u, indicates the number of bytes requested from the current address. The default is 4 bytes. The following are the preset characters:
-
b means single byte
-
h means double byte
-
w means four bytes
-
g means eight bytes
-
Example:
x/3uh 0x54320
Indicates that the content is read from the memory address 0x54320, h indicates that the unit is two bytes, 3 indicates three units, and u indicates that it is displayed in hexadecimal.
10.2 Automatic display
These variables can be displayed automatically when the program is stopped.
display <expr> display /<fmt> <expr>
Example:
display /i $pc
$pc is the environment variable of GDB, which represents the address of the instruction, /i outputs the assembly instruction.
Remove automatic display:
undisplay <dnums...> delete display <dnums...>
<dnums> are the serial numbers of variables that need to be displayed automatically.
Use info display to observe all currently automatically displayed variables, as well as their serial numbers.
Example of use:
delete display 2,3 #Delete the automatic display variables corresponding to serial numbers 2 and 3 at the same time disable display 2,3 #Automatically display variables corresponding to invalid serial numbers 2 and 3 at the same time enable display 2-5 #At the same time, all corresponding automatic display variables in the range of serial numbers 2-5 are enabled
11. Set the display (print) options
1. Turn on the address output. When the program displays the function information, GDB will display the parameter address of the function. The system is turned on by default
set print address on set print address off
2. Turn on the array display. When the array is displayed after opening, each element occupies a line. If it is not turned on, each element is separated by commas. This option is off by default.
set print array set print array on
3. Set the maximum length of the array display and display the option information of the current print elements.
set print elements <number-of-elements> show print elements
4. When displaying a string, it will stop displaying when it encounters a terminator. This option is off by default.
set print null-stop <on/off>
5. Display content such as structures in a beautiful format.
set print pretty on set print pretty off
6. Set the character display, whether to display in the format of "/nnn"
set print sevenbit-strings <on/off>
7. When setting the display structure, whether to display the union data in it.
set print union <on/off>
8. C++ class settings such as objects, members, virtual functions, etc.
set print object <on/off> #How to handle object pointers
In C++, if an object pointer points to its derived class, if this option is turned on, GDB will automatically display the output according to the rules of virtual method calls. If this option is turned off, GDB will ignore the virtual function table. This option is off by default.
set print static-members <on/off>
This option indicates whether to display the static data members of a C++ object when displaying the contents. Default is on.
set print vtbl <on/off>
When this option is turned on, GDB will use a more regular format to display the virtual function table. It is off by default.
11.2 print history
GDB will record the print records generated in the current running process, and number them in the way of $1, $2, $3... .
Then you can use $1 to re-enter the previous print expression.
11.3 GDB environment variables
You can define your own variables in the debugging environment of GDB to save some running data in the debugger.
A GDB variable can be defined with the following command:
set $foo = *object_ptr
Use the following command to view all currently set environment variables.
show convenience
Environment variables can be used interactively with program variables:
set $i = 0 print bar[$i++]->contents
11.4 View registers
Use the following command to view the registers (excluding floating-point registers):
info registers
The following commands can include floating point registers:
info all-registers
Check the status of the specified register:
info registers <regname ...>
It can also be accessed using the print command ($+register name):
print $sp