Inconsistent relation between debuginfo, opt-level and release/debug profile

Talk is cheap, let's just see code:

fn foo() { println!("Hey"); }

fn main() {
    println!("Hello world");

First, we build in release profile with debuginfo:

RUSTCFLAGS="-C debuginfo=2" cargo build --release

The result file will reside in target/release

Then, we build in debug profile with debuginfo and opt-level as 3 (debug build with release-level optimization):

RUSTCFLAGS="-C debuginfo=2 -C opt-level=3" cargo build

The result file will reside in target/debug

We can use llvm-dwarfdump to get the generated dwarf info:

llvm-dwarfdump -a path/to/binary | grep foo

Now we can get "foo" debuginfo in the second debug build, but not in the first release build.

It is strange, since this may be the only way to generate debuginfo in a release build (in my understanding, opt-level=3 just means release build, correct me if I am wrong).

OS: ubuntu 22.04
rustc version: rustc 1.68.2 (9eb3afe9e 2023-03-27)
llvm-dwarfdump version: Homebrew LLVM version 16.0.0

You can use

debug = 2

in your Cargo.toml to enable debuginfo for the release profile. And you can use

debug = 2
opt-level = 3

to enable debuginfo and optimizations for the dev profile (which is the official name of the profile used when not passing --release)

The difference between the dev and release profiles are bigger than the used optimization level and debuginfo. According to Profiles - The Cargo Book the defaults are:

opt-level = 0
debug = true
split-debuginfo = '...'  # Platform-specific.
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 256
rpath = false

opt-level = 3
debug = false
split-debuginfo = '...'  # Platform-specific.
debug-assertions = false
overflow-checks = false
lto = false
panic = 'unwind'
incremental = false
codegen-units = 16
rpath = false

But you are free to customize existing profiles and even create new profiles. These are just defaults that work fine in many cases, but definitively not all.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.