LLVMSymbolize.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. //===-- 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. #include "LLVMSymbolize.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/Config/config.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  17. #include "llvm/DebugInfo/PDB/PDB.h"
  18. #include "llvm/DebugInfo/PDB/PDBContext.h"
  19. #include "llvm/Object/ELFObjectFile.h"
  20. #include "llvm/Object/MachO.h"
  21. #include "llvm/Object/SymbolSize.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/Compression.h"
  24. #include "llvm/Support/DataExtractor.h"
  25. #include "llvm/Support/Errc.h"
  26. #include "llvm/Support/FileSystem.h"
  27. #include "llvm/Support/MemoryBuffer.h"
  28. #include "llvm/Support/Path.h"
  29. #include <sstream>
  30. #include <stdlib.h>
  31. #if defined(_MSC_VER)
  32. #include <windows.h>
  33. #include <dbghelp.h>
  34. #pragma comment(lib, "dbghelp.lib")
  35. #endif
  36. namespace llvm {
  37. namespace symbolize {
  38. static bool error(std::error_code ec) {
  39. if (!ec)
  40. return false;
  41. errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
  42. return true;
  43. }
  44. static DILineInfoSpecifier
  45. getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
  46. return DILineInfoSpecifier(
  47. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
  48. Opts.PrintFunctions);
  49. }
  50. ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
  51. : Module(Obj), DebugInfoContext(DICtx) {
  52. std::unique_ptr<DataExtractor> OpdExtractor;
  53. uint64_t OpdAddress = 0;
  54. // Find the .opd (function descriptor) section if any, for big-endian
  55. // PowerPC64 ELF.
  56. if (Module->getArch() == Triple::ppc64) {
  57. for (section_iterator Section : Module->sections()) {
  58. StringRef Name;
  59. if (!error(Section->getName(Name)) && Name == ".opd") {
  60. StringRef Data;
  61. if (!error(Section->getContents(Data))) {
  62. OpdExtractor.reset(new DataExtractor(Data, Module->isLittleEndian(),
  63. Module->getBytesInAddress()));
  64. OpdAddress = Section->getAddress();
  65. }
  66. break;
  67. }
  68. }
  69. }
  70. std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
  71. computeSymbolSizes(*Module);
  72. for (auto &P : Symbols)
  73. addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
  74. }
  75. void ModuleInfo::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
  76. DataExtractor *OpdExtractor, uint64_t OpdAddress) {
  77. SymbolRef::Type SymbolType = Symbol.getType();
  78. if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
  79. return;
  80. ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
  81. if (error(SymbolAddressOrErr.getError()))
  82. return;
  83. uint64_t SymbolAddress = *SymbolAddressOrErr;
  84. if (OpdExtractor) {
  85. // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
  86. // function descriptors. The first word of the descriptor is a pointer to
  87. // the function's code.
  88. // For the purposes of symbolization, pretend the symbol's address is that
  89. // of the function's code, not the descriptor.
  90. uint64_t OpdOffset = SymbolAddress - OpdAddress;
  91. uint32_t OpdOffset32 = OpdOffset;
  92. if (OpdOffset == OpdOffset32 &&
  93. OpdExtractor->isValidOffsetForAddress(OpdOffset32))
  94. SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
  95. }
  96. ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName();
  97. if (error(SymbolNameOrErr.getError()))
  98. return;
  99. StringRef SymbolName = *SymbolNameOrErr;
  100. // Mach-O symbol table names have leading underscore, skip it.
  101. if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
  102. SymbolName = SymbolName.drop_front();
  103. // FIXME: If a function has alias, there are two entries in symbol table
  104. // with same address size. Make sure we choose the correct one.
  105. auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
  106. SymbolDesc SD = { SymbolAddress, SymbolSize };
  107. M.insert(std::make_pair(SD, SymbolName));
  108. }
  109. bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
  110. std::string &Name, uint64_t &Addr,
  111. uint64_t &Size) const {
  112. const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
  113. if (SymbolMap.empty())
  114. return false;
  115. SymbolDesc SD = { Address, Address };
  116. auto SymbolIterator = SymbolMap.upper_bound(SD);
  117. if (SymbolIterator == SymbolMap.begin())
  118. return false;
  119. --SymbolIterator;
  120. if (SymbolIterator->first.Size != 0 &&
  121. SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
  122. return false;
  123. Name = SymbolIterator->second.str();
  124. Addr = SymbolIterator->first.Addr;
  125. Size = SymbolIterator->first.Size;
  126. return true;
  127. }
  128. DILineInfo ModuleInfo::symbolizeCode(
  129. uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
  130. DILineInfo LineInfo;
  131. if (DebugInfoContext) {
  132. LineInfo = DebugInfoContext->getLineInfoForAddress(
  133. ModuleOffset, getDILineInfoSpecifier(Opts));
  134. }
  135. // Override function name from symbol table if necessary.
  136. if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
  137. std::string FunctionName;
  138. uint64_t Start, Size;
  139. if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
  140. FunctionName, Start, Size)) {
  141. LineInfo.FunctionName = FunctionName;
  142. }
  143. }
  144. return LineInfo;
  145. }
  146. DIInliningInfo ModuleInfo::symbolizeInlinedCode(
  147. uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
  148. DIInliningInfo InlinedContext;
  149. if (DebugInfoContext) {
  150. InlinedContext = DebugInfoContext->getInliningInfoForAddress(
  151. ModuleOffset, getDILineInfoSpecifier(Opts));
  152. }
  153. // Make sure there is at least one frame in context.
  154. if (InlinedContext.getNumberOfFrames() == 0) {
  155. InlinedContext.addFrame(DILineInfo());
  156. }
  157. // Override the function name in lower frame with name from symbol table.
  158. if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
  159. DIInliningInfo PatchedInlinedContext;
  160. for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
  161. DILineInfo LineInfo = InlinedContext.getFrame(i);
  162. if (i == n - 1) {
  163. std::string FunctionName;
  164. uint64_t Start, Size;
  165. if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
  166. FunctionName, Start, Size)) {
  167. LineInfo.FunctionName = FunctionName;
  168. }
  169. }
  170. PatchedInlinedContext.addFrame(LineInfo);
  171. }
  172. InlinedContext = PatchedInlinedContext;
  173. }
  174. return InlinedContext;
  175. }
  176. bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
  177. uint64_t &Start, uint64_t &Size) const {
  178. return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
  179. Size);
  180. }
  181. const char LLVMSymbolizer::kBadString[] = "??";
  182. std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
  183. uint64_t ModuleOffset) {
  184. ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
  185. if (!Info)
  186. return printDILineInfo(DILineInfo());
  187. if (Opts.PrintInlining) {
  188. DIInliningInfo InlinedContext =
  189. Info->symbolizeInlinedCode(ModuleOffset, Opts);
  190. uint32_t FramesNum = InlinedContext.getNumberOfFrames();
  191. assert(FramesNum > 0);
  192. std::string Result;
  193. for (uint32_t i = 0; i < FramesNum; i++) {
  194. DILineInfo LineInfo = InlinedContext.getFrame(i);
  195. Result += printDILineInfo(LineInfo);
  196. }
  197. return Result;
  198. }
  199. DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
  200. return printDILineInfo(LineInfo);
  201. }
  202. std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
  203. uint64_t ModuleOffset) {
  204. std::string Name = kBadString;
  205. uint64_t Start = 0;
  206. uint64_t Size = 0;
  207. if (Opts.UseSymbolTable) {
  208. if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
  209. if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
  210. Name = DemangleName(Name);
  211. }
  212. }
  213. std::stringstream ss;
  214. ss << Name << "\n" << Start << " " << Size << "\n";
  215. return ss.str();
  216. }
  217. void LLVMSymbolizer::flush() {
  218. DeleteContainerSeconds(Modules);
  219. ObjectPairForPathArch.clear();
  220. ObjectFileForArch.clear();
  221. }
  222. // For Path="/path/to/foo" and Basename="foo" assume that debug info is in
  223. // /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
  224. // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
  225. // /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
  226. static
  227. std::string getDarwinDWARFResourceForPath(
  228. const std::string &Path, const std::string &Basename) {
  229. SmallString<16> ResourceName = StringRef(Path);
  230. if (sys::path::extension(Path) != ".dSYM") {
  231. ResourceName += ".dSYM";
  232. }
  233. sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
  234. sys::path::append(ResourceName, Basename);
  235. return ResourceName.str();
  236. }
  237. static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
  238. ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
  239. MemoryBuffer::getFileOrSTDIN(Path);
  240. if (!MB)
  241. return false;
  242. return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
  243. }
  244. static bool findDebugBinary(const std::string &OrigPath,
  245. const std::string &DebuglinkName, uint32_t CRCHash,
  246. std::string &Result) {
  247. std::string OrigRealPath = OrigPath;
  248. #if defined(HAVE_REALPATH)
  249. if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
  250. OrigRealPath = RP;
  251. free(RP);
  252. }
  253. #endif
  254. SmallString<16> OrigDir(OrigRealPath);
  255. llvm::sys::path::remove_filename(OrigDir);
  256. SmallString<16> DebugPath = OrigDir;
  257. // Try /path/to/original_binary/debuglink_name
  258. llvm::sys::path::append(DebugPath, DebuglinkName);
  259. if (checkFileCRC(DebugPath, CRCHash)) {
  260. Result = DebugPath.str();
  261. return true;
  262. }
  263. // Try /path/to/original_binary/.debug/debuglink_name
  264. DebugPath = OrigRealPath;
  265. llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
  266. if (checkFileCRC(DebugPath, CRCHash)) {
  267. Result = DebugPath.str();
  268. return true;
  269. }
  270. // Try /usr/lib/debug/path/to/original_binary/debuglink_name
  271. DebugPath = "/usr/lib/debug";
  272. llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
  273. DebuglinkName);
  274. if (checkFileCRC(DebugPath, CRCHash)) {
  275. Result = DebugPath.str();
  276. return true;
  277. }
  278. return false;
  279. }
  280. static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
  281. uint32_t &CRCHash) {
  282. if (!Obj)
  283. return false;
  284. for (const SectionRef &Section : Obj->sections()) {
  285. StringRef Name;
  286. Section.getName(Name);
  287. Name = Name.substr(Name.find_first_not_of("._"));
  288. if (Name == "gnu_debuglink") {
  289. StringRef Data;
  290. Section.getContents(Data);
  291. DataExtractor DE(Data, Obj->isLittleEndian(), 0);
  292. uint32_t Offset = 0;
  293. if (const char *DebugNameStr = DE.getCStr(&Offset)) {
  294. // 4-byte align the offset.
  295. Offset = (Offset + 3) & ~0x3;
  296. if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
  297. DebugName = DebugNameStr;
  298. CRCHash = DE.getU32(&Offset);
  299. return true;
  300. }
  301. }
  302. break;
  303. }
  304. }
  305. return false;
  306. }
  307. static
  308. bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
  309. const MachOObjectFile *Obj) {
  310. ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
  311. ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
  312. if (dbg_uuid.empty() || bin_uuid.empty())
  313. return false;
  314. return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
  315. }
  316. ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
  317. const MachOObjectFile *MachExeObj, const std::string &ArchName) {
  318. // On Darwin we may find DWARF in separate object file in
  319. // resource directory.
  320. std::vector<std::string> DsymPaths;
  321. StringRef Filename = sys::path::filename(ExePath);
  322. DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
  323. for (const auto &Path : Opts.DsymHints) {
  324. DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
  325. }
  326. for (const auto &path : DsymPaths) {
  327. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(path);
  328. std::error_code EC = BinaryOrErr.getError();
  329. if (EC != errc::no_such_file_or_directory && !error(EC)) {
  330. OwningBinary<Binary> B = std::move(BinaryOrErr.get());
  331. ObjectFile *DbgObj =
  332. getObjectFileFromBinary(B.getBinary(), ArchName);
  333. const MachOObjectFile *MachDbgObj =
  334. dyn_cast<const MachOObjectFile>(DbgObj);
  335. if (!MachDbgObj) continue;
  336. if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) {
  337. addOwningBinary(std::move(B));
  338. return DbgObj;
  339. }
  340. }
  341. }
  342. return nullptr;
  343. }
  344. LLVMSymbolizer::ObjectPair
  345. LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
  346. const std::string &ArchName) {
  347. const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
  348. if (I != ObjectPairForPathArch.end())
  349. return I->second;
  350. ObjectFile *Obj = nullptr;
  351. ObjectFile *DbgObj = nullptr;
  352. ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
  353. if (!error(BinaryOrErr.getError())) {
  354. OwningBinary<Binary> &B = BinaryOrErr.get();
  355. Obj = getObjectFileFromBinary(B.getBinary(), ArchName);
  356. if (!Obj) {
  357. ObjectPair Res = std::make_pair(nullptr, nullptr);
  358. ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
  359. return Res;
  360. }
  361. addOwningBinary(std::move(B));
  362. if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
  363. DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
  364. // Try to locate the debug binary using .gnu_debuglink section.
  365. if (!DbgObj) {
  366. std::string DebuglinkName;
  367. uint32_t CRCHash;
  368. std::string DebugBinaryPath;
  369. if (getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash) &&
  370. findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
  371. BinaryOrErr = createBinary(DebugBinaryPath);
  372. if (!error(BinaryOrErr.getError())) {
  373. OwningBinary<Binary> B = std::move(BinaryOrErr.get());
  374. DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName);
  375. addOwningBinary(std::move(B));
  376. }
  377. }
  378. }
  379. }
  380. if (!DbgObj)
  381. DbgObj = Obj;
  382. ObjectPair Res = std::make_pair(Obj, DbgObj);
  383. ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
  384. return Res;
  385. }
  386. ObjectFile *
  387. LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin,
  388. const std::string &ArchName) {
  389. if (!Bin)
  390. return nullptr;
  391. ObjectFile *Res = nullptr;
  392. if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
  393. const auto &I = ObjectFileForArch.find(
  394. std::make_pair(UB, ArchName));
  395. if (I != ObjectFileForArch.end())
  396. return I->second;
  397. ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
  398. UB->getObjectForArch(ArchName);
  399. if (ParsedObj) {
  400. Res = ParsedObj.get().get();
  401. ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
  402. }
  403. ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
  404. } else if (Bin->isObject()) {
  405. Res = cast<ObjectFile>(Bin);
  406. }
  407. return Res;
  408. }
  409. ModuleInfo *
  410. LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
  411. const auto &I = Modules.find(ModuleName);
  412. if (I != Modules.end())
  413. return I->second;
  414. std::string BinaryName = ModuleName;
  415. std::string ArchName = Opts.DefaultArch;
  416. size_t ColonPos = ModuleName.find_last_of(':');
  417. // Verify that substring after colon form a valid arch name.
  418. if (ColonPos != std::string::npos) {
  419. std::string ArchStr = ModuleName.substr(ColonPos + 1);
  420. if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
  421. BinaryName = ModuleName.substr(0, ColonPos);
  422. ArchName = ArchStr;
  423. }
  424. }
  425. ObjectPair Objects = getOrCreateObjects(BinaryName, ArchName);
  426. if (!Objects.first) {
  427. // Failed to find valid object file.
  428. Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
  429. return nullptr;
  430. }
  431. DIContext *Context = nullptr;
  432. if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
  433. // If this is a COFF object, assume it contains PDB debug information. If
  434. // we don't find any we will fall back to the DWARF case.
  435. std::unique_ptr<IPDBSession> Session;
  436. PDB_ErrorCode Error = loadDataForEXE(PDB_ReaderType::DIA,
  437. Objects.first->getFileName(), Session);
  438. if (Error == PDB_ErrorCode::Success) {
  439. Context = new PDBContext(*CoffObject, std::move(Session),
  440. Opts.RelativeAddresses);
  441. }
  442. }
  443. if (!Context)
  444. Context = new DWARFContextInMemory(*Objects.second);
  445. assert(Context);
  446. ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
  447. Modules.insert(make_pair(ModuleName, Info));
  448. return Info;
  449. }
  450. std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
  451. // By default, DILineInfo contains "<invalid>" for function/filename it
  452. // cannot fetch. We replace it to "??" to make our output closer to addr2line.
  453. static const std::string kDILineInfoBadString = "<invalid>";
  454. std::stringstream Result;
  455. if (Opts.PrintFunctions != FunctionNameKind::None) {
  456. std::string FunctionName = LineInfo.FunctionName;
  457. if (FunctionName == kDILineInfoBadString)
  458. FunctionName = kBadString;
  459. else if (Opts.Demangle)
  460. FunctionName = DemangleName(FunctionName);
  461. Result << FunctionName << "\n";
  462. }
  463. std::string Filename = LineInfo.FileName;
  464. if (Filename == kDILineInfoBadString)
  465. Filename = kBadString;
  466. Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n";
  467. return Result.str();
  468. }
  469. #if !defined(_MSC_VER)
  470. // Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
  471. extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
  472. size_t *length, int *status);
  473. #endif
  474. std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
  475. #if !defined(_MSC_VER)
  476. // We can spoil names of symbols with C linkage, so use an heuristic
  477. // approach to check if the name should be demangled.
  478. if (Name.substr(0, 2) != "_Z")
  479. return Name;
  480. int status = 0;
  481. char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
  482. if (status != 0)
  483. return Name;
  484. std::string Result = DemangledName;
  485. free(DemangledName);
  486. return Result;
  487. #else
  488. char DemangledName[1024] = {0};
  489. DWORD result = ::UnDecorateSymbolName(
  490. Name.c_str(), DemangledName, 1023,
  491. UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected
  492. UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
  493. UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications
  494. UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers
  495. UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords
  496. UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
  497. return (result == 0) ? Name : std::string(DemangledName);
  498. #endif
  499. }
  500. } // namespace symbolize
  501. } // namespace llvm