Add -Z embed-source=yes
to embed source code in DWARF debug info
This commit is contained in:
parent
80d8270d84
commit
0b87af9d4f
9 changed files with 76 additions and 4 deletions
|
@ -629,6 +629,9 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
|
||||||
};
|
};
|
||||||
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
|
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
|
||||||
|
|
||||||
|
let source =
|
||||||
|
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
llvm::LLVMRustDIBuilderCreateFile(
|
llvm::LLVMRustDIBuilderCreateFile(
|
||||||
DIB(cx),
|
DIB(cx),
|
||||||
|
@ -639,6 +642,8 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
|
||||||
hash_kind,
|
hash_kind,
|
||||||
hash_value.as_ptr().cast(),
|
hash_value.as_ptr().cast(),
|
||||||
hash_value.len(),
|
hash_value.len(),
|
||||||
|
source.map_or(ptr::null(), |x| x.as_ptr().cast()),
|
||||||
|
source.map_or(0, |x| x.len()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -659,6 +664,8 @@ pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
|
||||||
llvm::ChecksumKind::None,
|
llvm::ChecksumKind::None,
|
||||||
hash_value.as_ptr().cast(),
|
hash_value.as_ptr().cast(),
|
||||||
hash_value.len(),
|
hash_value.len(),
|
||||||
|
ptr::null(),
|
||||||
|
0,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -943,6 +950,8 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
|
||||||
llvm::ChecksumKind::None,
|
llvm::ChecksumKind::None,
|
||||||
ptr::null(),
|
ptr::null(),
|
||||||
0,
|
0,
|
||||||
|
ptr::null(),
|
||||||
|
0,
|
||||||
);
|
);
|
||||||
|
|
||||||
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
|
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
|
||||||
|
|
|
@ -1853,6 +1853,8 @@ extern "C" {
|
||||||
CSKind: ChecksumKind,
|
CSKind: ChecksumKind,
|
||||||
Checksum: *const c_char,
|
Checksum: *const c_char,
|
||||||
ChecksumLen: size_t,
|
ChecksumLen: size_t,
|
||||||
|
Source: *const c_char,
|
||||||
|
SourceLen: size_t,
|
||||||
) -> &'a DIFile;
|
) -> &'a DIFile;
|
||||||
|
|
||||||
pub fn LLVMRustDIBuilderCreateSubroutineType<'a>(
|
pub fn LLVMRustDIBuilderCreateSubroutineType<'a>(
|
||||||
|
|
|
@ -773,6 +773,7 @@ fn test_unstable_options_tracking_hash() {
|
||||||
tracked!(direct_access_external_data, Some(true));
|
tracked!(direct_access_external_data, Some(true));
|
||||||
tracked!(dual_proc_macros, true);
|
tracked!(dual_proc_macros, true);
|
||||||
tracked!(dwarf_version, Some(5));
|
tracked!(dwarf_version, Some(5));
|
||||||
|
tracked!(embed_source, true);
|
||||||
tracked!(emit_thin_lto, false);
|
tracked!(emit_thin_lto, false);
|
||||||
tracked!(export_executable_symbols, true);
|
tracked!(export_executable_symbols, true);
|
||||||
tracked!(fewer_names, Some(true));
|
tracked!(fewer_names, Some(true));
|
||||||
|
|
|
@ -901,14 +901,19 @@ extern "C" LLVMMetadataRef
|
||||||
LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
|
LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
|
||||||
size_t FilenameLen, const char *Directory,
|
size_t FilenameLen, const char *Directory,
|
||||||
size_t DirectoryLen, LLVMRustChecksumKind CSKind,
|
size_t DirectoryLen, LLVMRustChecksumKind CSKind,
|
||||||
const char *Checksum, size_t ChecksumLen) {
|
const char *Checksum, size_t ChecksumLen,
|
||||||
|
const char *Source, size_t SourceLen) {
|
||||||
|
|
||||||
std::optional<DIFile::ChecksumKind> llvmCSKind = fromRust(CSKind);
|
std::optional<DIFile::ChecksumKind> llvmCSKind = fromRust(CSKind);
|
||||||
std::optional<DIFile::ChecksumInfo<StringRef>> CSInfo{};
|
std::optional<DIFile::ChecksumInfo<StringRef>> CSInfo{};
|
||||||
if (llvmCSKind)
|
if (llvmCSKind)
|
||||||
CSInfo.emplace(*llvmCSKind, StringRef{Checksum, ChecksumLen});
|
CSInfo.emplace(*llvmCSKind, StringRef{Checksum, ChecksumLen});
|
||||||
|
std::optional<StringRef> oSource{};
|
||||||
|
if (Source)
|
||||||
|
oSource = StringRef(Source, SourceLen);
|
||||||
return wrap(Builder->createFile(StringRef(Filename, FilenameLen),
|
return wrap(Builder->createFile(StringRef(Filename, FilenameLen),
|
||||||
StringRef(Directory, DirectoryLen), CSInfo));
|
StringRef(Directory, DirectoryLen), CSInfo,
|
||||||
|
oSource));
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" LLVMMetadataRef
|
extern "C" LLVMMetadataRef
|
||||||
|
|
|
@ -14,6 +14,12 @@ session_crate_name_empty = crate name must not be empty
|
||||||
|
|
||||||
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
|
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
|
||||||
|
|
||||||
|
session_embed_source_insufficient_dwarf_version = `-Zembed-source=y` requires at least `-Z dwarf-version=5` but DWARF version is {$dwarf_version}
|
||||||
|
|
||||||
|
session_embed_source_requires_debug_info = `-Zembed-source=y` requires debug information to be enabled
|
||||||
|
|
||||||
|
session_embed_source_requires_llvm_backend = `-Zembed-source=y` is only supported on the LLVM codegen backend
|
||||||
|
|
||||||
session_expr_parentheses_needed = parentheses are required to parse this as an expression
|
session_expr_parentheses_needed = parentheses are required to parse this as an expression
|
||||||
|
|
||||||
session_failed_to_create_profiler = failed to create profiler: {$err}
|
session_failed_to_create_profiler = failed to create profiler: {$err}
|
||||||
|
|
|
@ -165,6 +165,20 @@ pub(crate) struct UnsupportedDwarfVersion {
|
||||||
pub(crate) dwarf_version: u32,
|
pub(crate) dwarf_version: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(session_embed_source_insufficient_dwarf_version)]
|
||||||
|
pub(crate) struct EmbedSourceInsufficientDwarfVersion {
|
||||||
|
pub(crate) dwarf_version: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(session_embed_source_requires_debug_info)]
|
||||||
|
pub(crate) struct EmbedSourceRequiresDebugInfo;
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(session_embed_source_requires_llvm_backend)]
|
||||||
|
pub(crate) struct EmbedSourceRequiresLLVMBackend;
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(session_target_stack_protector_not_supported)]
|
#[diag(session_target_stack_protector_not_supported)]
|
||||||
pub(crate) struct StackProtectorNotSupportedForTarget<'a> {
|
pub(crate) struct StackProtectorNotSupportedForTarget<'a> {
|
||||||
|
|
|
@ -1708,6 +1708,8 @@ options! {
|
||||||
them only if an error has not been emitted"),
|
them only if an error has not been emitted"),
|
||||||
ehcont_guard: bool = (false, parse_bool, [TRACKED],
|
ehcont_guard: bool = (false, parse_bool, [TRACKED],
|
||||||
"generate Windows EHCont Guard tables"),
|
"generate Windows EHCont Guard tables"),
|
||||||
|
embed_source: bool = (false, parse_bool, [TRACKED],
|
||||||
|
"embed source text in DWARF debug sections (default: no)"),
|
||||||
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
|
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
|
||||||
"emit a section containing stack size metadata (default: no)"),
|
"emit a section containing stack size metadata (default: no)"),
|
||||||
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
|
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
|
||||||
|
|
|
@ -37,8 +37,9 @@ use rustc_target::spec::{
|
||||||
use crate::code_stats::CodeStats;
|
use crate::code_stats::CodeStats;
|
||||||
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
|
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
|
||||||
use crate::config::{
|
use crate::config::{
|
||||||
self, CoverageLevel, CrateType, ErrorOutputType, FunctionReturn, Input, InstrumentCoverage,
|
self, CoverageLevel, CrateType, DebugInfo, ErrorOutputType, FunctionReturn, Input,
|
||||||
OptLevel, OutFileName, OutputType, RemapPathScopeComponents, SwitchWithOptPath,
|
InstrumentCoverage, OptLevel, OutFileName, OutputType, RemapPathScopeComponents,
|
||||||
|
SwitchWithOptPath,
|
||||||
};
|
};
|
||||||
use crate::parse::{add_feature_diagnostics, ParseSess};
|
use crate::parse::{add_feature_diagnostics, ParseSess};
|
||||||
use crate::search_paths::{PathKind, SearchPath};
|
use crate::search_paths::{PathKind, SearchPath};
|
||||||
|
@ -1300,6 +1301,26 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
|
||||||
.emit_err(errors::SplitDebugInfoUnstablePlatform { debuginfo: sess.split_debuginfo() });
|
.emit_err(errors::SplitDebugInfoUnstablePlatform { debuginfo: sess.split_debuginfo() });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sess.opts.unstable_opts.embed_source {
|
||||||
|
let dwarf_version =
|
||||||
|
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
|
||||||
|
|
||||||
|
let uses_llvm_backend =
|
||||||
|
matches!(sess.opts.unstable_opts.codegen_backend.as_deref(), None | Some("llvm"));
|
||||||
|
|
||||||
|
if dwarf_version < 5 {
|
||||||
|
sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });
|
||||||
|
}
|
||||||
|
|
||||||
|
if sess.opts.debuginfo == DebugInfo::None {
|
||||||
|
sess.dcx().emit_warn(errors::EmbedSourceRequiresDebugInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !uses_llvm_backend {
|
||||||
|
sess.dcx().emit_warn(errors::EmbedSourceRequiresLLVMBackend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if sess.opts.unstable_opts.instrument_xray.is_some() && !sess.target.options.supports_xray {
|
if sess.opts.unstable_opts.instrument_xray.is_some() && !sess.target.options.supports_xray {
|
||||||
sess.dcx().emit_err(errors::InstrumentationNotSupported { us: "XRay".to_string() });
|
sess.dcx().emit_err(errors::InstrumentationNotSupported { us: "XRay".to_string() });
|
||||||
}
|
}
|
||||||
|
|
12
src/doc/unstable-book/src/compiler-flags/embed-source.md
Normal file
12
src/doc/unstable-book/src/compiler-flags/embed-source.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# `embed-source`
|
||||||
|
|
||||||
|
This flag controls whether the compiler embeds the program source code text into
|
||||||
|
the object debug information section. It takes one of the following values:
|
||||||
|
|
||||||
|
* `y`, `yes`, `on` or `true`: put source code in debug info.
|
||||||
|
* `n`, `no`, `off`, `false` or no value: omit source code from debug info (the default).
|
||||||
|
|
||||||
|
This flag is ignored in configurations that don't emit DWARF debug information
|
||||||
|
and is ignored on non-LLVM backends. `-Z embed-source` requires DWARFv5. Use
|
||||||
|
`-Z dwarf-version=5` to control the compiler's DWARF target version and `-g` to
|
||||||
|
enable debug info generation.
|
Loading…
Add table
Reference in a new issue