View Stack Information for gdb Debugging

Posted by BIOSTALL on Tue, 30 Nov 2021 19:27:03 +0100

outline

backtrace (abbreviated as bt)

Prints all information about the current function call stack.

backtrace

n is a positive integer that only prints stack information at the top of the stack.

backtrace <-n>

The -n table is a negative integer indicating that only the stack information at the bottom of the stack is printed.

frame

n is a zero-based integer and is the layer number in the stack. For example: frame 0, for the top of the stack, frame 1, for the second layer of the stack.

up

Represents moving n layers up the stack, without hitting n, indicating moving up one layer.

down

Represents moving n layers down the stack, without typing n, to move down one layer.

select-frame ,up-silently ,down-silently

The above command prints out the information of the stack layer that you moved to. If you don't want it to type out information. You can use these three commands: select-frame for frame, up-silently for up, and down-silently for down.

frame or f

This information is printed out: the layer number of the stack, the current function name, the function parameter value, the file and line number where the function is located, and the statement to which the function is executed.

info frame

This command prints out more detailed information about the current stack layer, but most are runtime inland addresses. For example, the address of the function, the address of the calling function, the address of the called function, the language in which the current function is written, the address and value of the function parameter, the address of the local variable, and so on.

info args

Print out the parameter names and their values for the current function.

info locals

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

info catch

Prints out exception handling information in the current function.

Experiment

  1. View stack information, there is no stack information before the program runs
(gdb) backtrace
No stack.
  1. Run the program
(gdb) r
Starting program: /home/ubuntu/learn_gdb/tst 
result[1-100] = 5050 
result[1-250] = 31125 
[Inferior 1 (process 3011899) exited normally]
  1. View stack information, the program is running normally, the stack is destroyed, and there is no stack information
(gdb) backtrace
No stack.
  1. Set a Breakpoint
(gdb) break func
Breakpoint 1 at 0x555555555149: file tst.c, line 4.
  1. Run the program
(gdb) r
Starting program: /home/ubuntu/learn_gdb/tst 

Breakpoint 1, func (n=0) at tst.c:4
4       {
  1. Looking at the stack information, you can see that there are two function calls displayed in the stack
(gdb) bt
#0  func (n=0) at tst.c:4
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
  1. View stack information and print only the information on the top layer of the stack
(gdb) bt 1
#0  func (n=0) at tst.c:4
(More stack frames follow...)
  1. View stack information and print only the information at the bottom of the stack
(gdb) bt -1
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
  1. View stack information and print only the information on the top two floors of the stack
(gdb) bt 2
#0  func (n=0) at tst.c:4
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
  1. View stack information, print only two layers below the stack
(gdb) bt -2
#0  func (n=0) at tst.c:4
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
  1. Show the second layer of stack information
(gdb) frame 1
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
31          sum = func(10);
  1. Up one level (corresponding to the frame's subscript)
(gdb) up 1
#1  0x0000555555555221 in main (argc=1, argv=0x7fffffffe148) at tst.c:31
31          sum = func(10);
  1. Down one level (corresponding to the frame's subscript)
(gdb) down 1
#0  func (n=0) at tst.c:4
4       {
  1. Print information about the current layer
(gdb) frame
#0  func (n=0) at tst.c:4
4       {
  1. Print more detailed information using info frame
(gdb) info frame
Stack level 0, frame at 0x7fffffffe020:
 rip = 0x555555555149 in func (tst.c:4); saved rip = 0x555555555221
 called by frame at 0x7fffffffe060
 source language c.
 Arglist at 0x7fffffffe010, args: n=0
 Locals at 0x7fffffffe010, Previous frame's sp is 0x7fffffffe020
 Saved registers:
  rip at 0x7fffffffe018
  1. Print local variables
(gdb) info locals
sum = -8137
i = 32767
  1. Input parameters of print function
(gdb) info args
n = 0
  1. Print out exception handling information in the current function
(gdb) info catch
Undefined info command: "catch".  Try "help info".

Reference resources

Debugging Programs with GDB (IV)

Topics: Linux debug GDB