Skip to content

Commit 99b1369

Browse files
committed
Auto merge of #11572 - khuey:dwp, r=weihanglo
Make cargo aware of dwp files. 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. The split_debuginfo option is passed down into file_types to make this possible. For cargo clean manual handling is added to match the other split_debuginfo files. bin_link_for_target also passes in None because it won't care about the dwp file.
2 parents f6cf5ab + a393267 commit 99b1369

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,17 @@ impl Execs {
832832
self
833833
}
834834

835+
pub fn enable_split_debuginfo_packed(&mut self) -> &mut Self {
836+
self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed")
837+
.env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed")
838+
.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed")
839+
.env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed");
840+
self
841+
}
842+
835843
pub fn enable_mac_dsym(&mut self) -> &mut Self {
836844
if cfg!(target_os = "macos") {
837-
self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed")
838-
.env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed")
839-
.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed")
840-
.env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed");
845+
return self.enable_split_debuginfo_packed();
841846
}
842847
self
843848
}

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,25 @@ impl TargetInfo {
473473
// preserved.
474474
should_replace_hyphens: true,
475475
})
476+
} else {
477+
// Because DWARF Package (dwp) files are produced after the
478+
// fact by another tool, there is nothing in the binary that
479+
// provides a means to locate them. By convention, debuggers
480+
// take the binary filename and append ".dwp" (including to
481+
// binaries that already have an extension such as shared libs)
482+
// to find the dwp.
483+
ret.push(FileType {
484+
// It is important to preserve the existing suffix for
485+
// e.g. shared libraries, where the dwp for libfoo.so is
486+
// expected to be at libfoo.so.dwp.
487+
suffix: format!("{suffix}.dwp"),
488+
prefix: prefix.clone(),
489+
flavor: FileFlavor::DebugInfo,
490+
crate_type: Some(crate_type.clone()),
491+
// Likewise, the dwp needs to match the primary artifact's
492+
// hyphenation exactly.
493+
should_replace_hyphens: crate_type != CrateType::Bin,
494+
})
476495
}
477496
}
478497

src/cargo/ops/cargo_clean.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
203203
rm_rf_glob(&split_debuginfo_obj, config, &mut progress)?;
204204
let split_debuginfo_dwo = dir_glob.join(format!("{}.*.dwo", crate_name));
205205
rm_rf_glob(&split_debuginfo_dwo, config, &mut progress)?;
206+
let split_debuginfo_dwp = dir_glob.join(format!("{}.*.dwp", crate_name));
207+
rm_rf_glob(&split_debuginfo_dwp, config, &mut progress)?;
206208

207209
// Remove the uplifted copy.
208210
if let Some(uplift_dir) = uplift_dir {

tests/testsuite/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,6 +5216,29 @@ fn uplift_pdb_of_bin_on_windows() {
52165216
assert!(!p.target_debug_dir().join("d.pdb").exists());
52175217
}
52185218

5219+
#[cargo_test]
5220+
#[cfg(target_os = "linux")]
5221+
fn uplift_dwp_of_bin_on_linux() {
5222+
let p = project()
5223+
.file("src/main.rs", "fn main() { panic!(); }")
5224+
.file("src/bin/b.rs", "fn main() { panic!(); }")
5225+
.file("src/bin/foo-bar.rs", "fn main() { panic!(); }")
5226+
.file("examples/c.rs", "fn main() { panic!(); }")
5227+
.file("tests/d.rs", "fn main() { panic!(); }")
5228+
.build();
5229+
5230+
p.cargo("build --bins --examples --tests")
5231+
.enable_split_debuginfo_packed()
5232+
.run();
5233+
assert!(p.target_debug_dir().join("foo.dwp").is_file());
5234+
assert!(p.target_debug_dir().join("b.dwp").is_file());
5235+
assert!(p.target_debug_dir().join("examples/c.dwp").exists());
5236+
assert!(p.target_debug_dir().join("foo-bar").is_file());
5237+
assert!(p.target_debug_dir().join("foo-bar.dwp").is_file());
5238+
assert!(!p.target_debug_dir().join("c.dwp").exists());
5239+
assert!(!p.target_debug_dir().join("d.dwp").exists());
5240+
}
5241+
52195242
// Ensure that `cargo build` chooses the correct profile for building
52205243
// targets based on filters (assuming `--profile` is not specified).
52215244
#[cargo_test]

0 commit comments

Comments
 (0)