LLVM  8.0.1
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 
33 
35  DILineInfoSpecifier Specifier) {
36  DILineInfo Result;
37  Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
38 
39  uint32_t Length = 1;
40  std::unique_ptr<PDBSymbol> Symbol =
41  Session->findSymbolByAddress(Address, PDB_SymType::None);
42  if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
43  Length = Func->getLength();
44  } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
45  Length = Data->getLength();
46  }
47 
48  // If we couldn't find a symbol, then just assume 1 byte, so that we get
49  // only the line number of the first instruction.
50  auto LineNumbers = Session->findLineNumbersByAddress(Address, Length);
51  if (!LineNumbers || LineNumbers->getChildCount() == 0)
52  return Result;
53 
54  auto LineInfo = LineNumbers->getNext();
55  assert(LineInfo);
56  auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
57 
58  if (SourceFile &&
60  Result.FileName = SourceFile->getFileName();
61  Result.Column = LineInfo->getColumnNumber();
62  Result.Line = LineInfo->getLineNumber();
63  return Result;
64 }
65 
67 PDBContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
68  DILineInfoSpecifier Specifier) {
69  if (Size == 0)
70  return DILineInfoTable();
71 
72  DILineInfoTable Table;
73  auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
74  if (!LineNumbers || LineNumbers->getChildCount() == 0)
75  return Table;
76 
77  while (auto LineInfo = LineNumbers->getNext()) {
78  DILineInfo LineEntry =
79  getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
80  Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
81  }
82  return Table;
83 }
84 
87  DILineInfoSpecifier Specifier) {
88  DIInliningInfo InlineInfo;
89  DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
90  InlineInfo.addFrame(Frame);
91  return InlineInfo;
92 }
93 
94 std::string PDBContext::getFunctionName(uint64_t Address,
95  DINameKind NameKind) const {
96  if (NameKind == DINameKind::None)
97  return std::string();
98 
99  std::unique_ptr<PDBSymbol> FuncSymbol =
100  Session->findSymbolByAddress(Address, PDB_SymType::Function);
101  auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
102 
103  if (NameKind == DINameKind::LinkageName) {
104  // It is not possible to get the mangled linkage name through a
105  // PDBSymbolFunc. For that we have to specifically request a
106  // PDBSymbolPublicSymbol.
107  auto PublicSym =
108  Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
109  if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
110  // If we also have a function symbol, prefer the use of public symbol name
111  // only if it refers to the same address. The public symbol uses the
112  // linkage name while the function does not.
113  if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
114  return PS->getName();
115  }
116  }
117 
118  return Func ? Func->getName() : std::string();
119 }
void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override
Definition: PDBContext.cpp:32
Represents either an error or a value T.
Definition: ErrorOr.h:57
std::string FileName
Definition: DIContext.h:32
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void push_back(const T &Elt)
Definition: SmallVector.h:218
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:98
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:119
A format-neutral container for source line information.
Definition: DIContext.h:31
Definition: BitVector.h:938
FunctionNameKind FNKind
Definition: DIContext.h:128
FileLineInfoKind FLIKind
Definition: DIContext.h:127
DILineInfo getLineInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:34
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:159
uint32_t Column
Definition: DIContext.h:36
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:123
A format-neutral container for inlined code description.
Definition: DIContext.h:78
uint32_t Line
Definition: DIContext.h:35
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
std::string FunctionName
Definition: DIContext.h:33
DIInliningInfo getInliningInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:86
uint32_t Size
Definition: Profile.cpp:47
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:75
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:67
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
reference get()
Definition: ErrorOr.h:157