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 9 #include "llvm/DebugInfo/PDB/PDBContext.h" 10 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 11 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 12 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 13 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 14 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 15 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 16 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" 18 #include "llvm/Object/COFF.h" 19 20 using namespace llvm; 21 using namespace llvm::object; 22 using namespace llvm::pdb; 23 24 PDBContext::PDBContext(const COFFObjectFile &Object, 25 std::unique_ptr<IPDBSession> PDBSession) 26 : DIContext(CK_PDB), Session(std::move(PDBSession)) { 27 ErrorOr<uint64_t> ImageBase = Object.getImageBase(); 28 if (ImageBase) 29 Session->setLoadAddress(ImageBase.get()); 30 } 31 32 void PDBContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){} 33 34 DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address, 35 DILineInfoSpecifier Specifier) { 36 DILineInfo Result; 37 Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind); 38 39 uint32_t Length = 1; 40 std::unique_ptr<PDBSymbol> Symbol = 41 Session->findSymbolByAddress(Address.Address, PDB_SymType::None); 42 if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) { 43 Length = Func->getLength(); 44 } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) { 45 Length = Data->getLength(); 46 } 47 48 // If we couldn't find a symbol, then just assume 1 byte, so that we get 49 // only the line number of the first instruction. 50 auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Length); 51 if (!LineNumbers || LineNumbers->getChildCount() == 0) 52 return Result; 53 54 auto LineInfo = LineNumbers->getNext(); 55 assert(LineInfo); 56 auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId()); 57 58 if (SourceFile && 59 Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None) 60 Result.FileName = SourceFile->getFileName(); 61 Result.Column = LineInfo->getColumnNumber(); 62 Result.Line = LineInfo->getLineNumber(); 63 return Result; 64 } 65 66 DILineInfoTable 67 PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address, 68 uint64_t Size, 69 DILineInfoSpecifier Specifier) { 70 if (Size == 0) 71 return DILineInfoTable(); 72 73 DILineInfoTable Table; 74 auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Size); 75 if (!LineNumbers || LineNumbers->getChildCount() == 0) 76 return Table; 77 78 while (auto LineInfo = LineNumbers->getNext()) { 79 DILineInfo LineEntry = getLineInfoForAddress( 80 {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier); 81 Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry)); 82 } 83 return Table; 84 } 85 86 DIInliningInfo 87 PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, 88 DILineInfoSpecifier Specifier) { 89 DIInliningInfo InlineInfo; 90 DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier); 91 92 // Find the function at this address. 93 std::unique_ptr<PDBSymbol> ParentFunc = 94 Session->findSymbolByAddress(Address.Address, PDB_SymType::Function); 95 if (!ParentFunc) { 96 InlineInfo.addFrame(CurrentLine); 97 return InlineInfo; 98 } 99 100 auto Frames = ParentFunc->findInlineFramesByVA(Address.Address); 101 if (!Frames || Frames->getChildCount() == 0) { 102 InlineInfo.addFrame(CurrentLine); 103 return InlineInfo; 104 } 105 106 while (auto Frame = Frames->getNext()) { 107 uint32_t Length = 1; 108 auto LineNumbers = Frame->findInlineeLinesByVA(Address.Address, Length); 109 if (!LineNumbers || LineNumbers->getChildCount() == 0) 110 break; 111 112 std::unique_ptr<IPDBLineNumber> Line = LineNumbers->getNext(); 113 assert(Line); 114 115 DILineInfo LineInfo; 116 LineInfo.FunctionName = Frame->getName(); 117 auto SourceFile = Session->getSourceFileById(Line->getSourceFileId()); 118 if (SourceFile && 119 Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None) 120 LineInfo.FileName = SourceFile->getFileName(); 121 LineInfo.Line = Line->getLineNumber(); 122 LineInfo.Column = Line->getColumnNumber(); 123 InlineInfo.addFrame(LineInfo); 124 } 125 126 InlineInfo.addFrame(CurrentLine); 127 return InlineInfo; 128 } 129 130 std::vector<DILocal> 131 PDBContext::getLocalsForAddress(object::SectionedAddress Address) { 132 return std::vector<DILocal>(); 133 } 134 135 std::string PDBContext::getFunctionName(uint64_t Address, 136 DINameKind NameKind) const { 137 if (NameKind == DINameKind::None) 138 return std::string(); 139 140 std::unique_ptr<PDBSymbol> FuncSymbol = 141 Session->findSymbolByAddress(Address, PDB_SymType::Function); 142 auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get()); 143 144 if (NameKind == DINameKind::LinkageName) { 145 // It is not possible to get the mangled linkage name through a 146 // PDBSymbolFunc. For that we have to specifically request a 147 // PDBSymbolPublicSymbol. 148 auto PublicSym = 149 Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol); 150 if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) { 151 // If we also have a function symbol, prefer the use of public symbol name 152 // only if it refers to the same address. The public symbol uses the 153 // linkage name while the function does not. 154 if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress()) 155 return PS->getName(); 156 } 157 } 158 159 return Func ? Func->getName() : std::string(); 160 } 161