mirror of https://github.com/rust-lang/rust.git
rustc_codegen_llvm: add support for inlined function debuginfo.
This commit is contained in:
parent
737499593d
commit
0ce4452fce
|
@ -3,21 +3,26 @@ use super::utils::DIB;
|
|||
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
|
||||
use rustc_codegen_ssa::traits::*;
|
||||
|
||||
use crate::abi::FnAbi;
|
||||
use crate::common::CodegenCx;
|
||||
use crate::llvm;
|
||||
use crate::llvm::debuginfo::DIScope;
|
||||
use crate::llvm::debuginfo::{DILocation, DIScope};
|
||||
use rustc_middle::mir::{Body, SourceScope};
|
||||
use rustc_middle::ty::layout::FnAbiExt;
|
||||
use rustc_middle::ty::{self, Instance};
|
||||
use rustc_session::config::DebugInfo;
|
||||
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_index::vec::Idx;
|
||||
|
||||
/// Produces DIScope DIEs for each MIR Scope which has variables defined in it.
|
||||
// FIXME(eddyb) almost all of this should be in `rustc_codegen_ssa::mir::debuginfo`.
|
||||
pub fn compute_mir_scopes(
|
||||
cx: &CodegenCx<'ll, '_>,
|
||||
mir: &Body<'_>,
|
||||
cx: &CodegenCx<'ll, 'tcx>,
|
||||
instance: Instance<'tcx>,
|
||||
mir: &Body<'tcx>,
|
||||
fn_dbg_scope: &'ll DIScope,
|
||||
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
|
||||
debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>,
|
||||
) {
|
||||
// Find all the scopes with variables defined in them.
|
||||
let mut has_variables = BitSet::new_empty(mir.source_scopes.len());
|
||||
|
@ -37,16 +42,17 @@ pub fn compute_mir_scopes(
|
|||
// Instantiate all scopes.
|
||||
for idx in 0..mir.source_scopes.len() {
|
||||
let scope = SourceScope::new(idx);
|
||||
make_mir_scope(cx, &mir, fn_dbg_scope, &has_variables, debug_context, scope);
|
||||
make_mir_scope(cx, instance, &mir, fn_dbg_scope, &has_variables, debug_context, scope);
|
||||
}
|
||||
}
|
||||
|
||||
fn make_mir_scope(
|
||||
cx: &CodegenCx<'ll, '_>,
|
||||
mir: &Body<'_>,
|
||||
cx: &CodegenCx<'ll, 'tcx>,
|
||||
instance: Instance<'tcx>,
|
||||
mir: &Body<'tcx>,
|
||||
fn_dbg_scope: &'ll DIScope,
|
||||
has_variables: &BitSet<SourceScope>,
|
||||
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
|
||||
debug_context: &mut FunctionDebugContext<&'ll DIScope, &'ll DILocation>,
|
||||
scope: SourceScope,
|
||||
) {
|
||||
if debug_context.scopes[scope].dbg_scope.is_some() {
|
||||
|
@ -55,22 +61,23 @@ fn make_mir_scope(
|
|||
|
||||
let scope_data = &mir.source_scopes[scope];
|
||||
let parent_scope = if let Some(parent) = scope_data.parent_scope {
|
||||
make_mir_scope(cx, mir, fn_dbg_scope, has_variables, debug_context, parent);
|
||||
make_mir_scope(cx, instance, mir, fn_dbg_scope, has_variables, debug_context, parent);
|
||||
debug_context.scopes[parent]
|
||||
} else {
|
||||
// The root is the function itself.
|
||||
let loc = cx.lookup_debug_loc(mir.span.lo());
|
||||
debug_context.scopes[scope] = DebugScope {
|
||||
dbg_scope: Some(fn_dbg_scope),
|
||||
inlined_at: None,
|
||||
file_start_pos: loc.file.start_pos,
|
||||
file_end_pos: loc.file.end_pos,
|
||||
};
|
||||
return;
|
||||
};
|
||||
|
||||
if !has_variables.contains(scope) {
|
||||
// Do not create a DIScope if there are no variables
|
||||
// defined in this MIR Scope, to avoid debuginfo bloat.
|
||||
if !has_variables.contains(scope) && scope_data.inlined.is_none() {
|
||||
// Do not create a DIScope if there are no variables defined in this
|
||||
// MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat.
|
||||
debug_context.scopes[scope] = parent_scope;
|
||||
return;
|
||||
}
|
||||
|
@ -78,17 +85,39 @@ fn make_mir_scope(
|
|||
let loc = cx.lookup_debug_loc(scope_data.span.lo());
|
||||
let file_metadata = file_metadata(cx, &loc.file);
|
||||
|
||||
let dbg_scope = unsafe {
|
||||
Some(llvm::LLVMRustDIBuilderCreateLexicalBlock(
|
||||
DIB(cx),
|
||||
parent_scope.dbg_scope.unwrap(),
|
||||
file_metadata,
|
||||
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
loc.col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
|
||||
))
|
||||
let dbg_scope = match scope_data.inlined {
|
||||
Some((callee, _)) => {
|
||||
// FIXME(eddyb) this would be `self.monomorphize(&callee)`
|
||||
// if this is moved to `rustc_codegen_ssa::mir::debuginfo`.
|
||||
let callee = cx.tcx.subst_and_normalize_erasing_regions(
|
||||
instance.substs,
|
||||
ty::ParamEnv::reveal_all(),
|
||||
&callee,
|
||||
);
|
||||
let callee_fn_abi = FnAbi::of_instance(cx, callee, &[]);
|
||||
cx.dbg_scope_fn(callee, &callee_fn_abi, None)
|
||||
}
|
||||
None => unsafe {
|
||||
llvm::LLVMRustDIBuilderCreateLexicalBlock(
|
||||
DIB(cx),
|
||||
parent_scope.dbg_scope.unwrap(),
|
||||
file_metadata,
|
||||
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
loc.col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
|
||||
// FIXME(eddyb) this doesn't account for the macro-related
|
||||
// `Span` fixups that `rustc_codegen_ssa::mir::debuginfo` does.
|
||||
let callsite_scope = parent_scope.adjust_dbg_scope_for_span(cx, callsite_span);
|
||||
cx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span)
|
||||
});
|
||||
|
||||
debug_context.scopes[scope] = DebugScope {
|
||||
dbg_scope,
|
||||
dbg_scope: Some(dbg_scope),
|
||||
inlined_at: inlined_at.or(parent_scope.inlined_at),
|
||||
file_start_pos: loc.file.start_pos,
|
||||
file_end_pos: loc.file.end_pos,
|
||||
};
|
||||
|
|
|
@ -265,22 +265,27 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|||
instance: Instance<'tcx>,
|
||||
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
|
||||
llfn: &'ll Value,
|
||||
mir: &mir::Body<'_>,
|
||||
) -> Option<FunctionDebugContext<&'ll DIScope>> {
|
||||
mir: &mir::Body<'tcx>,
|
||||
) -> Option<FunctionDebugContext<&'ll DIScope, &'ll DILocation>> {
|
||||
if self.sess().opts.debuginfo == DebugInfo::None {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Initialize fn debug context (including scopes).
|
||||
// FIXME(eddyb) figure out a way to not need `Option` for `dbg_scope`.
|
||||
let empty_scope =
|
||||
DebugScope { dbg_scope: None, file_start_pos: BytePos(0), file_end_pos: BytePos(0) };
|
||||
let empty_scope = DebugScope {
|
||||
dbg_scope: None,
|
||||
inlined_at: None,
|
||||
file_start_pos: BytePos(0),
|
||||
file_end_pos: BytePos(0),
|
||||
};
|
||||
let mut fn_debug_context =
|
||||
FunctionDebugContext { scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes) };
|
||||
|
||||
// Fill in all the scopes, with the information from the MIR body.
|
||||
compute_mir_scopes(
|
||||
self,
|
||||
instance,
|
||||
mir,
|
||||
self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
|
||||
&mut fn_debug_context,
|
||||
|
@ -537,7 +542,12 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn dbg_loc(&self, scope: &'ll DIScope, span: Span) -> &'ll DILocation {
|
||||
fn dbg_loc(
|
||||
&self,
|
||||
scope: &'ll DIScope,
|
||||
inlined_at: Option<&'ll DILocation>,
|
||||
span: Span,
|
||||
) -> &'ll DILocation {
|
||||
let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
|
||||
|
||||
unsafe {
|
||||
|
@ -546,7 +556,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|||
line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
|
||||
scope,
|
||||
None,
|
||||
inlined_at,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ use super::operand::OperandValue;
|
|||
use super::place::PlaceRef;
|
||||
use super::{FunctionCx, LocalRef};
|
||||
|
||||
pub struct FunctionDebugContext<D> {
|
||||
pub scopes: IndexVec<mir::SourceScope, DebugScope<D>>,
|
||||
pub struct FunctionDebugContext<S, L> {
|
||||
pub scopes: IndexVec<mir::SourceScope, DebugScope<S, L>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
@ -36,15 +36,42 @@ pub struct PerLocalVarDebugInfo<'tcx, D> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct DebugScope<D> {
|
||||
pub struct DebugScope<S, L> {
|
||||
// FIXME(eddyb) this should never be `None`, after initialization.
|
||||
pub dbg_scope: Option<D>,
|
||||
pub dbg_scope: Option<S>,
|
||||
|
||||
/// Call site location, if this scope was inlined from another function.
|
||||
pub inlined_at: Option<L>,
|
||||
|
||||
// Start and end offsets of the file to which this DIScope belongs.
|
||||
// These are used to quickly determine whether some span refers to the same file.
|
||||
pub file_start_pos: BytePos,
|
||||
pub file_end_pos: BytePos,
|
||||
}
|
||||
|
||||
impl<'tcx, S: Copy, L: Copy> DebugScope<S, L> {
|
||||
/// DILocations inherit source file name from the parent DIScope. Due to macro expansions
|
||||
/// it may so happen that the current span belongs to a different file than the DIScope
|
||||
/// corresponding to span's containing source scope. If so, we need to create a DIScope
|
||||
/// "extension" into that file.
|
||||
pub fn adjust_dbg_scope_for_span<Cx: CodegenMethods<'tcx, DIScope = S, DILocation = L>>(
|
||||
&self,
|
||||
cx: &Cx,
|
||||
span: Span,
|
||||
) -> S {
|
||||
// FIXME(eddyb) this should never be `None`.
|
||||
let dbg_scope = self.dbg_scope.unwrap();
|
||||
|
||||
let pos = span.lo();
|
||||
if pos < self.file_start_pos || pos >= self.file_end_pos {
|
||||
let sm = cx.sess().source_map();
|
||||
cx.extend_scope_to_file(dbg_scope, &sm.lookup_char_pos(pos).file)
|
||||
} else {
|
||||
dbg_scope
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||
pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
|
||||
bx.set_span(source_info.span);
|
||||
|
@ -54,19 +81,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
}
|
||||
|
||||
fn dbg_loc(&self, source_info: mir::SourceInfo) -> Option<Bx::DILocation> {
|
||||
let (scope, span) = self.dbg_scope_and_span(source_info)?;
|
||||
Some(self.cx.dbg_loc(scope, span))
|
||||
let span = self.adjust_span_for_debugging(source_info.span);
|
||||
let scope = &self.debug_context.as_ref()?.scopes[source_info.scope];
|
||||
let dbg_scope = scope.adjust_dbg_scope_for_span(self.cx, span);
|
||||
Some(self.cx.dbg_loc(dbg_scope, scope.inlined_at, span))
|
||||
}
|
||||
|
||||
fn dbg_scope_and_span(&self, source_info: mir::SourceInfo) -> Option<(Bx::DIScope, Span)> {
|
||||
/// In order to have a good line stepping behavior in debugger, we overwrite debug
|
||||
/// locations of macro expansions with that of the outermost expansion site
|
||||
/// (unless the crate is being compiled with `-Z debug-macros`).
|
||||
fn adjust_span_for_debugging(&self, mut span: Span) -> Span {
|
||||
// Bail out if debug info emission is not enabled.
|
||||
let debug_context = self.debug_context.as_ref()?;
|
||||
let scope = &debug_context.scopes[source_info.scope];
|
||||
if self.debug_context.is_none() {
|
||||
return span;
|
||||
}
|
||||
|
||||
// In order to have a good line stepping behavior in debugger, we overwrite debug
|
||||
// locations of macro expansions with that of the outermost expansion site
|
||||
// (unless the crate is being compiled with `-Z debug-macros`).
|
||||
let mut span = source_info.span;
|
||||
if span.from_expansion() && !self.cx.sess().opts.debugging_opts.debug_macros {
|
||||
// Walk up the macro expansion chain until we reach a non-expanded span.
|
||||
// We also stop at the function body level because no line stepping can occur
|
||||
|
@ -75,20 +104,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
span = rustc_span::hygiene::walk_chain(span, self.mir.span.ctxt());
|
||||
}
|
||||
|
||||
// FIXME(eddyb) this should never be `None`.
|
||||
let mut dbg_scope = scope.dbg_scope?;
|
||||
|
||||
// DILocations inherit source file name from the parent DIScope. Due to macro expansions
|
||||
// it may so happen that the current span belongs to a different file than the DIScope
|
||||
// corresponding to span's containing source scope. If so, we need to create a DIScope
|
||||
// "extension" into that file.
|
||||
let pos = span.lo();
|
||||
if pos < scope.file_start_pos || pos >= scope.file_end_pos {
|
||||
let sm = self.cx.sess().source_map();
|
||||
dbg_scope = self.cx.extend_scope_to_file(dbg_scope, &sm.lookup_char_pos(pos).file);
|
||||
}
|
||||
|
||||
Some((dbg_scope, span))
|
||||
span
|
||||
}
|
||||
|
||||
/// Apply debuginfo and/or name, after creating the `alloca` for a local,
|
||||
|
@ -130,11 +146,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
let name = kw::Invalid;
|
||||
let decl = &self.mir.local_decls[local];
|
||||
let dbg_var = if full_debug_info {
|
||||
self.dbg_scope_and_span(decl.source_info).map(|(scope, span)| {
|
||||
self.debug_context.as_ref().map(|debug_context| {
|
||||
// FIXME(eddyb) is this `+ 1` needed at all?
|
||||
let kind = VariableKind::ArgumentVariable(arg_index + 1);
|
||||
|
||||
self.cx.create_dbg_var(name, self.monomorphize(&decl.ty), scope, kind, span)
|
||||
let arg_ty = self.monomorphize(&decl.ty);
|
||||
|
||||
let span = self.adjust_span_for_debugging(decl.source_info.span);
|
||||
let scope = &debug_context.scopes[decl.source_info.scope];
|
||||
let dbg_scope = scope.adjust_dbg_scope_for_span(self.cx, span);
|
||||
|
||||
self.cx.create_dbg_var(name, arg_ty, dbg_scope, kind, span)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
@ -288,9 +310,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
|
||||
let mut per_local = IndexVec::from_elem(vec![], &self.mir.local_decls);
|
||||
for var in &self.mir.var_debug_info {
|
||||
let scope_and_span =
|
||||
if full_debug_info { self.dbg_scope_and_span(var.source_info) } else { None };
|
||||
let dbg_var = scope_and_span.map(|(scope, span)| {
|
||||
let dbg_scope_and_span = if full_debug_info {
|
||||
self.debug_context.as_ref().map(|debug_context| {
|
||||
let span = self.adjust_span_for_debugging(var.source_info.span);
|
||||
let scope = &debug_context.scopes[var.source_info.scope];
|
||||
(scope.adjust_dbg_scope_for_span(self.cx, span), span)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let dbg_var = dbg_scope_and_span.map(|(dbg_scope, span)| {
|
||||
let place = var.place;
|
||||
let var_ty = self.monomorphized_place_ty(place.as_ref());
|
||||
let var_kind = if self.mir.local_kind(place.local) == mir::LocalKind::Arg
|
||||
|
@ -306,7 +335,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
} else {
|
||||
VariableKind::LocalVariable
|
||||
};
|
||||
self.cx.create_dbg_var(var.name, var_ty, scope, var_kind, span)
|
||||
self.cx.create_dbg_var(var.name, var_ty, dbg_scope, var_kind, span)
|
||||
});
|
||||
|
||||
per_local[var.place.local].push(PerLocalVarDebugInfo {
|
||||
|
|
|
@ -26,7 +26,7 @@ pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
|
|||
|
||||
mir: &'tcx mir::Body<'tcx>,
|
||||
|
||||
debug_context: Option<FunctionDebugContext<Bx::DIScope>>,
|
||||
debug_context: Option<FunctionDebugContext<Bx::DIScope, Bx::DILocation>>,
|
||||
|
||||
llfn: Bx::Function,
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ pub trait DebugInfoMethods<'tcx>: BackendTypes {
|
|||
instance: Instance<'tcx>,
|
||||
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
|
||||
llfn: Self::Function,
|
||||
mir: &mir::Body<'_>,
|
||||
) -> Option<FunctionDebugContext<Self::DIScope>>;
|
||||
mir: &mir::Body<'tcx>,
|
||||
) -> Option<FunctionDebugContext<Self::DIScope, Self::DILocation>>;
|
||||
|
||||
// FIXME(eddyb) find a common convention for all of the debuginfo-related
|
||||
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
|
||||
|
@ -30,7 +30,12 @@ pub trait DebugInfoMethods<'tcx>: BackendTypes {
|
|||
maybe_definition_llfn: Option<Self::Function>,
|
||||
) -> Self::DIScope;
|
||||
|
||||
fn dbg_loc(&self, scope: Self::DIScope, span: Span) -> Self::DILocation;
|
||||
fn dbg_loc(
|
||||
&self,
|
||||
scope: Self::DIScope,
|
||||
inlined_at: Option<Self::DILocation>,
|
||||
span: Span,
|
||||
) -> Self::DILocation;
|
||||
|
||||
fn extend_scope_to_file(
|
||||
&self,
|
||||
|
|
Loading…
Reference in New Issue