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. |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 88 | auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects; |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 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 { |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 96 | const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects; |
| 97 | if (SymbolMap.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 }; |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 100 | auto SymbolIterator = SymbolMap.upper_bound(SD); |
| 101 | if (SymbolIterator == SymbolMap.begin()) |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 102 | return false; |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 103 | --SymbolIterator; |
| 104 | if (SymbolIterator->first.Size != 0 && |
| 105 | SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address) |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 106 | return false; |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 107 | Name = SymbolIterator->second.str(); |
| 108 | Addr = SymbolIterator->first.Addr; |
| 109 | Size = SymbolIterator->first.Size; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 113 | DILineInfo ModuleInfo::symbolizeCode( |
| 114 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 115 | DILineInfo LineInfo; |
| 116 | if (DebugInfoContext) { |
| 117 | LineInfo = DebugInfoContext->getLineInfoForAddress( |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 118 | ModuleOffset, getDILineInfoSpecifier(Opts)); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 119 | } |
| 120 | // Override function name from symbol table if necessary. |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 121 | if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 122 | std::string FunctionName; |
| 123 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 124 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 125 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 126 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | return LineInfo; |
| 130 | } |
| 131 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 132 | DIInliningInfo ModuleInfo::symbolizeInlinedCode( |
| 133 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 134 | DIInliningInfo InlinedContext; |
| 135 | if (DebugInfoContext) { |
| 136 | InlinedContext = DebugInfoContext->getInliningInfoForAddress( |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 137 | ModuleOffset, getDILineInfoSpecifier(Opts)); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 138 | } |
| 139 | // Make sure there is at least one frame in context. |
| 140 | if (InlinedContext.getNumberOfFrames() == 0) { |
| 141 | InlinedContext.addFrame(DILineInfo()); |
| 142 | } |
| 143 | // Override the function name in lower frame with name from symbol table. |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 144 | if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 145 | DIInliningInfo PatchedInlinedContext; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 146 | for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 147 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 148 | if (i == n - 1) { |
| 149 | std::string FunctionName; |
| 150 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 151 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 152 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 153 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | PatchedInlinedContext.addFrame(LineInfo); |
| 157 | } |
| 158 | InlinedContext = PatchedInlinedContext; |
| 159 | } |
| 160 | return InlinedContext; |
| 161 | } |
| 162 | |
| 163 | bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name, |
| 164 | uint64_t &Start, uint64_t &Size) const { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 165 | return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start, |
| 166 | Size); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Alexey Samsonov | d6cef10 | 2013-02-04 15:55:26 +0000 | [diff] [blame] | 169 | const char LLVMSymbolizer::kBadString[] = "??"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 170 | |
| 171 | std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 172 | uint64_t ModuleOffset) { |
| 173 | ModuleInfo *Info = getOrCreateModuleInfo(ModuleName); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 174 | if (!Info) |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 175 | return printDILineInfo(DILineInfo()); |
| 176 | if (Opts.PrintInlining) { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 177 | DIInliningInfo InlinedContext = |
| 178 | Info->symbolizeInlinedCode(ModuleOffset, Opts); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 179 | uint32_t FramesNum = InlinedContext.getNumberOfFrames(); |
| 180 | assert(FramesNum > 0); |
| 181 | std::string Result; |
| 182 | for (uint32_t i = 0; i < FramesNum; i++) { |
| 183 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 184 | Result += printDILineInfo(LineInfo); |
| 185 | } |
| 186 | return Result; |
| 187 | } |
| 188 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts); |
| 189 | return printDILineInfo(LineInfo); |
| 190 | } |
| 191 | |
| 192 | std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 193 | uint64_t ModuleOffset) { |
| 194 | std::string Name = kBadString; |
| 195 | uint64_t Start = 0; |
| 196 | uint64_t Size = 0; |
| 197 | if (Opts.UseSymbolTable) { |
| 198 | if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) { |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 199 | if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 200 | Name = DemangleName(Name); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | std::stringstream ss; |
| 204 | ss << Name << "\n" << Start << " " << Size << "\n"; |
| 205 | return ss.str(); |
| 206 | } |
| 207 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 208 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 209 | DeleteContainerSeconds(Modules); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 210 | ObjectPairForPathArch.clear(); |
Alexey Samsonov | fe3a5d9 | 2013-06-28 15:08:29 +0000 | [diff] [blame] | 211 | ObjectFileForArch.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 214 | // For Path="/path/to/foo" and Basename="foo" assume that debug info is in |
| 215 | // /path/to/foo.dSYM/Contents/Resources/DWARF/foo. |
| 216 | // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in |
| 217 | // /path/to/bar.dSYM/Contents/Resources/DWARF/foo. |
| 218 | static |
| 219 | std::string getDarwinDWARFResourceForPath( |
| 220 | const std::string &Path, const std::string &Basename) { |
| 221 | SmallString<16> ResourceName = StringRef(Path); |
| 222 | if (sys::path::extension(Path) != ".dSYM") { |
| 223 | ResourceName += ".dSYM"; |
| 224 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 225 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 226 | sys::path::append(ResourceName, Basename); |
| 227 | return ResourceName.str(); |
| 228 | } |
| 229 | |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 230 | static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 231 | ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
| 232 | MemoryBuffer::getFileOrSTDIN(Path); |
| 233 | if (!MB) |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 234 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 235 | return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static bool findDebugBinary(const std::string &OrigPath, |
| 239 | const std::string &DebuglinkName, uint32_t CRCHash, |
| 240 | std::string &Result) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 241 | std::string OrigRealPath = OrigPath; |
| 242 | #if defined(HAVE_REALPATH) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 243 | if (char *RP = realpath(OrigPath.c_str(), nullptr)) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 244 | OrigRealPath = RP; |
| 245 | free(RP); |
| 246 | } |
| 247 | #endif |
| 248 | SmallString<16> OrigDir(OrigRealPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 249 | llvm::sys::path::remove_filename(OrigDir); |
| 250 | SmallString<16> DebugPath = OrigDir; |
| 251 | // Try /path/to/original_binary/debuglink_name |
| 252 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 253 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 254 | Result = DebugPath.str(); |
| 255 | return true; |
| 256 | } |
| 257 | // Try /path/to/original_binary/.debug/debuglink_name |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 258 | DebugPath = OrigRealPath; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 259 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 260 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 261 | Result = DebugPath.str(); |
| 262 | return true; |
| 263 | } |
| 264 | // Try /usr/lib/debug/path/to/original_binary/debuglink_name |
| 265 | DebugPath = "/usr/lib/debug"; |
| 266 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 267 | DebuglinkName); |
| 268 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 269 | Result = DebugPath.str(); |
| 270 | return true; |
| 271 | } |
| 272 | return false; |
| 273 | } |
| 274 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 275 | static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 276 | uint32_t &CRCHash) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 277 | if (!Obj) |
| 278 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 279 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 280 | StringRef Name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 281 | Section.getName(Name); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 282 | Name = Name.substr(Name.find_first_not_of("._")); |
| 283 | if (Name == "gnu_debuglink") { |
| 284 | StringRef Data; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 285 | Section.getContents(Data); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 286 | DataExtractor DE(Data, Obj->isLittleEndian(), 0); |
| 287 | uint32_t Offset = 0; |
| 288 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 289 | // 4-byte align the offset. |
| 290 | Offset = (Offset + 3) & ~0x3; |
| 291 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 292 | DebugName = DebugNameStr; |
| 293 | CRCHash = DE.getU32(&Offset); |
| 294 | return true; |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 303 | static |
| 304 | bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, |
| 305 | const MachOObjectFile *Obj) { |
| 306 | ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); |
| 307 | ArrayRef<uint8_t> bin_uuid = Obj->getUuid(); |
| 308 | if (dbg_uuid.empty() || bin_uuid.empty()) |
| 309 | return false; |
| 310 | return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); |
| 311 | } |
| 312 | |
| 313 | ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, |
| 314 | const MachOObjectFile *MachExeObj, const std::string &ArchName) { |
| 315 | // On Darwin we may find DWARF in separate object file in |
| 316 | // resource directory. |
| 317 | std::vector<std::string> DsymPaths; |
| 318 | StringRef Filename = sys::path::filename(ExePath); |
| 319 | DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename)); |
| 320 | for (const auto &Path : Opts.DsymHints) { |
| 321 | DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename)); |
| 322 | } |
| 323 | for (const auto &path : DsymPaths) { |
| 324 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(path); |
| 325 | std::error_code EC = BinaryOrErr.getError(); |
| 326 | if (EC != errc::no_such_file_or_directory && !error(EC)) { |
| 327 | OwningBinary<Binary> B = std::move(BinaryOrErr.get()); |
| 328 | ObjectFile *DbgObj = |
| 329 | getObjectFileFromBinary(B.getBinary().get(), ArchName); |
| 330 | const MachOObjectFile *MachDbgObj = |
| 331 | dyn_cast<const MachOObjectFile>(DbgObj); |
| 332 | if (!MachDbgObj) continue; |
| 333 | if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 334 | addOwningBinary(std::move(B)); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 335 | return DbgObj; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 338 | } |
| 339 | return nullptr; |
| 340 | } |
| 341 | |
| 342 | LLVMSymbolizer::ObjectPair |
| 343 | LLVMSymbolizer::getOrCreateObjects(const std::string &Path, |
| 344 | const std::string &ArchName) { |
| 345 | const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); |
| 346 | if (I != ObjectPairForPathArch.end()) |
| 347 | return I->second; |
| 348 | ObjectFile *Obj = nullptr; |
| 349 | ObjectFile *DbgObj = nullptr; |
| 350 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path); |
| 351 | if (!error(BinaryOrErr.getError())) { |
| 352 | OwningBinary<Binary> &B = BinaryOrErr.get(); |
| 353 | Obj = getObjectFileFromBinary(B.getBinary().get(), ArchName); |
| 354 | if (!Obj) { |
| 355 | ObjectPair Res = std::make_pair(nullptr, nullptr); |
| 356 | ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res; |
| 357 | return Res; |
| 358 | } |
| 359 | addOwningBinary(std::move(B)); |
| 360 | if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj)) |
| 361 | DbgObj = lookUpDsymFile(Path, MachObj, ArchName); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 362 | // Try to locate the debug binary using .gnu_debuglink section. |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 363 | if (!DbgObj) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 364 | std::string DebuglinkName; |
| 365 | uint32_t CRCHash; |
| 366 | std::string DebugBinaryPath; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 367 | if (getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash) && |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 368 | findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) { |
| 369 | BinaryOrErr = createBinary(DebugBinaryPath); |
| 370 | if (!error(BinaryOrErr.getError())) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 371 | OwningBinary<Binary> B = std::move(BinaryOrErr.get()); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 372 | DbgObj = getObjectFileFromBinary(B.getBinary().get(), ArchName); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 373 | addOwningBinary(std::move(B)); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 374 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 377 | } |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 378 | if (!DbgObj) |
| 379 | DbgObj = Obj; |
| 380 | ObjectPair Res = std::make_pair(Obj, DbgObj); |
| 381 | ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 382 | return Res; |
| 383 | } |
| 384 | |
| 385 | ObjectFile * |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 386 | LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, |
| 387 | const std::string &ArchName) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 388 | if (!Bin) |
| 389 | return nullptr; |
| 390 | ObjectFile *Res = nullptr; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 391 | if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) { |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 392 | const auto &I = ObjectFileForArch.find( |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 393 | std::make_pair(UB, ArchName)); |
| 394 | if (I != ObjectFileForArch.end()) |
| 395 | return I->second; |
Rafael Espindola | 4f7932b | 2014-06-23 20:41:02 +0000 | [diff] [blame] | 396 | ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj = |
| 397 | UB->getObjectForArch(Triple(ArchName).getArch()); |
| 398 | if (ParsedObj) { |
| 399 | Res = ParsedObj.get().get(); |
| 400 | ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get())); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 401 | } |
| 402 | ObjectFileForArch[std::make_pair(UB, ArchName)] = Res; |
| 403 | } else if (Bin->isObject()) { |
| 404 | Res = cast<ObjectFile>(Bin); |
| 405 | } |
| 406 | return Res; |
| 407 | } |
| 408 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 409 | ModuleInfo * |
| 410 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 411 | const auto &I = Modules.find(ModuleName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 412 | if (I != Modules.end()) |
| 413 | return I->second; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 414 | std::string BinaryName = ModuleName; |
| 415 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 416 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 417 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 418 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 419 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 420 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 421 | BinaryName = ModuleName.substr(0, ColonPos); |
| 422 | ArchName = ArchStr; |
| 423 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 424 | } |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 425 | ObjectPair Objects = getOrCreateObjects(BinaryName, ArchName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 426 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 427 | if (!Objects.first) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 428 | // Failed to find valid object file. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 429 | Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr)); |
| 430 | return nullptr; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 431 | } |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 432 | DIContext *Context = DIContext::getDWARFContext(*Objects.second); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 433 | assert(Context); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 434 | ModuleInfo *Info = new ModuleInfo(Objects.first, Context); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 435 | Modules.insert(make_pair(ModuleName, Info)); |
| 436 | return Info; |
| 437 | } |
| 438 | |
| 439 | std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const { |
| 440 | // By default, DILineInfo contains "<invalid>" for function/filename it |
| 441 | // cannot fetch. We replace it to "??" to make our output closer to addr2line. |
| 442 | static const std::string kDILineInfoBadString = "<invalid>"; |
| 443 | std::stringstream Result; |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 444 | if (Opts.PrintFunctions != FunctionNameKind::None) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 445 | std::string FunctionName = LineInfo.FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 446 | if (FunctionName == kDILineInfoBadString) |
| 447 | FunctionName = kBadString; |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 448 | else if (Opts.Demangle) |
| 449 | FunctionName = DemangleName(FunctionName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 450 | Result << FunctionName << "\n"; |
| 451 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 452 | std::string Filename = LineInfo.FileName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 453 | if (Filename == kDILineInfoBadString) |
| 454 | Filename = kBadString; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 455 | Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 456 | return Result.str(); |
| 457 | } |
| 458 | |
| 459 | #if !defined(_MSC_VER) |
| 460 | // Assume that __cxa_demangle is provided by libcxxabi (except for Windows). |
| 461 | extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer, |
| 462 | size_t *length, int *status); |
| 463 | #endif |
| 464 | |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 465 | std::string LLVMSymbolizer::DemangleName(const std::string &Name) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 466 | #if !defined(_MSC_VER) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 467 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 468 | // approach to check if the name should be demangled. |
| 469 | if (Name.substr(0, 2) != "_Z") |
| 470 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 471 | int status = 0; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 472 | char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 473 | if (status != 0) |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 474 | return Name; |
| 475 | std::string Result = DemangledName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 476 | free(DemangledName); |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 477 | return Result; |
| 478 | #else |
| 479 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 480 | #endif |
| 481 | } |
| 482 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 483 | } // namespace symbolize |
| 484 | } // namespace llvm |