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
.sufiles generated by GCC - Parses call graphs from
cflowoutput - 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 cflowTo see all available options:
python stack_analyzer.py --helpTo generate .su files, compile your C/C++ code with GCC using the -fstack-usage flag:
gcc -fstack-usage -c file.cThis will produce a .su file alongside each object file, containing stack usage information.
Add the flag to your CFLAGS:
CFLAGS += -fstack-usageadd_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.txtThe tool generates a JSON file with stack info of each found function.