1 //===-- LLVMSymbolize.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation for LLVM symbolization library.
12 //===----------------------------------------------------------------------===//
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/Path.h"
25 static bool error(error_code ec) {
28 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
33 getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
34 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
35 llvm::DILineInfoSpecifier::AbsoluteFilePath;
36 if (Opts.PrintFunctions)
37 Flags |= llvm::DILineInfoSpecifier::FunctionName;
41 static 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());
48 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
49 : Module(Obj), DebugInfoContext(DICtx) {
51 for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
52 si != se; si.increment(ec)) {
55 SymbolRef::Type SymbolType;
56 if (error(si->getType(SymbolType)))
58 if (SymbolType != SymbolRef::ST_Function &&
59 SymbolType != SymbolRef::ST_Data)
61 uint64_t SymbolAddress;
62 if (error(si->getAddress(SymbolAddress)) ||
63 SymbolAddress == UnknownAddressOrSize)
66 if (error(si->getSize(SymbolSize)) || SymbolSize == UnknownAddressOrSize)
69 if (error(si->getName(SymbolName)))
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.
73 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
74 SymbolDesc SD = { SymbolAddress, SymbolAddress + SymbolSize };
75 M.insert(std::make_pair(SD, SymbolName));
79 bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
80 std::string &Name, uint64_t &Addr,
81 uint64_t &Size) const {
82 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
83 SymbolDesc SD = { Address, Address + 1 };
84 SymbolMapTy::const_iterator it = M.find(SD);
87 if (Address < it->first.Addr || Address >= it->first.AddrEnd)
89 Name = it->second.str();
90 Addr = it->first.Addr;
91 Size = it->first.AddrEnd - it->first.Addr;
95 DILineInfo ModuleInfo::symbolizeCode(
96 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
98 if (DebugInfoContext) {
99 LineInfo = DebugInfoContext->getLineInfoForAddress(
100 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
102 // Override function name from symbol table if necessary.
103 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
104 std::string FunctionName;
105 uint64_t Start, Size;
106 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
107 FunctionName, Start, Size)) {
108 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
114 DIInliningInfo ModuleInfo::symbolizeInlinedCode(
115 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
116 DIInliningInfo InlinedContext;
117 if (DebugInfoContext) {
118 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
119 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
121 // Make sure there is at least one frame in context.
122 if (InlinedContext.getNumberOfFrames() == 0) {
123 InlinedContext.addFrame(DILineInfo());
125 // Override the function name in lower frame with name from symbol table.
126 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
127 DIInliningInfo PatchedInlinedContext;
128 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
129 DILineInfo LineInfo = InlinedContext.getFrame(i);
131 std::string FunctionName;
132 uint64_t Start, Size;
133 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
134 FunctionName, Start, Size)) {
135 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
138 PatchedInlinedContext.addFrame(LineInfo);
140 InlinedContext = PatchedInlinedContext;
142 return InlinedContext;
145 bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
146 uint64_t &Start, uint64_t &Size) const {
147 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
151 const char LLVMSymbolizer::kBadString[] = "??";
153 std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
154 uint64_t ModuleOffset) {
155 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
157 return printDILineInfo(DILineInfo());
158 if (Opts.PrintInlining) {
159 DIInliningInfo InlinedContext =
160 Info->symbolizeInlinedCode(ModuleOffset, Opts);
161 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
162 assert(FramesNum > 0);
164 for (uint32_t i = 0; i < FramesNum; i++) {
165 DILineInfo LineInfo = InlinedContext.getFrame(i);
166 Result += printDILineInfo(LineInfo);
170 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
171 return printDILineInfo(LineInfo);
174 std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
175 uint64_t ModuleOffset) {
176 std::string Name = kBadString;
179 if (Opts.UseSymbolTable) {
180 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
181 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
185 std::stringstream ss;
186 ss << Name << "\n" << Start << " " << Size << "\n";
190 void LLVMSymbolizer::flush() {
191 DeleteContainerSeconds(Modules);
194 // Returns true if the object endianness is known.
195 static bool getObjectEndianness(const ObjectFile *Obj, bool &IsLittleEndian) {
196 // FIXME: Implement this when libLLVMObject allows to do it easily.
197 IsLittleEndian = true;
201 static ObjectFile *getObjectFile(const std::string &Path) {
202 OwningPtr<MemoryBuffer> Buff;
203 if (error_code ec = MemoryBuffer::getFile(Path, Buff))
205 return ObjectFile::createObjectFile(Buff.take());
208 static 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();
218 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
219 ModuleMapTy::iterator I = Modules.find(ModuleName);
220 if (I != Modules.end())
223 ObjectFile *Obj = getObjectFile(ModuleName);
225 // Module name doesn't point to a valid object file.
226 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
230 DIContext *Context = 0;
232 if (getObjectEndianness(Obj, IsLittleEndian)) {
233 // On Darwin we may find DWARF in separate object file in
234 // resource directory.
235 ObjectFile *DbgObj = Obj;
236 if (isa<MachOObjectFile>(Obj)) {
237 const std::string &ResourceName =
238 getDarwinDWARFResourceForModule(ModuleName);
239 ObjectFile *ResourceObj = getObjectFile(ResourceName);
240 if (ResourceObj != 0)
241 DbgObj = ResourceObj;
243 Context = DIContext::getDWARFContext(DbgObj);
247 ModuleInfo *Info = new ModuleInfo(Obj, Context);
248 Modules.insert(make_pair(ModuleName, Info));
252 std::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";
264 std::string Filename = LineInfo.getFileName();
265 if (Filename == kDILineInfoBadString)
266 Filename = kBadString;
267 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
272 #if !defined(_MSC_VER)
273 // Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
274 extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
275 size_t *length, int *status);
278 void LLVMSymbolizer::DemangleName(std::string &Name) const {
279 #if !defined(_MSC_VER)
283 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
286 Name = DemangledName;
291 } // namespace symbolize