【发布时间】:2021-12-15 18:45:54
【问题描述】:
是否可以使用 GDB(在断点处)获取堆栈的当前大小(以字节为单位)?
我在 Internet 上没有找到任何关于此的信息。
【问题讨论】:
是否可以使用 GDB(在断点处)获取堆栈的当前大小(以字节为单位)?
我在 Internet 上没有找到任何关于此的信息。
【问题讨论】:
不清楚您是在问“我的线程到目前为止消耗了多少堆栈”,还是“这个线程将来可能消耗的最大堆栈大小是多少”。
第一个问题可以简单回答:
# 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
【讨论】: