blob: 71588e171fa9db0266301ee2d5c44e90b0d1ed4d [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) {
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000055 error_code ec;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000056 for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
57 si != se; si.increment(ec)) {
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000058 if (error(ec))
59 return;
60 SymbolRef::Type SymbolType;
61 if (error(si->getType(SymbolType)))
62 continue;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000063 if (SymbolType != SymbolRef::ST_Function &&
64 SymbolType != SymbolRef::ST_Data)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000065 continue;
66 uint64_t SymbolAddress;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000067 if (error(si->getAddress(SymbolAddress)) ||
68 SymbolAddress == UnknownAddressOrSize)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000069 continue;
70 uint64_t SymbolSize;
Alexey Samsonov35c987d2013-06-07 15:25:27 +000071 // Getting symbol size is linear for Mach-O files, so assume that symbol
72 // occupies the memory range up to the following symbol.
Alexey Samsonov5239d582013-06-04 07:57:38 +000073 if (isa<MachOObjectFile>(Obj))
74 SymbolSize = 0;
75 else if (error(si->getSize(SymbolSize)) ||
76 SymbolSize == UnknownAddressOrSize)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000077 continue;
78 StringRef SymbolName;
79 if (error(si->getName(SymbolName)))
80 continue;
Alexey Samsonov73233832013-06-28 14:25:52 +000081 // Mach-O symbol table names have leading underscore, skip it.
82 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
83 SymbolName = SymbolName.drop_front();
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000084 // FIXME: If a function has alias, there are two entries in symbol table
85 // with same address size. Make sure we choose the correct one.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000086 SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov35c987d2013-06-07 15:25:27 +000087 SymbolDesc SD = { SymbolAddress, SymbolSize };
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000088 M.insert(std::make_pair(SD, SymbolName));
89 }
90}
91
92bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
93 std::string &Name, uint64_t &Addr,
94 uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000095 const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonov5239d582013-06-04 07:57:38 +000096 if (M.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000097 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +000098 SymbolDesc SD = { Address, Address };
99 SymbolMapTy::const_iterator it = M.upper_bound(SD);
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000100 if (it == M.begin())
101 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000102 --it;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000103 if (it->first.Size != 0 && it->first.Addr + it->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000104 return false;
105 Name = it->second.str();
106 Addr = it->first.Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000107 Size = it->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000108 return true;
109}
110
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000111DILineInfo ModuleInfo::symbolizeCode(
112 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000113 DILineInfo LineInfo;
114 if (DebugInfoContext) {
115 LineInfo = DebugInfoContext->getLineInfoForAddress(
116 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
117 }
118 // Override function name from symbol table if necessary.
119 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
120 std::string FunctionName;
121 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000122 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
123 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000124 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
125 }
126 }
127 return LineInfo;
128}
129
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000130DIInliningInfo ModuleInfo::symbolizeInlinedCode(
131 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000132 DIInliningInfo InlinedContext;
133 if (DebugInfoContext) {
134 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
135 ModuleOffset, getDILineInfoSpecifierFlags(Opts));
136 }
137 // Make sure there is at least one frame in context.
138 if (InlinedContext.getNumberOfFrames() == 0) {
139 InlinedContext.addFrame(DILineInfo());
140 }
141 // Override the function name in lower frame with name from symbol table.
142 if (Opts.PrintFunctions && Opts.UseSymbolTable) {
143 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000144 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000145 DILineInfo LineInfo = InlinedContext.getFrame(i);
146 if (i == n - 1) {
147 std::string FunctionName;
148 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000149 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
150 FunctionName, Start, Size)) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000151 patchFunctionNameInDILineInfo(FunctionName, LineInfo);
152 }
153 }
154 PatchedInlinedContext.addFrame(LineInfo);
155 }
156 InlinedContext = PatchedInlinedContext;
157 }
158 return InlinedContext;
159}
160
161bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
162 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000163 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
164 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000165}
166
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000167const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000168
169std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
170 uint64_t ModuleOffset) {
171 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
172 if (Info == 0)
173 return printDILineInfo(DILineInfo());
174 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000175 DIInliningInfo InlinedContext =
176 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000177 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
178 assert(FramesNum > 0);
179 std::string Result;
180 for (uint32_t i = 0; i < FramesNum; i++) {
181 DILineInfo LineInfo = InlinedContext.getFrame(i);
182 Result += printDILineInfo(LineInfo);
183 }
184 return Result;
185 }
186 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
187 return printDILineInfo(LineInfo);
188}
189
190std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
191 uint64_t ModuleOffset) {
192 std::string Name = kBadString;
193 uint64_t Start = 0;
194 uint64_t Size = 0;
195 if (Opts.UseSymbolTable) {
196 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonov601beb72013-06-28 12:06:25 +0000197 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000198 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000199 }
200 }
201 std::stringstream ss;
202 ss << Name << "\n" << Start << " " << Size << "\n";
203 return ss.str();
204}
205
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000206void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000207 DeleteContainerSeconds(Modules);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000208 DeleteContainerPointers(ParsedBinariesAndObjects);
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) {
223 OwningPtr<MemoryBuffer> MB;
224 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)
234 if (char *RP = realpath(OrigPath.c_str(), NULL)) {
235 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;
271 error_code EC;
272 for (section_iterator I = Obj->begin_sections(), E = Obj->end_sections();
273 I != E; I.increment(EC)) {
274 StringRef Name;
275 I->getName(Name);
276 Name = Name.substr(Name.find_first_not_of("._"));
277 if (Name == "gnu_debuglink") {
278 StringRef Data;
279 I->getContents(Data);
280 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
281 uint32_t Offset = 0;
282 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
283 // 4-byte align the offset.
284 Offset = (Offset + 3) & ~0x3;
285 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
286 DebugName = DebugNameStr;
287 CRCHash = DE.getU32(&Offset);
288 return true;
289 }
290 }
291 break;
292 }
293 }
294 return false;
295}
296
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000297LLVMSymbolizer::BinaryPair
298LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
299 BinaryMapTy::iterator I = BinaryForPath.find(Path);
300 if (I != BinaryForPath.end())
301 return I->second;
302 Binary *Bin = 0;
303 Binary *DbgBin = 0;
Rafael Espindola63da2952014-01-15 19:37:43 +0000304 ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
305 if (!error(BinaryOrErr.getError())) {
306 OwningPtr<Binary> ParsedBinary(BinaryOrErr.get());
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000307 // Check if it's a universal binary.
308 Bin = ParsedBinary.take();
309 ParsedBinariesAndObjects.push_back(Bin);
310 if (Bin->isMachO() || Bin->isMachOUniversalBinary()) {
311 // On Darwin we may find DWARF in separate object file in
312 // resource directory.
313 const std::string &ResourcePath =
314 getDarwinDWARFResourceForPath(Path);
Rafael Espindola63da2952014-01-15 19:37:43 +0000315 BinaryOrErr = createBinary(ResourcePath);
316 error_code EC = BinaryOrErr.getError();
Rafael Espindola5cf52102014-01-15 04:49:50 +0000317 if (EC != errc::no_such_file_or_directory && !error(EC)) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000318 DbgBin = BinaryOrErr.get();
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000319 ParsedBinariesAndObjects.push_back(DbgBin);
320 }
321 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000322 // Try to locate the debug binary using .gnu_debuglink section.
323 if (DbgBin == 0) {
324 std::string DebuglinkName;
325 uint32_t CRCHash;
326 std::string DebugBinaryPath;
327 if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000328 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
329 BinaryOrErr = createBinary(DebugBinaryPath);
330 if (!error(BinaryOrErr.getError())) {
331 DbgBin = BinaryOrErr.get();
332 ParsedBinariesAndObjects.push_back(DbgBin);
333 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000334 }
335 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000336 }
337 if (DbgBin == 0)
338 DbgBin = Bin;
339 BinaryPair Res = std::make_pair(Bin, DbgBin);
340 BinaryForPath[Path] = Res;
341 return Res;
342}
343
344ObjectFile *
345LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin, const std::string &ArchName) {
346 if (Bin == 0)
347 return 0;
348 ObjectFile *Res = 0;
349 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
350 ObjectFileForArchMapTy::iterator I = ObjectFileForArch.find(
351 std::make_pair(UB, ArchName));
352 if (I != ObjectFileForArch.end())
353 return I->second;
354 OwningPtr<ObjectFile> ParsedObj;
355 if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) {
356 Res = ParsedObj.take();
357 ParsedBinariesAndObjects.push_back(Res);
358 }
359 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
360 } else if (Bin->isObject()) {
361 Res = cast<ObjectFile>(Bin);
362 }
363 return Res;
364}
365
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000366ModuleInfo *
367LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000368 ModuleMapTy::iterator I = Modules.find(ModuleName);
369 if (I != Modules.end())
370 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000371 std::string BinaryName = ModuleName;
372 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000373 size_t ColonPos = ModuleName.find_last_of(':');
374 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000375 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000376 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000377 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000378 BinaryName = ModuleName.substr(0, ColonPos);
379 ArchName = ArchStr;
380 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000381 }
382 BinaryPair Binaries = getOrCreateBinary(BinaryName);
383 ObjectFile *Obj = getObjectFileFromBinary(Binaries.first, ArchName);
384 ObjectFile *DbgObj = getObjectFileFromBinary(Binaries.second, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000385
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000386 if (Obj == 0) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000387 // Failed to find valid object file.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000388 Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000389 return 0;
390 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000391 DIContext *Context = DIContext::getDWARFContext(DbgObj);
392 assert(Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000393 ModuleInfo *Info = new ModuleInfo(Obj, Context);
394 Modules.insert(make_pair(ModuleName, Info));
395 return Info;
396}
397
398std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
399 // By default, DILineInfo contains "<invalid>" for function/filename it
400 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
401 static const std::string kDILineInfoBadString = "<invalid>";
402 std::stringstream Result;
403 if (Opts.PrintFunctions) {
404 std::string FunctionName = LineInfo.getFunctionName();
405 if (FunctionName == kDILineInfoBadString)
406 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000407 else if (Opts.Demangle)
408 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000409 Result << FunctionName << "\n";
410 }
411 std::string Filename = LineInfo.getFileName();
412 if (Filename == kDILineInfoBadString)
413 Filename = kBadString;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000414 Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
415 << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000416 return Result.str();
417}
418
419#if !defined(_MSC_VER)
420// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
421extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
422 size_t *length, int *status);
423#endif
424
Alexey Samsonov601beb72013-06-28 12:06:25 +0000425std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000426#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000427 // We can spoil names of symbols with C linkage, so use an heuristic
428 // approach to check if the name should be demangled.
429 if (Name.substr(0, 2) != "_Z")
430 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000431 int status = 0;
432 char *DemangledName = __cxa_demangle(Name.c_str(), 0, 0, &status);
433 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000434 return Name;
435 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000436 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000437 return Result;
438#else
439 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000440#endif
441}
442
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000443} // namespace symbolize
444} // namespace llvm