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/Object/MachO.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Path.h"
24 static bool error(error_code ec) {
25 if (!ec) return false;
26 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
30 static 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;
39 static 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());
46 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
48 , DebugInfoContext(DICtx) {
50 for (symbol_iterator si = Module->begin_symbols(),
51 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))
67 || SymbolSize == UnknownAddressOrSize)
70 if (error(si->getName(SymbolName)))
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 ?
76 SymbolDesc SD = {SymbolAddress, SymbolAddress + SymbolSize};
77 M.insert(std::make_pair(SD, SymbolName));
81 bool 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 ?
86 SymbolDesc SD = {Address, Address + 1};
87 SymbolMapTy::const_iterator it = M.find(SD);
90 if (Address < it->first.Addr || Address >= it->first.AddrEnd)
92 Name = it->second.str();
93 Addr = it->first.Addr;
94 Size = it->first.AddrEnd - it->first.Addr;
98 DILineInfo ModuleInfo::symbolizeCode(uint64_t ModuleOffset,
99 const LLVMSymbolizer::Options& Opts) const {
101 if (DebugInfoContext) {
102 LineInfo = DebugInfoContext->getLineInfoForAddress(
103 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
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);
117 DIInliningInfo 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));
124 // Make sure there is at least one frame in context.
125 if (InlinedContext.getNumberOfFrames() == 0) {
126 InlinedContext.addFrame(DILineInfo());
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();
133 DILineInfo LineInfo = InlinedContext.getFrame(i);
135 std::string FunctionName;
136 uint64_t Start, Size;
137 if (getNameFromSymbolTable(SymbolRef::ST_Function,
138 ModuleOffset, FunctionName, Start, Size)) {
139 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
142 PatchedInlinedContext.addFrame(LineInfo);
144 InlinedContext = PatchedInlinedContext;
146 return InlinedContext;
149 bool 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);
155 const char LLVMSymbolizer::kBadString[] = "??";
157 std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
158 uint64_t ModuleOffset) {
159 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
161 return printDILineInfo(DILineInfo());
162 if (Opts.PrintInlining) {
163 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
165 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
166 assert(FramesNum > 0);
168 for (uint32_t i = 0; i < FramesNum; i++) {
169 DILineInfo LineInfo = InlinedContext.getFrame(i);
170 Result += printDILineInfo(LineInfo);
174 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
175 return printDILineInfo(LineInfo);
178 std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
179 uint64_t ModuleOffset) {
180 std::string Name = kBadString;
183 if (Opts.UseSymbolTable) {
184 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
185 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
189 std::stringstream ss;
190 ss << Name << "\n" << Start << " " << Size << "\n";
194 // Returns true if the object endianness is known.
195 static bool getObjectEndianness(const ObjectFile *Obj,
196 bool &IsLittleEndian) {
197 // FIXME: Implement this when libLLVMObject allows to do it easily.
198 IsLittleEndian = true;
202 static ObjectFile *getObjectFile(const std::string &Path) {
203 OwningPtr<MemoryBuffer> Buff;
204 if (error_code ec = MemoryBuffer::getFile(Path, Buff))
206 return ObjectFile::createObjectFile(Buff.take());
209 static 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();
218 ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo(
219 const std::string &ModuleName) {
220 ModuleMapTy::iterator I = Modules.find(ModuleName);
221 if (I != Modules.end())
224 ObjectFile *Obj = getObjectFile(ModuleName);
226 // Module name doesn't point to a valid object file.
227 Modules.insert(make_pair(ModuleName, (ModuleInfo*)0));
231 DIContext *Context = 0;
233 if (getObjectEndianness(Obj, IsLittleEndian)) {
234 // On Darwin we may find DWARF in separate object file in
235 // resource directory.
236 ObjectFile *DbgObj = Obj;
237 if (isa<MachOObjectFile>(Obj)) {
238 const std::string &ResourceName = getDarwinDWARFResourceForModule(
240 ObjectFile *ResourceObj = getObjectFile(ResourceName);
241 if (ResourceObj != 0)
242 DbgObj = ResourceObj;
244 Context = DIContext::getDWARFContext(DbgObj);
248 ModuleInfo *Info = new ModuleInfo(Obj, Context);
249 Modules.insert(make_pair(ModuleName, Info));
253 std::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";
265 std::string Filename = LineInfo.getFileName();
266 if (Filename == kDILineInfoBadString)
267 Filename = kBadString;
268 Result << Filename << ":" << LineInfo.getLine()
269 << ":" << LineInfo.getColumn() << "\n";
273 #if !defined(_MSC_VER)
274 // Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
275 extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
276 size_t *length, int *status);
279 void LLVMSymbolizer::DemangleName(std::string &Name) const {
280 #if !defined(_MSC_VER)
284 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
287 Name = DemangledName;
292 } // namespace symbolize