Skip to content

Commit

Permalink
Use a DWARF version consistent with rustc
Browse files Browse the repository at this point in the history
Rustc defaults to DWARF-2 on some targets, and DWARF-4 on others.
However using -g with the C compiler yields whatever default version the
C compiler prefers.

One side effect is that the DWARF debug info shipped in some libraries
with rustc itself (e.g. libcompiler_builtins and others) have recently
switched to DWARF-5 as a side effect of upgrading the clang version
used on rustc CI. (rust-lang/rust#98746)

Ideally, the preferred DWARF version would be given by the rust compiler
and/or cargo, but that's not the case at the moment, so the next best
thing is something that aligns with the current defaults, although
work in under way to add a rustc flag that would allow to pick the
preferred DWARF version (rust-lang/rust#98350)
  • Loading branch information
glandium committed Nov 10, 2022
1 parent 8d4cc27 commit 380d5d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
29 changes: 26 additions & 3 deletions src/lib.rs
Expand Up @@ -214,13 +214,17 @@ enum ToolFamily {

impl ToolFamily {
/// What the flag to request debug info for this family of tools look like
fn add_debug_flags(&self, cmd: &mut Tool) {
fn add_debug_flags(&self, cmd: &mut Tool, dwarf_version: Option<u32>) {
match *self {
ToolFamily::Msvc { .. } => {
cmd.push_cc_arg("-Z7".into());
}
ToolFamily::Gnu | ToolFamily::Clang => {
cmd.push_cc_arg("-g".into());
cmd.push_cc_arg(
dwarf_version
.map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v))
.into(),
);
}
}
}
Expand Down Expand Up @@ -1589,7 +1593,7 @@ impl Build {
cmd.args.push("-G".into());
}
let family = cmd.family;
family.add_debug_flags(cmd);
family.add_debug_flags(cmd, self.get_dwarf_version());
}

if self.get_force_frame_pointer() {
Expand Down Expand Up @@ -2848,6 +2852,25 @@ impl Build {
})
}

fn get_dwarf_version(&self) -> Option<u32> {
// Tentatively matches the DWARF version defaults as of rustc 1.62.
let target = self.get_target().ok()?;
if target.contains("android")
|| target.contains("apple")
|| target.contains("dragonfly")
|| target.contains("freebsd")
|| target.contains("netbsd")
|| target.contains("openbsd")
|| target.contains("windows-gnu")
{
Some(2)
} else if target.contains("linux") {
Some(4)
} else {
None
}
}

fn get_force_frame_pointer(&self) -> bool {
self.force_frame_pointer.unwrap_or_else(|| self.get_debug())
}
Expand Down
25 changes: 20 additions & 5 deletions tests/test.rs
Expand Up @@ -20,7 +20,7 @@ fn gnu_smoke() {
test.cmd(0)
.must_have("-O2")
.must_have("foo.c")
.must_not_have("-g")
.must_not_have("-gdwarf-4")
.must_have("-c")
.must_have("-ffunction-sections")
.must_have("-fdata-sections");
Expand Down Expand Up @@ -52,19 +52,34 @@ fn gnu_opt_level_s() {
.must_not_have("-Oz");
}

#[test]
fn gnu_debug() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-gdwarf-4");

let test = Test::gnu();
test.gcc()
.target("x86_64-apple-darwin")
.debug(true)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-gdwarf-2");
}

#[test]
fn gnu_debug_fp_auto() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_have("-fno-omit-frame-pointer");
}

#[test]
fn gnu_debug_fp() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_have("-fno-omit-frame-pointer");
}

Expand All @@ -78,7 +93,7 @@ fn gnu_debug_nofp() {
.force_frame_pointer(false)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_not_have("-fno-omit-frame-pointer");

let test = Test::gnu();
Expand All @@ -87,7 +102,7 @@ fn gnu_debug_nofp() {
.debug(true)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_not_have("-fno-omit-frame-pointer");
}

Expand Down

0 comments on commit 380d5d8

Please sign in to comment.