llvm-undname.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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::OptionCategory UndNameCategory("UndName Options");
  29. cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
  30. cl::desc("dump backreferences"), cl::Hidden,
  31. cl::init(false), cl::cat(UndNameCategory));
  32. cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
  33. cl::desc("skip access specifiers"), cl::Hidden,
  34. cl::init(false), cl::cat(UndNameCategory));
  35. cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
  36. cl::desc("skip calling convention"),
  37. cl::Hidden, cl::init(false),
  38. cl::cat(UndNameCategory));
  39. cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
  40. cl::desc("skip return types"), cl::Hidden,
  41. cl::init(false), cl::cat(UndNameCategory));
  42. cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
  43. cl::desc("skip member types"), cl::Hidden,
  44. cl::init(false), cl::cat(UndNameCategory));
  45. cl::opt<bool> NoVariableType("no-variable-type", cl::Optional,
  46. cl::desc("skip variable types"), cl::Hidden,
  47. cl::init(false), cl::cat(UndNameCategory));
  48. cl::opt<std::string> RawFile("raw-file", cl::Optional,
  49. cl::desc("for fuzzer data"), cl::Hidden,
  50. cl::cat(UndNameCategory));
  51. cl::opt<bool> WarnTrailing("warn-trailing", cl::Optional,
  52. cl::desc("warn on trailing characters"), cl::Hidden,
  53. cl::init(false), cl::cat(UndNameCategory));
  54. cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
  55. cl::cat(UndNameCategory));
  56. static bool msDemangle(const std::string &S) {
  57. int Status;
  58. MSDemangleFlags Flags = MSDF_None;
  59. if (DumpBackReferences)
  60. Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
  61. if (NoAccessSpecifier)
  62. Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
  63. if (NoCallingConvention)
  64. Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
  65. if (NoReturnType)
  66. Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
  67. if (NoMemberType)
  68. Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
  69. if (NoVariableType)
  70. Flags = MSDemangleFlags(Flags | MSDF_NoVariableType);
  71. size_t NRead;
  72. char *ResultBuf =
  73. microsoftDemangle(S.c_str(), &NRead, nullptr, nullptr, &Status, Flags);
  74. if (Status == llvm::demangle_success) {
  75. outs() << ResultBuf << "\n";
  76. outs().flush();
  77. if (WarnTrailing && NRead < S.size())
  78. WithColor::warning() << "trailing characters: " << S.c_str() + NRead
  79. << "\n";
  80. } else {
  81. WithColor::error() << "Invalid mangled name\n";
  82. }
  83. std::free(ResultBuf);
  84. return Status == llvm::demangle_success;
  85. }
  86. int main(int argc, char **argv) {
  87. InitLLVM X(argc, argv);
  88. cl::HideUnrelatedOptions({&UndNameCategory, &getColorCategory()});
  89. cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
  90. if (!RawFile.empty()) {
  91. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  92. MemoryBuffer::getFileOrSTDIN(RawFile);
  93. if (std::error_code EC = FileOrErr.getError()) {
  94. WithColor::error() << "Could not open input file \'" << RawFile
  95. << "\': " << EC.message() << '\n';
  96. return 1;
  97. }
  98. return msDemangle(std::string(FileOrErr->get()->getBuffer())) ? 0 : 1;
  99. }
  100. bool Success = true;
  101. if (Symbols.empty()) {
  102. while (true) {
  103. std::string LineStr;
  104. std::getline(std::cin, LineStr);
  105. if (std::cin.eof())
  106. break;
  107. StringRef Line(LineStr);
  108. Line = Line.trim();
  109. if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
  110. continue;
  111. // If the user is manually typing in these decorated names, don't echo
  112. // them to the terminal a second time. If they're coming from redirected
  113. // input, however, then we should display the input line so that the
  114. // mangled and demangled name can be easily correlated in the output.
  115. if (!sys::Process::StandardInIsUserInput()) {
  116. outs() << Line << "\n";
  117. outs().flush();
  118. }
  119. if (!msDemangle(std::string(Line)))
  120. Success = false;
  121. outs() << "\n";
  122. }
  123. } else {
  124. for (StringRef S : Symbols) {
  125. outs() << S << "\n";
  126. outs().flush();
  127. if (!msDemangle(std::string(S)))
  128. Success = false;
  129. outs() << "\n";
  130. }
  131. }
  132. return Success ? 0 : 1;
  133. }