MathematicalOrchid
MathematicalOrchid

Reputation: 62808

Access old stack frames

If I'm understanding this right, each time you call a C++ function, SP (and possibly BP) get moved to allocate some temporary space on the stack — a stack frame. And when the function returns, the pointers get moved back, deallocating the stack frame again.

But it appears to me that the data on the old stack frame is still there, it's just not referenced any more. Is there some way to make GDB show me these deleted stack frames? (Obviously once you enter a new stack frame it will at least partially overwrite any previous ones... but until then it seems like it should be possible.)

Upvotes: 2

Views: 509

Answers (2)

Employed Russian
Employed Russian

Reputation: 213385

But it appears to me that the data on the old stack frame is still there, it's just not referenced any more.

Correct.

Is there some way to make GDB show me these deleted stack frames?

You can trivially look at the unused stack with GDB examine command. For example:

void fn()
{
  int x[100];
  for (int j = 0; j < 100; j++) x[j] = (0x1234 << 12) + j;
}


int main()
{
  fn();
  return 0;
}

Build and debug with:

gcc -g t.c
gdb -q ./a.out

(gdb) start
Temporary breakpoint 1 at 0x115f: file t.c, line 10.
Starting program: /tmp/a.out 

Temporary breakpoint 1, main () at t.c:10
10    fn();
(gdb) n
11    return 0;
(gdb) x/40x $rsp-0x40
0x7fffffffdc60: 0x0123405c  0x0123405d  0x0123405e  0x0123405f
0x7fffffffdc70: 0x01234060  0x01234061  0x01234062  0x01234063
0x7fffffffdc80: 0x55555170  0x00005555  0x55555040  0x00000064
0x7fffffffdc90: 0xffffdca0  0x00007fff  0x55555169  0x00005555
0x7fffffffdca0: 0x55555170  0x00005555  0xf7a3a52b  0x00007fff
0x7fffffffdcb0: 0x00000000  0x00000000  0xffffdd88  0x00007fff
0x7fffffffdcc0: 0x00080000  0x00000001  0x5555515b  0x00005555
0x7fffffffdcd0: 0x00000000  0x00000000  0xa91c6994  0xc8f4292d
0x7fffffffdce0: 0x55555040  0x00005555  0xffffdd80  0x00007fff
0x7fffffffdcf0: 0x00000000  0x00000000  0x00000000  0x00000000

Here you can clearly see x still on stack: 0x7fffffffdc60 is where x[92] used to be, 0x7fffffffdc70 is where x[96] used to be, etc.

There is no easy way to make GDB interpret that data as locals of fn though.

Upvotes: 2

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

Stackframes do not contain any information about its size or boundaries, rather this knowledge is hardcoded into functions' code. There is a (stack) frame pointer register, using which makes it possible to walk the stack up, but not down. In the current function you know the boundaries of the current frame, but there is no information of what could possibly be below it.

Upvotes: 2

Related Questions