简体   繁体   中英

Get current size of the stack in bytes with GDB

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.

It's unclear whether you are asking "how much stack did my thread consume", 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM