繁体   English   中英

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

[英]Get current size of the stack in bytes with GDB

是否可以使用 GDB(在断点处)以字节为单位获取堆栈的当前大小? 我在互联网上没有找到任何关于此的信息。

不清楚您是在问“我的线程消耗了多少堆栈”,还是“该线程将来可能消耗的最大堆栈大小是多少”。

第一个问题可以通过使用以下方法轻松回答:

# 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

要回答第二个问题,您需要使用调试符号构建的libpthread 如果使用 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

在这里您可以看到整个堆栈跨越[0x7ffff724c000, 0x7ffff7a4d000]区域。 您还可以确认$sp在该区域中:

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

暂无
暂无

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

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