blob: 561a91ea08bab79fcc8c6f7be1969f491e2abc2e [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;
22
23PDBContext::PDBContext(const COFFObjectFile &Object,
Reid Klecknere94fef72015-10-09 00:15:01 +000024 std::unique_ptr<IPDBSession> PDBSession)
Zachary Turner20dbd0d2015-04-27 17:19:51 +000025 : DIContext(CK_PDB), Session(std::move(PDBSession)) {
Reid Klecknere94fef72015-10-09 00:15:01 +000026 ErrorOr<uint64_t> ImageBase = Object.getImageBase();
27 if (ImageBase)
28 Session->setLoadAddress(ImageBase.get());
Zachary Turner20dbd0d2015-04-27 17:19:51 +000029}
30
Pete Cooper6c97f4c2015-12-18 18:51:08 +000031void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType,
32 bool DumpEH) {}
Zachary Turner20dbd0d2015-04-27 17:19:51 +000033
34DILineInfo PDBContext::getLineInfoForAddress(uint64_t Address,
35 DILineInfoSpecifier Specifier) {
Zachary Turnerc007aa42015-05-06 22:26:30 +000036 DILineInfo Result;
37 Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
Zachary Turner20dbd0d2015-04-27 17:19:51 +000038
39 uint32_t Length = 1;
Zachary Turnerc007aa42015-05-06 22:26:30 +000040 std::unique_ptr<PDBSymbol> Symbol =
41 Session->findSymbolByAddress(Address, PDB_SymType::None);
Zachary Turner20dbd0d2015-04-27 17:19:51 +000042 if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
Zachary Turner20dbd0d2015-04-27 17:19:51 +000043 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, 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
66DILineInfoTable
67PDBContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
68 DILineInfoSpecifier Specifier) {
69 if (Size == 0)
70 return DILineInfoTable();
71
72 DILineInfoTable Table;
73 auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
74 if (!LineNumbers || LineNumbers->getChildCount() == 0)
75 return Table;
76
77 while (auto LineInfo = LineNumbers->getNext()) {
78 DILineInfo LineEntry =
79 getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
80 Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
81 }
82 return Table;
83}
84
85DIInliningInfo
86PDBContext::getInliningInfoForAddress(uint64_t Address,
87 DILineInfoSpecifier Specifier) {
88 DIInliningInfo InlineInfo;
89 DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
90 InlineInfo.addFrame(Frame);
91 return InlineInfo;
92}
Zachary Turnerc007aa42015-05-06 22:26:30 +000093
94std::string PDBContext::getFunctionName(uint64_t Address,
95 DINameKind NameKind) const {
96 if (NameKind == DINameKind::None)
97 return std::string();
98
99 if (NameKind == DINameKind::LinkageName) {
100 // It is not possible to get the mangled linkage name through a
101 // PDBSymbolFunc. For that we have to specifically request a
102 // PDBSymbolPublicSymbol.
103 auto PublicSym =
104 Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
105 if (auto PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get()))
106 return PS->getName();
107 }
108
109 auto FuncSymbol =
110 Session->findSymbolByAddress(Address, PDB_SymType::Function);
111
112 // This could happen either if there was no public symbol (e.g. not
113 // external) or the user requested the short name. In the former case,
114 // although they technically requested the linkage name, if the linkage
115 // name is not available we fallback to at least returning a non-empty
116 // string.
117 if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get()))
118 return Func->getName();
119
120 return std::string();
121}