| ... | ... | @@ -51,7 +51,6 @@ mod utils; |
| 51 | 51 | |
| 52 | 52 | /// A context object for maintaining all state needed by the debuginfo module. |
| 53 | 53 | pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> { |
| 54 | | llmod: &'ll llvm::Module, |
| 55 | 54 | builder: DIBuilderBox<'ll>, |
| 56 | 55 | created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>, |
| 57 | 56 | |
| ... | ... | @@ -62,23 +61,8 @@ pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> { |
| 62 | 61 | } |
| 63 | 62 | |
| 64 | 63 | impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { |
| 65 | | pub(crate) fn new(llmod: &'ll llvm::Module) -> Self { |
| 64 | pub(crate) fn new(llmod: &'ll llvm::Module, sess: &Session) -> Self { |
| 66 | 65 | debug!("CodegenUnitDebugContext::new"); |
| 67 | | let builder = DIBuilderBox::new(llmod); |
| 68 | | // DIBuilder inherits context from the module, so we'd better use the same one |
| 69 | | CodegenUnitDebugContext { |
| 70 | | llmod, |
| 71 | | builder, |
| 72 | | created_files: Default::default(), |
| 73 | | type_map: Default::default(), |
| 74 | | adt_stack: Default::default(), |
| 75 | | namespace_map: RefCell::new(Default::default()), |
| 76 | | recursion_marker_type: OnceCell::new(), |
| 77 | | } |
| 78 | | } |
| 79 | | |
| 80 | | pub(crate) fn finalize(&self, sess: &Session) { |
| 81 | | unsafe { llvm::LLVMDIBuilderFinalize(self.builder.as_ref()) }; |
| 82 | 66 | |
| 83 | 67 | match sess.target.debuginfo_kind { |
| 84 | 68 | DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => { |
| ... | ... | @@ -89,7 +73,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { |
| 89 | 73 | // This can be overridden using --llvm-opts -dwarf-version,N. |
| 90 | 74 | // Android has the same issue (#22398) |
| 91 | 75 | llvm::add_module_flag_u32( |
| 92 | | self.llmod, |
| 76 | llmod, |
| 93 | 77 | // In the case where multiple CGUs with different dwarf version |
| 94 | 78 | // values are being merged together, such as with cross-crate |
| 95 | 79 | // LTO, then we want to use the highest version of dwarf |
| ... | ... | @@ -102,7 +86,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { |
| 102 | 86 | DebuginfoKind::Pdb => { |
| 103 | 87 | // Indicate that we want CodeView debug information |
| 104 | 88 | llvm::add_module_flag_u32( |
| 105 | | self.llmod, |
| 89 | llmod, |
| 106 | 90 | llvm::ModuleFlagMergeBehavior::Warning, |
| 107 | 91 | "CodeView", |
| 108 | 92 | 1, |
| ... | ... | @@ -112,28 +96,26 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { |
| 112 | 96 | |
| 113 | 97 | // Prevent bitcode readers from deleting the debug info. |
| 114 | 98 | llvm::add_module_flag_u32( |
| 115 | | self.llmod, |
| 99 | llmod, |
| 116 | 100 | llvm::ModuleFlagMergeBehavior::Warning, |
| 117 | 101 | "Debug Info Version", |
| 118 | 102 | unsafe { llvm::LLVMRustDebugMetadataVersion() }, |
| 119 | 103 | ); |
| 120 | | } |
| 121 | | } |
| 122 | 104 | |
| 123 | | /// Creates any deferred debug metadata nodes |
| 124 | | pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) { |
| 125 | | if let Some(dbg_cx) = &cx.dbg_cx { |
| 126 | | debug!("finalize"); |
| 127 | | |
| 128 | | if gdb::needs_gdb_debug_scripts_section(cx) { |
| 129 | | // Add a .debug_gdb_scripts section to this compile-unit. This will |
| 130 | | // cause GDB to try and load the gdb_load_rust_pretty_printers.py file, |
| 131 | | // which activates the Rust pretty printers for binary this section is |
| 132 | | // contained in. |
| 133 | | gdb::get_or_insert_gdb_debug_scripts_section_global(cx); |
| 105 | let builder = DIBuilderBox::new(llmod); |
| 106 | // DIBuilder inherits context from the module, so we'd better use the same one |
| 107 | CodegenUnitDebugContext { |
| 108 | builder, |
| 109 | created_files: Default::default(), |
| 110 | type_map: Default::default(), |
| 111 | adt_stack: Default::default(), |
| 112 | namespace_map: RefCell::new(Default::default()), |
| 113 | recursion_marker_type: OnceCell::new(), |
| 134 | 114 | } |
| 115 | } |
| 135 | 116 | |
| 136 | | dbg_cx.finalize(cx.sess()); |
| 117 | pub(crate) fn finalize(&self) { |
| 118 | unsafe { llvm::LLVMDIBuilderFinalize(self.builder.as_ref()) }; |
| 137 | 119 | } |
| 138 | 120 | } |
| 139 | 121 | |
| ... | ... | @@ -144,381 +126,109 @@ impl<'ll> Builder<'_, 'll, '_> { |
| 144 | 126 | } |
| 145 | 127 | |
| 146 | 128 | impl<'ll, 'tcx> DebugInfoBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { |
| 147 | | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
| 148 | | // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.). |
| 149 | | fn dbg_var_addr( |
| 129 | fn dbg_scope_fn( |
| 150 | 130 | &mut self, |
| 151 | | dbg_var: &'ll DIVariable, |
| 152 | | dbg_loc: &'ll DILocation, |
| 153 | | variable_alloca: Self::Value, |
| 154 | | direct_offset: Size, |
| 155 | | indirect_offsets: &[Size], |
| 156 | | fragment: &Option<Range<Size>>, |
| 157 | | ) { |
| 158 | | use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst}; |
| 131 | instance: Instance<'tcx>, |
| 132 | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
| 133 | maybe_definition_llfn: Option<&'ll Value>, |
| 134 | ) -> &'ll DIScope { |
| 135 | let tcx = self.tcx; |
| 159 | 136 | |
| 160 | | // Convert the direct and indirect offsets and fragment byte range to address ops. |
| 161 | | let mut addr_ops = SmallVec::<[u64; 8]>::new(); |
| 137 | let def_id = instance.def_id(); |
| 138 | let (containing_scope, is_method) = get_containing_scope(self, instance); |
| 139 | let span = tcx.def_span(def_id); |
| 140 | let loc = self.lookup_debug_loc(span.lo()); |
| 141 | let file_metadata = file_metadata(self, &loc.file); |
| 162 | 142 | |
| 163 | | if direct_offset.bytes() > 0 { |
| 164 | | addr_ops.push(DW_OP_plus_uconst); |
| 165 | | addr_ops.push(direct_offset.bytes()); |
| 166 | | } |
| 167 | | for &offset in indirect_offsets { |
| 168 | | addr_ops.push(DW_OP_deref); |
| 169 | | if offset.bytes() > 0 { |
| 170 | | addr_ops.push(DW_OP_plus_uconst); |
| 171 | | addr_ops.push(offset.bytes()); |
| 172 | | } |
| 173 | | } |
| 174 | | if let Some(fragment) = fragment { |
| 175 | | // `DW_OP_LLVM_fragment` takes as arguments the fragment's |
| 176 | | // offset and size, both of them in bits. |
| 177 | | addr_ops.push(DW_OP_LLVM_fragment); |
| 178 | | addr_ops.push(fragment.start.bits()); |
| 179 | | addr_ops.push((fragment.end - fragment.start).bits()); |
| 180 | | } |
| 143 | let function_type_metadata = |
| 144 | create_subroutine_type(self, &get_function_signature(self, fn_abi)); |
| 181 | 145 | |
| 182 | | let di_builder = DIB(self.cx()); |
| 183 | | let addr_expr = di_builder.create_expression(&addr_ops); |
| 184 | | unsafe { |
| 185 | | llvm::LLVMDIBuilderInsertDeclareRecordAtEnd( |
| 186 | | di_builder, |
| 187 | | variable_alloca, |
| 188 | | dbg_var, |
| 189 | | addr_expr, |
| 190 | | dbg_loc, |
| 191 | | self.llbb(), |
| 192 | | ) |
| 193 | | }; |
| 194 | | } |
| 146 | let mut name = String::with_capacity(64); |
| 147 | type_names::push_item_name(tcx, def_id, false, &mut name); |
| 195 | 148 | |
| 196 | | fn dbg_var_value( |
| 197 | | &mut self, |
| 198 | | dbg_var: &'ll DIVariable, |
| 199 | | dbg_loc: &'ll DILocation, |
| 200 | | value: Self::Value, |
| 201 | | direct_offset: Size, |
| 202 | | indirect_offsets: &[Size], |
| 203 | | fragment: &Option<Range<Size>>, |
| 204 | | ) { |
| 205 | | use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst, DW_OP_stack_value}; |
| 149 | // Find the enclosing function, in case this is a closure. |
| 150 | let enclosing_fn_def_id = tcx.typeck_root_def_id(def_id); |
| 206 | 151 | |
| 207 | | // Convert the direct and indirect offsets and fragment byte range to address ops. |
| 208 | | let mut addr_ops = SmallVec::<[u64; 8]>::new(); |
| 152 | // We look up the generics of the enclosing function and truncate the args |
| 153 | // to their length in order to cut off extra stuff that might be in there for |
| 154 | // closures or coroutines. |
| 155 | let generics = tcx.generics_of(enclosing_fn_def_id); |
| 156 | let args = instance.args.truncate_to(tcx, generics); |
| 209 | 157 | |
| 210 | | if direct_offset.bytes() > 0 { |
| 211 | | addr_ops.push(DW_OP_plus_uconst); |
| 212 | | addr_ops.push(direct_offset.bytes() as u64); |
| 213 | | addr_ops.push(DW_OP_stack_value); |
| 214 | | } |
| 215 | | for &offset in indirect_offsets { |
| 216 | | addr_ops.push(DW_OP_deref); |
| 217 | | if offset.bytes() > 0 { |
| 218 | | addr_ops.push(DW_OP_plus_uconst); |
| 219 | | addr_ops.push(offset.bytes() as u64); |
| 220 | | } |
| 221 | | } |
| 222 | | if let Some(fragment) = fragment { |
| 223 | | // `DW_OP_LLVM_fragment` takes as arguments the fragment's |
| 224 | | // offset and size, both of them in bits. |
| 225 | | addr_ops.push(DW_OP_LLVM_fragment); |
| 226 | | addr_ops.push(fragment.start.bits() as u64); |
| 227 | | addr_ops.push((fragment.end - fragment.start).bits() as u64); |
| 228 | | } |
| 158 | type_names::push_generic_args( |
| 159 | tcx, |
| 160 | tcx.normalize_erasing_regions(self.typing_env(), Unnormalized::new_wip(args)), |
| 161 | &mut name, |
| 162 | ); |
| 229 | 163 | |
| 230 | | let di_builder = DIB(self.cx()); |
| 231 | | let addr_expr = unsafe { |
| 232 | | llvm::LLVMDIBuilderCreateExpression(di_builder, addr_ops.as_ptr(), addr_ops.len()) |
| 233 | | }; |
| 234 | | unsafe { |
| 235 | | llvm::LLVMDIBuilderInsertDbgValueRecordAtEnd( |
| 236 | | di_builder, |
| 237 | | value, |
| 238 | | dbg_var, |
| 239 | | addr_expr, |
| 240 | | dbg_loc, |
| 241 | | self.llbb(), |
| 242 | | ); |
| 243 | | } |
| 244 | | } |
| 164 | let template_parameters = get_template_parameters(self, generics, args); |
| 245 | 165 | |
| 246 | | fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) { |
| 247 | | unsafe { |
| 248 | | llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, dbg_loc); |
| 249 | | } |
| 250 | | } |
| 166 | let linkage_name = &mangled_name_of_instance(self, instance).name; |
| 167 | // Omit the linkage_name if it is the same as subprogram name. |
| 168 | let linkage_name = if &name == linkage_name { "" } else { linkage_name }; |
| 251 | 169 | |
| 252 | | fn clear_dbg_loc(&mut self) { |
| 253 | | unsafe { |
| 254 | | llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, ptr::null()); |
| 255 | | } |
| 256 | | } |
| 170 | // FIXME(eddyb) does this need to be separate from `loc.line` for some reason? |
| 171 | let scope_line = loc.line; |
| 257 | 172 | |
| 258 | | fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) { |
| 259 | | gdb::insert_reference_to_gdb_debug_scripts_section_global(self) |
| 260 | | } |
| 173 | let mut flags = DIFlags::FlagPrototyped; |
| 261 | 174 | |
| 262 | | fn set_var_name(&mut self, value: &'ll Value, name: &str) { |
| 263 | | // Avoid wasting time if LLVM value names aren't even enabled. |
| 264 | | if self.sess().fewer_names() { |
| 265 | | return; |
| 175 | if fn_abi.ret.layout.is_uninhabited() { |
| 176 | flags |= DIFlags::FlagNoReturn; |
| 266 | 177 | } |
| 267 | 178 | |
| 268 | | // Only function parameters and instructions are local to a function, |
| 269 | | // don't change the name of anything else (e.g. globals). |
| 270 | | let param_or_inst = unsafe { |
| 271 | | llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some() |
| 272 | | }; |
| 273 | | if !param_or_inst { |
| 274 | | return; |
| 179 | let mut spflags = DISPFlags::SPFlagDefinition; |
| 180 | if is_node_local_to_unit(self, def_id) { |
| 181 | spflags |= DISPFlags::SPFlagLocalToUnit; |
| 275 | 182 | } |
| 276 | | |
| 277 | | // Avoid replacing the name if it already exists. |
| 278 | | // While we could combine the names somehow, it'd |
| 279 | | // get noisy quick, and the usefulness is dubious. |
| 280 | | if llvm::get_value_name(value).is_empty() { |
| 281 | | llvm::set_value_name(value, name.as_bytes()); |
| 183 | if self.sess().opts.optimize != config::OptLevel::No { |
| 184 | spflags |= DISPFlags::SPFlagOptimized; |
| 185 | } |
| 186 | if let Some((id, _)) = tcx.entry_fn(()) { |
| 187 | if id == def_id { |
| 188 | spflags |= DISPFlags::SPFlagMainSubprogram; |
| 189 | } |
| 282 | 190 | } |
| 283 | | } |
| 284 | 191 | |
| 285 | | /// Annotate move/copy operations with debug info for profiling. |
| 286 | | /// |
| 287 | | /// This creates a temporary debug scope that makes the move/copy appear as an inlined call to |
| 288 | | /// `compiler_move<T, SIZE>()` or `compiler_copy<T, SIZE>()`. The provided closure is executed |
| 289 | | /// with this temporary debug location active. |
| 290 | | /// |
| 291 | | /// The `instance` parameter should be the monomorphized instance of the `compiler_move` or |
| 292 | | /// `compiler_copy` function with the actual type and size. |
| 293 | | fn with_move_annotation<R>( |
| 294 | | &mut self, |
| 295 | | instance: ty::Instance<'tcx>, |
| 296 | | f: impl FnOnce(&mut Self) -> R, |
| 297 | | ) -> R { |
| 298 | | // Save the current debug location |
| 299 | | let saved_loc = self.get_dbg_loc(); |
| 192 | // When we're adding a method to a type DIE, we only want a DW_AT_declaration there, because |
| 193 | // LLVM LTO can't unify type definitions when a child DIE is a full subprogram definition. |
| 194 | // When we use this `decl` below, the subprogram definition gets created at the CU level |
| 195 | // with a DW_AT_specification pointing back to the type's declaration. |
| 196 | let decl = is_method.then(|| unsafe { |
| 197 | llvm::LLVMRustDIBuilderCreateMethod( |
| 198 | DIB(self), |
| 199 | containing_scope, |
| 200 | name.as_c_char_ptr(), |
| 201 | name.len(), |
| 202 | linkage_name.as_c_char_ptr(), |
| 203 | linkage_name.len(), |
| 204 | file_metadata, |
| 205 | loc.line, |
| 206 | function_type_metadata, |
| 207 | flags, |
| 208 | spflags & !DISPFlags::SPFlagDefinition, |
| 209 | template_parameters, |
| 210 | ) |
| 211 | }); |
| 300 | 212 | |
| 301 | | // Create a DIScope for the compiler_move/compiler_copy function |
| 302 | | // We use the function's FnAbi for debug info generation |
| 303 | | let fn_abi = self |
| 304 | | .cx() |
| 305 | | .tcx |
| 306 | | .fn_abi_of_instance( |
| 307 | | self.cx().typing_env().as_query_input((instance, ty::List::empty())), |
| 213 | return unsafe { |
| 214 | llvm::LLVMRustDIBuilderCreateFunction( |
| 215 | DIB(self), |
| 216 | containing_scope, |
| 217 | name.as_c_char_ptr(), |
| 218 | name.len(), |
| 219 | linkage_name.as_c_char_ptr(), |
| 220 | linkage_name.len(), |
| 221 | file_metadata, |
| 222 | loc.line, |
| 223 | function_type_metadata, |
| 224 | scope_line, |
| 225 | flags, |
| 226 | spflags, |
| 227 | maybe_definition_llfn, |
| 228 | template_parameters, |
| 229 | decl, |
| 308 | 230 | ) |
| 309 | | .unwrap(); |
| 310 | | |
| 311 | | let di_scope = self.cx().dbg_scope_fn(instance, fn_abi, None); |
| 312 | | |
| 313 | | // Create an inlined debug location: |
| 314 | | // - scope: the compiler_move/compiler_copy function |
| 315 | | // - inlined_at: the current location (where the move/copy actually occurs) |
| 316 | | // - span: use the function's definition span |
| 317 | | let fn_span = self.cx().tcx.def_span(instance.def_id()); |
| 318 | | let inlined_loc = self.cx().dbg_loc(di_scope, saved_loc, fn_span); |
| 319 | | |
| 320 | | // Set the temporary debug location |
| 321 | | self.set_dbg_loc(inlined_loc); |
| 322 | | |
| 323 | | // Execute the closure (which will generate the memcpy) |
| 324 | | let result = f(self); |
| 325 | | |
| 326 | | // Restore the original debug location |
| 327 | | if let Some(loc) = saved_loc { |
| 328 | | self.set_dbg_loc(loc); |
| 329 | | } else { |
| 330 | | self.clear_dbg_loc(); |
| 331 | | } |
| 332 | | |
| 333 | | result |
| 334 | | } |
| 335 | | } |
| 336 | | |
| 337 | | /// A source code location used to generate debug information. |
| 338 | | // FIXME(eddyb) rename this to better indicate it's a duplicate of |
| 339 | | // `rustc_span::Loc` rather than `DILocation`, perhaps by making |
| 340 | | // `lookup_char_pos` return the right information instead. |
| 341 | | struct DebugLoc { |
| 342 | | /// Information about the original source file. |
| 343 | | file: Arc<SourceFile>, |
| 344 | | /// The (1-based) line number. |
| 345 | | line: u32, |
| 346 | | /// The (1-based) column number. |
| 347 | | col: u32, |
| 348 | | } |
| 349 | | |
| 350 | | impl<'ll> CodegenCx<'ll, '_> { |
| 351 | | /// Looks up debug source information about a `BytePos`. |
| 352 | | // FIXME(eddyb) rename this to better indicate it's a duplicate of |
| 353 | | // `lookup_char_pos` rather than `dbg_loc`, perhaps by making |
| 354 | | // `lookup_char_pos` return the right information instead. |
| 355 | | fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc { |
| 356 | | let (file, line, col) = match self.sess().source_map().lookup_line(pos) { |
| 357 | | Ok(SourceFileAndLine { sf: file, line }) => { |
| 358 | | let line_pos = file.lines()[line]; |
| 359 | | |
| 360 | | // Use 1-based indexing. |
| 361 | | let line = (line + 1) as u32; |
| 362 | | let col = (file.relative_position(pos) - line_pos).to_u32() + 1; |
| 363 | | |
| 364 | | (file, line, col) |
| 365 | | } |
| 366 | | Err(file) => (file, UNKNOWN_LINE_NUMBER, UNKNOWN_COLUMN_NUMBER), |
| 367 | | }; |
| 368 | | |
| 369 | | // For MSVC, omit the column number. |
| 370 | | // Otherwise, emit it. This mimics clang behaviour. |
| 371 | | // See discussion in https://github.com/rust-lang/rust/issues/42921 |
| 372 | | if self.sess().target.is_like_msvc { |
| 373 | | DebugLoc { file, line, col: UNKNOWN_COLUMN_NUMBER } |
| 374 | | } else { |
| 375 | | DebugLoc { file, line, col } |
| 376 | | } |
| 377 | | } |
| 378 | | |
| 379 | | fn create_template_type_parameter( |
| 380 | | &self, |
| 381 | | name: &str, |
| 382 | | actual_type_metadata: &'ll DIType, |
| 383 | | ) -> &'ll DITemplateTypeParameter { |
| 384 | | unsafe { |
| 385 | | llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( |
| 386 | | DIB(self), |
| 387 | | None, |
| 388 | | name.as_c_char_ptr(), |
| 389 | | name.len(), |
| 390 | | actual_type_metadata, |
| 391 | | ) |
| 392 | | } |
| 393 | | } |
| 394 | | } |
| 395 | | |
| 396 | | impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 397 | | fn dbg_create_lexical_block(&self, pos: BytePos, parent_scope: &'ll DIScope) -> &'ll DIScope { |
| 398 | | let loc = self.lookup_debug_loc(pos); |
| 399 | | let file_metadata = file_metadata(self, &loc.file); |
| 400 | | unsafe { |
| 401 | | llvm::LLVMDIBuilderCreateLexicalBlock( |
| 402 | | DIB(self), |
| 403 | | parent_scope, |
| 404 | | file_metadata, |
| 405 | | loc.line, |
| 406 | | loc.col, |
| 407 | | ) |
| 408 | | } |
| 409 | | } |
| 410 | | |
| 411 | | fn dbg_location_clone_with_discriminator( |
| 412 | | &self, |
| 413 | | loc: &'ll DILocation, |
| 414 | | discriminator: u32, |
| 415 | | ) -> Option<&'ll DILocation> { |
| 416 | | unsafe { llvm::LLVMRustDILocationCloneWithBaseDiscriminator(loc, discriminator) } |
| 417 | | } |
| 418 | | |
| 419 | | fn dbg_scope_fn( |
| 420 | | &self, |
| 421 | | instance: Instance<'tcx>, |
| 422 | | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
| 423 | | maybe_definition_llfn: Option<&'ll Value>, |
| 424 | | ) -> &'ll DIScope { |
| 425 | | let tcx = self.tcx; |
| 426 | | |
| 427 | | let def_id = instance.def_id(); |
| 428 | | let (containing_scope, is_method) = get_containing_scope(self, instance); |
| 429 | | let span = tcx.def_span(def_id); |
| 430 | | let loc = self.lookup_debug_loc(span.lo()); |
| 431 | | let file_metadata = file_metadata(self, &loc.file); |
| 432 | | |
| 433 | | let function_type_metadata = |
| 434 | | create_subroutine_type(self, &get_function_signature(self, fn_abi)); |
| 435 | | |
| 436 | | let mut name = String::with_capacity(64); |
| 437 | | type_names::push_item_name(tcx, def_id, false, &mut name); |
| 438 | | |
| 439 | | // Find the enclosing function, in case this is a closure. |
| 440 | | let enclosing_fn_def_id = tcx.typeck_root_def_id(def_id); |
| 441 | | |
| 442 | | // We look up the generics of the enclosing function and truncate the args |
| 443 | | // to their length in order to cut off extra stuff that might be in there for |
| 444 | | // closures or coroutines. |
| 445 | | let generics = tcx.generics_of(enclosing_fn_def_id); |
| 446 | | let args = instance.args.truncate_to(tcx, generics); |
| 447 | | |
| 448 | | type_names::push_generic_args( |
| 449 | | tcx, |
| 450 | | tcx.normalize_erasing_regions(self.typing_env(), Unnormalized::new_wip(args)), |
| 451 | | &mut name, |
| 452 | | ); |
| 453 | | |
| 454 | | let template_parameters = get_template_parameters(self, generics, args); |
| 455 | | |
| 456 | | let linkage_name = &mangled_name_of_instance(self, instance).name; |
| 457 | | // Omit the linkage_name if it is the same as subprogram name. |
| 458 | | let linkage_name = if &name == linkage_name { "" } else { linkage_name }; |
| 459 | | |
| 460 | | // FIXME(eddyb) does this need to be separate from `loc.line` for some reason? |
| 461 | | let scope_line = loc.line; |
| 462 | | |
| 463 | | let mut flags = DIFlags::FlagPrototyped; |
| 464 | | |
| 465 | | if fn_abi.ret.layout.is_uninhabited() { |
| 466 | | flags |= DIFlags::FlagNoReturn; |
| 467 | | } |
| 468 | | |
| 469 | | let mut spflags = DISPFlags::SPFlagDefinition; |
| 470 | | if is_node_local_to_unit(self, def_id) { |
| 471 | | spflags |= DISPFlags::SPFlagLocalToUnit; |
| 472 | | } |
| 473 | | if self.sess().opts.optimize != config::OptLevel::No { |
| 474 | | spflags |= DISPFlags::SPFlagOptimized; |
| 475 | | } |
| 476 | | if let Some((id, _)) = tcx.entry_fn(()) { |
| 477 | | if id == def_id { |
| 478 | | spflags |= DISPFlags::SPFlagMainSubprogram; |
| 479 | | } |
| 480 | | } |
| 481 | | |
| 482 | | // When we're adding a method to a type DIE, we only want a DW_AT_declaration there, because |
| 483 | | // LLVM LTO can't unify type definitions when a child DIE is a full subprogram definition. |
| 484 | | // When we use this `decl` below, the subprogram definition gets created at the CU level |
| 485 | | // with a DW_AT_specification pointing back to the type's declaration. |
| 486 | | let decl = is_method.then(|| unsafe { |
| 487 | | llvm::LLVMRustDIBuilderCreateMethod( |
| 488 | | DIB(self), |
| 489 | | containing_scope, |
| 490 | | name.as_c_char_ptr(), |
| 491 | | name.len(), |
| 492 | | linkage_name.as_c_char_ptr(), |
| 493 | | linkage_name.len(), |
| 494 | | file_metadata, |
| 495 | | loc.line, |
| 496 | | function_type_metadata, |
| 497 | | flags, |
| 498 | | spflags & !DISPFlags::SPFlagDefinition, |
| 499 | | template_parameters, |
| 500 | | ) |
| 501 | | }); |
| 502 | | |
| 503 | | return unsafe { |
| 504 | | llvm::LLVMRustDIBuilderCreateFunction( |
| 505 | | DIB(self), |
| 506 | | containing_scope, |
| 507 | | name.as_c_char_ptr(), |
| 508 | | name.len(), |
| 509 | | linkage_name.as_c_char_ptr(), |
| 510 | | linkage_name.len(), |
| 511 | | file_metadata, |
| 512 | | loc.line, |
| 513 | | function_type_metadata, |
| 514 | | scope_line, |
| 515 | | flags, |
| 516 | | spflags, |
| 517 | | maybe_definition_llfn, |
| 518 | | template_parameters, |
| 519 | | decl, |
| 520 | | ) |
| 521 | | }; |
| 231 | }; |
| 522 | 232 | |
| 523 | 233 | fn get_function_signature<'ll, 'tcx>( |
| 524 | 234 | cx: &CodegenCx<'ll, 'tcx>, |
| ... | ... | @@ -658,8 +368,34 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 658 | 368 | } |
| 659 | 369 | } |
| 660 | 370 | |
| 371 | fn dbg_create_lexical_block( |
| 372 | &mut self, |
| 373 | pos: BytePos, |
| 374 | parent_scope: &'ll DIScope, |
| 375 | ) -> &'ll DIScope { |
| 376 | let loc = self.lookup_debug_loc(pos); |
| 377 | let file_metadata = file_metadata(self, &loc.file); |
| 378 | unsafe { |
| 379 | llvm::LLVMDIBuilderCreateLexicalBlock( |
| 380 | DIB(self), |
| 381 | parent_scope, |
| 382 | file_metadata, |
| 383 | loc.line, |
| 384 | loc.col, |
| 385 | ) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | fn dbg_location_clone_with_discriminator( |
| 390 | &mut self, |
| 391 | loc: &'ll DILocation, |
| 392 | discriminator: u32, |
| 393 | ) -> Option<&'ll DILocation> { |
| 394 | unsafe { llvm::LLVMRustDILocationCloneWithBaseDiscriminator(loc, discriminator) } |
| 395 | } |
| 396 | |
| 661 | 397 | fn dbg_loc( |
| 662 | | &self, |
| 398 | &mut self, |
| 663 | 399 | scope: &'ll DIScope, |
| 664 | 400 | inlined_at: Option<&'ll DILocation>, |
| 665 | 401 | span: Span, |
| ... | ... | @@ -679,31 +415,18 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 679 | 415 | unsafe { llvm::LLVMDIBuilderCreateDebugLocation(self.llcx, line, col, scope, inlined_at) } |
| 680 | 416 | } |
| 681 | 417 | |
| 682 | | fn create_vtable_debuginfo( |
| 683 | | &self, |
| 684 | | ty: Ty<'tcx>, |
| 685 | | trait_ref: Option<ty::ExistentialTraitRef<'tcx>>, |
| 686 | | vtable: Self::Value, |
| 687 | | ) { |
| 688 | | metadata::create_vtable_di_node(self, ty, trait_ref, vtable) |
| 689 | | } |
| 690 | | |
| 691 | 418 | fn extend_scope_to_file( |
| 692 | | &self, |
| 419 | &mut self, |
| 693 | 420 | scope_metadata: &'ll DIScope, |
| 694 | 421 | file: &rustc_span::SourceFile, |
| 695 | 422 | ) -> &'ll DILexicalBlock { |
| 696 | 423 | metadata::extend_scope_to_file(self, scope_metadata, file) |
| 697 | 424 | } |
| 698 | 425 | |
| 699 | | fn debuginfo_finalize(&self) { |
| 700 | | finalize(self) |
| 701 | | } |
| 702 | | |
| 703 | 426 | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
| 704 | 427 | // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.). |
| 705 | 428 | fn create_dbg_var( |
| 706 | | &self, |
| 429 | &mut self, |
| 707 | 430 | variable_name: Symbol, |
| 708 | 431 | variable_type: Ty<'tcx>, |
| 709 | 432 | scope_metadata: &'ll DIScope, |
| ... | ... | @@ -750,4 +473,280 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 750 | 473 | }, |
| 751 | 474 | } |
| 752 | 475 | } |
| 476 | |
| 477 | // FIXME(eddyb) find a common convention for all of the debuginfo-related |
| 478 | // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.). |
| 479 | fn dbg_var_addr( |
| 480 | &mut self, |
| 481 | dbg_var: &'ll DIVariable, |
| 482 | dbg_loc: &'ll DILocation, |
| 483 | variable_alloca: Self::Value, |
| 484 | direct_offset: Size, |
| 485 | indirect_offsets: &[Size], |
| 486 | fragment: &Option<Range<Size>>, |
| 487 | ) { |
| 488 | use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst}; |
| 489 | |
| 490 | // Convert the direct and indirect offsets and fragment byte range to address ops. |
| 491 | let mut addr_ops = SmallVec::<[u64; 8]>::new(); |
| 492 | |
| 493 | if direct_offset.bytes() > 0 { |
| 494 | addr_ops.push(DW_OP_plus_uconst); |
| 495 | addr_ops.push(direct_offset.bytes()); |
| 496 | } |
| 497 | for &offset in indirect_offsets { |
| 498 | addr_ops.push(DW_OP_deref); |
| 499 | if offset.bytes() > 0 { |
| 500 | addr_ops.push(DW_OP_plus_uconst); |
| 501 | addr_ops.push(offset.bytes()); |
| 502 | } |
| 503 | } |
| 504 | if let Some(fragment) = fragment { |
| 505 | // `DW_OP_LLVM_fragment` takes as arguments the fragment's |
| 506 | // offset and size, both of them in bits. |
| 507 | addr_ops.push(DW_OP_LLVM_fragment); |
| 508 | addr_ops.push(fragment.start.bits()); |
| 509 | addr_ops.push((fragment.end - fragment.start).bits()); |
| 510 | } |
| 511 | |
| 512 | let di_builder = DIB(self.cx()); |
| 513 | let addr_expr = di_builder.create_expression(&addr_ops); |
| 514 | unsafe { |
| 515 | llvm::LLVMDIBuilderInsertDeclareRecordAtEnd( |
| 516 | di_builder, |
| 517 | variable_alloca, |
| 518 | dbg_var, |
| 519 | addr_expr, |
| 520 | dbg_loc, |
| 521 | self.llbb(), |
| 522 | ) |
| 523 | }; |
| 524 | } |
| 525 | |
| 526 | fn dbg_var_value( |
| 527 | &mut self, |
| 528 | dbg_var: &'ll DIVariable, |
| 529 | dbg_loc: &'ll DILocation, |
| 530 | value: Self::Value, |
| 531 | direct_offset: Size, |
| 532 | indirect_offsets: &[Size], |
| 533 | fragment: &Option<Range<Size>>, |
| 534 | ) { |
| 535 | use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst, DW_OP_stack_value}; |
| 536 | |
| 537 | // Convert the direct and indirect offsets and fragment byte range to address ops. |
| 538 | let mut addr_ops = SmallVec::<[u64; 8]>::new(); |
| 539 | |
| 540 | if direct_offset.bytes() > 0 { |
| 541 | addr_ops.push(DW_OP_plus_uconst); |
| 542 | addr_ops.push(direct_offset.bytes() as u64); |
| 543 | addr_ops.push(DW_OP_stack_value); |
| 544 | } |
| 545 | for &offset in indirect_offsets { |
| 546 | addr_ops.push(DW_OP_deref); |
| 547 | if offset.bytes() > 0 { |
| 548 | addr_ops.push(DW_OP_plus_uconst); |
| 549 | addr_ops.push(offset.bytes() as u64); |
| 550 | } |
| 551 | } |
| 552 | if let Some(fragment) = fragment { |
| 553 | // `DW_OP_LLVM_fragment` takes as arguments the fragment's |
| 554 | // offset and size, both of them in bits. |
| 555 | addr_ops.push(DW_OP_LLVM_fragment); |
| 556 | addr_ops.push(fragment.start.bits() as u64); |
| 557 | addr_ops.push((fragment.end - fragment.start).bits() as u64); |
| 558 | } |
| 559 | |
| 560 | let di_builder = DIB(self.cx()); |
| 561 | let addr_expr = unsafe { |
| 562 | llvm::LLVMDIBuilderCreateExpression(di_builder, addr_ops.as_ptr(), addr_ops.len()) |
| 563 | }; |
| 564 | unsafe { |
| 565 | llvm::LLVMDIBuilderInsertDbgValueRecordAtEnd( |
| 566 | di_builder, |
| 567 | value, |
| 568 | dbg_var, |
| 569 | addr_expr, |
| 570 | dbg_loc, |
| 571 | self.llbb(), |
| 572 | ); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) { |
| 577 | unsafe { |
| 578 | llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, dbg_loc); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | fn clear_dbg_loc(&mut self) { |
| 583 | unsafe { |
| 584 | llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, ptr::null()); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) { |
| 589 | gdb::insert_reference_to_gdb_debug_scripts_section_global(self) |
| 590 | } |
| 591 | |
| 592 | fn set_var_name(&mut self, value: &'ll Value, name: &str) { |
| 593 | // Avoid wasting time if LLVM value names aren't even enabled. |
| 594 | if self.sess().fewer_names() { |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | // Only function parameters and instructions are local to a function, |
| 599 | // don't change the name of anything else (e.g. globals). |
| 600 | let param_or_inst = unsafe { |
| 601 | llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some() |
| 602 | }; |
| 603 | if !param_or_inst { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Avoid replacing the name if it already exists. |
| 608 | // While we could combine the names somehow, it'd |
| 609 | // get noisy quick, and the usefulness is dubious. |
| 610 | if llvm::get_value_name(value).is_empty() { |
| 611 | llvm::set_value_name(value, name.as_bytes()); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /// Annotate move/copy operations with debug info for profiling. |
| 616 | /// |
| 617 | /// This creates a temporary debug scope that makes the move/copy appear as an inlined call to |
| 618 | /// `compiler_move<T, SIZE>()` or `compiler_copy<T, SIZE>()`. The provided closure is executed |
| 619 | /// with this temporary debug location active. |
| 620 | /// |
| 621 | /// The `instance` parameter should be the monomorphized instance of the `compiler_move` or |
| 622 | /// `compiler_copy` function with the actual type and size. |
| 623 | fn with_move_annotation<R>( |
| 624 | &mut self, |
| 625 | instance: ty::Instance<'tcx>, |
| 626 | f: impl FnOnce(&mut Self) -> R, |
| 627 | ) -> R { |
| 628 | // Save the current debug location |
| 629 | let saved_loc = self.get_dbg_loc(); |
| 630 | |
| 631 | // Create a DIScope for the compiler_move/compiler_copy function |
| 632 | // We use the function's FnAbi for debug info generation |
| 633 | let fn_abi = self |
| 634 | .cx() |
| 635 | .tcx |
| 636 | .fn_abi_of_instance( |
| 637 | self.cx().typing_env().as_query_input((instance, ty::List::empty())), |
| 638 | ) |
| 639 | .unwrap(); |
| 640 | |
| 641 | let di_scope = self.dbg_scope_fn(instance, fn_abi, None); |
| 642 | |
| 643 | // Create an inlined debug location: |
| 644 | // - scope: the compiler_move/compiler_copy function |
| 645 | // - inlined_at: the current location (where the move/copy actually occurs) |
| 646 | // - span: use the function's definition span |
| 647 | let fn_span = self.cx().tcx.def_span(instance.def_id()); |
| 648 | let inlined_loc = self.dbg_loc(di_scope, saved_loc, fn_span); |
| 649 | |
| 650 | // Set the temporary debug location |
| 651 | self.set_dbg_loc(inlined_loc); |
| 652 | |
| 653 | // Execute the closure (which will generate the memcpy) |
| 654 | let result = f(self); |
| 655 | |
| 656 | // Restore the original debug location |
| 657 | if let Some(loc) = saved_loc { |
| 658 | self.set_dbg_loc(loc); |
| 659 | } else { |
| 660 | self.clear_dbg_loc(); |
| 661 | } |
| 662 | |
| 663 | result |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /// A source code location used to generate debug information. |
| 668 | // FIXME(eddyb) rename this to better indicate it's a duplicate of |
| 669 | // `rustc_span::Loc` rather than `DILocation`, perhaps by making |
| 670 | // `lookup_char_pos` return the right information instead. |
| 671 | struct DebugLoc { |
| 672 | /// Information about the original source file. |
| 673 | file: Arc<SourceFile>, |
| 674 | /// The (1-based) line number. |
| 675 | line: u32, |
| 676 | /// The (1-based) column number. |
| 677 | col: u32, |
| 678 | } |
| 679 | |
| 680 | impl<'ll> CodegenCx<'ll, '_> { |
| 681 | /// Looks up debug source information about a `BytePos`. |
| 682 | // FIXME(eddyb) rename this to better indicate it's a duplicate of |
| 683 | // `lookup_char_pos` rather than `dbg_loc`, perhaps by making |
| 684 | // `lookup_char_pos` return the right information instead. |
| 685 | fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc { |
| 686 | let (file, line, col) = match self.sess().source_map().lookup_line(pos) { |
| 687 | Ok(SourceFileAndLine { sf: file, line }) => { |
| 688 | let line_pos = file.lines()[line]; |
| 689 | |
| 690 | // Use 1-based indexing. |
| 691 | let line = (line + 1) as u32; |
| 692 | let col = (file.relative_position(pos) - line_pos).to_u32() + 1; |
| 693 | |
| 694 | (file, line, col) |
| 695 | } |
| 696 | Err(file) => (file, UNKNOWN_LINE_NUMBER, UNKNOWN_COLUMN_NUMBER), |
| 697 | }; |
| 698 | |
| 699 | // For MSVC, omit the column number. |
| 700 | // Otherwise, emit it. This mimics clang behaviour. |
| 701 | // See discussion in https://github.com/rust-lang/rust/issues/42921 |
| 702 | if self.sess().target.is_like_msvc { |
| 703 | DebugLoc { file, line, col: UNKNOWN_COLUMN_NUMBER } |
| 704 | } else { |
| 705 | DebugLoc { file, line, col } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | fn create_template_type_parameter( |
| 710 | &self, |
| 711 | name: &str, |
| 712 | actual_type_metadata: &'ll DIType, |
| 713 | ) -> &'ll DITemplateTypeParameter { |
| 714 | unsafe { |
| 715 | llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( |
| 716 | DIB(self), |
| 717 | None, |
| 718 | name.as_c_char_ptr(), |
| 719 | name.len(), |
| 720 | actual_type_metadata, |
| 721 | ) |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /// Creates any deferred debug metadata nodes |
| 726 | pub(crate) fn debuginfo_finalize(&self) { |
| 727 | if let Some(dbg_cx) = &self.dbg_cx { |
| 728 | debug!("finalize"); |
| 729 | |
| 730 | if gdb::needs_gdb_debug_scripts_section(self) { |
| 731 | // Add a .debug_gdb_scripts section to this compile-unit. This will |
| 732 | // cause GDB to try and load the gdb_load_rust_pretty_printers.py file, |
| 733 | // which activates the Rust pretty printers for binary this section is |
| 734 | // contained in. |
| 735 | gdb::get_or_insert_gdb_debug_scripts_section_global(self); |
| 736 | } |
| 737 | |
| 738 | dbg_cx.finalize(); |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
| 744 | fn create_vtable_debuginfo( |
| 745 | &self, |
| 746 | ty: Ty<'tcx>, |
| 747 | trait_ref: Option<ty::ExistentialTraitRef<'tcx>>, |
| 748 | vtable: Self::Value, |
| 749 | ) { |
| 750 | metadata::create_vtable_di_node(self, ty, trait_ref, vtable) |
| 751 | } |
| 753 | 752 | } |