GDB tips (3) - View stack information

View stack information

When the program is stopped, the first thing you need to do is to see where the program is anchored. When your program calls a function, address of the function, function parameters, local variables within the function will be pressed into the "stack" (Stack) in. You can use the GDB command to view information about the current stack.

Here are some viewing function call stack information GDB commands:

  • backtrace
  • bt

    Print all information about 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 

    As can be seen from the function call stack information: __ libc_start_main -> main () -> func ()
   

  • backtrace <n>
  • bt <n>

    n is a positive integer, the print information on the stack only n-layer stack.

  • backtrace <-n>
  • bt <-n>

    a table -n-negative integer stack information is printed only beneath the n-layer stack.
       
If you want to see a certain level of information, you need to switch the current stack, in general, when the program stops, the top level of the stack is the current stack, if you want to view details of the underlying layer stack, first thing to do is switching current stack.

  • frame <n>
  • f <n>

    n is an integer from 0 is the layer number of the stack. For example: frame 0, indicates the stack, frame 1, the second layer stack of FIG.

  • up <n>

    It represents the movement of the stack above the n-layer, n may not play, represents one layer move upwards.
   

  • down <n>

    Represents movement toward the lower n-layer stack, may not play n, the downwardly moving layer.
   

The above command will print out the information layer is moved to the stack. If you do not want to let it play information. You can use these three commands:

  •     select-frame <n> command corresponding to the frame.
  •     up-silently <n> corresponds to the up command.
  •     down-silently <n> corresponds to the down command.

View information about the current stack of layers, you can use the GDB commands:

  • frame or f

    Will print out this information: layer number stack, the current function names, function parameters, and the line number where the file functions, function to execute the statement.

  • info frame
  • info f

        This command will print out more detailed information about the current stack of layers, but, Nene address most of the runtime. For example: function address, the address of the function call, the address of the called function, the current function of what is written in the programming language, function and value of the parameter address, the address of a local variable, 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

   Print out the current parameter name and value of the function.

  • info locals

   Print out all the local variables and the current value of the function.
  

  • info catch

   Print out the exception process information of the current function.

            
View source

A display source

GDB can print out the source code debugger, of course, the program must be compiled with -g add parameters to compile the source code information into the executable file. Otherwise you can not see the source code. When the program stops, GDB will report on the program stopped at the first few lines of the file. You can use the list command to print the source code of the program. Or take a look at the source code view GDB command bar.
   

  • list <linenum>

    Display program source code around the first linenum row.

  • list <function>

    Display function named function of the source function.
   

  • list

    Displays the current behind the line source.

  • list -

    Displays the current front line of source code.

5 is generally printed on the current line and the next line 5, if the display function is the lower 8 rows are rows 2, line 10 is the default, of course, the range can also be custom display, using the following command to set a display source the number of rows.

  • set listsize <count>

    Setting a number of display lines of source code.
   

  • show listsize

    Listsize view the current settings.
       

list command also has the following usage:

  • list <first>, <last>

    Displays the first line to the last line of source code between.

  • list , <last>

    Displays the current source line to the last line between.
   

  • list +

    Next display the source code.
       

In general this can be followed by the following parameters in the back of their list:

<linenum>   行号。
<+offset>   当前行号的正偏移量。
<-offset>   当前行号的负偏移量。
<filename:linenum>  哪个文件的哪一行。
<function>  函数名。
<filename:function> 哪个文件中的哪个函数。
<*address>  程序运行时的语句在内存中的地址。

Second, the search for the source code

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

  • forward-search <regexp>
  • search <regexp>

    Search to the front.

  • reverse-search <regexp>

        All search.
       
Where, <regexp> is a regular expression, but also a master pattern matching string, on regular expressions, I will not talk about here, but please see the relevant information.


Third, specify the path of the source file

Sometimes, with the implementation of the program after the -g compilation included only in the name of the source file without a path name. GDB provides command allows you to specify the path of the source file for GDB search.

  • directory <dirname ... >
  • dir <dirname ... >

    Adding a source file path to the front of the current path. If you want to specify multiple paths, you can use the UNIX ":" Under Windows you can use ";."

  • directory

    Clear search path information source for all custom.

  • show directories

    It defines the display of the source file search path.
       

Fourth, the source memory

You can use the info line command to view the source code for an address in memory. info back line can be followed by "line numbers", "function", "File name: line number", "File name: function", this command will print out the memory address of the specified source code at runtime, such as:

(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) You can view the source code of the current machine code when executed, this command will present instruction memory dump out. The following example represents the function func view assembler code.

(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>:     movl   $0x0,0xfffffffc(%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>
0x804846c <func+28>:    jmp    0x8048480 <func+48>
0x804846e <func+30>:    mov    %esi,%esi
0x8048470 <func+32>:    mov    0xfffffff8(%ebp),%eax
0x8048473 <func+35>:    add    %eax,0xfffffffc(%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    0xfffffffc(%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 assembler dump.

 
----------------
Disclaimer: This article is the original article CSDN bloggers "haoel", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/haoel/article/details/2882

Published 175 original articles · won praise 262 · views 700 000 +

Guess you like

Origin blog.csdn.net/li_wen01/article/details/105223367