How to create a Control-Flow Graph of a Rust crate?

โš“ Rust    ๐Ÿ“… 2025-08-13    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 2      

surdeus

I 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 rustc unstable flags
    The rustc debugging guide shows some interesting flags. I passed them to cargo with RUSTCFLAGS="-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 removing target/โ€” did nothing. Same using cargo 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-callgraph and callgraph.rs cited in this reply to a post. But none of them succeeded installing with cargo install --git URL --locked. I think the docs are probably saying that one should clone the repo, and put the file to test under test/ 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

Read full topic

๐Ÿท๏ธ Rust_feed