blob: 59564161b4e867755669e8e8d675ddca92e55e44 [file] [log] [blame]
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +00001//===-- LLVMSymbolize.cpp -------------------------------------------------===//
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// Implementation for LLVM symbolization library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLVMSymbolize.h"
Alexey Samsonov51283a12013-03-19 15:33:18 +000015#include "llvm/ADT/STLExtras.h"
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000016#include "llvm/Object/MachO.h"
17#include "llvm/Support/Casting.h"
Alexey Samsonov888ca962013-06-04 07:57:38 +000018#include "llvm/Support/FileSystem.h"
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000019#include "llvm/Support/Path.h"
20
21#include <sstream>
22
23namespace llvm {
24namespace symbolize {
25
Dmitry Vyukovb1819192013-02-14 13:06:18 +000026static bool error(error_code ec) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000027 if (!ec)
28 return false;
Dmitry Vyukovb1819192013-02-14 13:06:18 +000029 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
30 return true;
31}
32
Alexey Samsonovc4439c32013-02-15 08:54:47 +000033static uint32_t
34getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000035 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
36 llvm::DILineInfoSpecifier::AbsoluteFilePath;
37 if (Opts.PrintFunctions)
38 Flags |= llvm::DILineInfoSpecifier::FunctionName;
39 return Flags;
40}
41
42static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
43 DILineInfo &LineInfo) {
44 std::string FileName = LineInfo.getFileName();
45 LineInfo = DILineInfo(StringRef(FileName), StringRef(NewFunctionName),
46 LineInfo.getLine(), LineInfo.getColumn());
47}
48
Dmitry Vyukovb1819192013-02-14 13:06:18 +000049ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovc4439c32013-02-15 08:54:47 +000050 : Module(Obj), DebugInfoContext(DICtx) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000051 error_code ec;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000052 for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
53 si != se; si.increment(ec)) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000054 if (error(ec))
55 return;
56 SymbolRef::Type SymbolType;
57 if (error(si->getType(SymbolType)))
58 continue;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000059 if (SymbolType != SymbolRef::ST_Function &&
60 SymbolType != SymbolRef::ST_Data)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000061 continue;
62 uint64_t SymbolAddress;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000063 if (error(si->getAddress(SymbolAddress)) ||
64 SymbolAddress == UnknownAddressOrSize)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000065 continue;
66 uint64_t SymbolSize;
Alexey Samsonovb6564642013-06-07 15:25:27 +000067 // Getting symbol size is linear for Mach-O files, so assume that symbol
68 // occupies the memory range up to the following symbol.
Alexey Samsonov888ca962013-06-04 07:57:38 +000069 if (isa<MachOObjectFile>(Obj))
70 SymbolSize = 0;
71 else if (error(si->getSize(SymbolSize)) ||
72 SymbolSize == UnknownAddressOrSize)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000073 continue;
74 StringRef SymbolName;
75 if (error(si->getName(SymbolName)))
76 continue;
Alexey Samsonov8c6e3242013-06-28 14:25:52 +000077 // Mach-O symbol table names have leading underscore, skip it.
78 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
79 SymbolName = SymbolName.drop_front();
Dmitry Vyukovb1819192013-02-14 13:06:18 +000080 // FIXME: If a function has alias, there are two entries in symbol table
81 // with same address size. Make sure we choose the correct one.
Alexey Samsonovc4439c32013-02-15 08:54:47 +000082 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonovb6564642013-06-07 15:25:27 +000083 SymbolDesc SD = { SymbolAddress, SymbolSize };
Dmitry Vyukovb1819192013-02-14 13:06:18 +000084 M.insert(std::make_pair(SD, SymbolName));
85 }
86}
87
88bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
89 std::string &Name, uint64_t &Addr,
90 uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000091 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov888ca962013-06-04 07:57:38 +000092 if (M.empty())
Dmitry Vyukovb1819192013-02-14 13:06:18 +000093 return false;
Alexey Samsonov888ca962013-06-04 07:57:38 +000094 SymbolDesc SD = { Address, Address };
95 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonovb6564642013-06-07 15:25:27 +000096 if (it == M.begin())
97 return false;
Alexey Samsonov888ca962013-06-04 07:57:38 +000098 --it;
Alexey Samsonovb6564642013-06-07 15:25:27 +000099 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000100 return false;
101 Name = it->second.str();
102 Addr = it->first.Addr;
Alexey Samsonovb6564642013-06-07 15:25:27 +0000103 Size = it->first.Size;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000104 return true;
105}
106
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000107DILineInfo ModuleInfo::symbolizeCode(
108 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000109 DILineInfo LineInfo;
110 if (DebugInfoContext) {
111 LineInfo = DebugInfoContext->getLineInfoForAddress(
112 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
113 }
114 // Override function name from symbol table if necessary.
115 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
116 std::string FunctionName;
117 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000118 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
119 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000120 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
121 }
122 }
123 return LineInfo;
124}
125
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000126DIInliningInfo ModuleInfo::symbolizeInlinedCode(
127 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000128 DIInliningInfo InlinedContext;
129 if (DebugInfoContext) {
130 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
131 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
132 }
133 // Make sure there is at least one frame in context.
134 if (InlinedContext.getNumberOfFrames() == 0) {
135 InlinedContext.addFrame(DILineInfo());
136 }
137 // Override the function name in lower frame with name from symbol table.
138 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
139 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000140 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000141 DILineInfo LineInfo = InlinedContext.getFrame(i);
142 if (i == n - 1) {
143 std::string FunctionName;
144 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000145 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
146 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000147 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
148 }
149 }
150 PatchedInlinedContext.addFrame(LineInfo);
151 }
152 InlinedContext = PatchedInlinedContext;
153 }
154 return InlinedContext;
155}
156
157bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
158 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000159 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
160 Size);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000161}
162
Alexey Samsonov638c63c2013-02-04 15:55:26 +0000163const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000164
165std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
166 uint64_t ModuleOffset) {
167 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
168 if (Info == 0)
169 return printDILineInfo(DILineInfo());
170 if (Opts.PrintInlining) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000171 DIInliningInfo InlinedContext =
172 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000173 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
174 assert(FramesNum > 0);
175 std::string Result;
176 for (uint32_t i = 0; i < FramesNum; i++) {
177 DILineInfo LineInfo = InlinedContext.getFrame(i);
178 Result += printDILineInfo(LineInfo);
179 }
180 return Result;
181 }
182 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
183 return printDILineInfo(LineInfo);
184}
185
186std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
187 uint64_t ModuleOffset) {
188 std::string Name = kBadString;
189 uint64_t Start = 0;
190 uint64_t Size = 0;
191 if (Opts.UseSymbolTable) {
192 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonovc071f892013-06-28 12:06:25 +0000193 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
194 Name = DemangleName(Name);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000195 }
196 }
197 std::stringstream ss;
198 ss << Name << "\n" << Start << " " << Size << "\n";
199 return ss.str();
200}
201
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000202void LLVMSymbolizer::flush() {
Alexey Samsonov51283a12013-03-19 15:33:18 +0000203 DeleteContainerSeconds(Modules);
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000204 DeleteContainerPointers(ParsedBinariesAndObjects);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000205}
206
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000207static std::string getDarwinDWARFResourceForPath(const std::string &Path) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000208 StringRef Basename = sys::path::filename(Path);
209 const std::string &DSymDirectory = Path + ".dSYM";
210 SmallString<16> ResourceName = StringRef(DSymDirectory);
211 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
212 sys::path::append(ResourceName, Basename);
213 return ResourceName.str();
214}
215
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000216LLVMSymbolizer::BinaryPair
217LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
218 BinaryMapTy::iterator I = BinaryForPath.find(Path);
219 if (I != BinaryForPath.end())
220 return I->second;
221 Binary *Bin = 0;
222 Binary *DbgBin = 0;
223 OwningPtr<Binary> ParsedBinary;
224 OwningPtr<Binary> ParsedDbgBinary;
225 if (!error(createBinary(Path, ParsedBinary))) {
226 // Check if it's a universal binary.
227 Bin = ParsedBinary.take();
228 ParsedBinariesAndObjects.push_back(Bin);
229 if (Bin->isMachO() || Bin->isMachOUniversalBinary()) {
230 // On Darwin we may find DWARF in separate object file in
231 // resource directory.
232 const std::string &ResourcePath =
233 getDarwinDWARFResourceForPath(Path);
234 bool ResourceFileExists = false;
235 if (!sys::fs::exists(ResourcePath, ResourceFileExists) &&
236 ResourceFileExists &&
237 !error(createBinary(ResourcePath, ParsedDbgBinary))) {
238 DbgBin = ParsedDbgBinary.take();
239 ParsedBinariesAndObjects.push_back(DbgBin);
240 }
241 }
242 }
243 if (DbgBin == 0)
244 DbgBin = Bin;
245 BinaryPair Res = std::make_pair(Bin, DbgBin);
246 BinaryForPath[Path] = Res;
247 return Res;
248}
249
250ObjectFile *
251LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) {
252 if (Bin == 0)
253 return 0;
254 ObjectFile *Res = 0;
255 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
256 ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find(
257 std::make_pair(UB, ArchName));
258 if (I != ObjectFileForArch.end())
259 return I->second;
260 OwningPtr<ObjectFile> ParsedObj;
261 if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) {
262 Res = ParsedObj.take();
263 ParsedBinariesAndObjects.push_back(Res);
264 }
265 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
266 } else if (Bin->isObject()) {
267 Res = cast<ObjectFile>(Bin);
268 }
269 return Res;
270}
271
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000272ModuleInfo *
273LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000274 ModuleMapTy::iterator I = Modules.find(ModuleName);
275 if (I != Modules.end())
276 return I->second;
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000277 std::string BinaryName = ModuleName;
278 std::string ArchName = Opts.DefaultArch;
279 size_t ColonPos = ModuleName.find(':');
280 if (ColonPos != std::string::npos) {
281 BinaryName = ModuleName.substr(0, ColonPos);
282 ArchName = ModuleName.substr(ColonPos + 1);
283 }
284 BinaryPair Binaries = getOrCreateBinary(BinaryName);
285 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
286 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000287
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000288 if (Obj == 0) {
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000289 // Failed to find valid object file.
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000290 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000291 return 0;
292 }
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000293 DIContext *Context = DIContext::getDWARFContext(DbgObj);
294 assert(Context);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000295 ModuleInfo *Info = new ModuleInfo(Obj, Context);
296 Modules.insert(make_pair(ModuleName, Info));
297 return Info;
298}
299
300std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
301 // By default, DILineInfo contains "<invalid>" for function/filename it
302 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
303 static const std::string kDILineInfoBadString = "<invalid>";
304 std::stringstream Result;
305 if (Opts.PrintFunctions) {
306 std::string FunctionName = LineInfo.getFunctionName();
307 if (FunctionName == kDILineInfoBadString)
308 FunctionName = kBadString;
Alexey Samsonovc071f892013-06-28 12:06:25 +0000309 else if (Opts.Demangle)
310 FunctionName = DemangleName(FunctionName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000311 Result << FunctionName << "\n";
312 }
313 std::string Filename = LineInfo.getFileName();
314 if (Filename == kDILineInfoBadString)
315 Filename = kBadString;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000316 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
317 << "\n";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000318 return Result.str();
319}
320
321#if !defined(_MSC_VER)
322// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
323extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
324 size_t *length, int *status);
325#endif
326
Alexey Samsonovc071f892013-06-28 12:06:25 +0000327std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000328#if !defined(_MSC_VER)
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000329 int status = 0;
330 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
331 if (status != 0)
Alexey Samsonovc071f892013-06-28 12:06:25 +0000332 return Name;
333 std::string Result = DemangledName;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000334 free(DemangledName);
Alexey Samsonovc071f892013-06-28 12:06:25 +0000335 return Result;
336#else
337 return Name;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000338#endif
339}
340
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000341} // namespace symbolize
342} // namespace llvm