How to get assembly output from building with Cargo?

后端 未结 3 1125
你的背包
你的背包 2020-12-24 11:18

While I\'ve seen docs on using rustc directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.<

3条回答
  •  猫巷女王i
    2020-12-24 11:23

    Both existing answers (using cargo rustc and RUSTFLAGS) are the best ways to obtain assembly with standard tools. If you find yourself trying to look at assembly fairly often, you might want to consider using the cargo asm subcommand. After installing it with cargo install cargo-asm, you can print assembly like:

    cargo build --release
    cargo asm my_crate::my_function
    

    There are a few things to pay attention to, though:

    • Unsure about the path of your function? Just run cargo asm and it will list all symbols you can inspect.
    • You have to cargo build --release before trying to look at the assembly, because cargo asm (apparently) only looks at the already existing build-artifacts
    • The code for the function you want to inspect has to be actually generated. For generic functions this means that the function has to be instantiated/monomorphized with a concrete type. If that doesn't happen in your crate, you can always add a dummy function at the top level that does everything you want to inspect the assembly of.

提交回复
热议问题