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 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;
33 static 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());
40 DILineInfo ModuleInfo::symbolizeCode(uint64_t ModuleOffset,
41 const LLVMSymbolizer::Options& Opts) const {
43 if (DebugInfoContext) {
44 LineInfo = DebugInfoContext->getLineInfoForAddress(
45 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
47 // Override function name from symbol table if necessary.
48 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
49 std::string FunctionName;
51 if (getNameFromSymbolTable(SymbolRef::ST_Function,
52 ModuleOffset, FunctionName, Start, Size)) {
53 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
59 DIInliningInfo 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));
66 // Make sure there is at least one frame in context.
67 if (InlinedContext.getNumberOfFrames() == 0) {
68 InlinedContext.addFrame(DILineInfo());
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();
75 DILineInfo LineInfo = InlinedContext.getFrame(i);
77 std::string FunctionName;
79 if (getNameFromSymbolTable(SymbolRef::ST_Function,
80 ModuleOffset, FunctionName, Start, Size)) {
81 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
84 PatchedInlinedContext.addFrame(LineInfo);
86 InlinedContext = PatchedInlinedContext;
88 return InlinedContext;
91 bool 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);
97 static bool error(error_code ec) {
98 if (!ec) return false;
99 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
103 bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
104 std::string &Name, uint64_t &Addr,
105 uint64_t &Size) const {
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;
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;
135 const char LLVMSymbolizer::kBadString[] = "??";
137 std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
138 uint64_t ModuleOffset) {
139 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
141 return printDILineInfo(DILineInfo());
142 if (Opts.PrintInlining) {
143 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
145 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
146 assert(FramesNum > 0);
148 for (uint32_t i = 0; i < FramesNum; i++) {
149 DILineInfo LineInfo = InlinedContext.getFrame(i);
150 Result += printDILineInfo(LineInfo);
154 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
155 return printDILineInfo(LineInfo);
158 std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
159 uint64_t ModuleOffset) {
160 std::string Name = kBadString;
163 if (Opts.UseSymbolTable) {
164 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
165 if (Info->symbolizeData(ModuleOffset, Name, Start, Size))
169 std::stringstream ss;
170 ss << Name << "\n" << Start << " " << Size << "\n";
174 // Returns true if the object endianness is known.
175 static bool getObjectEndianness(const ObjectFile *Obj,
176 bool &IsLittleEndian) {
177 // FIXME: Implement this when libLLVMObject allows to do it easily.
178 IsLittleEndian = true;
182 static ObjectFile *getObjectFile(const std::string &Path) {
183 OwningPtr<MemoryBuffer> Buff;
184 if (error_code ec = MemoryBuffer::getFile(Path, Buff))
186 return ObjectFile::createObjectFile(Buff.take());
189 static 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();
198 ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo(
199 const std::string &ModuleName) {
200 ModuleMapTy::iterator I = Modules.find(ModuleName);
201 if (I != Modules.end())
204 ObjectFile *Obj = getObjectFile(ModuleName);
205 ObjectFile *DbgObj = Obj;
207 // Module name doesn't point to a valid object file.
208 Modules.insert(make_pair(ModuleName, (ModuleInfo*)0));
212 DIContext *Context = 0;
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(
220 ObjectFile *ResourceObj = getObjectFile(ResourceName);
221 if (ResourceObj != 0)
222 DbgObj = ResourceObj;
224 Context = DIContext::getDWARFContext(DbgObj);
228 ModuleInfo *Info = new ModuleInfo(Obj, Context);
229 Modules.insert(make_pair(ModuleName, Info));
233 std::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";
245 std::string Filename = LineInfo.getFileName();
246 if (Filename == kDILineInfoBadString)
247 Filename = kBadString;
248 Result << Filename << ":" << LineInfo.getLine()
249 << ":" << LineInfo.getColumn() << "\n";
253 #if !defined(_MSC_VER)
254 // Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
255 extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
256 size_t *length, int *status);
259 void LLVMSymbolizer::DemangleName(std::string &Name) const {
260 #if !defined(_MSC_VER)
264 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
267 Name = DemangledName;
272 } // namespace symbolize