1//===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains support for writing dwarf debug info into asm files. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H 15#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H 16 17#include "AsmPrinterHandler.h" 18#include "DbgValueHistoryCalculator.h" 19#include "DebugLocStream.h" 20#include "DwarfAccelTable.h" 21#include "DwarfFile.h" 22#include "llvm/ADT/DenseMap.h" 23#include "llvm/ADT/DenseSet.h" 24#include "llvm/ADT/FoldingSet.h" 25#include "llvm/ADT/MapVector.h" 26#include "llvm/ADT/SmallPtrSet.h" 27#include "llvm/ADT/StringMap.h" 28#include "llvm/CodeGen/DIE.h" 29#include "llvm/CodeGen/LexicalScopes.h" 30#include "llvm/CodeGen/MachineInstr.h" 31#include "llvm/IR/DebugInfo.h" 32#include "llvm/IR/DebugLoc.h" 33#include "llvm/MC/MCDwarf.h" 34#include "llvm/MC/MachineLocation.h" 35#include "llvm/Support/Allocator.h" 36#include "llvm/Target/TargetOptions.h" 37#include <memory> 38 39namespace llvm { 40 41class AsmPrinter; 42class ByteStreamer; 43class ConstantInt; 44class ConstantFP; 45class DebugLocEntry; 46class DwarfCompileUnit; 47class DwarfDebug; 48class DwarfTypeUnit; 49class DwarfUnit; 50class MachineModuleInfo; 51 52//===----------------------------------------------------------------------===// 53/// This class is used to track local variable information. 54/// 55/// Variables can be created from allocas, in which case they're generated from 56/// the MMI table. Such variables can have multiple expressions and frame 57/// indices. The \a Expr and \a FrameIndices array must match. 58/// 59/// Variables can be created from \c DBG_VALUE instructions. Those whose 60/// location changes over time use \a DebugLocListIndex, while those with a 61/// single instruction use \a MInsn and (optionally) a single entry of \a Expr. 62/// 63/// Variables that have been optimized out use none of these fields. 64class DbgVariable { 65 const DILocalVariable *Var; /// Variable Descriptor. 66 const DILocation *IA; /// Inlined at location. 67 SmallVector<const DIExpression *, 1> Expr; /// Complex address. 68 DIE *TheDIE = nullptr; /// Variable DIE. 69 unsigned DebugLocListIndex = ~0u; /// Offset in DebugLocs. 70 const MachineInstr *MInsn = nullptr; /// DBG_VALUE instruction. 71 SmallVector<int, 1> FrameIndex; /// Frame index. 72 DwarfDebug *DD; 73 74public: 75 /// Construct a DbgVariable. 76 /// 77 /// Creates a variable without any DW_AT_location. Call \a initializeMMI() 78 /// for MMI entries, or \a initializeDbgValue() for DBG_VALUE instructions. 79 DbgVariable(const DILocalVariable *V, const DILocation *IA, DwarfDebug *DD) 80 : Var(V), IA(IA), DD(DD) {} 81 82 /// Initialize from the MMI table. 83 void initializeMMI(const DIExpression *E, int FI) { 84 assert(Expr.empty() && "Already initialized?"); 85 assert(FrameIndex.empty() && "Already initialized?"); 86 assert(!MInsn && "Already initialized?"); 87 88 assert((!E || E->isValid()) && "Expected valid expression"); 89 assert(~FI && "Expected valid index"); 90 91 Expr.push_back(E); 92 FrameIndex.push_back(FI); 93 } 94 95 /// Initialize from a DBG_VALUE instruction. 96 void initializeDbgValue(const MachineInstr *DbgValue) { 97 assert(Expr.empty() && "Already initialized?"); 98 assert(FrameIndex.empty() && "Already initialized?"); 99 assert(!MInsn && "Already initialized?"); 100 101 assert(Var == DbgValue->getDebugVariable() && "Wrong variable"); 102 assert(IA == DbgValue->getDebugLoc()->getInlinedAt() && "Wrong inlined-at"); 103 104 MInsn = DbgValue; 105 if (auto *E = DbgValue->getDebugExpression()) 106 if (E->getNumElements()) 107 Expr.push_back(E); 108 } 109 110 // Accessors. 111 const DILocalVariable *getVariable() const { return Var; } 112 const DILocation *getInlinedAt() const { return IA; } 113 ArrayRef<const DIExpression *> getExpression() const { return Expr; } 114 void setDIE(DIE &D) { TheDIE = &D; } 115 DIE *getDIE() const { return TheDIE; } 116 void setDebugLocListIndex(unsigned O) { DebugLocListIndex = O; } 117 unsigned getDebugLocListIndex() const { return DebugLocListIndex; } 118 StringRef getName() const { return Var->getName(); } 119 const MachineInstr *getMInsn() const { return MInsn; } 120 ArrayRef<int> getFrameIndex() const { return FrameIndex; } 121 122 void addMMIEntry(const DbgVariable &V) { 123 assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry"); 124 assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry"); 125 assert(V.Var == Var && "conflicting variable"); 126 assert(V.IA == IA && "conflicting inlined-at location"); 127 128 assert(!FrameIndex.empty() && "Expected an MMI entry"); 129 assert(!V.FrameIndex.empty() && "Expected an MMI entry"); 130 assert(Expr.size() == FrameIndex.size() && "Mismatched expressions"); 131 assert(V.Expr.size() == V.FrameIndex.size() && "Mismatched expressions"); 132 133 Expr.append(V.Expr.begin(), V.Expr.end()); 134 FrameIndex.append(V.FrameIndex.begin(), V.FrameIndex.end()); 135 assert(std::all_of(Expr.begin(), Expr.end(), [](const DIExpression *E) { 136 return E && E->isBitPiece(); 137 }) && "conflicting locations for variable"); 138 } 139 140 // Translate tag to proper Dwarf tag. 141 dwarf::Tag getTag() const { 142 // FIXME: Why don't we just infer this tag and store it all along? 143 if (Var->isParameter()) 144 return dwarf::DW_TAG_formal_parameter; 145 146 return dwarf::DW_TAG_variable; 147 } 148 /// Return true if DbgVariable is artificial. 149 bool isArtificial() const { 150 if (Var->isArtificial()) 151 return true; 152 if (getType()->isArtificial()) 153 return true; 154 return false; 155 } 156 157 bool isObjectPointer() const { 158 if (Var->isObjectPointer()) 159 return true; 160 if (getType()->isObjectPointer()) 161 return true; 162 return false; 163 } 164 165 bool hasComplexAddress() const { 166 assert(MInsn && "Expected DBG_VALUE, not MMI variable"); 167 assert(FrameIndex.empty() && "Expected DBG_VALUE, not MMI variable"); 168 assert( 169 (Expr.empty() || (Expr.size() == 1 && Expr.back()->getNumElements())) && 170 "Invalid Expr for DBG_VALUE"); 171 return !Expr.empty(); 172 } 173 bool isBlockByrefVariable() const; 174 const DIType *getType() const; 175 176private: 177 /// Look in the DwarfDebug map for the MDNode that 178 /// corresponds to the reference. 179 template <typename T> T *resolve(TypedDINodeRef<T> Ref) const; 180}; 181 182 183/// Helper used to pair up a symbol and its DWARF compile unit. 184struct SymbolCU { 185 SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {} 186 const MCSymbol *Sym; 187 DwarfCompileUnit *CU; 188}; 189 190/// Collects and handles dwarf debug information. 191class DwarfDebug : public AsmPrinterHandler { 192 /// Target of Dwarf emission. 193 AsmPrinter *Asm; 194 195 /// Collected machine module information. 196 MachineModuleInfo *MMI; 197 198 /// All DIEValues are allocated through this allocator. 199 BumpPtrAllocator DIEValueAllocator; 200 201 /// Maps MDNode with its corresponding DwarfCompileUnit. 202 MapVector<const MDNode *, DwarfCompileUnit *> CUMap; 203 204 /// Maps subprogram MDNode with its corresponding DwarfCompileUnit. 205 MapVector<const MDNode *, DwarfCompileUnit *> SPMap; 206 207 /// Maps a CU DIE with its corresponding DwarfCompileUnit. 208 DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap; 209 210 /// List of all labels used in aranges generation. 211 std::vector<SymbolCU> ArangeLabels; 212 213 /// Size of each symbol emitted (for those symbols that have a specific size). 214 DenseMap<const MCSymbol *, uint64_t> SymSize; 215 216 LexicalScopes LScopes; 217 218 /// Collection of abstract variables. 219 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables; 220 SmallVector<std::unique_ptr<DbgVariable>, 64> ConcreteVariables; 221 222 /// Collection of DebugLocEntry. Stored in a linked list so that DIELocLists 223 /// can refer to them in spite of insertions into this list. 224 DebugLocStream DebugLocs; 225 226 /// This is a collection of subprogram MDNodes that are processed to 227 /// create DIEs. 228 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes; 229 230 /// Maps instruction with label emitted before instruction. 231 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn; 232 233 /// Maps instruction with label emitted after instruction. 234 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn; 235 236 /// History of DBG_VALUE and clobber instructions for each user 237 /// variable. Variables are listed in order of appearance. 238 DbgValueHistoryMap DbgValues; 239 240 /// Previous instruction's location information. This is used to 241 /// determine label location to indicate scope boundries in dwarf 242 /// debug info. 243 DebugLoc PrevInstLoc; 244 MCSymbol *PrevLabel; 245 246 /// This location indicates end of function prologue and beginning of 247 /// function body. 248 DebugLoc PrologEndLoc; 249 250 /// If nonnull, stores the current machine function we're processing. 251 const MachineFunction *CurFn; 252 253 /// If nonnull, stores the current machine instruction we're processing. 254 const MachineInstr *CurMI; 255 256 /// If nonnull, stores the CU in which the previous subprogram was contained. 257 const DwarfCompileUnit *PrevCU; 258 259 /// As an optimization, there is no need to emit an entry in the directory 260 /// table for the same directory as DW_AT_comp_dir. 261 StringRef CompilationDir; 262 263 /// Holder for the file specific debug information. 264 DwarfFile InfoHolder; 265 266 /// Holders for the various debug information flags that we might need to 267 /// have exposed. See accessor functions below for description. 268 269 /// Map from MDNodes for user-defined types to the type units that 270 /// describe them. 271 DenseMap<const MDNode *, const DwarfTypeUnit *> DwarfTypeUnits; 272 273 SmallVector< 274 std::pair<std::unique_ptr<DwarfTypeUnit>, const DICompositeType *>, 1> 275 TypeUnitsUnderConstruction; 276 277 /// Whether to emit the pubnames/pubtypes sections. 278 bool HasDwarfPubSections; 279 280 /// Whether to use the GNU TLS opcode (instead of the standard opcode). 281 bool UseGNUTLSOpcode; 282 283 /// Whether to emit DW_AT_[MIPS_]linkage_name. 284 bool UseLinkageNames; 285 286 /// Version of dwarf we're emitting. 287 unsigned DwarfVersion; 288 289 /// Maps from a type identifier to the actual MDNode. 290 DITypeIdentifierMap TypeIdentifierMap; 291 292 /// DWARF5 Experimental Options 293 /// @{ 294 bool HasDwarfAccelTables; 295 bool HasSplitDwarf; 296 297 /// Separated Dwarf Variables 298 /// In general these will all be for bits that are left in the 299 /// original object file, rather than things that are meant 300 /// to be in the .dwo sections. 301 302 /// Holder for the skeleton information. 303 DwarfFile SkeletonHolder; 304 305 /// Store file names for type units under fission in a line table 306 /// header that will be emitted into debug_line.dwo. 307 // FIXME: replace this with a map from comp_dir to table so that we 308 // can emit multiple tables during LTO each of which uses directory 309 // 0, referencing the comp_dir of all the type units that use it. 310 MCDwarfDwoLineTable SplitTypeUnitFileTable; 311 /// @} 312 313 /// True iff there are multiple CUs in this module. 314 bool SingleCU; 315 bool IsDarwin; 316 317 AddressPool AddrPool; 318 319 DwarfAccelTable AccelNames; 320 DwarfAccelTable AccelObjC; 321 DwarfAccelTable AccelNamespace; 322 DwarfAccelTable AccelTypes; 323 324 // Identify a debugger for "tuning" the debug info. 325 DebuggerKind DebuggerTuning; 326 327 MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &); 328 329 const SmallVectorImpl<std::unique_ptr<DwarfUnit>> &getUnits() { 330 return InfoHolder.getUnits(); 331 } 332 333 typedef DbgValueHistoryMap::InlinedVariable InlinedVariable; 334 335 /// Find abstract variable associated with Var. 336 DbgVariable *getExistingAbstractVariable(InlinedVariable IV, 337 const DILocalVariable *&Cleansed); 338 DbgVariable *getExistingAbstractVariable(InlinedVariable IV); 339 void createAbstractVariable(const DILocalVariable *DV, LexicalScope *Scope); 340 void ensureAbstractVariableIsCreated(InlinedVariable Var, 341 const MDNode *Scope); 342 void ensureAbstractVariableIsCreatedIfScoped(InlinedVariable Var, 343 const MDNode *Scope); 344 345 DbgVariable *createConcreteVariable(LexicalScope &Scope, InlinedVariable IV); 346 347 /// Construct a DIE for this abstract scope. 348 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 349 350 /// Collect info for variables that were optimized out. 351 void collectDeadVariables(); 352 353 void finishVariableDefinitions(); 354 355 void finishSubprogramDefinitions(); 356 357 /// Finish off debug information after all functions have been 358 /// processed. 359 void finalizeModuleInfo(); 360 361 /// Emit the debug info section. 362 void emitDebugInfo(); 363 364 /// Emit the abbreviation section. 365 void emitAbbreviations(); 366 367 /// Emit a specified accelerator table. 368 void emitAccel(DwarfAccelTable &Accel, MCSection *Section, 369 StringRef TableName); 370 371 /// Emit visible names into a hashed accelerator table section. 372 void emitAccelNames(); 373 374 /// Emit objective C classes and categories into a hashed 375 /// accelerator table section. 376 void emitAccelObjC(); 377 378 /// Emit namespace dies into a hashed accelerator table. 379 void emitAccelNamespaces(); 380 381 /// Emit type dies into a hashed accelerator table. 382 void emitAccelTypes(); 383 384 /// Emit visible names into a debug pubnames section. 385 /// \param GnuStyle determines whether or not we want to emit 386 /// additional information into the table ala newer gcc for gdb 387 /// index. 388 void emitDebugPubNames(bool GnuStyle = false); 389 390 /// Emit visible types into a debug pubtypes section. 391 /// \param GnuStyle determines whether or not we want to emit 392 /// additional information into the table ala newer gcc for gdb 393 /// index. 394 void emitDebugPubTypes(bool GnuStyle = false); 395 396 void emitDebugPubSection( 397 bool GnuStyle, MCSection *PSec, StringRef Name, 398 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const); 399 400 /// Emit visible names into a debug str section. 401 void emitDebugStr(); 402 403 /// Emit visible names into a debug loc section. 404 void emitDebugLoc(); 405 406 /// Emit visible names into a debug loc dwo section. 407 void emitDebugLocDWO(); 408 409 /// Emit visible names into a debug aranges section. 410 void emitDebugARanges(); 411 412 /// Emit visible names into a debug ranges section. 413 void emitDebugRanges(); 414 415 /// DWARF 5 Experimental Split Dwarf Emitters 416 417 /// Initialize common features of skeleton units. 418 void initSkeletonUnit(const DwarfUnit &U, DIE &Die, 419 std::unique_ptr<DwarfUnit> NewU); 420 421 /// Construct the split debug info compile unit for the debug info 422 /// section. 423 DwarfCompileUnit &constructSkeletonCU(const DwarfCompileUnit &CU); 424 425 /// Emit the debug info dwo section. 426 void emitDebugInfoDWO(); 427 428 /// Emit the debug abbrev dwo section. 429 void emitDebugAbbrevDWO(); 430 431 /// Emit the debug line dwo section. 432 void emitDebugLineDWO(); 433 434 /// Emit the debug str dwo section. 435 void emitDebugStrDWO(); 436 437 /// Flags to let the linker know we have emitted new style pubnames. Only 438 /// emit it here if we don't have a skeleton CU for split dwarf. 439 void addGnuPubAttributes(DwarfUnit &U, DIE &D) const; 440 441 /// Create new DwarfCompileUnit for the given metadata node with tag 442 /// DW_TAG_compile_unit. 443 DwarfCompileUnit &constructDwarfCompileUnit(const DICompileUnit *DIUnit); 444 445 /// Construct imported_module or imported_declaration DIE. 446 void constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, 447 const DIImportedEntity *N); 448 449 /// Register a source line with debug info. Returns the unique 450 /// label that was emitted and which provides correspondence to the 451 /// source line list. 452 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope, 453 unsigned Flags); 454 455 /// Indentify instructions that are marking the beginning of or 456 /// ending of a scope. 457 void identifyScopeMarkers(); 458 459 /// Populate LexicalScope entries with variables' info. 460 void collectVariableInfo(DwarfCompileUnit &TheCU, const DISubprogram *SP, 461 DenseSet<InlinedVariable> &ProcessedVars); 462 463 /// Build the location list for all DBG_VALUEs in the 464 /// function that describe the same variable. 465 void buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, 466 const DbgValueHistoryMap::InstrRanges &Ranges); 467 468 /// Collect variable information from the side table maintained 469 /// by MMI. 470 void collectVariableInfoFromMMITable(DenseSet<InlinedVariable> &P); 471 472 /// Ensure that a label will be emitted before MI. 473 void requestLabelBeforeInsn(const MachineInstr *MI) { 474 LabelsBeforeInsn.insert(std::make_pair(MI, nullptr)); 475 } 476 477 /// Ensure that a label will be emitted after MI. 478 void requestLabelAfterInsn(const MachineInstr *MI) { 479 LabelsAfterInsn.insert(std::make_pair(MI, nullptr)); 480 } 481 482public: 483 //===--------------------------------------------------------------------===// 484 // Main entry points. 485 // 486 DwarfDebug(AsmPrinter *A, Module *M); 487 488 ~DwarfDebug() override; 489 490 /// Emit all Dwarf sections that should come prior to the 491 /// content. 492 void beginModule(); 493 494 /// Emit all Dwarf sections that should come after the content. 495 void endModule() override; 496 497 /// Gather pre-function debug information. 498 void beginFunction(const MachineFunction *MF) override; 499 500 /// Gather and emit post-function debug information. 501 void endFunction(const MachineFunction *MF) override; 502 503 /// Process beginning of an instruction. 504 void beginInstruction(const MachineInstr *MI) override; 505 506 /// Process end of an instruction. 507 void endInstruction() override; 508 509 /// Perform an MD5 checksum of \p Identifier and return the lower 64 bits. 510 static uint64_t makeTypeSignature(StringRef Identifier); 511 512 /// Add a DIE to the set of types that we're going to pull into 513 /// type units. 514 void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier, 515 DIE &Die, const DICompositeType *CTy); 516 517 /// Add a label so that arange data can be generated for it. 518 void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); } 519 520 /// For symbols that have a size designated (e.g. common symbols), 521 /// this tracks that size. 522 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override { 523 SymSize[Sym] = Size; 524 } 525 526 /// Returns whether to emit DW_AT_[MIPS_]linkage_name. 527 bool useLinkageNames() const { return UseLinkageNames; } 528 529 /// Returns whether to use DW_OP_GNU_push_tls_address, instead of the 530 /// standard DW_OP_form_tls_address opcode 531 bool useGNUTLSOpcode() const { return UseGNUTLSOpcode; } 532 533 /// \defgroup DebuggerTuning Predicates to tune DWARF for a given debugger. 534 /// 535 /// Returns whether we are "tuning" for a given debugger. 536 /// @{ 537 bool tuneForGDB() const { return DebuggerTuning == DebuggerKind::GDB; } 538 bool tuneForLLDB() const { return DebuggerTuning == DebuggerKind::LLDB; } 539 bool tuneForSCE() const { return DebuggerTuning == DebuggerKind::SCE; } 540 /// @} 541 542 // Experimental DWARF5 features. 543 544 /// Returns whether or not to emit tables that dwarf consumers can 545 /// use to accelerate lookup. 546 bool useDwarfAccelTables() const { return HasDwarfAccelTables; } 547 548 /// Returns whether or not to change the current debug info for the 549 /// split dwarf proposal support. 550 bool useSplitDwarf() const { return HasSplitDwarf; } 551 552 /// Returns the Dwarf Version. 553 unsigned getDwarfVersion() const { return DwarfVersion; } 554 555 /// Returns the previous CU that was being updated 556 const DwarfCompileUnit *getPrevCU() const { return PrevCU; } 557 void setPrevCU(const DwarfCompileUnit *PrevCU) { this->PrevCU = PrevCU; } 558 559 /// Returns the entries for the .debug_loc section. 560 const DebugLocStream &getDebugLocs() const { return DebugLocs; } 561 562 /// Emit an entry for the debug loc section. This can be used to 563 /// handle an entry that's going to be emitted into the debug loc section. 564 void emitDebugLocEntry(ByteStreamer &Streamer, 565 const DebugLocStream::Entry &Entry); 566 567 /// Emit the location for a debug loc entry, including the size header. 568 void emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry); 569 570 /// Find the MDNode for the given reference. 571 template <typename T> T *resolve(TypedDINodeRef<T> Ref) const { 572 return Ref.resolve(TypeIdentifierMap); 573 } 574 575 /// Return the TypeIdentifierMap. 576 const DITypeIdentifierMap &getTypeIdentifierMap() const { 577 return TypeIdentifierMap; 578 } 579 580 /// Find the DwarfCompileUnit for the given CU Die. 581 DwarfCompileUnit *lookupUnit(const DIE *CU) const { 582 return CUDieMap.lookup(CU); 583 } 584 585 void addSubprogramNames(const DISubprogram *SP, DIE &Die); 586 587 AddressPool &getAddressPool() { return AddrPool; } 588 589 void addAccelName(StringRef Name, const DIE &Die); 590 591 void addAccelObjC(StringRef Name, const DIE &Die); 592 593 void addAccelNamespace(StringRef Name, const DIE &Die); 594 595 void addAccelType(StringRef Name, const DIE &Die, char Flags); 596 597 const MachineFunction *getCurrentFunction() const { return CurFn; } 598 599 /// A helper function to check whether the DIE for a given Scope is 600 /// going to be null. 601 bool isLexicalScopeDIENull(LexicalScope *Scope); 602 603 /// Return Label preceding the instruction. 604 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI); 605 606 /// Return Label immediately following the instruction. 607 MCSymbol *getLabelAfterInsn(const MachineInstr *MI); 608 609 // FIXME: Sink these functions down into DwarfFile/Dwarf*Unit. 610 611 SmallPtrSet<const MDNode *, 16> &getProcessedSPNodes() { 612 return ProcessedSPNodes; 613 } 614}; 615} // End of namespace llvm 616 617#endif 618