Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 1 | //===-- LLVMSymbolize.cpp -------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implementation for LLVM symbolization library. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexey Samsonov | 57f8837 | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
| 14 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 15 | #include "SymbolizableObjectFile.h" |
| 16 | |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 18 | #include "llvm/BinaryFormat/COFF.h" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDB.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDBContext.h" |
Eugene Zemtsov | cd72cbc | 2018-03-07 23:07:34 +0000 | [diff] [blame] | 22 | #include "llvm/Demangle/Demangle.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 23 | #include "llvm/Object/COFF.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 24 | #include "llvm/Object/MachO.h" |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 25 | #include "llvm/Object/MachOUniversal.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Casting.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compression.h" |
| 28 | #include "llvm/Support/DataExtractor.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Errc.h" |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | #include <cassert> |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 35 | #include <cstring> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 36 | |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 37 | #if defined(_MSC_VER) |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 38 | #include <Windows.h> |
Chandler Carruth | aaeada6 | 2017-06-06 12:43:20 +0000 | [diff] [blame] | 39 | |
| 40 | // This must be included after windows.h. |
| 41 | #include <DbgHelp.h> |
Leny Kholodov | bebb27b | 2015-07-02 14:34:57 +0000 | [diff] [blame] | 42 | #pragma comment(lib, "dbghelp.lib") |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 43 | |
| 44 | // Windows.h conflicts with our COFF header definitions. |
| 45 | #ifdef IMAGE_FILE_MACHINE_I386 |
| 46 | #undef IMAGE_FILE_MACHINE_I386 |
| 47 | #endif |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 50 | namespace llvm { |
| 51 | namespace symbolize { |
| 52 | |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 53 | Expected<DILineInfo> |
| 54 | LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 55 | uint64_t ModuleOffset, StringRef DWPName) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 56 | SymbolizableModule *Info; |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 57 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName, DWPName)) |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 58 | Info = InfoOrErr.get(); |
| 59 | else |
| 60 | return InfoOrErr.takeError(); |
| 61 | |
| 62 | // A null module means an error has already been reported. Return an empty |
| 63 | // result. |
| 64 | if (!Info) |
| 65 | return DILineInfo(); |
Reid Kleckner | e94fef7 | 2015-10-09 00:15:01 +0000 | [diff] [blame] | 66 | |
| 67 | // If the user is giving us relative addresses, add the preferred base of the |
| 68 | // object to the offset before we do the query. It's what DIContext expects. |
| 69 | if (Opts.RelativeAddresses) |
| 70 | ModuleOffset += Info->getModulePreferredBase(); |
| 71 | |
Alexey Samsonov | 0fb6451 | 2015-10-26 22:34:56 +0000 | [diff] [blame] | 72 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions, |
| 73 | Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 74 | if (Opts.Demangle) |
| 75 | LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 76 | return LineInfo; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 79 | Expected<DIInliningInfo> |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 80 | LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 81 | uint64_t ModuleOffset, StringRef DWPName) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 82 | SymbolizableModule *Info; |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 83 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName, DWPName)) |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 84 | Info = InfoOrErr.get(); |
| 85 | else |
| 86 | return InfoOrErr.takeError(); |
| 87 | |
| 88 | // A null module means an error has already been reported. Return an empty |
| 89 | // result. |
| 90 | if (!Info) |
| 91 | return DIInliningInfo(); |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 92 | |
| 93 | // If the user is giving us relative addresses, add the preferred base of the |
| 94 | // object to the offset before we do the query. It's what DIContext expects. |
| 95 | if (Opts.RelativeAddresses) |
| 96 | ModuleOffset += Info->getModulePreferredBase(); |
| 97 | |
| 98 | DIInliningInfo InlinedContext = Info->symbolizeInlinedCode( |
| 99 | ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 100 | if (Opts.Demangle) { |
| 101 | for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
| 102 | auto *Frame = InlinedContext.getMutableFrame(i); |
| 103 | Frame->FunctionName = DemangleName(Frame->FunctionName, Info); |
| 104 | } |
| 105 | } |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 106 | return InlinedContext; |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 109 | Expected<DIGlobal> LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 110 | uint64_t ModuleOffset) { |
| 111 | SymbolizableModule *Info; |
| 112 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) |
| 113 | Info = InfoOrErr.get(); |
| 114 | else |
| 115 | return InfoOrErr.takeError(); |
| 116 | |
| 117 | // A null module means an error has already been reported. Return an empty |
| 118 | // result. |
| 119 | if (!Info) |
| 120 | return DIGlobal(); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 121 | |
| 122 | // If the user is giving us relative addresses, add the preferred base of |
| 123 | // the object to the offset before we do the query. It's what DIContext |
| 124 | // expects. |
| 125 | if (Opts.RelativeAddresses) |
| 126 | ModuleOffset += Info->getModulePreferredBase(); |
| 127 | |
| 128 | DIGlobal Global = Info->symbolizeData(ModuleOffset); |
| 129 | if (Opts.Demangle) |
| 130 | Global.Name = DemangleName(Global.Name, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 131 | return Global; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 134 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 135 | ObjectForUBPathAndArch.clear(); |
| 136 | BinaryForPath.clear(); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 137 | ObjectPairForPathArch.clear(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 138 | Modules.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 141 | namespace { |
| 142 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 143 | // For Path="/path/to/foo" and Basename="foo" assume that debug info is in |
| 144 | // /path/to/foo.dSYM/Contents/Resources/DWARF/foo. |
| 145 | // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in |
| 146 | // /path/to/bar.dSYM/Contents/Resources/DWARF/foo. |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 147 | std::string getDarwinDWARFResourceForPath( |
| 148 | const std::string &Path, const std::string &Basename) { |
| 149 | SmallString<16> ResourceName = StringRef(Path); |
| 150 | if (sys::path::extension(Path) != ".dSYM") { |
| 151 | ResourceName += ".dSYM"; |
| 152 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 153 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 154 | sys::path::append(ResourceName, Basename); |
| 155 | return ResourceName.str(); |
| 156 | } |
| 157 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 158 | bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 159 | ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
| 160 | MemoryBuffer::getFileOrSTDIN(Path); |
| 161 | if (!MB) |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 162 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 163 | return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 166 | bool findDebugBinary(const std::string &OrigPath, |
| 167 | const std::string &DebuglinkName, uint32_t CRCHash, |
| 168 | std::string &Result) { |
Jordan Rupprecht | 835df27 | 2019-02-01 21:04:16 +0000 | [diff] [blame] | 169 | SmallString<16> OrigDir(OrigPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 170 | llvm::sys::path::remove_filename(OrigDir); |
| 171 | SmallString<16> DebugPath = OrigDir; |
| 172 | // Try /path/to/original_binary/debuglink_name |
| 173 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 174 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 175 | Result = DebugPath.str(); |
| 176 | return true; |
| 177 | } |
| 178 | // Try /path/to/original_binary/.debug/debuglink_name |
Francis Ricci | fe6cbce | 2018-03-02 22:56:45 +0000 | [diff] [blame] | 179 | DebugPath = OrigDir; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 180 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 181 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 182 | Result = DebugPath.str(); |
| 183 | return true; |
| 184 | } |
Kamil Rytarowski | a8448ad | 2018-06-25 18:49:13 +0000 | [diff] [blame] | 185 | #if defined(__NetBSD__) |
| 186 | // Try /usr/libdata/debug/path/to/original_binary/debuglink_name |
| 187 | DebugPath = "/usr/libdata/debug"; |
| 188 | #else |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 189 | // Try /usr/lib/debug/path/to/original_binary/debuglink_name |
| 190 | DebugPath = "/usr/lib/debug"; |
Kamil Rytarowski | a8448ad | 2018-06-25 18:49:13 +0000 | [diff] [blame] | 191 | #endif |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 192 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 193 | DebuglinkName); |
| 194 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 195 | Result = DebugPath.str(); |
| 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 201 | bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, |
| 202 | uint32_t &CRCHash) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 203 | if (!Obj) |
| 204 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 205 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 206 | StringRef Name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 207 | Section.getName(Name); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 208 | Name = Name.substr(Name.find_first_not_of("._")); |
| 209 | if (Name == "gnu_debuglink") { |
| 210 | StringRef Data; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 211 | Section.getContents(Data); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 212 | DataExtractor DE(Data, Obj->isLittleEndian(), 0); |
| 213 | uint32_t Offset = 0; |
| 214 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 215 | // 4-byte align the offset. |
| 216 | Offset = (Offset + 3) & ~0x3; |
| 217 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 218 | DebugName = DebugNameStr; |
| 219 | CRCHash = DE.getU32(&Offset); |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 229 | bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, |
| 230 | const MachOObjectFile *Obj) { |
| 231 | ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); |
| 232 | ArrayRef<uint8_t> bin_uuid = Obj->getUuid(); |
| 233 | if (dbg_uuid.empty() || bin_uuid.empty()) |
| 234 | return false; |
| 235 | return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); |
| 236 | } |
| 237 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 238 | } // end anonymous namespace |
| 239 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 240 | ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, |
| 241 | const MachOObjectFile *MachExeObj, const std::string &ArchName) { |
| 242 | // On Darwin we may find DWARF in separate object file in |
| 243 | // resource directory. |
| 244 | std::vector<std::string> DsymPaths; |
| 245 | StringRef Filename = sys::path::filename(ExePath); |
| 246 | DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename)); |
| 247 | for (const auto &Path : Opts.DsymHints) { |
| 248 | DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename)); |
| 249 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 250 | for (const auto &Path : DsymPaths) { |
| 251 | auto DbgObjOrErr = getOrCreateObject(Path, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 252 | if (!DbgObjOrErr) { |
| 253 | // Ignore errors, the file might not exist. |
| 254 | consumeError(DbgObjOrErr.takeError()); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 255 | continue; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 256 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 257 | ObjectFile *DbgObj = DbgObjOrErr.get(); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 258 | if (!DbgObj) |
| 259 | continue; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 260 | const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 261 | if (!MachDbgObj) |
| 262 | continue; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 263 | if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 264 | return DbgObj; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 265 | } |
| 266 | return nullptr; |
| 267 | } |
| 268 | |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 269 | ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, |
| 270 | const ObjectFile *Obj, |
| 271 | const std::string &ArchName) { |
| 272 | std::string DebuglinkName; |
| 273 | uint32_t CRCHash; |
| 274 | std::string DebugBinaryPath; |
| 275 | if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash)) |
| 276 | return nullptr; |
| 277 | if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) |
| 278 | return nullptr; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 279 | auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 280 | if (!DbgObjOrErr) { |
| 281 | // Ignore errors, the file might not exist. |
| 282 | consumeError(DbgObjOrErr.takeError()); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 283 | return nullptr; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 284 | } |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 285 | return DbgObjOrErr.get(); |
| 286 | } |
| 287 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 288 | Expected<LLVMSymbolizer::ObjectPair> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 289 | LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, |
| 290 | const std::string &ArchName) { |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 291 | const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 292 | if (I != ObjectPairForPathArch.end()) { |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 293 | return I->second; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 294 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 295 | |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 296 | auto ObjOrErr = getOrCreateObject(Path, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 297 | if (!ObjOrErr) { |
| 298 | ObjectPairForPathArch.insert(std::make_pair(std::make_pair(Path, ArchName), |
| 299 | ObjectPair(nullptr, nullptr))); |
| 300 | return ObjOrErr.takeError(); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 301 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 302 | |
| 303 | ObjectFile *Obj = ObjOrErr.get(); |
| 304 | assert(Obj != nullptr); |
| 305 | ObjectFile *DbgObj = nullptr; |
| 306 | |
| 307 | if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj)) |
| 308 | DbgObj = lookUpDsymFile(Path, MachObj, ArchName); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 309 | if (!DbgObj) |
| 310 | DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 311 | if (!DbgObj) |
| 312 | DbgObj = Obj; |
| 313 | ObjectPair Res = std::make_pair(Obj, DbgObj); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 314 | ObjectPairForPathArch.insert( |
| 315 | std::make_pair(std::make_pair(Path, ArchName), Res)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 316 | return Res; |
| 317 | } |
| 318 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 319 | Expected<ObjectFile *> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 320 | LLVMSymbolizer::getOrCreateObject(const std::string &Path, |
| 321 | const std::string &ArchName) { |
| 322 | const auto &I = BinaryForPath.find(Path); |
| 323 | Binary *Bin = nullptr; |
| 324 | if (I == BinaryForPath.end()) { |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 325 | Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path); |
| 326 | if (!BinOrErr) { |
Reid Kleckner | 4ece163 | 2016-06-06 23:46:14 +0000 | [diff] [blame] | 327 | BinaryForPath.insert(std::make_pair(Path, OwningBinary<Binary>())); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 328 | return BinOrErr.takeError(); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 329 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 330 | Bin = BinOrErr->getBinary(); |
| 331 | BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get()))); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 332 | } else { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 333 | Bin = I->second.getBinary(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 336 | if (!Bin) |
| 337 | return static_cast<ObjectFile *>(nullptr); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 338 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 339 | if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) { |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 340 | const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); |
| 341 | if (I != ObjectForUBPathAndArch.end()) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 342 | return I->second.get(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 343 | } |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 344 | Expected<std::unique_ptr<ObjectFile>> ObjOrErr = |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 345 | UB->getObjectForArch(ArchName); |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 346 | if (!ObjOrErr) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 347 | ObjectForUBPathAndArch.insert(std::make_pair( |
| 348 | std::make_pair(Path, ArchName), std::unique_ptr<ObjectFile>())); |
| 349 | return ObjOrErr.takeError(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 350 | } |
| 351 | ObjectFile *Res = ObjOrErr->get(); |
| 352 | ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName), |
| 353 | std::move(ObjOrErr.get()))); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 354 | return Res; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 355 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 356 | if (Bin->isObject()) { |
| 357 | return cast<ObjectFile>(Bin); |
| 358 | } |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 359 | return errorCodeToError(object_error::arch_not_found); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 362 | Expected<SymbolizableModule *> |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 363 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName, |
| 364 | StringRef DWPName) { |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 365 | const auto &I = Modules.find(ModuleName); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 366 | if (I != Modules.end()) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 367 | return I->second.get(); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 368 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 369 | std::string BinaryName = ModuleName; |
| 370 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 371 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 372 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 373 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 374 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 375 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 376 | BinaryName = ModuleName.substr(0, ColonPos); |
| 377 | ArchName = ArchStr; |
| 378 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 379 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 380 | auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 381 | if (!ObjectsOrErr) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 382 | // Failed to find valid object file. |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 383 | Modules.insert( |
| 384 | std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>())); |
| 385 | return ObjectsOrErr.takeError(); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 386 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 387 | ObjectPair Objects = ObjectsOrErr.get(); |
| 388 | |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 389 | std::unique_ptr<DIContext> Context; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 390 | // If this is a COFF object containing PDB info, use a PDBContext to |
| 391 | // symbolize. Otherwise, use DWARF. |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 392 | if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) { |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 393 | const codeview::DebugInfo *DebugInfo; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 394 | StringRef PDBFileName; |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 395 | auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName); |
Reid Kleckner | d1882f2 | 2016-09-01 20:28:59 +0000 | [diff] [blame] | 396 | if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 397 | using namespace pdb; |
| 398 | std::unique_ptr<IPDBSession> Session; |
| 399 | if (auto Err = loadDataForEXE(PDB_ReaderType::DIA, |
| 400 | Objects.first->getFileName(), Session)) { |
| 401 | Modules.insert( |
| 402 | std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>())); |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 403 | // Return along the PDB filename to provide more context |
| 404 | return createFileError(PDBFileName, std::move(Err)); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 405 | } |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 406 | Context.reset(new PDBContext(*CoffObject, std::move(Session))); |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 407 | } |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 408 | } |
| 409 | if (!Context) |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 410 | Context = DWARFContext::create(*Objects.second, nullptr, |
| 411 | DWARFContext::defaultErrorHandler, DWPName); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 412 | assert(Context); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 413 | auto InfoOrErr = |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 414 | SymbolizableObjectFile::create(Objects.first, std::move(Context)); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 415 | std::unique_ptr<SymbolizableModule> SymMod; |
| 416 | if (InfoOrErr) |
| 417 | SymMod = std::move(InfoOrErr.get()); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 418 | auto InsertResult = |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 419 | Modules.insert(std::make_pair(ModuleName, std::move(SymMod))); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 420 | assert(InsertResult.second); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 421 | if (auto EC = InfoOrErr.getError()) |
| 422 | return errorCodeToError(EC); |
| 423 | return InsertResult.first->second.get(); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 426 | namespace { |
| 427 | |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 428 | // Undo these various manglings for Win32 extern "C" functions: |
| 429 | // cdecl - _foo |
| 430 | // stdcall - _foo@12 |
| 431 | // fastcall - @foo@12 |
| 432 | // vectorcall - foo@@12 |
| 433 | // These are all different linkage names for 'foo'. |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 434 | StringRef demanglePE32ExternCFunc(StringRef SymbolName) { |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 435 | // Remove any '_' or '@' prefix. |
| 436 | char Front = SymbolName.empty() ? '\0' : SymbolName[0]; |
| 437 | if (Front == '_' || Front == '@') |
| 438 | SymbolName = SymbolName.drop_front(); |
| 439 | |
| 440 | // Remove any '@[0-9]+' suffix. |
| 441 | if (Front != '?') { |
| 442 | size_t AtPos = SymbolName.rfind('@'); |
| 443 | if (AtPos != StringRef::npos && |
| 444 | std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(), |
| 445 | [](char C) { return C >= '0' && C <= '9'; })) { |
| 446 | SymbolName = SymbolName.substr(0, AtPos); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Remove any ending '@' for vectorcall. |
| 451 | if (SymbolName.endswith("@")) |
| 452 | SymbolName = SymbolName.drop_back(); |
| 453 | |
| 454 | return SymbolName; |
| 455 | } |
| 456 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 457 | } // end anonymous namespace |
| 458 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 459 | std::string |
| 460 | LLVMSymbolizer::DemangleName(const std::string &Name, |
| 461 | const SymbolizableModule *DbiModuleDescriptor) { |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 462 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 463 | // approach to check if the name should be demangled. |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 464 | if (Name.substr(0, 2) == "_Z") { |
| 465 | int status = 0; |
Eugene Zemtsov | cd72cbc | 2018-03-07 23:07:34 +0000 | [diff] [blame] | 466 | char *DemangledName = itaniumDemangle(Name.c_str(), nullptr, nullptr, &status); |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 467 | if (status != 0) |
| 468 | return Name; |
| 469 | std::string Result = DemangledName; |
| 470 | free(DemangledName); |
| 471 | return Result; |
| 472 | } |
Eugene Zemtsov | cd72cbc | 2018-03-07 23:07:34 +0000 | [diff] [blame] | 473 | |
| 474 | #if defined(_MSC_VER) |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 475 | if (!Name.empty() && Name.front() == '?') { |
| 476 | // Only do MSVC C++ demangling on symbols starting with '?'. |
| 477 | char DemangledName[1024] = {0}; |
| 478 | DWORD result = ::UnDecorateSymbolName( |
| 479 | Name.c_str(), DemangledName, 1023, |
| 480 | UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected |
| 481 | UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc |
| 482 | UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications |
| 483 | UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers |
| 484 | UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords |
| 485 | UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types |
| 486 | return (result == 0) ? Name : std::string(DemangledName); |
| 487 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 488 | #endif |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 489 | if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 490 | return std::string(demanglePE32ExternCFunc(Name)); |
| 491 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 494 | } // namespace symbolize |
| 495 | } // namespace llvm |