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