blob: 31bbedf8f032b58e1d4ef34a8a99739b975c6e2e [file] [log] [blame]
Alexey Samsonovea83baf2013-01-22 14:21:19 +00001//===-- 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 Samsonovdd71c5b2013-03-19 15:33:18 +000015#include "llvm/ADT/STLExtras.h"
Alexey Samsonova591ae62013-08-26 18:12:03 +000016#include "llvm/Config/config.h"
Alexey Samsonova5f07682014-02-26 13:10:01 +000017#include "llvm/Object/ELFObjectFile.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000018#include "llvm/Object/MachO.h"
19#include "llvm/Support/Casting.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000020#include "llvm/Support/Compression.h"
21#include "llvm/Support/DataExtractor.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000022#include "llvm/Support/Errc.h"
Alexey Samsonov5239d582013-06-04 07:57:38 +000023#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000024#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000025#include "llvm/Support/Path.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000026#include <sstream>
Alexey Samsonova591ae62013-08-26 18:12:03 +000027#include <stdlib.h>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000028
29namespace llvm {
30namespace symbolize {
31
Rafael Espindola4453e42942014-06-13 03:07:50 +000032static bool error(std::error_code ec) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000033 if (!ec)
34 return false;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000035 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
36 return true;
37}
38
Alexey Samsonovdce67342014-05-15 21:24:32 +000039static DILineInfoSpecifier
40getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
41 return DILineInfoSpecifier(
42 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
Alexey Samsonovcd014722014-05-17 00:07:48 +000043 Opts.PrintFunctions);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000044}
45
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000046ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000047 : Module(Obj), DebugInfoContext(DICtx) {
Alexey Samsonov464d2e42014-03-17 07:28:19 +000048 for (const SymbolRef &Symbol : Module->symbols()) {
49 addSymbol(Symbol);
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000050 }
Alexey Samsonova5f07682014-02-26 13:10:01 +000051 bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
52 if (NoSymbolTable && Module->isELF()) {
53 // Fallback to dynamic symbol table, if regular symbol table is stripped.
54 std::pair<symbol_iterator, symbol_iterator> IDyn =
55 getELFDynamicSymbolIterators(Module);
56 for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
Alexey Samsonov464d2e42014-03-17 07:28:19 +000057 addSymbol(*si);
Alexey Samsonova5f07682014-02-26 13:10:01 +000058 }
59 }
60}
61
Alexey Samsonov464d2e42014-03-17 07:28:19 +000062void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
Alexey Samsonova5f07682014-02-26 13:10:01 +000063 SymbolRef::Type SymbolType;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000064 if (error(Symbol.getType(SymbolType)))
Alexey Samsonova5f07682014-02-26 13:10:01 +000065 return;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000066 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
Alexey Samsonova5f07682014-02-26 13:10:01 +000067 return;
68 uint64_t SymbolAddress;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000069 if (error(Symbol.getAddress(SymbolAddress)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +000070 SymbolAddress == UnknownAddressOrSize)
71 return;
72 uint64_t SymbolSize;
73 // Getting symbol size is linear for Mach-O files, so assume that symbol
74 // occupies the memory range up to the following symbol.
75 if (isa<MachOObjectFile>(Module))
76 SymbolSize = 0;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000077 else if (error(Symbol.getSize(SymbolSize)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +000078 SymbolSize == UnknownAddressOrSize)
79 return;
80 StringRef SymbolName;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000081 if (error(Symbol.getName(SymbolName)))
Alexey Samsonova5f07682014-02-26 13:10:01 +000082 return;
83 // Mach-O symbol table names have leading underscore, skip it.
84 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
85 SymbolName = SymbolName.drop_front();
86 // FIXME: If a function has alias, there are two entries in symbol table
87 // with same address size. Make sure we choose the correct one.
Alexander Potapenko45bfe372014-10-14 13:40:44 +000088 auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonova5f07682014-02-26 13:10:01 +000089 SymbolDesc SD = { SymbolAddress, SymbolSize };
90 M.insert(std::make_pair(SD, SymbolName));
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000091}
92
93bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
94 std::string &Name, uint64_t &Addr,
95 uint64_t &Size) const {
Alexander Potapenko45bfe372014-10-14 13:40:44 +000096 const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
97 if (SymbolMap.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000098 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +000099 SymbolDesc SD = { Address, Address };
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000100 auto SymbolIterator = SymbolMap.upper_bound(SD);
101 if (SymbolIterator == SymbolMap.begin())
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000102 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000103 --SymbolIterator;
104 if (SymbolIterator->first.Size != 0 &&
105 SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000106 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000107 Name = SymbolIterator->second.str();
108 Addr = SymbolIterator->first.Addr;
109 Size = SymbolIterator->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000110 return true;
111}
112
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000113DILineInfo ModuleInfo::symbolizeCode(
114 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000115 DILineInfo LineInfo;
116 if (DebugInfoContext) {
117 LineInfo = DebugInfoContext->getLineInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000118 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000119 }
120 // Override function name from symbol table if necessary.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000121 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000122 std::string FunctionName;
123 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000124 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
125 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000126 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000127 }
128 }
129 return LineInfo;
130}
131
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000132DIInliningInfo ModuleInfo::symbolizeInlinedCode(
133 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000134 DIInliningInfo InlinedContext;
135 if (DebugInfoContext) {
136 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000137 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000138 }
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.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000144 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000145 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000146 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000147 DILineInfo LineInfo = InlinedContext.getFrame(i);
148 if (i == n - 1) {
149 std::string FunctionName;
150 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000151 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
152 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000153 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000154 }
155 }
156 PatchedInlinedContext.addFrame(LineInfo);
157 }
158 InlinedContext = PatchedInlinedContext;
159 }
160 return InlinedContext;
161}
162
163bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
164 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000165 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
166 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000167}
168
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000169const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000170
171std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
172 uint64_t ModuleOffset) {
173 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000174 if (!Info)
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000175 return printDILineInfo(DILineInfo());
176 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000177 DIInliningInfo InlinedContext =
178 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000179 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
192std::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 Samsonov601beb72013-06-28 12:06:25 +0000199 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000200 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000201 }
202 }
203 std::stringstream ss;
204 ss << Name << "\n" << Start << " " << Size << "\n";
205 return ss.str();
206}
207
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000208void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000209 DeleteContainerSeconds(Modules);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000210 ObjectPairForPathArch.clear();
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000211 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000212}
213
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000214// For Path="/path/to/foo" and Basename="foo" assume that debug info is in
215// /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
216// For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
217// /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
218static
219std::string getDarwinDWARFResourceForPath(
220 const std::string &Path, const std::string &Basename) {
221 SmallString<16> ResourceName = StringRef(Path);
222 if (sys::path::extension(Path) != ".dSYM") {
223 ResourceName += ".dSYM";
224 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000225 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
226 sys::path::append(ResourceName, Basename);
227 return ResourceName.str();
228}
229
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000230static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000231 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
232 MemoryBuffer::getFileOrSTDIN(Path);
233 if (!MB)
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000234 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000235 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000236}
237
238static bool findDebugBinary(const std::string &OrigPath,
239 const std::string &DebuglinkName, uint32_t CRCHash,
240 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000241 std::string OrigRealPath = OrigPath;
242#if defined(HAVE_REALPATH)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000243 if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000244 OrigRealPath = RP;
245 free(RP);
246 }
247#endif
248 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000249 llvm::sys::path::remove_filename(OrigDir);
250 SmallString<16> DebugPath = OrigDir;
251 // Try /path/to/original_binary/debuglink_name
252 llvm::sys::path::append(DebugPath, DebuglinkName);
253 if (checkFileCRC(DebugPath, CRCHash)) {
254 Result = DebugPath.str();
255 return true;
256 }
257 // Try /path/to/original_binary/.debug/debuglink_name
Alexey Samsonova591ae62013-08-26 18:12:03 +0000258 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000259 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
260 if (checkFileCRC(DebugPath, CRCHash)) {
261 Result = DebugPath.str();
262 return true;
263 }
264 // Try /usr/lib/debug/path/to/original_binary/debuglink_name
265 DebugPath = "/usr/lib/debug";
266 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
267 DebuglinkName);
268 if (checkFileCRC(DebugPath, CRCHash)) {
269 Result = DebugPath.str();
270 return true;
271 }
272 return false;
273}
274
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000275static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000276 uint32_t &CRCHash) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000277 if (!Obj)
278 return false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000279 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000280 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000281 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000282 Name = Name.substr(Name.find_first_not_of("._"));
283 if (Name == "gnu_debuglink") {
284 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000285 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000286 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
287 uint32_t Offset = 0;
288 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
289 // 4-byte align the offset.
290 Offset = (Offset + 3) & ~0x3;
291 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
292 DebugName = DebugNameStr;
293 CRCHash = DE.getU32(&Offset);
294 return true;
295 }
296 }
297 break;
298 }
299 }
300 return false;
301}
302
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000303static
304bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
305 const MachOObjectFile *Obj) {
306 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
307 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
308 if (dbg_uuid.empty() || bin_uuid.empty())
309 return false;
310 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
311}
312
313ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
314 const MachOObjectFile *MachExeObj, const std::string &ArchName) {
315 // On Darwin we may find DWARF in separate object file in
316 // resource directory.
317 std::vector<std::string> DsymPaths;
318 StringRef Filename = sys::path::filename(ExePath);
319 DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
320 for (const auto &Path : Opts.DsymHints) {
321 DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
322 }
323 for (const auto &path : DsymPaths) {
324 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(path);
325 std::error_code EC = BinaryOrErr.getError();
326 if (EC != errc::no_such_file_or_directory && !error(EC)) {
327 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
328 ObjectFile *DbgObj =
329 getObjectFileFromBinary(B.getBinary().get(), ArchName);
330 const MachOObjectFile *MachDbgObj =
331 dyn_cast<const MachOObjectFile>(DbgObj);
332 if (!MachDbgObj) continue;
333 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000334 addOwningBinary(std::move(B));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000335 return DbgObj;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000336 }
337 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000338 }
339 return nullptr;
340}
341
342LLVMSymbolizer::ObjectPair
343LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
344 const std::string &ArchName) {
345 const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
346 if (I != ObjectPairForPathArch.end())
347 return I->second;
348 ObjectFile *Obj = nullptr;
349 ObjectFile *DbgObj = nullptr;
350 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
351 if (!error(BinaryOrErr.getError())) {
352 OwningBinary<Binary> &B = BinaryOrErr.get();
353 Obj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
354 if (!Obj) {
355 ObjectPair Res = std::make_pair(nullptr, nullptr);
356 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
357 return Res;
358 }
359 addOwningBinary(std::move(B));
360 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
361 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000362 // Try to locate the debug binary using .gnu_debuglink section.
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000363 if (!DbgObj) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000364 std::string DebuglinkName;
365 uint32_t CRCHash;
366 std::string DebugBinaryPath;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000367 if (getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000368 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
369 BinaryOrErr = createBinary(DebugBinaryPath);
370 if (!error(BinaryOrErr.getError())) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000371 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000372 DbgObj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
Rafael Espindola48af1c22014-08-19 18:44:46 +0000373 addOwningBinary(std::move(B));
Rafael Espindola63da2952014-01-15 19:37:43 +0000374 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000375 }
376 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000377 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000378 if (!DbgObj)
379 DbgObj = Obj;
380 ObjectPair Res = std::make_pair(Obj, DbgObj);
381 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000382 return Res;
383}
384
385ObjectFile *
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000386LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin,
387 const std::string &ArchName) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000388 if (!Bin)
389 return nullptr;
390 ObjectFile *Res = nullptr;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000391 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000392 const auto &I = ObjectFileForArch.find(
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000393 std::make_pair(UB, ArchName));
394 if (I != ObjectFileForArch.end())
395 return I->second;
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000396 ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
397 UB->getObjectForArch(Triple(ArchName).getArch());
398 if (ParsedObj) {
399 Res = ParsedObj.get().get();
400 ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000401 }
402 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
403 } else if (Bin->isObject()) {
404 Res = cast<ObjectFile>(Bin);
405 }
406 return Res;
407}
408
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000409ModuleInfo *
410LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000411 const auto &I = Modules.find(ModuleName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000412 if (I != Modules.end())
413 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000414 std::string BinaryName = ModuleName;
415 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000416 size_t ColonPos = ModuleName.find_last_of(':');
417 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000418 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000419 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000420 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000421 BinaryName = ModuleName.substr(0, ColonPos);
422 ArchName = ArchStr;
423 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000424 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000425 ObjectPair Objects = getOrCreateObjects(BinaryName, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000426
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000427 if (!Objects.first) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000428 // Failed to find valid object file.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000429 Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
430 return nullptr;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000431 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000432 DIContext *Context = DIContext::getDWARFContext(*Objects.second);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000433 assert(Context);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000434 ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000435 Modules.insert(make_pair(ModuleName, Info));
436 return Info;
437}
438
439std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
440 // By default, DILineInfo contains "<invalid>" for function/filename it
441 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
442 static const std::string kDILineInfoBadString = "<invalid>";
443 std::stringstream Result;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000444 if (Opts.PrintFunctions != FunctionNameKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000445 std::string FunctionName = LineInfo.FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000446 if (FunctionName == kDILineInfoBadString)
447 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000448 else if (Opts.Demangle)
449 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000450 Result << FunctionName << "\n";
451 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000452 std::string Filename = LineInfo.FileName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000453 if (Filename == kDILineInfoBadString)
454 Filename = kBadString;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000455 Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000456 return Result.str();
457}
458
459#if !defined(_MSC_VER)
460// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
461extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
462 size_t *length, int *status);
463#endif
464
Alexey Samsonov601beb72013-06-28 12:06:25 +0000465std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000466#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000467 // We can spoil names of symbols with C linkage, so use an heuristic
468 // approach to check if the name should be demangled.
469 if (Name.substr(0, 2) != "_Z")
470 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000471 int status = 0;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000472 char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000473 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000474 return Name;
475 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000476 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000477 return Result;
478#else
479 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000480#endif
481}
482
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000483} // namespace symbolize
484} // namespace llvm