2015-04-28 01:19:51 +08:00
|
|
|
//===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-04-28 01:19:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/PDB/PDBContext.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbol.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
|
2015-05-07 06:26:30 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
|
2015-04-28 01:19:51 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
2016-05-05 04:32:13 +08:00
|
|
|
using namespace llvm::pdb;
|
2015-04-28 01:19:51 +08:00
|
|
|
|
|
|
|
PDBContext::PDBContext(const COFFObjectFile &Object,
|
2015-10-09 08:15:01 +08:00
|
|
|
std::unique_ptr<IPDBSession> PDBSession)
|
2015-04-28 01:19:51 +08:00
|
|
|
: DIContext(CK_PDB), Session(std::move(PDBSession)) {
|
2015-10-09 08:15:01 +08:00
|
|
|
ErrorOr<uint64_t> ImageBase = Object.getImageBase();
|
|
|
|
if (ImageBase)
|
|
|
|
Session->setLoadAddress(ImageBase.get());
|
2015-04-28 01:19:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 02:18:23 +08:00
|
|
|
void PDBContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){}
|
2015-04-28 01:19:51 +08:00
|
|
|
|
2019-02-27 21:17:36 +08:00
|
|
|
DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
|
2015-04-28 01:19:51 +08:00
|
|
|
DILineInfoSpecifier Specifier) {
|
2015-05-07 06:26:30 +08:00
|
|
|
DILineInfo Result;
|
2019-02-27 21:17:36 +08:00
|
|
|
Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);
|
2015-04-28 01:19:51 +08:00
|
|
|
|
|
|
|
uint32_t Length = 1;
|
2015-05-07 06:26:30 +08:00
|
|
|
std::unique_ptr<PDBSymbol> Symbol =
|
2019-02-27 21:17:36 +08:00
|
|
|
Session->findSymbolByAddress(Address.Address, PDB_SymType::None);
|
2015-04-28 01:19:51 +08:00
|
|
|
if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
|
|
|
|
Length = Func->getLength();
|
|
|
|
} else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
|
|
|
|
Length = Data->getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we couldn't find a symbol, then just assume 1 byte, so that we get
|
|
|
|
// only the line number of the first instruction.
|
2019-02-27 21:17:36 +08:00
|
|
|
auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Length);
|
2015-04-28 01:19:51 +08:00
|
|
|
if (!LineNumbers || LineNumbers->getChildCount() == 0)
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
auto LineInfo = LineNumbers->getNext();
|
|
|
|
assert(LineInfo);
|
|
|
|
auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
|
|
|
|
|
|
|
|
if (SourceFile &&
|
|
|
|
Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
|
|
|
|
Result.FileName = SourceFile->getFileName();
|
|
|
|
Result.Column = LineInfo->getColumnNumber();
|
|
|
|
Result.Line = LineInfo->getLineNumber();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DILineInfoTable
|
2019-02-27 21:17:36 +08:00
|
|
|
PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address,
|
|
|
|
uint64_t Size,
|
2015-04-28 01:19:51 +08:00
|
|
|
DILineInfoSpecifier Specifier) {
|
|
|
|
if (Size == 0)
|
|
|
|
return DILineInfoTable();
|
|
|
|
|
|
|
|
DILineInfoTable Table;
|
2019-02-27 21:17:36 +08:00
|
|
|
auto LineNumbers = Session->findLineNumbersByAddress(Address.Address, Size);
|
2015-04-28 01:19:51 +08:00
|
|
|
if (!LineNumbers || LineNumbers->getChildCount() == 0)
|
|
|
|
return Table;
|
|
|
|
|
|
|
|
while (auto LineInfo = LineNumbers->getNext()) {
|
2019-02-27 21:17:36 +08:00
|
|
|
DILineInfo LineEntry = getLineInfoForAddress(
|
|
|
|
{LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier);
|
2015-04-28 01:19:51 +08:00
|
|
|
Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
|
|
|
|
}
|
|
|
|
return Table;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIInliningInfo
|
2019-02-27 21:17:36 +08:00
|
|
|
PDBContext::getInliningInfoForAddress(object::SectionedAddress Address,
|
2015-04-28 01:19:51 +08:00
|
|
|
DILineInfoSpecifier Specifier) {
|
|
|
|
DIInliningInfo InlineInfo;
|
|
|
|
DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
|
|
|
|
InlineInfo.addFrame(Frame);
|
|
|
|
return InlineInfo;
|
|
|
|
}
|
2015-05-07 06:26:30 +08:00
|
|
|
|
2019-06-25 04:03:23 +08:00
|
|
|
std::vector<DILocal>
|
|
|
|
PDBContext::getLocalsForAddress(object::SectionedAddress Address) {
|
|
|
|
return std::vector<DILocal>();
|
|
|
|
}
|
|
|
|
|
2015-05-07 06:26:30 +08:00
|
|
|
std::string PDBContext::getFunctionName(uint64_t Address,
|
|
|
|
DINameKind NameKind) const {
|
|
|
|
if (NameKind == DINameKind::None)
|
|
|
|
return std::string();
|
|
|
|
|
2016-04-28 00:10:29 +08:00
|
|
|
std::unique_ptr<PDBSymbol> FuncSymbol =
|
|
|
|
Session->findSymbolByAddress(Address, PDB_SymType::Function);
|
|
|
|
auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
|
|
|
|
|
2015-05-07 06:26:30 +08:00
|
|
|
if (NameKind == DINameKind::LinkageName) {
|
|
|
|
// It is not possible to get the mangled linkage name through a
|
|
|
|
// PDBSymbolFunc. For that we have to specifically request a
|
|
|
|
// PDBSymbolPublicSymbol.
|
|
|
|
auto PublicSym =
|
|
|
|
Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
|
2016-04-28 00:10:29 +08:00
|
|
|
if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
|
|
|
|
// If we also have a function symbol, prefer the use of public symbol name
|
|
|
|
// only if it refers to the same address. The public symbol uses the
|
|
|
|
// linkage name while the function does not.
|
|
|
|
if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
|
|
|
|
return PS->getName();
|
|
|
|
}
|
2015-05-07 06:26:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 00:10:29 +08:00
|
|
|
return Func ? Func->getName() : std::string();
|
2015-05-07 06:26:30 +08:00
|
|
|
}
|