2015-04-24 13:20:54 +12:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
2015-04-24 17:25:35 +12:00
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
2015-04-24 13:20:54 +12:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
2015-04-24 17:25:35 +12:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
2015-04-24 13:20:54 +12:00
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// See doc.rs for documentation.
|
|
|
|
mod doc;
|
2012-12-03 16:48:01 -08:00
|
|
|
|
2015-04-24 17:20:13 +12:00
|
|
|
use self::VariableAccess::*;
|
|
|
|
use self::VariableKind::*;
|
|
|
|
|
2016-05-01 12:46:55 -04:00
|
|
|
use self::utils::{DIB, span_start, create_DIArray, is_node_local_to_unit};
|
2016-04-06 15:37:19 +03:00
|
|
|
use self::namespace::mangled_name_of_item;
|
2015-04-29 18:14:37 +12:00
|
|
|
use self::type_names::compute_debuginfo_type_name;
|
2016-07-31 22:33:41 +08:00
|
|
|
use self::metadata::{type_metadata, file_metadata, TypeMap};
|
2016-04-07 22:35:11 +03:00
|
|
|
use self::source_loc::InternalDebugLocation::{self, UnknownLocation};
|
2013-06-18 10:17:10 +02:00
|
|
|
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm;
|
|
|
|
use llvm::{ModuleRef, ContextRef, ValueRef};
|
2016-11-18 17:15:14 -05:00
|
|
|
use llvm::debuginfo::{DIFile, DIType, DIScope, DIBuilderRef, DISubprogram, DIArray, DIFlags};
|
2017-04-24 19:01:19 +02:00
|
|
|
use rustc::hir::def_id::{DefId, CrateNum};
|
2016-04-06 10:49:50 +03:00
|
|
|
use rustc::ty::subst::Substs;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-03-22 19:23:36 +02:00
|
|
|
use abi::Abi;
|
2017-03-01 01:30:41 +02:00
|
|
|
use common::{self, CrateContext};
|
2016-12-31 16:00:24 -07:00
|
|
|
use builder::Builder;
|
2017-03-01 01:30:41 +02:00
|
|
|
use monomorphize::Instance;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::{self, Ty};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2017-05-27 20:48:09 +02:00
|
|
|
use rustc::session::config::{self, FullDebugInfo, LimitedDebugInfo, NoDebugInfo};
|
|
|
|
use rustc::util::nodemap::{DefIdMap, FxHashMap, FxHashSet};
|
2012-12-13 13:05:22 -08:00
|
|
|
|
2015-04-24 16:48:10 +12:00
|
|
|
use libc::c_uint;
|
2013-12-18 19:09:14 -08:00
|
|
|
use std::cell::{Cell, RefCell};
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::ffi::CString;
|
2013-06-28 18:32:26 -04:00
|
|
|
use std::ptr;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{self, Span, Pos};
|
|
|
|
use syntax::ast;
|
2017-04-24 19:01:19 +02:00
|
|
|
use syntax::symbol::Symbol;
|
2017-06-21 17:38:22 +03:00
|
|
|
use rustc::ty::layout::{self, LayoutTyper};
|
2013-06-26 16:00:42 +02:00
|
|
|
|
2015-04-24 17:25:35 +12:00
|
|
|
pub mod gdb;
|
|
|
|
mod utils;
|
|
|
|
mod namespace;
|
2015-04-29 18:14:37 +12:00
|
|
|
mod type_names;
|
2016-07-13 00:40:38 +02:00
|
|
|
pub mod metadata;
|
2015-04-29 18:14:37 +12:00
|
|
|
mod create_scope_map;
|
|
|
|
mod source_loc;
|
|
|
|
|
2016-08-24 19:34:31 -07:00
|
|
|
pub use self::create_scope_map::{create_mir_scopes, MirDebugScope};
|
2015-04-29 18:14:37 +12:00
|
|
|
pub use self::source_loc::start_emitting_source_locations;
|
|
|
|
pub use self::metadata::create_global_var_metadata;
|
2016-08-24 19:34:31 -07:00
|
|
|
pub use self::metadata::extend_scope_to_file;
|
2016-12-16 13:25:18 -07:00
|
|
|
pub use self::source_loc::set_source_location;
|
2015-04-24 17:25:35 +12:00
|
|
|
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-12-06 11:39:25 -05:00
|
|
|
const DW_TAG_auto_variable: c_uint = 0x100;
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-12-06 11:39:25 -05:00
|
|
|
const DW_TAG_arg_variable: c_uint = 0x101;
|
2011-11-15 21:11:22 -05:00
|
|
|
|
2013-06-18 13:06:41 +02:00
|
|
|
/// A context object for maintaining all state needed by the debuginfo module.
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct CrateDebugContext<'tcx> {
|
2014-03-28 10:05:27 -07:00
|
|
|
llcontext: ContextRef,
|
2017-02-13 09:57:50 +00:00
|
|
|
llmod: ModuleRef,
|
2014-03-28 10:05:27 -07:00
|
|
|
builder: DIBuilderRef,
|
2017-04-24 19:01:19 +02:00
|
|
|
created_files: RefCell<FxHashMap<(Symbol, Symbol), DIFile>>,
|
2016-11-08 14:02:55 +11:00
|
|
|
created_enum_disr_types: RefCell<FxHashMap<(DefId, layout::Integer), DIType>>,
|
2014-05-30 17:09:16 +02:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
type_map: RefCell<TypeMap<'tcx>>,
|
2016-04-06 15:37:19 +03:00
|
|
|
namespace_map: RefCell<DefIdMap<DIScope>>,
|
2014-05-30 17:09:16 +02:00
|
|
|
|
2014-06-18 15:07:26 +12:00
|
|
|
// This collection is used to assert that composite types (structs, enums,
|
|
|
|
// ...) have their members only set once:
|
2016-11-08 14:02:55 +11:00
|
|
|
composite_types_completed: RefCell<FxHashSet<DIType>>,
|
2013-06-14 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> CrateDebugContext<'tcx> {
|
|
|
|
pub fn new(llmod: ModuleRef) -> CrateDebugContext<'tcx> {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("CrateDebugContext::new");
|
2016-08-02 02:35:09 +03:00
|
|
|
let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) };
|
2013-06-17 08:42:05 -07:00
|
|
|
// DIBuilder inherits context from the module, so we'd better use the same one
|
|
|
|
let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) };
|
2016-12-16 18:48:25 -07:00
|
|
|
CrateDebugContext {
|
2013-06-17 08:42:05 -07:00
|
|
|
llcontext: llcontext,
|
2017-02-13 09:57:50 +00:00
|
|
|
llmod: llmod,
|
2013-06-17 08:42:05 -07:00
|
|
|
builder: builder,
|
2016-11-08 14:02:55 +11:00
|
|
|
created_files: RefCell::new(FxHashMap()),
|
|
|
|
created_enum_disr_types: RefCell::new(FxHashMap()),
|
2014-05-30 17:09:16 +02:00
|
|
|
type_map: RefCell::new(TypeMap::new()),
|
2016-04-06 15:37:19 +03:00
|
|
|
namespace_map: RefCell::new(DefIdMap()),
|
2016-11-08 14:02:55 +11:00
|
|
|
composite_types_completed: RefCell::new(FxHashSet()),
|
2016-12-16 18:48:25 -07:00
|
|
|
}
|
2013-08-05 11:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 11:37:49 +01:00
|
|
|
pub enum FunctionDebugContext {
|
2016-12-15 15:08:18 -07:00
|
|
|
RegularContext(FunctionDebugContextData),
|
2014-04-15 18:05:38 -07:00
|
|
|
DebugInfoDisabled,
|
|
|
|
FunctionWithoutDebugInfo,
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionDebugContext {
|
2017-04-24 19:01:19 +02:00
|
|
|
pub fn get_ref<'a>(&'a self, span: Span) -> &'a FunctionDebugContextData {
|
2014-12-19 11:37:49 +01:00
|
|
|
match *self {
|
2016-12-15 15:08:18 -07:00
|
|
|
FunctionDebugContext::RegularContext(ref data) => data,
|
2014-12-19 11:37:49 +01:00
|
|
|
FunctionDebugContext::DebugInfoDisabled => {
|
2016-12-19 14:38:16 -07:00
|
|
|
span_bug!(span, "{}", FunctionDebugContext::debuginfo_disabled_message());
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
2014-12-19 11:37:49 +01:00
|
|
|
FunctionDebugContext::FunctionWithoutDebugInfo => {
|
2016-12-19 14:38:16 -07:00
|
|
|
span_bug!(span, "{}", FunctionDebugContext::should_be_ignored_message());
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 17:58:58 +02:00
|
|
|
fn debuginfo_disabled_message() -> &'static str {
|
|
|
|
"debuginfo: Error trying to access FunctionDebugContext although debug info is disabled!"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_be_ignored_message() -> &'static str {
|
|
|
|
"debuginfo: Error trying to access FunctionDebugContext for function that should be \
|
|
|
|
ignored by debug info!"
|
|
|
|
}
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 17:39:15 +03:00
|
|
|
pub struct FunctionDebugContextData {
|
2013-08-23 18:45:02 +02:00
|
|
|
fn_metadata: DISubprogram,
|
2013-12-20 20:57:25 -08:00
|
|
|
source_locations_enabled: Cell<bool>,
|
2017-04-24 19:01:19 +02:00
|
|
|
pub defining_crate: CrateNum,
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:49:20 +12:00
|
|
|
pub enum VariableAccess<'a> {
|
2013-09-03 18:23:59 +02:00
|
|
|
// The llptr given is an alloca containing the variable's value
|
|
|
|
DirectVariable { alloca: ValueRef },
|
2014-06-18 15:07:26 +12:00
|
|
|
// The llptr given is an alloca containing the start of some pointer chain
|
|
|
|
// leading to the variable's content.
|
2015-01-30 19:25:07 +01:00
|
|
|
IndirectVariable { alloca: ValueRef, address_operations: &'a [i64] }
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:49:20 +12:00
|
|
|
pub enum VariableKind {
|
2015-03-25 17:06:52 -07:00
|
|
|
ArgumentVariable(usize /*index*/),
|
2013-08-23 18:45:02 +02:00
|
|
|
LocalVariable,
|
|
|
|
CapturedVariable,
|
2013-08-05 11:12:40 +02:00
|
|
|
}
|
|
|
|
|
2013-06-14 11:59:49 -07:00
|
|
|
/// Create any deferred debug metadata nodes
|
2014-03-06 18:47:24 +02:00
|
|
|
pub fn finalize(cx: &CrateContext) {
|
2014-09-05 09:18:53 -07:00
|
|
|
if cx.dbg_cx().is_none() {
|
2013-09-06 16:00:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("finalize");
|
2014-12-03 14:48:18 -08:00
|
|
|
|
2015-04-24 15:25:42 +12:00
|
|
|
if gdb::needs_gdb_debug_scripts_section(cx) {
|
2014-12-03 14:48:18 -08:00
|
|
|
// Add a .debug_gdb_scripts section to this compile-unit. This will
|
|
|
|
// cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
|
|
|
|
// which activates the Rust pretty printers for binary this section is
|
|
|
|
// contained in.
|
2015-04-24 15:25:42 +12:00
|
|
|
gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
|
2014-12-03 14:48:18 -08:00
|
|
|
}
|
|
|
|
|
2013-06-11 12:41:09 -07:00
|
|
|
unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderFinalize(DIB(cx));
|
|
|
|
llvm::LLVMRustDIBuilderDispose(DIB(cx));
|
2014-01-28 00:05:33 -05:00
|
|
|
// Debuginfo generation in LLVM by default uses a higher
|
2017-03-12 14:13:35 -04:00
|
|
|
// version of dwarf than macOS currently understands. We can
|
2014-01-28 00:05:33 -05:00
|
|
|
// instruct LLVM to emit an older version of dwarf, however,
|
2017-03-12 14:13:35 -04:00
|
|
|
// for macOS to understand. For more info see #11352
|
2014-01-28 00:05:33 -05:00
|
|
|
// This can be overridden using --llvm-opts -dwarf-version,N.
|
2015-02-16 17:48:50 +09:00
|
|
|
// Android has the same issue (#22398)
|
|
|
|
if cx.sess().target.target.options.is_like_osx ||
|
|
|
|
cx.sess().target.target.options.is_like_android {
|
2014-11-25 13:28:35 -08:00
|
|
|
llvm::LLVMRustAddModuleFlag(cx.llmod(),
|
|
|
|
"Dwarf Version\0".as_ptr() as *const _,
|
|
|
|
2)
|
2014-01-28 00:05:33 -05:00
|
|
|
}
|
|
|
|
|
2016-01-30 22:30:19 -08:00
|
|
|
// Indicate that we want CodeView debug information on MSVC
|
|
|
|
if cx.sess().target.target.options.is_like_msvc {
|
|
|
|
llvm::LLVMRustAddModuleFlag(cx.llmod(),
|
|
|
|
"CodeView\0".as_ptr() as *const _,
|
|
|
|
1)
|
|
|
|
}
|
|
|
|
|
2014-01-28 00:05:33 -05:00
|
|
|
// Prevent bitcode readers from deleting the debug info.
|
2014-11-25 13:28:35 -08:00
|
|
|
let ptr = "Debug Info Version\0".as_ptr();
|
|
|
|
llvm::LLVMRustAddModuleFlag(cx.llmod(), ptr as *const _,
|
2015-05-11 21:07:38 -07:00
|
|
|
llvm::LLVMRustDebugMetadataVersion());
|
2013-06-14 11:38:29 -07:00
|
|
|
};
|
2011-11-10 00:55:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-29 11:44:11 +02:00
|
|
|
/// Creates the function-specific debug context.
|
|
|
|
///
|
2014-06-18 15:07:26 +12:00
|
|
|
/// Returns the FunctionDebugContext for the function which holds state needed
|
|
|
|
/// for debug info creation. The function may also return another variant of the
|
|
|
|
/// FunctionDebugContext enum which indicates why no debuginfo should be created
|
|
|
|
/// for the function.
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
|
2016-04-06 10:49:50 +03:00
|
|
|
instance: Instance<'tcx>,
|
2017-02-13 10:51:06 +02:00
|
|
|
sig: ty::FnSig<'tcx>,
|
2016-08-17 22:50:55 +03:00
|
|
|
llfn: ValueRef,
|
|
|
|
mir: &mir::Mir) -> FunctionDebugContext {
|
2014-03-05 16:36:01 +02:00
|
|
|
if cx.sess().opts.debuginfo == NoDebugInfo {
|
2014-12-19 11:37:49 +01:00
|
|
|
return FunctionDebugContext::DebugInfoDisabled;
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
for attr in instance.def.attrs(cx.tcx()).iter() {
|
2016-12-19 07:33:35 -07:00
|
|
|
if attr.check_name("no_debug") {
|
|
|
|
return FunctionDebugContext::FunctionWithoutDebugInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:50:55 +03:00
|
|
|
let containing_scope = get_containing_scope(cx, instance);
|
|
|
|
let span = mir.span;
|
2016-05-01 12:46:55 -04:00
|
|
|
|
2013-08-23 18:45:02 +02:00
|
|
|
// This can be the case for functions inlined from another crate
|
2016-06-21 18:08:13 -04:00
|
|
|
if span == syntax_pos::DUMMY_SP {
|
2016-12-18 11:08:57 -07:00
|
|
|
// FIXME(simulacrum): Probably can't happen; remove.
|
2014-12-19 11:37:49 +01:00
|
|
|
return FunctionDebugContext::FunctionWithoutDebugInfo;
|
2013-08-23 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 19:01:19 +02:00
|
|
|
let def_id = instance.def_id();
|
2013-06-18 13:06:41 +02:00
|
|
|
let loc = span_start(cx, span);
|
2017-04-24 19:01:19 +02:00
|
|
|
let file_metadata = file_metadata(cx, &loc.file.name, def_id.krate);
|
2013-06-18 13:06:41 +02:00
|
|
|
|
2013-08-13 12:52:39 +02:00
|
|
|
let function_type_metadata = unsafe {
|
2017-02-13 10:51:06 +02:00
|
|
|
let fn_signature = get_function_signature(cx, sig);
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(cx), file_metadata, fn_signature)
|
2013-08-13 12:52:39 +02:00
|
|
|
};
|
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
// Find the enclosing function, in case this is a closure.
|
2017-04-24 19:01:19 +02:00
|
|
|
let def_key = cx.tcx().def_key(def_id);
|
2016-04-06 15:37:19 +03:00
|
|
|
let mut name = def_key.disambiguated_data.data.to_string();
|
|
|
|
let name_len = name.len();
|
2016-11-03 22:19:33 +02:00
|
|
|
|
2017-04-24 19:01:19 +02:00
|
|
|
let enclosing_fn_def_id = cx.tcx().closure_base_def_id(def_id);
|
2016-04-06 15:37:19 +03:00
|
|
|
|
2014-06-18 15:07:26 +12:00
|
|
|
// Get_template_parameters() will append a `<...>` clause to the function
|
|
|
|
// name if necessary.
|
2017-04-24 19:01:19 +02:00
|
|
|
let generics = cx.tcx().generics_of(enclosing_fn_def_id);
|
2016-11-03 22:19:33 +02:00
|
|
|
let substs = instance.substs.truncate_to(cx.tcx(), generics);
|
2013-09-15 12:26:47 +02:00
|
|
|
let template_parameters = get_template_parameters(cx,
|
2016-04-06 15:37:19 +03:00
|
|
|
&generics,
|
2016-11-03 22:19:33 +02:00
|
|
|
substs,
|
2013-09-15 12:26:47 +02:00
|
|
|
file_metadata,
|
2016-04-06 15:37:19 +03:00
|
|
|
&mut name);
|
|
|
|
|
|
|
|
// Build the linkage_name out of the item path and "template" parameters.
|
2017-02-08 18:31:03 +01:00
|
|
|
let linkage_name = mangled_name_of_item(cx, instance.def_id(), &name[name_len..]);
|
2013-09-06 16:00:08 +02:00
|
|
|
|
2016-04-06 10:49:50 +03:00
|
|
|
let scope_line = span_start(cx, span).line;
|
2013-12-14 17:12:59 +01:00
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
let local_id = cx.tcx().hir.as_local_node_id(instance.def_id());
|
2016-04-06 10:49:50 +03:00
|
|
|
let is_local_to_unit = local_id.map_or(false, |id| is_node_local_to_unit(cx, id));
|
2013-08-19 18:23:43 +02:00
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
let function_name = CString::new(name).unwrap();
|
2015-02-17 22:47:40 -08:00
|
|
|
let linkage_name = CString::new(linkage_name).unwrap();
|
2016-05-01 12:46:55 -04:00
|
|
|
|
2016-11-30 14:59:45 -07:00
|
|
|
let mut flags = DIFlags::FlagPrototyped;
|
|
|
|
match *cx.sess().entry_fn.borrow() {
|
|
|
|
Some((id, _)) => {
|
|
|
|
if local_id == Some(id) {
|
|
|
|
flags = flags | DIFlags::FlagMainSubprogram;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
let fn_metadata = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateFunction(
|
2014-11-25 13:28:35 -08:00
|
|
|
DIB(cx),
|
|
|
|
containing_scope,
|
|
|
|
function_name.as_ptr(),
|
|
|
|
linkage_name.as_ptr(),
|
|
|
|
file_metadata,
|
|
|
|
loc.line as c_uint,
|
|
|
|
function_type_metadata,
|
|
|
|
is_local_to_unit,
|
|
|
|
true,
|
|
|
|
scope_line as c_uint,
|
2016-11-30 14:59:45 -07:00
|
|
|
flags,
|
2015-12-31 16:50:06 +13:00
|
|
|
cx.sess().opts.optimize != config::OptLevel::No,
|
2014-11-25 13:28:35 -08:00
|
|
|
llfn,
|
|
|
|
template_parameters,
|
|
|
|
ptr::null_mut())
|
|
|
|
};
|
2013-08-13 12:52:39 +02:00
|
|
|
|
2013-09-06 16:00:08 +02:00
|
|
|
// Initialize fn debug context (including scope map and namespace map)
|
2016-12-15 15:08:18 -07:00
|
|
|
let fn_debug_context = FunctionDebugContextData {
|
2013-08-16 18:46:29 +02:00
|
|
|
fn_metadata: fn_metadata,
|
2013-12-20 20:57:25 -08:00
|
|
|
source_locations_enabled: Cell::new(false),
|
2017-04-24 19:01:19 +02:00
|
|
|
defining_crate: def_id.krate,
|
2013-08-16 18:46:29 +02:00
|
|
|
};
|
2013-08-13 12:52:39 +02:00
|
|
|
|
2014-12-19 11:37:49 +01:00
|
|
|
return FunctionDebugContext::RegularContext(fn_debug_context);
|
2013-08-13 12:52:39 +02:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn get_function_signature<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
|
2017-02-13 10:51:06 +02:00
|
|
|
sig: ty::FnSig<'tcx>) -> DIArray {
|
2014-03-05 16:36:01 +02:00
|
|
|
if cx.sess().opts.debuginfo == LimitedDebugInfo {
|
2014-11-17 21:39:01 +13:00
|
|
|
return create_DIArray(DIB(cx), &[]);
|
2013-08-13 12:52:39 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:35:38 -07:00
|
|
|
let mut signature = Vec::with_capacity(sig.inputs().len() + 1);
|
2015-07-13 21:48:07 +02:00
|
|
|
|
|
|
|
// Return type -- llvm::DIBuilder wants this at index 0
|
2016-11-28 19:35:38 -07:00
|
|
|
signature.push(match sig.output().sty {
|
2017-01-11 15:58:37 +08:00
|
|
|
ty::TyTuple(ref tys, _) if tys.is_empty() => ptr::null_mut(),
|
2016-11-28 19:35:38 -07:00
|
|
|
_ => type_metadata(cx, sig.output(), syntax_pos::DUMMY_SP)
|
2015-07-13 21:48:07 +02:00
|
|
|
});
|
2013-08-05 11:12:40 +02:00
|
|
|
|
2017-02-13 10:51:06 +02:00
|
|
|
let inputs = if sig.abi == Abi::RustCall {
|
2016-11-28 19:35:38 -07:00
|
|
|
&sig.inputs()[..sig.inputs().len() - 1]
|
2015-07-14 19:03:13 +02:00
|
|
|
} else {
|
2016-11-28 19:35:38 -07:00
|
|
|
sig.inputs()
|
2015-07-14 19:03:13 +02:00
|
|
|
};
|
|
|
|
|
2013-09-06 16:00:08 +02:00
|
|
|
// Arguments types
|
2017-06-21 17:38:22 +03:00
|
|
|
if cx.sess().target.target.options.is_like_msvc {
|
|
|
|
// FIXME(#42800):
|
|
|
|
// There is a bug in MSDIA that leads to a crash when it encounters
|
|
|
|
// a fixed-size array of `u8` or something zero-sized in a
|
|
|
|
// function-type (see #40477).
|
|
|
|
// As a workaround, we replace those fixed-size arrays with a
|
|
|
|
// pointer-type. So a function `fn foo(a: u8, b: [u8; 4])` would
|
|
|
|
// appear as `fn foo(a: u8, b: *const u8)` in debuginfo,
|
|
|
|
// and a function `fn bar(x: [(); 7])` as `fn bar(x: *const ())`.
|
|
|
|
// This transformed type is wrong, but these function types are
|
|
|
|
// already inaccurate due to ABI adjustments (see #42800).
|
|
|
|
signature.extend(inputs.iter().map(|&t| {
|
|
|
|
let t = match t.sty {
|
|
|
|
ty::TyArray(ct, _)
|
|
|
|
if (ct == cx.tcx().types.u8) ||
|
|
|
|
(cx.layout_of(ct).size(cx).bytes() == 0) => {
|
|
|
|
cx.tcx().mk_imm_ptr(ct)
|
|
|
|
}
|
|
|
|
_ => t
|
|
|
|
};
|
|
|
|
type_metadata(cx, t, syntax_pos::DUMMY_SP)
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
signature.extend(inputs.iter().map(|t| {
|
|
|
|
type_metadata(cx, t, syntax_pos::DUMMY_SP)
|
|
|
|
}));
|
2013-08-05 11:12:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 10:51:06 +02:00
|
|
|
if sig.abi == Abi::RustCall && !sig.inputs().is_empty() {
|
2017-01-11 15:58:37 +08:00
|
|
|
if let ty::TyTuple(args, _) = sig.inputs()[sig.inputs().len() - 1].sty {
|
2016-03-06 16:30:21 +02:00
|
|
|
for &argument_type in args {
|
2016-06-21 18:08:13 -04:00
|
|
|
signature.push(type_metadata(cx, argument_type, syntax_pos::DUMMY_SP));
|
2016-03-06 16:30:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 14:48:57 -05:00
|
|
|
return create_DIArray(DIB(cx), &signature[..]);
|
2013-08-13 12:52:39 +02:00
|
|
|
}
|
2013-08-08 18:33:06 +02:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn get_template_parameters<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
|
2017-01-25 22:01:11 +02:00
|
|
|
generics: &ty::Generics,
|
2016-08-18 08:32:50 +03:00
|
|
|
substs: &Substs<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
file_metadata: DIFile,
|
|
|
|
name_to_append_suffix_to: &mut String)
|
2014-12-17 14:16:28 -05:00
|
|
|
-> DIArray
|
|
|
|
{
|
2016-08-18 08:32:50 +03:00
|
|
|
if substs.types().next().is_none() {
|
2014-11-17 21:39:01 +13:00
|
|
|
return create_DIArray(DIB(cx), &[]);
|
2013-08-15 18:21:16 +02:00
|
|
|
}
|
2013-08-08 18:33:06 +02:00
|
|
|
|
2014-10-14 23:05:01 -07:00
|
|
|
name_to_append_suffix_to.push('<');
|
2016-08-27 01:13:48 +03:00
|
|
|
for (i, actual_type) in substs.types().enumerate() {
|
2016-08-18 08:32:50 +03:00
|
|
|
if i != 0 {
|
|
|
|
name_to_append_suffix_to.push_str(",");
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:33:20 +02:00
|
|
|
let actual_type = cx.tcx().normalize_associated_type(&actual_type);
|
2013-08-15 18:21:16 +02:00
|
|
|
// Add actual type name to <...> clause of function name
|
debuginfo: Make names of types in debuginfo reliable and omit source locations from debug info type descriptions.
So far, type names generated for debuginfo where a bit sketchy. It was not clearly defined when a name should be fully qualified and when not, if region parameters should be shown or not, and other things like that.
This commit makes the debuginfo module responsible for creating type names instead of using ppaux::ty_to_str() and brings type names, as they show up in the DWARF information, in line with GCC and Clang:
* The name of the type being described is unqualified. It's path is defined by its position in the namespace hierarchy.
* Type arguments are always fully qualified, no matter if they would actually be in scope at the type definition location.
Care is also taken to reliably make type names consistent across crate boundaries. That is, the code now tries make the type name the same, regardless if the type is in the local crate or reconstructed from metadata. Otherwise LLVM will complain about violating the one-definition-rule when using link-time-optimization.
This commit also removes all source location information from type descriptions because these cannot be reconstructed for types instantiated from metadata. Again, with LTO enabled, this can lead to two versions of the debuginfo type description, one with and one without source location information, which then triggers the LLVM ODR assertion.
Fortunately, source location information about types is rarely used, so this has little impact. Once source location information is preserved in metadata (#1972) it can also be reenabled for type descriptions.
2014-06-26 13:46:54 +02:00
|
|
|
let actual_type_name = compute_debuginfo_type_name(cx,
|
|
|
|
actual_type,
|
|
|
|
true);
|
2015-02-18 14:48:57 -05:00
|
|
|
name_to_append_suffix_to.push_str(&actual_type_name[..]);
|
2016-04-06 10:49:50 +03:00
|
|
|
}
|
|
|
|
name_to_append_suffix_to.push('>');
|
2013-08-15 12:25:35 +02:00
|
|
|
|
2016-04-06 10:49:50 +03:00
|
|
|
// Again, only create type information if full debuginfo is enabled
|
|
|
|
let template_params: Vec<_> = if cx.sess().opts.debuginfo == FullDebugInfo {
|
2016-08-10 20:39:09 +03:00
|
|
|
let names = get_type_parameter_names(cx, generics);
|
2016-08-18 08:32:50 +03:00
|
|
|
substs.types().zip(names).map(|(ty, name)| {
|
2016-08-27 01:13:48 +03:00
|
|
|
let actual_type = cx.tcx().normalize_associated_type(&ty);
|
2016-06-21 18:08:13 -04:00
|
|
|
let actual_type_metadata = type_metadata(cx, actual_type, syntax_pos::DUMMY_SP);
|
2016-08-10 20:39:09 +03:00
|
|
|
let name = CString::new(name.as_str().as_bytes()).unwrap();
|
2016-04-06 10:49:50 +03:00
|
|
|
unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
|
2014-11-25 13:28:35 -08:00
|
|
|
DIB(cx),
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
ptr::null_mut(),
|
2014-11-25 13:28:35 -08:00
|
|
|
name.as_ptr(),
|
|
|
|
actual_type_metadata,
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
file_metadata,
|
2014-11-25 13:28:35 -08:00
|
|
|
0,
|
|
|
|
0)
|
2016-04-06 10:49:50 +03:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
2013-08-15 18:21:16 +02:00
|
|
|
|
2015-02-18 14:48:57 -05:00
|
|
|
return create_DIArray(DIB(cx), &template_params[..]);
|
2013-08-08 18:33:06 +02:00
|
|
|
}
|
2016-05-01 12:46:55 -04:00
|
|
|
|
2017-01-25 22:01:11 +02:00
|
|
|
fn get_type_parameter_names(cx: &CrateContext, generics: &ty::Generics) -> Vec<ast::Name> {
|
2016-08-10 20:39:09 +03:00
|
|
|
let mut names = generics.parent.map_or(vec![], |def_id| {
|
2017-04-24 15:20:46 +03:00
|
|
|
get_type_parameter_names(cx, cx.tcx().generics_of(def_id))
|
2016-08-10 20:39:09 +03:00
|
|
|
});
|
|
|
|
names.extend(generics.types.iter().map(|param| param.name));
|
|
|
|
names
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:50:55 +03:00
|
|
|
fn get_containing_scope<'ccx, 'tcx>(cx: &CrateContext<'ccx, 'tcx>,
|
|
|
|
instance: Instance<'tcx>)
|
|
|
|
-> DIScope {
|
2016-05-01 12:46:55 -04:00
|
|
|
// First, let's see if this is a method within an inherent impl. Because
|
|
|
|
// if yes, we want to make the result subroutine DIE a child of the
|
|
|
|
// subroutine's self-type.
|
2017-02-08 18:31:03 +01:00
|
|
|
let self_type = cx.tcx().impl_of_method(instance.def_id()).and_then(|impl_def_id| {
|
2016-05-01 12:46:55 -04:00
|
|
|
// If the method does *not* belong to a trait, proceed
|
|
|
|
if cx.tcx().trait_id_of_impl(impl_def_id).is_none() {
|
2017-03-01 01:30:41 +02:00
|
|
|
let impl_self_ty =
|
|
|
|
common::def_ty(cx.shared(), impl_def_id, instance.substs);
|
2016-08-26 07:39:17 +03:00
|
|
|
|
|
|
|
// Only "class" methods are generally understood by LLVM,
|
|
|
|
// so avoid methods on other types (e.g. `<*mut T>::null`).
|
|
|
|
match impl_self_ty.sty {
|
2017-01-30 23:18:29 +03:00
|
|
|
ty::TyAdt(def, ..) if !def.is_box() => {
|
2016-08-26 07:39:17 +03:00
|
|
|
Some(type_metadata(cx, impl_self_ty, syntax_pos::DUMMY_SP))
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
2016-05-01 12:46:55 -04:00
|
|
|
} else {
|
|
|
|
// For trait method impls we still use the "parallel namespace"
|
|
|
|
// strategy
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-17 22:50:55 +03:00
|
|
|
self_type.unwrap_or_else(|| {
|
2016-05-01 12:46:55 -04:00
|
|
|
namespace::item_namespace(cx, DefId {
|
2017-02-08 18:31:03 +01:00
|
|
|
krate: instance.def_id().krate,
|
2016-05-01 12:46:55 -04:00
|
|
|
index: cx.tcx()
|
2017-02-08 18:31:03 +01:00
|
|
|
.def_key(instance.def_id())
|
2016-05-01 12:46:55 -04:00
|
|
|
.parent
|
2016-08-17 22:50:55 +03:00
|
|
|
.expect("get_containing_scope: missing parent?")
|
2016-05-01 12:46:55 -04:00
|
|
|
})
|
2016-08-17 22:50:55 +03:00
|
|
|
})
|
2016-05-01 12:46:55 -04:00
|
|
|
}
|
2013-08-19 18:23:43 +02:00
|
|
|
}
|
2013-06-18 13:06:41 +02:00
|
|
|
|
2016-12-31 16:00:24 -07:00
|
|
|
pub fn declare_local<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
|
2016-12-18 23:04:25 -07:00
|
|
|
dbg_context: &FunctionDebugContext,
|
2016-12-17 19:54:32 -07:00
|
|
|
variable_name: ast::Name,
|
|
|
|
variable_type: Ty<'tcx>,
|
|
|
|
scope_metadata: DIScope,
|
|
|
|
variable_access: VariableAccess,
|
|
|
|
variable_kind: VariableKind,
|
|
|
|
span: Span) {
|
2016-12-19 16:25:00 -07:00
|
|
|
let cx = bcx.ccx;
|
2014-05-15 14:13:33 +02:00
|
|
|
|
2016-06-09 16:36:20 -04:00
|
|
|
let file = span_start(cx, span).file;
|
2017-04-24 19:01:19 +02:00
|
|
|
let file_metadata = file_metadata(cx,
|
|
|
|
&file.name[..],
|
|
|
|
dbg_context.get_ref(span).defining_crate);
|
2014-05-30 17:09:16 +02:00
|
|
|
|
2015-04-29 18:14:37 +12:00
|
|
|
let loc = span_start(cx, span);
|
|
|
|
let type_metadata = type_metadata(cx, variable_type, span);
|
2014-05-15 14:13:33 +02:00
|
|
|
|
2015-04-29 18:14:37 +12:00
|
|
|
let (argument_index, dwarf_tag) = match variable_kind {
|
|
|
|
ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
|
|
|
|
LocalVariable |
|
|
|
|
CapturedVariable => (0, DW_TAG_auto_variable)
|
|
|
|
};
|
2017-03-02 05:35:25 +02:00
|
|
|
let align = cx.align_of(variable_type);
|
2014-05-15 14:13:33 +02:00
|
|
|
|
2015-07-28 18:07:20 +02:00
|
|
|
let name = CString::new(variable_name.as_str().as_bytes()).unwrap();
|
2015-04-29 18:14:37 +12:00
|
|
|
match (variable_access, &[][..]) {
|
|
|
|
(DirectVariable { alloca }, address_operations) |
|
|
|
|
(IndirectVariable {alloca, address_operations}, _) => {
|
|
|
|
let metadata = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateVariable(
|
2015-04-29 18:14:37 +12:00
|
|
|
DIB(cx),
|
|
|
|
dwarf_tag,
|
|
|
|
scope_metadata,
|
|
|
|
name.as_ptr(),
|
|
|
|
file_metadata,
|
|
|
|
loc.line as c_uint,
|
|
|
|
type_metadata,
|
2015-12-31 16:50:06 +13:00
|
|
|
cx.sess().opts.optimize != config::OptLevel::No,
|
2016-11-18 17:15:14 -05:00
|
|
|
DIFlags::FlagZero,
|
2016-11-18 11:11:18 -05:00
|
|
|
argument_index,
|
2017-02-05 20:19:27 +13:00
|
|
|
align,
|
2016-11-18 11:11:18 -05:00
|
|
|
)
|
2015-04-29 18:14:37 +12:00
|
|
|
};
|
2016-12-18 23:04:25 -07:00
|
|
|
source_loc::set_debug_location(bcx,
|
2016-04-07 22:35:11 +03:00
|
|
|
InternalDebugLocation::new(scope_metadata, loc.line, loc.col.to_usize()));
|
2015-04-29 18:14:37 +12:00
|
|
|
unsafe {
|
2016-12-16 18:48:25 -07:00
|
|
|
let debug_loc = llvm::LLVMGetCurrentDebugLocation(bcx.llbuilder);
|
2016-08-02 02:35:09 +03:00
|
|
|
let instr = llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
|
2015-04-29 18:14:37 +12:00
|
|
|
DIB(cx),
|
|
|
|
alloca,
|
|
|
|
metadata,
|
|
|
|
address_operations.as_ptr(),
|
|
|
|
address_operations.len() as c_uint,
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
debug_loc,
|
2016-12-10 20:32:44 -07:00
|
|
|
bcx.llbb());
|
2015-04-29 18:14:37 +12:00
|
|
|
|
2016-12-10 20:32:44 -07:00
|
|
|
llvm::LLVMSetInstDebugLocation(bcx.llbuilder, instr);
|
2014-05-15 14:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 18:14:37 +12:00
|
|
|
|
|
|
|
match variable_kind {
|
|
|
|
ArgumentVariable(_) | CapturedVariable => {
|
2016-12-18 23:04:25 -07:00
|
|
|
assert!(!dbg_context.get_ref(span).source_locations_enabled.get());
|
|
|
|
source_loc::set_debug_location(bcx, UnknownLocation);
|
2015-04-29 18:14:37 +12:00
|
|
|
}
|
|
|
|
_ => { /* nothing to do */ }
|
|
|
|
}
|
2014-05-15 14:13:33 +02:00
|
|
|
}
|