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