1 //===- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing dwarf debug info into asm files.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
16 #include "AddressPool.h"
17 #include "DebugLocEntry.h"
18 #include "DebugLocStream.h"
19 #include "DwarfFile.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/MapVector.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/BinaryFormat/Dwarf.h"
29 #include "llvm/CodeGen/AccelTable.h"
30 #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
31 #include "llvm/CodeGen/DebugHandlerBase.h"
32 #include "llvm/IR/DebugInfoMetadata.h"
33 #include "llvm/IR/DebugLoc.h"
34 #include "llvm/IR/Metadata.h"
35 #include "llvm/MC/MCDwarf.h"
36 #include "llvm/Support/Allocator.h"
37 #include "llvm/Target/TargetOptions.h"
51 class DwarfCompileUnit
;
52 class DwarfExpression
;
56 class MachineFunction
;
61 //===----------------------------------------------------------------------===//
62 /// This class is defined as the common parent of DbgVariable and DbgLabel
63 /// such that it could levarage polymorphism to extract common code for
64 /// DbgVariable and DbgLabel.
74 const DILocation
*InlinedAt
;
75 DIE
*TheDIE
= nullptr;
76 const DbgEntityKind SubclassID
;
79 DbgEntity(const DINode
*N
, const DILocation
*IA
, DbgEntityKind ID
)
80 : Entity(N
), InlinedAt(IA
), SubclassID(ID
) {}
81 virtual ~DbgEntity() = default;
85 const DINode
*getEntity() const { return Entity
; }
86 const DILocation
*getInlinedAt() const { return InlinedAt
; }
87 DIE
*getDIE() const { return TheDIE
; }
88 DbgEntityKind
getDbgEntityID() const { return SubclassID
; }
91 void setDIE(DIE
&D
) { TheDIE
= &D
; }
93 static bool classof(const DbgEntity
*N
) {
94 switch (N
->getDbgEntityID()) {
99 llvm_unreachable("Invalid DbgEntityKind");
105 bool operator<(const struct FrameIndexExpr
&LHS
,
106 const struct FrameIndexExpr
&RHS
);
107 bool operator<(const struct EntryValueInfo
&LHS
,
108 const struct EntryValueInfo
&RHS
);
110 /// Proxy for one MMI entry.
111 struct FrameIndexExpr
{
113 const DIExpression
*Expr
;
115 /// Operator enabling sorting based on fragment offset.
116 friend bool operator<(const FrameIndexExpr
&LHS
, const FrameIndexExpr
&RHS
);
119 /// Represents an entry-value location, or a fragment of one.
120 struct EntryValueInfo
{
122 const DIExpression
&Expr
;
124 /// Operator enabling sorting based on fragment offset.
125 friend bool operator<(const EntryValueInfo
&LHS
, const EntryValueInfo
&RHS
);
128 // Namespace for alternatives of a DbgVariable.
130 /// Single value location description.
132 std::unique_ptr
<DbgValueLoc
> ValueLoc
;
133 const DIExpression
*Expr
;
136 explicit Single(DbgValueLoc ValueLoc
);
137 explicit Single(const MachineInstr
*DbgValue
);
138 const DbgValueLoc
&getValueLoc() const { return *ValueLoc
; }
139 const DIExpression
*getExpr() const { return Expr
; }
141 /// Multi-value location description.
143 /// Index of the entry list in DebugLocs.
144 unsigned DebugLocListIndex
;
145 /// DW_OP_LLVM_tag_offset value from DebugLocs.
146 std::optional
<uint8_t> DebugLocListTagOffset
;
149 explicit Multi(unsigned DebugLocListIndex
,
150 std::optional
<uint8_t> DebugLocListTagOffset
)
151 : DebugLocListIndex(DebugLocListIndex
),
152 DebugLocListTagOffset(DebugLocListTagOffset
) {}
153 unsigned getDebugLocListIndex() const { return DebugLocListIndex
; }
154 std::optional
<uint8_t> getDebugLocListTagOffset() const {
155 return DebugLocListTagOffset
;
158 /// Single location defined by (potentially multiple) MMI entries.
160 std::set
<FrameIndexExpr
> FrameIndexExprs
;
163 explicit MMI(const DIExpression
*E
, int FI
) : FrameIndexExprs({{FI
, E
}}) {
164 assert((!E
|| E
->isValid()) && "Expected valid expression");
165 assert(FI
!= std::numeric_limits
<int>::max() && "Expected valid index");
167 void addFrameIndexExpr(const DIExpression
*Expr
, int FI
);
168 /// Get the FI entries, sorted by fragment offset.
169 const std::set
<FrameIndexExpr
> &getFrameIndexExprs() const;
171 /// Single location defined by (potentially multiple) EntryValueInfo.
173 std::set
<EntryValueInfo
> EntryValues
;
174 explicit EntryValue(MCRegister Reg
, const DIExpression
&Expr
) {
177 // Add the pair Reg, Expr to the list of entry values describing the variable.
178 // If multiple expressions are added, it is the callers responsibility to
179 // ensure they are all non-overlapping fragments.
180 void addExpr(MCRegister Reg
, const DIExpression
&Expr
) {
181 std::optional
<const DIExpression
*> NonVariadicExpr
=
182 DIExpression::convertToNonVariadicExpression(&Expr
);
183 assert(NonVariadicExpr
&& *NonVariadicExpr
);
185 EntryValues
.insert({Reg
, **NonVariadicExpr
});
188 /// Alias for the std::variant specialization base class of DbgVariable.
189 using Variant
= std::variant
<std::monostate
, Loc::Single
, Loc::Multi
, Loc::MMI
,
193 //===----------------------------------------------------------------------===//
194 /// This class is used to track local variable information.
196 /// Variables that have been optimized out hold the \c monostate alternative.
197 /// This is not distinguished from the case of a constructed \c DbgVariable
198 /// which has not be initialized yet.
200 /// Variables can be created from allocas, in which case they're generated from
201 /// the MMI table. Such variables hold the \c Loc::MMI alternative which can
202 /// have multiple expressions and frame indices.
204 /// Variables can be created from the entry value of registers, in which case
205 /// they're generated from the MMI table. Such variables hold the \c
206 /// EntryValueLoc alternative which can either have a single expression or
207 /// multiple *fragment* expressions.
209 /// Variables can be created from \c DBG_VALUE instructions. Those whose
210 /// location changes over time hold a \c Loc::Multi alternative which uses \c
211 /// DebugLocListIndex and (optionally) \c DebugLocListTagOffset, while those
212 /// with a single location hold a \c Loc::Single alternative which use \c
213 /// ValueLoc and (optionally) a single \c Expr.
214 class DbgVariable
: public DbgEntity
, public Loc::Variant
{
217 /// To workaround P2162R0 https://github.com/cplusplus/papers/issues/873 the
218 /// base class subobject needs to be passed directly to std::visit, so expose
219 /// it directly here.
220 Loc::Variant
&asVariant() { return *static_cast<Loc::Variant
*>(this); }
221 const Loc::Variant
&asVariant() const {
222 return *static_cast<const Loc::Variant
*>(this);
224 /// Member shorthand for std::holds_alternative
225 template <typename T
> bool holds() const {
226 return std::holds_alternative
<T
>(*this);
228 /// Asserting, noexcept member alternative to std::get
229 template <typename T
> auto &get() noexcept
{
231 return *std::get_if
<T
>(this);
233 /// Asserting, noexcept member alternative to std::get
234 template <typename T
> const auto &get() const noexcept
{
236 return *std::get_if
<T
>(this);
239 /// Construct a DbgVariable.
241 /// Creates a variable without any DW_AT_location.
242 DbgVariable(const DILocalVariable
*V
, const DILocation
*IA
)
243 : DbgEntity(V
, IA
, DbgVariableKind
) {}
246 const DILocalVariable
*getVariable() const {
247 return cast
<DILocalVariable
>(getEntity());
250 StringRef
getName() const { return getVariable()->getName(); }
252 // Translate tag to proper Dwarf tag.
253 dwarf::Tag
getTag() const {
254 // FIXME: Why don't we just infer this tag and store it all along?
255 if (getVariable()->isParameter())
256 return dwarf::DW_TAG_formal_parameter
;
258 return dwarf::DW_TAG_variable
;
261 /// Return true if DbgVariable is artificial.
262 bool isArtificial() const {
263 if (getVariable()->isArtificial())
265 if (getType()->isArtificial())
270 bool isObjectPointer() const {
271 if (getVariable()->isObjectPointer())
273 if (getType()->isObjectPointer())
278 const DIType
*getType() const;
280 static bool classof(const DbgEntity
*N
) {
281 return N
->getDbgEntityID() == DbgVariableKind
;
285 //===----------------------------------------------------------------------===//
286 /// This class is used to track label information.
288 /// Labels are collected from \c DBG_LABEL instructions.
289 class DbgLabel
: public DbgEntity
{
290 const MCSymbol
*Sym
; /// Symbol before DBG_LABEL instruction.
293 /// We need MCSymbol information to generate DW_AT_low_pc.
294 DbgLabel(const DILabel
*L
, const DILocation
*IA
, const MCSymbol
*Sym
= nullptr)
295 : DbgEntity(L
, IA
, DbgLabelKind
), Sym(Sym
) {}
299 const DILabel
*getLabel() const { return cast
<DILabel
>(getEntity()); }
300 const MCSymbol
*getSymbol() const { return Sym
; }
302 StringRef
getName() const { return getLabel()->getName(); }
305 /// Translate tag to proper Dwarf tag.
306 dwarf::Tag
getTag() const {
307 return dwarf::DW_TAG_label
;
310 static bool classof(const DbgEntity
*N
) {
311 return N
->getDbgEntityID() == DbgLabelKind
;
315 /// Used for tracking debug info about call site parameters.
316 class DbgCallSiteParam
{
318 unsigned Register
; ///< Parameter register at the callee entry point.
319 DbgValueLoc Value
; ///< Corresponding location for the parameter value at
322 DbgCallSiteParam(unsigned Reg
, DbgValueLoc Val
)
323 : Register(Reg
), Value(Val
) {
324 assert(Reg
&& "Parameter register cannot be undef");
327 unsigned getRegister() const { return Register
; }
328 DbgValueLoc
getValue() const { return Value
; }
331 /// Collection used for storing debug call site parameters.
332 using ParamSet
= SmallVector
<DbgCallSiteParam
, 4>;
334 /// Helper used to pair up a symbol and its DWARF compile unit.
336 SymbolCU(DwarfCompileUnit
*CU
, const MCSymbol
*Sym
) : Sym(Sym
), CU(CU
) {}
339 DwarfCompileUnit
*CU
;
342 /// The kind of accelerator tables we should emit.
343 enum class AccelTableKind
{
344 Default
, ///< Platform default.
346 Apple
, ///< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
347 Dwarf
, ///< DWARF v5 .debug_names.
350 /// Collects and handles dwarf debug information.
351 class DwarfDebug
: public DebugHandlerBase
{
352 /// All DIEValues are allocated through this allocator.
353 BumpPtrAllocator DIEValueAllocator
;
355 /// Maps MDNode with its corresponding DwarfCompileUnit.
356 MapVector
<const MDNode
*, DwarfCompileUnit
*> CUMap
;
358 /// Maps a CU DIE with its corresponding DwarfCompileUnit.
359 DenseMap
<const DIE
*, DwarfCompileUnit
*> CUDieMap
;
361 /// List of all labels used in aranges generation.
362 std::vector
<SymbolCU
> ArangeLabels
;
364 /// Size of each symbol emitted (for those symbols that have a specific size).
365 DenseMap
<const MCSymbol
*, uint64_t> SymSize
;
367 /// Collection of abstract variables/labels.
368 SmallVector
<std::unique_ptr
<DbgEntity
>, 64> ConcreteEntities
;
370 /// Collection of DebugLocEntry. Stored in a linked list so that DIELocLists
371 /// can refer to them in spite of insertions into this list.
372 DebugLocStream DebugLocs
;
374 /// This is a collection of subprogram MDNodes that are processed to
376 SmallSetVector
<const DISubprogram
*, 16> ProcessedSPNodes
;
378 /// Map function-local imported entities to their parent local scope
379 /// (either DILexicalBlock or DISubprogram) for a processed function
380 /// (including inlined subprograms).
381 using MDNodeSet
= SetVector
<const MDNode
*, SmallVector
<const MDNode
*, 2>,
382 SmallPtrSet
<const MDNode
*, 2>>;
383 DenseMap
<const DILocalScope
*, MDNodeSet
> LocalDeclsPerLS
;
385 SmallDenseSet
<const MachineInstr
*> ForceIsStmtInstrs
;
387 /// If nonnull, stores the current machine function we're processing.
388 const MachineFunction
*CurFn
= nullptr;
390 /// If nonnull, stores the CU in which the previous subprogram was contained.
391 const DwarfCompileUnit
*PrevCU
= nullptr;
393 /// As an optimization, there is no need to emit an entry in the directory
394 /// table for the same directory as DW_AT_comp_dir.
395 StringRef CompilationDir
;
397 /// Holder for the file specific debug information.
398 DwarfFile InfoHolder
;
400 /// Holders for the various debug information flags that we might need to
401 /// have exposed. See accessor functions below for description.
403 /// Map from MDNodes for user-defined types to their type signatures. Also
404 /// used to keep track of which types we have emitted type units for.
405 DenseMap
<const MDNode
*, uint64_t> TypeSignatures
;
407 DenseMap
<const MCSection
*, const MCSymbol
*> SectionLabels
;
410 std::pair
<std::unique_ptr
<DwarfTypeUnit
>, const DICompositeType
*>, 1>
411 TypeUnitsUnderConstruction
;
413 /// Symbol pointing to the current function's DWARF line table entries.
414 MCSymbol
*FunctionLineTableLabel
;
416 /// Used to set a uniqe ID for a Type Unit.
417 /// This counter represents number of DwarfTypeUnits created, not necessarily
418 /// number of type units that will be emitted.
419 unsigned NumTypeUnitsCreated
= 0;
421 /// Whether to use the GNU TLS opcode (instead of the standard opcode).
422 bool UseGNUTLSOpcode
;
424 /// Whether to use DWARF 2 bitfields (instead of the DWARF 4 format).
425 bool UseDWARF2Bitfields
;
427 /// Whether to emit all linkage names, or just abstract subprograms.
428 bool UseAllLinkageNames
;
430 /// Use inlined strings.
431 bool UseInlineStrings
= false;
433 /// Allow emission of .debug_ranges section.
434 bool UseRangesSection
= true;
436 /// True if the sections itself must be used as references and don't create
437 /// temp symbols inside DWARF sections.
438 bool UseSectionsAsReferences
= false;
440 /// Allow emission of .debug_aranges section
441 bool UseARangesSection
= false;
443 /// Generate DWARF v4 type units.
444 bool GenerateTypeUnits
;
446 /// Emit a .debug_macro section instead of .debug_macinfo.
447 bool UseDebugMacroSection
;
449 /// Avoid using DW_OP_convert due to consumer incompatibilities.
450 bool EnableOpConvert
;
453 enum class MinimizeAddrInV5
{
461 enum class DWARF5AccelTableKind
{
467 /// Force the use of DW_AT_ranges even for single-entry range lists.
468 MinimizeAddrInV5 MinimizeAddr
= MinimizeAddrInV5::Disabled
;
470 /// DWARF5 Experimental Options
472 AccelTableKind TheAccelTableKind
;
473 bool HasAppleExtensionAttributes
;
476 /// Whether to generate the DWARF v5 string offsets table.
477 /// It consists of a series of contributions, each preceded by a header.
478 /// The pre-DWARF v5 string offsets table for split dwarf is, in contrast,
479 /// a monolithic sequence of string offsets.
480 bool UseSegmentedStringOffsetsTable
;
482 /// Enable production of call site parameters needed to print the debug entry
483 /// values. Useful for testing purposes when a debugger does not support the
485 bool EmitDebugEntryValues
;
487 /// Separated Dwarf Variables
488 /// In general these will all be for bits that are left in the
489 /// original object file, rather than things that are meant
490 /// to be in the .dwo sections.
492 /// Holder for the skeleton information.
493 DwarfFile SkeletonHolder
;
495 /// Store file names for type units under fission in a line table
496 /// header that will be emitted into debug_line.dwo.
497 // FIXME: replace this with a map from comp_dir to table so that we
498 // can emit multiple tables during LTO each of which uses directory
499 // 0, referencing the comp_dir of all the type units that use it.
500 MCDwarfDwoLineTable SplitTypeUnitFileTable
;
503 /// True iff there are multiple CUs in this module.
507 /// Map for tracking Fortran deferred CHARACTER lengths.
508 DenseMap
<const DIStringType
*, unsigned> StringTypeLocMap
;
510 AddressPool AddrPool
;
512 /// Accelerator tables.
513 DWARF5AccelTable AccelDebugNames
;
514 DWARF5AccelTable AccelTypeUnitsDebugNames
;
515 /// Used to hide which DWARF5AccelTable we are using now.
516 DWARF5AccelTable
*CurrentDebugNames
= &AccelDebugNames
;
517 AccelTable
<AppleAccelTableOffsetData
> AccelNames
;
518 AccelTable
<AppleAccelTableOffsetData
> AccelObjC
;
519 AccelTable
<AppleAccelTableOffsetData
> AccelNamespace
;
520 AccelTable
<AppleAccelTableTypeData
> AccelTypes
;
522 /// Identify a debugger for "tuning" the debug info.
524 /// The "tuning" should be used to set defaults for individual feature flags
525 /// in DwarfDebug; if a given feature has a more specific command-line option,
526 /// that option should take precedence over the tuning.
527 DebuggerKind DebuggerTuning
= DebuggerKind::Default
;
529 MCDwarfDwoLineTable
*getDwoLineTable(const DwarfCompileUnit
&);
531 const SmallVectorImpl
<std::unique_ptr
<DwarfCompileUnit
>> &getUnits() {
532 return InfoHolder
.getUnits();
535 using InlinedEntity
= DbgValueHistoryMap::InlinedEntity
;
537 void ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit
&CU
,
539 const MDNode
*Scope
);
541 DbgEntity
*createConcreteEntity(DwarfCompileUnit
&TheCU
,
544 const DILocation
*Location
,
545 const MCSymbol
*Sym
= nullptr);
547 /// Construct a DIE for this abstract scope.
548 void constructAbstractSubprogramScopeDIE(DwarfCompileUnit
&SrcCU
, LexicalScope
*Scope
);
550 /// Construct DIEs for call site entries describing the calls in \p MF.
551 void constructCallSiteEntryDIEs(const DISubprogram
&SP
, DwarfCompileUnit
&CU
,
552 DIE
&ScopeDIE
, const MachineFunction
&MF
);
554 template <typename DataT
>
555 void addAccelNameImpl(const DwarfUnit
&Unit
,
556 const DICompileUnit::DebugNameTableKind NameTableKind
,
557 AccelTable
<DataT
> &AppleAccel
, StringRef Name
,
560 void finishEntityDefinitions();
562 void finishSubprogramDefinitions();
564 /// Finish off debug information after all functions have been
566 void finalizeModuleInfo();
568 /// Emit the debug info section.
569 void emitDebugInfo();
571 /// Emit the abbreviation section.
572 void emitAbbreviations();
574 /// Emit the string offsets table header.
575 void emitStringOffsetsTableHeader();
577 /// Emit a specified accelerator table.
578 template <typename AccelTableT
>
579 void emitAccel(AccelTableT
&Accel
, MCSection
*Section
, StringRef TableName
);
581 /// Emit DWARF v5 accelerator table.
582 void emitAccelDebugNames();
584 /// Emit visible names into a hashed accelerator table section.
585 void emitAccelNames();
587 /// Emit objective C classes and categories into a hashed
588 /// accelerator table section.
589 void emitAccelObjC();
591 /// Emit namespace dies into a hashed accelerator table.
592 void emitAccelNamespaces();
594 /// Emit type dies into a hashed accelerator table.
595 void emitAccelTypes();
597 /// Emit visible names and types into debug pubnames and pubtypes sections.
598 void emitDebugPubSections();
600 void emitDebugPubSection(bool GnuStyle
, StringRef Name
,
601 DwarfCompileUnit
*TheU
,
602 const StringMap
<const DIE
*> &Globals
);
604 /// Emit null-terminated strings into a debug str section.
607 /// Emit variable locations into a debug loc section.
610 /// Emit variable locations into a debug loc dwo section.
611 void emitDebugLocDWO();
613 void emitDebugLocImpl(MCSection
*Sec
);
615 /// Emit address ranges into a debug aranges section.
616 void emitDebugARanges();
618 /// Emit address ranges into a debug ranges section.
619 void emitDebugRanges();
620 void emitDebugRangesDWO();
621 void emitDebugRangesImpl(const DwarfFile
&Holder
, MCSection
*Section
);
623 /// Emit macros into a debug macinfo section.
624 void emitDebugMacinfo();
625 /// Emit macros into a debug macinfo.dwo section.
626 void emitDebugMacinfoDWO();
627 void emitDebugMacinfoImpl(MCSection
*Section
);
628 void emitMacro(DIMacro
&M
);
629 void emitMacroFile(DIMacroFile
&F
, DwarfCompileUnit
&U
);
630 void emitMacroFileImpl(DIMacroFile
&F
, DwarfCompileUnit
&U
,
631 unsigned StartFile
, unsigned EndFile
,
632 StringRef (*MacroFormToString
)(unsigned Form
));
633 void handleMacroNodes(DIMacroNodeArray Nodes
, DwarfCompileUnit
&U
);
635 /// DWARF 5 Experimental Split Dwarf Emitters
637 /// Initialize common features of skeleton units.
638 void initSkeletonUnit(const DwarfUnit
&U
, DIE
&Die
,
639 std::unique_ptr
<DwarfCompileUnit
> NewU
);
641 /// Construct the split debug info compile unit for the debug info section.
642 /// In DWARF v5, the skeleton unit DIE may have the following attributes:
643 /// DW_AT_addr_base, DW_AT_comp_dir, DW_AT_dwo_name, DW_AT_high_pc,
644 /// DW_AT_low_pc, DW_AT_ranges, DW_AT_stmt_list, and DW_AT_str_offsets_base.
645 /// Prior to DWARF v5 it may also have DW_AT_GNU_dwo_id. DW_AT_GNU_dwo_name
646 /// is used instead of DW_AT_dwo_name, Dw_AT_GNU_addr_base instead of
647 /// DW_AT_addr_base, and DW_AT_GNU_ranges_base instead of DW_AT_rnglists_base.
648 DwarfCompileUnit
&constructSkeletonCU(const DwarfCompileUnit
&CU
);
650 /// Emit the debug info dwo section.
651 void emitDebugInfoDWO();
653 /// Emit the debug abbrev dwo section.
654 void emitDebugAbbrevDWO();
656 /// Emit the debug line dwo section.
657 void emitDebugLineDWO();
659 /// Emit the dwo stringoffsets table header.
660 void emitStringOffsetsTableHeaderDWO();
662 /// Emit the debug str dwo section.
663 void emitDebugStrDWO();
665 /// Emit DWO addresses.
666 void emitDebugAddr();
668 /// Flags to let the linker know we have emitted new style pubnames. Only
669 /// emit it here if we don't have a skeleton CU for split dwarf.
670 void addGnuPubAttributes(DwarfCompileUnit
&U
, DIE
&D
) const;
672 /// Create new DwarfCompileUnit for the given metadata node with tag
673 /// DW_TAG_compile_unit.
674 DwarfCompileUnit
&getOrCreateDwarfCompileUnit(const DICompileUnit
*DIUnit
);
675 void finishUnitAttributes(const DICompileUnit
*DIUnit
,
676 DwarfCompileUnit
&NewCU
);
678 /// Register a source line with debug info. Returns the unique
679 /// label that was emitted and which provides correspondence to the
680 /// source line list.
681 void recordSourceLine(unsigned Line
, unsigned Col
, const MDNode
*Scope
,
684 /// Populate LexicalScope entries with variables' info.
685 void collectEntityInfo(DwarfCompileUnit
&TheCU
, const DISubprogram
*SP
,
686 DenseSet
<InlinedEntity
> &ProcessedVars
);
688 /// Build the location list for all DBG_VALUEs in the
689 /// function that describe the same variable. If the resulting
690 /// list has only one entry that is valid for entire variable's
691 /// scope return true.
692 bool buildLocationList(SmallVectorImpl
<DebugLocEntry
> &DebugLoc
,
693 const DbgValueHistoryMap::Entries
&Entries
);
695 /// Collect variable information from the side table maintained by MF.
696 void collectVariableInfoFromMFTable(DwarfCompileUnit
&TheCU
,
697 DenseSet
<InlinedEntity
> &P
);
699 /// Emit the reference to the section.
700 void emitSectionReference(const DwarfCompileUnit
&CU
);
702 void findForceIsStmtInstrs(const MachineFunction
*MF
);
705 /// Gather pre-function debug information.
706 void beginFunctionImpl(const MachineFunction
*MF
) override
;
708 /// Gather and emit post-function debug information.
709 void endFunctionImpl(const MachineFunction
*MF
) override
;
711 /// Get Dwarf compile unit ID for line table.
712 unsigned getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit
&CU
);
714 void skippedNonDebugFunction() override
;
717 //===--------------------------------------------------------------------===//
718 // Main entry points.
720 DwarfDebug(AsmPrinter
*A
);
722 ~DwarfDebug() override
;
724 /// Emit all Dwarf sections that should come prior to the
726 void beginModule(Module
*M
) override
;
728 /// Emit all Dwarf sections that should come after the content.
729 void endModule() override
;
731 /// Emits inital debug location directive. Returns instruction at which
732 /// the function prologue ends.
733 const MachineInstr
*emitInitialLocDirective(const MachineFunction
&MF
,
736 /// Process beginning of an instruction.
737 void beginInstruction(const MachineInstr
*MI
) override
;
739 /// Process beginning of code alignment.
740 void beginCodeAlignment(const MachineBasicBlock
&MBB
) override
;
742 /// Perform an MD5 checksum of \p Identifier and return the lower 64 bits.
743 static uint64_t makeTypeSignature(StringRef Identifier
);
745 /// Add a DIE to the set of types that we're going to pull into
747 void addDwarfTypeUnitType(DwarfCompileUnit
&CU
, StringRef Identifier
,
748 DIE
&Die
, const DICompositeType
*CTy
);
750 /// Add a label so that arange data can be generated for it.
751 void addArangeLabel(SymbolCU SCU
) { ArangeLabels
.push_back(SCU
); }
753 /// For symbols that have a size designated (e.g. common symbols),
754 /// this tracks that size.
755 void setSymbolSize(const MCSymbol
*Sym
, uint64_t Size
) override
{
759 /// Returns whether we should emit all DW_AT_[MIPS_]linkage_name.
760 /// If not, we still might emit certain cases.
761 bool useAllLinkageNames() const { return UseAllLinkageNames
; }
763 /// Returns whether to use DW_OP_GNU_push_tls_address, instead of the
764 /// standard DW_OP_form_tls_address opcode
765 bool useGNUTLSOpcode() const { return UseGNUTLSOpcode
; }
767 /// Returns whether to use the DWARF2 format for bitfields instyead of the
769 bool useDWARF2Bitfields() const { return UseDWARF2Bitfields
; }
771 /// Returns whether to use inline strings.
772 bool useInlineStrings() const { return UseInlineStrings
; }
774 /// Returns whether ranges section should be emitted.
775 bool useRangesSection() const { return UseRangesSection
; }
777 /// Returns whether range encodings should be used for single entry range
779 bool alwaysUseRanges(const DwarfCompileUnit
&) const;
781 // Returns whether novel exprloc addrx+offset encodings should be used to
782 // reduce debug_addr size.
783 bool useAddrOffsetExpressions() const {
784 return MinimizeAddr
== MinimizeAddrInV5::Expressions
;
787 // Returns whether addrx+offset LLVM extension form should be used to reduce
789 bool useAddrOffsetForm() const {
790 return MinimizeAddr
== MinimizeAddrInV5::Form
;
793 /// Returns whether to use sections as labels rather than temp symbols.
794 bool useSectionsAsReferences() const {
795 return UseSectionsAsReferences
;
798 /// Returns whether to generate DWARF v4 type units.
799 bool generateTypeUnits() const { return GenerateTypeUnits
; }
801 // Experimental DWARF5 features.
803 /// Returns what kind (if any) of accelerator tables to emit.
804 AccelTableKind
getAccelTableKind() const { return TheAccelTableKind
; }
806 /// Seet TheAccelTableKind
807 void setTheAccelTableKind(AccelTableKind K
) { TheAccelTableKind
= K
; };
809 bool useAppleExtensionAttributes() const {
810 return HasAppleExtensionAttributes
;
813 /// Returns whether or not to change the current debug info for the
814 /// split dwarf proposal support.
815 bool useSplitDwarf() const { return HasSplitDwarf
; }
817 /// Returns whether to generate a string offsets table with (possibly shared)
818 /// contributions from each CU and type unit. This implies the use of
819 /// DW_FORM_strx* indirect references with DWARF v5 and beyond. Note that
820 /// DW_FORM_GNU_str_index is also an indirect reference, but it is used with
821 /// a pre-DWARF v5 implementation of split DWARF sections, which uses a
822 /// monolithic string offsets table.
823 bool useSegmentedStringOffsetsTable() const {
824 return UseSegmentedStringOffsetsTable
;
827 bool emitDebugEntryValues() const {
828 return EmitDebugEntryValues
;
831 bool useOpConvert() const {
832 return EnableOpConvert
;
835 bool shareAcrossDWOCUs() const;
837 /// Returns the Dwarf Version.
838 uint16_t getDwarfVersion() const;
840 /// Returns a suitable DWARF form to represent a section offset, i.e.
841 /// * DW_FORM_sec_offset for DWARF version >= 4;
842 /// * DW_FORM_data8 for 64-bit DWARFv3;
843 /// * DW_FORM_data4 for 32-bit DWARFv3 and DWARFv2.
844 dwarf::Form
getDwarfSectionOffsetForm() const;
846 /// Returns the previous CU that was being updated
847 const DwarfCompileUnit
*getPrevCU() const { return PrevCU
; }
848 void setPrevCU(const DwarfCompileUnit
*PrevCU
) { this->PrevCU
= PrevCU
; }
850 /// Terminate the line table by adding the last range label.
851 void terminateLineTable(const DwarfCompileUnit
*CU
);
853 /// Returns the entries for the .debug_loc section.
854 const DebugLocStream
&getDebugLocs() const { return DebugLocs
; }
856 /// Emit an entry for the debug loc section. This can be used to
857 /// handle an entry that's going to be emitted into the debug loc section.
858 void emitDebugLocEntry(ByteStreamer
&Streamer
,
859 const DebugLocStream::Entry
&Entry
,
860 const DwarfCompileUnit
*CU
);
862 /// Emit the location for a debug loc entry, including the size header.
863 void emitDebugLocEntryLocation(const DebugLocStream::Entry
&Entry
,
864 const DwarfCompileUnit
*CU
);
866 void addSubprogramNames(const DwarfUnit
&Unit
,
867 const DICompileUnit::DebugNameTableKind NameTableKind
,
868 const DISubprogram
*SP
, DIE
&Die
);
870 AddressPool
&getAddressPool() { return AddrPool
; }
872 void addAccelName(const DwarfUnit
&Unit
,
873 const DICompileUnit::DebugNameTableKind NameTableKind
,
874 StringRef Name
, const DIE
&Die
);
876 void addAccelObjC(const DwarfUnit
&Unit
,
877 const DICompileUnit::DebugNameTableKind NameTableKind
,
878 StringRef Name
, const DIE
&Die
);
880 void addAccelNamespace(const DwarfUnit
&Unit
,
881 const DICompileUnit::DebugNameTableKind NameTableKind
,
882 StringRef Name
, const DIE
&Die
);
884 void addAccelType(const DwarfUnit
&Unit
,
885 const DICompileUnit::DebugNameTableKind NameTableKind
,
886 StringRef Name
, const DIE
&Die
, char Flags
);
888 const MachineFunction
*getCurrentFunction() const { return CurFn
; }
890 /// A helper function to check whether the DIE for a given Scope is
891 /// going to be null.
892 bool isLexicalScopeDIENull(LexicalScope
*Scope
);
894 /// Find the matching DwarfCompileUnit for the given CU DIE.
895 DwarfCompileUnit
*lookupCU(const DIE
*Die
) { return CUDieMap
.lookup(Die
); }
896 const DwarfCompileUnit
*lookupCU(const DIE
*Die
) const {
897 return CUDieMap
.lookup(Die
);
900 unsigned getStringTypeLoc(const DIStringType
*ST
) const {
901 return StringTypeLocMap
.lookup(ST
);
904 void addStringTypeLoc(const DIStringType
*ST
, unsigned Loc
) {
907 StringTypeLocMap
[ST
] = Loc
;
910 /// \defgroup DebuggerTuning Predicates to tune DWARF for a given debugger.
912 /// Returns whether we are "tuning" for a given debugger.
914 bool tuneForGDB() const { return DebuggerTuning
== DebuggerKind::GDB
; }
915 bool tuneForLLDB() const { return DebuggerTuning
== DebuggerKind::LLDB
; }
916 bool tuneForSCE() const { return DebuggerTuning
== DebuggerKind::SCE
; }
917 bool tuneForDBX() const { return DebuggerTuning
== DebuggerKind::DBX
; }
920 const MCSymbol
*getSectionLabel(const MCSection
*S
);
921 void insertSectionLabel(const MCSymbol
*S
);
923 static void emitDebugLocValue(const AsmPrinter
&AP
, const DIBasicType
*BT
,
924 const DbgValueLoc
&Value
,
925 DwarfExpression
&DwarfExpr
);
927 /// If the \p File has an MD5 checksum, return it as an MD5Result
928 /// allocated in the MCContext.
929 std::optional
<MD5::MD5Result
> getMD5AsBytes(const DIFile
*File
) const;
931 MDNodeSet
&getLocalDeclsForScope(const DILocalScope
*S
) {
932 return LocalDeclsPerLS
[S
];
935 /// Sets the current DWARF5AccelTable to use.
936 void setCurrentDWARF5AccelTable(const DWARF5AccelTableKind Kind
) {
938 case DWARF5AccelTableKind::CU
:
939 CurrentDebugNames
= &AccelDebugNames
;
941 case DWARF5AccelTableKind::TU
:
942 CurrentDebugNames
= &AccelTypeUnitsDebugNames
;
945 /// Returns either CU or TU DWARF5AccelTable.
946 DWARF5AccelTable
&getCurrentDWARF5AccelTable() { return *CurrentDebugNames
; }
949 } // end namespace llvm
951 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H