DwarfDebug.h revision 25b68c6770b7ba044ad37e0aed4f1c414f3b5209
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 CODEGEN_ASMPRINTER_DWARFDEBUG_H__ 15#define CODEGEN_ASMPRINTER_DWARFDEBUG_H__ 16 17#include "DIE.h" 18#include "DwarfPrinter.h" 19#include "llvm/CodeGen/AsmPrinter.h" 20#include "llvm/CodeGen/MachineLocation.h" 21#include "llvm/Analysis/DebugInfo.h" 22#include "llvm/Support/raw_ostream.h" 23#include "llvm/ADT/DenseMap.h" 24#include "llvm/ADT/FoldingSet.h" 25#include "llvm/ADT/SmallSet.h" 26#include "llvm/ADT/StringMap.h" 27#include "llvm/ADT/UniqueVector.h" 28#include <string> 29 30namespace llvm { 31 32class CompileUnit; 33class DbgConcreteScope; 34class DbgScope; 35class DbgVariable; 36class MachineFrameInfo; 37class MachineModuleInfo; 38class MCAsmInfo; 39class Timer; 40 41//===----------------------------------------------------------------------===// 42/// SrcLineInfo - This class is used to record source line correspondence. 43/// 44class SrcLineInfo { 45 unsigned Line; // Source line number. 46 unsigned Column; // Source column. 47 unsigned SourceID; // Source ID number. 48 MCSymbol *Label; // Label in code ID number. 49public: 50 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label) 51 : Line(L), Column(C), SourceID(S), Label(label) {} 52 53 // Accessors 54 unsigned getLine() const { return Line; } 55 unsigned getColumn() const { return Column; } 56 unsigned getSourceID() const { return SourceID; } 57 MCSymbol *getLabel() const { return Label; } 58}; 59 60class DwarfDebug : public DwarfPrinter { 61 //===--------------------------------------------------------------------===// 62 // Attributes used to construct specific Dwarf sections. 63 // 64 65 /// ModuleCU - All DIEs are inserted in ModuleCU. 66 CompileUnit *ModuleCU; 67 68 /// AbbreviationsSet - Used to uniquely define abbreviations. 69 /// 70 FoldingSet<DIEAbbrev> AbbreviationsSet; 71 72 /// Abbreviations - A list of all the unique abbreviations in use. 73 /// 74 std::vector<DIEAbbrev *> Abbreviations; 75 76 /// DirectoryIdMap - Directory name to directory id map. 77 /// 78 StringMap<unsigned> DirectoryIdMap; 79 80 /// DirectoryNames - A list of directory names. 81 SmallVector<std::string, 8> DirectoryNames; 82 83 /// SourceFileIdMap - Source file name to source file id map. 84 /// 85 StringMap<unsigned> SourceFileIdMap; 86 87 /// SourceFileNames - A list of source file names. 88 SmallVector<std::string, 8> SourceFileNames; 89 90 /// SourceIdMap - Source id map, i.e. pair of directory id and source file 91 /// id mapped to a unique id. 92 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap; 93 94 /// SourceIds - Reverse map from source id to directory id + file id pair. 95 /// 96 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds; 97 98 /// Lines - List of source line correspondence. 99 std::vector<SrcLineInfo> Lines; 100 101 /// DIEValues - A list of all the unique values in use. 102 /// 103 std::vector<DIEValue *> DIEValues; 104 105 /// StringPool - A String->Symbol mapping of strings used by indirect 106 /// references. 107 StringMap<std::pair<MCSymbol*, unsigned> > StringPool; 108 unsigned NextStringPoolNumber; 109 110 MCSymbol *getStringPoolEntry(StringRef Str); 111 112 /// SectionMap - Provides a unique id per text section. 113 /// 114 UniqueVector<const MCSection*> SectionMap; 115 116 /// SectionSourceLines - Tracks line numbers per text section. 117 /// 118 std::vector<std::vector<SrcLineInfo> > SectionSourceLines; 119 120 /// didInitial - Flag to indicate if initial emission has been done. 121 /// 122 bool didInitial; 123 124 /// shouldEmit - Flag to indicate if debug information should be emitted. 125 /// 126 bool shouldEmit; 127 128 // CurrentFnDbgScope - Top level scope for the current function. 129 // 130 DbgScope *CurrentFnDbgScope; 131 132 /// DbgScopeMap - Tracks the scopes in the current function. Owns the 133 /// contained DbgScope*s. 134 /// 135 DenseMap<MDNode *, DbgScope *> DbgScopeMap; 136 137 /// ConcreteScopes - Tracks the concrete scopees in the current function. 138 /// These scopes are also included in DbgScopeMap. 139 DenseMap<MDNode *, DbgScope *> ConcreteScopes; 140 141 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are 142 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s. 143 DenseMap<MDNode *, DbgScope *> AbstractScopes; 144 SmallVector<DbgScope *, 4>AbstractScopesList; 145 146 /// AbstractVariables - Collection on abstract variables. Owned by the 147 /// DbgScopes in AbstractScopes. 148 DenseMap<MDNode *, DbgVariable *> AbstractVariables; 149 150 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked 151 /// (at the end of the module) as DW_AT_inline. 152 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs; 153 154 DenseMap<DIE *, MDNode *> ContainingTypeMap; 155 156 /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs. 157 SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs; 158 159 /// TopLevelDIEs - Collection of top level DIEs. 160 SmallPtrSet<DIE *, 4> TopLevelDIEs; 161 SmallVector<DIE *, 4> TopLevelDIEsVector; 162 163 typedef SmallVector<DbgScope *, 2> ScopeVector; 164 typedef DenseMap<const MachineInstr *, ScopeVector> 165 InsnToDbgScopeMapTy; 166 167 /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts. 168 InsnToDbgScopeMapTy DbgScopeBeginMap; 169 170 /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends. 171 InsnToDbgScopeMapTy DbgScopeEndMap; 172 173 /// InlineInfo - Keep track of inlined functions and their location. This 174 /// information is used to populate debug_inlined section. 175 typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels; 176 DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo; 177 SmallVector<MDNode *, 4> InlinedSPNodes; 178 179 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is 180 /// used when calculating the "origin" of a concrete instance of an inlined 181 /// function. 182 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets; 183 184 /// DebugTimer - Timer for the Dwarf debug writer. 185 Timer *DebugTimer; 186 187 struct FunctionDebugFrameInfo { 188 unsigned Number; 189 std::vector<MachineMove> Moves; 190 191 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M) 192 : Number(Num), Moves(M) {} 193 }; 194 195 std::vector<FunctionDebugFrameInfo> DebugFrames; 196 197 /// getSourceDirectoryAndFileIds - Return the directory and file ids that 198 /// maps to the source id. Source id starts at 1. 199 std::pair<unsigned, unsigned> 200 getSourceDirectoryAndFileIds(unsigned SId) const { 201 return SourceIds[SId-1]; 202 } 203 204 /// getNumSourceDirectories - Return the number of source directories in the 205 /// debug info. 206 unsigned getNumSourceDirectories() const { 207 return DirectoryNames.size(); 208 } 209 210 /// getSourceDirectoryName - Return the name of the directory corresponding 211 /// to the id. 212 const std::string &getSourceDirectoryName(unsigned Id) const { 213 return DirectoryNames[Id - 1]; 214 } 215 216 /// getSourceFileName - Return the name of the source file corresponding 217 /// to the id. 218 const std::string &getSourceFileName(unsigned Id) const { 219 return SourceFileNames[Id - 1]; 220 } 221 222 /// getNumSourceIds - Return the number of unique source ids. 223 unsigned getNumSourceIds() const { 224 return SourceIds.size(); 225 } 226 227 /// assignAbbrevNumber - Define a unique number for the abbreviation. 228 /// 229 void assignAbbrevNumber(DIEAbbrev &Abbrev); 230 231 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 232 /// information entry. 233 DIEEntry *createDIEEntry(DIE *Entry); 234 235 /// addUInt - Add an unsigned integer attribute data and value. 236 /// 237 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer); 238 239 /// addSInt - Add an signed integer attribute data and value. 240 /// 241 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer); 242 243 /// addString - Add a string attribute data and value. 244 /// 245 void addString(DIE *Die, unsigned Attribute, unsigned Form, 246 const StringRef Str); 247 248 /// addLabel - Add a Dwarf label attribute data and value. 249 /// 250 void addLabel(DIE *Die, unsigned Attribute, unsigned Form, 251 const MCSymbol *Label); 252 253 /// addSectionOffset - Add a section offset label attribute data and value. 254 /// 255 void addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form, 256 const MCSymbol *Label, const MCSymbol *Section, 257 bool isEH = false); 258 259 /// addDelta - Add a label delta attribute data and value. 260 /// 261 void addDelta(DIE *Die, unsigned Attribute, unsigned Form, 262 const MCSymbol *Hi, const MCSymbol *Lo); 263 264 /// addDIEEntry - Add a DIE attribute data and value. 265 /// 266 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) { 267 Die->addValue(Attribute, Form, createDIEEntry(Entry)); 268 } 269 270 /// addBlock - Add block data. 271 /// 272 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block); 273 274 /// addSourceLine - Add location information to specified debug information 275 /// entry. 276 void addSourceLine(DIE *Die, const DIVariable *V); 277 void addSourceLine(DIE *Die, const DIGlobal *G); 278 void addSourceLine(DIE *Die, const DISubprogram *SP); 279 void addSourceLine(DIE *Die, const DIType *Ty); 280 void addSourceLine(DIE *Die, const DINameSpace *NS); 281 282 /// addAddress - Add an address attribute to a die based on the location 283 /// provided. 284 void addAddress(DIE *Die, unsigned Attribute, 285 const MachineLocation &Location); 286 287 /// addComplexAddress - Start with the address based on the location provided, 288 /// and generate the DWARF information necessary to find the actual variable 289 /// (navigating the extra location information encoded in the type) based on 290 /// the starting location. Add the DWARF information to the die. 291 /// 292 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute, 293 const MachineLocation &Location); 294 295 // FIXME: Should be reformulated in terms of addComplexAddress. 296 /// addBlockByrefAddress - Start with the address based on the location 297 /// provided, and generate the DWARF information necessary to find the 298 /// actual Block variable (navigating the Block struct) based on the 299 /// starting location. Add the DWARF information to the die. Obsolete, 300 /// please use addComplexAddress instead. 301 /// 302 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute, 303 const MachineLocation &Location); 304 305 /// addToContextOwner - Add Die into the list of its context owner's children. 306 void addToContextOwner(DIE *Die, DIDescriptor Context); 307 308 /// addType - Add a new type attribute to the specified entity. 309 void addType(DIE *Entity, DIType Ty); 310 311 312 /// getOrCreateNameSpace - Create a DIE for DINameSpace. 313 DIE *getOrCreateNameSpace(DINameSpace NS); 314 315 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 316 /// given DIType. 317 DIE *getOrCreateTypeDIE(DIType Ty); 318 319 void addPubTypes(DISubprogram SP); 320 321 /// constructTypeDIE - Construct basic type die from DIBasicType. 322 void constructTypeDIE(DIE &Buffer, 323 DIBasicType BTy); 324 325 /// constructTypeDIE - Construct derived type die from DIDerivedType. 326 void constructTypeDIE(DIE &Buffer, 327 DIDerivedType DTy); 328 329 /// constructTypeDIE - Construct type DIE from DICompositeType. 330 void constructTypeDIE(DIE &Buffer, 331 DICompositeType CTy); 332 333 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 334 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy); 335 336 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 337 void constructArrayTypeDIE(DIE &Buffer, 338 DICompositeType *CTy); 339 340 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 341 DIE *constructEnumTypeDIE(DIEnumerator ETy); 342 343 /// createGlobalVariableDIE - Create new DIE using GV. 344 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV); 345 346 /// createMemberDIE - Create new member DIE. 347 DIE *createMemberDIE(const DIDerivedType &DT); 348 349 /// createSubprogramDIE - Create new DIE using SP. 350 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false); 351 352 /// getUpdatedDbgScope - Find or create DbgScope assicated with 353 /// the instruction. Initialize scope and update scope hierarchy. 354 DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI, MDNode *InlinedAt); 355 356 /// createDbgScope - Create DbgScope for the scope. 357 void createDbgScope(MDNode *Scope, MDNode *InlinedAt); 358 359 DbgScope *getOrCreateAbstractScope(MDNode *N); 360 361 /// findAbstractVariable - Find abstract variable associated with Var. 362 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx, 363 DILocation &Loc); 364 365 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and 366 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes. 367 /// If there are global variables in this scope then create and insert 368 /// DIEs for these variables. 369 DIE *updateSubprogramScopeDIE(MDNode *SPNode); 370 371 /// constructLexicalScope - Construct new DW_TAG_lexical_block 372 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels. 373 DIE *constructLexicalScopeDIE(DbgScope *Scope); 374 375 /// constructInlinedScopeDIE - This scope represents inlined body of 376 /// a function. Construct DIE to represent this concrete inlined copy 377 /// of the function. 378 DIE *constructInlinedScopeDIE(DbgScope *Scope); 379 380 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 381 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S); 382 383 /// constructScopeDIE - Construct a DIE for this scope. 384 DIE *constructScopeDIE(DbgScope *Scope); 385 386 /// emitInitial - Emit initial Dwarf declarations. This is necessary for cc 387 /// tools to recognize the object file contains Dwarf information. 388 void emitInitial(); 389 390 /// emitDIE - Recusively Emits a debug information entry. 391 /// 392 void emitDIE(DIE *Die); 393 394 /// computeSizeAndOffset - Compute the size and offset of a DIE. 395 /// 396 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last); 397 398 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs. 399 /// 400 void computeSizeAndOffsets(); 401 402 /// EmitDebugInfo - Emit the debug info section. 403 /// 404 void emitDebugInfo(); 405 406 /// emitAbbreviations - Emit the abbreviation section. 407 /// 408 void emitAbbreviations() const; 409 410 /// emitEndOfLineMatrix - Emit the last address of the section and the end of 411 /// the line matrix. 412 /// 413 void emitEndOfLineMatrix(unsigned SectionEnd); 414 415 /// emitDebugLines - Emit source line information. 416 /// 417 void emitDebugLines(); 418 419 /// emitCommonDebugFrame - Emit common frame info into a debug frame section. 420 /// 421 void emitCommonDebugFrame(); 422 423 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame 424 /// section. 425 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo); 426 427 /// emitDebugPubNames - Emit visible names into a debug pubnames section. 428 /// 429 void emitDebugPubNames(); 430 431 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section. 432 /// 433 void emitDebugPubTypes(); 434 435 /// emitDebugStr - Emit visible names into a debug str section. 436 /// 437 void emitDebugStr(); 438 439 /// emitDebugLoc - Emit visible names into a debug loc section. 440 /// 441 void emitDebugLoc(); 442 443 /// EmitDebugARanges - Emit visible names into a debug aranges section. 444 /// 445 void EmitDebugARanges(); 446 447 /// emitDebugRanges - Emit visible names into a debug ranges section. 448 /// 449 void emitDebugRanges(); 450 451 /// emitDebugMacInfo - Emit visible names into a debug macinfo section. 452 /// 453 void emitDebugMacInfo(); 454 455 /// emitDebugInlineInfo - Emit inline info using following format. 456 /// Section Header: 457 /// 1. length of section 458 /// 2. Dwarf version number 459 /// 3. address size. 460 /// 461 /// Entries (one "entry" for each function that was inlined): 462 /// 463 /// 1. offset into __debug_str section for MIPS linkage name, if exists; 464 /// otherwise offset into __debug_str for regular function name. 465 /// 2. offset into __debug_str section for regular function name. 466 /// 3. an unsigned LEB128 number indicating the number of distinct inlining 467 /// instances for the function. 468 /// 469 /// The rest of the entry consists of a {die_offset, low_pc} pair for each 470 /// inlined instance; the die_offset points to the inlined_subroutine die in 471 /// the __debug_info section, and the low_pc is the starting address for the 472 /// inlining instance. 473 void emitDebugInlineInfo(); 474 475 /// GetOrCreateSourceID - Look up the source id with the given directory and 476 /// source file names. If none currently exists, create a new id and insert it 477 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps 478 /// as well. 479 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName); 480 481 void constructCompileUnit(MDNode *N); 482 483 void constructGlobalVariableDIE(MDNode *N); 484 485 void constructSubprogramDIE(MDNode *N); 486 487 // FIXME: This should go away in favor of complex addresses. 488 /// Find the type the programmer originally declared the variable to be 489 /// and return that type. Obsolete, use GetComplexAddrType instead. 490 /// 491 DIType getBlockByrefType(DIType Ty, std::string Name); 492 493public: 494 //===--------------------------------------------------------------------===// 495 // Main entry points. 496 // 497 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T); 498 virtual ~DwarfDebug(); 499 500 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should 501 /// be emitted. 502 bool ShouldEmitDwarfDebug() const { return shouldEmit; } 503 504 /// beginModule - Emit all Dwarf sections that should come prior to the 505 /// content. 506 void beginModule(Module *M, MachineModuleInfo *MMI); 507 508 /// endModule - Emit all Dwarf sections that should come after the content. 509 /// 510 void endModule(); 511 512 /// beginFunction - Gather pre-function debug information. Assumes being 513 /// emitted immediately after the function entry point. 514 void beginFunction(const MachineFunction *MF); 515 516 /// endFunction - Gather and emit post-function debug information. 517 /// 518 void endFunction(const MachineFunction *MF); 519 520 /// recordSourceLine - Register a source line with debug info. Returns the 521 /// unique label that was emitted and which provides correspondence to 522 /// the source line list. 523 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope); 524 525 /// getSourceLineCount - Return the number of source lines in the debug 526 /// info. 527 unsigned getSourceLineCount() const { 528 return Lines.size(); 529 } 530 531 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be 532 /// timed. Look up the source id with the given directory and source file 533 /// names. If none currently exists, create a new id and insert it in the 534 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as 535 /// well. 536 unsigned getOrCreateSourceID(const std::string &DirName, 537 const std::string &FileName); 538 539 /// extractScopeInformation - Scan machine instructions in this function 540 /// and collect DbgScopes. Return true, if atleast one scope was found. 541 bool extractScopeInformation(); 542 543 /// collectVariableInfo - Populate DbgScope entries with variables' info. 544 void collectVariableInfo(); 545 546 /// beginScope - Process beginning of a scope starting at Label. 547 void beginScope(const MachineInstr *MI, MCSymbol *Label); 548 549 /// endScope - Prcess end of a scope. 550 void endScope(const MachineInstr *MI); 551}; 552} // End of namespace llvm 553 554#endif 555