LLVM  10.0.0svn
PDBContext.cpp
Go to the documentation of this file.
1 //===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===/
8 
17 #include "llvm/Object/COFF.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 using namespace llvm::pdb;
22 
23 PDBContext::PDBContext(const COFFObjectFile &Object,
24  std::unique_ptr<IPDBSession> PDBSession)
25  : DIContext(CK_PDB), Session(std::move(PDBSession)) {
26  ErrorOr<uint64_t> ImageBase = Object.getImageBase();
27  if (ImageBase)
28  Session->setLoadAddress(ImageBase.get());
29 }
30 
32 
34  DILineInfoSpecifier Specifier) {
35  DILineInfo Result;
36  Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);
37 
38  uint32_t Length = 1;
39  std::unique_ptr<PDBSymbol> Symbol =
40  Session->findSymbolByAddress(Address.Address, PDB_SymType::None);
41  if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
42  Length = Func->getLength();
43  } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
44  Length = Data->getLength();
45  }
46 
47  // If we couldn't find a symbol, then just assume 1 byte, so that we get
48  // only the line number of the first instruction.
49  auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Length);
50  if (!LineNumbers || LineNumbers->getChildCount() == 0)
51  return Result;
52 
53  auto LineInfo = LineNumbers->getNext();
54  assert(LineInfo);
55  auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
56 
57  if (SourceFile &&
59  Result.FileName = SourceFile->getFileName();
60  Result.Column = LineInfo->getColumnNumber();
61  Result.Line = LineInfo->getLineNumber();
62  return Result;
63 }
64 
67  uint64_t Size,
68  DILineInfoSpecifier Specifier) {
69  if (Size == 0)
70  return DILineInfoTable();
71 
72  DILineInfoTable Table;
73  auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Size);
74  if (!LineNumbers || LineNumbers->getChildCount() == 0)
75  return Table;
76 
77  while (auto LineInfo = LineNumbers->getNext()) {
79  {LineInfo->getVirtualAddress(), Address.SectionIndex}, 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::vector<DILocal>
96  return std::vector<DILocal>();
97 }
98 
99 std::string PDBContext::getFunctionName(uint64_t Address,
100  DINameKind NameKind) const {
101  if (NameKind == DINameKind::None)
102  return std::string();
103 
104  std::unique_ptr<PDBSymbol> FuncSymbol =
105  Session->findSymbolByAddress(Address, PDB_SymType::Function);
106  auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
107 
108  if (NameKind == DINameKind::LinkageName) {
109  // It is not possible to get the mangled linkage name through a
110  // PDBSymbolFunc. For that we have to specifically request a
111  // PDBSymbolPublicSymbol.
112  auto PublicSym =
113  Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
114  if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
115  // If we also have a function symbol, prefer the use of public symbol name
116  // only if it refers to the same address. The public symbol uses the
117  // linkage name while the function does not.
118  if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
119  return PS->getName();
120  }
121  }
122 
123  return Func ? Func->getName() : std::string();
124 }
void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override
Definition: PDBContext.cpp:31
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::string FileName
Definition: DIContext.h:35
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:33
void push_back(const T &Elt)
Definition: SmallVector.h:211
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:101
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:131
A format-neutral container for source line information.
Definition: DIContext.h:30
Definition: BitVector.h:937
FunctionNameKind FNKind
Definition: DIContext.h:140
FileLineInfoKind FLIKind
Definition: DIContext.h:139
DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:66
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:171
uint32_t Column
Definition: DIContext.h:39
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:135
A format-neutral container for inlined code description.
Definition: DIContext.h:81
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:86
uint32_t Line
Definition: DIContext.h:38
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
std::string FunctionName
Definition: DIContext.h:36
std::vector< DILocal > getLocalsForAddress(object::SectionedAddress Address) override
Definition: PDBContext.cpp:95
uint32_t Size
Definition: Profile.cpp:46
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:78
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
reference get()
Definition: ErrorOr.h:156