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