Skip to content

Commit 30503f1

Browse files
committed
add -C embed-source=yes to embed source code in dwarf debug info
1 parent a299aa5 commit 30503f1

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,8 @@ 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 = cx.sess().opts.cg.embed_source.then_some(()).and(source_file.src.as_ref());
643+
642644
unsafe {
643645
llvm::LLVMRustDIBuilderCreateFile(
644646
DIB(cx),
@@ -649,6 +651,8 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
649651
hash_kind,
650652
hash_value.as_ptr().cast(),
651653
hash_value.len(),
654+
source.map(|x| x.as_ptr().cast()).unwrap_or(ptr::null()),
655+
source.map(|x| x.len()).unwrap_or(0),
652656
)
653657
}
654658
}
@@ -669,6 +673,8 @@ pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
669673
llvm::ChecksumKind::None,
670674
hash_value.as_ptr().cast(),
671675
hash_value.len(),
676+
ptr::null(),
677+
0,
672678
)
673679
})
674680
}
@@ -915,6 +921,8 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
915921
llvm::ChecksumKind::None,
916922
ptr::null(),
917923
0,
924+
ptr::null(),
925+
0,
918926
);
919927

920928
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
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ fn test_codegen_options_tracking_hash() {
605605
tracked!(debug_assertions, Some(true));
606606
tracked!(debuginfo, DebugInfo::Limited);
607607
tracked!(embed_bitcode, false);
608+
tracked!(embed_source, true);
608609
tracked!(force_frame_pointers, FramePointer::Always);
609610
tracked!(force_unwind_tables, Some(true));
610611
tracked!(inline_threshold, Some(0xf007ba11));

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,15 +888,19 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFile(
888888
LLVMRustDIBuilderRef Builder,
889889
const char *Filename, size_t FilenameLen,
890890
const char *Directory, size_t DirectoryLen, LLVMRustChecksumKind CSKind,
891-
const char *Checksum, size_t ChecksumLen) {
891+
const char *Checksum, size_t ChecksumLen,
892+
const char *Source, size_t SourceLen) {
892893

893894
std::optional<DIFile::ChecksumKind> llvmCSKind = fromRust(CSKind);
894895
std::optional<DIFile::ChecksumInfo<StringRef>> CSInfo{};
895896
if (llvmCSKind)
896897
CSInfo.emplace(*llvmCSKind, StringRef{Checksum, ChecksumLen});
898+
std::optional<StringRef> oSource{};
899+
if (Source)
900+
oSource = StringRef(Source, SourceLen);
897901
return wrap(Builder->createFile(StringRef(Filename, FilenameLen),
898902
StringRef(Directory, DirectoryLen),
899-
CSInfo));
903+
CSInfo, oSource));
900904
}
901905

902906
extern "C" LLVMMetadataRef

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,8 @@ options! {
14891489
"import library generation tool (ignored except when targeting windows-gnu)"),
14901490
embed_bitcode: bool = (true, parse_bool, [TRACKED],
14911491
"emit bitcode in rlibs (default: yes)"),
1492+
embed_source: bool = (false, parse_bool, [TRACKED],
1493+
"embed source text in DWARF debug sections (default: no)"),
14921494
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
14931495
"extra data to put in each output filename"),
14941496
force_frame_pointers: FramePointer = (FramePointer::MayOmit, parse_frame_pointer, [TRACKED],

src/doc/rustc/src/codegen-options/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ at start-up, because the combination is invalid.
146146
> reason for that is that it's how it was for rustc 1.44 and prior. In 1.45 this
147147
> option was added to turn off what had always been the default.
148148
149+
## embed-source
150+
151+
TODO
152+
149153
## extra-filename
150154

151155
This option allows you to put extra data in each output filename. It takes a

0 commit comments

Comments
 (0)