简体   繁体   English

使用 GDB 获取堆栈的当前大小(以字节为单位)

[英]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)?是否可以使用 GDB(在断点处)以字节为单位获取堆栈的当前大小? 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.要回答第二个问题,您需要使用调试符号构建的libpthread If using GLIBC, you can do this:如果使用 GLIBC,您可以这样做:

# 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.在这里您可以看到整个堆栈跨越[0x7ffff724c000, 0x7ffff7a4d000]区域。 You can also confirm that $sp is in that region:您还可以确认$sp在该区域中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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