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