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