Skip to content

Embed MSVC .natvis files into .pdbs and mangle debuginfo for &str, *T, and [T]. #43221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/etc/natvis/intrinsic.natvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="str">
<DisplayString>{data_ptr,[length]}</DisplayString>
<StringView>data_ptr,[length]</StringView>
<Expand>
<Item Name="[size]" ExcludeView="simple">length</Item>
<ArrayItems>
<Size>length</Size>
<ValuePointer>data_ptr</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="slice&lt;*&gt;">
<DisplayString>{{ length={length} }}</DisplayString>
<Expand>
<Item Name="[size]" ExcludeView="simple">length</Item>
<ArrayItems>
<Size>length</Size>
<ValuePointer>data_ptr</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>
25 changes: 25 additions & 0 deletions src/librustc_trans/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,31 @@ impl<'a> Linker for MsvcLinker<'a> {
// This will cause the Microsoft linker to generate a PDB file
// from the CodeView line tables in the object files.
self.cmd.arg("/DEBUG");

// This will cause the Microsoft linker to embed .natvis info into the the PDB file
let sysroot = self.sess.sysroot();
let natvis_dir_path = sysroot.join("lib\\rustlib\\etc");
if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
for entry in natvis_dir {
match entry {
Ok(entry) => {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == OsStr::new("natvis") {
if let Some(natvis_path_str) = path.to_str() {
self.cmd.arg(&format!("/NATVIS:{}",natvis_path_str));
} else {
self.sess.warn(&format!("natvis path not unicode: {:?}", path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command::arg() should be able to take a OsStr. You probably can't use format! with that but you could build up the OsString with its push method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Testing a patch now.

}
}
}
},
Err(err) => {
self.sess.warn(&format!("error enumerating natvis directory: {}", err));
},
}
}
}
}

// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
Expand Down
34 changes: 30 additions & 4 deletions src/librustc_trans/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
t: Ty<'tcx>,
qualified: bool,
output: &mut String) {
// When targeting MSVC, emit C++ style type names for compatability with
// .natvis visualizers (and perhaps other existing native debuggers?)
let cpp_like_names = cx.sess().target.target.options.is_like_msvc;

match t.sty {
ty::TyBool => output.push_str("bool"),
ty::TyChar => output.push_str("char"),
Expand All @@ -61,21 +65,33 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
output.push(')');
},
ty::TyRawPtr(ty::TypeAndMut { ty: inner_type, mutbl } ) => {
output.push('*');
if !cpp_like_names {
output.push('*');
}
match mutbl {
hir::MutImmutable => output.push_str("const "),
hir::MutMutable => output.push_str("mut "),
}

push_debuginfo_type_name(cx, inner_type, true, output);

if cpp_like_names {
output.push('*');
}
},
ty::TyRef(_, ty::TypeAndMut { ty: inner_type, mutbl }) => {
output.push('&');
if !cpp_like_names {
output.push('&');
}
if mutbl == hir::MutMutable {
output.push_str("mut ");
}

push_debuginfo_type_name(cx, inner_type, true, output);

if cpp_like_names {
output.push('*');
}
},
ty::TyArray(inner_type, len) => {
output.push('[');
Expand All @@ -84,9 +100,19 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
output.push(']');
},
ty::TySlice(inner_type) => {
output.push('[');
if cpp_like_names {
output.push_str("slice<");
} else {
output.push('[');
}

push_debuginfo_type_name(cx, inner_type, true, output);
output.push(']');

if cpp_like_names {
output.push('>');
} else {
output.push(']');
}
},
ty::TyDynamic(ref trait_data, ..) => {
if let Some(principal) = trait_data.principal() {
Expand Down