Debug program with GDB (iv) _GDB

Source: Internet
Author: User
View stack Information
—————

When the program is stopped, the first thing you need to do is to see where the program stops. When your program calls a function, the address of the function, the function arguments, the local variables within the function are pressed into the stack. You can use the GDB command to view the information in the current stack.

Here are some GDB commands for viewing the function call stack information:

    backtrace
    bt
        Prints all the information for the current function call stack. such as:
       
        (gdb) bt
        #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
       
& nbsp;       can see the call stack information for the function: __libc_start_main--> main ()--> func ()
        
   
    backtrace <n>
     bt <n>
        n is a positive integer that prints only the stack information at the top n level of the stack.

BackTrace <-n>
BT <-n>
-N Table A negative integer that represents only the stack information at the bottom n level of the stack.

If you want to see a layer of information, you need to switch the current stack, in general, when the program stops, the topmost stack is the current stack, if you want to look at the bottom layer of the stack details, the first thing to do is to switch the current stack.

Frame <n>
F <n>
n is an integer that starts at 0 and is the layer number in the stack. For example: Frame 0, which represents the top of the stack, frame 1, represents the second layer of the stack.

Up <n>
To move an n layer above the stack, you can move it up one level without hitting N.

Down <n>
Represents moving an n layer underneath the stack, without hitting N, which means moving down one level.

The above command will print out the information of the stack layer that is moved to. If you don't want it to be typed. You can use these three commands:

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


To view the current stack layer information, you can use the following GDB command:

Frame or F
Will print out this information: Stack layer number, current function name, function parameter value, function file and line number, function to execute the statement.

Info frame
Info F
This command prints more detailed information about the current stack layer, except that most are internal addresses at run time. For example: function address, call function address, the address of the called function, the current function is written by what program language, function parameter address and value, local variable address and so on. Such as:
(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
Prints the parameter name of the current function and its value.

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

Info catch
Prints exception handling information in the current function.




View Source Program
—————

One, display source code

GDB can print out the source code of the program being debugged, and of course, when compiling the program, it must add-g parameter to compile the source program information into the execution file. Otherwise, you will not see the source program. When the program stops, GDB reports that the program is parked on the first line of that file. You can use the List command to print the source code of the program. Let's take a look at the GDB command to view the source code.

List <linenum>
Displays the source program around the LineNum line.

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

List
Displays the source program after the current line.

List-
Displays the source program before the current line.

Generally print the current line of the top 5 lines and the next 5 lines, if the display function is 2 lines under 8 lines, the default is 10 lines, of course, you can also customize the scope of the display, use the following command to set the number of lines to display the source program.

Set Listsize <count>
Sets the number of lines to display the source code at once.

Show Listsize
View the settings for the current listsize.

The list command also has the following usage:

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

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

List +
Display the source code later.

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

<linenum> line number.
<+offset> the positive offset of the current line number.
<-offset> negative offset of current line number.
<filename:linenum> which row of which file.
<function> function name.
<filename:function> which function in which file.
The address of the statement in memory at the time the <*address> program runs.

Second, search source code

Not only that, GDB also provides the command for source code search:

Forward-search <regexp>
Search <regexp>
Search the front.

Reverse-search <regexp>
Search all.

Where,<regexp> is the regular expression, but also the main string matching pattern, about regular expressions, I do not speak here, but also ask you to check the relevant information.


Third, specify the path of the source file

At some point, the execution program that was compiled with-G only included the name of the source file, no path name. GDB provides a command that allows you to specify the path to the source file so that gdb can search.

Directory <dirname ... >
Dir <dirname ... >
Add a source file path to the front of the current path. If you want to specify multiple paths, Unix you can use ":" Under Windows you can use ";"
Directory
Clears all custom source file search path information.

Show directories
Displays the defined source file search path.

Four, the source code of memory

You can use the Info Line command to view the address of the source code in memory. Info line can be followed by "row number", "Function name", "FileName: line number", "FileName: function name", this command will print out the specified source code at runtime memory address, such as:

(GDB) Info line Tst.c:func
Line 5 of the "tst.c" starts at the address 0x8048456 <func+6> and ends at 0x804845d <func+13>.

There is also a command (disassemble) where you can view the machine code for the current execution of the source program, which will dump the instructions in the current memory. The following example represents the assembly code that views the function func.

(GDB) Disassemble func
Dump of assembler code for function func:
0x8048450 <func>: Push%EBP
0x8048451 <func+1>: mov%esp,%ebp
0x8048453 <func+3>: Sub $0x18,%esp
0x8048456 <func+6&gt: Movl $0X0,0XFFFFFFFC (%EBP)
0x804845d <func+13&gt: Movl $0x1,0xfffffff8 (%EBP)
0x8048464 <func+20&gt: mov 0xfffffff8 (%EBP),%eax
0x8048467 &LT;FUNC+23&GT: CMP 0x8 (%EBP),%eax
0X804846A <func+26&gt: Jle 0x8048470 <func+32>
0x804846c <func+28&gt: jmp 0x8048480 <func+48>
0x804846e <func+30>: mov%esi,%esi
0x8048470 <func+32&gt: mov 0xfffffff8 (%EBP),%eax
0x8048473 <func+35&gt: Add%EAX,0XFFFFFFFC (%EBP)
0x8048476 <func+38&gt: Incl 0xfffffff8 (%EBP)
0x8048479 <func+41&gt: jmp 0x8048464 <func+20>
0x804847b <func+43>: NOP
0x804847c <func+44&gt: Lea 0x0 (%esi,1),%esi
0x8048480 <func+48&gt: mov 0xfffffffc (%EBP),%edx
0x8048483 <func+51>: mov%edx,%eax
0x8048485 <func+53&gt: jmp 0x8048487 <func+55>
0x8048487 <func+55>: mov%ebp,%esp
0x8048489 <func+57>: Pop%EBP
0x804848a <func+58>: ret
End of assembler dump.


Origin: http://blog.csdn.net/haoel/article/details/2882

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: info-contact@alibabacloud.com 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.