Program debugging with GDB (4)

Source: Internet
Author: User

View stack information

When the program is stopped, the first thing you need to do is to check where the program stops. When your program calls a function, the function address, function parameters, and local variables in the function will be pushed into the stack. You can use the gdb command to view information in the current stack.

The following are some gdb commands for viewing the stack information of function calls:

Print all information about the current function call stack. For example:

#0 func (n = 250) at TST. C: 6
#1 0x08048524 in main (argc = 1, argv = 0xbffff674) at TST. C: 30
#2 0x400409ed in _ libc_start_main () from/lib/libc. so.6

From the above we can see the call stack information of the function: __libc_start_main --> main () --> func ()

Backtrace <n>
BT <n>
N is a positive integer, indicating that only stack information of N layers on top of the stack is printed.

Backtrace <-N>
BT <-N>
-N indicates that only stack information of N layers under the stack is printed.

If you want to view the information of a certain layer, you need to switch the current stack. Generally, when the program stops, the top stack is the current stack, if you want to view the details of the layer below the stack, you must first switch the current stack.

Frame <n>
F <n>
N is an integer starting from 0 and a layer number in the stack. For example, frame 0 indicates the top of the stack, frame 1 indicates the second layer of the stack.

Up <n>
It indicates moving N layers to the top of the stack. If n is not required, it indicates moving up a layer.

Down <n>
It indicates moving N layers to the bottom of the stack. If n is not used, it indicates moving down a layer.

The above command will print the information to be moved to the stack layer. If you do not want them to output information. You can use these three commands:

Select-frame <n> corresponds to the frame command.
Up-silently <n> corresponds to the up command.
Down-silently <n> corresponds to the down command.

To view information about the current stack layer, run the following GDB command:

Frame or F
The following information is printed: the stack layer number, the current function name, function parameter value, the file and row number of the function, and the statement executed by the function.

Info Frame
Info F
This command prints more detailed information about the current stack layer, except that most of the information is the inner address of the runtime. For example, the function address, the address of the called function, the address of the called function, the current program language of the function, the address and value of the function parameter, the address of the local variable, and so on. For example:
(GDB) info F
Stack level 0, frame at 0xbffff5d4:
EIP = 0x804845d in func (TST. C: 6); saved EIP 0x8048524
Called by frame at 0xbffff60c
Source Language C.
Arglist at 0xbffff5d4, argS: N = 250
Locals at 0xbffff5d4, previous frame's SP is 0x0
Saved registers:
EBP at 0xbffff5d4, EIP at 0xbffff5d8

Info ARGs
Print the parameter name and value of the current function.

Info locals
Print all local variables and their values in the current function.

Info catch
Print the exception handling information in the current function.

View Source program

1. display source code

GDB can print the source code of the program to be debugged. Of course, you must add the-G parameter during program compilation to compile the source program information into the execution file. Otherwise, you will not be able to see the source program. When the program stops, GDB will report the row on which the program stops. You can use the LIST command to print the program source code. Let's take a look at the source code's GDB command.

List <linenum>
Displays the source code around the linenum line of the program.

List <function>
Displays the source program of the function named function.

Displays the source code behind the current row.

Displays the source code before the current row.

It is usually used to print the top 5 rows and the next 5 rows of the current row. If the display function is the top 2 and the next 8 rows, the default value is 10. Of course, you can also customize the display range. You can use the following command to set the number of lines that display the source program at a time.

Set listsize <count>
Set the number of lines for displaying source code at a time.

Show listsize
View the current listsize settings.

The list command also has the following usage:

List <first>, <last>
Displays the source code from the first row to the last row.

List, <last>
Displays the source code from the current row to the last row.

List +
The source code is displayed later.

Generally, the following parameters can be followed after the list:

<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 of the file.
<Function> function name.
<FILENAME: function> the function in the file.
<* Address> the address of the statement in the memory when the program runs.

2. Search source code

Besides, GDB provides the following source code search commands:

Forward-search <Regexp>
Search <Regexp>
Search forward.

Reverse-search <Regexp>
Search all.

Here, <Regexp> is a regular expression, which also specifies the matching mode of a string. I will not talk about the regular expression here. Please check the relevant information.

3. Specify the source file path

In some cases, the execution program compiled with-G only contains the name of the source file without a path name. GDB provides commands that allow you to specify the path of the source file for GDB to search.

Directory <dirname...>
Dir <dirname...>
Add a source file path to the front of the current path. If you want to specify multiple paths, you can use ":" in UNIX, and ";" in windows.
Clear all custom source file search paths.

Show Directories
Displays the defined source file search path.

4. Source Code memory

You can use the info line command to view the address of the source code in the memory. Info line can be followed by "row number", "function name", "File Name: line number", and "File Name: function name ", this command prints out the memory address of the specified source code at runtime, for example:

(GDB) info line TST. C: func
Line 5 of "TST. c" starts at address 0x8048456 <func + 6> and ends at 0x804845d <func + 13>.

There is also a command (disassemble) that you can view the machine code of the current execution of the source program. This command will dump the commands in the current memory. The following example shows the assembly code of the function func.

(GDB) disassemble func
Dump of worker er code for function FUNC:
0x8048450 <func>: Push % EBP
0x8048451 <func + 1>: mov % ESP, % EBP
0x8048453 <func + 3>: Sub $0x18, % ESP
0x8048456 <func + 6>: movl $0x0, 0 xfffffffc (% EBP)
0x804845d <func + 13>: movl $0x1, 0xfffffff8 (% EBP)
0x8048464 <func + 20>: mov 0xfffffff8 (% EBP), % eax
0x8048467 <func + 23>: CMP 0x8 (% EBP), % eax
0x804846a <func + 26>: jle 0x8048470 <func + 32>
0x8048480 <func + 28>: JMP 0 <func + 48>
0x804846e <func + 30>: mov % ESI, % ESI
0x8048470 <func + 32>: mov 0xfffffff8 (% EBP), % eax
0x8048473 <func + 35>: Add % eax, 0 xfffffffc (% EBP)
0x8048476 <func + 38>: incl 0xfffffff8 (% EBP)
0x8048479 <func + 41>: JMP 0x8048464 <func + 20>
0x804847b <func + 43>: NOP
0x804847c <func + 44>: Lea 0x0 (% ESI, 1), % ESI
0x8048480 <func + 48>: mov 0 xfffffffc (% EBP), % edX
0x8048483 <func + 51>: mov % edX, % eax
0x8048485 <func + 53>: JMP 0x8048487 <func + 55>
0x8048487 <func + 55>: mov % EBP, % ESP
0x8048489 <func + 57>: Pop % EBP
0x804848a <func + 58>: Ret
End of worker er dump.


<-Previous Page->

(All Rights Reserved. Please indicate the author and source when reprinting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.