blob: 74e9843c434304cf551f397d02b8e0e90294dc9c [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;
77 // FIXME: If a function has alias, there are two entries in symbol table
78 // with same address size. Make sure we choose the correct one.
Alexey Samsonovc4439c32013-02-15 08:54:47 +000079 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonovb6564642013-06-07 15:25:27 +000080 SymbolDesc SD = { SymbolAddress, SymbolSize };
Dmitry Vyukovb1819192013-02-14 13:06:18 +000081 M.insert(std::make_pair(SD, SymbolName));
82 }
83}
84
85bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
86 std::string &Name, uint64_t &Addr,
87 uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000088 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov888ca962013-06-04 07:57:38 +000089 if (M.empty())
Dmitry Vyukovb1819192013-02-14 13:06:18 +000090 return false;
Alexey Samsonov888ca962013-06-04 07:57:38 +000091 SymbolDesc SD = { Address, Address };
92 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonovb6564642013-06-07 15:25:27 +000093 if (it == M.begin())
94 return false;
Alexey Samsonov888ca962013-06-04 07:57:38 +000095 --it;
Alexey Samsonovb6564642013-06-07 15:25:27 +000096 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovb1819192013-02-14 13:06:18 +000097 return false;
98 Name = it->second.str();
99 Addr = it->first.Addr;
Alexey Samsonovb6564642013-06-07 15:25:27 +0000100 Size = it->first.Size;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000101 return true;
102}
103
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000104DILineInfo ModuleInfo::symbolizeCode(
105 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000106 DILineInfo LineInfo;
107 if (DebugInfoContext) {
108 LineInfo = DebugInfoContext->getLineInfoForAddress(
109 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
110 }
111 // Override function name from symbol table if necessary.
112 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
113 std::string FunctionName;
114 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000115 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
116 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000117 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
118 }
119 }
120 return LineInfo;
121}
122
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000123DIInliningInfo ModuleInfo::symbolizeInlinedCode(
124 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000125 DIInliningInfo InlinedContext;
126 if (DebugInfoContext) {
127 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
128 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
129 }
130 // Make sure there is at least one frame in context.
131 if (InlinedContext.getNumberOfFrames() == 0) {
132 InlinedContext.addFrame(DILineInfo());
133 }
134 // Override the function name in lower frame with name from symbol table.
135 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
136 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000137 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000138 DILineInfo LineInfo = InlinedContext.getFrame(i);
139 if (i == n - 1) {
140 std::string FunctionName;
141 uint64_t Start, Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000142 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
143 FunctionName, Start, Size)) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000144 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
145 }
146 }
147 PatchedInlinedContext.addFrame(LineInfo);
148 }
149 InlinedContext = PatchedInlinedContext;
150 }
151 return InlinedContext;
152}
153
154bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
155 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000156 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
157 Size);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000158}
159
Alexey Samsonov638c63c2013-02-04 15:55:26 +0000160const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000161
162std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
163 uint64_t ModuleOffset) {
164 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
165 if (Info == 0)
166 return printDILineInfo(DILineInfo());
167 if (Opts.PrintInlining) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000168 DIInliningInfo InlinedContext =
169 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000170 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
171 assert(FramesNum > 0);
172 std::string Result;
173 for (uint32_t i = 0; i < FramesNum; i++) {
174 DILineInfo LineInfo = InlinedContext.getFrame(i);
175 Result += printDILineInfo(LineInfo);
176 }
177 return Result;
178 }
179 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
180 return printDILineInfo(LineInfo);
181}
182
183std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
184 uint64_t ModuleOffset) {
185 std::string Name = kBadString;
186 uint64_t Start = 0;
187 uint64_t Size = 0;
188 if (Opts.UseSymbolTable) {
189 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
190 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
191 DemangleName(Name);
192 }
193 }
194 std::stringstream ss;
195 ss << Name << "\n" << Start << " " << Size << "\n";
196 return ss.str();
197}
198
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000199void LLVMSymbolizer::flush() {
Alexey Samsonov51283a12013-03-19 15:33:18 +0000200 DeleteContainerSeconds(Modules);
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000201 DeleteContainerPointers(ParsedBinariesAndObjects);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +0000202}
203
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000204static std::string getDarwinDWARFResourceForPath(const std::string &Path) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000205 StringRef Basename = sys::path::filename(Path);
206 const std::string &DSymDirectory = Path + ".dSYM";
207 SmallString<16> ResourceName = StringRef(DSymDirectory);
208 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
209 sys::path::append(ResourceName, Basename);
210 return ResourceName.str();
211}
212
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000213LLVMSymbolizer::BinaryPair
214LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
215 BinaryMapTy::iterator I = BinaryForPath.find(Path);
216 if (I != BinaryForPath.end())
217 return I->second;
218 Binary *Bin = 0;
219 Binary *DbgBin = 0;
220 OwningPtr<Binary> ParsedBinary;
221 OwningPtr<Binary> ParsedDbgBinary;
222 if (!error(createBinary(Path, ParsedBinary))) {
223 // Check if it's a universal binary.
224 Bin = ParsedBinary.take();
225 ParsedBinariesAndObjects.push_back(Bin);
226 if (Bin->isMachO() || Bin->isMachOUniversalBinary()) {
227 // On Darwin we may find DWARF in separate object file in
228 // resource directory.
229 const std::string &ResourcePath =
230 getDarwinDWARFResourceForPath(Path);
231 bool ResourceFileExists = false;
232 if (!sys::fs::exists(ResourcePath, ResourceFileExists) &&
233 ResourceFileExists &&
234 !error(createBinary(ResourcePath, ParsedDbgBinary))) {
235 DbgBin = ParsedDbgBinary.take();
236 ParsedBinariesAndObjects.push_back(DbgBin);
237 }
238 }
239 }
240 if (DbgBin == 0)
241 DbgBin = Bin;
242 BinaryPair Res = std::make_pair(Bin, DbgBin);
243 BinaryForPath[Path] = Res;
244 return Res;
245}
246
247ObjectFile *
248LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) {
249 if (Bin == 0)
250 return 0;
251 ObjectFile *Res = 0;
252 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
253 ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find(
254 std::make_pair(UB, ArchName));
255 if (I != ObjectFileForArch.end())
256 return I->second;
257 OwningPtr<ObjectFile> ParsedObj;
258 if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) {
259 Res = ParsedObj.take();
260 ParsedBinariesAndObjects.push_back(Res);
261 }
262 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
263 } else if (Bin->isObject()) {
264 Res = cast<ObjectFile>(Bin);
265 }
266 return Res;
267}
268
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000269ModuleInfo *
270LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000271 ModuleMapTy::iterator I = Modules.find(ModuleName);
272 if (I != Modules.end())
273 return I->second;
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000274 std::string BinaryName = ModuleName;
275 std::string ArchName = Opts.DefaultArch;
276 size_t ColonPos = ModuleName.find(':');
277 if (ColonPos != std::string::npos) {
278 BinaryName = ModuleName.substr(0, ColonPos);
279 ArchName = ModuleName.substr(ColonPos + 1);
280 }
281 BinaryPair Binaries = getOrCreateBinary(BinaryName);
282 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
283 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000284
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000285 if (Obj == 0) {
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000286 // Failed to find valid object file.
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000287 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000288 return 0;
289 }
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000290 DIContext *Context = DIContext::getDWARFContext(DbgObj);
291 assert(Context);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000292 ModuleInfo *Info = new ModuleInfo(Obj, Context);
293 Modules.insert(make_pair(ModuleName, Info));
294 return Info;
295}
296
297std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
298 // By default, DILineInfo contains "<invalid>" for function/filename it
299 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
300 static const std::string kDILineInfoBadString = "<invalid>";
301 std::stringstream Result;
302 if (Opts.PrintFunctions) {
303 std::string FunctionName = LineInfo.getFunctionName();
304 if (FunctionName == kDILineInfoBadString)
305 FunctionName = kBadString;
306 DemangleName(FunctionName);
307 Result << FunctionName << "\n";
308 }
309 std::string Filename = LineInfo.getFileName();
310 if (Filename == kDILineInfoBadString)
311 Filename = kBadString;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000312 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
313 << "\n";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000314 return Result.str();
315}
316
317#if !defined(_MSC_VER)
318// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
319extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
320 size_t *length, int *status);
321#endif
322
323void LLVMSymbolizer::DemangleName(std::string &Name) const {
324#if !defined(_MSC_VER)
325 if (!Opts.Demangle)
326 return;
327 int status = 0;
328 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
329 if (status != 0)
330 return;
331 Name = DemangledName;
332 free(DemangledName);
333#endif
334}
335
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000336} // namespace symbolize
337} // namespace llvm