Skip to content

Commit d65e2f1

Browse files
committed
Implement debuginfo path remapping
1 parent 7ad7232 commit d65e2f1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/librustc/session/config.rs

+20
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ top_level_options!(
253253
// Include the debug_assertions flag into dependency tracking, since it
254254
// can influence whether overflow checks are done or not.
255255
debug_assertions: bool [TRACKED],
256+
debug_prefix_map: Option<(String, String)> [TRACKED],
256257
debuginfo: DebugInfoLevel [TRACKED],
257258
lint_opts: Vec<(String, lint::Level)> [TRACKED],
258259
lint_cap: Option<lint::Level> [TRACKED],
@@ -445,6 +446,7 @@ pub fn basic_options() -> Options {
445446
libs: Vec::new(),
446447
unstable_features: UnstableFeatures::Disallow,
447448
debug_assertions: true,
449+
debug_prefix_map: None,
448450
actually_rustdoc: false,
449451
}
450452
}
@@ -801,6 +803,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
801803
"optimize with possible levels 0-3, s, or z"),
802804
debug_assertions: Option<bool> = (None, parse_opt_bool, [TRACKED],
803805
"explicitly enable the cfg(debug_assertions) directive"),
806+
debug_prefix_map: String = ("".to_string(), parse_string, [TRACKED],
807+
"remap OLD to NEW in debug info (OLD=NEW)"),
804808
inline_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
805809
"set the inlining threshold for"),
806810
panic: Option<PanicStrategy> = (None, parse_panic_strategy,
@@ -1423,6 +1427,20 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14231427
}
14241428
};
14251429
let debug_assertions = cg.debug_assertions.unwrap_or(opt_level == OptLevel::No);
1430+
1431+
let debug_prefix_map = if !cg.debug_prefix_map.is_empty() {
1432+
let mut parts = cg.debug_prefix_map.splitn(2, '=');
1433+
let old = parts.next().unwrap();
1434+
let new = match parts.next() {
1435+
Some(new) => new,
1436+
None => early_error(error_format,
1437+
"-C debug-prefix-map value must be of the format `OLD=NEW`"),
1438+
};
1439+
Some((old.to_string(), new.to_string()))
1440+
} else {
1441+
None
1442+
};
1443+
14261444
let debuginfo = if matches.opt_present("g") {
14271445
if cg.debuginfo.is_some() {
14281446
early_error(error_format, "-g and -C debuginfo both provided");
@@ -1539,6 +1557,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15391557
libs: libs,
15401558
unstable_features: UnstableFeatures::from_environment(),
15411559
debug_assertions: debug_assertions,
1560+
debug_prefix_map: debug_prefix_map,
15421561
actually_rustdoc: false,
15431562
},
15441563
cfg)
@@ -1714,6 +1733,7 @@ mod dep_tracking {
17141733
impl_dep_tracking_hash_via_hash!(Option<bool>);
17151734
impl_dep_tracking_hash_via_hash!(Option<usize>);
17161735
impl_dep_tracking_hash_via_hash!(Option<String>);
1736+
impl_dep_tracking_hash_via_hash!(Option<(String, String)>);
17171737
impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
17181738
impl_dep_tracking_hash_via_hash!(Option<lint::Level>);
17191739
impl_dep_tracking_hash_via_hash!(Option<PathBuf>);

src/librustc_trans/debuginfo/metadata.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ pub fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
657657
metadata
658658
}
659659

660+
fn remap_path(sess: &Session, path: &str) -> String {
661+
for (old, new) in sess.opts.debug_prefix_map.clone() {
662+
if path.starts_with(&old) {
663+
return new + &path[old.len()..];
664+
}
665+
}
666+
path.to_string()
667+
}
668+
660669
pub fn file_metadata(cx: &CrateContext, path: &str, full_path: &Option<String>) -> DIFile {
661670
// FIXME (#9639): This needs to handle non-utf8 paths
662671
let work_dir = cx.sess().working_dir.to_str().unwrap();
@@ -669,7 +678,10 @@ pub fn file_metadata(cx: &CrateContext, path: &str, full_path: &Option<String>)
669678
}
670679
});
671680

672-
file_metadata_(cx, path, file_name, &work_dir)
681+
let sess = cx.sess();
682+
let remap_file_name = remap_path(sess, file_name);
683+
let remap_dir = remap_path(sess, work_dir);
684+
file_metadata_(cx, path, &remap_file_name, &remap_dir)
673685
}
674686

675687
pub fn unknown_file_metadata(cx: &CrateContext) -> DIFile {
@@ -759,7 +771,7 @@ pub fn compile_unit_metadata(scc: &SharedCrateContext,
759771
debug_context: &CrateDebugContext,
760772
sess: &Session)
761773
-> DIDescriptor {
762-
let work_dir = &sess.working_dir;
774+
let work_dir = sess.working_dir.to_str().unwrap();
763775
let compile_unit_name = match sess.local_crate_source_file {
764776
None => fallback_path(scc),
765777
Some(ref abs_path) => {
@@ -781,12 +793,14 @@ pub fn compile_unit_metadata(scc: &SharedCrateContext,
781793
}
782794
};
783795

796+
let work_dir = remap_path(sess, work_dir);
797+
784798
debug!("compile_unit_metadata: {:?}", compile_unit_name);
785799
let producer = format!("rustc version {}",
786800
(option_env!("CFG_VERSION")).expect("CFG_VERSION"));
787801

788802
let compile_unit_name = compile_unit_name.as_ptr();
789-
let work_dir = path2cstr(&work_dir);
803+
let work_dir = CString::new(work_dir).unwrap();
790804
let producer = CString::new(producer).unwrap();
791805
let flags = "\0";
792806
let split_name = "\0";

0 commit comments

Comments
 (0)