How can I see the contents of the stack using GDB?

I am new to GDB, so I have a few questions:

  • How to view the contents of the stack? Example: to see the contents of a register, I type info registers . For the stack, what should it be?

  • How can I see the contents of $0x4(%esp) ? When I print print /d $0x4(%esp) , GDB throws an error.

Platform: Linux and GDB

+79
c assembly gdb
Oct 21 '11 at 11:31
source share
3 answers

info frame to show stack frame information

To read memory at given addresses, you should take a look at x

x/x $esp for hex x/d $esp for signed x/u $esp for unsigned, etc. x uses format syntax, you can also look at the current instruction through x/i $eip , etc.

+97
Oct 21 '11 at 11:43
source share

Using:

1. bt - backtrace: show stack functions and arguments

2. info frame - show start / end stack / args / locals

x/100x $sp - show the memory stack

    
 (gdb) bt
 # 0 zzz () at zzz.c: 96
 # 1 0xf7d39cba in yyy (arg = arg @ entry = 0x0) at yyy.c: 542
 # 2 0xf7d3a4f6 in yyyinit () at yyy.c: 590
 # 3 0x0804ac0c in gnninit () at gnn.c: 374
 # 4 main (argc = 1, argv = 0xffffd5e4) at gnn.c: 389

 (gdb) info frame
 Stack level 0, frame at 0xffeac770:
  eip = 0x8049047 in main (goo.c: 291);  saved eip 0xf7f1fea1
  source language c.
  Arglist at 0xffeac768, args: argc = 1, argv = 0xffffd5e4
  Locals at 0xffeac768, Previous frame sp is 0xffeac770
  Saved registers:
   ebx at 0xffeac75c, ebp at 0xffeac768, esi at 0xffeac760, edi at 0xffeac764, eip at 0xffeac76c

 (gdb) x / 10x $ sp
 0xffeac63c: 0xf7d39cba 0xf7d3c0d8 0xf7d3c21b 0x00000001
 0xffeac64c: 0xf78d133f 0xffeac6f4 0xf7a14450 0xffeac678
 0xffeac65c: 0x00000000 0xf7d3790e
+57
May 6 '16 at 15:23
source share

You need to use gdb memory mapping commands. Basic for x to check . Here is an example on a linked page that uses

 gdb> x/4xw $sp 

to print "four words ( w ) of memory above the stack pointer (here, $sp ) in hexadecimal ( x ) format." The quote is slightly paraphrased.

+52
21 Oct 2018-11-11T00:
source share



All Articles