blob: c1a0654e87f4d6533c602b1514887540d8ce5a54 [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"
Alexey Samsonov5239d582013-06-04 07:57:38 +000022#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000023#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000024#include "llvm/Support/Path.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000025#include <sstream>
Alexey Samsonova591ae62013-08-26 18:12:03 +000026#include <stdlib.h>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000027
28namespace llvm {
29namespace symbolize {
30
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000031static bool error(error_code ec) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000032 if (!ec)
33 return false;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000034 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
35 return true;
36}
37
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000038static uint32_t
39getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +000040 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
41 llvm::DILineInfoSpecifier::AbsoluteFilePath;
42 if (Opts.PrintFunctions)
43 Flags |= llvm::DILineInfoSpecifier::FunctionName;
44 return Flags;
45}
46
47static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
48 DILineInfo &LineInfo) {
49 std::string FileName = LineInfo.getFileName();
50 LineInfo = DILineInfo(StringRef(FileName), StringRef(NewFunctionName),
51 LineInfo.getLine(), LineInfo.getColumn());
52}
53
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000054ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000055 : Module(Obj), DebugInfoContext(DICtx) {
Rafael Espindolab5155a52014-02-10 20:24:04 +000056 for (symbol_iterator si = Module->symbol_begin(), se = Module->symbol_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +000057 si != se; ++si) {
Alexey Samsonova5f07682014-02-26 13:10:01 +000058 addSymbol(si);
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000059 }
Alexey Samsonova5f07682014-02-26 13:10:01 +000060 bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
61 if (NoSymbolTable && Module->isELF()) {
62 // Fallback to dynamic symbol table, if regular symbol table is stripped.
63 std::pair<symbol_iterator, symbol_iterator> IDyn =
64 getELFDynamicSymbolIterators(Module);
65 for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
66 addSymbol(si);
67 }
68 }
69}
70
71void ModuleInfo::addSymbol(const symbol_iterator &Sym) {
72 SymbolRef::Type SymbolType;
73 if (error(Sym->getType(SymbolType)))
74 return;
75 if (SymbolType != SymbolRef::ST_Function &&
76 SymbolType != SymbolRef::ST_Data)
77 return;
78 uint64_t SymbolAddress;
79 if (error(Sym->getAddress(SymbolAddress)) ||
80 SymbolAddress == UnknownAddressOrSize)
81 return;
82 uint64_t SymbolSize;
83 // Getting symbol size is linear for Mach-O files, so assume that symbol
84 // occupies the memory range up to the following symbol.
85 if (isa<MachOObjectFile>(Module))
86 SymbolSize = 0;
87 else if (error(Sym->getSize(SymbolSize)) ||
88 SymbolSize == UnknownAddressOrSize)
89 return;
90 StringRef SymbolName;
91 if (error(Sym->getName(SymbolName)))
92 return;
93 // Mach-O symbol table names have leading underscore, skip it.
94 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
95 SymbolName = SymbolName.drop_front();
96 // FIXME: If a function has alias, there are two entries in symbol table
97 // with same address size. Make sure we choose the correct one.
98 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
99 SymbolDesc SD = { SymbolAddress, SymbolSize };
100 M.insert(std::make_pair(SD, SymbolName));
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000101}
102
103bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
104 std::string &Name, uint64_t &Addr,
105 uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000106 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000107 if (M.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000108 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000109 SymbolDesc SD = { Address, Address };
110 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000111 if (it == M.begin())
112 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000113 --it;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000114 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000115 return false;
116 Name = it->second.str();
117 Addr = it->first.Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000118 Size = it->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000119 return true;
120}
121
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000122DILineInfo ModuleInfo::symbolizeCode(
123 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000124 DILineInfo LineInfo;
125 if (DebugInfoContext) {
126 LineInfo = DebugInfoContext->getLineInfoForAddress(
127 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
128 }
129 // Override function name from symbol table if necessary.
130 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
131 std::string FunctionName;
132 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000133 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
134 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000135 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
136 }
137 }
138 return LineInfo;
139}
140
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000141DIInliningInfo ModuleInfo::symbolizeInlinedCode(
142 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000143 DIInliningInfo InlinedContext;
144 if (DebugInfoContext) {
145 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
146 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
147 }
148 // Make sure there is at least one frame in context.
149 if (InlinedContext.getNumberOfFrames() == 0) {
150 InlinedContext.addFrame(DILineInfo());
151 }
152 // Override the function name in lower frame with name from symbol table.
153 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
154 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000155 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000156 DILineInfo LineInfo = InlinedContext.getFrame(i);
157 if (i == n - 1) {
158 std::string FunctionName;
159 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000160 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
161 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000162 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
163 }
164 }
165 PatchedInlinedContext.addFrame(LineInfo);
166 }
167 InlinedContext = PatchedInlinedContext;
168 }
169 return InlinedContext;
170}
171
172bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
173 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000174 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
175 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000176}
177
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000178const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000179
180std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
181 uint64_t ModuleOffset) {
182 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
183 if (Info == 0)
184 return printDILineInfo(DILineInfo());
185 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000186 DIInliningInfo InlinedContext =
187 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000188 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
189 assert(FramesNum > 0);
190 std::string Result;
191 for (uint32_t i = 0; i < FramesNum; i++) {
192 DILineInfo LineInfo = InlinedContext.getFrame(i);
193 Result += printDILineInfo(LineInfo);
194 }
195 return Result;
196 }
197 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
198 return printDILineInfo(LineInfo);
199}
200
201std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
202 uint64_t ModuleOffset) {
203 std::string Name = kBadString;
204 uint64_t Start = 0;
205 uint64_t Size = 0;
206 if (Opts.UseSymbolTable) {
207 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonov601beb72013-06-28 12:06:25 +0000208 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000209 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000210 }
211 }
212 std::stringstream ss;
213 ss << Name << "\n" << Start << " " << Size << "\n";
214 return ss.str();
215}
216
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000217void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000218 DeleteContainerSeconds(Modules);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000219 DeleteContainerPointers(ParsedBinariesAndObjects);
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000220 BinaryForPath.clear();
221 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000222}
223
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000224static std::string getDarwinDWARFResourceForPath(const std::string &Path) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000225 StringRef Basename = sys::path::filename(Path);
226 const std::string &DSymDirectory = Path + ".dSYM";
227 SmallString<16> ResourceName = StringRef(DSymDirectory);
228 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
229 sys::path::append(ResourceName, Basename);
230 return ResourceName.str();
231}
232
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000233static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000234 std::unique_ptr<MemoryBuffer> MB;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000235 if (MemoryBuffer::getFileOrSTDIN(Path, MB))
236 return false;
237 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB->getBuffer());
238}
239
240static bool findDebugBinary(const std::string &OrigPath,
241 const std::string &DebuglinkName, uint32_t CRCHash,
242 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000243 std::string OrigRealPath = OrigPath;
244#if defined(HAVE_REALPATH)
245 if (char *RP = realpath(OrigPath.c_str(), NULL)) {
246 OrigRealPath = RP;
247 free(RP);
248 }
249#endif
250 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000251 llvm::sys::path::remove_filename(OrigDir);
252 SmallString<16> DebugPath = OrigDir;
253 // Try /path/to/original_binary/debuglink_name
254 llvm::sys::path::append(DebugPath, DebuglinkName);
255 if (checkFileCRC(DebugPath, CRCHash)) {
256 Result = DebugPath.str();
257 return true;
258 }
259 // Try /path/to/original_binary/.debug/debuglink_name
Alexey Samsonova591ae62013-08-26 18:12:03 +0000260 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000261 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
262 if (checkFileCRC(DebugPath, CRCHash)) {
263 Result = DebugPath.str();
264 return true;
265 }
266 // Try /usr/lib/debug/path/to/original_binary/debuglink_name
267 DebugPath = "/usr/lib/debug";
268 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
269 DebuglinkName);
270 if (checkFileCRC(DebugPath, CRCHash)) {
271 Result = DebugPath.str();
272 return true;
273 }
274 return false;
275}
276
277static bool getGNUDebuglinkContents(const Binary *Bin, std::string &DebugName,
278 uint32_t &CRCHash) {
279 const ObjectFile *Obj = dyn_cast<ObjectFile>(Bin);
280 if (!Obj)
281 return false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000282 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000283 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000284 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000285 Name = Name.substr(Name.find_first_not_of("._"));
286 if (Name == "gnu_debuglink") {
287 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000288 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000289 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
290 uint32_t Offset = 0;
291 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
292 // 4-byte align the offset.
293 Offset = (Offset + 3) & ~0x3;
294 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
295 DebugName = DebugNameStr;
296 CRCHash = DE.getU32(&Offset);
297 return true;
298 }
299 }
300 break;
301 }
302 }
303 return false;
304}
305
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000306LLVMSymbolizer::BinaryPair
307LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
308 BinaryMapTy::iterator I = BinaryForPath.find(Path);
309 if (I != BinaryForPath.end())
310 return I->second;
311 Binary *Bin = 0;
312 Binary *DbgBin = 0;
Rafael Espindola63da2952014-01-15 19:37:43 +0000313 ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
314 if (!error(BinaryOrErr.getError())) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000315 std::unique_ptr<Binary> ParsedBinary(BinaryOrErr.get());
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000316 // Check if it's a universal binary.
Ahmed Charles96c9d952014-03-05 10:19:29 +0000317 Bin = ParsedBinary.release();
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000318 ParsedBinariesAndObjects.push_back(Bin);
319 if (Bin->isMachO() || Bin->isMachOUniversalBinary()) {
320 // On Darwin we may find DWARF in separate object file in
321 // resource directory.
322 const std::string &ResourcePath =
323 getDarwinDWARFResourceForPath(Path);
Rafael Espindola63da2952014-01-15 19:37:43 +0000324 BinaryOrErr = createBinary(ResourcePath);
325 error_code EC = BinaryOrErr.getError();
Rafael Espindola5cf52102014-01-15 04:49:50 +0000326 if (EC != errc::no_such_file_or_directory && !error(EC)) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000327 DbgBin = BinaryOrErr.get();
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000328 ParsedBinariesAndObjects.push_back(DbgBin);
329 }
330 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000331 // Try to locate the debug binary using .gnu_debuglink section.
332 if (DbgBin == 0) {
333 std::string DebuglinkName;
334 uint32_t CRCHash;
335 std::string DebugBinaryPath;
336 if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000337 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
338 BinaryOrErr = createBinary(DebugBinaryPath);
339 if (!error(BinaryOrErr.getError())) {
340 DbgBin = BinaryOrErr.get();
341 ParsedBinariesAndObjects.push_back(DbgBin);
342 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000343 }
344 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000345 }
346 if (DbgBin == 0)
347 DbgBin = Bin;
348 BinaryPair Res = std::make_pair(Bin, DbgBin);
349 BinaryForPath[Path] = Res;
350 return Res;
351}
352
353ObjectFile *
354LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) {
355 if (Bin == 0)
356 return 0;
357 ObjectFile *Res = 0;
358 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
359 ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find(
360 std::make_pair(UB, ArchName));
361 if (I != ObjectFileForArch.end())
362 return I->second;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000363 std::unique_ptr<ObjectFile> ParsedObj;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000364 if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) {
Ahmed Charles96c9d952014-03-05 10:19:29 +0000365 Res = ParsedObj.release();
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000366 ParsedBinariesAndObjects.push_back(Res);
367 }
368 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
369 } else if (Bin->isObject()) {
370 Res = cast<ObjectFile>(Bin);
371 }
372 return Res;
373}
374
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000375ModuleInfo *
376LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000377 ModuleMapTy::iterator I = Modules.find(ModuleName);
378 if (I != Modules.end())
379 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000380 std::string BinaryName = ModuleName;
381 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000382 size_t ColonPos = ModuleName.find_last_of(':');
383 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000384 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000385 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000386 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000387 BinaryName = ModuleName.substr(0, ColonPos);
388 ArchName = ArchStr;
389 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000390 }
391 BinaryPair Binaries = getOrCreateBinary(BinaryName);
392 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
393 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000394
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000395 if (Obj == 0) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000396 // Failed to find valid object file.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000397 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000398 return 0;
399 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000400 DIContext *Context = DIContext::getDWARFContext(DbgObj);
401 assert(Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000402 ModuleInfo *Info = new ModuleInfo(Obj, Context);
403 Modules.insert(make_pair(ModuleName, Info));
404 return Info;
405}
406
407std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
408 // By default, DILineInfo contains "<invalid>" for function/filename it
409 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
410 static const std::string kDILineInfoBadString = "<invalid>";
411 std::stringstream Result;
412 if (Opts.PrintFunctions) {
413 std::string FunctionName = LineInfo.getFunctionName();
414 if (FunctionName == kDILineInfoBadString)
415 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000416 else if (Opts.Demangle)
417 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000418 Result << FunctionName << "\n";
419 }
420 std::string Filename = LineInfo.getFileName();
421 if (Filename == kDILineInfoBadString)
422 Filename = kBadString;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000423 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
424 << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000425 return Result.str();
426}
427
428#if !defined(_MSC_VER)
429// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
430extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
431 size_t *length, int *status);
432#endif
433
Alexey Samsonov601beb72013-06-28 12:06:25 +0000434std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000435#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000436 // We can spoil names of symbols with C linkage, so use an heuristic
437 // approach to check if the name should be demangled.
438 if (Name.substr(0, 2) != "_Z")
439 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000440 int status = 0;
441 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
442 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000443 return Name;
444 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000445 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000446 return Result;
447#else
448 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000449#endif
450}
451
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000452} // namespace symbolize
453} // namespace llvm