LLVM  14.0.0git
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) {
89  DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier);
90 
91  // Find the function at this address.
92  std::unique_ptr<PDBSymbol> ParentFunc =
93  Session->findSymbolByAddress(Address.Address, PDB_SymType::Function);
94  if (!ParentFunc) {
95  InlineInfo.addFrame(CurrentLine);
96  return InlineInfo;
97  }
98 
99  auto Frames = ParentFunc->findInlineFramesByVA(Address.Address);
100  if (!Frames || Frames->getChildCount() == 0) {
101  InlineInfo.addFrame(CurrentLine);
102  return InlineInfo;
103  }
104 
105  while (auto Frame = Frames->getNext()) {
106  uint32_t Length = 1;
107  auto LineNumbers = Frame->findInlineeLinesByVA(Address.Address, Length);
108  if (!LineNumbers || LineNumbers->getChildCount() == 0)
109  break;
110 
111  std::unique_ptr<IPDBLineNumber> Line = LineNumbers->getNext();
112  assert(Line);
113 
114  DILineInfo LineInfo;
115  LineInfo.FunctionName = Frame->getName();
116  auto SourceFile = Session->getSourceFileById(Line->getSourceFileId());
117  if (SourceFile &&
119  LineInfo.FileName = SourceFile->getFileName();
120  LineInfo.Line = Line->getLineNumber();
121  LineInfo.Column = Line->getColumnNumber();
122  InlineInfo.addFrame(LineInfo);
123  }
124 
125  InlineInfo.addFrame(CurrentLine);
126  return InlineInfo;
127 }
128 
129 std::vector<DILocal>
131  return std::vector<DILocal>();
132 }
133 
134 std::string PDBContext::getFunctionName(uint64_t Address,
135  DINameKind NameKind) const {
136  if (NameKind == DINameKind::None)
137  return std::string();
138 
139  std::unique_ptr<PDBSymbol> FuncSymbol =
140  Session->findSymbolByAddress(Address, PDB_SymType::Function);
141  auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
142 
143  if (NameKind == DINameKind::LinkageName) {
144  // It is not possible to get the mangled linkage name through a
145  // PDBSymbolFunc. For that we have to specifically request a
146  // PDBSymbolPublicSymbol.
147  auto PublicSym =
148  Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
149  if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
150  // If we also have a function symbol, prefer the use of public symbol name
151  // only if it refers to the same address. The public symbol uses the
152  // linkage name while the function does not.
153  if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
154  return PS->getName();
155  }
156  }
157 
158  return Func ? Func->getName() : std::string();
159 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::DILineInfoTable
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:84
llvm::DIInliningInfo
A format-neutral container for inlined code description.
Definition: DIContext.h:87
llvm::DILineInfoSpecifier
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:135
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::DINameKind::LinkageName
@ LinkageName
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DILineInfo::FunctionName
std::string FunctionName
Definition: DIContext.h:37
IPDBLineNumber.h
llvm::DILineInfo::Column
uint32_t Column
Definition: DIContext.h:41
llvm::object
Definition: ObjectFileTransformer.h:18
llvm::DINameKind
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:131
llvm::DIContext
Definition: DIContext.h:217
InlineInfo
@ InlineInfo
Definition: FunctionInfo.cpp:24
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
PDBSymbol.h
llvm::pdb::PDB_TableType::LineNumbers
@ LineNumbers
PDBSymbolPublicSymbol.h
llvm::pdb
Definition: ConcreteSymbolEnumerator.h:20
llvm::OutputFileType::Object
@ Object
PDBSymbolFunc.h
llvm::DILineInfo::Line
uint32_t Line
Definition: DIContext.h:40
uint64_t
llvm::DILineInfo
A format-neutral container for source line information.
Definition: DIContext.h:31
llvm::HighlightColor::Address
@ Address
IPDBEnumChildren.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1609
llvm::pdb::PDB_SymType::Function
@ Function
llvm::pdb::PDB_SymType::None
@ None
PDBContext.h
uint32_t
IPDBSourceFile.h
llvm::pdb::PDBContext::getLineInfoForAddress
DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:33
llvm::DILineInfoSpecifier::FNKind
FunctionNameKind FNKind
Definition: DIContext.h:149
std
Definition: BitVector.h:838
llvm::DILineInfoSpecifier::FileLineInfoKind::None
@ None
llvm::object::COFFObjectFile
Definition: COFF.h:799
llvm::pdb::PDBContext::dump
void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override
Definition: PDBContext.cpp:31
llvm::pdb::PDBContext::getInliningInfoForAddress
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:86
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::DILineInfo::FileName
std::string FileName
Definition: DIContext.h:36
COFF.h
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
llvm::DINameKind::None
@ None
llvm::pdb::PDBContext::getLocalsForAddress
std::vector< DILocal > getLocalsForAddress(object::SectionedAddress Address) override
Definition: PDBContext.cpp:130
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::object::SectionedAddress
Definition: ObjectFile.h:144
llvm::pdb::PDB_SymType::PublicSymbol
@ PublicSymbol
llvm::pdb::PDBContext::getLineInfoForAddressRange
DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:66
llvm::DILineInfoSpecifier::FLIKind
FileLineInfoKind FLIKind
Definition: DIContext.h:148
PDBSymbolData.h
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:180