blob: ebbae39a57aa170d3e55d2f267aed6c644e12e84 [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.
88 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
89 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 {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000096 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov5239d582013-06-04 07:57:38 +000097 if (M.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000098 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +000099 SymbolDesc SD = { Address, Address };
100 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000101 if (it == M.begin())
102 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000103 --it;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000104 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000105 return false;
106 Name = it->second.str();
107 Addr = it->first.Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000108 Size = it->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000109 return true;
110}
111
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000112DILineInfo ModuleInfo::symbolizeCode(
113 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000114 DILineInfo LineInfo;
115 if (DebugInfoContext) {
116 LineInfo = DebugInfoContext->getLineInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000117 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000118 }
119 // Override function name from symbol table if necessary.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000120 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000121 std::string FunctionName;
122 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000123 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
124 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000125 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000126 }
127 }
128 return LineInfo;
129}
130
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000131DIInliningInfo ModuleInfo::symbolizeInlinedCode(
132 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000133 DIInliningInfo InlinedContext;
134 if (DebugInfoContext) {
135 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000136 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000137 }
138 // Make sure there is at least one frame in context.
139 if (InlinedContext.getNumberOfFrames() == 0) {
140 InlinedContext.addFrame(DILineInfo());
141 }
142 // Override the function name in lower frame with name from symbol table.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000143 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000144 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000145 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000146 DILineInfo LineInfo = InlinedContext.getFrame(i);
147 if (i == n - 1) {
148 std::string FunctionName;
149 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000150 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
151 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000152 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000153 }
154 }
155 PatchedInlinedContext.addFrame(LineInfo);
156 }
157 InlinedContext = PatchedInlinedContext;
158 }
159 return InlinedContext;
160}
161
162bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
163 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000164 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
165 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000166}
167
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000168const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000169
170std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
171 uint64_t ModuleOffset) {
172 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000173 if (!Info)
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000174 return printDILineInfo(DILineInfo());
175 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000176 DIInliningInfo InlinedContext =
177 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000178 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
179 assert(FramesNum > 0);
180 std::string Result;
181 for (uint32_t i = 0; i < FramesNum; i++) {
182 DILineInfo LineInfo = InlinedContext.getFrame(i);
183 Result += printDILineInfo(LineInfo);
184 }
185 return Result;
186 }
187 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
188 return printDILineInfo(LineInfo);
189}
190
191std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
192 uint64_t ModuleOffset) {
193 std::string Name = kBadString;
194 uint64_t Start = 0;
195 uint64_t Size = 0;
196 if (Opts.UseSymbolTable) {
197 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonov601beb72013-06-28 12:06:25 +0000198 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000199 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000200 }
201 }
202 std::stringstream ss;
203 ss << Name << "\n" << Start << " " << Size << "\n";
204 return ss.str();
205}
206
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000207void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000208 DeleteContainerSeconds(Modules);
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000209 BinaryForPath.clear();
210 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000211}
212
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000213static std::string getDarwinDWARFResourceForPath(const std::string &Path) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000214 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 Samsonov3e9997f2013-08-14 17:09:30 +0000222static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000223 std::unique_ptr<MemoryBuffer> MB;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000224 if (MemoryBuffer::getFileOrSTDIN(Path, MB))
225 return false;
226 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB->getBuffer());
227}
228
229static bool findDebugBinary(const std::string &OrigPath,
230 const std::string &DebuglinkName, uint32_t CRCHash,
231 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000232 std::string OrigRealPath = OrigPath;
233#if defined(HAVE_REALPATH)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000234 if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000235 OrigRealPath = RP;
236 free(RP);
237 }
238#endif
239 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000240 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 Samsonova591ae62013-08-26 18:12:03 +0000249 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000250 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
266static 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;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000271 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000272 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000273 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000274 Name = Name.substr(Name.find_first_not_of("._"));
275 if (Name == "gnu_debuglink") {
276 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000277 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000278 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
279 uint32_t Offset = 0;
280 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
281 // 4-byte align the offset.
282 Offset = (Offset + 3) & ~0x3;
283 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
284 DebugName = DebugNameStr;
285 CRCHash = DE.getU32(&Offset);
286 return true;
287 }
288 }
289 break;
290 }
291 }
292 return false;
293}
294
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000295LLVMSymbolizer::BinaryPair
296LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
297 BinaryMapTy::iterator I = BinaryForPath.find(Path);
298 if (I != BinaryForPath.end())
299 return I->second;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000300 Binary *Bin = nullptr;
301 Binary *DbgBin = nullptr;
Rafael Espindola63da2952014-01-15 19:37:43 +0000302 ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
303 if (!error(BinaryOrErr.getError())) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000304 std::unique_ptr<Binary> ParsedBinary(BinaryOrErr.get());
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000305 // Check if it's a universal binary.
David Blaikie2f2021a2014-04-22 05:26:14 +0000306 Bin = ParsedBinary.get();
307 ParsedBinariesAndObjects.push_back(std::move(ParsedBinary));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000308 if (Bin->isMachO() || Bin->isMachOUniversalBinary()) {
309 // On Darwin we may find DWARF in separate object file in
310 // resource directory.
311 const std::string &ResourcePath =
312 getDarwinDWARFResourceForPath(Path);
Rafael Espindola63da2952014-01-15 19:37:43 +0000313 BinaryOrErr = createBinary(ResourcePath);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000314 std::error_code EC = BinaryOrErr.getError();
Rafael Espindola2a826e42014-06-13 17:20:48 +0000315 if (EC != errc::no_such_file_or_directory && !error(EC)) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000316 DbgBin = BinaryOrErr.get();
David Blaikie2f2021a2014-04-22 05:26:14 +0000317 ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000318 }
319 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000320 // Try to locate the debug binary using .gnu_debuglink section.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000321 if (!DbgBin) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000322 std::string DebuglinkName;
323 uint32_t CRCHash;
324 std::string DebugBinaryPath;
325 if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000326 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
327 BinaryOrErr = createBinary(DebugBinaryPath);
328 if (!error(BinaryOrErr.getError())) {
329 DbgBin = BinaryOrErr.get();
David Blaikie2f2021a2014-04-22 05:26:14 +0000330 ParsedBinariesAndObjects.push_back(std::unique_ptr<Binary>(DbgBin));
Rafael Espindola63da2952014-01-15 19:37:43 +0000331 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000332 }
333 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000334 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000335 if (!DbgBin)
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000336 DbgBin = Bin;
337 BinaryPair Res = std::make_pair(Bin, DbgBin);
338 BinaryForPath[Path] = Res;
339 return Res;
340}
341
342ObjectFile *
343LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000344 if (!Bin)
345 return nullptr;
346 ObjectFile *Res = nullptr;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000347 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
348 ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find(
349 std::make_pair(UB, ArchName));
350 if (I != ObjectFileForArch.end())
351 return I->second;
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000352 ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
353 UB->getObjectForArch(Triple(ArchName).getArch());
354 if (ParsedObj) {
355 Res = ParsedObj.get().get();
356 ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000357 }
358 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
359 } else if (Bin->isObject()) {
360 Res = cast<ObjectFile>(Bin);
361 }
362 return Res;
363}
364
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000365ModuleInfo *
366LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000367 ModuleMapTy::iterator I = Modules.find(ModuleName);
368 if (I != Modules.end())
369 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000370 std::string BinaryName = ModuleName;
371 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000372 size_t ColonPos = ModuleName.find_last_of(':');
373 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000374 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000375 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000376 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000377 BinaryName = ModuleName.substr(0, ColonPos);
378 ArchName = ArchStr;
379 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000380 }
381 BinaryPair Binaries = getOrCreateBinary(BinaryName);
382 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
383 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000384
Craig Toppere6cb63e2014-04-25 04:24:47 +0000385 if (!Obj) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000386 // Failed to find valid object file.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000387 Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
388 return nullptr;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000389 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000390 DIContext *Context = DIContext::getDWARFContext(DbgObj);
391 assert(Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000392 ModuleInfo *Info = new ModuleInfo(Obj, Context);
393 Modules.insert(make_pair(ModuleName, Info));
394 return Info;
395}
396
397std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
398 // By default, DILineInfo contains "<invalid>" for function/filename it
399 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
400 static const std::string kDILineInfoBadString = "<invalid>";
401 std::stringstream Result;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000402 if (Opts.PrintFunctions != FunctionNameKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000403 std::string FunctionName = LineInfo.FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000404 if (FunctionName == kDILineInfoBadString)
405 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000406 else if (Opts.Demangle)
407 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000408 Result << FunctionName << "\n";
409 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000410 std::string Filename = LineInfo.FileName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000411 if (Filename == kDILineInfoBadString)
412 Filename = kBadString;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000413 Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000414 return Result.str();
415}
416
417#if !defined(_MSC_VER)
418// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
419extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
420 size_t *length, int *status);
421#endif
422
Alexey Samsonov601beb72013-06-28 12:06:25 +0000423std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000424#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000425 // We can spoil names of symbols with C linkage, so use an heuristic
426 // approach to check if the name should be demangled.
427 if (Name.substr(0, 2) != "_Z")
428 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000429 int status = 0;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000430 char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000431 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000432 return Name;
433 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000434 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000435 return Result;
436#else
437 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000438#endif
439}
440
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000441} // namespace symbolize
442} // namespace llvm