GDB is the program debugging tool under the Unix/linux system, compared with the graphical debugging tools of the IDE (such as VS, Eclipse, etc.), GDB has a shortage of breakpoints and trace display, but it is richer in some aspects than the graphical debugging tools.
GDB Debugging Prerequisites
If you want the program to be debugged by GDB, you need to specify the-G option when compiling the program. GDB Debugging and program release optimization, there is also a level, can be set manually. The default GDB level is 2, when the GDB debug level is set to 3, you can during the GDB debugging process macro Expand/exp to the macros in the program to expand the definition.
Graphical Tool for GDB debugging
GDB itself does not have a graphical interface, which makes it difficult to easily know the current context of the program in debugging. To overcome this weakness, GDB has added a number of graphical toolsets, such as Gdb-tui. Use the $ gdb-tui program name to enter GDB's graphical debug state. Or execute the GDB program name into command-line debugging, and then execute CTRL + X + A to turn on TUI mode, CTRL + X + A to turn off TUI mode.
Common directives for GDB debugging
Pre-Commissioning Preparation
1. Set the operating parameters
Set args sets run-time parameters, set Args-ip "192.168.0.1"-port 6555
Show args can display run-time parameters
2. Operating Environment
Path to set the program's Running paths
Show paths shows the running path of the program
Set Environment VarName = value allows setting environment variables
Show environment varname can display environment variables
3. Working directory
CD equivalent to the shell CD
PWD Displays the directory where you are currently located
4. Input and output of the program
Info Terminal display the terminal mode used by the program
Using the output of the redirect control program, such as R (Run) > outfile
Common commands
| GDB Common Commands |
Format |
Meaning |
Shorthand |
| List |
List [Start, end] |
Listing code listings for a file |
L |
| Prit |
Print variable Name |
Print variable Contents |
P |
| Break |
break [line number or function name] |
Set breakpoints |
B |
| Continue |
Continue [Start, end] |
Continue running |
C |
| Finish |
Finish |
Continue running until the program ends |
|
| Info |
Info variable Name |
List information |
I |
| Next |
Next |
Next line |
N |
| Step |
Step |
Enter function (Step into) |
S |
| Until |
Until line number |
Run to the specified row |
U |
| Display |
Display variable name/expression
|
Display the value of a parameter or an expression |
|
| File |
File file name (can be an absolute path and a relative path) |
Loading files |
|
| Run |
Run args |
Run the program |
R |
| Enable |
Enable B (NUM)/display (num) |
Make breakpoint/Display valid |
|
| Disable |
Disable B (num)/ display (num) |
Make breakpoint/Display invalid |
|
| Clear |
Clearn filename:linenumber (filename null indicates the current file) |
Deletes breakpoints that are set on a specific source file, on a specific line. |
|
| BackTrace |
Bt |
View the stack frame of a function call |
Bt |
| Enter |
|
Repeat the previous command |
|
Info break Displays the list of current breakpoints, including the number of times the breakpoint was reached.
Info files Displays detailed information about the files being debugged.
Info func Displays all the function names.
Info Local Displays the local variable information in the function.
Info prog shows the execution state of the program being debugged.
Info var displays all the global and static variable names.
Kill terminates the program that is being debugged.
L (list) Filename:line_number the line used to display the specified file
Breakpoint
B (Break) line number adds a breakpoint on the specified line
B (Break) function name add a breakpoint before you make a function
B (break) FileName: line number adds a breakpoint before the specified line in the specified file
B (break) FileName: The function name adds a breakpoint before the specified function of the specified file
B (Break) *address stops when the program runs to the specified memory address
B (Break) ... if ... Conditional breakpoint, such as break 337 if i = = 0
Info b Lists all the breakpoint information
D Delete all breakpoints, D number
Condition Break-num Conds When the conds condition is met, Break-num is triggered. Conditions that can be used to set or modify breakpoints.
Condition Break-num makes Break-num's breakpoint unconditional.
Observation point
Watch Stop the program as soon as the expression value changes
Rwatch when an expression/variable is read, stop the program
Awatch Stop a program when an expression/variable is read or written
Info watchpoints lists all the points of observation
Observer deletion, using the ordinal of the D observer (Info B can list all Breakpoints + observation points + case)
Set the Run command for the Stop point (useful function)
commands [Break-num]
.... command list ....
End
Specify a list of commands for breakpoint Break-num, and GDB executes the commands in the list in turn when stopped at the stop point
Debugging child processes
1. Set-follow-fork-mode
GDB can set up multi-process debugging for programs that produce child processes using fork/vfork.
Set-follow-fork-mode Parent continues to debug the parental process after fork/vfork
Set-follow-fork-mode Child continues to debug the subprocess after Fork/vfork
(GDB) Set Follow-fork-mode child
(GDB) Break child process line number
2. Attach (gdb debugging a process, using attach another process PID, debugging another process, after debugging, detach release another process, continue to debug this process)
The attach command can be bound to an external program for debugging, the PID of the attach process
Suppose you want to debug a subprocess of a process proc1, use Ps-ef |grep proc1 to find the PID of the child process, and then (GDB) Attach the subprocess pid.
After debugging, use the detach PID to release the child process and continue debugging the parent process.
Custom Debug Tempo
GDB can customize commands to simplify the debugging process, and the commands are defined in the file. gdbinit file in the format:
Define <command>
<code>
End
Document <command>
End
View macro Definitions
First add the parameter '-gdwarf-2 ' '-g3 ' at compile time:
$ gcc-gdwarf-2-g3 Sample.c-o Sample
Used to notify the GCC compiler to add extended information at compile time.
* Info Macro <macro name>: Display macro Information
* Macro Expand Expression: unfolds expressions. However, the expression results are not displayed.
Suppose there is a definition #define ADD (x, y)
(GDB) macro expand ADD (7,8)
Expands to: (7) + (8)
Common options for GDB tui mode
Executes the GDB-TUI program name, automatically enters the TUI mode layout src mode, namely the window above displays the program source code, below is the command window.
Layout src default mode, above for the program source code, below the command window
Layout ASM Displays assembly code for the program, below the command window
Layout split three windows, above SRC, ASM in center, CMD below
Layout reg Displays the Register window at the top (above src or above asm)
TUI Reg General Display Universal Register
TUI Reg FLOAT Displays floating-point registers
TUI Reg system Display Systems Register
TUI Reg Next Displays the next page of the Register
Focus Src/asm/cmd/reg Switches the cursor to the specified window
Refresh refreshes all windows
Update Updates Source code window and current execution point
Winheight name +/-line adjusts the height of the name window
GDB Debug Core Dump
The program may have a random run-time crash, which is generally difficult to reproduce. You can use the core dump file to debug this problem. The core dump file is a log file generated by the program in the event of a crash.
Ulimit-c View system settings core file size limits, ULIMIT-C size set the system settings for the core file sizes, such as Ulimit-c unlimited set size unrestricted//settings are only valid for the current shell
Debug version of the program at run time error, after generating a core file. Debugging a core file with GDB allows you to debug random errors.
$ GDB Program Name core file
After booting, debug as normal gdb.
GDB Debugging Learning