LLVM  4.0.0
PDBContext.cpp
Go to the documentation of this file.
1 //===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
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 
18 #include "llvm/Object/COFF.h"
19 
20 using namespace llvm;
21 using namespace llvm::object;
22 using namespace llvm::pdb;
23 
24 PDBContext::PDBContext(const COFFObjectFile &Object,
25  std::unique_ptr<IPDBSession> PDBSession)
26  : DIContext(CK_PDB), Session(std::move(PDBSession)) {
27  ErrorOr<uint64_t> ImageBase = Object.getImageBase();
28  if (ImageBase)
29  Session->setLoadAddress(ImageBase.get());
30 }
31 
32 void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
33  bool SummarizeTypes) {}
34 
36  DILineInfoSpecifier Specifier) {
37  DILineInfo Result;
38  Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
39 
40  uint32_t Length = 1;
41  std::unique_ptr<PDBSymbol> Symbol =
42  Session->findSymbolByAddress(Address, PDB_SymType::None);
43  if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
44  Length = Func->getLength();
45  } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
46  Length = Data->getLength();
47  }
48 
49  // If we couldn't find a symbol, then just assume 1 byte, so that we get
50  // only the line number of the first instruction.
51  auto LineNumbers = Session->findLineNumbersByAddress(Address, Length);
52  if (!LineNumbers || LineNumbers->getChildCount() == 0)
53  return Result;
54 
55  auto LineInfo = LineNumbers->getNext();
56  assert(LineInfo);
57  auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
58 
59  if (SourceFile &&
61  Result.FileName = SourceFile->getFileName();
62  Result.Column = LineInfo->getColumnNumber();
63  Result.Line = LineInfo->getLineNumber();
64  return Result;
65 }
66 
69  DILineInfoSpecifier Specifier) {
70  if (Size == 0)
71  return DILineInfoTable();
72 
73  DILineInfoTable Table;
74  auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
75  if (!LineNumbers || LineNumbers->getChildCount() == 0)
76  return Table;
77 
78  while (auto LineInfo = LineNumbers->getNext()) {
79  DILineInfo LineEntry =
80  getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
81  Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
82  }
83  return Table;
84 }
85 
88  DILineInfoSpecifier Specifier) {
89  DIInliningInfo InlineInfo;
90  DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
91  InlineInfo.addFrame(Frame);
92  return InlineInfo;
93 }
94 
95 std::string PDBContext::getFunctionName(uint64_t Address,
96  DINameKind NameKind) const {
97  if (NameKind == DINameKind::None)
98  return std::string();
99 
100  std::unique_ptr<PDBSymbol> FuncSymbol =
101  Session->findSymbolByAddress(Address, PDB_SymType::Function);
102  auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
103 
104  if (NameKind == DINameKind::LinkageName) {
105  // It is not possible to get the mangled linkage name through a
106  // PDBSymbolFunc. For that we have to specifically request a
107  // PDBSymbolPublicSymbol.
108  auto PublicSym =
109  Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
110  if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
111  // If we also have a function symbol, prefer the use of public symbol name
112  // only if it refers to the same address. The public symbol uses the
113  // linkage name while the function does not.
114  if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
115  return PS->getName();
116  }
117  }
118 
119  return Func ? Func->getName() : std::string();
120 }
void push_back(const T &Elt)
Definition: SmallVector.h:211
Represents either an error or a value T.
Definition: ErrorOr.h:68
std::string FileName
Definition: DIContext.h:33
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:81
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:97
DILineInfo - a format-neutral container for source line information.
Definition: DIContext.h:32
FunctionNameKind FNKind
Definition: DIContext.h:106
FileLineInfoKind FLIKind
Definition: DIContext.h:105
DILineInfo getLineInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:35
uint32_t Column
Definition: DIContext.h:36
DILineInfoSpecifier - controls which fields of DILineInfo container should be filled with data...
Definition: DIContext.h:101
void dump(raw_ostream &OS, DIDumpType DumpType=DIDT_All, bool DumpEH=false, bool SummarizeTypes=false) override
Definition: PDBContext.cpp:32
DIInliningInfo - a format-neutral container for inlined code description.
Definition: DIContext.h:61
DIDumpType
Selects which debug sections get dumped.
Definition: DIContext.h:114
uint32_t Line
Definition: DIContext.h:35
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:58
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
std::string FunctionName
Definition: DIContext.h:34
DIInliningInfo getInliningInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:87
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:68
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
reference get()
Definition: ErrorOr.h:166