How to create a Control-Flow Graph of a Rust crate?
โ Rust ๐ 2025-08-13 ๐ค surdeus ๐๏ธ 12I am trying to create a Control-Flow Graph:
a representation using graph notation, of all paths that might be traversed through a program during its execution
The graph is not of a specific run, but rather a control-flow description of the static program.
The expectation is to generate a .dot file and the convert it to .png for visualisation.
What I tried so far
-
Using
rustcunstable flags
Therustcdebugging guide shows some interesting flags. I passed them to cargo withRUSTCFLAGS="-Zdump-mir=main -Zdump-mir-dataflow -Zdump-mir-graphviz"; cargo +nightly build.No need for the mir-dataflow though, just the control-flow diagram. That command (or a very similar one) ran successfully once, producing the
mir-dump/but subsequent runs โeven removingtarget/โ did nothing. Same usingcargo clean.
The expectation is that the MIR would be quite helpful, but HIR or just the source-code's CFG directly would be better. -
Another try were
cargo-callgraphandcallgraph.rscited in this reply to a post. But none of them succeeded installing withcargo install --git URL --locked. I think the docs are probably saying that one should clone the repo, and put the file to test undertest/to build the callgraph. I didn't try that yet.
So, can cargo +nightly currently do this without any package nor plugin ? Any maintained package that you recommend (or insist on the above)?
1 post - 1 participant
๐ท๏ธ Rust_feed