llvm-undname.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===-- llvm-undname.cpp - Microsoft ABI name undecorator
  2. //------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This utility works like the windows undname utility. It converts mangled
  11. // Microsoft symbol names into pretty C/C++ human-readable names.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Demangle/Demangle.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/ErrorOr.h"
  18. #include "llvm/Support/InitLLVM.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/Process.h"
  21. #include "llvm/Support/WithColor.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cstdio>
  24. #include <cstring>
  25. #include <iostream>
  26. #include <string>
  27. using namespace llvm;
  28. cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
  29. cl::desc("dump backreferences"), cl::Hidden,
  30. cl::init(false));
  31. cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
  32. cl::desc("skip access specifiers"), cl::Hidden,
  33. cl::init(false));
  34. cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
  35. cl::desc("skip calling convention"),
  36. cl::Hidden, cl::init(false));
  37. cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
  38. cl::desc("skip return types"), cl::Hidden,
  39. cl::init(false));
  40. cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
  41. cl::desc("skip member types"), cl::Hidden,
  42. cl::init(false));
  43. cl::opt<std::string> RawFile("raw-file", cl::Optional,
  44. cl::desc("for fuzzer data"), cl::Hidden);
  45. cl::opt<bool> WarnTrailing("warn-trailing", cl::Optional,
  46. cl::desc("warn on trailing characters"), cl::Hidden,
  47. cl::init(false));
  48. cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
  49. cl::ZeroOrMore);
  50. static bool msDemangle(const std::string &S) {
  51. int Status;
  52. MSDemangleFlags Flags = MSDF_None;
  53. if (DumpBackReferences)
  54. Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
  55. if (NoAccessSpecifier)
  56. Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
  57. if (NoCallingConvention)
  58. Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
  59. if (NoReturnType)
  60. Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
  61. if (NoMemberType)
  62. Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
  63. size_t NRead;
  64. char *ResultBuf =
  65. microsoftDemangle(S.c_str(), &NRead, nullptr, nullptr, &Status, Flags);
  66. if (Status == llvm::demangle_success) {
  67. outs() << ResultBuf << "\n";
  68. outs().flush();
  69. if (WarnTrailing && NRead < S.size())
  70. WithColor::warning() << "trailing characters: " << S.c_str() + NRead
  71. << "\n";
  72. } else {
  73. WithColor::error() << "Invalid mangled name\n";
  74. }
  75. std::free(ResultBuf);
  76. return Status == llvm::demangle_success;
  77. }
  78. int main(int argc, char **argv) {
  79. InitLLVM X(argc, argv);
  80. cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
  81. if (!RawFile.empty()) {
  82. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  83. MemoryBuffer::getFileOrSTDIN(RawFile);
  84. if (std::error_code EC = FileOrErr.getError()) {
  85. WithColor::error() << "Could not open input file \'" << RawFile
  86. << "\': " << EC.message() << '\n';
  87. return 1;
  88. }
  89. return msDemangle(std::string(FileOrErr->get()->getBuffer())) ? 0 : 1;
  90. }
  91. bool Success = true;
  92. if (Symbols.empty()) {
  93. while (true) {
  94. std::string LineStr;
  95. std::getline(std::cin, LineStr);
  96. if (std::cin.eof())
  97. break;
  98. StringRef Line(LineStr);
  99. Line = Line.trim();
  100. if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
  101. continue;
  102. // If the user is manually typing in these decorated names, don't echo
  103. // them to the terminal a second time. If they're coming from redirected
  104. // input, however, then we should display the input line so that the
  105. // mangled and demangled name can be easily correlated in the output.
  106. if (!sys::Process::StandardInIsUserInput()) {
  107. outs() << Line << "\n";
  108. outs().flush();
  109. }
  110. if (!msDemangle(std::string(Line)))
  111. Success = false;
  112. outs() << "\n";
  113. }
  114. } else {
  115. for (StringRef S : Symbols) {
  116. outs() << S << "\n";
  117. outs().flush();
  118. if (!msDemangle(std::string(S)))
  119. Success = false;
  120. outs() << "\n";
  121. }
  122. }
  123. return Success ? 0 : 1;
  124. }