97

Right now I've been using GDB to disassemble a binary file and check out different registers and whatnot. Is there an easy command to examine everything on the stack? Can this be limited to everything in a function?

2

4 Answers 4

114

You can view the contents of the stack with x/10x $sp

This will print the top 10 elements of the stack.

4
  • What version of GDB is this? I can't get gdb to use registers as command arguments on GDB 7.7-0ubuntu3.1
    – nightpool
    Commented Nov 14, 2014 at 16:36
  • This is answer tells you how actually look at the bytes on the stack frame, which I've had some trouble finding out how to do. Thanks!
    – user3853034
    Commented Sep 12, 2017 at 11:35
  • But this will print the element pointed by $sp and the 9 elements after it, that is (if the stack grows downwards like for example in x86) 9 elements that are not actually used by the program yet. Right?
    – cYrus
    Commented Nov 19, 2018 at 15:26
  • @cYrus $sp points to the top of the stack, i.e. the lower address. You're probably thinking of $bp which stores the bottom address of the stack.
    – raphael
    Commented Feb 14, 2019 at 14:56
100

For the current stack frame:

  • info frame lists general info about the frame (where things start in memory, etc.)
  • info args lists arguments to the function
  • info locals lists local variables stored in the frame
2
  • what if I have a variable also called frame?
    – Jeoker
    Commented May 24, 2021 at 15:36
  • 1
    @Jeoker info frame is always going to tell you about the frame. If you want info about a variable you'd have to use other subcommands like info locals <name>, or other commands entirely like print Commented May 25, 2021 at 3:05
86
  • bt (or backtrace) will give you a call stack.

  • frame <args> will select a frame on the call stack for inspection

  • info frame <args> will give you information about a specific frame from the stack. When called without arguments it will display the currently selected frame

  • info locals can give you information about any local variables on the stack.

1
  • 2
    frame <args> also selects a frame. info frame <addr> is used to examine the frame without selecting it
    – rostamn739
    Commented May 17, 2018 at 7:32
9
  • just try bt full, and you will get all frames and locals
  • input frame x, to enter the x frame

by the way, you should know about process address space and what it is composed: linux virtual address space, this will help you understand how the frame is used.

1
  • Hello and welcome to SO! Please read the tour, and How do I write a good answer? For example adding a link to what is process address space and elaborate how it can help to solve this question might be helpful. Commented Jan 11, 2021 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.