blob: fe32178f02dbbb582737ee950fa67e0f320d9c91 [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"
15#include "llvm/Object/MachO.h"
16#include "llvm/Support/Casting.h"
17#include "llvm/Support/Path.h"
18
19#include <sstream>
20
21namespace llvm {
22namespace symbolize {
23
Dmitry Vyukovb1819192013-02-14 13:06:18 +000024static bool error(error_code ec) {
25 if (!ec) return false;
26 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
27 return true;
28}
29
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000030static uint32_t getDILineInfoSpecifierFlags(
31 const LLVMSymbolizer::Options &Opts) {
32 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
33 llvm::DILineInfoSpecifier::AbsoluteFilePath;
34 if (Opts.PrintFunctions)
35 Flags |= llvm::DILineInfoSpecifier::FunctionName;
36 return Flags;
37}
38
39static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
40 DILineInfo &LineInfo) {
41 std::string FileName = LineInfo.getFileName();
42 LineInfo = DILineInfo(StringRef(FileName), StringRef(NewFunctionName),
43 LineInfo.getLine(), LineInfo.getColumn());
44}
45
Dmitry Vyukovb1819192013-02-14 13:06:18 +000046ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
47 : Module(Obj)
48 , DebugInfoContext(DICtx) {
49 error_code ec;
50 for (symbol_iterator si = Module->begin_symbols(),
51 se = Module->end_symbols();
52 si != se; si.increment(ec)) {
53 if (error(ec))
54 return;
55 SymbolRef::Type SymbolType;
56 if (error(si->getType(SymbolType)))
57 continue;
58 if (SymbolType != SymbolRef::ST_Function
59 && SymbolType != SymbolRef::ST_Data)
60 continue;
61 uint64_t SymbolAddress;
62 if (error(si->getAddress(SymbolAddress))
63 || SymbolAddress == UnknownAddressOrSize)
64 continue;
65 uint64_t SymbolSize;
66 if (error(si->getSize(SymbolSize))
67 || SymbolSize == UnknownAddressOrSize)
68 continue;
69 StringRef SymbolName;
70 if (error(si->getName(SymbolName)))
71 continue;
72 // FIXME: If a function has alias, there are two entries in symbol table
73 // with same address size. Make sure we choose the correct one.
74 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ?
75 Functions : Objects;
76 SymbolDesc SD = {SymbolAddress, SymbolAddress + SymbolSize};
77 M.insert(std::make_pair(SD, SymbolName));
78 }
79}
80
81bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
82 std::string &Name, uint64_t &Addr,
83 uint64_t &Size) const {
84 const SymbolMapTy& M = Type == SymbolRef::ST_Function ?
85 Functions : Objects;
86 SymbolDesc SD = {Address, Address + 1};
87 SymbolMapTy::const_iterator it = M.find(SD);
88 if (it == M.end())
89 return false;
90 if (Address < it->first.Addr || Address >= it->first.AddrEnd)
91 return false;
92 Name = it->second.str();
93 Addr = it->first.Addr;
94 Size = it->first.AddrEnd - it->first.Addr;
95 return true;
96}
97
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000098DILineInfo ModuleInfo::symbolizeCode(uint64_t ModuleOffset,
99 const LLVMSymbolizer::Options& Opts) const {
100 DILineInfo LineInfo;
101 if (DebugInfoContext) {
102 LineInfo = DebugInfoContext->getLineInfoForAddress(
103 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
104 }
105 // Override function name from symbol table if necessary.
106 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
107 std::string FunctionName;
108 uint64_t Start, Size;
109 if (getNameFromSymbolTable(SymbolRef::ST_Function,
110 ModuleOffset, FunctionName, Start, Size)) {
111 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
112 }
113 }
114 return LineInfo;
115}
116
117DIInliningInfo ModuleInfo::symbolizeInlinedCode(uint64_t ModuleOffset,
118 const LLVMSymbolizer::Options& Opts) const {
119 DIInliningInfo InlinedContext;
120 if (DebugInfoContext) {
121 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
122 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
123 }
124 // Make sure there is at least one frame in context.
125 if (InlinedContext.getNumberOfFrames() == 0) {
126 InlinedContext.addFrame(DILineInfo());
127 }
128 // Override the function name in lower frame with name from symbol table.
129 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
130 DIInliningInfo PatchedInlinedContext;
131 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames();
132 i < n; i++) {
133 DILineInfo LineInfo = InlinedContext.getFrame(i);
134 if (i == n - 1) {
135 std::string FunctionName;
136 uint64_t Start, Size;
137 if (getNameFromSymbolTable(SymbolRef::ST_Function,
138 ModuleOffset, FunctionName, Start, Size)) {
139 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
140 }
141 }
142 PatchedInlinedContext.addFrame(LineInfo);
143 }
144 InlinedContext = PatchedInlinedContext;
145 }
146 return InlinedContext;
147}
148
149bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
150 uint64_t &Start, uint64_t &Size) const {
151 return getNameFromSymbolTable(SymbolRef::ST_Data,
152 ModuleOffset, Name, Start, Size);
153}
154
Alexey Samsonov638c63c2013-02-04 15:55:26 +0000155const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000156
157std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
158 uint64_t ModuleOffset) {
159 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
160 if (Info == 0)
161 return printDILineInfo(DILineInfo());
162 if (Opts.PrintInlining) {
163 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
164 ModuleOffset, Opts);
165 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
166 assert(FramesNum > 0);
167 std::string Result;
168 for (uint32_t i = 0; i < FramesNum; i++) {
169 DILineInfo LineInfo = InlinedContext.getFrame(i);
170 Result += printDILineInfo(LineInfo);
171 }
172 return Result;
173 }
174 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
175 return printDILineInfo(LineInfo);
176}
177
178std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
179 uint64_t ModuleOffset) {
180 std::string Name = kBadString;
181 uint64_t Start = 0;
182 uint64_t Size = 0;
183 if (Opts.UseSymbolTable) {
184 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
185 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
186 DemangleName(Name);
187 }
188 }
189 std::stringstream ss;
190 ss << Name << "\n" << Start << " " << Size << "\n";
191 return ss.str();
192}
193
194// Returns true if the object endianness is known.
195static bool getObjectEndianness(const ObjectFile *Obj,
196 bool &IsLittleEndian) {
197 // FIXME: Implement this when libLLVMObject allows to do it easily.
198 IsLittleEndian = true;
199 return true;
200}
201
202static ObjectFile *getObjectFile(const std::string &Path) {
203 OwningPtr<MemoryBuffer> Buff;
Alexey Samsonov933b8512013-02-05 07:01:34 +0000204 if (error_code ec = MemoryBuffer::getFile(Path, Buff))
205 error(ec);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000206 return ObjectFile::createObjectFile(Buff.take());
207}
208
209static std::string getDarwinDWARFResourceForModule(const std::string &Path) {
210 StringRef Basename = sys::path::filename(Path);
211 const std::string &DSymDirectory = Path + ".dSYM";
212 SmallString<16> ResourceName = StringRef(DSymDirectory);
213 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
214 sys::path::append(ResourceName, Basename);
215 return ResourceName.str();
216}
217
218ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo(
219 const std::string &ModuleName) {
220 ModuleMapTy::iterator I = Modules.find(ModuleName);
221 if (I != Modules.end())
222 return I->second;
223
224 ObjectFile *Obj = getObjectFile(ModuleName);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000225 if (Obj == 0) {
226 // Module name doesn't point to a valid object file.
227 Modules.insert(make_pair(ModuleName, (ModuleInfo*)0));
228 return 0;
229 }
230
231 DIContext *Context = 0;
232 bool IsLittleEndian;
233 if (getObjectEndianness(Obj, IsLittleEndian)) {
234 // On Darwin we may find DWARF in separate object file in
235 // resource directory.
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000236 ObjectFile *DbgObj = Obj;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000237 if (isa<MachOObjectFile>(Obj)) {
238 const std::string &ResourceName = getDarwinDWARFResourceForModule(
239 ModuleName);
240 ObjectFile *ResourceObj = getObjectFile(ResourceName);
241 if (ResourceObj != 0)
242 DbgObj = ResourceObj;
243 }
244 Context = DIContext::getDWARFContext(DbgObj);
245 assert(Context);
246 }
247
248 ModuleInfo *Info = new ModuleInfo(Obj, Context);
249 Modules.insert(make_pair(ModuleName, Info));
250 return Info;
251}
252
253std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
254 // By default, DILineInfo contains "<invalid>" for function/filename it
255 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
256 static const std::string kDILineInfoBadString = "<invalid>";
257 std::stringstream Result;
258 if (Opts.PrintFunctions) {
259 std::string FunctionName = LineInfo.getFunctionName();
260 if (FunctionName == kDILineInfoBadString)
261 FunctionName = kBadString;
262 DemangleName(FunctionName);
263 Result << FunctionName << "\n";
264 }
265 std::string Filename = LineInfo.getFileName();
266 if (Filename == kDILineInfoBadString)
267 Filename = kBadString;
268 Result << Filename << ":" << LineInfo.getLine()
269 << ":" << LineInfo.getColumn() << "\n";
270 return Result.str();
271}
272
273#if !defined(_MSC_VER)
274// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
275extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
276 size_t *length, int *status);
277#endif
278
279void LLVMSymbolizer::DemangleName(std::string &Name) const {
280#if !defined(_MSC_VER)
281 if (!Opts.Demangle)
282 return;
283 int status = 0;
284 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
285 if (status != 0)
286 return;
287 Name = DemangledName;
288 free(DemangledName);
289#endif
290}
291
292} // namespace symbolize
293} // namespace llvm