Skip to content

Commit

Permalink
Make cargo aware of dwp files.
Browse files Browse the repository at this point in the history
When using -Csplit-debuginfo=packed on Linux, rustc will produce a dwp file.
Unlike the dwo files, whose paths are embedded into the binary, there's no
information in the binary to help a debugger locate a dwp file. By convention,
the dwp file for <EXE> is given the name <EXE>.dwp and placed next to <EXE>.

When cargo hardlinks the executable file rustc put in target/debug/deps into
target/debug, it also needs to hardlink the dwp file along with it. Failing to
do this prevents the debugger from finding the dwp file when the binary is
executed from target/debug, as there's no way for the debugger to know to look
in the deps subdirectory.
  • Loading branch information
khuey committed Jan 14, 2023
1 parent 1cd6d38 commit f21f2b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,17 @@ impl Execs {
self
}

pub fn enable_split_debuginfo_packed(&mut self) -> &mut Self {
self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed");
self
}

pub fn enable_mac_dsym(&mut self) -> &mut Self {
if cfg!(target_os = "macos") {
self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed")
.env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed");
return self.enable_split_debuginfo_packed();
}
self
}
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ impl TargetInfo {
// preserved.
should_replace_hyphens: true,
})
} else {
ret.push(FileType {
suffix: format!("{}.dwp", suffix),
prefix: prefix.clone(),
flavor: FileFlavor::DebugInfo,
crate_type: Some(crate_type.clone()),
should_replace_hyphens: crate_type != CrateType::Bin,
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
rm_rf_glob(&split_debuginfo_obj, config, &mut progress)?;
let split_debuginfo_dwo = dir_glob.join(format!("{}.*.dwo", crate_name));
rm_rf_glob(&split_debuginfo_dwo, config, &mut progress)?;
let split_debuginfo_dwp = dir_glob.join(format!("{}.*.dwp", crate_name));
rm_rf_glob(&split_debuginfo_dwp, config, &mut progress)?;

// Remove the uplifted copy.
if let Some(uplift_dir) = uplift_dir {
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5216,6 +5216,29 @@ fn uplift_pdb_of_bin_on_windows() {
assert!(!p.target_debug_dir().join("d.pdb").exists());
}

#[cargo_test]
#[cfg(target_os = "linux")]
fn uplift_dwp_of_bin_on_linux() {
let p = project()
.file("src/main.rs", "fn main() { panic!(); }")
.file("src/bin/b.rs", "fn main() { panic!(); }")
.file("src/bin/foo-bar.rs", "fn main() { panic!(); }")
.file("examples/c.rs", "fn main() { panic!(); }")
.file("tests/d.rs", "fn main() { panic!(); }")
.build();

p.cargo("build --bins --examples --tests")
.enable_split_debuginfo_packed()
.run();
assert!(p.target_debug_dir().join("foo.dwp").is_file());
assert!(p.target_debug_dir().join("b.dwp").is_file());
assert!(p.target_debug_dir().join("examples/c.dwp").exists());
assert!(p.target_debug_dir().join("foo-bar").is_file());
assert!(p.target_debug_dir().join("foo-bar.dwp").is_file());
assert!(!p.target_debug_dir().join("c.dwp").exists());
assert!(!p.target_debug_dir().join("d.dwp").exists());
}

// Ensure that `cargo build` chooses the correct profile for building
// targets based on filters (assuming `--profile` is not specified).
#[cargo_test]
Expand Down

0 comments on commit f21f2b4

Please sign in to comment.