Skip to content

Commit bda71e2

Browse files
committed
Support -Zembed-source in debuginfo
- honour the session DWARF version, enabling v5 when embed-source is requested and plumb the flag through the debug context - attach embedded source bytes to line table FileInfo records alongside existing MD5 hashes - stop excluding rustc’s embed-source-dwarf run-make test so CG_CLIF exercises the feature
1 parent 1988e68 commit bda71e2

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

scripts/test_rustc_tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ rm tests/ui/asm/global-asm-mono-sym-fn.rs # same
6060
rm tests/ui/asm/naked-asm-mono-sym-fn.rs # same
6161
rm tests/ui/asm/x86_64/goto.rs # inline asm labels not supported
6262
rm tests/ui/simd/simd-bitmask-notpow2.rs # non-pow-of-2 simd vector sizes
63-
rm -r tests/run-make/embed-source-dwarf # embedding sources in debuginfo
6463
rm -r tests/run-make/used-proc-macro # used(linker) isn't supported yet
6564
rm tests/ui/linking/no-gc-encapsulation-symbols.rs # same
6665
rm tests/ui/attributes/fn-align-dyn.rs # per-function alignment not supported

src/debuginfo/line_info.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::path::{Component, Path};
66
use cranelift_codegen::MachSrcLoc;
77
use cranelift_codegen::binemit::CodeOffset;
88
use gimli::write::{AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable};
9-
use rustc_span::{
10-
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm, hygiene,
11-
};
9+
use rustc_span::{FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHashAlgorithm, hygiene};
1210

1311
use crate::debuginfo::FunctionDebugContext;
1412
use crate::debuginfo::emit::address_for_func;
@@ -44,21 +42,27 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
4442
}
4543
}
4644

47-
const MD5_LEN: usize = 16;
48-
49-
fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
50-
if hash.kind == SourceFileHashAlgorithm::Md5 {
51-
let mut buf = [0u8; MD5_LEN];
52-
buf.copy_from_slice(hash.hash_bytes());
53-
Some(FileInfo {
54-
timestamp: 0,
55-
size: 0,
56-
md5: buf,
57-
source: None, // FIXME implement -Zembed-source
58-
})
59-
} else {
60-
None
45+
fn make_file_info(source_file: &SourceFile, embed_source: bool) -> Option<FileInfo> {
46+
let has_md5 = source_file.src_hash.kind == SourceFileHashAlgorithm::Md5;
47+
let has_source = embed_source && source_file.src.is_some();
48+
49+
if !has_md5 && !has_source {
50+
return None;
51+
}
52+
53+
let mut info = FileInfo::default();
54+
55+
if has_md5 {
56+
info.md5.copy_from_slice(source_file.src_hash.hash_bytes());
6157
}
58+
59+
if embed_source {
60+
if let Some(src) = &source_file.src {
61+
info.source = Some(LineString::String(src.as_bytes().to_vec()));
62+
}
63+
}
64+
65+
Some(info)
6266
}
6367

6468
impl DebugContext {
@@ -105,9 +109,10 @@ impl DebugContext {
105109
let file_name =
106110
LineString::new(file_name, line_program.encoding(), line_strings);
107111

108-
let info = make_file_info(source_file.src_hash);
112+
let info = make_file_info(source_file, self.embed_source);
109113

110-
line_program.file_has_md5 &= info.is_some();
114+
let has_md5 = source_file.src_hash.kind == SourceFileHashAlgorithm::Md5;
115+
line_program.file_has_md5 &= has_md5;
111116
line_program.add_file(file_name, dir_id, info)
112117
}
113118
filename => {

src/debuginfo/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(crate) struct DebugContext {
4545
array_size_type: UnitEntryId,
4646

4747
filename_display_preference: FileNameDisplayPreference,
48+
embed_source: bool,
4849
}
4950

5051
pub(crate) struct FunctionDebugContext {
@@ -67,21 +68,24 @@ impl DebugContext {
6768
return None;
6869
}
6970

71+
let mut requested_dwarf_version = tcx.sess.dwarf_version();
72+
if tcx.sess.target.is_like_darwin && requested_dwarf_version > 4 {
73+
// Apple’s shipped debuggers still expect DWARF <= 4 by default.
74+
// Stay on v4 unless the user explicitly opts into a feature that
75+
// only works with v5 (e.g. -Zembed-source).
76+
if !tcx.sess.opts.unstable_opts.embed_source {
77+
requested_dwarf_version = 4;
78+
}
79+
}
80+
7081
let encoding = Encoding {
7182
format: Format::Dwarf32,
72-
// FIXME this should be configurable
73-
// macOS doesn't seem to support DWARF > 3
74-
// 5 version is required for md5 file hash
75-
version: if tcx.sess.target.is_like_darwin {
76-
3
77-
} else {
78-
// FIXME change to version 5 once the gdb and lldb shipping with the latest debian
79-
// support it.
80-
4
81-
},
83+
version: requested_dwarf_version as u16,
8284
address_size: isa.frontend_config().pointer_bytes(),
8385
};
8486

87+
let embed_source = tcx.sess.opts.unstable_opts.embed_source && encoding.version >= 5;
88+
8589
let endian = match isa.endianness() {
8690
Endianness::Little => RunTimeEndian::Little,
8791
Endianness::Big => RunTimeEndian::Big,
@@ -125,6 +129,9 @@ impl DebugContext {
125129
file_info,
126130
);
127131
line_program.file_has_md5 = file_has_md5;
132+
if embed_source {
133+
line_program.file_has_source = true;
134+
}
128135

129136
dwarf.unit.line_program = line_program;
130137

@@ -169,6 +176,7 @@ impl DebugContext {
169176
namespace_map: DefIdMap::default(),
170177
array_size_type,
171178
filename_display_preference,
179+
embed_source,
172180
})
173181
}
174182

0 commit comments

Comments
 (0)