LLVM  8.0.1
DwarfFile.cpp
Go to the documentation of this file.
1 //===- llvm/CodeGen/DwarfFile.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 #include "DwarfFile.h"
11 #include "DwarfCompileUnit.h"
12 #include "DwarfDebug.h"
13 #include "DwarfUnit.h"
14 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include <algorithm>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
25  : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
26 
27 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
28  CUs.push_back(std::move(U));
29 }
30 
31 // Emit the various dwarf units to the unit section USection with
32 // the abbreviations going into ASection.
33 void DwarfFile::emitUnits(bool UseOffsets) {
34  for (const auto &TheU : CUs)
35  emitUnit(TheU.get(), UseOffsets);
36 }
37 
38 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
39  if (TheU->getCUNode()->isDebugDirectivesOnly())
40  return;
41 
42  MCSection *S = TheU->getSection();
43 
44  if (!S)
45  return;
46 
47  Asm->OutStreamer->SwitchSection(S);
48  TheU->emitHeader(UseOffsets);
49  Asm->emitDwarfDIE(TheU->getUnitDie());
50 
51  if (MCSymbol *EndLabel = TheU->getEndLabel())
52  Asm->OutStreamer->EmitLabel(EndLabel);
53 }
54 
55 // Compute the size and offset for each DIE.
57  // Offset from the first CU in the debug info section is 0 initially.
58  unsigned SecOffset = 0;
59 
60  // Iterate over each compile unit and set the size and offsets for each
61  // DIE within each compile unit. All offsets are CU relative.
62  for (const auto &TheU : CUs) {
63  if (TheU->getCUNode()->isDebugDirectivesOnly())
64  continue;
65 
66  TheU->setDebugSectionOffset(SecOffset);
67  SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
68  }
69 }
70 
72  // CU-relative offset is reset to 0 here.
73  unsigned Offset = sizeof(int32_t) + // Length of Unit Info
74  TheU->getHeaderSize(); // Unit-specific headers
75 
76  // The return value here is CU-relative, after laying out
77  // all of the CU DIE.
78  return computeSizeAndOffset(TheU->getUnitDie(), Offset);
79 }
80 
81 // Compute the size and offset of a DIE. The offset is relative to start of the
82 // CU. It returns the offset after laying out the DIE.
83 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
84  return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
85 }
86 
87 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
88 
89 // Emit strings into a string section.
90 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
91  bool UseRelativeOffsets) {
92  StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
93 }
94 
96  auto &ScopeVars = ScopeVariables[LS];
97  const DILocalVariable *DV = Var->getVariable();
98  if (unsigned ArgNum = DV->getArg()) {
99  auto Cached = ScopeVars.Args.find(ArgNum);
100  if (Cached == ScopeVars.Args.end())
101  ScopeVars.Args[ArgNum] = Var;
102  else {
103  Cached->second->addMMIEntry(*Var);
104  return false;
105  }
106  } else {
107  ScopeVars.Locals.push_back(Var);
108  }
109  return true;
110 }
111 
113  SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
114  Labels.push_back(Label);
115 }
116 
117 std::pair<uint32_t, RangeSpanList *>
119  CURangeLists.push_back(
120  RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
121  return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
122 }
const DICompileUnit * getCUNode() const
Definition: DwarfUnit.h:90
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:27
void addMMIEntry(const DbgVariable &V)
Definition: DwarfDebug.cpp:254
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:262
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset)
Compute the size and offset of a DIE given an incoming Offset.
Definition: DwarfFile.cpp:83
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
std::pair< uint32_t, RangeSpanList * > addRange(const DwarfCompileUnit &CU, SmallVector< RangeSpan, 2 > R)
Definition: DwarfFile.cpp:118
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
bool isDebugDirectivesOnly() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
virtual void emitHeader(bool UseOffsets)=0
Emit the header for this unit, not including the initial length field.
This class is used to track local variable information.
Definition: DwarfDebug.h:117
const DILocalVariable * getVariable() const
Definition: DwarfDebug.h:163
This class is used to track label information.
Definition: DwarfDebug.h:233
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:71
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:41
void emitUnit(DwarfUnit *U, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:38
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
A structured debug information entry.
Definition: DIE.h:662
DIE & getUnitDie()
Definition: DIE.h:834
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:56
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:95
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:24
unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:278
void addScopeLabel(LexicalScope *LS, DbgLabel *Label)
Definition: DwarfFile.cpp:112
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:827
MCSymbol * getEndLabel() const
Definition: DwarfUnit.h:88
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:90
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:87
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:171
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:33
MCSymbol * createTempSymbol(const Twine &Name) const