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