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