Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 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" |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ELFObjectFile.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachO.h" |
| 19 | #include "llvm/Support/Casting.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compression.h" |
| 21 | #include "llvm/Support/DataExtractor.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Errc.h" |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 26 | #include <sstream> |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 28 | |
| 29 | namespace llvm { |
| 30 | namespace symbolize { |
| 31 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 32 | static bool error(std::error_code ec) { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 33 | if (!ec) |
| 34 | return false; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 35 | errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n"; |
| 36 | return true; |
| 37 | } |
| 38 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 39 | static DILineInfoSpecifier |
| 40 | getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) { |
| 41 | return DILineInfoSpecifier( |
| 42 | DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 43 | Opts.PrintFunctions); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 46 | ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx) |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 47 | : Module(Obj), DebugInfoContext(DICtx) { |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 48 | for (const SymbolRef &Symbol : Module->symbols()) { |
| 49 | addSymbol(Symbol); |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 50 | } |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 51 | bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end()); |
| 52 | if (NoSymbolTable && Module->isELF()) { |
| 53 | // Fallback to dynamic symbol table, if regular symbol table is stripped. |
| 54 | std::pair<symbol_iterator, symbol_iterator> IDyn = |
| 55 | getELFDynamicSymbolIterators(Module); |
| 56 | for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) { |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 57 | addSymbol(*si); |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 62 | void ModuleInfo::addSymbol(const SymbolRef &Symbol) { |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 63 | SymbolRef::Type SymbolType; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 64 | if (error(Symbol.getType(SymbolType))) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 65 | return; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 66 | if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 67 | return; |
| 68 | uint64_t SymbolAddress; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 69 | if (error(Symbol.getAddress(SymbolAddress)) || |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 70 | SymbolAddress == UnknownAddressOrSize) |
| 71 | return; |
| 72 | uint64_t SymbolSize; |
| 73 | // Getting symbol size is linear for Mach-O files, so assume that symbol |
| 74 | // occupies the memory range up to the following symbol. |
| 75 | if (isa<MachOObjectFile>(Module)) |
| 76 | SymbolSize = 0; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 77 | else if (error(Symbol.getSize(SymbolSize)) || |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 78 | SymbolSize == UnknownAddressOrSize) |
| 79 | return; |
| 80 | StringRef SymbolName; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 81 | if (error(Symbol.getName(SymbolName))) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 82 | return; |
| 83 | // Mach-O symbol table names have leading underscore, skip it. |
| 84 | if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_') |
| 85 | SymbolName = SymbolName.drop_front(); |
| 86 | // FIXME: If a function has alias, there are two entries in symbol table |
| 87 | // with same address size. Make sure we choose the correct one. |
| 88 | SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects; |
| 89 | SymbolDesc SD = { SymbolAddress, SymbolSize }; |
| 90 | M.insert(std::make_pair(SD, SymbolName)); |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, |
| 94 | std::string &Name, uint64_t &Addr, |
| 95 | uint64_t &Size) const { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 96 | const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 97 | if (M.empty()) |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 98 | return false; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 99 | SymbolDesc SD = { Address, Address }; |
| 100 | SymbolMapTy::const_iterator it = M.upper_bound(SD); |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 101 | if (it == M.begin()) |
| 102 | return false; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 103 | --it; |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 104 | if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address) |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 105 | return false; |
| 106 | Name = it->second.str(); |
| 107 | Addr = it->first.Addr; |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 108 | Size = it->first.Size; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 112 | DILineInfo ModuleInfo::symbolizeCode( |
| 113 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 114 | DILineInfo LineInfo; |
| 115 | if (DebugInfoContext) { |
| 116 | LineInfo = DebugInfoContext->getLineInfoForAddress( |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 117 | ModuleOffset, getDILineInfoSpecifier(Opts)); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 118 | } |
| 119 | // Override function name from symbol table if necessary. |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 120 | if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 121 | std::string FunctionName; |
| 122 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 123 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 124 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 125 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | return LineInfo; |
| 129 | } |
| 130 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 131 | DIInliningInfo ModuleInfo::symbolizeInlinedCode( |
| 132 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 133 | DIInliningInfo InlinedContext; |
| 134 | if (DebugInfoContext) { |
| 135 | InlinedContext = DebugInfoContext->getInliningInfoForAddress( |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 136 | ModuleOffset, getDILineInfoSpecifier(Opts)); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 137 | } |
| 138 | // Make sure there is at least one frame in context. |
| 139 | if (InlinedContext.getNumberOfFrames() == 0) { |
| 140 | InlinedContext.addFrame(DILineInfo()); |
| 141 | } |
| 142 | // Override the function name in lower frame with name from symbol table. |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 143 | if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 144 | DIInliningInfo PatchedInlinedContext; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 145 | for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 146 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 147 | if (i == n - 1) { |
| 148 | std::string FunctionName; |
| 149 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 150 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 151 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 152 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | PatchedInlinedContext.addFrame(LineInfo); |
| 156 | } |
| 157 | InlinedContext = PatchedInlinedContext; |
| 158 | } |
| 159 | return InlinedContext; |
| 160 | } |
| 161 | |
| 162 | bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name, |
| 163 | uint64_t &Start, uint64_t &Size) const { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 164 | return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start, |
| 165 | Size); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alexey Samsonov | d6cef10 | 2013-02-04 15:55:26 +0000 | [diff] [blame] | 168 | const char LLVMSymbolizer::kBadString[] = "??"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 169 | |
| 170 | std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 171 | uint64_t ModuleOffset) { |
| 172 | ModuleInfo *Info = getOrCreateModuleInfo(ModuleName); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 173 | if (!Info) |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 174 | return printDILineInfo(DILineInfo()); |
| 175 | if (Opts.PrintInlining) { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 176 | DIInliningInfo InlinedContext = |
| 177 | Info->symbolizeInlinedCode(ModuleOffset, Opts); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 178 | uint32_t FramesNum = InlinedContext.getNumberOfFrames(); |
| 179 | assert(FramesNum > 0); |
| 180 | std::string Result; |
| 181 | for (uint32_t i = 0; i < FramesNum; i++) { |
| 182 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 183 | Result += printDILineInfo(LineInfo); |
| 184 | } |
| 185 | return Result; |
| 186 | } |
| 187 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts); |
| 188 | return printDILineInfo(LineInfo); |
| 189 | } |
| 190 | |
| 191 | std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 192 | uint64_t ModuleOffset) { |
| 193 | std::string Name = kBadString; |
| 194 | uint64_t Start = 0; |
| 195 | uint64_t Size = 0; |
| 196 | if (Opts.UseSymbolTable) { |
| 197 | if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) { |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 198 | if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 199 | Name = DemangleName(Name); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | std::stringstream ss; |
| 203 | ss << Name << "\n" << Start << " " << Size << "\n"; |
| 204 | return ss.str(); |
| 205 | } |
| 206 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 207 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 208 | DeleteContainerSeconds(Modules); |
Alexey Samsonov | fe3a5d9 | 2013-06-28 15:08:29 +0000 | [diff] [blame] | 209 | BinaryForPath.clear(); |
| 210 | ObjectFileForArch.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 213 | static std::string getDarwinDWARFResourceForPath(const std::string &Path) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 214 | StringRef Basename = sys::path::filename(Path); |
| 215 | const std::string &DSymDirectory = Path + ".dSYM"; |
| 216 | SmallString<16> ResourceName = StringRef(DSymDirectory); |
| 217 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 218 | sys::path::append(ResourceName, Basename); |
| 219 | return ResourceName.str(); |
| 220 | } |
| 221 | |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 222 | static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 223 | ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
| 224 | MemoryBuffer::getFileOrSTDIN(Path); |
| 225 | if (!MB) |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 226 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 227 | return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static bool findDebugBinary(const std::string &OrigPath, |
| 231 | const std::string &DebuglinkName, uint32_t CRCHash, |
| 232 | std::string &Result) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 233 | std::string OrigRealPath = OrigPath; |
| 234 | #if defined(HAVE_REALPATH) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 235 | if (char *RP = realpath(OrigPath.c_str(), nullptr)) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 236 | OrigRealPath = RP; |
| 237 | free(RP); |
| 238 | } |
| 239 | #endif |
| 240 | SmallString<16> OrigDir(OrigRealPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 241 | llvm::sys::path::remove_filename(OrigDir); |
| 242 | SmallString<16> DebugPath = OrigDir; |
| 243 | // Try /path/to/original_binary/debuglink_name |
| 244 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 245 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 246 | Result = DebugPath.str(); |
| 247 | return true; |
| 248 | } |
| 249 | // Try /path/to/original_binary/.debug/debuglink_name |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 250 | DebugPath = OrigRealPath; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 251 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 252 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 253 | Result = DebugPath.str(); |
| 254 | return true; |
| 255 | } |
| 256 | // Try /usr/lib/debug/path/to/original_binary/debuglink_name |
| 257 | DebugPath = "/usr/lib/debug"; |
| 258 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 259 | DebuglinkName); |
| 260 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 261 | Result = DebugPath.str(); |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | static bool getGNUDebuglinkContents(const Binary *Bin, std::string &DebugName, |
| 268 | uint32_t &CRCHash) { |
| 269 | const ObjectFile *Obj = dyn_cast<ObjectFile>(Bin); |
| 270 | if (!Obj) |
| 271 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 272 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 273 | StringRef Name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 274 | Section.getName(Name); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 275 | Name = Name.substr(Name.find_first_not_of("._")); |
| 276 | if (Name == "gnu_debuglink") { |
| 277 | StringRef Data; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 278 | Section.getContents(Data); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 279 | DataExtractor DE(Data, Obj->isLittleEndian(), 0); |
| 280 | uint32_t Offset = 0; |
| 281 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 282 | // 4-byte align the offset. |
| 283 | Offset = (Offset + 3) & ~0x3; |
| 284 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 285 | DebugName = DebugNameStr; |
| 286 | CRCHash = DE.getU32(&Offset); |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 296 | LLVMSymbolizer::BinaryPair |
| 297 | LLVMSymbolizer::getOrCreateBinary(const std::string &Path) { |
| 298 | BinaryMapTy::iterator I = BinaryForPath.find(Path); |
| 299 | if (I != BinaryForPath.end()) |
| 300 | return I->second; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 301 | Binary *Bin = nullptr; |
| 302 | Binary *DbgBin = nullptr; |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 303 | ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(Path); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 304 | if (!error(BinaryOrErr.getError())) { |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 305 | std::unique_ptr<Binary> &ParsedBinary = BinaryOrErr.get(); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 306 | // Check if it's a universal binary. |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 307 | Bin = ParsedBinary.get(); |
| 308 | ParsedBinariesAndObjects.push_back(std::move(ParsedBinary)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 309 | if (Bin->isMachO() || Bin->isMachOUniversalBinary()) { |
| 310 | // On Darwin we may find DWARF in separate object file in |
| 311 | // resource directory. |
| 312 | const std::string &ResourcePath = |
| 313 | getDarwinDWARFResourceForPath(Path); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 314 | BinaryOrErr = createBinary(ResourcePath); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 315 | std::error_code EC = BinaryOrErr.getError(); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 316 | if (EC != errc::no_such_file_or_directory && !error(EC)) { |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 317 | DbgBin = BinaryOrErr.get().get(); |
| 318 | ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get())); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 321 | // Try to locate the debug binary using .gnu_debuglink section. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 322 | if (!DbgBin) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 323 | std::string DebuglinkName; |
| 324 | uint32_t CRCHash; |
| 325 | std::string DebugBinaryPath; |
| 326 | if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) && |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 327 | findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) { |
| 328 | BinaryOrErr = createBinary(DebugBinaryPath); |
| 329 | if (!error(BinaryOrErr.getError())) { |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 330 | DbgBin = BinaryOrErr.get().get(); |
| 331 | ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get())); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 332 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 335 | } |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 336 | if (!DbgBin) |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 337 | DbgBin = Bin; |
| 338 | BinaryPair Res = std::make_pair(Bin, DbgBin); |
| 339 | BinaryForPath[Path] = Res; |
| 340 | return Res; |
| 341 | } |
| 342 | |
| 343 | ObjectFile * |
| 344 | LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 345 | if (!Bin) |
| 346 | return nullptr; |
| 347 | ObjectFile *Res = nullptr; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 348 | if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) { |
| 349 | ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find( |
| 350 | std::make_pair(UB, ArchName)); |
| 351 | if (I != ObjectFileForArch.end()) |
| 352 | return I->second; |
Rafael Espindola | 4f7932b | 2014-06-23 20:41:02 +0000 | [diff] [blame] | 353 | ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj = |
| 354 | UB->getObjectForArch(Triple(ArchName).getArch()); |
| 355 | if (ParsedObj) { |
| 356 | Res = ParsedObj.get().get(); |
| 357 | ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get())); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 358 | } |
| 359 | ObjectFileForArch[std::make_pair(UB, ArchName)] = Res; |
| 360 | } else if (Bin->isObject()) { |
| 361 | Res = cast<ObjectFile>(Bin); |
| 362 | } |
| 363 | return Res; |
| 364 | } |
| 365 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 366 | ModuleInfo * |
| 367 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 368 | ModuleMapTy::iterator I = Modules.find(ModuleName); |
| 369 | if (I != Modules.end()) |
| 370 | return I->second; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 371 | std::string BinaryName = ModuleName; |
| 372 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 373 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 374 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 375 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 376 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 377 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 378 | BinaryName = ModuleName.substr(0, ColonPos); |
| 379 | ArchName = ArchStr; |
| 380 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 381 | } |
| 382 | BinaryPair Binaries = getOrCreateBinary(BinaryName); |
| 383 | ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName); |
| 384 | ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 385 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 386 | if (!Obj) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 387 | // Failed to find valid object file. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 388 | Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr)); |
| 389 | return nullptr; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 390 | } |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 391 | DIContext *Context = DIContext::getDWARFContext(*DbgObj); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 392 | assert(Context); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 393 | ModuleInfo *Info = new ModuleInfo(Obj, Context); |
| 394 | Modules.insert(make_pair(ModuleName, Info)); |
| 395 | return Info; |
| 396 | } |
| 397 | |
| 398 | std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const { |
| 399 | // By default, DILineInfo contains "<invalid>" for function/filename it |
| 400 | // cannot fetch. We replace it to "??" to make our output closer to addr2line. |
| 401 | static const std::string kDILineInfoBadString = "<invalid>"; |
| 402 | std::stringstream Result; |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 403 | if (Opts.PrintFunctions != FunctionNameKind::None) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 404 | std::string FunctionName = LineInfo.FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 405 | if (FunctionName == kDILineInfoBadString) |
| 406 | FunctionName = kBadString; |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 407 | else if (Opts.Demangle) |
| 408 | FunctionName = DemangleName(FunctionName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 409 | Result << FunctionName << "\n"; |
| 410 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 411 | std::string Filename = LineInfo.FileName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 412 | if (Filename == kDILineInfoBadString) |
| 413 | Filename = kBadString; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 414 | Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 415 | return Result.str(); |
| 416 | } |
| 417 | |
| 418 | #if !defined(_MSC_VER) |
| 419 | // Assume that __cxa_demangle is provided by libcxxabi (except for Windows). |
| 420 | extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer, |
| 421 | size_t *length, int *status); |
| 422 | #endif |
| 423 | |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 424 | std::string LLVMSymbolizer::DemangleName(const std::string &Name) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 425 | #if !defined(_MSC_VER) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 426 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 427 | // approach to check if the name should be demangled. |
| 428 | if (Name.substr(0, 2) != "_Z") |
| 429 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 430 | int status = 0; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 431 | char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 432 | if (status != 0) |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 433 | return Name; |
| 434 | std::string Result = DemangledName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 435 | free(DemangledName); |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 436 | return Result; |
| 437 | #else |
| 438 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 439 | #endif |
| 440 | } |
| 441 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 442 | } // namespace symbolize |
| 443 | } // namespace llvm |