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