DwarfDebug.cpp revision 1a8d2d2d4f9c019e3ace0053ce3f33449cf85c44
1//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===// 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#include "DwarfDebug.h" 15#include "llvm/Module.h" 16#include "llvm/CodeGen/MachineModuleInfo.h" 17#include "llvm/Support/Timer.h" 18#include "llvm/System/Path.h" 19#include "llvm/Target/TargetAsmInfo.h" 20#include "llvm/Target/TargetRegisterInfo.h" 21#include "llvm/Target/TargetData.h" 22#include "llvm/Target/TargetFrameInfo.h" 23using namespace llvm; 24 25static TimerGroup &getDwarfTimerGroup() { 26 static TimerGroup DwarfTimerGroup("Dwarf Debugging"); 27 return DwarfTimerGroup; 28} 29 30//===----------------------------------------------------------------------===// 31 32/// Configuration values for initial hash set sizes (log2). 33/// 34static const unsigned InitDiesSetSize = 9; // log2(512) 35static const unsigned InitAbbreviationsSetSize = 9; // log2(512) 36static const unsigned InitValuesSetSize = 9; // log2(512) 37 38namespace llvm { 39 40//===----------------------------------------------------------------------===// 41/// CompileUnit - This dwarf writer support class manages information associate 42/// with a source file. 43class VISIBILITY_HIDDEN CompileUnit { 44 /// ID - File identifier for source. 45 /// 46 unsigned ID; 47 48 /// Die - Compile unit debug information entry. 49 /// 50 DIE *Die; 51 52 /// GVToDieMap - Tracks the mapping of unit level debug informaton 53 /// variables to debug information entries. 54 std::map<GlobalVariable *, DIE *> GVToDieMap; 55 56 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton 57 /// descriptors to debug information entries using a DIEEntry proxy. 58 std::map<GlobalVariable *, DIEEntry *> GVToDIEEntryMap; 59 60 /// Globals - A map of globally visible named entities for this unit. 61 /// 62 StringMap<DIE*> Globals; 63 64 /// DiesSet - Used to uniquely define dies within the compile unit. 65 /// 66 FoldingSet<DIE> DiesSet; 67public: 68 CompileUnit(unsigned I, DIE *D) 69 : ID(I), Die(D), DiesSet(InitDiesSetSize) {} 70 ~CompileUnit() { delete Die; } 71 72 // Accessors. 73 unsigned getID() const { return ID; } 74 DIE* getDie() const { return Die; } 75 StringMap<DIE*> &getGlobals() { return Globals; } 76 77 /// hasContent - Return true if this compile unit has something to write out. 78 /// 79 bool hasContent() const { return !Die->getChildren().empty(); } 80 81 /// AddGlobal - Add a new global entity to the compile unit. 82 /// 83 void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; } 84 85 /// getDieMapSlotFor - Returns the debug information entry map slot for the 86 /// specified debug variable. 87 DIE *&getDieMapSlotFor(GlobalVariable *GV) { return GVToDieMap[GV]; } 88 89 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for the 90 /// specified debug variable. 91 DIEEntry *&getDIEEntrySlotFor(GlobalVariable *GV) { 92 return GVToDIEEntryMap[GV]; 93 } 94 95 /// AddDie - Adds or interns the DIE to the compile unit. 96 /// 97 DIE *AddDie(DIE &Buffer) { 98 FoldingSetNodeID ID; 99 Buffer.Profile(ID); 100 void *Where; 101 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where); 102 103 if (!Die) { 104 Die = new DIE(Buffer); 105 DiesSet.InsertNode(Die, Where); 106 this->Die->AddChild(Die); 107 Buffer.Detach(); 108 } 109 110 return Die; 111 } 112}; 113 114//===----------------------------------------------------------------------===// 115/// DbgVariable - This class is used to track local variable information. 116/// 117class VISIBILITY_HIDDEN DbgVariable { 118 DIVariable Var; // Variable Descriptor. 119 unsigned FrameIndex; // Variable frame index. 120 bool InlinedFnVar; // Variable for an inlined function. 121public: 122 DbgVariable(DIVariable V, unsigned I, bool IFV) 123 : Var(V), FrameIndex(I), InlinedFnVar(IFV) {} 124 125 // Accessors. 126 DIVariable getVariable() const { return Var; } 127 unsigned getFrameIndex() const { return FrameIndex; } 128 bool isInlinedFnVar() const { return InlinedFnVar; } 129}; 130 131//===----------------------------------------------------------------------===// 132/// DbgScope - This class is used to track scope information. 133/// 134class DbgConcreteScope; 135class VISIBILITY_HIDDEN DbgScope { 136 DbgScope *Parent; // Parent to this scope. 137 DIDescriptor Desc; // Debug info descriptor for scope. 138 // Either subprogram or block. 139 unsigned StartLabelID; // Label ID of the beginning of scope. 140 unsigned EndLabelID; // Label ID of the end of scope. 141 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope. 142 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope. 143 SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs. 144 145 // Private state for dump() 146 mutable unsigned IndentLevel; 147public: 148 DbgScope(DbgScope *P, DIDescriptor D) 149 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), IndentLevel(0) {} 150 virtual ~DbgScope(); 151 152 // Accessors. 153 DbgScope *getParent() const { return Parent; } 154 DIDescriptor getDesc() const { return Desc; } 155 unsigned getStartLabelID() const { return StartLabelID; } 156 unsigned getEndLabelID() const { return EndLabelID; } 157 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; } 158 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; } 159 SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; } 160 void setStartLabelID(unsigned S) { StartLabelID = S; } 161 void setEndLabelID(unsigned E) { EndLabelID = E; } 162 163 /// AddScope - Add a scope to the scope. 164 /// 165 void AddScope(DbgScope *S) { Scopes.push_back(S); } 166 167 /// AddVariable - Add a variable to the scope. 168 /// 169 void AddVariable(DbgVariable *V) { Variables.push_back(V); } 170 171 /// AddConcreteInst - Add a concrete instance to the scope. 172 /// 173 void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); } 174 175#ifndef NDEBUG 176 void dump() const; 177#endif 178}; 179 180#ifndef NDEBUG 181void DbgScope::dump() const { 182 std::string Indent(IndentLevel, ' '); 183 184 cerr << Indent; Desc.dump(); 185 cerr << " [" << StartLabelID << ", " << EndLabelID << "]\n"; 186 187 IndentLevel += 2; 188 189 for (unsigned i = 0, e = Scopes.size(); i != e; ++i) 190 if (Scopes[i] != this) 191 Scopes[i]->dump(); 192 193 IndentLevel -= 2; 194} 195#endif 196 197//===----------------------------------------------------------------------===// 198/// DbgConcreteScope - This class is used to track a scope that holds concrete 199/// instance information. 200/// 201class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope { 202 CompileUnit *Unit; 203 DIE *Die; // Debug info for this concrete scope. 204public: 205 DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {} 206 207 // Accessors. 208 DIE *getDie() const { return Die; } 209 void setDie(DIE *D) { Die = D; } 210}; 211 212DbgScope::~DbgScope() { 213 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) 214 delete Scopes[i]; 215 for (unsigned j = 0, M = Variables.size(); j < M; ++j) 216 delete Variables[j]; 217 for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k) 218 delete ConcreteInsts[k]; 219} 220 221} // end llvm namespace 222 223DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T) 224 : Dwarf(OS, A, T, "dbg"), ModuleCU(0), 225 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(), 226 ValuesSet(InitValuesSetSize), Values(), StringPool(), SectionMap(), 227 SectionSourceLines(), didInitial(false), shouldEmit(false), 228 FunctionDbgScope(0), DebugTimer(0) { 229 if (TimePassesIsEnabled) 230 DebugTimer = new Timer("Dwarf Debug Writer", 231 getDwarfTimerGroup()); 232} 233DwarfDebug::~DwarfDebug() { 234 for (unsigned j = 0, M = Values.size(); j < M; ++j) 235 delete Values[j]; 236 237 for (DenseMap<const GlobalVariable *, DbgScope *>::iterator 238 I = AbstractInstanceRootMap.begin(), 239 E = AbstractInstanceRootMap.end(); I != E;++I) 240 delete I->second; 241 242 delete DebugTimer; 243} 244 245/// AssignAbbrevNumber - Define a unique number for the abbreviation. 246/// 247void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) { 248 // Profile the node so that we can make it unique. 249 FoldingSetNodeID ID; 250 Abbrev.Profile(ID); 251 252 // Check the set for priors. 253 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); 254 255 // If it's newly added. 256 if (InSet == &Abbrev) { 257 // Add to abbreviation list. 258 Abbreviations.push_back(&Abbrev); 259 260 // Assign the vector position + 1 as its number. 261 Abbrev.setNumber(Abbreviations.size()); 262 } else { 263 // Assign existing abbreviation number. 264 Abbrev.setNumber(InSet->getNumber()); 265 } 266} 267 268/// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug 269/// information entry. 270DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) { 271 DIEEntry *Value; 272 273 if (Entry) { 274 FoldingSetNodeID ID; 275 DIEEntry::Profile(ID, Entry); 276 void *Where; 277 Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where)); 278 279 if (Value) return Value; 280 281 Value = new DIEEntry(Entry); 282 ValuesSet.InsertNode(Value, Where); 283 } else { 284 Value = new DIEEntry(Entry); 285 } 286 287 Values.push_back(Value); 288 return Value; 289} 290 291/// SetDIEEntry - Set a DIEEntry once the debug information entry is defined. 292/// 293void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) { 294 Value->setEntry(Entry); 295 296 // Add to values set if not already there. If it is, we merely have a 297 // duplicate in the values list (no harm.) 298 ValuesSet.GetOrInsertNode(Value); 299} 300 301/// AddUInt - Add an unsigned integer attribute data and value. 302/// 303void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute, 304 unsigned Form, uint64_t Integer) { 305 if (!Form) Form = DIEInteger::BestForm(false, Integer); 306 307 FoldingSetNodeID ID; 308 DIEInteger::Profile(ID, Integer); 309 void *Where; 310 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 311 312 if (!Value) { 313 Value = new DIEInteger(Integer); 314 ValuesSet.InsertNode(Value, Where); 315 Values.push_back(Value); 316 } 317 318 Die->AddValue(Attribute, Form, Value); 319} 320 321/// AddSInt - Add an signed integer attribute data and value. 322/// 323void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute, 324 unsigned Form, int64_t Integer) { 325 if (!Form) Form = DIEInteger::BestForm(true, Integer); 326 327 FoldingSetNodeID ID; 328 DIEInteger::Profile(ID, (uint64_t)Integer); 329 void *Where; 330 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 331 332 if (!Value) { 333 Value = new DIEInteger(Integer); 334 ValuesSet.InsertNode(Value, Where); 335 Values.push_back(Value); 336 } 337 338 Die->AddValue(Attribute, Form, Value); 339} 340 341/// AddString - Add a string attribute data and value. 342/// 343void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form, 344 const std::string &String) { 345 FoldingSetNodeID ID; 346 DIEString::Profile(ID, String); 347 void *Where; 348 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 349 350 if (!Value) { 351 Value = new DIEString(String); 352 ValuesSet.InsertNode(Value, Where); 353 Values.push_back(Value); 354 } 355 356 Die->AddValue(Attribute, Form, Value); 357} 358 359/// AddLabel - Add a Dwarf label attribute data and value. 360/// 361void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form, 362 const DWLabel &Label) { 363 FoldingSetNodeID ID; 364 DIEDwarfLabel::Profile(ID, Label); 365 void *Where; 366 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 367 368 if (!Value) { 369 Value = new DIEDwarfLabel(Label); 370 ValuesSet.InsertNode(Value, Where); 371 Values.push_back(Value); 372 } 373 374 Die->AddValue(Attribute, Form, Value); 375} 376 377/// AddObjectLabel - Add an non-Dwarf label attribute data and value. 378/// 379void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form, 380 const std::string &Label) { 381 FoldingSetNodeID ID; 382 DIEObjectLabel::Profile(ID, Label); 383 void *Where; 384 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 385 386 if (!Value) { 387 Value = new DIEObjectLabel(Label); 388 ValuesSet.InsertNode(Value, Where); 389 Values.push_back(Value); 390 } 391 392 Die->AddValue(Attribute, Form, Value); 393} 394 395/// AddSectionOffset - Add a section offset label attribute data and value. 396/// 397void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form, 398 const DWLabel &Label, const DWLabel &Section, 399 bool isEH, bool useSet) { 400 FoldingSetNodeID ID; 401 DIESectionOffset::Profile(ID, Label, Section); 402 void *Where; 403 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 404 405 if (!Value) { 406 Value = new DIESectionOffset(Label, Section, isEH, useSet); 407 ValuesSet.InsertNode(Value, Where); 408 Values.push_back(Value); 409 } 410 411 Die->AddValue(Attribute, Form, Value); 412} 413 414/// AddDelta - Add a label delta attribute data and value. 415/// 416void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form, 417 const DWLabel &Hi, const DWLabel &Lo) { 418 FoldingSetNodeID ID; 419 DIEDelta::Profile(ID, Hi, Lo); 420 void *Where; 421 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 422 423 if (!Value) { 424 Value = new DIEDelta(Hi, Lo); 425 ValuesSet.InsertNode(Value, Where); 426 Values.push_back(Value); 427 } 428 429 Die->AddValue(Attribute, Form, Value); 430} 431 432/// AddBlock - Add block data. 433/// 434void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form, 435 DIEBlock *Block) { 436 Block->ComputeSize(TD); 437 FoldingSetNodeID ID; 438 Block->Profile(ID); 439 void *Where; 440 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); 441 442 if (!Value) { 443 Value = Block; 444 ValuesSet.InsertNode(Value, Where); 445 Values.push_back(Value); 446 } else { 447 // Already exists, reuse the previous one. 448 delete Block; 449 Block = cast<DIEBlock>(Value); 450 } 451 452 Die->AddValue(Attribute, Block->BestForm(), Value); 453} 454 455/// AddSourceLine - Add location information to specified debug information 456/// entry. 457void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) { 458 // If there is no compile unit specified, don't add a line #. 459 if (V->getCompileUnit().isNull()) 460 return; 461 462 unsigned Line = V->getLineNumber(); 463 unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID(); 464 assert(FileID && "Invalid file id"); 465 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 466 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 467} 468 469/// AddSourceLine - Add location information to specified debug information 470/// entry. 471void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) { 472 // If there is no compile unit specified, don't add a line #. 473 if (G->getCompileUnit().isNull()) 474 return; 475 476 unsigned Line = G->getLineNumber(); 477 unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID(); 478 assert(FileID && "Invalid file id"); 479 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 480 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 481} 482void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) { 483 // If there is no compile unit specified, don't add a line #. 484 DICompileUnit CU = Ty->getCompileUnit(); 485 if (CU.isNull()) 486 return; 487 488 unsigned Line = Ty->getLineNumber(); 489 unsigned FileID = FindCompileUnit(CU).getID(); 490 assert(FileID && "Invalid file id"); 491 AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 492 AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 493} 494 495/// AddAddress - Add an address attribute to a die based on the location 496/// provided. 497void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute, 498 const MachineLocation &Location) { 499 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); 500 DIEBlock *Block = new DIEBlock(); 501 502 if (Location.isReg()) { 503 if (Reg < 32) { 504 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); 505 } else { 506 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 507 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 508 } 509 } else { 510 if (Reg < 32) { 511 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 512 } else { 513 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 514 AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 515 } 516 517 AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); 518 } 519 520 AddBlock(Die, Attribute, 0, Block); 521} 522 523/// AddType - Add a new type attribute to the specified entity. 524void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) { 525 if (Ty.isNull()) 526 return; 527 528 // Check for pre-existence. 529 DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getGV()); 530 531 // If it exists then use the existing value. 532 if (Slot) { 533 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot); 534 return; 535 } 536 537 // Set up proxy. 538 Slot = CreateDIEEntry(); 539 540 // Construct type. 541 DIE Buffer(dwarf::DW_TAG_base_type); 542 if (Ty.isBasicType(Ty.getTag())) 543 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV())); 544 else if (Ty.isDerivedType(Ty.getTag())) 545 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV())); 546 else { 547 assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType"); 548 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV())); 549 } 550 551 // Add debug information entry to entity and appropriate context. 552 DIE *Die = NULL; 553 DIDescriptor Context = Ty.getContext(); 554 if (!Context.isNull()) 555 Die = DW_Unit->getDieMapSlotFor(Context.getGV()); 556 557 if (Die) { 558 DIE *Child = new DIE(Buffer); 559 Die->AddChild(Child); 560 Buffer.Detach(); 561 SetDIEEntry(Slot, Child); 562 } else { 563 Die = DW_Unit->AddDie(Buffer); 564 SetDIEEntry(Slot, Die); 565 } 566 567 Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot); 568} 569 570/// ConstructTypeDIE - Construct basic type die from DIBasicType. 571void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 572 DIBasicType BTy) { 573 // Get core information. 574 std::string Name; 575 BTy.getName(Name); 576 Buffer.setTag(dwarf::DW_TAG_base_type); 577 AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 578 BTy.getEncoding()); 579 580 // Add name if not anonymous or intermediate type. 581 if (!Name.empty()) 582 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 583 uint64_t Size = BTy.getSizeInBits() >> 3; 584 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 585} 586 587/// ConstructTypeDIE - Construct derived type die from DIDerivedType. 588void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 589 DIDerivedType DTy) { 590 // Get core information. 591 std::string Name; 592 DTy.getName(Name); 593 uint64_t Size = DTy.getSizeInBits() >> 3; 594 unsigned Tag = DTy.getTag(); 595 596 // FIXME - Workaround for templates. 597 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; 598 599 Buffer.setTag(Tag); 600 601 // Map to main type, void will not have a type. 602 DIType FromTy = DTy.getTypeDerivedFrom(); 603 AddType(DW_Unit, &Buffer, FromTy); 604 605 // Add name if not anonymous or intermediate type. 606 if (!Name.empty()) 607 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 608 609 // Add size if non-zero (derived types might be zero-sized.) 610 if (Size) 611 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 612 613 // Add source line info if available and TyDesc is not a forward declaration. 614 if (!DTy.isForwardDecl()) 615 AddSourceLine(&Buffer, &DTy); 616} 617 618/// ConstructTypeDIE - Construct type DIE from DICompositeType. 619void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 620 DICompositeType CTy) { 621 // Get core information. 622 std::string Name; 623 CTy.getName(Name); 624 625 uint64_t Size = CTy.getSizeInBits() >> 3; 626 unsigned Tag = CTy.getTag(); 627 Buffer.setTag(Tag); 628 629 switch (Tag) { 630 case dwarf::DW_TAG_vector_type: 631 case dwarf::DW_TAG_array_type: 632 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy); 633 break; 634 case dwarf::DW_TAG_enumeration_type: { 635 DIArray Elements = CTy.getTypeArray(); 636 637 // Add enumerators to enumeration type. 638 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 639 DIE *ElemDie = NULL; 640 DIEnumerator Enum(Elements.getElement(i).getGV()); 641 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum); 642 Buffer.AddChild(ElemDie); 643 } 644 } 645 break; 646 case dwarf::DW_TAG_subroutine_type: { 647 // Add return type. 648 DIArray Elements = CTy.getTypeArray(); 649 DIDescriptor RTy = Elements.getElement(0); 650 AddType(DW_Unit, &Buffer, DIType(RTy.getGV())); 651 652 // Add prototype flag. 653 AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); 654 655 // Add arguments. 656 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { 657 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 658 DIDescriptor Ty = Elements.getElement(i); 659 AddType(DW_Unit, Arg, DIType(Ty.getGV())); 660 Buffer.AddChild(Arg); 661 } 662 } 663 break; 664 case dwarf::DW_TAG_structure_type: 665 case dwarf::DW_TAG_union_type: 666 case dwarf::DW_TAG_class_type: { 667 // Add elements to structure type. 668 DIArray Elements = CTy.getTypeArray(); 669 670 // A forward struct declared type may not have elements available. 671 if (Elements.isNull()) 672 break; 673 674 // Add elements to structure type. 675 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 676 DIDescriptor Element = Elements.getElement(i); 677 DIE *ElemDie = NULL; 678 if (Element.getTag() == dwarf::DW_TAG_subprogram) 679 ElemDie = CreateSubprogramDIE(DW_Unit, 680 DISubprogram(Element.getGV())); 681 else 682 ElemDie = CreateMemberDIE(DW_Unit, 683 DIDerivedType(Element.getGV())); 684 Buffer.AddChild(ElemDie); 685 } 686 687 // FIXME: We'd like an API to register additional attributes for the 688 // frontend to use while synthesizing, and then we'd use that api in clang 689 // instead of this. 690 if (Name == "__block_literal_generic") 691 AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1); 692 693 unsigned RLang = CTy.getRunTimeLang(); 694 if (RLang) 695 AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, 696 dwarf::DW_FORM_data1, RLang); 697 break; 698 } 699 default: 700 break; 701 } 702 703 // Add name if not anonymous or intermediate type. 704 if (!Name.empty()) 705 AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 706 707 if (Tag == dwarf::DW_TAG_enumeration_type || 708 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { 709 // Add size if non-zero (derived types might be zero-sized.) 710 if (Size) 711 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 712 else { 713 // Add zero size if it is not a forward declaration. 714 if (CTy.isForwardDecl()) 715 AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 716 else 717 AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); 718 } 719 720 // Add source line info if available. 721 if (!CTy.isForwardDecl()) 722 AddSourceLine(&Buffer, &CTy); 723 } 724} 725 726/// ConstructSubrangeDIE - Construct subrange DIE from DISubrange. 727void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){ 728 int64_t L = SR.getLo(); 729 int64_t H = SR.getHi(); 730 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); 731 732 if (L != H) { 733 AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); 734 if (L) 735 AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L); 736 AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H); 737 } 738 739 Buffer.AddChild(DW_Subrange); 740} 741 742/// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType. 743void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 744 DICompositeType *CTy) { 745 Buffer.setTag(dwarf::DW_TAG_array_type); 746 if (CTy->getTag() == dwarf::DW_TAG_vector_type) 747 AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1); 748 749 // Emit derived type. 750 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom()); 751 DIArray Elements = CTy->getTypeArray(); 752 753 // Construct an anonymous type for index type. 754 DIE IdxBuffer(dwarf::DW_TAG_base_type); 755 AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); 756 AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 757 dwarf::DW_ATE_signed); 758 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer); 759 760 // Add subranges to array type. 761 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 762 DIDescriptor Element = Elements.getElement(i); 763 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 764 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy); 765 } 766} 767 768/// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 769DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) { 770 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); 771 std::string Name; 772 ETy->getName(Name); 773 AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 774 int64_t Value = ETy->getEnumValue(); 775 AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); 776 return Enumerator; 777} 778 779/// CreateGlobalVariableDIE - Create new DIE using GV. 780DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit, 781 const DIGlobalVariable &GV) { 782 DIE *GVDie = new DIE(dwarf::DW_TAG_variable); 783 std::string Name; 784 GV.getDisplayName(Name); 785 AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 786 std::string LinkageName; 787 GV.getLinkageName(LinkageName); 788 if (!LinkageName.empty()) 789 AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, 790 LinkageName); 791 AddType(DW_Unit, GVDie, GV.getType()); 792 if (!GV.isLocalToUnit()) 793 AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); 794 AddSourceLine(GVDie, &GV); 795 return GVDie; 796} 797 798/// CreateMemberDIE - Create new member DIE. 799DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){ 800 DIE *MemberDie = new DIE(DT.getTag()); 801 std::string Name; 802 DT.getName(Name); 803 if (!Name.empty()) 804 AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 805 806 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom()); 807 808 AddSourceLine(MemberDie, &DT); 809 810 uint64_t Size = DT.getSizeInBits(); 811 uint64_t FieldSize = DT.getOriginalTypeSize(); 812 813 if (Size != FieldSize) { 814 // Handle bitfield. 815 AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); 816 AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); 817 818 uint64_t Offset = DT.getOffsetInBits(); 819 uint64_t FieldOffset = Offset; 820 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 821 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 822 FieldOffset = (HiMark - FieldSize); 823 Offset -= FieldOffset; 824 825 // Maybe we need to work from the other end. 826 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size); 827 AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); 828 } 829 830 DIEBlock *Block = new DIEBlock(); 831 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 832 AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); 833 AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block); 834 835 if (DT.isProtected()) 836 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0, 837 dwarf::DW_ACCESS_protected); 838 else if (DT.isPrivate()) 839 AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0, 840 dwarf::DW_ACCESS_private); 841 842 return MemberDie; 843} 844 845/// CreateSubprogramDIE - Create new DIE using SP. 846DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit, 847 const DISubprogram &SP, 848 bool IsConstructor, 849 bool IsInlined) { 850 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram); 851 852 std::string Name; 853 SP.getName(Name); 854 AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 855 856 std::string LinkageName; 857 SP.getLinkageName(LinkageName); 858 if (!LinkageName.empty()) 859 AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, 860 LinkageName); 861 AddSourceLine(SPDie, &SP); 862 863 DICompositeType SPTy = SP.getType(); 864 DIArray Args = SPTy.getTypeArray(); 865 866 // Add prototyped tag, if C or ObjC. 867 unsigned Lang = SP.getCompileUnit().getLanguage(); 868 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 || 869 Lang == dwarf::DW_LANG_ObjC) 870 AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); 871 872 // Add Return Type. 873 unsigned SPTag = SPTy.getTag(); 874 if (!IsConstructor) { 875 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type) 876 AddType(DW_Unit, SPDie, SPTy); 877 else 878 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV())); 879 } 880 881 if (!SP.isDefinition()) { 882 AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 883 884 // Add arguments. Do not add arguments for subprogram definition. They will 885 // be handled through RecordVariable. 886 if (SPTag == dwarf::DW_TAG_subroutine_type) 887 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 888 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 889 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV())); 890 AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ?? 891 SPDie->AddChild(Arg); 892 } 893 } 894 895 if (!SP.isLocalToUnit() && !IsInlined) 896 AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); 897 898 // DW_TAG_inlined_subroutine may refer to this DIE. 899 DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV()); 900 Slot = SPDie; 901 return SPDie; 902} 903 904/// FindCompileUnit - Get the compile unit for the given descriptor. 905/// 906CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const { 907 DenseMap<Value *, CompileUnit *>::const_iterator I = 908 CompileUnitMap.find(Unit.getGV()); 909 assert(I != CompileUnitMap.end() && "Missing compile unit."); 910 return *I->second; 911} 912 913/// CreateDbgScopeVariable - Create a new scope variable. 914/// 915DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) { 916 // Get the descriptor. 917 const DIVariable &VD = DV->getVariable(); 918 919 // Translate tag to proper Dwarf tag. The result variable is dropped for 920 // now. 921 unsigned Tag; 922 switch (VD.getTag()) { 923 case dwarf::DW_TAG_return_variable: 924 return NULL; 925 case dwarf::DW_TAG_arg_variable: 926 Tag = dwarf::DW_TAG_formal_parameter; 927 break; 928 case dwarf::DW_TAG_auto_variable: // fall thru 929 default: 930 Tag = dwarf::DW_TAG_variable; 931 break; 932 } 933 934 // Define variable debug information entry. 935 DIE *VariableDie = new DIE(Tag); 936 std::string Name; 937 VD.getName(Name); 938 AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 939 940 // Add source line info if available. 941 AddSourceLine(VariableDie, &VD); 942 943 // Add variable type. 944 AddType(Unit, VariableDie, VD.getType()); 945 946 // Add variable address. 947 if (!DV->isInlinedFnVar()) { 948 // Variables for abstract instances of inlined functions don't get a 949 // location. 950 MachineLocation Location; 951 Location.set(RI->getFrameRegister(*MF), 952 RI->getFrameIndexOffset(*MF, DV->getFrameIndex())); 953 AddAddress(VariableDie, dwarf::DW_AT_location, Location); 954 } 955 956 return VariableDie; 957} 958 959/// getOrCreateScope - Returns the scope associated with the given descriptor. 960/// 961DbgScope *DwarfDebug::getOrCreateScope(GlobalVariable *V) { 962 DbgScope *&Slot = DbgScopeMap[V]; 963 if (Slot) return Slot; 964 965 DbgScope *Parent = NULL; 966 DIBlock Block(V); 967 968 // Don't create a new scope if we already created one for an inlined function. 969 DenseMap<const GlobalVariable *, DbgScope *>::iterator 970 II = AbstractInstanceRootMap.find(V); 971 if (II != AbstractInstanceRootMap.end()) 972 return LexicalScopeStack.back(); 973 974 if (!Block.isNull()) { 975 DIDescriptor ParentDesc = Block.getContext(); 976 Parent = 977 ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV()); 978 } 979 980 Slot = new DbgScope(Parent, DIDescriptor(V)); 981 982 if (Parent) 983 Parent->AddScope(Slot); 984 else 985 // First function is top level function. 986 FunctionDbgScope = Slot; 987 988 return Slot; 989} 990 991/// ConstructDbgScope - Construct the components of a scope. 992/// 993void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope, 994 unsigned ParentStartID, 995 unsigned ParentEndID, 996 DIE *ParentDie, CompileUnit *Unit) { 997 // Add variables to scope. 998 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables(); 999 for (unsigned i = 0, N = Variables.size(); i < N; ++i) { 1000 DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit); 1001 if (VariableDie) ParentDie->AddChild(VariableDie); 1002 } 1003 1004 // Add concrete instances to scope. 1005 SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = 1006 ParentScope->getConcreteInsts(); 1007 for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) { 1008 DbgConcreteScope *ConcreteInst = ConcreteInsts[i]; 1009 DIE *Die = ConcreteInst->getDie(); 1010 1011 unsigned StartID = ConcreteInst->getStartLabelID(); 1012 unsigned EndID = ConcreteInst->getEndLabelID(); 1013 1014 // Add the scope bounds. 1015 if (StartID) 1016 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1017 DWLabel("label", StartID)); 1018 else 1019 AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1020 DWLabel("func_begin", SubprogramCount)); 1021 1022 if (EndID) 1023 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1024 DWLabel("label", EndID)); 1025 else 1026 AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1027 DWLabel("func_end", SubprogramCount)); 1028 1029 ParentDie->AddChild(Die); 1030 } 1031 1032 // Add nested scopes. 1033 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes(); 1034 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { 1035 // Define the Scope debug information entry. 1036 DbgScope *Scope = Scopes[j]; 1037 1038 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID()); 1039 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID()); 1040 1041 // Ignore empty scopes. 1042 if (StartID == EndID && StartID != 0) continue; 1043 1044 // Do not ignore inlined scopes even if they don't have any variables or 1045 // scopes. 1046 if (Scope->getScopes().empty() && Scope->getVariables().empty() && 1047 Scope->getConcreteInsts().empty()) 1048 continue; 1049 1050 if (StartID == ParentStartID && EndID == ParentEndID) { 1051 // Just add stuff to the parent scope. 1052 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit); 1053 } else { 1054 DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block); 1055 1056 // Add the scope bounds. 1057 if (StartID) 1058 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1059 DWLabel("label", StartID)); 1060 else 1061 AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1062 DWLabel("func_begin", SubprogramCount)); 1063 1064 if (EndID) 1065 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1066 DWLabel("label", EndID)); 1067 else 1068 AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1069 DWLabel("func_end", SubprogramCount)); 1070 1071 // Add the scope's contents. 1072 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit); 1073 ParentDie->AddChild(ScopeDie); 1074 } 1075 } 1076} 1077 1078/// ConstructFunctionDbgScope - Construct the scope for the subprogram. 1079/// 1080void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope, 1081 bool AbstractScope) { 1082 // Exit if there is no root scope. 1083 if (!RootScope) return; 1084 DIDescriptor Desc = RootScope->getDesc(); 1085 if (Desc.isNull()) 1086 return; 1087 1088 // Get the subprogram debug information entry. 1089 DISubprogram SPD(Desc.getGV()); 1090 1091 // Get the subprogram die. 1092 DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getGV()); 1093 assert(SPDie && "Missing subprogram descriptor"); 1094 1095 if (!AbstractScope) { 1096 // Add the function bounds. 1097 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1098 DWLabel("func_begin", SubprogramCount)); 1099 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1100 DWLabel("func_end", SubprogramCount)); 1101 MachineLocation Location(RI->getFrameRegister(*MF)); 1102 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location); 1103 } 1104 1105 ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU); 1106} 1107 1108/// ConstructDefaultDbgScope - Construct a default scope for the subprogram. 1109/// 1110void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) { 1111 const char *FnName = MF->getFunction()->getNameStart(); 1112 StringMap<DIE*> &Globals = ModuleCU->getGlobals(); 1113 StringMap<DIE*>::iterator GI = Globals.find(FnName); 1114 if (GI != Globals.end()) { 1115 DIE *SPDie = GI->second; 1116 1117 // Add the function bounds. 1118 AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 1119 DWLabel("func_begin", SubprogramCount)); 1120 AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, 1121 DWLabel("func_end", SubprogramCount)); 1122 1123 MachineLocation Location(RI->getFrameRegister(*MF)); 1124 AddAddress(SPDie, dwarf::DW_AT_frame_base, Location); 1125 } 1126} 1127 1128/// GetOrCreateSourceID - Look up the source id with the given directory and 1129/// source file names. If none currently exists, create a new id and insert it 1130/// in the SourceIds map. This can update DirectoryNames and SourceFileNames 1131/// maps as well. 1132unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName, 1133 const std::string &FileName) { 1134 unsigned DId; 1135 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName); 1136 if (DI != DirectoryIdMap.end()) { 1137 DId = DI->getValue(); 1138 } else { 1139 DId = DirectoryNames.size() + 1; 1140 DirectoryIdMap[DirName] = DId; 1141 DirectoryNames.push_back(DirName); 1142 } 1143 1144 unsigned FId; 1145 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName); 1146 if (FI != SourceFileIdMap.end()) { 1147 FId = FI->getValue(); 1148 } else { 1149 FId = SourceFileNames.size() + 1; 1150 SourceFileIdMap[FileName] = FId; 1151 SourceFileNames.push_back(FileName); 1152 } 1153 1154 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI = 1155 SourceIdMap.find(std::make_pair(DId, FId)); 1156 if (SI != SourceIdMap.end()) 1157 return SI->second; 1158 1159 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0. 1160 SourceIdMap[std::make_pair(DId, FId)] = SrcId; 1161 SourceIds.push_back(std::make_pair(DId, FId)); 1162 1163 return SrcId; 1164} 1165 1166void DwarfDebug::ConstructCompileUnit(GlobalVariable *GV) { 1167 DICompileUnit DIUnit(GV); 1168 std::string Dir, FN, Prod; 1169 unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir), 1170 DIUnit.getFilename(FN)); 1171 1172 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); 1173 AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 1174 DWLabel("section_line", 0), DWLabel("section_line", 0), 1175 false); 1176 AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, 1177 DIUnit.getProducer(Prod)); 1178 AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1, 1179 DIUnit.getLanguage()); 1180 AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN); 1181 1182 if (!Dir.empty()) 1183 AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir); 1184 if (DIUnit.isOptimized()) 1185 AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); 1186 1187 std::string Flags; 1188 DIUnit.getFlags(Flags); 1189 if (!Flags.empty()) 1190 AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags); 1191 1192 unsigned RVer = DIUnit.getRunTimeVersion(); 1193 if (RVer) 1194 AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 1195 dwarf::DW_FORM_data1, RVer); 1196 1197 CompileUnit *Unit = new CompileUnit(ID, Die); 1198 if (!ModuleCU && DIUnit.isMain()) { 1199 // Use first compile unit marked as isMain as the compile unit 1200 // for this module. 1201 ModuleCU = Unit; 1202 } 1203 1204 CompileUnitMap[DIUnit.getGV()] = Unit; 1205 CompileUnits.push_back(Unit); 1206} 1207 1208void DwarfDebug::ConstructGlobalVariableDIE(GlobalVariable *GV) { 1209 DIGlobalVariable DI_GV(GV); 1210 1211 // Check for pre-existence. 1212 DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getGV()); 1213 if (Slot) 1214 return; 1215 1216 DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV); 1217 1218 // Add address. 1219 DIEBlock *Block = new DIEBlock(); 1220 AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 1221 std::string GLN; 1222 AddObjectLabel(Block, 0, dwarf::DW_FORM_udata, 1223 Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN)); 1224 AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block); 1225 1226 // Add to map. 1227 Slot = VariableDie; 1228 1229 // Add to context owner. 1230 ModuleCU->getDie()->AddChild(VariableDie); 1231 1232 // Expose as global. FIXME - need to check external flag. 1233 std::string Name; 1234 ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie); 1235 return; 1236} 1237 1238void DwarfDebug::ConstructSubprogram(GlobalVariable *GV) { 1239 DISubprogram SP(GV); 1240 1241 // Check for pre-existence. 1242 DIE *&Slot = ModuleCU->getDieMapSlotFor(GV); 1243 if (Slot) 1244 return; 1245 1246 if (!SP.isDefinition()) 1247 // This is a method declaration which will be handled while constructing 1248 // class type. 1249 return; 1250 1251 DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP); 1252 1253 // Add to map. 1254 Slot = SubprogramDie; 1255 1256 // Add to context owner. 1257 ModuleCU->getDie()->AddChild(SubprogramDie); 1258 1259 // Expose as global. 1260 std::string Name; 1261 ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie); 1262 return; 1263} 1264 1265 /// BeginModule - Emit all Dwarf sections that should come prior to the 1266 /// content. Create global DIEs and emit initial debug info sections. 1267 /// This is inovked by the target AsmPrinter. 1268void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) { 1269 this->M = M; 1270 1271 if (TimePassesIsEnabled) 1272 DebugTimer->startTimer(); 1273 1274 SmallVector<GlobalVariable *, 2> CUs; 1275 SmallVector<GlobalVariable *, 4> GVs; 1276 SmallVector<GlobalVariable *, 4> SPs; 1277 CollectDebugInfoAnchors(*M, CUs, GVs, SPs); 1278 1279 // Create all the compile unit DIEs. 1280 for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(), 1281 E = CUs.end(); I != E; ++I) 1282 ConstructCompileUnit(*I); 1283 1284 if (CompileUnits.empty()) { 1285 if (TimePassesIsEnabled) 1286 DebugTimer->stopTimer(); 1287 1288 return; 1289 } 1290 1291 // If main compile unit for this module is not seen than randomly 1292 // select first compile unit. 1293 if (!ModuleCU) 1294 ModuleCU = CompileUnits[0]; 1295 1296 // If there is not any debug info available for any global variables and any 1297 // subprograms then there is not any debug info to emit. 1298 if (GVs.empty() && SPs.empty()) { 1299 if (TimePassesIsEnabled) 1300 DebugTimer->stopTimer(); 1301 1302 return; 1303 } 1304 1305 // Create DIEs for each of the externally visible global variables. 1306 for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(), 1307 E = GVs.end(); I != E; ++I) 1308 ConstructGlobalVariableDIE(*I); 1309 1310 // Create DIEs for each of the externally visible subprograms. 1311 for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(), 1312 E = SPs.end(); I != E; ++I) 1313 ConstructSubprogram(*I); 1314 1315 MMI = mmi; 1316 shouldEmit = true; 1317 MMI->setDebugInfoAvailability(true); 1318 1319 // Prime section data. 1320 SectionMap.insert(TAI->getTextSection()); 1321 1322 // Print out .file directives to specify files for .loc directives. These are 1323 // printed out early so that they precede any .loc directives. 1324 if (TAI->hasDotLocAndDotFile()) { 1325 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) { 1326 // Remember source id starts at 1. 1327 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i); 1328 sys::Path FullPath(getSourceDirectoryName(Id.first)); 1329 bool AppendOk = 1330 FullPath.appendComponent(getSourceFileName(Id.second)); 1331 assert(AppendOk && "Could not append filename to directory!"); 1332 AppendOk = false; 1333 Asm->EmitFile(i, FullPath.toString()); 1334 Asm->EOL(); 1335 } 1336 } 1337 1338 // Emit initial sections 1339 EmitInitial(); 1340 1341 if (TimePassesIsEnabled) 1342 DebugTimer->stopTimer(); 1343} 1344 1345/// EndModule - Emit all Dwarf sections that should come after the content. 1346/// 1347void DwarfDebug::EndModule() { 1348 if (!ShouldEmitDwarfDebug()) 1349 return; 1350 1351 if (TimePassesIsEnabled) 1352 DebugTimer->startTimer(); 1353 1354 // Standard sections final addresses. 1355 Asm->SwitchToSection(TAI->getTextSection()); 1356 EmitLabel("text_end", 0); 1357 Asm->SwitchToSection(TAI->getDataSection()); 1358 EmitLabel("data_end", 0); 1359 1360 // End text sections. 1361 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) { 1362 Asm->SwitchToSection(SectionMap[i]); 1363 EmitLabel("section_end", i); 1364 } 1365 1366 // Emit common frame information. 1367 EmitCommonDebugFrame(); 1368 1369 // Emit function debug frame information 1370 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(), 1371 E = DebugFrames.end(); I != E; ++I) 1372 EmitFunctionDebugFrame(*I); 1373 1374 // Compute DIE offsets and sizes. 1375 SizeAndOffsets(); 1376 1377 // Emit all the DIEs into a debug info section 1378 EmitDebugInfo(); 1379 1380 // Corresponding abbreviations into a abbrev section. 1381 EmitAbbreviations(); 1382 1383 // Emit source line correspondence into a debug line section. 1384 EmitDebugLines(); 1385 1386 // Emit info into a debug pubnames section. 1387 EmitDebugPubNames(); 1388 1389 // Emit info into a debug str section. 1390 EmitDebugStr(); 1391 1392 // Emit info into a debug loc section. 1393 EmitDebugLoc(); 1394 1395 // Emit info into a debug aranges section. 1396 EmitDebugARanges(); 1397 1398 // Emit info into a debug ranges section. 1399 EmitDebugRanges(); 1400 1401 // Emit info into a debug macinfo section. 1402 EmitDebugMacInfo(); 1403 1404 // Emit inline info. 1405 EmitDebugInlineInfo(); 1406 1407 if (TimePassesIsEnabled) 1408 DebugTimer->stopTimer(); 1409} 1410 1411/// BeginFunction - Gather pre-function debug information. Assumes being 1412/// emitted immediately after the function entry point. 1413void DwarfDebug::BeginFunction(MachineFunction *MF) { 1414 this->MF = MF; 1415 1416 if (!ShouldEmitDwarfDebug()) return; 1417 1418 if (TimePassesIsEnabled) 1419 DebugTimer->startTimer(); 1420 1421 // Begin accumulating function debug information. 1422 MMI->BeginFunction(MF); 1423 1424 // Assumes in correct section after the entry point. 1425 EmitLabel("func_begin", ++SubprogramCount); 1426 1427 // Emit label for the implicitly defined dbg.stoppoint at the start of the 1428 // function. 1429 DebugLoc FDL = MF->getDefaultDebugLoc(); 1430 if (!FDL.isUnknown()) { 1431 DebugLocTuple DLT = MF->getDebugLocTuple(FDL); 1432 unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, 1433 DICompileUnit(DLT.CompileUnit)); 1434 Asm->printLabel(LabelID); 1435 } 1436 1437 if (TimePassesIsEnabled) 1438 DebugTimer->stopTimer(); 1439} 1440 1441/// EndFunction - Gather and emit post-function debug information. 1442/// 1443void DwarfDebug::EndFunction(MachineFunction *MF) { 1444 if (!ShouldEmitDwarfDebug()) return; 1445 1446 if (TimePassesIsEnabled) 1447 DebugTimer->startTimer(); 1448 1449 // Define end label for subprogram. 1450 EmitLabel("func_end", SubprogramCount); 1451 1452 // Get function line info. 1453 if (!Lines.empty()) { 1454 // Get section line info. 1455 unsigned ID = SectionMap.insert(Asm->CurrentSection_); 1456 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID); 1457 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1]; 1458 // Append the function info to section info. 1459 SectionLineInfos.insert(SectionLineInfos.end(), 1460 Lines.begin(), Lines.end()); 1461 } 1462 1463 // Construct the DbgScope for abstract instances. 1464 for (SmallVector<DbgScope *, 32>::iterator 1465 I = AbstractInstanceRootList.begin(), 1466 E = AbstractInstanceRootList.end(); I != E; ++I) 1467 ConstructFunctionDbgScope(*I); 1468 1469 // Construct scopes for subprogram. 1470 if (FunctionDbgScope) 1471 ConstructFunctionDbgScope(FunctionDbgScope); 1472 else 1473 // FIXME: This is wrong. We are essentially getting past a problem with 1474 // debug information not being able to handle unreachable blocks that have 1475 // debug information in them. In particular, those unreachable blocks that 1476 // have "region end" info in them. That situation results in the "root 1477 // scope" not being created. If that's the case, then emit a "default" 1478 // scope, i.e., one that encompasses the whole function. This isn't 1479 // desirable. And a better way of handling this (and all of the debugging 1480 // information) needs to be explored. 1481 ConstructDefaultDbgScope(MF); 1482 1483 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount, 1484 MMI->getFrameMoves())); 1485 1486 // Clear debug info 1487 if (FunctionDbgScope) { 1488 delete FunctionDbgScope; 1489 DbgScopeMap.clear(); 1490 DbgAbstractScopeMap.clear(); 1491 DbgConcreteScopeMap.clear(); 1492 InlinedVariableScopes.clear(); 1493 FunctionDbgScope = NULL; 1494 LexicalScopeStack.clear(); 1495 AbstractInstanceRootList.clear(); 1496 AbstractInstanceRootMap.clear(); 1497 } 1498 1499 Lines.clear(); 1500 1501 if (TimePassesIsEnabled) 1502 DebugTimer->stopTimer(); 1503} 1504 1505/// RecordSourceLine - Records location information and associates it with a 1506/// label. Returns a unique label ID used to generate a label and provide 1507/// correspondence to the source line list. 1508unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) { 1509 if (TimePassesIsEnabled) 1510 DebugTimer->startTimer(); 1511 1512 CompileUnit *Unit = CompileUnitMap[V]; 1513 assert(Unit && "Unable to find CompileUnit"); 1514 unsigned ID = MMI->NextLabelID(); 1515 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID)); 1516 1517 if (TimePassesIsEnabled) 1518 DebugTimer->stopTimer(); 1519 1520 return ID; 1521} 1522 1523/// RecordSourceLine - Records location information and associates it with a 1524/// label. Returns a unique label ID used to generate a label and provide 1525/// correspondence to the source line list. 1526unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col, 1527 DICompileUnit CU) { 1528 if (TimePassesIsEnabled) 1529 DebugTimer->startTimer(); 1530 1531 std::string Dir, Fn; 1532 unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir), 1533 CU.getFilename(Fn)); 1534 unsigned ID = MMI->NextLabelID(); 1535 Lines.push_back(SrcLineInfo(Line, Col, Src, ID)); 1536 1537 if (TimePassesIsEnabled) 1538 DebugTimer->stopTimer(); 1539 1540 return ID; 1541} 1542 1543/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be 1544/// timed. Look up the source id with the given directory and source file 1545/// names. If none currently exists, create a new id and insert it in the 1546/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as 1547/// well. 1548unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName, 1549 const std::string &FileName) { 1550 if (TimePassesIsEnabled) 1551 DebugTimer->startTimer(); 1552 1553 unsigned SrcId = GetOrCreateSourceID(DirName, FileName); 1554 1555 if (TimePassesIsEnabled) 1556 DebugTimer->stopTimer(); 1557 1558 return SrcId; 1559} 1560 1561/// RecordRegionStart - Indicate the start of a region. 1562unsigned DwarfDebug::RecordRegionStart(GlobalVariable *V) { 1563 if (TimePassesIsEnabled) 1564 DebugTimer->startTimer(); 1565 1566 DbgScope *Scope = getOrCreateScope(V); 1567 unsigned ID = MMI->NextLabelID(); 1568 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID); 1569 LexicalScopeStack.push_back(Scope); 1570 1571 if (TimePassesIsEnabled) 1572 DebugTimer->stopTimer(); 1573 1574 return ID; 1575} 1576 1577/// RecordRegionEnd - Indicate the end of a region. 1578unsigned DwarfDebug::RecordRegionEnd(GlobalVariable *V) { 1579 if (TimePassesIsEnabled) 1580 DebugTimer->startTimer(); 1581 1582 DbgScope *Scope = getOrCreateScope(V); 1583 unsigned ID = MMI->NextLabelID(); 1584 Scope->setEndLabelID(ID); 1585 // FIXME : region.end() may not be in the last basic block. 1586 // For now, do not pop last lexical scope because next basic 1587 // block may start new inlined function's body. 1588 unsigned LSSize = LexicalScopeStack.size(); 1589 if (LSSize != 0 && LSSize != 1) 1590 LexicalScopeStack.pop_back(); 1591 1592 if (TimePassesIsEnabled) 1593 DebugTimer->stopTimer(); 1594 1595 return ID; 1596} 1597 1598/// RecordVariable - Indicate the declaration of a local variable. 1599void DwarfDebug::RecordVariable(GlobalVariable *GV, unsigned FrameIndex, 1600 const MachineInstr *MI) { 1601 if (TimePassesIsEnabled) 1602 DebugTimer->startTimer(); 1603 1604 DIDescriptor Desc(GV); 1605 DbgScope *Scope = NULL; 1606 bool InlinedFnVar = false; 1607 1608 if (Desc.getTag() == dwarf::DW_TAG_variable) { 1609 // GV is a global variable. 1610 DIGlobalVariable DG(GV); 1611 Scope = getOrCreateScope(DG.getContext().getGV()); 1612 } else { 1613 DenseMap<const MachineInstr *, DbgScope *>::iterator 1614 SI = InlinedVariableScopes.find(MI); 1615 1616 if (SI != InlinedVariableScopes.end()) { 1617 // or GV is an inlined local variable. 1618 Scope = SI->second; 1619 InlinedFnVar = true; 1620 } else { 1621 DIVariable DV(GV); 1622 GlobalVariable *V = DV.getContext().getGV(); 1623 1624 // or GV is a local variable. 1625 Scope = getOrCreateScope(V); 1626 } 1627 } 1628 1629 assert(Scope && "Unable to find the variable's scope"); 1630 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex, InlinedFnVar); 1631 Scope->AddVariable(DV); 1632 1633 if (TimePassesIsEnabled) 1634 DebugTimer->stopTimer(); 1635} 1636 1637//// RecordInlinedFnStart - Indicate the start of inlined subroutine. 1638unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU, 1639 unsigned Line, unsigned Col) { 1640 unsigned LabelID = MMI->NextLabelID(); 1641 1642 if (!TAI->doesDwarfUsesInlineInfoSection()) 1643 return LabelID; 1644 1645 if (TimePassesIsEnabled) 1646 DebugTimer->startTimer(); 1647 1648 GlobalVariable *GV = SP.getGV(); 1649 DenseMap<const GlobalVariable *, DbgScope *>::iterator 1650 II = AbstractInstanceRootMap.find(GV); 1651 1652 if (II == AbstractInstanceRootMap.end()) { 1653 // Create an abstract instance entry for this inlined function if it doesn't 1654 // already exist. 1655 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV)); 1656 1657 // Get the compile unit context. 1658 DIE *SPDie = ModuleCU->getDieMapSlotFor(GV); 1659 if (!SPDie) 1660 SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true); 1661 1662 // Mark as being inlined. This makes this subprogram entry an abstract 1663 // instance root. 1664 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only 1665 // that it's defined. That probably won't change in the future. However, 1666 // this could be more elegant. 1667 AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined); 1668 1669 // Keep track of the abstract scope for this function. 1670 DbgAbstractScopeMap[GV] = Scope; 1671 1672 AbstractInstanceRootMap[GV] = Scope; 1673 AbstractInstanceRootList.push_back(Scope); 1674 } 1675 1676 // Create a concrete inlined instance for this inlined function. 1677 DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV)); 1678 DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine); 1679 ScopeDie->setAbstractCompileUnit(ModuleCU); 1680 1681 DIE *Origin = ModuleCU->getDieMapSlotFor(GV); 1682 AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin, 1683 dwarf::DW_FORM_ref4, Origin); 1684 AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID()); 1685 AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line); 1686 AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col); 1687 1688 ConcreteScope->setDie(ScopeDie); 1689 ConcreteScope->setStartLabelID(LabelID); 1690 MMI->RecordUsedDbgLabel(LabelID); 1691 1692 LexicalScopeStack.back()->AddConcreteInst(ConcreteScope); 1693 1694 // Keep track of the concrete scope that's inlined into this function. 1695 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator 1696 SI = DbgConcreteScopeMap.find(GV); 1697 1698 if (SI == DbgConcreteScopeMap.end()) 1699 DbgConcreteScopeMap[GV].push_back(ConcreteScope); 1700 else 1701 SI->second.push_back(ConcreteScope); 1702 1703 // Track the start label for this inlined function. 1704 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator 1705 I = InlineInfo.find(GV); 1706 1707 if (I == InlineInfo.end()) 1708 InlineInfo[GV].push_back(LabelID); 1709 else 1710 I->second.push_back(LabelID); 1711 1712 if (TimePassesIsEnabled) 1713 DebugTimer->stopTimer(); 1714 1715 return LabelID; 1716} 1717 1718/// RecordInlinedFnEnd - Indicate the end of inlined subroutine. 1719unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) { 1720 if (!TAI->doesDwarfUsesInlineInfoSection()) 1721 return 0; 1722 1723 if (TimePassesIsEnabled) 1724 DebugTimer->startTimer(); 1725 1726 GlobalVariable *GV = SP.getGV(); 1727 DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator 1728 I = DbgConcreteScopeMap.find(GV); 1729 1730 if (I == DbgConcreteScopeMap.end()) { 1731 // FIXME: Can this situation actually happen? And if so, should it? 1732 if (TimePassesIsEnabled) 1733 DebugTimer->stopTimer(); 1734 1735 return 0; 1736 } 1737 1738 SmallVector<DbgScope *, 8> &Scopes = I->second; 1739 if (Scopes.empty()) { 1740 // Returned ID is 0 if this is unbalanced "end of inlined 1741 // scope". This could happen if optimizer eats dbg intrinsics 1742 // or "beginning of inlined scope" is not recoginized due to 1743 // missing location info. In such cases, ignore this region.end. 1744 return 0; 1745 } 1746 1747 DbgScope *Scope = Scopes.back(); Scopes.pop_back(); 1748 unsigned ID = MMI->NextLabelID(); 1749 MMI->RecordUsedDbgLabel(ID); 1750 Scope->setEndLabelID(ID); 1751 1752 if (TimePassesIsEnabled) 1753 DebugTimer->stopTimer(); 1754 1755 return ID; 1756} 1757 1758/// RecordVariableScope - Record scope for the variable declared by 1759/// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes 1760/// for only inlined subroutine variables. Other variables's scopes are 1761/// determined during RecordVariable(). 1762void DwarfDebug::RecordVariableScope(DIVariable &DV, 1763 const MachineInstr *DeclareMI) { 1764 if (TimePassesIsEnabled) 1765 DebugTimer->startTimer(); 1766 1767 DISubprogram SP(DV.getContext().getGV()); 1768 1769 if (SP.isNull()) { 1770 if (TimePassesIsEnabled) 1771 DebugTimer->stopTimer(); 1772 1773 return; 1774 } 1775 1776 DenseMap<GlobalVariable *, DbgScope *>::iterator 1777 I = DbgAbstractScopeMap.find(SP.getGV()); 1778 if (I != DbgAbstractScopeMap.end()) 1779 InlinedVariableScopes[DeclareMI] = I->second; 1780 1781 if (TimePassesIsEnabled) 1782 DebugTimer->stopTimer(); 1783} 1784 1785//===----------------------------------------------------------------------===// 1786// Emit Methods 1787//===----------------------------------------------------------------------===// 1788 1789/// SizeAndOffsetDie - Compute the size and offset of a DIE. 1790/// 1791unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) { 1792 // Get the children. 1793 const std::vector<DIE *> &Children = Die->getChildren(); 1794 1795 // If not last sibling and has children then add sibling offset attribute. 1796 if (!Last && !Children.empty()) Die->AddSiblingOffset(); 1797 1798 // Record the abbreviation. 1799 AssignAbbrevNumber(Die->getAbbrev()); 1800 1801 // Get the abbreviation for this DIE. 1802 unsigned AbbrevNumber = Die->getAbbrevNumber(); 1803 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; 1804 1805 // Set DIE offset 1806 Die->setOffset(Offset); 1807 1808 // Start the size with the size of abbreviation code. 1809 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber); 1810 1811 const SmallVector<DIEValue*, 32> &Values = Die->getValues(); 1812 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); 1813 1814 // Size the DIE attribute values. 1815 for (unsigned i = 0, N = Values.size(); i < N; ++i) 1816 // Size attribute value. 1817 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm()); 1818 1819 // Size the DIE children if any. 1820 if (!Children.empty()) { 1821 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes && 1822 "Children flag not set"); 1823 1824 for (unsigned j = 0, M = Children.size(); j < M; ++j) 1825 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M); 1826 1827 // End of children marker. 1828 Offset += sizeof(int8_t); 1829 } 1830 1831 Die->setSize(Offset - Die->getOffset()); 1832 return Offset; 1833} 1834 1835/// SizeAndOffsets - Compute the size and offset of all the DIEs. 1836/// 1837void DwarfDebug::SizeAndOffsets() { 1838 // Compute size of compile unit header. 1839 static unsigned Offset = 1840 sizeof(int32_t) + // Length of Compilation Unit Info 1841 sizeof(int16_t) + // DWARF version number 1842 sizeof(int32_t) + // Offset Into Abbrev. Section 1843 sizeof(int8_t); // Pointer Size (in bytes) 1844 1845 SizeAndOffsetDie(ModuleCU->getDie(), Offset, true); 1846 CompileUnitOffsets[ModuleCU] = 0; 1847} 1848 1849/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc 1850/// tools to recognize the object file contains Dwarf information. 1851void DwarfDebug::EmitInitial() { 1852 // Check to see if we already emitted intial headers. 1853 if (didInitial) return; 1854 didInitial = true; 1855 1856 // Dwarf sections base addresses. 1857 if (TAI->doesDwarfRequireFrameSection()) { 1858 Asm->SwitchToDataSection(TAI->getDwarfFrameSection()); 1859 EmitLabel("section_debug_frame", 0); 1860 } 1861 1862 Asm->SwitchToDataSection(TAI->getDwarfInfoSection()); 1863 EmitLabel("section_info", 0); 1864 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection()); 1865 EmitLabel("section_abbrev", 0); 1866 Asm->SwitchToDataSection(TAI->getDwarfARangesSection()); 1867 EmitLabel("section_aranges", 0); 1868 1869 if (const char *LineInfoDirective = TAI->getDwarfMacroInfoSection()) { 1870 Asm->SwitchToDataSection(LineInfoDirective); 1871 EmitLabel("section_macinfo", 0); 1872 } 1873 1874 Asm->SwitchToDataSection(TAI->getDwarfLineSection()); 1875 EmitLabel("section_line", 0); 1876 Asm->SwitchToDataSection(TAI->getDwarfLocSection()); 1877 EmitLabel("section_loc", 0); 1878 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection()); 1879 EmitLabel("section_pubnames", 0); 1880 Asm->SwitchToDataSection(TAI->getDwarfStrSection()); 1881 EmitLabel("section_str", 0); 1882 Asm->SwitchToDataSection(TAI->getDwarfRangesSection()); 1883 EmitLabel("section_ranges", 0); 1884 1885 Asm->SwitchToSection(TAI->getTextSection()); 1886 EmitLabel("text_begin", 0); 1887 Asm->SwitchToSection(TAI->getDataSection()); 1888 EmitLabel("data_begin", 0); 1889} 1890 1891/// EmitDIE - Recusively Emits a debug information entry. 1892/// 1893void DwarfDebug::EmitDIE(DIE *Die) { 1894 // Get the abbreviation for this DIE. 1895 unsigned AbbrevNumber = Die->getAbbrevNumber(); 1896 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; 1897 1898 Asm->EOL(); 1899 1900 // Emit the code (index) for the abbreviation. 1901 Asm->EmitULEB128Bytes(AbbrevNumber); 1902 1903 if (Asm->isVerbose()) 1904 Asm->EOL(std::string("Abbrev [" + 1905 utostr(AbbrevNumber) + 1906 "] 0x" + utohexstr(Die->getOffset()) + 1907 ":0x" + utohexstr(Die->getSize()) + " " + 1908 dwarf::TagString(Abbrev->getTag()))); 1909 else 1910 Asm->EOL(); 1911 1912 SmallVector<DIEValue*, 32> &Values = Die->getValues(); 1913 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); 1914 1915 // Emit the DIE attribute values. 1916 for (unsigned i = 0, N = Values.size(); i < N; ++i) { 1917 unsigned Attr = AbbrevData[i].getAttribute(); 1918 unsigned Form = AbbrevData[i].getForm(); 1919 assert(Form && "Too many attributes for DIE (check abbreviation)"); 1920 1921 switch (Attr) { 1922 case dwarf::DW_AT_sibling: 1923 Asm->EmitInt32(Die->SiblingOffset()); 1924 break; 1925 case dwarf::DW_AT_abstract_origin: { 1926 DIEEntry *E = cast<DIEEntry>(Values[i]); 1927 DIE *Origin = E->getEntry(); 1928 unsigned Addr = 1929 CompileUnitOffsets[Die->getAbstractCompileUnit()] + 1930 Origin->getOffset(); 1931 1932 Asm->EmitInt32(Addr); 1933 break; 1934 } 1935 default: 1936 // Emit an attribute using the defined form. 1937 Values[i]->EmitValue(this, Form); 1938 break; 1939 } 1940 1941 Asm->EOL(dwarf::AttributeString(Attr)); 1942 } 1943 1944 // Emit the DIE children if any. 1945 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) { 1946 const std::vector<DIE *> &Children = Die->getChildren(); 1947 1948 for (unsigned j = 0, M = Children.size(); j < M; ++j) 1949 EmitDIE(Children[j]); 1950 1951 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark"); 1952 } 1953} 1954 1955/// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section. 1956/// 1957void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) { 1958 DIE *Die = Unit->getDie(); 1959 1960 // Emit the compile units header. 1961 EmitLabel("info_begin", Unit->getID()); 1962 1963 // Emit size of content not including length itself 1964 unsigned ContentSize = Die->getSize() + 1965 sizeof(int16_t) + // DWARF version number 1966 sizeof(int32_t) + // Offset Into Abbrev. Section 1967 sizeof(int8_t) + // Pointer Size (in bytes) 1968 sizeof(int32_t); // FIXME - extra pad for gdb bug. 1969 1970 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info"); 1971 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); 1972 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false); 1973 Asm->EOL("Offset Into Abbrev. Section"); 1974 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); 1975 1976 EmitDIE(Die); 1977 // FIXME - extra padding for gdb bug. 1978 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 1979 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 1980 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 1981 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); 1982 EmitLabel("info_end", Unit->getID()); 1983 1984 Asm->EOL(); 1985} 1986 1987void DwarfDebug::EmitDebugInfo() { 1988 // Start debug info section. 1989 Asm->SwitchToDataSection(TAI->getDwarfInfoSection()); 1990 1991 EmitDebugInfoPerCU(ModuleCU); 1992} 1993 1994/// EmitAbbreviations - Emit the abbreviation section. 1995/// 1996void DwarfDebug::EmitAbbreviations() const { 1997 // Check to see if it is worth the effort. 1998 if (!Abbreviations.empty()) { 1999 // Start the debug abbrev section. 2000 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection()); 2001 2002 EmitLabel("abbrev_begin", 0); 2003 2004 // For each abbrevation. 2005 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) { 2006 // Get abbreviation data 2007 const DIEAbbrev *Abbrev = Abbreviations[i]; 2008 2009 // Emit the abbrevations code (base 1 index.) 2010 Asm->EmitULEB128Bytes(Abbrev->getNumber()); 2011 Asm->EOL("Abbreviation Code"); 2012 2013 // Emit the abbreviations data. 2014 Abbrev->Emit(Asm); 2015 2016 Asm->EOL(); 2017 } 2018 2019 // Mark end of abbreviations. 2020 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)"); 2021 2022 EmitLabel("abbrev_end", 0); 2023 Asm->EOL(); 2024 } 2025} 2026 2027/// EmitEndOfLineMatrix - Emit the last address of the section and the end of 2028/// the line matrix. 2029/// 2030void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) { 2031 // Define last address of section. 2032 Asm->EmitInt8(0); Asm->EOL("Extended Op"); 2033 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); 2034 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); 2035 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label"); 2036 2037 // Mark end of matrix. 2038 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence"); 2039 Asm->EmitULEB128Bytes(1); Asm->EOL(); 2040 Asm->EmitInt8(1); Asm->EOL(); 2041} 2042 2043/// EmitDebugLines - Emit source line information. 2044/// 2045void DwarfDebug::EmitDebugLines() { 2046 // If the target is using .loc/.file, the assembler will be emitting the 2047 // .debug_line table automatically. 2048 if (TAI->hasDotLocAndDotFile()) 2049 return; 2050 2051 // Minimum line delta, thus ranging from -10..(255-10). 2052 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1); 2053 // Maximum line delta, thus ranging from -10..(255-10). 2054 const int MaxLineDelta = 255 + MinLineDelta; 2055 2056 // Start the dwarf line section. 2057 Asm->SwitchToDataSection(TAI->getDwarfLineSection()); 2058 2059 // Construct the section header. 2060 EmitDifference("line_end", 0, "line_begin", 0, true); 2061 Asm->EOL("Length of Source Line Info"); 2062 EmitLabel("line_begin", 0); 2063 2064 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); 2065 2066 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true); 2067 Asm->EOL("Prolog Length"); 2068 EmitLabel("line_prolog_begin", 0); 2069 2070 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length"); 2071 2072 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag"); 2073 2074 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)"); 2075 2076 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)"); 2077 2078 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base"); 2079 2080 // Line number standard opcode encodings argument count 2081 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count"); 2082 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count"); 2083 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count"); 2084 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count"); 2085 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count"); 2086 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count"); 2087 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count"); 2088 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count"); 2089 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count"); 2090 2091 // Emit directories. 2092 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) { 2093 Asm->EmitString(getSourceDirectoryName(DI)); 2094 Asm->EOL("Directory"); 2095 } 2096 2097 Asm->EmitInt8(0); Asm->EOL("End of directories"); 2098 2099 // Emit files. 2100 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) { 2101 // Remember source id starts at 1. 2102 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI); 2103 Asm->EmitString(getSourceFileName(Id.second)); 2104 Asm->EOL("Source"); 2105 Asm->EmitULEB128Bytes(Id.first); 2106 Asm->EOL("Directory #"); 2107 Asm->EmitULEB128Bytes(0); 2108 Asm->EOL("Mod date"); 2109 Asm->EmitULEB128Bytes(0); 2110 Asm->EOL("File size"); 2111 } 2112 2113 Asm->EmitInt8(0); Asm->EOL("End of files"); 2114 2115 EmitLabel("line_prolog_end", 0); 2116 2117 // A sequence for each text section. 2118 unsigned SecSrcLinesSize = SectionSourceLines.size(); 2119 2120 for (unsigned j = 0; j < SecSrcLinesSize; ++j) { 2121 // Isolate current sections line info. 2122 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j]; 2123 2124 if (Asm->isVerbose()) { 2125 const Section* S = SectionMap[j + 1]; 2126 O << '\t' << TAI->getCommentString() << " Section" 2127 << S->getName() << '\n'; 2128 } else { 2129 Asm->EOL(); 2130 } 2131 2132 // Dwarf assumes we start with first line of first source file. 2133 unsigned Source = 1; 2134 unsigned Line = 1; 2135 2136 // Construct rows of the address, source, line, column matrix. 2137 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) { 2138 const SrcLineInfo &LineInfo = LineInfos[i]; 2139 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID()); 2140 if (!LabelID) continue; 2141 2142 if (!Asm->isVerbose()) 2143 Asm->EOL(); 2144 else { 2145 std::pair<unsigned, unsigned> SourceID = 2146 getSourceDirectoryAndFileIds(LineInfo.getSourceID()); 2147 O << '\t' << TAI->getCommentString() << ' ' 2148 << getSourceDirectoryName(SourceID.first) << ' ' 2149 << getSourceFileName(SourceID.second) 2150 <<" :" << utostr_32(LineInfo.getLine()) << '\n'; 2151 } 2152 2153 // Define the line address. 2154 Asm->EmitInt8(0); Asm->EOL("Extended Op"); 2155 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); 2156 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); 2157 EmitReference("label", LabelID); Asm->EOL("Location label"); 2158 2159 // If change of source, then switch to the new source. 2160 if (Source != LineInfo.getSourceID()) { 2161 Source = LineInfo.getSourceID(); 2162 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file"); 2163 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source"); 2164 } 2165 2166 // If change of line. 2167 if (Line != LineInfo.getLine()) { 2168 // Determine offset. 2169 int Offset = LineInfo.getLine() - Line; 2170 int Delta = Offset - MinLineDelta; 2171 2172 // Update line. 2173 Line = LineInfo.getLine(); 2174 2175 // If delta is small enough and in range... 2176 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { 2177 // ... then use fast opcode. 2178 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta"); 2179 } else { 2180 // ... otherwise use long hand. 2181 Asm->EmitInt8(dwarf::DW_LNS_advance_line); 2182 Asm->EOL("DW_LNS_advance_line"); 2183 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset"); 2184 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); 2185 } 2186 } else { 2187 // Copy the previous row (different address or source) 2188 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); 2189 } 2190 } 2191 2192 EmitEndOfLineMatrix(j + 1); 2193 } 2194 2195 if (SecSrcLinesSize == 0) 2196 // Because we're emitting a debug_line section, we still need a line 2197 // table. The linker and friends expect it to exist. If there's nothing to 2198 // put into it, emit an empty table. 2199 EmitEndOfLineMatrix(1); 2200 2201 EmitLabel("line_end", 0); 2202 Asm->EOL(); 2203} 2204 2205/// EmitCommonDebugFrame - Emit common frame info into a debug frame section. 2206/// 2207void DwarfDebug::EmitCommonDebugFrame() { 2208 if (!TAI->doesDwarfRequireFrameSection()) 2209 return; 2210 2211 int stackGrowth = 2212 Asm->TM.getFrameInfo()->getStackGrowthDirection() == 2213 TargetFrameInfo::StackGrowsUp ? 2214 TD->getPointerSize() : -TD->getPointerSize(); 2215 2216 // Start the dwarf frame section. 2217 Asm->SwitchToDataSection(TAI->getDwarfFrameSection()); 2218 2219 EmitLabel("debug_frame_common", 0); 2220 EmitDifference("debug_frame_common_end", 0, 2221 "debug_frame_common_begin", 0, true); 2222 Asm->EOL("Length of Common Information Entry"); 2223 2224 EmitLabel("debug_frame_common_begin", 0); 2225 Asm->EmitInt32((int)dwarf::DW_CIE_ID); 2226 Asm->EOL("CIE Identifier Tag"); 2227 Asm->EmitInt8(dwarf::DW_CIE_VERSION); 2228 Asm->EOL("CIE Version"); 2229 Asm->EmitString(""); 2230 Asm->EOL("CIE Augmentation"); 2231 Asm->EmitULEB128Bytes(1); 2232 Asm->EOL("CIE Code Alignment Factor"); 2233 Asm->EmitSLEB128Bytes(stackGrowth); 2234 Asm->EOL("CIE Data Alignment Factor"); 2235 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false)); 2236 Asm->EOL("CIE RA Column"); 2237 2238 std::vector<MachineMove> Moves; 2239 RI->getInitialFrameState(Moves); 2240 2241 EmitFrameMoves(NULL, 0, Moves, false); 2242 2243 Asm->EmitAlignment(2, 0, 0, false); 2244 EmitLabel("debug_frame_common_end", 0); 2245 2246 Asm->EOL(); 2247} 2248 2249/// EmitFunctionDebugFrame - Emit per function frame info into a debug frame 2250/// section. 2251void 2252DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){ 2253 if (!TAI->doesDwarfRequireFrameSection()) 2254 return; 2255 2256 // Start the dwarf frame section. 2257 Asm->SwitchToDataSection(TAI->getDwarfFrameSection()); 2258 2259 EmitDifference("debug_frame_end", DebugFrameInfo.Number, 2260 "debug_frame_begin", DebugFrameInfo.Number, true); 2261 Asm->EOL("Length of Frame Information Entry"); 2262 2263 EmitLabel("debug_frame_begin", DebugFrameInfo.Number); 2264 2265 EmitSectionOffset("debug_frame_common", "section_debug_frame", 2266 0, 0, true, false); 2267 Asm->EOL("FDE CIE offset"); 2268 2269 EmitReference("func_begin", DebugFrameInfo.Number); 2270 Asm->EOL("FDE initial location"); 2271 EmitDifference("func_end", DebugFrameInfo.Number, 2272 "func_begin", DebugFrameInfo.Number); 2273 Asm->EOL("FDE address range"); 2274 2275 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, 2276 false); 2277 2278 Asm->EmitAlignment(2, 0, 0, false); 2279 EmitLabel("debug_frame_end", DebugFrameInfo.Number); 2280 2281 Asm->EOL(); 2282} 2283 2284void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) { 2285 EmitDifference("pubnames_end", Unit->getID(), 2286 "pubnames_begin", Unit->getID(), true); 2287 Asm->EOL("Length of Public Names Info"); 2288 2289 EmitLabel("pubnames_begin", Unit->getID()); 2290 2291 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version"); 2292 2293 EmitSectionOffset("info_begin", "section_info", 2294 Unit->getID(), 0, true, false); 2295 Asm->EOL("Offset of Compilation Unit Info"); 2296 2297 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(), 2298 true); 2299 Asm->EOL("Compilation Unit Length"); 2300 2301 StringMap<DIE*> &Globals = Unit->getGlobals(); 2302 for (StringMap<DIE*>::const_iterator 2303 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { 2304 const char *Name = GI->getKeyData(); 2305 DIE * Entity = GI->second; 2306 2307 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset"); 2308 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name"); 2309 } 2310 2311 Asm->EmitInt32(0); Asm->EOL("End Mark"); 2312 EmitLabel("pubnames_end", Unit->getID()); 2313 2314 Asm->EOL(); 2315} 2316 2317/// EmitDebugPubNames - Emit visible names into a debug pubnames section. 2318/// 2319void DwarfDebug::EmitDebugPubNames() { 2320 // Start the dwarf pubnames section. 2321 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection()); 2322 2323 EmitDebugPubNamesPerCU(ModuleCU); 2324} 2325 2326/// EmitDebugStr - Emit visible names into a debug str section. 2327/// 2328void DwarfDebug::EmitDebugStr() { 2329 // Check to see if it is worth the effort. 2330 if (!StringPool.empty()) { 2331 // Start the dwarf str section. 2332 Asm->SwitchToDataSection(TAI->getDwarfStrSection()); 2333 2334 // For each of strings in the string pool. 2335 for (unsigned StringID = 1, N = StringPool.size(); 2336 StringID <= N; ++StringID) { 2337 // Emit a label for reference from debug information entries. 2338 EmitLabel("string", StringID); 2339 2340 // Emit the string itself. 2341 const std::string &String = StringPool[StringID]; 2342 Asm->EmitString(String); Asm->EOL(); 2343 } 2344 2345 Asm->EOL(); 2346 } 2347} 2348 2349/// EmitDebugLoc - Emit visible names into a debug loc section. 2350/// 2351void DwarfDebug::EmitDebugLoc() { 2352 // Start the dwarf loc section. 2353 Asm->SwitchToDataSection(TAI->getDwarfLocSection()); 2354 Asm->EOL(); 2355} 2356 2357/// EmitDebugARanges - Emit visible names into a debug aranges section. 2358/// 2359void DwarfDebug::EmitDebugARanges() { 2360 // Start the dwarf aranges section. 2361 Asm->SwitchToDataSection(TAI->getDwarfARangesSection()); 2362 2363 // FIXME - Mock up 2364#if 0 2365 CompileUnit *Unit = GetBaseCompileUnit(); 2366 2367 // Don't include size of length 2368 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info"); 2369 2370 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); 2371 2372 EmitReference("info_begin", Unit->getID()); 2373 Asm->EOL("Offset of Compilation Unit Info"); 2374 2375 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address"); 2376 2377 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor"); 2378 2379 Asm->EmitInt16(0); Asm->EOL("Pad (1)"); 2380 Asm->EmitInt16(0); Asm->EOL("Pad (2)"); 2381 2382 // Range 1 2383 EmitReference("text_begin", 0); Asm->EOL("Address"); 2384 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length"); 2385 2386 Asm->EmitInt32(0); Asm->EOL("EOM (1)"); 2387 Asm->EmitInt32(0); Asm->EOL("EOM (2)"); 2388#endif 2389 2390 Asm->EOL(); 2391} 2392 2393/// EmitDebugRanges - Emit visible names into a debug ranges section. 2394/// 2395void DwarfDebug::EmitDebugRanges() { 2396 // Start the dwarf ranges section. 2397 Asm->SwitchToDataSection(TAI->getDwarfRangesSection()); 2398 Asm->EOL(); 2399} 2400 2401/// EmitDebugMacInfo - Emit visible names into a debug macinfo section. 2402/// 2403void DwarfDebug::EmitDebugMacInfo() { 2404 if (const char *LineInfoDirective = TAI->getDwarfMacroInfoSection()) { 2405 // Start the dwarf macinfo section. 2406 Asm->SwitchToDataSection(LineInfoDirective); 2407 Asm->EOL(); 2408 } 2409} 2410 2411/// EmitDebugInlineInfo - Emit inline info using following format. 2412/// Section Header: 2413/// 1. length of section 2414/// 2. Dwarf version number 2415/// 3. address size. 2416/// 2417/// Entries (one "entry" for each function that was inlined): 2418/// 2419/// 1. offset into __debug_str section for MIPS linkage name, if exists; 2420/// otherwise offset into __debug_str for regular function name. 2421/// 2. offset into __debug_str section for regular function name. 2422/// 3. an unsigned LEB128 number indicating the number of distinct inlining 2423/// instances for the function. 2424/// 2425/// The rest of the entry consists of a {die_offset, low_pc} pair for each 2426/// inlined instance; the die_offset points to the inlined_subroutine die in the 2427/// __debug_info section, and the low_pc is the starting address for the 2428/// inlining instance. 2429void DwarfDebug::EmitDebugInlineInfo() { 2430 if (!TAI->doesDwarfUsesInlineInfoSection()) 2431 return; 2432 2433 if (!ModuleCU) 2434 return; 2435 2436 Asm->SwitchToDataSection(TAI->getDwarfDebugInlineSection()); 2437 Asm->EOL(); 2438 EmitDifference("debug_inlined_end", 1, 2439 "debug_inlined_begin", 1, true); 2440 Asm->EOL("Length of Debug Inlined Information Entry"); 2441 2442 EmitLabel("debug_inlined_begin", 1); 2443 2444 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); 2445 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); 2446 2447 for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator 2448 I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) { 2449 GlobalVariable *GV = I->first; 2450 SmallVector<unsigned, 4> &Labels = I->second; 2451 DISubprogram SP(GV); 2452 std::string Name; 2453 std::string LName; 2454 2455 SP.getLinkageName(LName); 2456 SP.getName(Name); 2457 2458 Asm->EmitString(LName.empty() ? Name : LName); 2459 Asm->EOL("MIPS linkage name"); 2460 2461 Asm->EmitString(Name); Asm->EOL("Function name"); 2462 2463 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count"); 2464 2465 for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(), 2466 LE = Labels.end(); LI != LE; ++LI) { 2467 DIE *SP = ModuleCU->getDieMapSlotFor(GV); 2468 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset"); 2469 2470 if (TD->getPointerSize() == sizeof(int32_t)) 2471 O << TAI->getData32bitsDirective(); 2472 else 2473 O << TAI->getData64bitsDirective(); 2474 2475 PrintLabelName("label", *LI); Asm->EOL("low_pc"); 2476 } 2477 } 2478 2479 EmitLabel("debug_inlined_end", 1); 2480 Asm->EOL(); 2481} 2482