LLVM  16.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 
19 #include "llvm/Object/COFF.h"
20 
21 using namespace llvm;
22 using namespace llvm::object;
23 using namespace llvm::pdb;
24 
25 PDBContext::PDBContext(const COFFObjectFile &Object,
26  std::unique_ptr<IPDBSession> PDBSession)
27  : DIContext(CK_PDB), Session(std::move(PDBSession)) {
28  ErrorOr<uint64_t> ImageBase = Object.getImageBase();
29  if (ImageBase)
30  Session->setLoadAddress(ImageBase.get());
31 }
32 
34 
36  DILineInfoSpecifier Specifier) {
37  DILineInfo Result;
38  Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);
39 
40  uint32_t Length = 1;
41  std::unique_ptr<PDBSymbol> Symbol =
42  Session->findSymbolByAddress(Address.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.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  // Unimplemented. S_GDATA and S_LDATA in CodeView (used to describe global
70  // variables) aren't capable of carrying line information.
71  return DILineInfo();
72 }
73 
76  uint64_t Size,
77  DILineInfoSpecifier Specifier) {
78  if (Size == 0)
79  return DILineInfoTable();
80 
81  DILineInfoTable Table;
82  auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Size);
83  if (!LineNumbers || LineNumbers->getChildCount() == 0)
84  return Table;
85 
86  while (auto LineInfo = LineNumbers->getNext()) {
88  {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier);
89  Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
90  }
91  return Table;
92 }
93 
96  DILineInfoSpecifier Specifier) {
98  DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier);
99 
100  // Find the function at this address.
101  std::unique_ptr<PDBSymbol> ParentFunc =
102  Session->findSymbolByAddress(Address.Address, PDB_SymType::Function);
103  if (!ParentFunc) {
104  InlineInfo.addFrame(CurrentLine);
105  return InlineInfo;
106  }
107 
108  auto Frames = ParentFunc->findInlineFramesByVA(Address.Address);
109  if (!Frames || Frames->getChildCount() == 0) {
110  InlineInfo.addFrame(CurrentLine);
111  return InlineInfo;
112  }
113 
114  while (auto Frame = Frames->getNext()) {
115  uint32_t Length = 1;
116  auto LineNumbers = Frame->findInlineeLinesByVA(Address.Address, Length);
117  if (!LineNumbers || LineNumbers->getChildCount() == 0)
118  break;
119 
120  std::unique_ptr<IPDBLineNumber> Line = LineNumbers->getNext();
121  assert(Line);
122 
123  DILineInfo LineInfo;
124  LineInfo.FunctionName = Frame->getName();
125  auto SourceFile = Session->getSourceFileById(Line->getSourceFileId());
126  if (SourceFile &&
128  LineInfo.FileName = SourceFile->getFileName();
129  LineInfo.Line = Line->getLineNumber();
130  LineInfo.Column = Line->getColumnNumber();
131  InlineInfo.addFrame(LineInfo);
132  }
133 
134  InlineInfo.addFrame(CurrentLine);
135  return InlineInfo;
136 }
137 
138 std::vector<DILocal>
140  return std::vector<DILocal>();
141 }
142 
143 std::string PDBContext::getFunctionName(uint64_t Address,
144  DINameKind NameKind) const {
145  if (NameKind == DINameKind::None)
146  return std::string();
147 
148  std::unique_ptr<PDBSymbol> FuncSymbol =
149  Session->findSymbolByAddress(Address, PDB_SymType::Function);
150  auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
151 
152  if (NameKind == DINameKind::LinkageName) {
153  // It is not possible to get the mangled linkage name through a
154  // PDBSymbolFunc. For that we have to specifically request a
155  // PDBSymbolPublicSymbol.
156  auto PublicSym =
157  Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
158  if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
159  // If we also have a function symbol, prefer the use of public symbol name
160  // only if it refers to the same address. The public symbol uses the
161  // linkage name while the function does not.
162  if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
163  return PS->getName();
164  }
165  }
166 
167  return Func ? Func->getName() : std::string();
168 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:139
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1183
llvm::DINameKind::LinkageName
@ LinkageName
llvm::pdb::PDBContext::getLineInfoForDataAddress
DILineInfo getLineInfoForDataAddress(object::SectionedAddress Address) override
Definition: PDBContext.cpp:68
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: DWARFDebugLoc.h:25
llvm::DINameKind
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:135
llvm::DIContext
Definition: DIContext.h:225
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:52
PDBSymbol.h
PDBTypes.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
llvm::pdb::PDB_ColorItem::Address
@ Address
uint64_t
llvm::DILineInfo
A format-neutral container for source line information.
Definition: DIContext.h:31
PDBSymbolTypeFunctionSig.h
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:1676
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:35
llvm::DILineInfoSpecifier::FNKind
FunctionNameKind FNKind
Definition: DIContext.h:153
std
Definition: BitVector.h:851
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:33
llvm::pdb::PDBContext::getInliningInfoForAddress
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:95
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:83
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:139
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:75
llvm::DILineInfoSpecifier::FLIKind
FileLineInfoKind FLIKind
Definition: DIContext.h:152
PDBSymbolData.h
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:188