[llvm-symbolizer] rewrite r183213 in a more clear way
[oota-llvm.git] / tools / llvm-symbolizer / LLVMSymbolize.cpp
1 //===-- 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/ADT/STLExtras.h"
16 #include "llvm/Object/MachO.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20
21 #include <sstream>
22
23 namespace llvm {
24 namespace symbolize {
25
26 static bool error(error_code ec) {
27   if (!ec)
28     return false;
29   errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
30   return true;
31 }
32
33 static uint32_t
34 getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
35   uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
36                    llvm::DILineInfoSpecifier::AbsoluteFilePath;
37   if (Opts.PrintFunctions)
38     Flags |= llvm::DILineInfoSpecifier::FunctionName;
39   return Flags;
40 }
41
42 static 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
49 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
50     : Module(Obj), DebugInfoContext(DICtx) {
51   error_code ec;
52   for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
53        si != se; si.increment(ec)) {
54     if (error(ec))
55       return;
56     SymbolRef::Type SymbolType;
57     if (error(si->getType(SymbolType)))
58       continue;
59     if (SymbolType != SymbolRef::ST_Function &&
60         SymbolType != SymbolRef::ST_Data)
61       continue;
62     uint64_t SymbolAddress;
63     if (error(si->getAddress(SymbolAddress)) ||
64         SymbolAddress == UnknownAddressOrSize)
65       continue;
66     uint64_t SymbolSize;
67     // Getting symbol size is linear for Mach-O files, so assume that symbol
68     // occupies the memory range up to the following symbol.
69     if (isa<MachOObjectFile>(Obj))
70       SymbolSize = 0;
71     else if (error(si->getSize(SymbolSize)) ||
72              SymbolSize == UnknownAddressOrSize)
73       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.
79     SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
80     SymbolDesc SD = { SymbolAddress, SymbolSize };
81     M.insert(std::make_pair(SD, SymbolName));
82   }
83 }
84
85 bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
86                                         std::string &Name, uint64_t &Addr,
87                                         uint64_t &Size) const {
88   const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
89   if (M.empty())
90     return false;
91   SymbolDesc SD = { Address, Address };
92   SymbolMapTy::const_iterator it = M.upper_bound(SD);
93   if (it == M.begin())
94     return false;
95   --it;
96   if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
97     return false;
98   Name = it->second.str();
99   Addr = it->first.Addr;
100   Size = it->first.Size;
101   return true;
102 }
103
104 DILineInfo ModuleInfo::symbolizeCode(
105     uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
106   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;
115     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
116                                FunctionName, Start, Size)) {
117       patchFunctionNameInDILineInfo(FunctionName, LineInfo);
118     }
119   }
120   return LineInfo;
121 }
122
123 DIInliningInfo ModuleInfo::symbolizeInlinedCode(
124     uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
125   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;
137     for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
138       DILineInfo LineInfo = InlinedContext.getFrame(i);
139       if (i == n - 1) {
140         std::string FunctionName;
141         uint64_t Start, Size;
142         if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
143                                    FunctionName, Start, Size)) {
144           patchFunctionNameInDILineInfo(FunctionName, LineInfo);
145         }
146       }
147       PatchedInlinedContext.addFrame(LineInfo);
148     }
149     InlinedContext = PatchedInlinedContext;
150   }
151   return InlinedContext;
152 }
153
154 bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
155                                uint64_t &Start, uint64_t &Size) const {
156   return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
157                                 Size);
158 }
159
160 const char LLVMSymbolizer::kBadString[] = "??";
161
162 std::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) {
168     DIInliningInfo InlinedContext =
169         Info->symbolizeInlinedCode(ModuleOffset, Opts);
170     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
183 std::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
199 void LLVMSymbolizer::flush() {
200   DeleteContainerSeconds(Modules);
201 }
202
203 // Returns true if the object endianness is known.
204 static bool getObjectEndianness(const ObjectFile *Obj, bool &IsLittleEndian) {
205   // FIXME: Implement this when libLLVMObject allows to do it easily.
206   IsLittleEndian = true;
207   return true;
208 }
209
210 static ObjectFile *getObjectFile(const std::string &Path) {
211   OwningPtr<MemoryBuffer> Buff;
212   if (error(MemoryBuffer::getFile(Path, Buff)))
213     return 0;
214   return ObjectFile::createObjectFile(Buff.take());
215 }
216
217 static std::string getDarwinDWARFResourceForModule(const std::string &Path) {
218   StringRef Basename = sys::path::filename(Path);
219   const std::string &DSymDirectory = Path + ".dSYM";
220   SmallString<16> ResourceName = StringRef(DSymDirectory);
221   sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
222   sys::path::append(ResourceName, Basename);
223   return ResourceName.str();
224 }
225
226 ModuleInfo *
227 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
228   ModuleMapTy::iterator I = Modules.find(ModuleName);
229   if (I != Modules.end())
230     return I->second;
231
232   ObjectFile *Obj = getObjectFile(ModuleName);
233   if (Obj == 0) {
234     // Module name doesn't point to a valid object file.
235     Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
236     return 0;
237   }
238
239   DIContext *Context = 0;
240   bool IsLittleEndian;
241   if (getObjectEndianness(Obj, IsLittleEndian)) {
242     // On Darwin we may find DWARF in separate object file in
243     // resource directory.
244     ObjectFile *DbgObj = Obj;
245     if (isa<MachOObjectFile>(Obj)) {
246       const std::string &ResourceName =
247           getDarwinDWARFResourceForModule(ModuleName);
248       bool ResourceFileExists = false;
249       if (!sys::fs::exists(ResourceName, ResourceFileExists) &&
250           ResourceFileExists) {
251         if (ObjectFile *ResourceObj = getObjectFile(ResourceName))
252           DbgObj = ResourceObj;
253       }
254     }
255     Context = DIContext::getDWARFContext(DbgObj);
256     assert(Context);
257   }
258
259   ModuleInfo *Info = new ModuleInfo(Obj, Context);
260   Modules.insert(make_pair(ModuleName, Info));
261   return Info;
262 }
263
264 std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
265   // By default, DILineInfo contains "<invalid>" for function/filename it
266   // cannot fetch. We replace it to "??" to make our output closer to addr2line.
267   static const std::string kDILineInfoBadString = "<invalid>";
268   std::stringstream Result;
269   if (Opts.PrintFunctions) {
270     std::string FunctionName = LineInfo.getFunctionName();
271     if (FunctionName == kDILineInfoBadString)
272       FunctionName = kBadString;
273     DemangleName(FunctionName);
274     Result << FunctionName << "\n";
275   }
276   std::string Filename = LineInfo.getFileName();
277   if (Filename == kDILineInfoBadString)
278     Filename = kBadString;
279   Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
280          << "\n";
281   return Result.str();
282 }
283
284 #if !defined(_MSC_VER)
285 // Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
286 extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
287                                 size_t *length, int *status);
288 #endif
289
290 void LLVMSymbolizer::DemangleName(std::string &Name) const {
291 #if !defined(_MSC_VER)
292   if (!Opts.Demangle)
293     return;
294   int status = 0;
295   char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
296   if (status != 0)
297     return;
298   Name = DemangledName;
299   free(DemangledName);
300 #endif
301 }
302
303 } // namespace symbolize
304 } // namespace llvm