Skip to content

Commit b80455e

Browse files
committed
Add -Z embed-source=yes to embed source code in DWARF debug info
1 parent 536235f commit b80455e

File tree

9 files changed

+45
-2
lines changed

9 files changed

+45
-2
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+9
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
639639
};
640640
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
641641

642+
let source =
643+
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
644+
642645
unsafe {
643646
llvm::LLVMRustDIBuilderCreateFile(
644647
DIB(cx),
@@ -649,6 +652,8 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
649652
hash_kind,
650653
hash_value.as_ptr().cast(),
651654
hash_value.len(),
655+
source.map_or(ptr::null(), |x| x.as_ptr().cast()),
656+
source.map_or(0, |x| x.len()),
652657
)
653658
}
654659
}
@@ -669,6 +674,8 @@ pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
669674
llvm::ChecksumKind::None,
670675
hash_value.as_ptr().cast(),
671676
hash_value.len(),
677+
ptr::null(),
678+
0,
672679
)
673680
})
674681
}
@@ -915,6 +922,8 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
915922
llvm::ChecksumKind::None,
916923
ptr::null(),
917924
0,
925+
ptr::null(),
926+
0,
918927
);
919928

920929
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,8 @@ extern "C" {
18561856
CSKind: ChecksumKind,
18571857
Checksum: *const c_char,
18581858
ChecksumLen: size_t,
1859+
Source: *const c_char,
1860+
SourceLen: size_t,
18591861
) -> &'a DIFile;
18601862

18611863
pub fn LLVMRustDIBuilderCreateSubroutineType<'a>(

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ fn test_unstable_options_tracking_hash() {
770770
tracked!(direct_access_external_data, Some(true));
771771
tracked!(dual_proc_macros, true);
772772
tracked!(dwarf_version, Some(5));
773+
tracked!(embed_source, true);
773774
tracked!(emit_thin_lto, false);
774775
tracked!(export_executable_symbols, true);
775776
tracked!(fewer_names, Some(true));

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,19 @@ extern "C" LLVMMetadataRef
901901
LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
902902
size_t FilenameLen, const char *Directory,
903903
size_t DirectoryLen, LLVMRustChecksumKind CSKind,
904-
const char *Checksum, size_t ChecksumLen) {
904+
const char *Checksum, size_t ChecksumLen,
905+
const char *Source, size_t SourceLen) {
905906

906907
std::optional<DIFile::ChecksumKind> llvmCSKind = fromRust(CSKind);
907908
std::optional<DIFile::ChecksumInfo<StringRef>> CSInfo{};
908909
if (llvmCSKind)
909910
CSInfo.emplace(*llvmCSKind, StringRef{Checksum, ChecksumLen});
911+
std::optional<StringRef> oSource{};
912+
if (Source)
913+
oSource = StringRef(Source, SourceLen);
910914
return wrap(Builder->createFile(StringRef(Filename, FilenameLen),
911-
StringRef(Directory, DirectoryLen), CSInfo));
915+
StringRef(Directory, DirectoryLen), CSInfo,
916+
oSource));
912917
}
913918

914919
extern "C" LLVMMetadataRef

compiler/rustc_session/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ session_crate_name_empty = crate name must not be empty
1414
1515
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
1616
17+
session_embed_source_insufficient_dwarf_version = `-Zembed-source=y` requires at least `-Z dwarf-version=5` while session uses version {$dwarf_version}
18+
1719
session_expr_parentheses_needed = parentheses are required to parse this as an expression
1820
1921
session_failed_to_create_profiler = failed to create profiler: {$err}

compiler/rustc_session/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ pub(crate) struct UnsupportedDwarfVersion {
163163
pub(crate) dwarf_version: u32,
164164
}
165165

166+
#[derive(Diagnostic)]
167+
#[diag(session_embed_source_insufficient_dwarf_version)]
168+
pub(crate) struct EmbedSourceInsufficientDwarfVersion {
169+
pub(crate) dwarf_version: u32,
170+
}
171+
166172
#[derive(Diagnostic)]
167173
#[diag(session_target_stack_protector_not_supported)]
168174
pub(crate) struct StackProtectorNotSupportedForTarget<'a> {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,8 @@ options! {
16801680
them only if an error has not been emitted"),
16811681
ehcont_guard: bool = (false, parse_bool, [TRACKED],
16821682
"generate Windows EHCont Guard tables"),
1683+
embed_source: bool = (false, parse_bool, [TRACKED],
1684+
"embed source text in DWARF debug sections (default: no)"),
16831685
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
16841686
"emit a section containing stack size metadata (default: no)"),
16851687
emit_thin_lto: bool = (true, parse_bool, [TRACKED],

compiler/rustc_session/src/session.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,12 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
13021302
.emit_err(errors::SplitDebugInfoUnstablePlatform { debuginfo: sess.split_debuginfo() });
13031303
}
13041304

1305+
let dwarf_version =
1306+
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
1307+
if sess.opts.unstable_opts.embed_source && dwarf_version < 5 {
1308+
sess.dcx().emit_err(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });
1309+
}
1310+
13051311
if sess.opts.unstable_opts.instrument_xray.is_some() && !sess.target.options.supports_xray {
13061312
sess.dcx().emit_err(errors::InstrumentationNotSupported { us: "XRay".to_string() });
13071313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `embed-source`
2+
3+
This flag controls whether the compiler embeds the program source code text into
4+
object debug info section. It takes one of the following values:
5+
6+
* `y`, `yes`, `on` or `true`: put source code in debug info.
7+
* `n`, `no`, `off`, `false` or no value: omit source code from debug info (the default).
8+
9+
`-Z embed-source` requires DWARFv5 and is only supported with the LLVM backend.
10+
Use `-Z dwarf-version=5` to control the compiler's DWARF target version.

0 commit comments

Comments
 (0)