Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0b87af9

Browse files
committed
Add -Z embed-source=yes to embed source code in DWARF debug info
1 parent 80d8270 commit 0b87af9

File tree

9 files changed

+76
-4
lines changed

9 files changed

+76
-4
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
629629
};
630630
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
631631

632+
let source =
633+
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
634+
632635
unsafe {
633636
llvm::LLVMRustDIBuilderCreateFile(
634637
DIB(cx),
@@ -639,6 +642,8 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
639642
hash_kind,
640643
hash_value.as_ptr().cast(),
641644
hash_value.len(),
645+
source.map_or(ptr::null(), |x| x.as_ptr().cast()),
646+
source.map_or(0, |x| x.len()),
642647
)
643648
}
644649
}
@@ -659,6 +664,8 @@ pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
659664
llvm::ChecksumKind::None,
660665
hash_value.as_ptr().cast(),
661666
hash_value.len(),
667+
ptr::null(),
668+
0,
662669
)
663670
})
664671
}
@@ -943,6 +950,8 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
943950
llvm::ChecksumKind::None,
944951
ptr::null(),
945952
0,
953+
ptr::null(),
954+
0,
946955
);
947956

948957
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,8 @@ extern "C" {
18531853
CSKind: ChecksumKind,
18541854
Checksum: *const c_char,
18551855
ChecksumLen: size_t,
1856+
Source: *const c_char,
1857+
SourceLen: size_t,
18561858
) -> &'a DIFile;
18571859

18581860
pub fn LLVMRustDIBuilderCreateSubroutineType<'a>(

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ fn test_unstable_options_tracking_hash() {
773773
tracked!(direct_access_external_data, Some(true));
774774
tracked!(dual_proc_macros, true);
775775
tracked!(dwarf_version, Some(5));
776+
tracked!(embed_source, true);
776777
tracked!(emit_thin_lto, false);
777778
tracked!(export_executable_symbols, true);
778779
tracked!(fewer_names, Some(true));

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 7 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ 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` but DWARF version is {$dwarf_version}
18+
19+
session_embed_source_requires_debug_info = `-Zembed-source=y` requires debug information to be enabled
20+
21+
session_embed_source_requires_llvm_backend = `-Zembed-source=y` is only supported on the LLVM codegen backend
22+
1723
session_expr_parentheses_needed = parentheses are required to parse this as an expression
1824
1925
session_failed_to_create_profiler = failed to create profiler: {$err}

compiler/rustc_session/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ pub(crate) struct UnsupportedDwarfVersion {
165165
pub(crate) dwarf_version: u32,
166166
}
167167

168+
#[derive(Diagnostic)]
169+
#[diag(session_embed_source_insufficient_dwarf_version)]
170+
pub(crate) struct EmbedSourceInsufficientDwarfVersion {
171+
pub(crate) dwarf_version: u32,
172+
}
173+
174+
#[derive(Diagnostic)]
175+
#[diag(session_embed_source_requires_debug_info)]
176+
pub(crate) struct EmbedSourceRequiresDebugInfo;
177+
178+
#[derive(Diagnostic)]
179+
#[diag(session_embed_source_requires_llvm_backend)]
180+
pub(crate) struct EmbedSourceRequiresLLVMBackend;
181+
168182
#[derive(Diagnostic)]
169183
#[diag(session_target_stack_protector_not_supported)]
170184
pub(crate) struct StackProtectorNotSupportedForTarget<'a> {

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,8 @@ options! {
17081708
them only if an error has not been emitted"),
17091709
ehcont_guard: bool = (false, parse_bool, [TRACKED],
17101710
"generate Windows EHCont Guard tables"),
1711+
embed_source: bool = (false, parse_bool, [TRACKED],
1712+
"embed source text in DWARF debug sections (default: no)"),
17111713
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
17121714
"emit a section containing stack size metadata (default: no)"),
17131715
emit_thin_lto: bool = (true, parse_bool, [TRACKED],

compiler/rustc_session/src/session.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use rustc_target::spec::{
3737
use crate::code_stats::CodeStats;
3838
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
3939
use crate::config::{
40-
self, CoverageLevel, CrateType, ErrorOutputType, FunctionReturn, Input, InstrumentCoverage,
41-
OptLevel, OutFileName, OutputType, RemapPathScopeComponents, SwitchWithOptPath,
40+
self, CoverageLevel, CrateType, DebugInfo, ErrorOutputType, FunctionReturn, Input,
41+
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
42+
SwitchWithOptPath,
4243
};
4344
use crate::parse::{add_feature_diagnostics, ParseSess};
4445
use crate::search_paths::{PathKind, SearchPath};
@@ -1300,6 +1301,26 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
13001301
.emit_err(errors::SplitDebugInfoUnstablePlatform { debuginfo: sess.split_debuginfo() });
13011302
}
13021303

1304+
if sess.opts.unstable_opts.embed_source {
1305+
let dwarf_version =
1306+
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
1307+
1308+
let uses_llvm_backend =
1309+
matches!(sess.opts.unstable_opts.codegen_backend.as_deref(), None | Some("llvm"));
1310+
1311+
if dwarf_version < 5 {
1312+
sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });
1313+
}
1314+
1315+
if sess.opts.debuginfo == DebugInfo::None {
1316+
sess.dcx().emit_warn(errors::EmbedSourceRequiresDebugInfo);
1317+
}
1318+
1319+
if !uses_llvm_backend {
1320+
sess.dcx().emit_warn(errors::EmbedSourceRequiresLLVMBackend);
1321+
}
1322+
}
1323+
13031324
if sess.opts.unstable_opts.instrument_xray.is_some() && !sess.target.options.supports_xray {
13041325
sess.dcx().emit_err(errors::InstrumentationNotSupported { us: "XRay".to_string() });
13051326
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `embed-source`
2+
3+
This flag controls whether the compiler embeds the program source code text into
4+
the object debug information 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+
This flag is ignored in configurations that don't emit DWARF debug information
10+
and is ignored on non-LLVM backends. `-Z embed-source` requires DWARFv5. Use
11+
`-Z dwarf-version=5` to control the compiler's DWARF target version and `-g` to
12+
enable debug info generation.

0 commit comments

Comments
 (0)