A Python tool to analyze stack usage in C/C++ programs by combining compiler-generated stack usage information (.su
files) with function call graphs (generated by cflow
). This helps identify potential stack overflow issues in embedded systems or resource-constrained environments.
- Recursively scans directories for
.su
files generated by GCC - Parses call graphs from
cflow
output - Calculates both direct and total stack usage for each function
- Generates a comprehensive JSON report of stack usage analysis
- Identifies the full call chain contributing to stack usage
- Python 3.6+
- GCC with stack usage reporting capability
- GNU cflow
sudo apt install python3 gcc cflow
To see all available options:
python stack_analyzer.py --help
To generate .su
files, compile your C/C++ code with GCC using the -fstack-usage
flag:
gcc -fstack-usage -c file.c
This will produce a .su
file alongside each object file, containing stack usage information.
Add the flag to your CFLAGS:
CFLAGS += -fstack-usage
add_compile_options(-fstack-usage)
The tool expects cflow output in the GNU format with levels printed. Generate it using:
cflow --all --format=gnu --print-level <source-files> > callgraph.txt
The tool generates a JSON file with stack info of each found function.