问题
I have few benchmarks like fft, dijkstra. I want to collect the stack size of all library functions and user defined functions. C code is also available.
I am managing cache in hardware, so i need the exact stack size of each small function, variable.
回答1:
If compiling with a recent GCC you could pass the -fstack-usage flag to gcc (in addition of optimization flags, if any) which:
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
staticmeans 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
dynamicmeans 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 qualifierboundedis 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.
You could also pass a -Wstack-usage=len warning flag, which:
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.
At last, you might consider using MELT to customize the compiler for your purposes. For example, you might consider extending GCC with your MELT extension to instrument dynamic stack usage in every of your functions.
Of course, if you want the same information for libraries, you should re-compile them from their source code.
BTW, the stack usage of some functions, or of some function calls occurrences, might be ill-defined (and certainly depends upon the optimization flags and the target system), since GCC is sometimes capable of tail call optimizations, and of function inlining (even on functions not qualified inline!) and/or function cloning. Also, some few C standard library functions (printf, memset, ....) are magically known to the compiler which might use some internal builtin functions to compile them. At last, several softwares (and more and more libraries) are compiled with link-time optimizations (using -flto), then the stack usage of individual functions is not well defined (since they are often inlined).
So I am not sure your question makes any precise sense. You might rephrase it and motivate and improve it.
来源:https://stackoverflow.com/questions/30044266/how-to-extract-stack-size-of-all-library-functions-from-c-code