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