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