blob: 40751998579c7bce7bbad48685c31349a312a556 [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 Samsonovea83baf2013-01-22 14:21:19 +000017#include "llvm/Object/MachO.h"
18#include "llvm/Support/Casting.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000019#include "llvm/Support/Compression.h"
20#include "llvm/Support/DataExtractor.h"
Alexey Samsonov5239d582013-06-04 07:57:38 +000021#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000022#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000023#include "llvm/Support/Path.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000024#include <sstream>
Alexey Samsonova591ae62013-08-26 18:12:03 +000025#include <stdlib.h>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000026
27namespace llvm {
28namespace symbolize {
29
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000030static bool error(error_code ec) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000031 if (!ec)
32 return false;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000033 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
34 return true;
35}
36
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000037static uint32_t
38getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +000039 uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
40 llvm::DILineInfoSpecifier::AbsoluteFilePath;
41 if (Opts.PrintFunctions)
42 Flags |= llvm::DILineInfoSpecifier::FunctionName;
43 return Flags;
44}
45
46static 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 Vyukovef8fb722013-02-14 13:06:18 +000053ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000054 : Module(Obj), DebugInfoContext(DICtx) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000055 for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
Rafael Espindola5e812af2014-01-30 02:49:50 +000056 si != se; ++si) {
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000057 SymbolRef::Type SymbolType;
58 if (error(si->getType(SymbolType)))
59 continue;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000060 if (SymbolType != SymbolRef::ST_Function &&
61 SymbolType != SymbolRef::ST_Data)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000062 continue;
63 uint64_t SymbolAddress;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000064 if (error(si->getAddress(SymbolAddress)) ||
65 SymbolAddress == UnknownAddressOrSize)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000066 continue;
67 uint64_t SymbolSize;
Alexey Samsonov35c987d2013-06-07 15:25:27 +000068 // Getting symbol size is linear for Mach-O files, so assume that symbol
69 // occupies the memory range up to the following symbol.
Alexey Samsonov5239d582013-06-04 07:57:38 +000070 if (isa<MachOObjectFile>(Obj))
71 SymbolSize = 0;
72 else if (error(si->getSize(SymbolSize)) ||
73 SymbolSize == UnknownAddressOrSize)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000074 continue;
75 StringRef SymbolName;
76 if (error(si->getName(SymbolName)))
77 continue;
Alexey Samsonov73233832013-06-28 14:25:52 +000078 // 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 Vyukovef8fb722013-02-14 13:06:18 +000081 // 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 Samsonovd5d7bb52013-02-15 08:54:47 +000083 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov35c987d2013-06-07 15:25:27 +000084 SymbolDesc SD = { SymbolAddress, SymbolSize };
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000085 M.insert(std::make_pair(SD, SymbolName));
86 }
87}
88
89bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
90 std::string &Name, uint64_t &Addr,
91 uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000092 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov5239d582013-06-04 07:57:38 +000093 if (M.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000094 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +000095 SymbolDesc SD = { Address, Address };
96 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonov35c987d2013-06-07 15:25:27 +000097 if (it == M.begin())
98 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +000099 --it;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000100 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000101 return false;
102 Name = it->second.str();
103 Addr = it->first.Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000104 Size = it->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000105 return true;
106}
107
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000108DILineInfo ModuleInfo::symbolizeCode(
109 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000110 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 Samsonovd5d7bb52013-02-15 08:54:47 +0000119 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
120 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000121 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
122 }
123 }
124 return LineInfo;
125}
126
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000127DIInliningInfo ModuleInfo::symbolizeInlinedCode(
128 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000129 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 Samsonovd5d7bb52013-02-15 08:54:47 +0000141 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000142 DILineInfo LineInfo = InlinedContext.getFrame(i);
143 if (i == n - 1) {
144 std::string FunctionName;
145 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000146 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
147 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000148 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
149 }
150 }
151 PatchedInlinedContext.addFrame(LineInfo);
152 }
153 InlinedContext = PatchedInlinedContext;
154 }
155 return InlinedContext;
156}
157
158bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
159 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000160 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
161 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000162}
163
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000164const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000165
166std::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 Samsonovd5d7bb52013-02-15 08:54:47 +0000172 DIInliningInfo InlinedContext =
173 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000174 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
187std::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 Samsonov601beb72013-06-28 12:06:25 +0000194 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000195 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000196 }
197 }
198 std::stringstream ss;
199 ss << Name << "\n" << Start << " " << Size << "\n";
200 return ss.str();
201}
202
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000203void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000204 DeleteContainerSeconds(Modules);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000205 DeleteContainerPointers(ParsedBinariesAndObjects);
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000206 BinaryForPath.clear();
207 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000208}
209
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000210static std::string getDarwinDWARFResourceForPath(const std::string &Path) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000211 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 Samsonov3e9997f2013-08-14 17:09:30 +0000219static 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
226static bool findDebugBinary(const std::string &OrigPath,
227 const std::string &DebuglinkName, uint32_t CRCHash,
228 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000229 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 Samsonov3e9997f2013-08-14 17:09:30 +0000237 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 Samsonova591ae62013-08-26 18:12:03 +0000246 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000247 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
263static 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 Samsonov3e9997f2013-08-14 17:09:30 +0000268 for (section_iterator I = Obj->begin_sections(), E = Obj->end_sections();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000269 I != E; ++I) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000270 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 Samsonov2ca65362013-06-28 08:15:40 +0000293LLVMSymbolizer::BinaryPair
294LLVMSymbolizer::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 Espindola63da2952014-01-15 19:37:43 +0000300 ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
301 if (!error(BinaryOrErr.getError())) {
302 OwningPtr<Binary> ParsedBinary(BinaryOrErr.get());
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000303 // 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 Espindola63da2952014-01-15 19:37:43 +0000311 BinaryOrErr = createBinary(ResourcePath);
312 error_code EC = BinaryOrErr.getError();
Rafael Espindola5cf52102014-01-15 04:49:50 +0000313 if (EC != errc::no_such_file_or_directory && !error(EC)) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000314 DbgBin = BinaryOrErr.get();
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000315 ParsedBinariesAndObjects.push_back(DbgBin);
316 }
317 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000318 // 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 Espindola63da2952014-01-15 19:37:43 +0000324 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 Samsonov3e9997f2013-08-14 17:09:30 +0000330 }
331 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000332 }
333 if (DbgBin == 0)
334 DbgBin = Bin;
335 BinaryPair Res = std::make_pair(Bin, DbgBin);
336 BinaryForPath[Path] = Res;
337 return Res;
338}
339
340ObjectFile *
341LLVMSymbolizer::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 Samsonovd5d7bb52013-02-15 08:54:47 +0000362ModuleInfo *
363LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000364 ModuleMapTy::iterator I = Modules.find(ModuleName);
365 if (I != Modules.end())
366 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000367 std::string BinaryName = ModuleName;
368 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000369 size_t ColonPos = ModuleName.find_last_of(':');
370 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000371 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000372 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000373 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000374 BinaryName = ModuleName.substr(0, ColonPos);
375 ArchName = ArchStr;
376 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000377 }
378 BinaryPair Binaries = getOrCreateBinary(BinaryName);
379 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
380 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000381
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000382 if (Obj == 0) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000383 // Failed to find valid object file.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000384 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000385 return 0;
386 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000387 DIContext *Context = DIContext::getDWARFContext(DbgObj);
388 assert(Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000389 ModuleInfo *Info = new ModuleInfo(Obj, Context);
390 Modules.insert(make_pair(ModuleName, Info));
391 return Info;
392}
393
394std::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 Samsonov601beb72013-06-28 12:06:25 +0000403 else if (Opts.Demangle)
404 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000405 Result << FunctionName << "\n";
406 }
407 std::string Filename = LineInfo.getFileName();
408 if (Filename == kDILineInfoBadString)
409 Filename = kBadString;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000410 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
411 << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000412 return Result.str();
413}
414
415#if !defined(_MSC_VER)
416// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
417extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
418 size_t *length, int *status);
419#endif
420
Alexey Samsonov601beb72013-06-28 12:06:25 +0000421std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000422#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000423 // 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 Samsonovea83baf2013-01-22 14:21:19 +0000427 int status = 0;
428 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
429 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000430 return Name;
431 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000432 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000433 return Result;
434#else
435 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000436#endif
437}
438
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000439} // namespace symbolize
440} // namespace llvm