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