]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / debuginfo / metadata.rs
1 use self::type_map::DINodeCreationResult;
2 use self::type_map::Stub;
3 use self::type_map::UniqueTypeId;
4
5 use super::namespace::mangled_name_of_instance;
6 use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
7 use super::utils::{
8 create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, DIB,
9 };
10 use super::CodegenUnitDebugContext;
11
12 use crate::abi;
13 use crate::common::CodegenCx;
14 use crate::debuginfo::metadata::type_map::build_type_with_children;
15 use crate::debuginfo::utils::fat_pointer_kind;
16 use crate::debuginfo::utils::FatPtrKind;
17 use crate::llvm;
18 use crate::llvm::debuginfo::{
19 DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DebugEmissionKind,
20 };
21 use crate::value::Value;
22
23 use cstr::cstr;
24 use rustc_codegen_ssa::debuginfo::type_names::cpp_like_debuginfo;
25 use rustc_codegen_ssa::debuginfo::type_names::VTableNameKind;
26 use rustc_codegen_ssa::traits::*;
27 use rustc_fs_util::path_to_c_string;
28 use rustc_hir::def::CtorKind;
29 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
30 use rustc_index::vec::{Idx, IndexVec};
31 use rustc_middle::bug;
32 use rustc_middle::mir::{self, GeneratorLayout};
33 use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
34 use rustc_middle::ty::subst::GenericArgKind;
35 use rustc_middle::ty::{
36 self, AdtKind, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt, Visibility,
37 };
38 use rustc_session::config::{self, DebugInfo, Lto};
39 use rustc_span::symbol::Symbol;
40 use rustc_span::FileName;
41 use rustc_span::{self, FileNameDisplayPreference, SourceFile};
42 use rustc_symbol_mangling::typeid_for_trait_ref;
43 use rustc_target::abi::{Align, Size};
44 use smallvec::smallvec;
45 use tracing::debug;
46
47 use libc::{c_char, c_longlong, c_uint};
48 use std::borrow::Cow;
49 use std::fmt::{self, Write};
50 use std::hash::{Hash, Hasher};
51 use std::iter;
52 use std::path::{Path, PathBuf};
53 use std::ptr;
54 use tracing::instrument;
55
56 impl PartialEq for llvm::Metadata {
57 fn eq(&self, other: &Self) -> bool {
58 ptr::eq(self, other)
59 }
60 }
61
62 impl Eq for llvm::Metadata {}
63
64 impl Hash for llvm::Metadata {
65 fn hash<H: Hasher>(&self, hasher: &mut H) {
66 (self as *const Self).hash(hasher);
67 }
68 }
69
70 impl fmt::Debug for llvm::Metadata {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 (self as *const Self).fmt(f)
73 }
74 }
75
76 // From DWARF 5.
77 // See http://www.dwarfstd.org/ShowIssue.php?issue=140129.1.
78 const DW_LANG_RUST: c_uint = 0x1c;
79 #[allow(non_upper_case_globals)]
80 const DW_ATE_boolean: c_uint = 0x02;
81 #[allow(non_upper_case_globals)]
82 const DW_ATE_float: c_uint = 0x04;
83 #[allow(non_upper_case_globals)]
84 const DW_ATE_signed: c_uint = 0x05;
85 #[allow(non_upper_case_globals)]
86 const DW_ATE_unsigned: c_uint = 0x07;
87 #[allow(non_upper_case_globals)]
88 const DW_ATE_UTF: c_uint = 0x10;
89
90 pub(super) const UNKNOWN_LINE_NUMBER: c_uint = 0;
91 pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
92
93 const NO_SCOPE_METADATA: Option<&DIScope> = None;
94 /// A function that returns an empty list of generic parameter debuginfo nodes.
95 const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<&'ll DIType> = |_| SmallVec::new();
96
97 // SmallVec is used quite a bit in this module, so create a shorthand.
98 // The actual number of elements is not so important.
99 pub type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
100
101 mod enums;
102 mod type_map;
103
104 pub(crate) use type_map::TypeMap;
105
106 /// Returns from the enclosing function if the type debuginfo node with the given
107 /// unique ID can be found in the type map.
108 macro_rules! return_if_di_node_created_in_meantime {
109 ($cx: expr, $unique_type_id: expr) => {
110 if let Some(di_node) = debug_context($cx).type_map.di_node_for_unique_id($unique_type_id) {
111 return DINodeCreationResult::new(di_node, true);
112 }
113 };
114 }
115
116 /// Extract size and alignment from a TyAndLayout.
117 fn size_and_align_of<'tcx>(ty_and_layout: TyAndLayout<'tcx>) -> (Size, Align) {
118 (ty_and_layout.size, ty_and_layout.align.abi)
119 }
120
121 /// Creates debuginfo for a fixed size array (e.g. `[u64; 123]`).
122 /// For slices (that is, "arrays" of unknown size) use [build_slice_type_di_node].
123 fn build_fixed_size_array_di_node<'ll, 'tcx>(
124 cx: &CodegenCx<'ll, 'tcx>,
125 unique_type_id: UniqueTypeId<'tcx>,
126 array_type: Ty<'tcx>,
127 ) -> DINodeCreationResult<'ll> {
128 let ty::Array(element_type, len) = array_type.kind() else {
129 bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
130 };
131
132 let element_type_di_node = type_di_node(cx, *element_type);
133
134 return_if_di_node_created_in_meantime!(cx, unique_type_id);
135
136 let (size, align) = cx.size_and_align_of(array_type);
137
138 let upper_bound = len.eval_usize(cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong;
139
140 let subrange =
141 unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
142
143 let subscripts = create_DIArray(DIB(cx), &[subrange]);
144 let di_node = unsafe {
145 llvm::LLVMRustDIBuilderCreateArrayType(
146 DIB(cx),
147 size.bits(),
148 align.bits() as u32,
149 element_type_di_node,
150 subscripts,
151 )
152 };
153
154 DINodeCreationResult::new(di_node, false)
155 }
156
157 /// Creates debuginfo for built-in pointer-like things:
158 ///
159 /// - ty::Ref
160 /// - ty::RawPtr
161 /// - ty::Adt in the case it's Box
162 ///
163 /// At some point we might want to remove the special handling of Box
164 /// and treat it the same as other smart pointers (like Rc, Arc, ...).
165 fn build_pointer_or_reference_di_node<'ll, 'tcx>(
166 cx: &CodegenCx<'ll, 'tcx>,
167 ptr_type: Ty<'tcx>,
168 pointee_type: Ty<'tcx>,
169 unique_type_id: UniqueTypeId<'tcx>,
170 ) -> DINodeCreationResult<'ll> {
171 // The debuginfo generated by this function is only valid if `ptr_type` is really just
172 // a (fat) pointer. Make sure it is not called for e.g. `Box<T, NonZSTAllocator>`.
173 debug_assert_eq!(
174 cx.size_and_align_of(ptr_type),
175 cx.size_and_align_of(cx.tcx.mk_mut_ptr(pointee_type))
176 );
177
178 let pointee_type_di_node = type_di_node(cx, pointee_type);
179
180 return_if_di_node_created_in_meantime!(cx, unique_type_id);
181
182 let (thin_pointer_size, thin_pointer_align) =
183 cx.size_and_align_of(cx.tcx.mk_imm_ptr(cx.tcx.types.unit));
184 let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
185
186 match fat_pointer_kind(cx, pointee_type) {
187 None => {
188 // This is a thin pointer. Create a regular pointer type and give it the correct name.
189 debug_assert_eq!(
190 (thin_pointer_size, thin_pointer_align),
191 cx.size_and_align_of(ptr_type),
192 "ptr_type={}, pointee_type={}",
193 ptr_type,
194 pointee_type,
195 );
196
197 let di_node = unsafe {
198 llvm::LLVMRustDIBuilderCreatePointerType(
199 DIB(cx),
200 pointee_type_di_node,
201 thin_pointer_size.bits(),
202 thin_pointer_align.bits() as u32,
203 0, // Ignore DWARF address space.
204 ptr_type_debuginfo_name.as_ptr().cast(),
205 ptr_type_debuginfo_name.len(),
206 )
207 };
208
209 DINodeCreationResult { di_node, already_stored_in_typemap: false }
210 }
211 Some(fat_pointer_kind) => {
212 type_map::build_type_with_children(
213 cx,
214 type_map::stub(
215 cx,
216 Stub::Struct,
217 unique_type_id,
218 &ptr_type_debuginfo_name,
219 cx.size_and_align_of(ptr_type),
220 NO_SCOPE_METADATA,
221 DIFlags::FlagZero,
222 ),
223 |cx, owner| {
224 // FIXME: If this fat pointer is a `Box` then we don't want to use its
225 // type layout and instead use the layout of the raw pointer inside
226 // of it.
227 // The proper way to handle this is to not treat Box as a pointer
228 // at all and instead emit regular struct debuginfo for it. We just
229 // need to make sure that we don't break existing debuginfo consumers
230 // by doing that (at least not without a warning period).
231 let layout_type =
232 if ptr_type.is_box() { cx.tcx.mk_mut_ptr(pointee_type) } else { ptr_type };
233
234 let layout = cx.layout_of(layout_type);
235 let addr_field = layout.field(cx, abi::FAT_PTR_ADDR);
236 let extra_field = layout.field(cx, abi::FAT_PTR_EXTRA);
237
238 let (addr_field_name, extra_field_name) = match fat_pointer_kind {
239 FatPtrKind::Dyn => ("pointer", "vtable"),
240 FatPtrKind::Slice => ("data_ptr", "length"),
241 };
242
243 debug_assert_eq!(abi::FAT_PTR_ADDR, 0);
244 debug_assert_eq!(abi::FAT_PTR_EXTRA, 1);
245
246 // The data pointer type is a regular, thin pointer, regardless of whether this
247 // is a slice or a trait object.
248 let data_ptr_type_di_node = unsafe {
249 llvm::LLVMRustDIBuilderCreatePointerType(
250 DIB(cx),
251 pointee_type_di_node,
252 addr_field.size.bits(),
253 addr_field.align.abi.bits() as u32,
254 0, // Ignore DWARF address space.
255 std::ptr::null(),
256 0,
257 )
258 };
259
260 smallvec![
261 build_field_di_node(
262 cx,
263 owner,
264 addr_field_name,
265 (addr_field.size, addr_field.align.abi),
266 layout.fields.offset(abi::FAT_PTR_ADDR),
267 DIFlags::FlagZero,
268 data_ptr_type_di_node,
269 ),
270 build_field_di_node(
271 cx,
272 owner,
273 extra_field_name,
274 (extra_field.size, extra_field.align.abi),
275 layout.fields.offset(abi::FAT_PTR_EXTRA),
276 DIFlags::FlagZero,
277 type_di_node(cx, extra_field.ty),
278 ),
279 ]
280 },
281 NO_GENERICS,
282 )
283 }
284 }
285 }
286
287 fn build_subroutine_type_di_node<'ll, 'tcx>(
288 cx: &CodegenCx<'ll, 'tcx>,
289 unique_type_id: UniqueTypeId<'tcx>,
290 ) -> DINodeCreationResult<'ll> {
291 // It's possible to create a self-referential
292 // type in Rust by using 'impl trait':
293 //
294 // fn foo() -> impl Copy { foo }
295 //
296 // Unfortunately LLVM's API does not allow us to create recursive subroutine types.
297 // In order to work around that restriction we place a marker type in the type map,
298 // before creating the actual type. If the actual type is recursive, it will hit the
299 // marker type. So we end up with a type that looks like
300 //
301 // fn foo() -> <recursive_type>
302 //
303 // Once that is created, we replace the marker in the typemap with the actual type.
304 debug_context(cx)
305 .type_map
306 .unique_id_to_di_node
307 .borrow_mut()
308 .insert(unique_type_id, recursion_marker_type_di_node(cx));
309
310 let fn_ty = unique_type_id.expect_ty();
311 let signature = cx
312 .tcx
313 .normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), fn_ty.fn_sig(cx.tcx));
314
315 let signature_di_nodes: SmallVec<_> = iter::once(
316 // return type
317 match signature.output().kind() {
318 ty::Tuple(tys) if tys.is_empty() => {
319 // this is a "void" function
320 None
321 }
322 _ => Some(type_di_node(cx, signature.output())),
323 },
324 )
325 .chain(
326 // regular arguments
327 signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
328 )
329 .collect();
330
331 debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
332
333 let fn_di_node = unsafe {
334 llvm::LLVMRustDIBuilderCreateSubroutineType(
335 DIB(cx),
336 create_DIArray(DIB(cx), &signature_di_nodes[..]),
337 )
338 };
339
340 // This is actually a function pointer, so wrap it in pointer DI.
341 let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
342 let di_node = unsafe {
343 llvm::LLVMRustDIBuilderCreatePointerType(
344 DIB(cx),
345 fn_di_node,
346 cx.tcx.data_layout.pointer_size.bits(),
347 cx.tcx.data_layout.pointer_align.abi.bits() as u32,
348 0, // Ignore DWARF address space.
349 name.as_ptr().cast(),
350 name.len(),
351 )
352 };
353
354 DINodeCreationResult::new(di_node, false)
355 }
356
357 /// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs
358 /// we with the correct type name (e.g. "dyn SomeTrait<Foo, Item=u32> + Sync").
359 fn build_dyn_type_di_node<'ll, 'tcx>(
360 cx: &CodegenCx<'ll, 'tcx>,
361 dyn_type: Ty<'tcx>,
362 unique_type_id: UniqueTypeId<'tcx>,
363 ) -> DINodeCreationResult<'ll> {
364 if let ty::Dynamic(..) = dyn_type.kind() {
365 let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
366 type_map::build_type_with_children(
367 cx,
368 type_map::stub(
369 cx,
370 Stub::Struct,
371 unique_type_id,
372 &type_name,
373 cx.size_and_align_of(dyn_type),
374 NO_SCOPE_METADATA,
375 DIFlags::FlagZero,
376 ),
377 |_, _| smallvec![],
378 NO_GENERICS,
379 )
380 } else {
381 bug!(
382 "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
383 dyn_type
384 )
385 }
386 }
387
388 /// Create debuginfo for `[T]` and `str`. These are unsized.
389 ///
390 /// NOTE: We currently emit just emit the debuginfo for the element type here
391 /// (i.e. `T` for slices and `u8` for `str`), so that we end up with
392 /// `*const T` for the `data_ptr` field of the corresponding fat-pointer
393 /// debuginfo of `&[T]`.
394 ///
395 /// It would be preferable and more accurate if we emitted a DIArray of T
396 /// without an upper bound instead. That is, LLVM already supports emitting
397 /// debuginfo of arrays of unknown size. But GDB currently seems to end up
398 /// in an infinite loop when confronted with such a type.
399 ///
400 /// As a side effect of the current encoding every instance of a type like
401 /// `struct Foo { unsized_field: [u8] }` will look like
402 /// `struct Foo { unsized_field: u8 }` in debuginfo. If the length of the
403 /// slice is zero, then accessing `unsized_field` in the debugger would
404 /// result in an out-of-bounds access.
405 fn build_slice_type_di_node<'ll, 'tcx>(
406 cx: &CodegenCx<'ll, 'tcx>,
407 slice_type: Ty<'tcx>,
408 unique_type_id: UniqueTypeId<'tcx>,
409 ) -> DINodeCreationResult<'ll> {
410 let element_type = match slice_type.kind() {
411 ty::Slice(element_type) => *element_type,
412 ty::Str => cx.tcx.types.u8,
413 _ => {
414 bug!(
415 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
416 slice_type
417 )
418 }
419 };
420
421 let element_type_di_node = type_di_node(cx, element_type);
422 return_if_di_node_created_in_meantime!(cx, unique_type_id);
423 DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
424 }
425
426 /// Get the debuginfo node for the given type.
427 ///
428 /// This function will look up the debuginfo node in the TypeMap. If it can't find it, it
429 /// will create the node by dispatching to the corresponding `build_*_di_node()` function.
430 pub fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
431 let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
432
433 if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
434 {
435 return existing_di_node;
436 }
437
438 debug!("type_di_node: {:?}", t);
439
440 let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
441 ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
442 build_basic_type_di_node(cx, t)
443 }
444 ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
445 ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t),
446 ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
447 ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
448 ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
449 ty::RawPtr(ty::TypeAndMut { ty: pointee_type, .. }) | ty::Ref(_, pointee_type, _) => {
450 build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
451 }
452 // Box<T, A> may have a non-ZST allocator A. In that case, we
453 // cannot treat Box<T, A> as just an owned alias of `*mut T`.
454 ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
455 build_pointer_or_reference_di_node(cx, t, t.boxed_ty(), unique_type_id)
456 }
457 ty::FnDef(..) | ty::FnPtr(_) => build_subroutine_type_di_node(cx, unique_type_id),
458 ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
459 ty::Generator(..) => enums::build_generator_di_node(cx, unique_type_id),
460 ty::Adt(def, ..) => match def.adt_kind() {
461 AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id),
462 AdtKind::Union => build_union_type_di_node(cx, unique_type_id),
463 AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id),
464 },
465 ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
466 // Type parameters from polymorphized functions.
467 ty::Param(_) => build_param_type_di_node(cx, t),
468 _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t),
469 };
470
471 {
472 if already_stored_in_typemap {
473 // Make sure that we really do have a `TypeMap` entry for the unique type ID.
474 let di_node_for_uid =
475 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
476 Some(di_node) => di_node,
477 None => {
478 bug!(
479 "expected type debuginfo node for unique \
480 type ID '{:?}' to already be in \
481 the `debuginfo::TypeMap` but it \
482 was not.",
483 unique_type_id,
484 );
485 }
486 };
487
488 debug_assert_eq!(di_node_for_uid as *const _, di_node as *const _);
489 } else {
490 debug_context(cx).type_map.insert(unique_type_id, di_node);
491 }
492 }
493
494 di_node
495 }
496
497 // FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context.
498 fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
499 *debug_context(cx).recursion_marker_type.get_or_init(move || {
500 unsafe {
501 // The choice of type here is pretty arbitrary -
502 // anything reading the debuginfo for a recursive
503 // type is going to see *something* weird - the only
504 // question is what exactly it will see.
505 //
506 // FIXME: the name `<recur_type>` does not fit the naming scheme
507 // of other types.
508 //
509 // FIXME: it might make sense to use an actual pointer type here
510 // so that debuggers can show the address.
511 let name = "<recur_type>";
512 llvm::LLVMRustDIBuilderCreateBasicType(
513 DIB(cx),
514 name.as_ptr().cast(),
515 name.len(),
516 cx.tcx.data_layout.pointer_size.bits(),
517 DW_ATE_unsigned,
518 )
519 }
520 })
521 }
522
523 fn hex_encode(data: &[u8]) -> String {
524 let mut hex_string = String::with_capacity(data.len() * 2);
525 for byte in data.iter() {
526 write!(&mut hex_string, "{:02x}", byte).unwrap();
527 }
528 hex_string
529 }
530
531 pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
532 let cache_key = Some((source_file.name_hash, source_file.src_hash));
533 return debug_context(cx)
534 .created_files
535 .borrow_mut()
536 .entry(cache_key)
537 .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
538
539 #[instrument(skip(cx, source_file), level = "debug")]
540 fn alloc_new_file_metadata<'ll>(
541 cx: &CodegenCx<'ll, '_>,
542 source_file: &SourceFile,
543 ) -> &'ll DIFile {
544 debug!(?source_file.name);
545
546 let (directory, file_name) = match &source_file.name {
547 FileName::Real(filename) => {
548 let working_directory = &cx.sess().opts.working_dir;
549 debug!(?working_directory);
550
551 let filename = cx
552 .sess()
553 .source_map()
554 .path_mapping()
555 .to_embeddable_absolute_path(filename.clone(), working_directory);
556
557 // Construct the absolute path of the file
558 let abs_path = filename.remapped_path_if_available();
559 debug!(?abs_path);
560
561 if let Ok(rel_path) =
562 abs_path.strip_prefix(working_directory.remapped_path_if_available())
563 {
564 // If the compiler's working directory (which also is the DW_AT_comp_dir of
565 // the compilation unit) is a prefix of the path we are about to emit, then
566 // only emit the part relative to the working directory.
567 // Because of path remapping we sometimes see strange things here: `abs_path`
568 // might actually look like a relative path
569 // (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
570 // taking the working directory into account, downstream tooling will
571 // interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
572 // which makes no sense. Usually in such cases the working directory will also
573 // be remapped to `<crate-name-and-version>` or some other prefix of the path
574 // we are remapping, so we end up with
575 // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
576 // By moving the working directory portion into the `directory` part of the
577 // DIFile, we allow LLVM to emit just the relative path for DWARF, while
578 // still emitting the correct absolute path for CodeView.
579 (
580 working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
581 rel_path.to_string_lossy().into_owned(),
582 )
583 } else {
584 ("".into(), abs_path.to_string_lossy().into_owned())
585 }
586 }
587 other => ("".into(), other.prefer_remapped().to_string_lossy().into_owned()),
588 };
589
590 let hash_kind = match source_file.src_hash.kind {
591 rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
592 rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
593 rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
594 };
595 let hash_value = hex_encode(source_file.src_hash.hash_bytes());
596
597 unsafe {
598 llvm::LLVMRustDIBuilderCreateFile(
599 DIB(cx),
600 file_name.as_ptr().cast(),
601 file_name.len(),
602 directory.as_ptr().cast(),
603 directory.len(),
604 hash_kind,
605 hash_value.as_ptr().cast(),
606 hash_value.len(),
607 )
608 }
609 }
610 }
611
612 pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
613 debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe {
614 let file_name = "<unknown>";
615 let directory = "";
616 let hash_value = "";
617
618 llvm::LLVMRustDIBuilderCreateFile(
619 DIB(cx),
620 file_name.as_ptr().cast(),
621 file_name.len(),
622 directory.as_ptr().cast(),
623 directory.len(),
624 llvm::ChecksumKind::None,
625 hash_value.as_ptr().cast(),
626 hash_value.len(),
627 )
628 })
629 }
630
631 trait MsvcBasicName {
632 fn msvc_basic_name(self) -> &'static str;
633 }
634
635 impl MsvcBasicName for ty::IntTy {
636 fn msvc_basic_name(self) -> &'static str {
637 match self {
638 ty::IntTy::Isize => "ptrdiff_t",
639 ty::IntTy::I8 => "__int8",
640 ty::IntTy::I16 => "__int16",
641 ty::IntTy::I32 => "__int32",
642 ty::IntTy::I64 => "__int64",
643 ty::IntTy::I128 => "__int128",
644 }
645 }
646 }
647
648 impl MsvcBasicName for ty::UintTy {
649 fn msvc_basic_name(self) -> &'static str {
650 match self {
651 ty::UintTy::Usize => "size_t",
652 ty::UintTy::U8 => "unsigned __int8",
653 ty::UintTy::U16 => "unsigned __int16",
654 ty::UintTy::U32 => "unsigned __int32",
655 ty::UintTy::U64 => "unsigned __int64",
656 ty::UintTy::U128 => "unsigned __int128",
657 }
658 }
659 }
660
661 impl MsvcBasicName for ty::FloatTy {
662 fn msvc_basic_name(self) -> &'static str {
663 match self {
664 ty::FloatTy::F32 => "float",
665 ty::FloatTy::F64 => "double",
666 }
667 }
668 }
669
670 fn build_basic_type_di_node<'ll, 'tcx>(
671 cx: &CodegenCx<'ll, 'tcx>,
672 t: Ty<'tcx>,
673 ) -> DINodeCreationResult<'ll> {
674 debug!("build_basic_type_di_node: {:?}", t);
675
676 // When targeting MSVC, emit MSVC style type names for compatibility with
677 // .natvis visualizers (and perhaps other existing native debuggers?)
678 let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
679
680 let (name, encoding) = match t.kind() {
681 ty::Never => ("!", DW_ATE_unsigned),
682 ty::Tuple(elements) if elements.is_empty() => {
683 if cpp_like_debuginfo {
684 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
685 } else {
686 ("()", DW_ATE_unsigned)
687 }
688 }
689 ty::Bool => ("bool", DW_ATE_boolean),
690 ty::Char => ("char", DW_ATE_UTF),
691 ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
692 ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
693 ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
694 ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
695 ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
696 ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
697 _ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
698 };
699
700 let ty_di_node = unsafe {
701 llvm::LLVMRustDIBuilderCreateBasicType(
702 DIB(cx),
703 name.as_ptr().cast(),
704 name.len(),
705 cx.size_of(t).bits(),
706 encoding,
707 )
708 };
709
710 if !cpp_like_debuginfo {
711 return DINodeCreationResult::new(ty_di_node, false);
712 }
713
714 let typedef_name = match t.kind() {
715 ty::Int(int_ty) => int_ty.name_str(),
716 ty::Uint(uint_ty) => uint_ty.name_str(),
717 ty::Float(float_ty) => float_ty.name_str(),
718 _ => return DINodeCreationResult::new(ty_di_node, false),
719 };
720
721 let typedef_di_node = unsafe {
722 llvm::LLVMRustDIBuilderCreateTypedef(
723 DIB(cx),
724 ty_di_node,
725 typedef_name.as_ptr().cast(),
726 typedef_name.len(),
727 unknown_file_metadata(cx),
728 0,
729 None,
730 )
731 };
732
733 DINodeCreationResult::new(typedef_di_node, false)
734 }
735
736 fn build_foreign_type_di_node<'ll, 'tcx>(
737 cx: &CodegenCx<'ll, 'tcx>,
738 t: Ty<'tcx>,
739 unique_type_id: UniqueTypeId<'tcx>,
740 ) -> DINodeCreationResult<'ll> {
741 debug!("build_foreign_type_di_node: {:?}", t);
742
743 let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
744 bug!("build_foreign_type_di_node() called with unexpected type: {:?}", unique_type_id.expect_ty());
745 };
746
747 build_type_with_children(
748 cx,
749 type_map::stub(
750 cx,
751 Stub::Struct,
752 unique_type_id,
753 &compute_debuginfo_type_name(cx.tcx, t, false),
754 cx.size_and_align_of(t),
755 Some(get_namespace_for_item(cx, def_id)),
756 DIFlags::FlagZero,
757 ),
758 |_, _| smallvec![],
759 NO_GENERICS,
760 )
761 }
762
763 fn build_param_type_di_node<'ll, 'tcx>(
764 cx: &CodegenCx<'ll, 'tcx>,
765 t: Ty<'tcx>,
766 ) -> DINodeCreationResult<'ll> {
767 debug!("build_param_type_di_node: {:?}", t);
768 let name = format!("{:?}", t);
769 DINodeCreationResult {
770 di_node: unsafe {
771 llvm::LLVMRustDIBuilderCreateBasicType(
772 DIB(cx),
773 name.as_ptr().cast(),
774 name.len(),
775 Size::ZERO.bits(),
776 DW_ATE_unsigned,
777 )
778 },
779 already_stored_in_typemap: false,
780 }
781 }
782
783 pub fn build_compile_unit_di_node<'ll, 'tcx>(
784 tcx: TyCtxt<'tcx>,
785 codegen_unit_name: &str,
786 debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
787 ) -> &'ll DIDescriptor {
788 let mut name_in_debuginfo = match tcx.sess.local_crate_source_file {
789 Some(ref path) => path.clone(),
790 None => PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()),
791 };
792
793 // To avoid breaking split DWARF, we need to ensure that each codegen unit
794 // has a unique `DW_AT_name`. This is because there's a remote chance that
795 // different codegen units for the same module will have entirely
796 // identical DWARF entries for the purpose of the DWO ID, which would
797 // violate Appendix F ("Split Dwarf Object Files") of the DWARF 5
798 // specification. LLVM uses the algorithm specified in section 7.32 "Type
799 // Signature Computation" to compute the DWO ID, which does not include
800 // any fields that would distinguish compilation units. So we must embed
801 // the codegen unit name into the `DW_AT_name`. (Issue #88521.)
802 //
803 // Additionally, the OSX linker has an idiosyncrasy where it will ignore
804 // some debuginfo if multiple object files with the same `DW_AT_name` are
805 // linked together.
806 //
807 // As a workaround for these two issues, we generate unique names for each
808 // object file. Those do not correspond to an actual source file but that
809 // is harmless.
810 name_in_debuginfo.push("@");
811 name_in_debuginfo.push(codegen_unit_name);
812
813 debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
814 let rustc_producer =
815 format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"),);
816 // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
817 let producer = format!("clang LLVM ({})", rustc_producer);
818
819 let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
820 let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
821 let flags = "\0";
822 let output_filenames = tcx.output_filenames(());
823 let split_name = if tcx.sess.target_can_use_split_dwarf() {
824 output_filenames
825 .split_dwarf_path(
826 tcx.sess.split_debuginfo(),
827 tcx.sess.opts.debugging_opts.split_dwarf_kind,
828 Some(codegen_unit_name),
829 )
830 // We get a path relative to the working directory from split_dwarf_path
831 .map(|f| tcx.sess.source_map().path_mapping().map_prefix(f).0)
832 } else {
833 None
834 }
835 .unwrap_or_default();
836 let split_name = split_name.to_str().unwrap();
837
838 // FIXME(#60020):
839 //
840 // This should actually be
841 //
842 // let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
843 //
844 // That is, we should set LLVM's emission kind to `LineTablesOnly` if
845 // we are compiling with "limited" debuginfo. However, some of the
846 // existing tools relied on slightly more debuginfo being generated than
847 // would be the case with `LineTablesOnly`, and we did not want to break
848 // these tools in a "drive-by fix", without a good idea or plan about
849 // what limited debuginfo should exactly look like. So for now we keep
850 // the emission kind as `FullDebug`.
851 //
852 // See https://github.com/rust-lang/rust/issues/60020 for details.
853 let kind = DebugEmissionKind::FullDebug;
854 assert!(tcx.sess.opts.debuginfo != DebugInfo::None);
855
856 unsafe {
857 let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
858 debug_context.builder,
859 name_in_debuginfo.as_ptr().cast(),
860 name_in_debuginfo.len(),
861 work_dir.as_ptr().cast(),
862 work_dir.len(),
863 llvm::ChecksumKind::None,
864 ptr::null(),
865 0,
866 );
867
868 let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
869 debug_context.builder,
870 DW_LANG_RUST,
871 compile_unit_file,
872 producer.as_ptr().cast(),
873 producer.len(),
874 tcx.sess.opts.optimize != config::OptLevel::No,
875 flags.as_ptr().cast(),
876 0,
877 // NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
878 // put the path supplied to `MCSplitDwarfFile` into the debug info of the final
879 // output(s).
880 split_name.as_ptr().cast(),
881 split_name.len(),
882 kind,
883 0,
884 tcx.sess.opts.debugging_opts.split_dwarf_inlining,
885 );
886
887 if tcx.sess.opts.debugging_opts.profile {
888 let cu_desc_metadata =
889 llvm::LLVMRustMetadataAsValue(debug_context.llcontext, unit_metadata);
890 let default_gcda_path = &output_filenames.with_extension("gcda");
891 let gcda_path =
892 tcx.sess.opts.debugging_opts.profile_emit.as_ref().unwrap_or(default_gcda_path);
893
894 let gcov_cu_info = [
895 path_to_mdstring(debug_context.llcontext, &output_filenames.with_extension("gcno")),
896 path_to_mdstring(debug_context.llcontext, gcda_path),
897 cu_desc_metadata,
898 ];
899 let gcov_metadata = llvm::LLVMMDNodeInContext(
900 debug_context.llcontext,
901 gcov_cu_info.as_ptr(),
902 gcov_cu_info.len() as c_uint,
903 );
904
905 let llvm_gcov_ident = cstr!("llvm.gcov");
906 llvm::LLVMAddNamedMetadataOperand(
907 debug_context.llmod,
908 llvm_gcov_ident.as_ptr(),
909 gcov_metadata,
910 );
911 }
912
913 // Insert `llvm.ident` metadata on the wasm targets since that will
914 // get hooked up to the "producer" sections `processed-by` information.
915 if tcx.sess.target.is_like_wasm {
916 let name_metadata = llvm::LLVMMDStringInContext(
917 debug_context.llcontext,
918 rustc_producer.as_ptr().cast(),
919 rustc_producer.as_bytes().len() as c_uint,
920 );
921 llvm::LLVMAddNamedMetadataOperand(
922 debug_context.llmod,
923 cstr!("llvm.ident").as_ptr(),
924 llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
925 );
926 }
927
928 return unit_metadata;
929 };
930
931 fn path_to_mdstring<'ll>(llcx: &'ll llvm::Context, path: &Path) -> &'ll Value {
932 let path_str = path_to_c_string(path);
933 unsafe {
934 llvm::LLVMMDStringInContext(
935 llcx,
936 path_str.as_ptr(),
937 path_str.as_bytes().len() as c_uint,
938 )
939 }
940 }
941 }
942
943 /// Creates a `DW_TAG_member` entry inside the DIE represented by the given `type_di_node`.
944 fn build_field_di_node<'ll, 'tcx>(
945 cx: &CodegenCx<'ll, 'tcx>,
946 owner: &'ll DIScope,
947 name: &str,
948 size_and_align: (Size, Align),
949 offset: Size,
950 flags: DIFlags,
951 type_di_node: &'ll DIType,
952 ) -> &'ll DIType {
953 unsafe {
954 llvm::LLVMRustDIBuilderCreateMemberType(
955 DIB(cx),
956 owner,
957 name.as_ptr().cast(),
958 name.len(),
959 unknown_file_metadata(cx),
960 UNKNOWN_LINE_NUMBER,
961 size_and_align.0.bits(),
962 size_and_align.1.bits() as u32,
963 offset.bits(),
964 flags,
965 type_di_node,
966 )
967 }
968 }
969
970 /// Creates the debuginfo node for a Rust struct type. Maybe be a regular struct or a tuple-struct.
971 fn build_struct_type_di_node<'ll, 'tcx>(
972 cx: &CodegenCx<'ll, 'tcx>,
973 unique_type_id: UniqueTypeId<'tcx>,
974 ) -> DINodeCreationResult<'ll> {
975 let struct_type = unique_type_id.expect_ty();
976 let ty::Adt(adt_def, _) = struct_type.kind() else {
977 bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
978 };
979 debug_assert!(adt_def.is_struct());
980 let containing_scope = get_namespace_for_item(cx, adt_def.did());
981 let struct_type_and_layout = cx.layout_of(struct_type);
982 let variant_def = adt_def.non_enum_variant();
983
984 type_map::build_type_with_children(
985 cx,
986 type_map::stub(
987 cx,
988 Stub::Struct,
989 unique_type_id,
990 &compute_debuginfo_type_name(cx.tcx, struct_type, false),
991 size_and_align_of(struct_type_and_layout),
992 Some(containing_scope),
993 DIFlags::FlagZero,
994 ),
995 // Fields:
996 |cx, owner| {
997 variant_def
998 .fields
999 .iter()
1000 .enumerate()
1001 .map(|(i, f)| {
1002 let field_name = if variant_def.ctor_kind == CtorKind::Fn {
1003 // This is a tuple struct
1004 tuple_field_name(i)
1005 } else {
1006 // This is struct with named fields
1007 Cow::Borrowed(f.name.as_str())
1008 };
1009 let field_layout = struct_type_and_layout.field(cx, i);
1010 build_field_di_node(
1011 cx,
1012 owner,
1013 &field_name[..],
1014 (field_layout.size, field_layout.align.abi),
1015 struct_type_and_layout.fields.offset(i),
1016 DIFlags::FlagZero,
1017 type_di_node(cx, field_layout.ty),
1018 )
1019 })
1020 .collect()
1021 },
1022 |cx| build_generic_type_param_di_nodes(cx, struct_type),
1023 )
1024 }
1025
1026 //=-----------------------------------------------------------------------------
1027 // Tuples
1028 //=-----------------------------------------------------------------------------
1029
1030 /// Returns names of captured upvars for closures and generators.
1031 ///
1032 /// Here are some examples:
1033 /// - `name__field1__field2` when the upvar is captured by value.
1034 /// - `_ref__name__field` when the upvar is captured by reference.
1035 ///
1036 /// For generators this only contains upvars that are shared by all states.
1037 fn closure_saved_names_of_captured_variables(tcx: TyCtxt<'_>, def_id: DefId) -> SmallVec<String> {
1038 let body = tcx.optimized_mir(def_id);
1039
1040 body.var_debug_info
1041 .iter()
1042 .filter_map(|var| {
1043 let is_ref = match var.value {
1044 mir::VarDebugInfoContents::Place(place) if place.local == mir::Local::new(1) => {
1045 // The projection is either `[.., Field, Deref]` or `[.., Field]`. It
1046 // implies whether the variable is captured by value or by reference.
1047 matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
1048 }
1049 _ => return None,
1050 };
1051 let prefix = if is_ref { "_ref__" } else { "" };
1052 Some(prefix.to_owned() + var.name.as_str())
1053 })
1054 .collect()
1055 }
1056
1057 /// Builds the DW_TAG_member debuginfo nodes for the upvars of a closure or generator.
1058 /// For a generator, this will handle upvars shared by all states.
1059 fn build_upvar_field_di_nodes<'ll, 'tcx>(
1060 cx: &CodegenCx<'ll, 'tcx>,
1061 closure_or_generator_ty: Ty<'tcx>,
1062 closure_or_generator_di_node: &'ll DIType,
1063 ) -> SmallVec<&'ll DIType> {
1064 let (&def_id, up_var_tys) = match closure_or_generator_ty.kind() {
1065 ty::Generator(def_id, substs, _) => {
1066 let upvar_tys: SmallVec<_> = substs.as_generator().prefix_tys().collect();
1067 (def_id, upvar_tys)
1068 }
1069 ty::Closure(def_id, substs) => {
1070 let upvar_tys: SmallVec<_> = substs.as_closure().upvar_tys().collect();
1071 (def_id, upvar_tys)
1072 }
1073 _ => {
1074 bug!(
1075 "build_upvar_field_di_nodes() called with non-closure-or-generator-type: {:?}",
1076 closure_or_generator_ty
1077 )
1078 }
1079 };
1080
1081 debug_assert!(
1082 up_var_tys
1083 .iter()
1084 .all(|&t| t == cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t))
1085 );
1086
1087 let capture_names = closure_saved_names_of_captured_variables(cx.tcx, def_id);
1088 let layout = cx.layout_of(closure_or_generator_ty);
1089
1090 up_var_tys
1091 .into_iter()
1092 .zip(capture_names.iter())
1093 .enumerate()
1094 .map(|(index, (up_var_ty, capture_name))| {
1095 build_field_di_node(
1096 cx,
1097 closure_or_generator_di_node,
1098 capture_name,
1099 cx.size_and_align_of(up_var_ty),
1100 layout.fields.offset(index),
1101 DIFlags::FlagZero,
1102 type_di_node(cx, up_var_ty),
1103 )
1104 })
1105 .collect()
1106 }
1107
1108 /// Builds the DW_TAG_structure_type debuginfo node for a Rust tuple type.
1109 fn build_tuple_type_di_node<'ll, 'tcx>(
1110 cx: &CodegenCx<'ll, 'tcx>,
1111 unique_type_id: UniqueTypeId<'tcx>,
1112 ) -> DINodeCreationResult<'ll> {
1113 let tuple_type = unique_type_id.expect_ty();
1114 let &ty::Tuple(component_types) = tuple_type.kind() else {
1115 bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1116 };
1117
1118 let tuple_type_and_layout = cx.layout_of(tuple_type);
1119 let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1120
1121 type_map::build_type_with_children(
1122 cx,
1123 type_map::stub(
1124 cx,
1125 Stub::Struct,
1126 unique_type_id,
1127 &type_name,
1128 size_and_align_of(tuple_type_and_layout),
1129 NO_SCOPE_METADATA,
1130 DIFlags::FlagZero,
1131 ),
1132 // Fields:
1133 |cx, tuple_di_node| {
1134 component_types
1135 .into_iter()
1136 .enumerate()
1137 .map(|(index, component_type)| {
1138 build_field_di_node(
1139 cx,
1140 tuple_di_node,
1141 &tuple_field_name(index),
1142 cx.size_and_align_of(component_type),
1143 tuple_type_and_layout.fields.offset(index),
1144 DIFlags::FlagZero,
1145 type_di_node(cx, component_type),
1146 )
1147 })
1148 .collect()
1149 },
1150 NO_GENERICS,
1151 )
1152 }
1153
1154 /// Builds the debuginfo node for a closure environment.
1155 fn build_closure_env_di_node<'ll, 'tcx>(
1156 cx: &CodegenCx<'ll, 'tcx>,
1157 unique_type_id: UniqueTypeId<'tcx>,
1158 ) -> DINodeCreationResult<'ll> {
1159 let closure_env_type = unique_type_id.expect_ty();
1160 let &ty::Closure(def_id, _substs) = closure_env_type.kind() else {
1161 bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1162 };
1163 let containing_scope = get_namespace_for_item(cx, def_id);
1164 let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1165
1166 type_map::build_type_with_children(
1167 cx,
1168 type_map::stub(
1169 cx,
1170 Stub::Struct,
1171 unique_type_id,
1172 &type_name,
1173 cx.size_and_align_of(closure_env_type),
1174 Some(containing_scope),
1175 DIFlags::FlagZero,
1176 ),
1177 // Fields:
1178 |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1179 NO_GENERICS,
1180 )
1181 }
1182
1183 /// Build the debuginfo node for a Rust `union` type.
1184 fn build_union_type_di_node<'ll, 'tcx>(
1185 cx: &CodegenCx<'ll, 'tcx>,
1186 unique_type_id: UniqueTypeId<'tcx>,
1187 ) -> DINodeCreationResult<'ll> {
1188 let union_type = unique_type_id.expect_ty();
1189 let (union_def_id, variant_def) = match union_type.kind() {
1190 ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1191 _ => bug!("build_union_type_di_node on a non-ADT"),
1192 };
1193 let containing_scope = get_namespace_for_item(cx, union_def_id);
1194 let union_ty_and_layout = cx.layout_of(union_type);
1195 let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1196
1197 type_map::build_type_with_children(
1198 cx,
1199 type_map::stub(
1200 cx,
1201 Stub::Union,
1202 unique_type_id,
1203 &type_name,
1204 size_and_align_of(union_ty_and_layout),
1205 Some(containing_scope),
1206 DIFlags::FlagZero,
1207 ),
1208 // Fields:
1209 |cx, owner| {
1210 variant_def
1211 .fields
1212 .iter()
1213 .enumerate()
1214 .map(|(i, f)| {
1215 let field_layout = union_ty_and_layout.field(cx, i);
1216 build_field_di_node(
1217 cx,
1218 owner,
1219 f.name.as_str(),
1220 size_and_align_of(field_layout),
1221 Size::ZERO,
1222 DIFlags::FlagZero,
1223 type_di_node(cx, field_layout.ty),
1224 )
1225 })
1226 .collect()
1227 },
1228 // Generics:
1229 |cx| build_generic_type_param_di_nodes(cx, union_type),
1230 )
1231 }
1232
1233 // FIXME(eddyb) maybe precompute this? Right now it's computed once
1234 // per generator monomorphization, but it doesn't depend on substs.
1235 fn generator_layout_and_saved_local_names<'tcx>(
1236 tcx: TyCtxt<'tcx>,
1237 def_id: DefId,
1238 ) -> (&'tcx GeneratorLayout<'tcx>, IndexVec<mir::GeneratorSavedLocal, Option<Symbol>>) {
1239 let body = tcx.optimized_mir(def_id);
1240 let generator_layout = body.generator_layout().unwrap();
1241 let mut generator_saved_local_names = IndexVec::from_elem(None, &generator_layout.field_tys);
1242
1243 let state_arg = mir::Local::new(1);
1244 for var in &body.var_debug_info {
1245 let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
1246 if place.local != state_arg {
1247 continue;
1248 }
1249 match place.projection[..] {
1250 [
1251 // Deref of the `Pin<&mut Self>` state argument.
1252 mir::ProjectionElem::Field(..),
1253 mir::ProjectionElem::Deref,
1254 // Field of a variant of the state.
1255 mir::ProjectionElem::Downcast(_, variant),
1256 mir::ProjectionElem::Field(field, _),
1257 ] => {
1258 let name = &mut generator_saved_local_names
1259 [generator_layout.variant_fields[variant][field]];
1260 if name.is_none() {
1261 name.replace(var.name);
1262 }
1263 }
1264 _ => {}
1265 }
1266 }
1267 (generator_layout, generator_saved_local_names)
1268 }
1269
1270 /// Computes the type parameters for a type, if any, for the given metadata.
1271 fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1272 cx: &CodegenCx<'ll, 'tcx>,
1273 ty: Ty<'tcx>,
1274 ) -> SmallVec<&'ll DIType> {
1275 if let ty::Adt(def, substs) = *ty.kind() {
1276 if substs.types().next().is_some() {
1277 let generics = cx.tcx.generics_of(def.did());
1278 let names = get_parameter_names(cx, generics);
1279 let template_params: SmallVec<_> = iter::zip(substs, names)
1280 .filter_map(|(kind, name)| {
1281 if let GenericArgKind::Type(ty) = kind.unpack() {
1282 let actual_type =
1283 cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
1284 let actual_type_di_node = type_di_node(cx, actual_type);
1285 let name = name.as_str();
1286 Some(unsafe {
1287 llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
1288 DIB(cx),
1289 None,
1290 name.as_ptr().cast(),
1291 name.len(),
1292 actual_type_di_node,
1293 )
1294 })
1295 } else {
1296 None
1297 }
1298 })
1299 .collect();
1300
1301 return template_params;
1302 }
1303 }
1304
1305 return smallvec![];
1306
1307 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1308 let mut names = generics
1309 .parent
1310 .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1311 names.extend(generics.params.iter().map(|param| param.name));
1312 names
1313 }
1314 }
1315
1316 /// Creates debug information for the given global variable.
1317 ///
1318 /// Adds the created debuginfo nodes directly to the crate's IR.
1319 pub fn build_global_var_di_node<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId, global: &'ll Value) {
1320 if cx.dbg_cx.is_none() {
1321 return;
1322 }
1323
1324 // Only create type information if full debuginfo is enabled
1325 if cx.sess().opts.debuginfo != DebugInfo::Full {
1326 return;
1327 }
1328
1329 let tcx = cx.tcx;
1330
1331 // We may want to remove the namespace scope if we're in an extern block (see
1332 // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
1333 let var_scope = get_namespace_for_item(cx, def_id);
1334 let span = tcx.def_span(def_id);
1335
1336 let (file_metadata, line_number) = if !span.is_dummy() {
1337 let loc = cx.lookup_debug_loc(span.lo());
1338 (file_metadata(cx, &loc.file), loc.line)
1339 } else {
1340 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1341 };
1342
1343 let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1344 let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, ty::ParamEnv::reveal_all());
1345 let type_di_node = type_di_node(cx, variable_type);
1346 let var_name = tcx.item_name(def_id);
1347 let var_name = var_name.as_str();
1348 let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1349 // When empty, linkage_name field is omitted,
1350 // which is what we want for no_mangle statics
1351 let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1352
1353 let global_align = cx.align_of(variable_type);
1354
1355 unsafe {
1356 llvm::LLVMRustDIBuilderCreateStaticVariable(
1357 DIB(cx),
1358 Some(var_scope),
1359 var_name.as_ptr().cast(),
1360 var_name.len(),
1361 linkage_name.as_ptr().cast(),
1362 linkage_name.len(),
1363 file_metadata,
1364 line_number,
1365 type_di_node,
1366 is_local_to_unit,
1367 global,
1368 None,
1369 global_align.bits() as u32,
1370 );
1371 }
1372 }
1373
1374 /// Generates LLVM debuginfo for a vtable.
1375 ///
1376 /// The vtable type looks like a struct with a field for each function pointer and super-trait
1377 /// pointer it contains (plus the `size` and `align` fields).
1378 ///
1379 /// Except for `size`, `align`, and `drop_in_place`, the field names don't try to mirror
1380 /// the name of the method they implement. This can be implemented in the future once there
1381 /// is a proper disambiguation scheme for dealing with methods from different traits that have
1382 /// the same name.
1383 fn build_vtable_type_di_node<'ll, 'tcx>(
1384 cx: &CodegenCx<'ll, 'tcx>,
1385 ty: Ty<'tcx>,
1386 poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
1387 ) -> &'ll DIType {
1388 let tcx = cx.tcx;
1389
1390 let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1391 let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1392 let trait_ref = tcx.erase_regions(trait_ref);
1393
1394 tcx.vtable_entries(trait_ref)
1395 } else {
1396 TyCtxt::COMMON_VTABLE_ENTRIES
1397 };
1398
1399 // All function pointers are described as opaque pointers. This could be improved in the future
1400 // by describing them as actual function pointers.
1401 let void_pointer_ty = tcx.mk_imm_ptr(tcx.types.unit);
1402 let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1403 let usize_di_node = type_di_node(cx, tcx.types.usize);
1404 let (pointer_size, pointer_align) = cx.size_and_align_of(void_pointer_ty);
1405 // If `usize` is not pointer-sized and -aligned then the size and alignment computations
1406 // for the vtable as a whole would be wrong. Let's make sure this holds even on weird
1407 // platforms.
1408 assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1409
1410 let vtable_type_name =
1411 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1412 let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1413 let size = pointer_size * vtable_entries.len() as u64;
1414
1415 // This gets mapped to a DW_AT_containing_type attribute which allows GDB to correlate
1416 // the vtable to the type it is for.
1417 let vtable_holder = type_di_node(cx, ty);
1418
1419 build_type_with_children(
1420 cx,
1421 type_map::stub(
1422 cx,
1423 Stub::VtableTy { vtable_holder },
1424 unique_type_id,
1425 &vtable_type_name,
1426 (size, pointer_align),
1427 NO_SCOPE_METADATA,
1428 DIFlags::FlagArtificial,
1429 ),
1430 |cx, vtable_type_di_node| {
1431 vtable_entries
1432 .iter()
1433 .enumerate()
1434 .filter_map(|(index, vtable_entry)| {
1435 let (field_name, field_type_di_node) = match vtable_entry {
1436 ty::VtblEntry::MetadataDropInPlace => {
1437 ("drop_in_place".to_string(), void_pointer_type_di_node)
1438 }
1439 ty::VtblEntry::Method(_) => {
1440 // Note: This code does not try to give a proper name to each method
1441 // because their might be multiple methods with the same name
1442 // (coming from different traits).
1443 (format!("__method{}", index), void_pointer_type_di_node)
1444 }
1445 ty::VtblEntry::TraitVPtr(_) => {
1446 (format!("__super_trait_ptr{}", index), void_pointer_type_di_node)
1447 }
1448 ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1449 ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1450 ty::VtblEntry::Vacant => return None,
1451 };
1452
1453 let field_offset = pointer_size * index as u64;
1454
1455 Some(build_field_di_node(
1456 cx,
1457 vtable_type_di_node,
1458 &field_name,
1459 (pointer_size, pointer_align),
1460 field_offset,
1461 DIFlags::FlagZero,
1462 field_type_di_node,
1463 ))
1464 })
1465 .collect()
1466 },
1467 NO_GENERICS,
1468 )
1469 .di_node
1470 }
1471
1472 fn vcall_visibility_metadata<'ll, 'tcx>(
1473 cx: &CodegenCx<'ll, 'tcx>,
1474 ty: Ty<'tcx>,
1475 trait_ref: Option<PolyExistentialTraitRef<'tcx>>,
1476 vtable: &'ll Value,
1477 ) {
1478 enum VCallVisibility {
1479 Public = 0,
1480 LinkageUnit = 1,
1481 TranslationUnit = 2,
1482 }
1483
1484 let Some(trait_ref) = trait_ref else { return };
1485
1486 let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1487 let trait_ref_self = cx.tcx.erase_regions(trait_ref_self);
1488 let trait_def_id = trait_ref_self.def_id();
1489 let trait_vis = cx.tcx.visibility(trait_def_id);
1490
1491 let cgus = cx.sess().codegen_units();
1492 let single_cgu = cgus == 1;
1493
1494 let lto = cx.sess().lto();
1495
1496 // Since LLVM requires full LTO for the virtual function elimination optimization to apply,
1497 // only the `Lto::Fat` cases are relevant currently.
1498 let vcall_visibility = match (lto, trait_vis, single_cgu) {
1499 // If there is not LTO and the visibility in public, we have to assume that the vtable can
1500 // be seen from anywhere. With multiple CGUs, the vtable is quasi-public.
1501 (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1502 | (Lto::No, Visibility::Restricted(_) | Visibility::Invisible, false) => {
1503 VCallVisibility::Public
1504 }
1505 // With LTO and a quasi-public visibility, the usages of the functions of the vtable are
1506 // all known by the `LinkageUnit`.
1507 // FIXME: LLVM only supports this optimization for `Lto::Fat` currently. Once it also
1508 // supports `Lto::Thin` the `VCallVisibility` may have to be adjusted for those.
1509 (Lto::Fat | Lto::Thin, Visibility::Public, _)
1510 | (
1511 Lto::ThinLocal | Lto::Thin | Lto::Fat,
1512 Visibility::Restricted(_) | Visibility::Invisible,
1513 false,
1514 ) => VCallVisibility::LinkageUnit,
1515 // If there is only one CGU, private vtables can only be seen by that CGU/translation unit
1516 // and therefore we know of all usages of functions in the vtable.
1517 (_, Visibility::Restricted(_) | Visibility::Invisible, true) => {
1518 VCallVisibility::TranslationUnit
1519 }
1520 };
1521
1522 let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1523
1524 unsafe {
1525 let typeid = llvm::LLVMMDStringInContext(
1526 cx.llcx,
1527 trait_ref_typeid.as_ptr() as *const c_char,
1528 trait_ref_typeid.as_bytes().len() as c_uint,
1529 );
1530 let v = [cx.const_usize(0), typeid];
1531 llvm::LLVMRustGlobalAddMetadata(
1532 vtable,
1533 llvm::MD_type as c_uint,
1534 llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
1535 cx.llcx,
1536 v.as_ptr(),
1537 v.len() as c_uint,
1538 )),
1539 );
1540 let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1541 let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1542 llvm::LLVMGlobalSetMetadata(
1543 vtable,
1544 llvm::MetadataType::MD_vcall_visibility as c_uint,
1545 vcall_visibility_metadata,
1546 );
1547 }
1548 }
1549
1550 /// Creates debug information for the given vtable, which is for the
1551 /// given type.
1552 ///
1553 /// Adds the created metadata nodes directly to the crate's IR.
1554 pub fn create_vtable_di_node<'ll, 'tcx>(
1555 cx: &CodegenCx<'ll, 'tcx>,
1556 ty: Ty<'tcx>,
1557 poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
1558 vtable: &'ll Value,
1559 ) {
1560 // FIXME(flip1995): The virtual function elimination optimization only works with full LTO in
1561 // LLVM at the moment.
1562 if cx.sess().opts.debugging_opts.virtual_function_elimination && cx.sess().lto() == Lto::Fat {
1563 vcall_visibility_metadata(cx, ty, poly_trait_ref, vtable);
1564 }
1565
1566 if cx.dbg_cx.is_none() {
1567 return;
1568 }
1569
1570 // Only create type information if full debuginfo is enabled
1571 if cx.sess().opts.debuginfo != DebugInfo::Full {
1572 return;
1573 }
1574
1575 let vtable_name =
1576 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1577 let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1578 let linkage_name = "";
1579
1580 unsafe {
1581 llvm::LLVMRustDIBuilderCreateStaticVariable(
1582 DIB(cx),
1583 NO_SCOPE_METADATA,
1584 vtable_name.as_ptr().cast(),
1585 vtable_name.len(),
1586 linkage_name.as_ptr().cast(),
1587 linkage_name.len(),
1588 unknown_file_metadata(cx),
1589 UNKNOWN_LINE_NUMBER,
1590 vtable_type_di_node,
1591 true,
1592 vtable,
1593 None,
1594 0,
1595 );
1596 }
1597 }
1598
1599 /// Creates an "extension" of an existing `DIScope` into another file.
1600 pub fn extend_scope_to_file<'ll>(
1601 cx: &CodegenCx<'ll, '_>,
1602 scope_metadata: &'ll DIScope,
1603 file: &SourceFile,
1604 ) -> &'ll DILexicalBlock {
1605 let file_metadata = file_metadata(cx, file);
1606 unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) }
1607 }
1608
1609 pub fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1610 const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1611 "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1612 "__12", "__13", "__14", "__15",
1613 ];
1614 TUPLE_FIELD_NAMES
1615 .get(field_index)
1616 .map(|s| Cow::from(*s))
1617 .unwrap_or_else(|| Cow::from(format!("__{}", field_index)))
1618 }