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