1. Commonly used methods to enter gdb
1、gdb attach pid
Debug a service program in the system.
2、gdb program core
Debug the core file generated by the program
Two, breakpoint
1. Break function (function name)//abbreviated b
2. Break file: line (file name: line number)//abbreviated b
3. info break (check breakpoint information)
4. Maintain breakpoints:
clear followed by the function or file name: line number
enable followed by breakpoint number
disable followed by breakpoint number
If none of the three are followed, all breakpoints will be operated.
Three, observation point
1. watch/rwatch/awatch [expression or variable] (break to the current position when the expression or variable changes/is read/written)
2、info watchpoints
Fourth, resume program execution and
1, continue /c to run the program
2. Next/n single-step debugging encounters the function but does not enter the function
3. Step/s single-step debugging encounters the function to enter the function
Five, signal
1. Info handel view
2. Handel SIG34 nostop noprint Since some signals will interrupt the execution after the continue execution, the change signal needs to be ignored at this time.
Six, view stack information
1, info thread view the current statement of all threads
2. bt [n] Viewing the call stack of the main thread can be followed by integers or not, followed by printing a few layers (positive n is the last n layer, negative n is the previous n layer)
3. Thread apply all bt View all thread stack information of the process
4. info registers view register address
5. Frame n jumps to the first level of the stack
6, info frame view the current jump to which layer of the stack
7, info args view the parameters of the current function
8, info locals view the local variables of the current function
9, list function view function source code
10. List line View the source code around the line line
Seven, view runtime variables
1. Print/px view x value
2. Output format p/xi to view the hexadecimal of i
x Display variables in hexadecimal format.
d Display variables in decimal format.
u Display unsigned integer in hexadecimal format.
o Display variables in octal format.
t Display variables in binary format.
a Display variables in hexadecimal format.
c Display variables in character format.
f Display variables in floating point format.
3. p *array@len to view the array (len length of the first address of the array)/p arr[i] is also OK
4. x/nfu addr view memory
{
n, f, u are optional parameters.
n(the repeat count) is a positive integer, the default is 1, which means that the length of the memory is displayed starting from addr (according to
units u count), that is to say, display the contents of several addresses backward from the current address.
f(the display format) represents the display format, see above. If the address refers to a string, then the format
It can be s, if the address is an instruction address, then the format can be i. The default is'x' (hexadecimal)
u(the unit size) represents the number of bytes requested from the current address. If not specified, GDB defaults to w(4
bytes). The u parameter can be replaced with the following characters, b (Bytes) means single byte, h (Halfwords) means double word
In section, w (Words) represents four bytes, and g (Giant word) represents eight bytes. When we specify the byte length, GDB will
Starting from the specified memory address in the specified memory, read and write the specified byte, and take it out as a value. Specify one at a time
Unit size, this size will become the default unit the next time you use the x command.
}
The three parameters n/f/u can be used together. E.g:
The command x/3uh 0x54320 means to read the content from the memory address 0x54320, and h means to use double bytes as a unit, 3
Means three units, u means display in hexadecimal.
5. show print union view the union
6. Turn on the address output
set print address on
set print address off
show print address
7. Open the array display. When the array is displayed after opening, each element occupies a line. If it is not opened, each element is marked with a comma.
Number separated. This option is turned off by default. The two related commands are as follows, I won’t say more
set print array on
set print array off
show print array
8. This option is mainly to set the array. If your array is too large, you can specify one to specify the data display
The maximum length. When this length is reached, GDB will no longer display down. If set to 0, it means no limit.
set print elements
show print elements
9. If this option is turned on, when the character string is displayed, the display will stop when it encounters an end character. This option is off by default.
set print null-stop
10. Pretty option
set print pretty on
If you turn on the option printf pretty, then GDB will look pretty when it displays the structure. Such as:
$1 = {
a= 0x0,
b= {
c= 1,
d= 1
},
e= 0x54 "xx"
}
set print pretty off
Turn off the printf pretty option, GDB will display the structure as follows:
$1 = {a = 0x0, b = {c = 1, d = 1}, e = 0x54 "xx"}
show print pretty
See how the organization is displayed
11. Character display switch
set print sevenbit-strings
Set the character display, whether it is displayed in the format of "\nnn", if it is turned on, the character string or character data is displayed in \nnn,
Such as "\065".
show print sevenbit-strings
12. When setting the display structure, whether to display the union data in it
set print union
When setting to display the structure, whether to display the union data in it. For example, the following data structure:
typedef enum {a, b} enum_a;
typedef enum {c,d, e} enum_b;
typedef enum {f, g, h}enum_c;
struct st_test {
enum_a a_en;
union {
enum_b b_en;
enum_c c_en;
} form;
};
struct st_test foo = {a, {c}};
When this switch is turned on, after the p foo command is executed, it will be displayed as follows:
$ 1 = {a_en = Tree, form = {b_en = c, c_en = f}}
When this switch is turned off, after the p foo command is executed, it will be displayed as follows:
$1 = {a_en = Tree, form = {...}}
show print union
View how the consortium data is displayed
8. Set runtime variables and call functions
1. print/px=10 set the value of the variable
At some point, it is very possible that your variables conflict with the parameters in GDB, such as:
(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set width=47
Invalid syntax in expression.
Because set width is a GDB command, there is an error in the setting of "Invalid syntax in expression"
Error, at this time, you can use the set var command to tell GDB that width is not a parameter of your GDB, but a program change
The name of the quantity, such as:
(gdb) set var width=47
In addition, there may be some cases where GDB does not report this error, so to be on the safe side, before you change the value of the program variable
At the same time, it is best to use the GDB command in set var format.
2、call
call expr
The expression can be a function to achieve the purpose of forcing the function to be called. And display the return value of the function, if the function returns
If the return value is void, then it will not be displayed.