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