有什么方法可以获取C语言中文件或项目中所有函数的堆栈大小?

发布于 2024-11-30 16:07:45 字数 74 浏览 2 评论 0原文

我想获取堆栈大小大于指定大小的所有函数,有什么简单的方法可以建议吗?

获取.obj 文件,反汇编它,然后分析输出文件?

I want to get all functions whose stack size is bigger than the assigned size, is there any easy method to suggest?

Get the .obj file, disassemble it, and then analyze the output file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-12-07 16:07:45

您可以查看每个函数的程序集转储。在 gcc 中,您可以使用 -S 选项。

假设您对函数 foo 感兴趣。然后,gcc 将函数名称修改为 _foo。如果您看一下程序集,您应该会在函数顶部附近看到一条移动堆栈指针的指令。例如,在 OSX 中,您可以看到如下内容:

_foo:
    ...
    movq %rsp, %rbp
    ...
    subq $48, %rsp

该数字 $48 是堆栈大小。

或者,您可以使用 nm 查找等效命令来查找函数开始的地址,使用 ndisasm 为您提供人类可读的转储,然后扫描函数所在的位置堆栈指针被移动。

You can look at the assembly dump for each function. In gcc, you use the -S option.

Let's say you are interested in the function foo. Then, gcc mangles the function name to _foo. If you take a peek at the assembly, you should see, near the top of the function, an instruction to move the stack pointer. For example, in OSX you have something like:

_foo:
    ...
    movq %rsp, %rbp
    ...
    subq $48, %rsp

That number, $48, is the stack size.

Alternatively, you can look for the equivalent command using nm to find the address where the function starts, ndisasm to give you a human readable dump, and then scanning for the where the stack pointer is moved.

菩提树下叶撕阳。 2024-12-07 16:07:45

在嵌入式计算之外,无限递归比任何特定调用更有可能导致堆栈溢出。

话虽这么说,在第一个近似值中,查找本地分配的数组:

void foo() {
    ...
    char buffer[1024] = "";
    ...
}

另外,不要忘记 alloca() 调用 - 它以与 malloc() 相同的方式在堆栈上动态分配空间 在堆上分配它。

Outside of embedded computing, infinite recursion is much more likely culprit in stack overflow than any particular call.

That being said, in the first approximation, look for locally-allocated arrays:

void foo() {
    ...
    char buffer[1024] = "";
    ...
}

Also, don't forget alloca() call - it dynamically allocates space on the stack the same way malloc() allocates it on the heap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文