first, Common common debugging command
1. Brief Introduction to GDB
Introduction: GDB is the Linux Environment Code debugging ⼯ tool.
Make ⽤: You need to add the-G option when the source code is ⽣.
Start making ⽤: GdB binfile
Exit: Ctrl + D or quit
2. Debugging Process
(1) the list command list linenum ⽰binfile The source code around the LineNum line, and then the last position is listed down, 10 ⾏ at a time. The list function displays the source program list of functions with function name display the source list after the current line-Displays the source program before the current line
(2) Run or R runs the program. The run args Run command can be directly connected to the command line parameter value, or it can be implemented by the set args + parameter value before executing run.
(3) Break (b)
Interrupt point, usage: b linenum break in a line Point B +offset/-offset stop B f before or after the current line number Ilename:linenum in a certain line of a file interrupt point B filename:function & nbsp In a certain file a function entry stop B *address  &NB sp; at the running address of the program B &N Bsp no parameters stop in the next sentence B where if condition  &N Bsp Stop at a certain line when a condition is met (this is useful, for example, B-if ret = 5) Breaktrace (or BT) View all levels function ⽤ and parameters delete breakpoints Delete all breakpoints Delete Breakpoints n Deletes a breakpoint with an ordinal number of n disable breakpoints Forbidden ⽤ Breakpoint Enable breakpoints Kai ⽤ Breakpoint
For the break command, we need to use it flexibly. For example, hit multiple breakpoints. In multithreaded programs, we can interrupt the main function after the thread is created, and execute the entry interrupt point of the threading function.
(4) One step command
The common usage will not be spoken. Step count executes count steps one at a time, if a function enters the function next count to execute count once, does not enter the function finish to run the program until the current function completes, and prints the function back The stack address and return value at the back and the parameter information until exit the loop body (especially for the For loop this is annoying)
(5) Continue command
Continue (or C): Starting from the current position of continuous ⽽⾮ Single step ⾏ procedure
After the program is stopped, you can use the Continue (c) command to resume the program until the program ends or the next breakpoint is reached. Note here that if there is no breakpoint the program will end directly.
(6) Print (P) command
This command is more commonly used to view what we want to see. For example, an array can look at all, or a section from left to right:
The output format that the Print command looks at for variables is: x displays variables in hexadecimal format
d display variables in decimal format
U show unsigned integer in hexadecimal format
o Display variables in octal format
t display variables in binary format t display variables in binary format
A variable is displayed in hexadecimal format
c Display variables by character format
F Display variables by floating-point number format
(7) Watch command
This command is more useful. Watch is generally used to observe whether the value of an expression (a variable is an expression) is changed, and if so, stop the program immediately. We have a few ways to set the observation point: watch expr sets an observation point for expression expr, and immediately stops the program Rwatch expr when expression expr is read, stop Program Awatch expr stops a program when the value of an expression is read or written. Info watchpoints lists all observation points (the info instruction can usually go to the set example below, demonstrating the observed value of *i, once the change stops:
In the loop we can also use watch, with the Ignore, it is in addition to the until command another way we can jump out of the loop, but the Watch+ignore is more powerful, you can jump to the first loop. They mean looking at a variable that can be understood as a breakpoint, how many times the breakpoint is ignore, and then using continue to skip directly.
(8) Examine command
Use this command to view the values in the memory address. The syntax is: x/u addr
Addr represents a memory address. N, F, and U after "x/" are optional parameters, n is a positive integer indicating the length of the display memory, that is, displaying the contents of several addresses backwards from the current address; F indicates the format to display, if the address refers to a string, then the format can be S, if the address is the instruction address, then the format can be i;u Represents the number of bytes requested from the current address and, if not specified, gdb defaults to 4 bytes. The u parameter can be replaced by some characters: B for Single-byte, H for Double-byte, W for four bytes, and G for eight bytes. When we specify the byte length, GDB starts with the specified memory address, reads and writes the specified byte, and takes it out as a value. N, F, and u these 3 parameters can be used together, for example, the command "X/3uh 0x54320" indicates that 3 units (3) of memory are displayed in Double-byte 1 units (h) and 16 (U) from the memory address 0x54320.
(9) Jump command
The jump command does not change the contents of the program stack, and generally only jumps within the same function. Jump Linespec Specifies the run point of the next statement, Linespec can be linenum,filename+linenum,+offset these forms jump address jumps to the line of code
(a) Signal order
The signal is sent to the program using the signal signal name (such as SIGINT), and if the program registers the Signal_handler function, it can also be processed to help debug the program.
The Setcommand set args sets the command line argument set env Environmentvarname=value sets the environment variable. such as: Set env User=benben
(a) calls command call function forces a function to be called
Forces a call to a function that displays the return value of the function (if the function return value is not void). The Print command can also complete this function.
(a) disassemble Command
Disassembly command to view the machine code for the source code at execution time.
(14) Other commands info (i) locals: view the value of the current stack frame local variable
Info break: View breakpoint Information
Info (or i) Breakpoints: See which breakpoints are currently set
finish: hold ⾏ to the current function return, and then stand down to wait for the command
set var: Modify the value of a variable
display variable name: Trace view ⼀ variable, each stop ⽰ its value
undisplay: Cancels tracking of those variables that were previously set
until X⾏: jump ⾄x⾏ directly back to N or next: single ⾏