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