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