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