Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | // See doc.rs for documentation. |
| 2 | mod doc; |
| 3 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 4 | use rustc_codegen_ssa::mir::debuginfo::VariableKind::*; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 5 | |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 6 | use self::metadata::{file_metadata, type_metadata, TypeMap}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 7 | use self::namespace::mangled_name_of_instance; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 8 | use self::type_names::compute_debuginfo_type_name; |
| 9 | use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 10 | |
| 11 | use crate::llvm; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 12 | use crate::llvm::debuginfo::{ |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 13 | DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DISPFlags, DIScope, DIType, DIVariable, |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 14 | }; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 15 | use rustc::ty::subst::{GenericArgKind, SubstsRef}; |
| 16 | use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 17 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 18 | use crate::abi::FnAbi; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 19 | use crate::builder::Builder; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 20 | use crate::common::CodegenCx; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 21 | use crate::value::Value; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 22 | use rustc::mir; |
| 23 | use rustc::session::config::{self, DebugInfo}; |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 24 | use rustc::ty::{self, Instance, ParamEnv, Ty}; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 25 | use rustc_codegen_ssa::debuginfo::type_names; |
| 26 | use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind}; |
| 27 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 28 | use rustc_index::vec::IndexVec; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 29 | |
| 30 | use libc::c_uint; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 31 | use log::debug; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 32 | use std::cell::RefCell; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 33 | |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 34 | use rustc::ty::layout::{self, HasTyCtxt, LayoutOf, Size}; |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 35 | use rustc_ast::ast; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 36 | use rustc_codegen_ssa::traits::*; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 37 | use rustc_span::symbol::Symbol; |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 38 | use rustc_span::{self, BytePos, Span}; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 39 | use smallvec::SmallVec; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 40 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 41 | mod create_scope_map; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 42 | pub mod gdb; |
| 43 | pub mod metadata; |
| 44 | mod namespace; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 45 | mod source_loc; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 46 | mod utils; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 47 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 48 | pub use self::create_scope_map::compute_mir_scopes; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 49 | pub use self::metadata::create_global_var_metadata; |
| 50 | pub use self::metadata::extend_scope_to_file; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 51 | |
| 52 | #[allow(non_upper_case_globals)] |
| 53 | const DW_TAG_auto_variable: c_uint = 0x100; |
| 54 | #[allow(non_upper_case_globals)] |
| 55 | const DW_TAG_arg_variable: c_uint = 0x101; |
| 56 | |
| 57 | /// A context object for maintaining all state needed by the debuginfo module. |
| 58 | pub struct CrateDebugContext<'a, 'tcx> { |
| 59 | llcontext: &'a llvm::Context, |
| 60 | llmod: &'a llvm::Module, |
| 61 | builder: &'a mut DIBuilder<'a>, |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame] | 62 | created_files: RefCell<FxHashMap<(Option<String>, Option<String>), &'a DIFile>>, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 63 | created_enum_disr_types: RefCell<FxHashMap<(DefId, layout::Primitive), &'a DIType>>, |
| 64 | |
| 65 | type_map: RefCell<TypeMap<'a, 'tcx>>, |
| 66 | namespace_map: RefCell<DefIdMap<&'a DIScope>>, |
| 67 | |
| 68 | // This collection is used to assert that composite types (structs, enums, |
| 69 | // ...) have their members only set once: |
| 70 | composite_types_completed: RefCell<FxHashSet<&'a DIType>>, |
| 71 | } |
| 72 | |
| 73 | impl Drop for CrateDebugContext<'a, 'tcx> { |
| 74 | fn drop(&mut self) { |
| 75 | unsafe { |
| 76 | llvm::LLVMRustDIBuilderDispose(&mut *(self.builder as *mut _)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> { |
| 82 | pub fn new(llmod: &'a llvm::Module) -> Self { |
| 83 | debug!("CrateDebugContext::new"); |
| 84 | let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) }; |
| 85 | // DIBuilder inherits context from the module, so we'd better use the same one |
| 86 | let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) }; |
| 87 | CrateDebugContext { |
| 88 | llcontext, |
| 89 | llmod, |
| 90 | builder, |
| 91 | created_files: Default::default(), |
| 92 | created_enum_disr_types: Default::default(), |
| 93 | type_map: Default::default(), |
| 94 | namespace_map: RefCell::new(Default::default()), |
| 95 | composite_types_completed: Default::default(), |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// Creates any deferred debug metadata nodes |
| 101 | pub fn finalize(cx: &CodegenCx<'_, '_>) { |
| 102 | if cx.dbg_cx.is_none() { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | debug!("finalize"); |
| 107 | |
| 108 | if gdb::needs_gdb_debug_scripts_section(cx) { |
| 109 | // Add a .debug_gdb_scripts section to this compile-unit. This will |
| 110 | // cause GDB to try and load the gdb_load_rust_pretty_printers.py file, |
| 111 | // which activates the Rust pretty printers for binary this section is |
| 112 | // contained in. |
| 113 | gdb::get_or_insert_gdb_debug_scripts_section_global(cx); |
| 114 | } |
| 115 | |
| 116 | unsafe { |
| 117 | llvm::LLVMRustDIBuilderFinalize(DIB(cx)); |
| 118 | // Debuginfo generation in LLVM by default uses a higher |
| 119 | // version of dwarf than macOS currently understands. We can |
| 120 | // instruct LLVM to emit an older version of dwarf, however, |
| 121 | // for macOS to understand. For more info see #11352 |
| 122 | // This can be overridden using --llvm-opts -dwarf-version,N. |
| 123 | // Android has the same issue (#22398) |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 124 | if cx.sess().target.target.options.is_like_osx |
| 125 | || cx.sess().target.target.options.is_like_android |
| 126 | { |
| 127 | llvm::LLVMRustAddModuleFlag(cx.llmod, "Dwarf Version\0".as_ptr().cast(), 2) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Indicate that we want CodeView debug information on MSVC |
| 131 | if cx.sess().target.target.options.is_like_msvc { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 132 | llvm::LLVMRustAddModuleFlag(cx.llmod, "CodeView\0".as_ptr().cast(), 1) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Prevent bitcode readers from deleting the debug info. |
| 136 | let ptr = "Debug Info Version\0".as_ptr(); |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 137 | llvm::LLVMRustAddModuleFlag(cx.llmod, ptr.cast(), llvm::LLVMRustDebugMetadataVersion()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 138 | }; |
| 139 | } |
| 140 | |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 141 | impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> { |
| 142 | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
| 143 | // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.). |
| 144 | fn dbg_var_addr( |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 145 | &mut self, |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 146 | dbg_var: &'ll DIVariable, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 147 | scope_metadata: &'ll DIScope, |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 148 | variable_alloca: Self::Value, |
| 149 | direct_offset: Size, |
| 150 | indirect_offsets: &[Size], |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 151 | span: Span, |
| 152 | ) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 153 | let cx = self.cx(); |
| 154 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 155 | // Convert the direct and indirect offsets to address ops. |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 156 | // FIXME(eddyb) use `const`s instead of getting the values via FFI, |
| 157 | // the values should match the ones in the DWARF standard anyway. |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 158 | let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() }; |
| 159 | let op_plus_uconst = || unsafe { llvm::LLVMRustDIBuilderCreateOpPlusUconst() }; |
| 160 | let mut addr_ops = SmallVec::<[_; 8]>::new(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 161 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 162 | if direct_offset.bytes() > 0 { |
| 163 | addr_ops.push(op_plus_uconst()); |
| 164 | addr_ops.push(direct_offset.bytes() as i64); |
| 165 | } |
| 166 | for &offset in indirect_offsets { |
| 167 | addr_ops.push(op_deref()); |
| 168 | if offset.bytes() > 0 { |
| 169 | addr_ops.push(op_plus_uconst()); |
| 170 | addr_ops.push(offset.bytes() as i64); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 173 | |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 174 | // FIXME(eddyb) maybe this information could be extracted from `dbg_var`, |
| 175 | // to avoid having to pass it down in both places? |
| 176 | // NB: `var` doesn't seem to know about the column, so that's a limitation. |
| 177 | let dbg_loc = cx.create_debug_loc(scope_metadata, span); |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 178 | unsafe { |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 179 | // FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`. |
| 180 | llvm::LLVMRustDIBuilderInsertDeclareAtEnd( |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 181 | DIB(cx), |
| 182 | variable_alloca, |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 183 | dbg_var, |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 184 | addr_ops.as_ptr(), |
| 185 | addr_ops.len() as c_uint, |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 186 | dbg_loc, |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 187 | self.llbb(), |
| 188 | ); |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 189 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 192 | fn set_source_location(&mut self, scope: &'ll DIScope, span: Span) { |
| 193 | debug!("set_source_location: {}", self.sess().source_map().span_to_string(span)); |
| 194 | |
| 195 | let dbg_loc = self.cx().create_debug_loc(scope, span); |
| 196 | |
| 197 | unsafe { |
| 198 | llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc); |
| 199 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 200 | } |
| 201 | fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) { |
| 202 | gdb::insert_reference_to_gdb_debug_scripts_section_global(self) |
| 203 | } |
| 204 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 205 | fn set_var_name(&mut self, value: &'ll Value, name: &str) { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 206 | // Avoid wasting time if LLVM value names aren't even enabled. |
| 207 | if self.sess().fewer_names() { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Only function parameters and instructions are local to a function, |
| 212 | // don't change the name of anything else (e.g. globals). |
| 213 | let param_or_inst = unsafe { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 214 | llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some() |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 215 | }; |
| 216 | if !param_or_inst { |
| 217 | return; |
| 218 | } |
| 219 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 220 | // Avoid replacing the name if it already exists. |
| 221 | // While we could combine the names somehow, it'd |
| 222 | // get noisy quick, and the usefulness is dubious. |
| 223 | if llvm::get_value_name(value).is_empty() { |
| 224 | llvm::set_value_name(value, name.as_bytes()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 230 | fn create_function_debug_context( |
| 231 | &self, |
| 232 | instance: Instance<'tcx>, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 233 | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 234 | llfn: &'ll Value, |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame] | 235 | mir: &mir::Body<'_>, |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 236 | ) -> Option<FunctionDebugContext<&'ll DIScope>> { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 237 | if self.sess().opts.debuginfo == DebugInfo::None { |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 238 | return None; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 241 | let span = mir.span; |
| 242 | |
| 243 | // This can be the case for functions inlined from another crate |
| 244 | if span.is_dummy() { |
| 245 | // FIXME(simulacrum): Probably can't happen; remove. |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 246 | return None; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | let def_id = instance.def_id(); |
| 250 | let containing_scope = get_containing_scope(self, instance); |
| 251 | let loc = span_start(self, span); |
| 252 | let file_metadata = file_metadata(self, &loc.file.name, def_id.krate); |
| 253 | |
| 254 | let function_type_metadata = unsafe { |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 255 | let fn_signature = get_function_signature(self, fn_abi); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 256 | llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(self), file_metadata, fn_signature) |
| 257 | }; |
| 258 | |
| 259 | // Find the enclosing function, in case this is a closure. |
| 260 | let def_key = self.tcx().def_key(def_id); |
| 261 | let mut name = def_key.disambiguated_data.data.to_string(); |
| 262 | |
| 263 | let enclosing_fn_def_id = self.tcx().closure_base_def_id(def_id); |
| 264 | |
| 265 | // Get_template_parameters() will append a `<...>` clause to the function |
| 266 | // name if necessary. |
| 267 | let generics = self.tcx().generics_of(enclosing_fn_def_id); |
| 268 | let substs = instance.substs.truncate_to(self.tcx(), generics); |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 269 | let template_parameters = |
| 270 | get_template_parameters(self, &generics, substs, file_metadata, &mut name); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 271 | |
| 272 | // Get the linkage_name, which is just the symbol name |
| 273 | let linkage_name = mangled_name_of_instance(self, instance); |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 274 | let linkage_name = linkage_name.name.as_str(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 275 | |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 276 | // FIXME(eddyb) does this need to be separate from `loc.line` for some reason? |
| 277 | let scope_line = loc.line; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 278 | |
| 279 | let mut flags = DIFlags::FlagPrototyped; |
| 280 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 281 | if fn_abi.ret.layout.abi.is_uninhabited() { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 282 | flags |= DIFlags::FlagNoReturn; |
| 283 | } |
| 284 | |
| 285 | let mut spflags = DISPFlags::SPFlagDefinition; |
| 286 | if is_node_local_to_unit(self, def_id) { |
| 287 | spflags |= DISPFlags::SPFlagLocalToUnit; |
| 288 | } |
| 289 | if self.sess().opts.optimize != config::OptLevel::No { |
| 290 | spflags |= DISPFlags::SPFlagOptimized; |
| 291 | } |
| 292 | if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) { |
| 293 | if id == def_id { |
| 294 | spflags |= DISPFlags::SPFlagMainSubprogram; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | let fn_metadata = unsafe { |
| 299 | llvm::LLVMRustDIBuilderCreateFunction( |
| 300 | DIB(self), |
| 301 | containing_scope, |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 302 | name.as_ptr().cast(), |
| 303 | name.len(), |
| 304 | linkage_name.as_ptr().cast(), |
| 305 | linkage_name.len(), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 306 | file_metadata, |
| 307 | loc.line as c_uint, |
| 308 | function_type_metadata, |
| 309 | scope_line as c_uint, |
| 310 | flags, |
| 311 | spflags, |
| 312 | llfn, |
| 313 | template_parameters, |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 314 | None, |
| 315 | ) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 318 | // Initialize fn debug context (including scopes). |
| 319 | // FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`. |
| 320 | let null_scope = DebugScope { |
| 321 | scope_metadata: None, |
| 322 | file_start_pos: BytePos(0), |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 323 | file_end_pos: BytePos(0), |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 324 | }; |
| 325 | let mut fn_debug_context = FunctionDebugContext { |
| 326 | scopes: IndexVec::from_elem(null_scope, &mir.source_scopes), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 327 | defining_crate: def_id.krate, |
| 328 | }; |
| 329 | |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 330 | // Fill in all the scopes, with the information from the MIR body. |
| 331 | compute_mir_scopes(self, mir, fn_metadata, &mut fn_debug_context); |
| 332 | |
| 333 | return Some(fn_debug_context); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 334 | |
| 335 | fn get_function_signature<'ll, 'tcx>( |
| 336 | cx: &CodegenCx<'ll, 'tcx>, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 337 | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 338 | ) -> &'ll DIArray { |
| 339 | if cx.sess().opts.debuginfo == DebugInfo::Limited { |
| 340 | return create_DIArray(DIB(cx), &[]); |
| 341 | } |
| 342 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 343 | let mut signature = Vec::with_capacity(fn_abi.args.len() + 1); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 344 | |
| 345 | // Return type -- llvm::DIBuilder wants this at index 0 |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 346 | signature.push(if fn_abi.ret.is_ignore() { |
| 347 | None |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 348 | } else { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 349 | Some(type_metadata(cx, fn_abi.ret.layout.ty, rustc_span::DUMMY_SP)) |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 350 | }); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 351 | |
| 352 | // Arguments types |
| 353 | if cx.sess().target.target.options.is_like_msvc { |
| 354 | // FIXME(#42800): |
| 355 | // There is a bug in MSDIA that leads to a crash when it encounters |
| 356 | // a fixed-size array of `u8` or something zero-sized in a |
| 357 | // function-type (see #40477). |
| 358 | // As a workaround, we replace those fixed-size arrays with a |
| 359 | // pointer-type. So a function `fn foo(a: u8, b: [u8; 4])` would |
| 360 | // appear as `fn foo(a: u8, b: *const u8)` in debuginfo, |
| 361 | // and a function `fn bar(x: [(); 7])` as `fn bar(x: *const ())`. |
| 362 | // This transformed type is wrong, but these function types are |
| 363 | // already inaccurate due to ABI adjustments (see #42800). |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 364 | signature.extend(fn_abi.args.iter().map(|arg| { |
| 365 | let t = arg.layout.ty; |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 366 | let t = match t.kind { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 367 | ty::Array(ct, _) |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 368 | if (ct == cx.tcx.types.u8) || cx.layout_of(ct).is_zst() => |
| 369 | { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 370 | cx.tcx.mk_imm_ptr(ct) |
| 371 | } |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 372 | _ => t, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 373 | }; |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 374 | Some(type_metadata(cx, t, rustc_span::DUMMY_SP)) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 375 | })); |
| 376 | } else { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 377 | signature.extend( |
| 378 | fn_abi |
| 379 | .args |
| 380 | .iter() |
| 381 | .map(|arg| Some(type_metadata(cx, arg.layout.ty, rustc_span::DUMMY_SP))), |
| 382 | ); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 385 | create_DIArray(DIB(cx), &signature[..]) |
| 386 | } |
| 387 | |
| 388 | fn get_template_parameters<'ll, 'tcx>( |
| 389 | cx: &CodegenCx<'ll, 'tcx>, |
| 390 | generics: &ty::Generics, |
| 391 | substs: SubstsRef<'tcx>, |
| 392 | file_metadata: &'ll DIFile, |
| 393 | name_to_append_suffix_to: &mut String, |
| 394 | ) -> &'ll DIArray { |
| 395 | if substs.types().next().is_none() { |
| 396 | return create_DIArray(DIB(cx), &[]); |
| 397 | } |
| 398 | |
| 399 | name_to_append_suffix_to.push('<'); |
| 400 | for (i, actual_type) in substs.types().enumerate() { |
| 401 | if i != 0 { |
| 402 | name_to_append_suffix_to.push_str(","); |
| 403 | } |
| 404 | |
| 405 | let actual_type = |
| 406 | cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), actual_type); |
| 407 | // Add actual type name to <...> clause of function name |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 408 | let actual_type_name = compute_debuginfo_type_name(cx.tcx(), actual_type, true); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 409 | name_to_append_suffix_to.push_str(&actual_type_name[..]); |
| 410 | } |
| 411 | name_to_append_suffix_to.push('>'); |
| 412 | |
| 413 | // Again, only create type information if full debuginfo is enabled |
| 414 | let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full { |
| 415 | let names = get_parameter_names(cx, generics); |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 416 | substs |
| 417 | .iter() |
| 418 | .zip(names) |
| 419 | .filter_map(|(kind, name)| { |
| 420 | if let GenericArgKind::Type(ty) = kind.unpack() { |
| 421 | let actual_type = |
| 422 | cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); |
| 423 | let actual_type_metadata = |
| 424 | type_metadata(cx, actual_type, rustc_span::DUMMY_SP); |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 425 | let name = name.as_str(); |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 426 | Some(unsafe { |
| 427 | Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( |
| 428 | DIB(cx), |
| 429 | None, |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 430 | name.as_ptr().cast(), |
| 431 | name.len(), |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 432 | actual_type_metadata, |
| 433 | file_metadata, |
| 434 | 0, |
| 435 | 0, |
| 436 | )) |
| 437 | }) |
| 438 | } else { |
| 439 | None |
| 440 | } |
| 441 | }) |
| 442 | .collect() |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 443 | } else { |
| 444 | vec![] |
| 445 | }; |
| 446 | |
| 447 | return create_DIArray(DIB(cx), &template_params[..]); |
| 448 | } |
| 449 | |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 450 | fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> { |
| 451 | let mut names = generics |
| 452 | .parent |
| 453 | .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 454 | names.extend(generics.params.iter().map(|param| param.name)); |
| 455 | names |
| 456 | } |
| 457 | |
| 458 | fn get_containing_scope<'ll, 'tcx>( |
| 459 | cx: &CodegenCx<'ll, 'tcx>, |
| 460 | instance: Instance<'tcx>, |
| 461 | ) -> &'ll DIScope { |
| 462 | // First, let's see if this is a method within an inherent impl. Because |
| 463 | // if yes, we want to make the result subroutine DIE a child of the |
| 464 | // subroutine's self-type. |
| 465 | let self_type = cx.tcx.impl_of_method(instance.def_id()).and_then(|impl_def_id| { |
| 466 | // If the method does *not* belong to a trait, proceed |
| 467 | if cx.tcx.trait_id_of_impl(impl_def_id).is_none() { |
| 468 | let impl_self_ty = cx.tcx.subst_and_normalize_erasing_regions( |
| 469 | instance.substs, |
| 470 | ty::ParamEnv::reveal_all(), |
| 471 | &cx.tcx.type_of(impl_def_id), |
| 472 | ); |
| 473 | |
| 474 | // Only "class" methods are generally understood by LLVM, |
| 475 | // so avoid methods on other types (e.g., `<*mut T>::null`). |
Chih-Hung Hsieh | 2ccedcd | 2019-12-19 15:10:50 -0800 | [diff] [blame] | 476 | match impl_self_ty.kind { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 477 | ty::Adt(def, ..) if !def.is_box() => { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 478 | Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP)) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 479 | } |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 480 | _ => None, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 481 | } |
| 482 | } else { |
| 483 | // For trait method impls we still use the "parallel namespace" |
| 484 | // strategy |
| 485 | None |
| 486 | } |
| 487 | }); |
| 488 | |
| 489 | self_type.unwrap_or_else(|| { |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 490 | namespace::item_namespace( |
| 491 | cx, |
| 492 | DefId { |
| 493 | krate: instance.def_id().krate, |
| 494 | index: cx |
| 495 | .tcx |
| 496 | .def_key(instance.def_id()) |
| 497 | .parent |
| 498 | .expect("get_containing_scope: missing parent?"), |
| 499 | }, |
| 500 | ) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 501 | }) |
| 502 | } |
| 503 | } |
| 504 | |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 505 | fn create_vtable_metadata(&self, ty: Ty<'tcx>, vtable: Self::Value) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 506 | metadata::create_vtable_metadata(self, ty, vtable) |
| 507 | } |
| 508 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 509 | fn extend_scope_to_file( |
Matthew Maurer | 859223d | 2020-03-27 12:47:38 -0700 | [diff] [blame] | 510 | &self, |
| 511 | scope_metadata: &'ll DIScope, |
| 512 | file: &rustc_span::SourceFile, |
| 513 | defining_crate: CrateNum, |
| 514 | ) -> &'ll DILexicalBlock { |
| 515 | metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate) |
| 516 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 517 | |
| 518 | fn debuginfo_finalize(&self) { |
| 519 | finalize(self) |
| 520 | } |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame^] | 521 | |
| 522 | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
| 523 | // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.). |
| 524 | fn create_dbg_var( |
| 525 | &self, |
| 526 | dbg_context: &FunctionDebugContext<&'ll DIScope>, |
| 527 | variable_name: ast::Name, |
| 528 | variable_type: Ty<'tcx>, |
| 529 | scope_metadata: &'ll DIScope, |
| 530 | variable_kind: VariableKind, |
| 531 | span: Span, |
| 532 | ) -> &'ll DIVariable { |
| 533 | let loc = span_start(self, span); |
| 534 | let file_metadata = file_metadata(self, &loc.file.name, dbg_context.defining_crate); |
| 535 | |
| 536 | let type_metadata = type_metadata(self, variable_type, span); |
| 537 | |
| 538 | let (argument_index, dwarf_tag) = match variable_kind { |
| 539 | ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable), |
| 540 | LocalVariable => (0, DW_TAG_auto_variable), |
| 541 | }; |
| 542 | let align = self.align_of(variable_type); |
| 543 | |
| 544 | let name = variable_name.as_str(); |
| 545 | unsafe { |
| 546 | llvm::LLVMRustDIBuilderCreateVariable( |
| 547 | DIB(self), |
| 548 | dwarf_tag, |
| 549 | scope_metadata, |
| 550 | name.as_ptr().cast(), |
| 551 | name.len(), |
| 552 | file_metadata, |
| 553 | loc.line as c_uint, |
| 554 | type_metadata, |
| 555 | true, |
| 556 | DIFlags::FlagZero, |
| 557 | argument_index, |
| 558 | align.bytes() as u32, |
| 559 | ) |
| 560 | } |
| 561 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 562 | } |