View stack information
When the program is stopped, the first thing to confirm is where the program is broken. This is generally viewed by viewing the call stack information. In gdb, the command for viewing the call stack is backtrace, which can be abbreviated as bt.
(Gdb) bt
#0 pop () at stack. c: 10
#1 0x080484a6 in main () at main. c: 12
You can also use the info stack command to implement similar functions (I prefer this command ):
(Gdb) info stack
#0 pop () at stack. c: 10
#1 0x080484a6 in main () at main. c: 12
View Source program
When the program is disconnected, gdb displays the current breakpoint location:
Breakpoint 1, pop () at stack. c: 10
10 return stack [top --];
You can use the list command to view the source code of the program near the current breakpoint:
(Gdb) list
5 int top =-1;
6
7
8 char pop (void)
9 {
10 return stack [top --];
11}
12
13 void push (char c)
14 {
More parameters can be added after the list command to display more functions:
- <Linenum> row number.
- <+> [Offset] The positive offset of the current row number.
- <-> [Offset] The negative offset of the current row number.
- <Filename: linenum> specifies the row in the file.
- <Function> function Code
- <Filename: function> specifies the function in the file.
- <* Address> the address of the statement in the memory when the program runs.
However, even with this information, it is not easy to view the code. Currently, the new version of gdb has a tui function, which can be enabled using the focus command. Its main interface is as follows:
This interface is much more convenient than the list interface. It can highlight the execution position of the current statement, and the step will also change, a little bit Using Turbo C.
I don't know if it is because of the newer focus. It seems that there are not many articles on the Internet to introduce it. Although it is easy to get started, there are a lot of things to introduce, I will not explain it more here. If you are interested, you can refer to the gdb gui usage article.
View runtime data
The command for viewing variables in gdb is print, which is usually abbreviated as p. Its syntax is as follows:
Print [</format>] <expr>
The expr parameter can be a variable or expression. Format indicates the output format. For example, you can use/x to output the result in hexadecimal format. The following are some basic examples:
(Gdb) p top
$16 = 1
(Gdb) p & top
$17 = (int *) 0x804a014 <top>
(Gdb) p 3 + 2*5
$18 = 13
(Gdb) p/x 3 + 2*5
$19 = 0xd
The value range of format is as follows:
- X displays variables in hexadecimal format.
- D. Display 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 variables in hexadecimal format.
- C. Display variables in character format.
- F. Display variables in floating point format.
View function return values
Viewing function return values is a common requirement during debugging. For example
Int foo ()
{
Return 100;
}
You can obtain the return value of a function as follows:
1. Run the "finish" command until the function ends. The return value of the function is printed.
(Gdb) finish
Run till exit from #0 foo () at main. c: 9
Main () at main. c: 15
15}
Value returned is $2 = 100
2. The returned values are stored in the eax register. You can view the information to obtain the returned values.
(Gdb) p $ eax
$3 = 100
(Gdb) info registers
Eax 0x64 100
View continuous memory
You can use the "@" operator of GDB to view the continuous memory. The left side of "@" is the value of the first memory address, and the right side of "@" is the length of the memory you want to view.
For example, for the following code: int arr [] = {2, 4, 6, 8, 10};, you can run the following command to view the data of the first three units of arr.
(Gdb) p * arr @ 3
$2 = {2, 4, 6}
View memory
You can use the examine command (abbreviated as x) to view the value in the memory address. The syntax of the x command is as follows:
X/<n/f/u> <addr>
- N indicates the length of the display memory, that is, the content of several addresses is displayed from the current address to the back.
- F indicates the display format. If it is a string, use s. If it is a number, use I.
- U indicates the number of bytes requested from the current address. The default value is 4 bytes. (B single-byte, h dual-byte, w four-byte, g eight-character Section)
- <Addr> indicates a memory address.
For example, the 32-byte memory after the address of the preceding array is displayed in two bytes.
(Gdb) x/16uh arr
0xbffff4cc: 2 0 4 0 6 0 8 0
0xbffff4dc: 10 0 34032 2052 0 0 0 0
Auto display
In Visual Studio, You can dynamically view the value of a variable in the monitoring window. In gdb, a similar command display is provided. Its syntax is:
Display <expr>
Display/<fmt> <expr>
Display/<fmt> <addr>
Expr is an expression. fmt indicates the display format, and addr indicates the memory address. After you have set one or more expressions with display, GDB will automatically display the values of these expressions as long as your program is stopped (when tracking in a single step.
Several related commands are as follows:
- Undisplay <dnums...> dispaly not displayed
- Delete display [dnums] delete automatic display. All automatic displays are deleted without the dnums parameter, and range deletion is supported, for example, delete display 1, 3-5.
- Disable display <dnums...> invalidates display.
- Enable display <dnums...> restore display
- Info display view display information