93

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?

Flow
  • 23,572
  • 15
  • 99
  • 156
GetOffMyLawn
  • 1,362
  • 4
  • 14
  • 21
  • 3
    This should be of some help to you : http://cs.nyu.edu/courses/spring07/V22.0474-001/misc/gdb-refcard.pdf. You can always use "frame" command to jump between the frames. – dicaprio May 05 '10 at 05:58
  • The link requires login now. Here's an archived link: https://web.archive.org/web/20141222091304/http://cs.nyu.edu/courses/spring07/V22.0474-001/misc/gdb-refcard.pdf – johan Dec 06 '22 at 04:49

4 Answers4

108

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

This will print the top 10 elements of the stack.

Flow
  • 23,572
  • 15
  • 99
  • 156
  • What version of GDB is this? I can't get gdb to use registers as command arguments on GDB 7.7-0ubuntu3.1 – nightpool Nov 14 '14 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! –  Sep 12 '17 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 Nov 19 '18 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 Feb 14 '19 at 14:56
93

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
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • what if I have a variable also called frame? – Jeoker May 24 '21 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 `, or other commands entirely like `print` – Michael Mrozek May 25 '21 at 03:05
83
  • 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.

varesa
  • 2,399
  • 7
  • 26
  • 45
dlanod
  • 8,664
  • 8
  • 54
  • 96
7
  • 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.

kdbreboot
  • 79
  • 1
  • 4
  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-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. – Tomer Shetah Jan 11 '21 at 11:06