blob: 29d91a0e92a378b70ed58f9880809bbc00fccd88 [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"
18#include "llvm/Support/Path.h"
19
20#include <sstream>
21
22namespace llvm {
23namespace symbolize {
24
Dmitry Vyukovb1819192013-02-14 13:06:18 +000025static bool error(error_code ec) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000026 if (!ec)
27 return false;
Dmitry Vyukovb1819192013-02-14 13:06:18 +000028 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
29 return true;
30}
31
Alexey Samsonovc4439c32013-02-15 08:54:47 +000032static uint32_t
33getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000034 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
35 llvm::DILineInfoSpecifier::AbsoluteFilePath;
36 if (Opts.PrintFunctions)
37 Flags |= llvm::DILineInfoSpecifier::FunctionName;
38 return Flags;
39}
40
41static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
42 DILineInfo &LineInfo) {
43 std::string FileName = LineInfo.getFileName();
44 LineInfo = DILineInfo(StringRef(FileName), StringRef(NewFunctionName),
45 LineInfo.getLine(), LineInfo.getColumn());
46}
47
Dmitry Vyukovb1819192013-02-14 13:06:18 +000048ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovc4439c32013-02-15 08:54:47 +000049 : Module(Obj), DebugInfoContext(DICtx) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000050 error_code ec;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000051 for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
52 si != se; si.increment(ec)) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000053 if (error(ec))
54 return;
55 SymbolRef::Type SymbolType;
56 if (error(si->getType(SymbolType)))
57 continue;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000058 if (SymbolType != SymbolRef::ST_Function &&
59 SymbolType != SymbolRef::ST_Data)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000060 continue;
61 uint64_t SymbolAddress;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000062 if (error(si->getAddress(SymbolAddress)) ||
63 SymbolAddress == UnknownAddressOrSize)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000064 continue;
65 uint64_t SymbolSize;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000066 if (error(si->getSize(SymbolSize)) || SymbolSize == UnknownAddressOrSize)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000067 continue;
68 StringRef SymbolName;
69 if (error(si->getName(SymbolName)))
70 continue;
71 // FIXME: If a function has alias, there are two entries in symbol table
72 // with same address size. Make sure we choose the correct one.
Alexey Samsonovc4439c32013-02-15 08:54:47 +000073 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
74 SymbolDesc SD = { SymbolAddress, SymbolAddress + SymbolSize };
Dmitry Vyukovb1819192013-02-14 13:06:18 +000075 M.insert(std::make_pair(SD, SymbolName));
76 }
77}
78
79bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
80 std::string &Name, uint64_t &Addr,
81 uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000082 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
83 SymbolDesc SD = { Address, Address + 1 };
Dmitry Vyukovb1819192013-02-14 13:06:18 +000084 SymbolMapTy::const_iterator it = M.find(SD);
85 if (it == M.end())
86 return false;
87 if (Address < it->first.Addr || Address >= it->first.AddrEnd)
88 return false;
89 Name = it->second.str();
90 Addr = it->first.Addr;
91 Size = it->first.AddrEnd - it->first.Addr;
92 return true;
93}
94
Alexey Samsonovc4439c32013-02-15 08:54:47 +000095DILineInfo ModuleInfo::symbolizeCode(
96 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000097 DILineInfo LineInfo;
98 if (DebugInfoContext) {
99 LineInfo = DebugInfoContext->getLineInfoForAddress(
100 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
101 }
102 // Override function name from symbol table if necessary.
103 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
104 std::string FunctionName;
105 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000106 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
107 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000108 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
109 }
110 }
111 return LineInfo;
112}
113
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000114DIInliningInfo ModuleInfo::symbolizeInlinedCode(
115 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000116 DIInliningInfo InlinedContext;
117 if (DebugInfoContext) {
118 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
119 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
120 }
121 // Make sure there is at least one frame in context.
122 if (InlinedContext.getNumberOfFrames() == 0) {
123 InlinedContext.addFrame(DILineInfo());
124 }
125 // Override the function name in lower frame with name from symbol table.
126 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
127 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000128 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000129 DILineInfo LineInfo = InlinedContext.getFrame(i);
130 if (i == n - 1) {
131 std::string FunctionName;
132 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000133 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
134 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000135 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
136 }
137 }
138 PatchedInlinedContext.addFrame(LineInfo);
139 }
140 InlinedContext = PatchedInlinedContext;
141 }
142 return InlinedContext;
143}
144
145bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
146 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000147 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
148 Size);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000149}
150
Alexey Samsonov638c63c2013-02-04 15:55:26 +0000151const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000152
153std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
154 uint64_t ModuleOffset) {
155 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
156 if (Info == 0)
157 return printDILineInfo(DILineInfo());
158 if (Opts.PrintInlining) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000159 DIInliningInfo InlinedContext =
160 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000161 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
162 assert(FramesNum > 0);
163 std::string Result;
164 for (uint32_t i = 0; i < FramesNum; i++) {
165 DILineInfo LineInfo = InlinedContext.getFrame(i);
166 Result += printDILineInfo(LineInfo);
167 }
168 return Result;
169 }
170 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
171 return printDILineInfo(LineInfo);
172}
173
174std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
175 uint64_t ModuleOffset) {
176 std::string Name = kBadString;
177 uint64_t Start = 0;
178 uint64_t Size = 0;
179 if (Opts.UseSymbolTable) {
180 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
181 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
182 DemangleName(Name);
183 }
184 }
185 std::stringstream ss;
186 ss << Name << "\n" << Start << " " << Size << "\n";
187 return ss.str();
188}
189
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000190void LLVMSymbolizer::flush() {
Alexey Samsonov51283a12013-03-19 15:33:18 +0000191 DeleteContainerSeconds(Modules);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000192}
193
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000194// Returns true if the object endianness is known.
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000195static bool getObjectEndianness(const ObjectFile *Obj, bool &IsLittleEndian) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000196 // FIXME: Implement this when libLLVMObject allows to do it easily.
197 IsLittleEndian = true;
198 return true;
199}
200
201static ObjectFile *getObjectFile(const std::string &Path) {
202 OwningPtr<MemoryBuffer> Buff;
Alexey Samsonov933b8512013-02-05 07:01:34 +0000203 if (error_code ec = MemoryBuffer::getFile(Path, Buff))
204 error(ec);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000205 return ObjectFile::createObjectFile(Buff.take());
206}
207
208static std::string getDarwinDWARFResourceForModule(const std::string &Path) {
209 StringRef Basename = sys::path::filename(Path);
210 const std::string &DSymDirectory = Path + ".dSYM";
211 SmallString<16> ResourceName = StringRef(DSymDirectory);
212 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
213 sys::path::append(ResourceName, Basename);
214 return ResourceName.str();
215}
216
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000217ModuleInfo *
218LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000219 ModuleMapTy::iterator I = Modules.find(ModuleName);
220 if (I != Modules.end())
221 return I->second;
222
223 ObjectFile *Obj = getObjectFile(ModuleName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000224 if (Obj == 0) {
225 // Module name doesn't point to a valid object file.
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000226 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000227 return 0;
228 }
229
230 DIContext *Context = 0;
231 bool IsLittleEndian;
232 if (getObjectEndianness(Obj, IsLittleEndian)) {
233 // On Darwin we may find DWARF in separate object file in
234 // resource directory.
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000235 ObjectFile *DbgObj = Obj;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000236 if (isa<MachOObjectFile>(Obj)) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000237 const std::string &ResourceName =
238 getDarwinDWARFResourceForModule(ModuleName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000239 ObjectFile *ResourceObj = getObjectFile(ResourceName);
240 if (ResourceObj != 0)
241 DbgObj = ResourceObj;
242 }
243 Context = DIContext::getDWARFContext(DbgObj);
244 assert(Context);
245 }
246
247 ModuleInfo *Info = new ModuleInfo(Obj, Context);
248 Modules.insert(make_pair(ModuleName, Info));
249 return Info;
250}
251
252std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
253 // By default, DILineInfo contains "<invalid>" for function/filename it
254 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
255 static const std::string kDILineInfoBadString = "<invalid>";
256 std::stringstream Result;
257 if (Opts.PrintFunctions) {
258 std::string FunctionName = LineInfo.getFunctionName();
259 if (FunctionName == kDILineInfoBadString)
260 FunctionName = kBadString;
261 DemangleName(FunctionName);
262 Result << FunctionName << "\n";
263 }
264 std::string Filename = LineInfo.getFileName();
265 if (Filename == kDILineInfoBadString)
266 Filename = kBadString;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000267 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
268 << "\n";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000269 return Result.str();
270}
271
272#if !defined(_MSC_VER)
273// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
274extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
275 size_t *length, int *status);
276#endif
277
278void LLVMSymbolizer::DemangleName(std::string &Name) const {
279#if !defined(_MSC_VER)
280 if (!Opts.Demangle)
281 return;
282 int status = 0;
283 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
284 if (status != 0)
285 return;
286 Name = DemangledName;
287 free(DemangledName);
288#endif
289}
290
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000291} // namespace symbolize
292} // namespace llvm