ead
ead

Reputation: 34347

GDB: Adding contents of stack to auto display

In gdb it is possible to show let's say 5 last values on the stack via:

(gdb) x/5x $sp
0x7fffffffde40: 0x00000001  0x00000000  0xffffe1e9  0x00007fff
0x7fffffffde50: 0x00000000

as explained here. However I would like to add it to auto display and was not able to come up with a solution. My try so far yields only the address of the last element on the stack:

(gdb) display/5x $sp
1: /x $sp = 0x7fffffffde40

as display seems just to skip 5.

Is is possible to show the content of the stack in the auto display?

Upvotes: 0

Views: 520

Answers (1)

Mark Plotnick
Mark Plotnick

Reputation: 10271

gdb's display command acts like the print command, and they differ a bit from the x command:

  • display and print don't use repeat counts in the /format option. If you give a count, display will ignore it, and print will complain about it.
  • display and print print the value of the expression, but x takes the value of the expression, treats it as an address, and prints the value in memory at that address. That's why, in your example, display /x $sp outputs 0x7fffffffde40 and x/x $sp outputs 0x00000001.

There are a couple of ways to get display (and print) to show a series of values starting at a given address:

  • prefix the expression (which is presumably an address, or a variable or register whose value is an address) with {type}, where type is an array type:

    display {int[5]}$sp
    
  • use the @ operator in the expression. @ represents an array starting at the address of its left-hand argument and containing the number of elements specified by its right-hand argument:

    print *(int *)$sp@5
    

Upvotes: 1

Related Questions