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