rustc_codegen_llvm/debuginfo/
mod.rs1#![doc = include_str!("doc.md")]
2
3use std::cell::{OnceCell, RefCell};
4use std::ops::Range;
5use std::sync::Arc;
6use std::{iter, ptr};
7
8use libc::c_uint;
9use metadata::create_subroutine_type;
10use rustc_abi::Size;
11use rustc_codegen_ssa::debuginfo::type_names;
12use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
13use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
14use rustc_codegen_ssa::traits::*;
15use rustc_data_structures::unord::UnordMap;
16use rustc_hir::def_id::{DefId, DefIdMap};
17use rustc_index::IndexVec;
18use rustc_middle::mir;
19use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
20use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TypeVisitableExt};
21use rustc_session::Session;
22use rustc_session::config::{self, DebugInfo};
23use rustc_span::{
24 BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, Symbol,
25};
26use rustc_target::callconv::FnAbi;
27use rustc_target::spec::DebuginfoKind;
28use smallvec::SmallVec;
29use tracing::debug;
30
31use self::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, type_di_node};
32use self::namespace::mangled_name_of_instance;
33use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
34use crate::builder::Builder;
35use crate::common::{AsCCharPtr, CodegenCx};
36use crate::llvm;
37use crate::llvm::debuginfo::{
38 DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
39 DITemplateTypeParameter, DIType, DIVariable,
40};
41use crate::value::Value;
42
43mod create_scope_map;
44mod dwarf_const;
45mod gdb;
46pub(crate) mod metadata;
47mod namespace;
48mod utils;
49
50use self::create_scope_map::compute_mir_scopes;
51pub(crate) use self::metadata::build_global_var_di_node;
52
53#[allow(non_upper_case_globals)]
58const DW_TAG_auto_variable: c_uint = 0x100;
59#[allow(non_upper_case_globals)]
60const DW_TAG_arg_variable: c_uint = 0x101;
61
62pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
64 llmod: &'ll llvm::Module,
65 builder: DIBuilderBox<'ll>,
66 created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>,
67
68 type_map: metadata::TypeMap<'ll, 'tcx>,
69 adt_stack: RefCell<Vec<(DefId, GenericArgsRef<'tcx>)>>,
70 namespace_map: RefCell<DefIdMap<&'ll DIScope>>,
71 recursion_marker_type: OnceCell<&'ll DIType>,
72}
73
74impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
75 pub(crate) fn new(llmod: &'ll llvm::Module) -> Self {
76 debug!("CodegenUnitDebugContext::new");
77 let builder = DIBuilderBox::new(llmod);
78 CodegenUnitDebugContext {
80 llmod,
81 builder,
82 created_files: Default::default(),
83 type_map: Default::default(),
84 adt_stack: Default::default(),
85 namespace_map: RefCell::new(Default::default()),
86 recursion_marker_type: OnceCell::new(),
87 }
88 }
89
90 pub(crate) fn finalize(&self, sess: &Session) {
91 unsafe { llvm::LLVMDIBuilderFinalize(self.builder.as_ref()) };
92
93 match sess.target.debuginfo_kind {
94 DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => {
95 llvm::add_module_flag_u32(
102 self.llmod,
103 llvm::ModuleFlagMergeBehavior::Max,
108 "Dwarf Version",
109 sess.dwarf_version(),
110 );
111 }
112 DebuginfoKind::Pdb => {
113 llvm::add_module_flag_u32(
115 self.llmod,
116 llvm::ModuleFlagMergeBehavior::Warning,
117 "CodeView",
118 1,
119 );
120 }
121 }
122
123 llvm::add_module_flag_u32(
125 self.llmod,
126 llvm::ModuleFlagMergeBehavior::Warning,
127 "Debug Info Version",
128 unsafe { llvm::LLVMRustDebugMetadataVersion() },
129 );
130 }
131}
132
133pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
135 if let Some(dbg_cx) = &cx.dbg_cx {
136 debug!("finalize");
137
138 if gdb::needs_gdb_debug_scripts_section(cx) {
139 gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
144 }
145
146 dbg_cx.finalize(cx.sess());
147 }
148}
149
150impl<'ll> Builder<'_, 'll, '_> {
151 pub(crate) fn get_dbg_loc(&self) -> Option<&'ll DILocation> {
152 unsafe { llvm::LLVMGetCurrentDebugLocation2(self.llbuilder) }
153 }
154}
155
156impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
157 fn dbg_var_addr(
160 &mut self,
161 dbg_var: &'ll DIVariable,
162 dbg_loc: &'ll DILocation,
163 variable_alloca: Self::Value,
164 direct_offset: Size,
165 indirect_offsets: &[Size],
166 fragment: Option<Range<Size>>,
167 ) {
168 use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst};
169
170 let mut addr_ops = SmallVec::<[u64; 8]>::new();
172
173 if direct_offset.bytes() > 0 {
174 addr_ops.push(DW_OP_plus_uconst);
175 addr_ops.push(direct_offset.bytes() as u64);
176 }
177 for &offset in indirect_offsets {
178 addr_ops.push(DW_OP_deref);
179 if offset.bytes() > 0 {
180 addr_ops.push(DW_OP_plus_uconst);
181 addr_ops.push(offset.bytes() as u64);
182 }
183 }
184 if let Some(fragment) = fragment {
185 addr_ops.push(DW_OP_LLVM_fragment);
188 addr_ops.push(fragment.start.bits() as u64);
189 addr_ops.push((fragment.end - fragment.start).bits() as u64);
190 }
191
192 unsafe {
193 llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
195 DIB(self.cx()),
196 variable_alloca,
197 dbg_var,
198 addr_ops.as_ptr(),
199 addr_ops.len() as c_uint,
200 dbg_loc,
201 self.llbb(),
202 );
203 }
204 }
205
206 fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) {
207 unsafe {
208 llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, dbg_loc);
209 }
210 }
211
212 fn clear_dbg_loc(&mut self) {
213 unsafe {
214 llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, ptr::null());
215 }
216 }
217
218 fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
219 gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
220 }
221
222 fn set_var_name(&mut self, value: &'ll Value, name: &str) {
223 if self.sess().fewer_names() {
225 return;
226 }
227
228 let param_or_inst = unsafe {
231 llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some()
232 };
233 if !param_or_inst {
234 return;
235 }
236
237 if llvm::get_value_name(value).is_empty() {
241 llvm::set_value_name(value, name.as_bytes());
242 }
243 }
244}
245
246struct DebugLoc {
251 file: Arc<SourceFile>,
253 line: u32,
255 col: u32,
257}
258
259impl<'ll> CodegenCx<'ll, '_> {
260 fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
265 let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
266 Ok(SourceFileAndLine { sf: file, line }) => {
267 let line_pos = file.lines()[line];
268
269 let line = (line + 1) as u32;
271 let col = (file.relative_position(pos) - line_pos).to_u32() + 1;
272
273 (file, line, col)
274 }
275 Err(file) => (file, UNKNOWN_LINE_NUMBER, UNKNOWN_COLUMN_NUMBER),
276 };
277
278 if self.sess().target.is_like_msvc {
282 DebugLoc { file, line, col: UNKNOWN_COLUMN_NUMBER }
283 } else {
284 DebugLoc { file, line, col }
285 }
286 }
287
288 fn create_template_type_parameter(
289 &self,
290 name: &str,
291 actual_type_metadata: &'ll DIType,
292 ) -> &'ll DITemplateTypeParameter {
293 unsafe {
294 llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
295 DIB(self),
296 None,
297 name.as_c_char_ptr(),
298 name.len(),
299 actual_type_metadata,
300 )
301 }
302 }
303}
304
305impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
306 fn create_function_debug_context(
307 &self,
308 instance: Instance<'tcx>,
309 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
310 llfn: &'ll Value,
311 mir: &mir::Body<'tcx>,
312 ) -> Option<FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>> {
313 if self.sess().opts.debuginfo == DebugInfo::None {
314 return None;
315 }
316
317 let empty_scope = DebugScope {
319 dbg_scope: self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
320 inlined_at: None,
321 file_start_pos: BytePos(0),
322 file_end_pos: BytePos(0),
323 };
324 let mut fn_debug_context = FunctionDebugContext {
325 scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes),
326 inlined_function_scopes: Default::default(),
327 };
328
329 compute_mir_scopes(self, instance, mir, &mut fn_debug_context);
331
332 Some(fn_debug_context)
333 }
334
335 fn dbg_scope_fn(
336 &self,
337 instance: Instance<'tcx>,
338 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
339 maybe_definition_llfn: Option<&'ll Value>,
340 ) -> &'ll DIScope {
341 let tcx = self.tcx;
342
343 let def_id = instance.def_id();
344 let (containing_scope, is_method) = get_containing_scope(self, instance);
345 let span = tcx.def_span(def_id);
346 let loc = self.lookup_debug_loc(span.lo());
347 let file_metadata = file_metadata(self, &loc.file);
348
349 let function_type_metadata =
350 create_subroutine_type(self, get_function_signature(self, fn_abi));
351
352 let mut name = String::with_capacity(64);
353 type_names::push_item_name(tcx, def_id, false, &mut name);
354
355 let enclosing_fn_def_id = tcx.typeck_root_def_id(def_id);
357
358 let generics = tcx.generics_of(enclosing_fn_def_id);
362 let args = instance.args.truncate_to(tcx, generics);
363
364 type_names::push_generic_params(
365 tcx,
366 tcx.normalize_erasing_regions(self.typing_env(), args),
367 &mut name,
368 );
369
370 let template_parameters = get_template_parameters(self, generics, args);
371
372 let linkage_name = &mangled_name_of_instance(self, instance).name;
373 let linkage_name = if &name == linkage_name { "" } else { linkage_name };
375
376 let scope_line = loc.line;
378
379 let mut flags = DIFlags::FlagPrototyped;
380
381 if fn_abi.ret.layout.is_uninhabited() {
382 flags |= DIFlags::FlagNoReturn;
383 }
384
385 let mut spflags = DISPFlags::SPFlagDefinition;
386 if is_node_local_to_unit(self, def_id) {
387 spflags |= DISPFlags::SPFlagLocalToUnit;
388 }
389 if self.sess().opts.optimize != config::OptLevel::No {
390 spflags |= DISPFlags::SPFlagOptimized;
391 }
392 if let Some((id, _)) = tcx.entry_fn(()) {
393 if id == def_id {
394 spflags |= DISPFlags::SPFlagMainSubprogram;
395 }
396 }
397
398 let decl = is_method.then(|| unsafe {
403 llvm::LLVMRustDIBuilderCreateMethod(
404 DIB(self),
405 containing_scope,
406 name.as_c_char_ptr(),
407 name.len(),
408 linkage_name.as_c_char_ptr(),
409 linkage_name.len(),
410 file_metadata,
411 loc.line,
412 function_type_metadata,
413 flags,
414 spflags & !DISPFlags::SPFlagDefinition,
415 template_parameters,
416 )
417 });
418
419 return unsafe {
420 llvm::LLVMRustDIBuilderCreateFunction(
421 DIB(self),
422 containing_scope,
423 name.as_c_char_ptr(),
424 name.len(),
425 linkage_name.as_c_char_ptr(),
426 linkage_name.len(),
427 file_metadata,
428 loc.line,
429 function_type_metadata,
430 scope_line,
431 flags,
432 spflags,
433 maybe_definition_llfn,
434 template_parameters,
435 decl,
436 )
437 };
438
439 fn get_function_signature<'ll, 'tcx>(
440 cx: &CodegenCx<'ll, 'tcx>,
441 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
442 ) -> &'ll DIArray {
443 if cx.sess().opts.debuginfo != DebugInfo::Full {
444 return create_DIArray(DIB(cx), &[]);
445 }
446
447 let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
448
449 signature.push(if fn_abi.ret.is_ignore() {
451 None
452 } else {
453 Some(type_di_node(cx, fn_abi.ret.layout.ty))
454 });
455
456 if cx.sess().target.is_like_msvc {
458 signature.extend(fn_abi.args.iter().map(|arg| {
469 let t = arg.layout.ty;
470 let t = match t.kind() {
471 ty::Array(ct, _)
472 if (*ct == cx.tcx.types.u8) || cx.layout_of(*ct).is_zst() =>
473 {
474 Ty::new_imm_ptr(cx.tcx, *ct)
475 }
476 _ => t,
477 };
478 Some(type_di_node(cx, t))
479 }));
480 } else {
481 signature
482 .extend(fn_abi.args.iter().map(|arg| Some(type_di_node(cx, arg.layout.ty))));
483 }
484
485 create_DIArray(DIB(cx), &signature[..])
486 }
487
488 fn get_template_parameters<'ll, 'tcx>(
489 cx: &CodegenCx<'ll, 'tcx>,
490 generics: &ty::Generics,
491 args: GenericArgsRef<'tcx>,
492 ) -> &'ll DIArray {
493 if args.types().next().is_none() {
494 return create_DIArray(DIB(cx), &[]);
495 }
496
497 let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full {
499 let names = get_parameter_names(cx, generics);
500 iter::zip(args, names)
501 .filter_map(|(kind, name)| {
502 kind.as_type().map(|ty| {
503 let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
504 let actual_type_metadata = type_di_node(cx, actual_type);
505 Some(cx.create_template_type_parameter(
506 name.as_str(),
507 actual_type_metadata,
508 ))
509 })
510 })
511 .collect()
512 } else {
513 vec![]
514 };
515
516 create_DIArray(DIB(cx), &template_params)
517 }
518
519 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
520 let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
521 get_parameter_names(cx, cx.tcx.generics_of(def_id))
522 });
523 names.extend(generics.own_params.iter().map(|param| param.name));
524 names
525 }
526
527 fn get_containing_scope<'ll, 'tcx>(
530 cx: &CodegenCx<'ll, 'tcx>,
531 instance: Instance<'tcx>,
532 ) -> (&'ll DIScope, bool) {
533 if let Some(impl_def_id) = cx.tcx.impl_of_method(instance.def_id()) {
537 if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
539 let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
540 instance.args,
541 cx.typing_env(),
542 cx.tcx.type_of(impl_def_id),
543 );
544
545 if let ty::Adt(def, ..) = impl_self_ty.kind()
548 && !def.is_box()
549 {
550 if cx.sess().opts.debuginfo == DebugInfo::Full && !impl_self_ty.has_param()
552 {
553 return (type_di_node(cx, impl_self_ty), true);
554 } else {
555 return (namespace::item_namespace(cx, def.did()), false);
556 }
557 }
558 } else {
559 }
562 }
563
564 let scope = namespace::item_namespace(
565 cx,
566 DefId {
567 krate: instance.def_id().krate,
568 index: cx
569 .tcx
570 .def_key(instance.def_id())
571 .parent
572 .expect("get_containing_scope: missing parent?"),
573 },
574 );
575 (scope, false)
576 }
577 }
578
579 fn dbg_loc(
580 &self,
581 scope: &'ll DIScope,
582 inlined_at: Option<&'ll DILocation>,
583 span: Span,
584 ) -> &'ll DILocation {
585 let (line, col) = if span.is_dummy() && !self.sess().target.is_like_msvc {
591 (0, 0)
592 } else {
593 let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
594 (line, col)
595 };
596
597 unsafe { llvm::LLVMDIBuilderCreateDebugLocation(self.llcx, line, col, scope, inlined_at) }
598 }
599
600 fn create_vtable_debuginfo(
601 &self,
602 ty: Ty<'tcx>,
603 trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
604 vtable: Self::Value,
605 ) {
606 metadata::create_vtable_di_node(self, ty, trait_ref, vtable)
607 }
608
609 fn extend_scope_to_file(
610 &self,
611 scope_metadata: &'ll DIScope,
612 file: &rustc_span::SourceFile,
613 ) -> &'ll DILexicalBlock {
614 metadata::extend_scope_to_file(self, scope_metadata, file)
615 }
616
617 fn debuginfo_finalize(&self) {
618 finalize(self)
619 }
620
621 fn create_dbg_var(
624 &self,
625 variable_name: Symbol,
626 variable_type: Ty<'tcx>,
627 scope_metadata: &'ll DIScope,
628 variable_kind: VariableKind,
629 span: Span,
630 ) -> &'ll DIVariable {
631 let loc = self.lookup_debug_loc(span.lo());
632 let file_metadata = file_metadata(self, &loc.file);
633
634 let type_metadata = type_di_node(self, variable_type);
635
636 let (argument_index, dwarf_tag) = match variable_kind {
637 ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
638 LocalVariable => (0, DW_TAG_auto_variable),
639 };
640 let align = self.align_of(variable_type);
641
642 let name = variable_name.as_str();
643 unsafe {
644 llvm::LLVMRustDIBuilderCreateVariable(
645 DIB(self),
646 dwarf_tag,
647 scope_metadata,
648 name.as_c_char_ptr(),
649 name.len(),
650 file_metadata,
651 loc.line,
652 type_metadata,
653 true,
654 DIFlags::FlagZero,
655 argument_index,
656 align.bits() as u32,
657 )
658 }
659 }
660}