LLVM 20.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
21using namespace llvm;
22using namespace llvm::object;
23using namespace llvm::pdb;
24
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 &&
60 Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
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
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 &&
127 Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
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
138std::vector<DILocal>
140 return std::vector<DILocal>();
141}
142
143std::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}
uint64_t Size
@ InlineInfo
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A format-neutral container for inlined code description.
Definition: DIContext.h:94
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:75
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:95
std::vector< DILocal > getLocalsForAddress(object::SectionedAddress Address) override
Definition: PDBContext.cpp:139
DILineInfo getLineInfoForDataAddress(object::SectionedAddress Address) override
Definition: PDBContext.cpp:68
DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
Definition: PDBContext.cpp:35
PDBContext(const object::COFFObjectFile &Object, std::unique_ptr< IPDBSession > PDBSession)
Definition: PDBContext.cpp:25
void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override
Definition: PDBContext.cpp:33
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:480
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:91
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:142
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:1856
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:146
FileLineInfoKind FLIKind
Definition: DIContext.h:158
FunctionNameKind FNKind
Definition: DIContext.h:159
A format-neutral container for source line information.
Definition: DIContext.h:32
uint32_t Line
Definition: DIContext.h:46
std::string FileName
Definition: DIContext.h:38
std::string FunctionName
Definition: DIContext.h:39
uint32_t Column
Definition: DIContext.h:47