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

问题描述 投票:0回答:2

是否可以使用 GDB(在断点处)获取堆栈的当前大小(以字节为单位)?

我在互联网上没有找到任何与此相关的内容。

c debugging gdb stack
2个回答
3
投票

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

第一个问题可以通过使用简单地回答:

# 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

0
投票

您可以使用命令

info proc mappings
显示堆栈。堆栈的信息位于机器人

(gdb) info proc mappings
process 2281
Mapped address spaces:

        Start Addr   End Addr       Size     Offset objfile
        <SNIP>
        0xfffdc000 0xffffe000    0x22000        0x0  rwxp   [stack]

在本例中,尺寸为

0x22000

© www.soinside.com 2019 - 2024. All rights reserved.