Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 9 | #include "CommandObjectHelp.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "lldb/Interpreter/CommandInterpreter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 11 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | //------------------------------------------------------------------------- |
| 19 | // CommandObjectHelp |
| 20 | //------------------------------------------------------------------------- |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage( |
Zachary Turner | a49c201 | 2016-11-16 21:34:22 +0000 | [diff] [blame] | 23 | Stream *s, llvm::StringRef command, llvm::StringRef prefix, llvm::StringRef subcommand, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | bool include_apropos, bool include_type_lookup) { |
Zachary Turner | a49c201 | 2016-11-16 21:34:22 +0000 | [diff] [blame] | 25 | if (!s || command.empty()) |
| 26 | return; |
| 27 | |
| 28 | std::string command_str = command.str(); |
| 29 | std::string prefix_str = prefix.str(); |
| 30 | std::string subcommand_str = subcommand.str(); |
| 31 | const std::string &lookup_str = !subcommand_str.empty() ? subcommand_str : command_str; |
| 32 | s->Printf("'%s' is not a known command.\n", command_str.c_str()); |
| 33 | s->Printf("Try '%shelp' to see a current list of commands.\n", |
| 34 | prefix.str().c_str()); |
| 35 | if (include_apropos) { |
| 36 | s->Printf("Try '%sapropos %s' for a list of related commands.\n", |
| 37 | prefix_str.c_str(), lookup_str.c_str()); |
| 38 | } |
| 39 | if (include_type_lookup) { |
| 40 | s->Printf("Try '%stype lookup %s' for information on types, methods, " |
| 41 | "functions, modules, etc.", |
| 42 | prefix_str.c_str(), lookup_str.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | } |
Enrico Granata | 46d4aa2 | 2016-02-29 23:22:53 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 46 | CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | : CommandObjectParsed(interpreter, "help", "Show a list of all debugger " |
| 48 | "commands, or give details " |
| 49 | "about a specific command.", |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 50 | "help [<cmd-name>]"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | m_options() { |
| 52 | CommandArgumentEntry arg; |
| 53 | CommandArgumentData command_arg; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | // Define the first (and only) variant of this arg. |
| 56 | command_arg.arg_type = eArgTypeCommandName; |
| 57 | command_arg.arg_repetition = eArgRepeatStar; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | // There is only one variant this argument could be; put it into the argument |
| 60 | // entry. |
| 61 | arg.push_back(command_arg); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | // Push the data for the first argument into the m_arguments vector. |
| 64 | m_arguments.push_back(arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Eugene Zelenko | 26cac3a | 2016-02-20 00:58:29 +0000 | [diff] [blame] | 67 | CommandObjectHelp::~CommandObjectHelp() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 69 | static constexpr OptionDefinition g_help_options[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | // clang-format off |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 71 | {LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide aliases in the command list."}, |
| 72 | {LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide user-defined commands from the list."}, |
| 73 | {LLDB_OPT_SET_ALL, false, "show-hidden-commands", 'h', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Include commands prefixed with an underscore."}, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | // clang-format on |
Enrico Granata | 08633ee | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 77 | llvm::ArrayRef<OptionDefinition> |
| 78 | CommandObjectHelp::CommandOptions::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 79 | return llvm::makeArrayRef(g_help_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) { |
| 83 | CommandObject::CommandMap::iterator pos; |
| 84 | CommandObject *cmd_obj; |
| 85 | const size_t argc = command.GetArgumentCount(); |
Enrico Granata | 08633ee | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 86 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 87 | // 'help' doesn't take any arguments, other than command names. If argc is |
| 88 | // 0, we show the user all commands (aliases and user commands if asked for). |
| 89 | // Otherwise every argument must be the name of a command or a sub-command. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | if (argc == 0) { |
| 91 | uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin; |
| 92 | if (m_options.m_show_aliases) |
| 93 | cmd_types |= CommandInterpreter::eCommandTypesAliases; |
| 94 | if (m_options.m_show_user_defined) |
| 95 | cmd_types |= CommandInterpreter::eCommandTypesUserDef; |
| 96 | if (m_options.m_show_hidden) |
| 97 | cmd_types |= CommandInterpreter::eCommandTypesHidden; |
| 98 | |
| 99 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 100 | m_interpreter.GetHelp(result, cmd_types); // General help |
| 101 | } else { |
| 102 | // Get command object for the first command argument. Only search built-in |
| 103 | // command dictionary. |
| 104 | StringList matches; |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 105 | auto command_name = command[0].ref; |
| 106 | cmd_obj = m_interpreter.GetCommandObject(command_name, &matches); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | |
| 108 | if (cmd_obj != nullptr) { |
| 109 | StringList matches; |
| 110 | bool all_okay = true; |
| 111 | CommandObject *sub_cmd_obj = cmd_obj; |
| 112 | // Loop down through sub_command dictionaries until we find the command |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 113 | // object that corresponds to the help command entered. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | std::string sub_command; |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 115 | for (auto &entry : command.entries().drop_front()) { |
| 116 | sub_command = entry.ref; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | matches.Clear(); |
| 118 | if (sub_cmd_obj->IsAlias()) |
| 119 | sub_cmd_obj = |
| 120 | ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get(); |
| 121 | if (!sub_cmd_obj->IsMultiwordObject()) { |
| 122 | all_okay = false; |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 123 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | } else { |
| 125 | CommandObject *found_cmd; |
| 126 | found_cmd = |
| 127 | sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches); |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 128 | if (found_cmd == nullptr || matches.GetSize() > 1) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | all_okay = false; |
Zachary Turner | 97d2c40 | 2016-10-05 23:40:23 +0000 | [diff] [blame] | 130 | break; |
| 131 | } else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | sub_cmd_obj = found_cmd; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!all_okay || (sub_cmd_obj == nullptr)) { |
| 137 | std::string cmd_string; |
| 138 | command.GetCommandString(cmd_string); |
| 139 | if (matches.GetSize() >= 2) { |
| 140 | StreamString s; |
| 141 | s.Printf("ambiguous command %s", cmd_string.c_str()); |
| 142 | size_t num_matches = matches.GetSize(); |
| 143 | for (size_t match_idx = 0; match_idx < num_matches; match_idx++) { |
| 144 | s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx)); |
| 145 | } |
| 146 | s.Printf("\n"); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 147 | result.AppendError(s.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | result.SetStatus(eReturnStatusFailed); |
| 149 | return false; |
| 150 | } else if (!sub_cmd_obj) { |
| 151 | StreamString error_msg_stream; |
| 152 | GenerateAdditionalHelpAvenuesMessage( |
| 153 | &error_msg_stream, cmd_string.c_str(), |
| 154 | m_interpreter.GetCommandPrefix(), sub_command.c_str()); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 155 | result.AppendError(error_msg_stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | result.SetStatus(eReturnStatusFailed); |
| 157 | return false; |
| 158 | } else { |
| 159 | GenerateAdditionalHelpAvenuesMessage( |
| 160 | &result.GetOutputStream(), cmd_string.c_str(), |
| 161 | m_interpreter.GetCommandPrefix(), sub_command.c_str()); |
| 162 | result.GetOutputStream().Printf( |
| 163 | "\nThe closest match is '%s'. Help on it follows.\n\n", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame] | 164 | sub_cmd_obj->GetCommandName().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | sub_cmd_obj->GenerateHelpText(result); |
Jim Ingham | 79d8105 | 2018-12-21 01:45:28 +0000 | [diff] [blame] | 169 | std::string alias_full_name; |
| 170 | // Don't use AliasExists here, that only checks exact name matches. If |
| 171 | // the user typed a shorter unique alias name, we should still tell them |
| 172 | // it was an alias. |
| 173 | if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | StreamString sstr; |
Jim Ingham | 79d8105 | 2018-12-21 01:45:28 +0000 | [diff] [blame] | 175 | m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n", |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 177 | command[0].c_str(), sstr.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | } |
| 179 | } else if (matches.GetSize() > 0) { |
| 180 | Stream &output_strm = result.GetOutputStream(); |
| 181 | output_strm.Printf("Help requested with ambiguous command name, possible " |
| 182 | "completions:\n"); |
| 183 | const size_t match_count = matches.GetSize(); |
| 184 | for (size_t i = 0; i < match_count; i++) { |
| 185 | output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); |
| 186 | } |
| 187 | } else { |
| 188 | // Maybe the user is asking for help about a command argument rather than |
| 189 | // a command. |
| 190 | const CommandArgumentType arg_type = |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 191 | CommandObject::LookupArgumentName(command_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | if (arg_type != eArgTypeLastArg) { |
| 193 | Stream &output_strm = result.GetOutputStream(); |
| 194 | CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter); |
| 195 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 196 | } else { |
| 197 | StreamString error_msg_stream; |
Zachary Turner | 14f6b2c | 2016-12-09 01:08:29 +0000 | [diff] [blame] | 198 | GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name, |
| 199 | m_interpreter.GetCommandPrefix(), |
| 200 | ""); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 201 | result.AppendError(error_msg_stream.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | result.SetStatus(eReturnStatusFailed); |
| 203 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return result.Succeeded(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 210 | int CommandObjectHelp::HandleCompletion(CompletionRequest &request) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | // Return the completions of the commands in the help system: |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 212 | if (request.GetCursorIndex() == 0) { |
| 213 | return m_interpreter.HandleCompletionMatches(request); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | } else { |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 215 | CommandObject *cmd_obj = |
| 216 | m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | |
| 218 | // The command that they are getting help on might be ambiguous, in which |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 219 | // case we should complete that, otherwise complete with the command the |
| 220 | // user is getting help on... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | |
| 222 | if (cmd_obj) { |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 223 | request.GetParsedLine().Shift(); |
| 224 | request.SetCursorIndex(request.GetCursorIndex() - 1); |
| 225 | return cmd_obj->HandleCompletion(request); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | } else { |
Raphael Isemann | 2443bbd | 2018-07-02 21:29:56 +0000 | [diff] [blame] | 227 | return m_interpreter.HandleCompletionMatches(request); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | } |