Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 1 | //===-- llvm-undname.cpp - Microsoft ABI name undecorator |
| 2 | //------------------===// |
| 3 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 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 | |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Demangle/Demangle.h" |
| 17 | #include "llvm/Support/CommandLine.h" |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorOr.h" |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/InitLLVM.h" |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Process.h" |
Jonas Devlieghere | bada60a | 2018-11-11 22:11:47 +0000 | [diff] [blame] | 22 | #include "llvm/Support/WithColor.h" |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include <cstdio> |
| 25 | #include <cstring> |
| 26 | #include <iostream> |
| 27 | #include <string> |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
Zachary Turner | 3a758e2 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 31 | cl::opt<bool> DumpBackReferences("backrefs", cl::Optional, |
| 32 | cl::desc("dump backreferences"), cl::Hidden, |
| 33 | cl::init(false)); |
Martin Storsjo | da92ed8 | 2019-10-15 08:29:56 +0000 | [diff] [blame] | 34 | cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional, |
| 35 | cl::desc("skip access specifiers"), cl::Hidden, |
| 36 | cl::init(false)); |
| 37 | cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional, |
| 38 | cl::desc("skip calling convention"), |
| 39 | cl::Hidden, cl::init(false)); |
| 40 | cl::opt<bool> NoReturnType("no-return-type", cl::Optional, |
| 41 | cl::desc("skip return types"), cl::Hidden, |
| 42 | cl::init(false)); |
| 43 | cl::opt<bool> NoMemberType("no-member-type", cl::Optional, |
| 44 | cl::desc("skip member types"), cl::Hidden, |
| 45 | cl::init(false)); |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 46 | cl::opt<std::string> RawFile("raw-file", cl::Optional, |
| 47 | cl::desc("for fuzzer data"), cl::Hidden); |
Nico Weber | bc1c365 | 2020-05-18 19:57:30 -0400 | [diff] [blame] | 48 | cl::opt<bool> WarnTrailing("warn-trailing", cl::Optional, |
| 49 | cl::desc("warn on trailing characters"), cl::Hidden, |
| 50 | cl::init(false)); |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 51 | cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"), |
| 52 | cl::ZeroOrMore); |
| 53 | |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 54 | static bool msDemangle(const std::string &S) { |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 55 | int Status; |
Zachary Turner | 3a758e2 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 56 | MSDemangleFlags Flags = MSDF_None; |
| 57 | if (DumpBackReferences) |
| 58 | Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs); |
Martin Storsjo | da92ed8 | 2019-10-15 08:29:56 +0000 | [diff] [blame] | 59 | if (NoAccessSpecifier) |
| 60 | Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier); |
| 61 | if (NoCallingConvention) |
| 62 | Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention); |
| 63 | if (NoReturnType) |
| 64 | Flags = MSDemangleFlags(Flags | MSDF_NoReturnType); |
| 65 | if (NoMemberType) |
| 66 | Flags = MSDemangleFlags(Flags | MSDF_NoMemberType); |
Zachary Turner | 3a758e2 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 67 | |
Nico Weber | bc1c365 | 2020-05-18 19:57:30 -0400 | [diff] [blame] | 68 | size_t NRead; |
Zachary Turner | 3a758e2 | 2018-08-01 18:33:04 +0000 | [diff] [blame] | 69 | char *ResultBuf = |
Nico Weber | bc1c365 | 2020-05-18 19:57:30 -0400 | [diff] [blame] | 70 | microsoftDemangle(S.c_str(), &NRead, nullptr, nullptr, &Status, Flags); |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 71 | if (Status == llvm::demangle_success) { |
| 72 | outs() << ResultBuf << "\n"; |
| 73 | outs().flush(); |
Nico Weber | bc1c365 | 2020-05-18 19:57:30 -0400 | [diff] [blame] | 74 | if (WarnTrailing && NRead < S.size()) |
| 75 | WithColor::warning() << "trailing characters: " << S.c_str() + NRead |
| 76 | << "\n"; |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 77 | } else { |
Jonas Devlieghere | bada60a | 2018-11-11 22:11:47 +0000 | [diff] [blame] | 78 | WithColor::error() << "Invalid mangled name\n"; |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 79 | } |
| 80 | std::free(ResultBuf); |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 81 | return Status == llvm::demangle_success; |
Martin Storsjo | 21524be | 2018-07-20 20:48:36 +0000 | [diff] [blame] | 82 | } |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 83 | |
| 84 | int main(int argc, char **argv) { |
| 85 | InitLLVM X(argc, argv); |
| 86 | |
| 87 | cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n"); |
| 88 | |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 89 | if (!RawFile.empty()) { |
| 90 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 91 | MemoryBuffer::getFileOrSTDIN(RawFile); |
| 92 | if (std::error_code EC = FileOrErr.getError()) { |
| 93 | WithColor::error() << "Could not open input file \'" << RawFile |
| 94 | << "\': " << EC.message() << '\n'; |
| 95 | return 1; |
| 96 | } |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 97 | return msDemangle(std::string(FileOrErr->get()->getBuffer())) ? 0 : 1; |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool Success = true; |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 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 | |
| 108 | StringRef Line(LineStr); |
| 109 | Line = Line.trim(); |
| 110 | if (Line.empty() || Line.startswith("#") || Line.startswith(";")) |
| 111 | continue; |
| 112 | |
| 113 | // If the user is manually typing in these decorated names, don't echo |
| 114 | // them to the terminal a second time. If they're coming from redirected |
| 115 | // input, however, then we should display the input line so that the |
| 116 | // mangled and demangled name can be easily correlated in the output. |
Zachary Turner | c93b870 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 117 | if (!sys::Process::StandardInIsUserInput()) { |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 118 | outs() << Line << "\n"; |
Zachary Turner | c93b870 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 119 | outs().flush(); |
| 120 | } |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 121 | if (!msDemangle(std::string(Line))) |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 122 | Success = false; |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 123 | outs() << "\n"; |
| 124 | } |
| 125 | } else { |
| 126 | for (StringRef S : Symbols) { |
| 127 | outs() << S << "\n"; |
Zachary Turner | c93b870 | 2018-07-21 15:39:05 +0000 | [diff] [blame] | 128 | outs().flush(); |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 129 | if (!msDemangle(std::string(S))) |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 130 | Success = false; |
Zachary Turner | f435a7e | 2018-07-20 17:27:48 +0000 | [diff] [blame] | 131 | outs() << "\n"; |
| 132 | } |
| 133 | } |
| 134 | |
Nico Weber | 3bfa6a6 | 2019-04-16 12:51:40 +0000 | [diff] [blame] | 135 | return Success ? 0 : 1; |
Martin Storsjo | 21524be | 2018-07-20 20:48:36 +0000 | [diff] [blame] | 136 | } |