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