blob: 94b81ecf561e23cdf51de7183e1c68261ebfd07c [file] [log] [blame]
Zachary Turner20dbd0d2015-04-27 17:19:51 +00001//===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===/
9
10#include "llvm/DebugInfo/PDB/PDBContext.h"
11#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
12#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
13#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
14#include "llvm/DebugInfo/PDB/PDBSymbol.h"
15#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
16#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turnerc007aa42015-05-06 22:26:30 +000017#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner20dbd0d2015-04-27 17:19:51 +000018#include "llvm/Object/COFF.h"
19
20using namespace llvm;
21using namespace llvm::object;
Zachary Turnerec28fc32016-05-04 20:32:13 +000022using namespace llvm::pdb;
Zachary Turner20dbd0d2015-04-27 17:19:51 +000023
24PDBContext::PDBContext(const COFFObjectFile &Object,
Reid Klecknere94fef72015-10-09 00:15:01 +000025 std::unique_ptr<IPDBSession> PDBSession)
Zachary Turner20dbd0d2015-04-27 17:19:51 +000026 : DIContext(CK_PDB), Session(std::move(PDBSession)) {
Reid Klecknere94fef72015-10-09 00:15:01 +000027 ErrorOr<uint64_t> ImageBase = Object.getImageBase();
28 if (ImageBase)
29 Session->setLoadAddress(ImageBase.get());
Zachary Turner20dbd0d2015-04-27 17:19:51 +000030}
31
David Blaikie50cc27e2016-10-18 21:09:48 +000032void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
33 bool SummarizeTypes) {}
Zachary Turner20dbd0d2015-04-27 17:19:51 +000034
35DILineInfo PDBContext::getLineInfoForAddress(uint64_t Address,
36 DILineInfoSpecifier Specifier) {
Zachary Turnerc007aa42015-05-06 22:26:30 +000037 DILineInfo Result;
38 Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
Zachary Turner20dbd0d2015-04-27 17:19:51 +000039
40 uint32_t Length = 1;
Zachary Turnerc007aa42015-05-06 22:26:30 +000041 std::unique_ptr<PDBSymbol> Symbol =
42 Session->findSymbolByAddress(Address, PDB_SymType::None);
Zachary Turner20dbd0d2015-04-27 17:19:51 +000043 if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
Zachary Turner20dbd0d2015-04-27 17:19:51 +000044 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, 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
67DILineInfoTable
68PDBContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
69 DILineInfoSpecifier Specifier) {
70 if (Size == 0)
71 return DILineInfoTable();
72
73 DILineInfoTable Table;
74 auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
75 if (!LineNumbers || LineNumbers->getChildCount() == 0)
76 return Table;
77
78 while (auto LineInfo = LineNumbers->getNext()) {
79 DILineInfo LineEntry =
80 getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
81 Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
82 }
83 return Table;
84}
85
86DIInliningInfo
87PDBContext::getInliningInfoForAddress(uint64_t Address,
88 DILineInfoSpecifier Specifier) {
89 DIInliningInfo InlineInfo;
90 DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
91 InlineInfo.addFrame(Frame);
92 return InlineInfo;
93}
Zachary Turnerc007aa42015-05-06 22:26:30 +000094
95std::string PDBContext::getFunctionName(uint64_t Address,
96 DINameKind NameKind) const {
97 if (NameKind == DINameKind::None)
98 return std::string();
99
Reid Kleckner0336cc02016-04-27 16:10:29 +0000100 std::unique_ptr<PDBSymbol> FuncSymbol =
101 Session->findSymbolByAddress(Address, PDB_SymType::Function);
102 auto *Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get());
103
Zachary Turnerc007aa42015-05-06 22:26:30 +0000104 if (NameKind == DINameKind::LinkageName) {
105 // It is not possible to get the mangled linkage name through a
106 // PDBSymbolFunc. For that we have to specifically request a
107 // PDBSymbolPublicSymbol.
108 auto PublicSym =
109 Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
Reid Kleckner0336cc02016-04-27 16:10:29 +0000110 if (auto *PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get())) {
111 // If we also have a function symbol, prefer the use of public symbol name
112 // only if it refers to the same address. The public symbol uses the
113 // linkage name while the function does not.
114 if (!Func || Func->getVirtualAddress() == PS->getVirtualAddress())
115 return PS->getName();
116 }
Zachary Turnerc007aa42015-05-06 22:26:30 +0000117 }
118
Reid Kleckner0336cc02016-04-27 16:10:29 +0000119 return Func ? Func->getName() : std::string();
Zachary Turnerc007aa42015-05-06 22:26:30 +0000120}