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