xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/PDB/PDBContext.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBContext.h"
100b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbol.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
150b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
17*81ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
18*81ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/PDBTypes.h"
190b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric using namespace llvm::object;
230b57cec5SDimitry Andric using namespace llvm::pdb;
240b57cec5SDimitry Andric 
PDBContext(const COFFObjectFile & Object,std::unique_ptr<IPDBSession> PDBSession)250b57cec5SDimitry Andric PDBContext::PDBContext(const COFFObjectFile &Object,
260b57cec5SDimitry Andric                        std::unique_ptr<IPDBSession> PDBSession)
270b57cec5SDimitry Andric     : DIContext(CK_PDB), Session(std::move(PDBSession)) {
280b57cec5SDimitry Andric   ErrorOr<uint64_t> ImageBase = Object.getImageBase();
290b57cec5SDimitry Andric   if (ImageBase)
300b57cec5SDimitry Andric     Session->setLoadAddress(ImageBase.get());
310b57cec5SDimitry Andric }
320b57cec5SDimitry Andric 
dump(raw_ostream & OS,DIDumpOptions DumpOpts)330b57cec5SDimitry Andric void PDBContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){}
340b57cec5SDimitry Andric 
getLineInfoForAddress(object::SectionedAddress Address,DILineInfoSpecifier Specifier)350b57cec5SDimitry Andric DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
360b57cec5SDimitry Andric                                              DILineInfoSpecifier Specifier) {
370b57cec5SDimitry Andric   DILineInfo Result;
380b57cec5SDimitry Andric   Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   uint32_t Length = 1;
410b57cec5SDimitry Andric   std::unique_ptr<PDBSymbol> Symbol =
420b57cec5SDimitry Andric       Session->findSymbolByAddress(Address.Address, PDB_SymType::None);
430b57cec5SDimitry Andric   if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
440b57cec5SDimitry Andric     Length = Func->getLength();
450b57cec5SDimitry Andric   } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
460b57cec5SDimitry Andric     Length = Data->getLength();
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // If we couldn't find a symbol, then just assume 1 byte, so that we get
500b57cec5SDimitry Andric   // only the line number of the first instruction.
510b57cec5SDimitry Andric   auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Length);
520b57cec5SDimitry Andric   if (!LineNumbers || LineNumbers->getChildCount() == 0)
530b57cec5SDimitry Andric     return Result;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   auto LineInfo = LineNumbers->getNext();
560b57cec5SDimitry Andric   assert(LineInfo);
570b57cec5SDimitry Andric   auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   if (SourceFile &&
600b57cec5SDimitry Andric       Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
610b57cec5SDimitry Andric     Result.FileName = SourceFile->getFileName();
620b57cec5SDimitry Andric   Result.Column = LineInfo->getColumnNumber();
630b57cec5SDimitry Andric   Result.Line = LineInfo->getLineNumber();
640b57cec5SDimitry Andric   return Result;
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
67*81ad6265SDimitry Andric DILineInfo
getLineInfoForDataAddress(object::SectionedAddress Address)68*81ad6265SDimitry Andric PDBContext::getLineInfoForDataAddress(object::SectionedAddress Address) {
69*81ad6265SDimitry Andric   // Unimplemented. S_GDATA and S_LDATA in CodeView (used to describe global
70*81ad6265SDimitry Andric   // variables) aren't capable of carrying line information.
71*81ad6265SDimitry Andric   return DILineInfo();
72*81ad6265SDimitry Andric }
73*81ad6265SDimitry Andric 
740b57cec5SDimitry Andric DILineInfoTable
getLineInfoForAddressRange(object::SectionedAddress Address,uint64_t Size,DILineInfoSpecifier Specifier)750b57cec5SDimitry Andric PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address,
760b57cec5SDimitry Andric                                        uint64_t Size,
770b57cec5SDimitry Andric                                        DILineInfoSpecifier Specifier) {
780b57cec5SDimitry Andric   if (Size == 0)
790b57cec5SDimitry Andric     return DILineInfoTable();
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   DILineInfoTable Table;
820b57cec5SDimitry Andric   auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Size);
830b57cec5SDimitry Andric   if (!LineNumbers || LineNumbers->getChildCount() == 0)
840b57cec5SDimitry Andric     return Table;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   while (auto LineInfo = LineNumbers->getNext()) {
870b57cec5SDimitry Andric     DILineInfo LineEntry = getLineInfoForAddress(
880b57cec5SDimitry Andric         {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier);
890b57cec5SDimitry Andric     Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric   return Table;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric DIInliningInfo
getInliningInfoForAddress(object::SectionedAddress Address,DILineInfoSpecifier Specifier)950b57cec5SDimitry Andric PDBContext::getInliningInfoForAddress(object::SectionedAddress Address,
960b57cec5SDimitry Andric                                       DILineInfoSpecifier Specifier) {
970b57cec5SDimitry Andric   DIInliningInfo InlineInfo;
98e8d8bef9SDimitry Andric   DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier);
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric   // Find the function at this address.
101e8d8bef9SDimitry Andric   std::unique_ptr<PDBSymbol> ParentFunc =
102e8d8bef9SDimitry Andric       Session->findSymbolByAddress(Address.Address, PDB_SymType::Function);
103e8d8bef9SDimitry Andric   if (!ParentFunc) {
104e8d8bef9SDimitry Andric     InlineInfo.addFrame(CurrentLine);
105e8d8bef9SDimitry Andric     return InlineInfo;
106e8d8bef9SDimitry Andric   }
107e8d8bef9SDimitry Andric 
108e8d8bef9SDimitry Andric   auto Frames = ParentFunc->findInlineFramesByVA(Address.Address);
109e8d8bef9SDimitry Andric   if (!Frames || Frames->getChildCount() == 0) {
110e8d8bef9SDimitry Andric     InlineInfo.addFrame(CurrentLine);
111e8d8bef9SDimitry Andric     return InlineInfo;
112e8d8bef9SDimitry Andric   }
113e8d8bef9SDimitry Andric 
114e8d8bef9SDimitry Andric   while (auto Frame = Frames->getNext()) {
115e8d8bef9SDimitry Andric     uint32_t Length = 1;
116e8d8bef9SDimitry Andric     auto LineNumbers = Frame->findInlineeLinesByVA(Address.Address, Length);
117e8d8bef9SDimitry Andric     if (!LineNumbers || LineNumbers->getChildCount() == 0)
118e8d8bef9SDimitry Andric       break;
119e8d8bef9SDimitry Andric 
120e8d8bef9SDimitry Andric     std::unique_ptr<IPDBLineNumber> Line = LineNumbers->getNext();
121e8d8bef9SDimitry Andric     assert(Line);
122e8d8bef9SDimitry Andric 
123e8d8bef9SDimitry Andric     DILineInfo LineInfo;
124e8d8bef9SDimitry Andric     LineInfo.FunctionName = Frame->getName();
125e8d8bef9SDimitry Andric     auto SourceFile = Session->getSourceFileById(Line->getSourceFileId());
126e8d8bef9SDimitry Andric     if (SourceFile &&
127e8d8bef9SDimitry Andric         Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
128e8d8bef9SDimitry Andric       LineInfo.FileName = SourceFile->getFileName();
129e8d8bef9SDimitry Andric     LineInfo.Line = Line->getLineNumber();
130e8d8bef9SDimitry Andric     LineInfo.Column = Line->getColumnNumber();
131e8d8bef9SDimitry Andric     InlineInfo.addFrame(LineInfo);
132e8d8bef9SDimitry Andric   }
133e8d8bef9SDimitry Andric 
134e8d8bef9SDimitry Andric   InlineInfo.addFrame(CurrentLine);
1350b57cec5SDimitry Andric   return InlineInfo;
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric std::vector<DILocal>
getLocalsForAddress(object::SectionedAddress Address)1390b57cec5SDimitry Andric PDBContext::getLocalsForAddress(object::SectionedAddress Address) {
1400b57cec5SDimitry Andric   return std::vector<DILocal>();
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
getFunctionName(uint64_t Address,DINameKind NameKind) const1430b57cec5SDimitry Andric std::string PDBContext::getFunctionName(uint64_t Address,
1440b57cec5SDimitry Andric                                         DINameKind NameKind) const {
1450b57cec5SDimitry Andric   if (NameKind == DINameKind::None)
1460b57cec5SDimitry Andric     return std::string();
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   std::unique_ptr<PDBSymbol> FuncSymbol =
1490b57cec5SDimitry Andric       Session->findSymbolByAddress(Address, PDB_SymType::Function);
1500b57cec5SDimitry Andric   auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   if (NameKind == DINameKind::LinkageName) {
1530b57cec5SDimitry Andric     // It is not possible to get the mangled linkage name through a
1540b57cec5SDimitry Andric     // PDBSymbolFunc.  For that we have to specifically request a
1550b57cec5SDimitry Andric     // PDBSymbolPublicSymbol.
1560b57cec5SDimitry Andric     auto PublicSym =
1570b57cec5SDimitry Andric         Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
1580b57cec5SDimitry Andric     if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
1590b57cec5SDimitry Andric       // If we also have a function symbol, prefer the use of public symbol name
1600b57cec5SDimitry Andric       // only if it refers to the same address. The public symbol uses the
1610b57cec5SDimitry Andric       // linkage name while the function does not.
1620b57cec5SDimitry Andric       if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
1630b57cec5SDimitry Andric         return PS->getName();
1640b57cec5SDimitry Andric     }
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   return Func ? Func->getName() : std::string();
1680b57cec5SDimitry Andric }
169