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