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" |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ELFObjectFile.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachO.h" |
| 19 | #include "llvm/Support/Casting.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compression.h" |
| 21 | #include "llvm/Support/DataExtractor.h" |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 25 | #include <sstream> |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
| 29 | namespace symbolize { |
| 30 | |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 31 | static bool error(error_code ec) { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 32 | if (!ec) |
| 33 | return false; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 34 | errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n"; |
| 35 | return true; |
| 36 | } |
| 37 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 38 | static uint32_t |
| 39 | getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 40 | uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo | |
| 41 | llvm::DILineInfoSpecifier::AbsoluteFilePath; |
| 42 | if (Opts.PrintFunctions) |
| 43 | Flags |= llvm::DILineInfoSpecifier::FunctionName; |
| 44 | return Flags; |
| 45 | } |
| 46 | |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 47 | ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx) |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 48 | : Module(Obj), DebugInfoContext(DICtx) { |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 49 | for (const SymbolRef &Symbol : Module->symbols()) { |
| 50 | addSymbol(Symbol); |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 51 | } |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 52 | bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end()); |
| 53 | if (NoSymbolTable && Module->isELF()) { |
| 54 | // Fallback to dynamic symbol table, if regular symbol table is stripped. |
| 55 | std::pair<symbol_iterator, symbol_iterator> IDyn = |
| 56 | getELFDynamicSymbolIterators(Module); |
| 57 | for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) { |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 58 | addSymbol(*si); |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 63 | void ModuleInfo::addSymbol(const SymbolRef &Symbol) { |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 64 | SymbolRef::Type SymbolType; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 65 | if (error(Symbol.getType(SymbolType))) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 66 | return; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 67 | if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 68 | return; |
| 69 | uint64_t SymbolAddress; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 70 | if (error(Symbol.getAddress(SymbolAddress)) || |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 71 | SymbolAddress == UnknownAddressOrSize) |
| 72 | return; |
| 73 | uint64_t SymbolSize; |
| 74 | // Getting symbol size is linear for Mach-O files, so assume that symbol |
| 75 | // occupies the memory range up to the following symbol. |
| 76 | if (isa<MachOObjectFile>(Module)) |
| 77 | SymbolSize = 0; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 78 | else if (error(Symbol.getSize(SymbolSize)) || |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 79 | SymbolSize == UnknownAddressOrSize) |
| 80 | return; |
| 81 | StringRef SymbolName; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 82 | if (error(Symbol.getName(SymbolName))) |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 83 | return; |
| 84 | // Mach-O symbol table names have leading underscore, skip it. |
| 85 | if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_') |
| 86 | SymbolName = SymbolName.drop_front(); |
| 87 | // FIXME: If a function has alias, there are two entries in symbol table |
| 88 | // with same address size. Make sure we choose the correct one. |
| 89 | SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects; |
| 90 | SymbolDesc SD = { SymbolAddress, SymbolSize }; |
| 91 | M.insert(std::make_pair(SD, SymbolName)); |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, |
| 95 | std::string &Name, uint64_t &Addr, |
| 96 | uint64_t &Size) const { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 97 | const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 98 | if (M.empty()) |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 99 | return false; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 100 | SymbolDesc SD = { Address, Address }; |
| 101 | SymbolMapTy::const_iterator it = M.upper_bound(SD); |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 102 | if (it == M.begin()) |
| 103 | return false; |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 104 | --it; |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 105 | if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address) |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 106 | return false; |
| 107 | Name = it->second.str(); |
| 108 | Addr = it->first.Addr; |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 109 | Size = it->first.Size; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 113 | DILineInfo ModuleInfo::symbolizeCode( |
| 114 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 115 | DILineInfo LineInfo; |
| 116 | if (DebugInfoContext) { |
| 117 | LineInfo = DebugInfoContext->getLineInfoForAddress( |
| 118 | ModuleOffset, getDILineInfoSpecifierFlags(Opts)); |
| 119 | } |
| 120 | // Override function name from symbol table if necessary. |
| 121 | if (Opts.PrintFunctions && Opts.UseSymbolTable) { |
| 122 | std::string FunctionName; |
| 123 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 124 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 125 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 126 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | return LineInfo; |
| 130 | } |
| 131 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 132 | DIInliningInfo ModuleInfo::symbolizeInlinedCode( |
| 133 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 134 | DIInliningInfo InlinedContext; |
| 135 | if (DebugInfoContext) { |
| 136 | InlinedContext = DebugInfoContext->getInliningInfoForAddress( |
| 137 | ModuleOffset, getDILineInfoSpecifierFlags(Opts)); |
| 138 | } |
| 139 | // Make sure there is at least one frame in context. |
| 140 | if (InlinedContext.getNumberOfFrames() == 0) { |
| 141 | InlinedContext.addFrame(DILineInfo()); |
| 142 | } |
| 143 | // Override the function name in lower frame with name from symbol table. |
| 144 | if (Opts.PrintFunctions && Opts.UseSymbolTable) { |
| 145 | DIInliningInfo PatchedInlinedContext; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 146 | for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 147 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 148 | if (i == n - 1) { |
| 149 | std::string FunctionName; |
| 150 | uint64_t Start, Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 151 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 152 | FunctionName, Start, Size)) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 153 | LineInfo.FunctionName = FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | PatchedInlinedContext.addFrame(LineInfo); |
| 157 | } |
| 158 | InlinedContext = PatchedInlinedContext; |
| 159 | } |
| 160 | return InlinedContext; |
| 161 | } |
| 162 | |
| 163 | bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name, |
| 164 | uint64_t &Start, uint64_t &Size) const { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 165 | return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start, |
| 166 | Size); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Alexey Samsonov | d6cef10 | 2013-02-04 15:55:26 +0000 | [diff] [blame] | 169 | const char LLVMSymbolizer::kBadString[] = "??"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 170 | |
| 171 | std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 172 | uint64_t ModuleOffset) { |
| 173 | ModuleInfo *Info = getOrCreateModuleInfo(ModuleName); |
| 174 | if (Info == 0) |
| 175 | return printDILineInfo(DILineInfo()); |
| 176 | if (Opts.PrintInlining) { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 177 | DIInliningInfo InlinedContext = |
| 178 | Info->symbolizeInlinedCode(ModuleOffset, Opts); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 179 | uint32_t FramesNum = InlinedContext.getNumberOfFrames(); |
| 180 | assert(FramesNum > 0); |
| 181 | std::string Result; |
| 182 | for (uint32_t i = 0; i < FramesNum; i++) { |
| 183 | DILineInfo LineInfo = InlinedContext.getFrame(i); |
| 184 | Result += printDILineInfo(LineInfo); |
| 185 | } |
| 186 | return Result; |
| 187 | } |
| 188 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts); |
| 189 | return printDILineInfo(LineInfo); |
| 190 | } |
| 191 | |
| 192 | std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 193 | uint64_t ModuleOffset) { |
| 194 | std::string Name = kBadString; |
| 195 | uint64_t Start = 0; |
| 196 | uint64_t Size = 0; |
| 197 | if (Opts.UseSymbolTable) { |
| 198 | if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) { |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 199 | if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 200 | Name = DemangleName(Name); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | std::stringstream ss; |
| 204 | ss << Name << "\n" << Start << " " << Size << "\n"; |
| 205 | return ss.str(); |
| 206 | } |
| 207 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 208 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 209 | DeleteContainerSeconds(Modules); |
Alexey Samsonov | fe3a5d9 | 2013-06-28 15:08:29 +0000 | [diff] [blame] | 210 | BinaryForPath.clear(); |
| 211 | ObjectFileForArch.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 214 | static std::string getDarwinDWARFResourceForPath(const std::string &Path) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 215 | StringRef Basename = sys::path::filename(Path); |
| 216 | const std::string &DSymDirectory = Path + ".dSYM"; |
| 217 | SmallString<16> ResourceName = StringRef(DSymDirectory); |
| 218 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 219 | sys::path::append(ResourceName, Basename); |
| 220 | return ResourceName.str(); |
| 221 | } |
| 222 | |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 223 | static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 224 | std::unique_ptr<MemoryBuffer> MB; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 225 | if (MemoryBuffer::getFileOrSTDIN(Path, MB)) |
| 226 | return false; |
| 227 | return !zlib::isAvailable() || CRCHash == zlib::crc32(MB->getBuffer()); |
| 228 | } |
| 229 | |
| 230 | static bool findDebugBinary(const std::string &OrigPath, |
| 231 | const std::string &DebuglinkName, uint32_t CRCHash, |
| 232 | std::string &Result) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 233 | std::string OrigRealPath = OrigPath; |
| 234 | #if defined(HAVE_REALPATH) |
| 235 | if (char *RP = realpath(OrigPath.c_str(), NULL)) { |
| 236 | OrigRealPath = RP; |
| 237 | free(RP); |
| 238 | } |
| 239 | #endif |
| 240 | SmallString<16> OrigDir(OrigRealPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 241 | llvm::sys::path::remove_filename(OrigDir); |
| 242 | SmallString<16> DebugPath = OrigDir; |
| 243 | // Try /path/to/original_binary/debuglink_name |
| 244 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 245 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 246 | Result = DebugPath.str(); |
| 247 | return true; |
| 248 | } |
| 249 | // Try /path/to/original_binary/.debug/debuglink_name |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 250 | DebugPath = OrigRealPath; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 251 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 252 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 253 | Result = DebugPath.str(); |
| 254 | return true; |
| 255 | } |
| 256 | // Try /usr/lib/debug/path/to/original_binary/debuglink_name |
| 257 | DebugPath = "/usr/lib/debug"; |
| 258 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 259 | DebuglinkName); |
| 260 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 261 | Result = DebugPath.str(); |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | static bool getGNUDebuglinkContents(const Binary *Bin, std::string &DebugName, |
| 268 | uint32_t &CRCHash) { |
| 269 | const ObjectFile *Obj = dyn_cast<ObjectFile>(Bin); |
| 270 | if (!Obj) |
| 271 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 272 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 273 | StringRef Name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 274 | Section.getName(Name); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 275 | Name = Name.substr(Name.find_first_not_of("._")); |
| 276 | if (Name == "gnu_debuglink") { |
| 277 | StringRef Data; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 278 | Section.getContents(Data); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 279 | DataExtractor DE(Data, Obj->isLittleEndian(), 0); |
| 280 | uint32_t Offset = 0; |
| 281 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 282 | // 4-byte align the offset. |
| 283 | Offset = (Offset + 3) & ~0x3; |
| 284 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 285 | DebugName = DebugNameStr; |
| 286 | CRCHash = DE.getU32(&Offset); |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 296 | LLVMSymbolizer::BinaryPair |
| 297 | LLVMSymbolizer::getOrCreateBinary(const std::string &Path) { |
| 298 | BinaryMapTy::iterator I = BinaryForPath.find(Path); |
| 299 | if (I != BinaryForPath.end()) |
| 300 | return I->second; |
| 301 | Binary *Bin = 0; |
| 302 | Binary *DbgBin = 0; |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 303 | ErrorOr<Binary *> BinaryOrErr = createBinary(Path); |
| 304 | if (!error(BinaryOrErr.getError())) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 305 | std::unique_ptr<Binary> ParsedBinary(BinaryOrErr.get()); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 306 | // Check if it's a universal binary. |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 307 | Bin = ParsedBinary.get(); |
| 308 | ParsedBinariesAndObjects.push_back(std::move(ParsedBinary)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 309 | if (Bin->isMachO() || Bin->isMachOUniversalBinary()) { |
| 310 | // On Darwin we may find DWARF in separate object file in |
| 311 | // resource directory. |
| 312 | const std::string &ResourcePath = |
| 313 | getDarwinDWARFResourceForPath(Path); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 314 | BinaryOrErr = createBinary(ResourcePath); |
| 315 | error_code EC = BinaryOrErr.getError(); |
Rafael Espindola | 5cf5210 | 2014-01-15 04:49:50 +0000 | [diff] [blame] | 316 | if (EC != errc::no_such_file_or_directory && !error(EC)) { |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 317 | DbgBin = BinaryOrErr.get(); |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 318 | ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 321 | // Try to locate the debug binary using .gnu_debuglink section. |
| 322 | if (DbgBin == 0) { |
| 323 | std::string DebuglinkName; |
| 324 | uint32_t CRCHash; |
| 325 | std::string DebugBinaryPath; |
| 326 | if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) && |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 327 | findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) { |
| 328 | BinaryOrErr = createBinary(DebugBinaryPath); |
| 329 | if (!error(BinaryOrErr.getError())) { |
| 330 | DbgBin = BinaryOrErr.get(); |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 331 | ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin)); |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 332 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 335 | } |
| 336 | if (DbgBin == 0) |
| 337 | DbgBin = Bin; |
| 338 | BinaryPair Res = std::make_pair(Bin, DbgBin); |
| 339 | BinaryForPath[Path] = Res; |
| 340 | return Res; |
| 341 | } |
| 342 | |
| 343 | ObjectFile * |
| 344 | LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) { |
| 345 | if (Bin == 0) |
| 346 | return 0; |
| 347 | ObjectFile *Res = 0; |
| 348 | if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) { |
| 349 | ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find( |
| 350 | std::make_pair(UB, ArchName)); |
| 351 | if (I != ObjectFileForArch.end()) |
| 352 | return I->second; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 353 | std::unique_ptr<ObjectFile> ParsedObj; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 354 | if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) { |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 355 | Res = ParsedObj.get(); |
| 356 | ParsedBinariesAndObjects.push_back(std::move(ParsedObj)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 357 | } |
| 358 | ObjectFileForArch[std::make_pair(UB, ArchName)] = Res; |
| 359 | } else if (Bin->isObject()) { |
| 360 | Res = cast<ObjectFile>(Bin); |
| 361 | } |
| 362 | return Res; |
| 363 | } |
| 364 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 365 | ModuleInfo * |
| 366 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 367 | ModuleMapTy::iterator I = Modules.find(ModuleName); |
| 368 | if (I != Modules.end()) |
| 369 | return I->second; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 370 | std::string BinaryName = ModuleName; |
| 371 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 372 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 373 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 374 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 375 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 376 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 377 | BinaryName = ModuleName.substr(0, ColonPos); |
| 378 | ArchName = ArchStr; |
| 379 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 380 | } |
| 381 | BinaryPair Binaries = getOrCreateBinary(BinaryName); |
| 382 | ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName); |
| 383 | ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 384 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 385 | if (Obj == 0) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 386 | // Failed to find valid object file. |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 387 | Modules.insert(make_pair(ModuleName, (ModuleInfo *)0)); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 388 | return 0; |
| 389 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 390 | DIContext *Context = DIContext::getDWARFContext(DbgObj); |
| 391 | assert(Context); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 392 | ModuleInfo *Info = new ModuleInfo(Obj, Context); |
| 393 | Modules.insert(make_pair(ModuleName, Info)); |
| 394 | return Info; |
| 395 | } |
| 396 | |
| 397 | std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const { |
| 398 | // By default, DILineInfo contains "<invalid>" for function/filename it |
| 399 | // cannot fetch. We replace it to "??" to make our output closer to addr2line. |
| 400 | static const std::string kDILineInfoBadString = "<invalid>"; |
| 401 | std::stringstream Result; |
| 402 | if (Opts.PrintFunctions) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 403 | std::string FunctionName = LineInfo.FunctionName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 404 | if (FunctionName == kDILineInfoBadString) |
| 405 | FunctionName = kBadString; |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 406 | else if (Opts.Demangle) |
| 407 | FunctionName = DemangleName(FunctionName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 408 | Result << FunctionName << "\n"; |
| 409 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 410 | std::string Filename = LineInfo.FileName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 411 | if (Filename == kDILineInfoBadString) |
| 412 | Filename = kBadString; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 413 | Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 414 | return Result.str(); |
| 415 | } |
| 416 | |
| 417 | #if !defined(_MSC_VER) |
| 418 | // Assume that __cxa_demangle is provided by libcxxabi (except for Windows). |
| 419 | extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer, |
| 420 | size_t *length, int *status); |
| 421 | #endif |
| 422 | |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 423 | std::string LLVMSymbolizer::DemangleName(const std::string &Name) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 424 | #if !defined(_MSC_VER) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 425 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 426 | // approach to check if the name should be demangled. |
| 427 | if (Name.substr(0, 2) != "_Z") |
| 428 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 429 | int status = 0; |
| 430 | char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status); |
| 431 | if (status != 0) |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 432 | return Name; |
| 433 | std::string Result = DemangledName; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 434 | free(DemangledName); |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 435 | return Result; |
| 436 | #else |
| 437 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 438 | #endif |
| 439 | } |
| 440 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 441 | } // namespace symbolize |
| 442 | } // namespace llvm |