GDB common debugging commands (Linux)

First of all, we need to know that there are two ways to release the program: debug mode and release mode Linux gcc/g++ binary program, the default is release mode. To use gdb debugging, you must add the -g option when generating the binary program from the source code .

1. The easiest way to debug: let the program stop at one position and use printf to output the values ​​of some related variables that need to be viewed, so we can debug by adding printf to the position where we feel that there is a problem in the program, but This method is too cumbersome, so it is very convenient for us to use GDB for debugging, and the basic principle is similar.

2. Entry method : When compiling with gcc, add -g to enter debug mode, and then use gdb+filename to enter debugging. Use ctrl + d or quit to exit debugging.

3. Some commonly used commands:

list/l Line number : Display the source code of binFile, and then go to the following position from the last position, 10 lines each time.

list/l function name : List the source code of a function.

r or run : run the program.

n or next : single execution.

s or step : step into a function call

break(b) line number : set a breakpoint on a line

break function name : set a breakpoint at the beginning of a function

info break : View breakpoint information.

finish : Execute until the current function returns, then stand down and wait for the command

print(p) : print the value of the expression, through which the value of the variable can be modified or the function can be called

p variable : print the variable value.

set var : modify the value of a variable

continue (or c) : Execute the program continuously instead of stepping from the current position

run (or r) : Executes the program continuously rather than stepping from the beginning

delete breakpoints : delete all breakpoints

delete breakpoints n : delete the breakpoint with sequence number n

disable breakpoints : disable breakpoints

enable breakpoints : enable breakpoints

info (or i) breakpoints : See which breakpoints are currently set

display variable name : track and view a variable and display its value every time you stop

undisplay : untrack those variables that were previously set

until X line number : jump to X line

breaktrace (or bt) : View function calls and parameters at all levels

info(i) locals : View the value of the local variables of the current stack frame

quit : quit gdb

Fourth, the use of basic methods :

a. First of all, we need to stop the program at certain positions, then we need to add breakpoints, use (b+line number) to set breakpoints on certain lines, if you don’t know where to set the settings, you can use list to view the relevant lines , (l+line number) is to print ten lines in the center of the changed line, (l+function name) can view all the code in the function, (r) can directly run the program, when we use (l) to view the original code, press Enter You can continue to look down.

b. Check the breakpoint information (info b) after adding a breakpoint, clear the breakpoint of the line (clear) , in debugging, we use continue to run to the next breakpoint directly after running it with r . Here are three very important The instructions must keep in mind: (1) single-step execution does not enter the function next, (2) single-step execution enters the function step, (3) terminate the program being debugged kill.

c. When viewing a dynamic array, use (p *arrayname(array name)@len(array length)), the left side of @ is the first address of the array, that is, the content pointed to by the array array, and the right side is the length of the data, that is You can use this command to view all elements in the array. Static arrays can be viewed directly by using (p+array name) .

d. View the value of the variable in the program. There are three kinds of variables we can view:

(1) Global variables (all files)
(2) Static global variables (current file)
(3) Local variables (current code segment)

It should be noted that when our local variable and global variable have the same name, when the program runs to the code segment where the local variable is located, the value of the local variable is directly displayed (the viewing method is still: p+variable name) . At this time, if you want to To view the value of a global variable, you need to add :: before the variable name to view the value of the global variable of that name.

e. View the contents of the address: (x/)
n indicates the length of the display memory, that is to say, the contents of several addresses are displayed backward from the current address.
f indicates the display format.
u indicates the word requested from the current address . The number of sections (default four bytes).

Example: x/4uh 0x12121212 4 is the position of n to represent 4 units, u is the position of f and is displayed in hexadecimal, and h is the position of u to represent double bytes (each unit).

As long as you master the above commonly used gdb commands, you can perform some basic debugging.

5. Data display format of GDB:
x Display variables 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.

Example: View the value of the variable num: p num (default decimal), then p/x num , it will display the hexadecimal value of num.

 

 

Guess you like

Origin blog.csdn.net/weixin_49312527/article/details/122029443