Home > front end >  Get current size of the stack in bytes with GDB
Get current size of the stack in bytes with GDB

Time:11-01

Is it possible to get current size of the stack in bytes with GDB (at a breakpoint)? I didn't find anything regarding this on the Internet.

CodePudding user response:

It's unclear whether you are asking "how much stack have my thread consumed so far", or "what is the maximum stack size this thread may consume in the future".

The first question can be trivially answered by using:

# go to the innermost frame
(gdb) down 100000
(gdb) set var $stack = $sp

# go to the outermost frame
(gdb) up 100000
(gdb) print $sp - $stack

To answer the second question, you would need libpthread that is built with debug symbols. If using GLIBC, you can do this:

# Go to frame which is `start_thread`
(gdb) frame 2 
#2  0x00007ffff7d7eeae in start_thread (arg=0x7ffff7a4c640) at pthread_create.c:463
463     in pthread_create.c

(gdb) p pd.stackblock
$1 = (void *) 0x7ffff724c000   # low end of stack block
(gdb) p pd.stackblock_size
$1 = 8392704

Here you can see that the entire stack spans [0x7ffff724c000, 0x7ffff7a4d000] region. You can also confirm that $sp is in that region, near the high address end of the stack (which grows from high to low addresses on this system):

(gdb) p $sp
$9 = (void *) 0x7ffff7a4be60
  • Related