linux - 如何从c代码中提取所有库函数的堆栈大小?

标签 linux gcc assembly dll stack

我几乎没有像 fft、dijkstra 这样的基准测试。我想收集所有库函数和用户定义函数的堆栈大小。 C 代码也可用。

我正在硬件中管理缓存,因此我需要每个小函数、变量的确切堆栈大小。

最佳答案

如果使用最近的 GCC 进行编译你可以通过-fstack-usage标记为gcc(除了优化标记,如果有的话),其中:

Makes the compiler output stack usage information for the program, on a per-function basis. The filename for the dump is made by appending .su to the auxname. auxname is generated from the name of the output file, if explicitly specified and it is not an executable, otherwise it is the basename of the source file. An entry is made up of three fields:

The name of the function. A number of bytes. One or more qualifiers: static, dynamic, bounded.

The qualifier static means that the function manipulates the stack statically: a fixed number of bytes are allocated for the frame on function entry and released on function exit; no stack adjustments are otherwise made in the function. The second field is this fixed number of bytes.

The qualifier dynamic means that the function manipulates the stack dynamically: in addition to the static allocation described above, stack adjustments are made in the body of the function, for example to push/pop arguments around function calls. If the qualifier bounded is also present, the amount of these adjustments is bounded at compile time and the second field is an upper bound of the total amount of stack used by the function. If it is not present, the amount of these adjustments is not bounded at compile time and the second field only represents the bounded part.

您还可以传递 -Wstack-usage=len warning flag ,其中:

Warn if the stack usage of a function might be larger than len bytes. The computation done to determine the stack usage is conservative. Any space allocated via alloca, variable-length arrays, or related constructs is included by the compiler when determining whether or not to issue a warning.

您可以考虑写您的 GCC plugin提取由最近的 GCC 编译的函数的堆栈大小(例如 2020 年 10 月的 GCC 10),并且由于 GCC 是 free software ,你可以改进它。

当然,如果您想要库的相同信息,您应该从源代码重新编译它们。

顺便说一句,某些函数或某些函数调用发生的堆栈使用可能是不明确的(并且当然取决于优化标志和目标系统),因为 GCC 有时能够 tail call优化和功能inlining (即使是在未限定内联的函数上!)和/或 function cloning 。另外,一些少数 C standard library编译器神奇地知道函数(printfmemset、...),它们可能会使用一些内部 builtin函数来编译它们。最后,用 link-time optimizations 编译了几个软件(以及越来越多的库)。 (使用 -flto ),那么单个函数的堆栈使用没有明确定义(因为它们通常是内联的)。

所以我不确定你的问题是否有任何精确意义。您可以重新措辞并激励和改进它。

关于linux - 如何从c代码中提取所有库函数的堆栈大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044266/

相关文章:

linux - 在内核模块中预定义一个宏

linux - 挂载 Windows XP dd 镜像失败

linux - Linux 中的返回信号

c - 海湾合作委员会警告: assignment makes integer from pointer without a cast

windows - 如何让 gcc 接受路径中的空格

c++ - 数组中的函数指针(nix c++)

linux - 使用 cygwin 运行 "./"bash/批处理文件

c - 这个 MIPS strlen 是否从相应的 C 循环正确转换而来?

assembly - 如何获取 VESA BIOS 信息

assembly - L.D F0,0(R1)是什么意思?