blob: 7e984f95642b7b0a1ba0f8f98aef9e2b672cf1da [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- CommandObjectHelp.cpp ---------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko26cac3a2016-02-20 00:58:29 +00009#include "CommandObjectHelp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Interpreter/CommandReturnObject.h"
12
13using namespace lldb;
14using namespace lldb_private;
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016// CommandObjectHelp
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Kate Stoneb9c1b512016-09-06 20:57:50 +000018void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
Jonas Devlieghered5b44032019-02-13 06:25:41 +000019 Stream *s, llvm::StringRef command, llvm::StringRef prefix,
20 llvm::StringRef subcommand, bool include_upropos,
21 bool include_type_lookup) {
Zachary Turnera49c2012016-11-16 21:34:22 +000022 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 Prantla9259742019-10-30 15:26:19 -070028 const std::string &lookup_str =
29 !subcommand_str.empty() ? subcommand_str : command_str;
Zachary Turnera49c2012016-11-16 21:34:22 +000030 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 Devlieghered5b44032019-02-13 06:25:41 +000033 if (include_upropos) {
Zachary Turnera49c2012016-11-16 21:34:22 +000034 s->Printf("Try '%sapropos %s' for a list of related commands.\n",
Adrian Prantla9259742019-10-30 15:26:19 -070035 prefix_str.c_str(), lookup_str.c_str());
Zachary Turnera49c2012016-11-16 21:34:22 +000036 }
37 if (include_type_lookup) {
38 s->Printf("Try '%stype lookup %s' for information on types, methods, "
39 "functions, modules, etc.",
Adrian Prantla9259742019-10-30 15:26:19 -070040 prefix_str.c_str(), lookup_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 }
Enrico Granata46d4aa22016-02-29 23:22:53 +000042}
43
Kate Stone7428a182016-07-14 22:03:10 +000044CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
Adrian Prantla9259742019-10-30 15:26:19 -070045 : CommandObjectParsed(interpreter, "help",
46 "Show a list of all debugger "
47 "commands, or give details "
48 "about a specific command.",
Kate Stone7428a182016-07-14 22:03:10 +000049 "help [<cmd-name>]"),
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 m_options() {
51 CommandArgumentEntry arg;
52 CommandArgumentData command_arg;
Caroline Tice405fe672010-10-04 22:28:36 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 // Define the first (and only) variant of this arg.
55 command_arg.arg_type = eArgTypeCommandName;
56 command_arg.arg_repetition = eArgRepeatStar;
Caroline Tice405fe672010-10-04 22:28:36 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 // There is only one variant this argument could be; put it into the argument
59 // entry.
60 arg.push_back(command_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 // Push the data for the first argument into the m_arguments vector.
63 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000066CommandObjectHelp::~CommandObjectHelp() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
Raphael Isemann6f4fb4e2019-07-12 15:30:55 +000068#define LLDB_OPTIONS_help
Raphael Isemannc5a2d742019-07-16 09:27:02 +000069#include "CommandOptions.inc"
Enrico Granata08633ee2011-09-09 17:49:36 +000070
Zachary Turner1f0f5b52016-09-22 20:22:55 +000071llvm::ArrayRef<OptionDefinition>
72CommandObjectHelp::CommandOptions::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +000073 return llvm::makeArrayRef(g_help_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
77 CommandObject::CommandMap::iterator pos;
78 CommandObject *cmd_obj;
79 const size_t argc = command.GetArgumentCount();
Enrico Granata08633ee2011-09-09 17:49:36 +000080
Adrian Prantl05097242018-04-30 16:49:04 +000081 // '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 Stoneb9c1b512016-09-06 20:57:50 +000084 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 Isemann0d9a2012019-09-13 11:26:48 +000099 auto command_name = command[0].ref();
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000100 cmd_obj = m_interpreter.GetCommandObject(command_name, &matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101
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 Turner97d2c402016-10-05 23:40:23 +0000107 // object that corresponds to the help command entered.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 std::string sub_command;
Zachary Turner97d2c402016-10-05 23:40:23 +0000109 for (auto &entry : command.entries().drop_front()) {
Raphael Isemann0d9a2012019-09-13 11:26:48 +0000110 sub_command = entry.ref();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 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 Turner97d2c402016-10-05 23:40:23 +0000117 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 } else {
119 CommandObject *found_cmd;
120 found_cmd =
121 sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
Zachary Turner97d2c402016-10-05 23:40:23 +0000122 if (found_cmd == nullptr || matches.GetSize() > 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 all_okay = false;
Zachary Turner97d2c402016-10-05 23:40:23 +0000124 break;
125 } else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 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 Turnerc1564272016-11-16 21:15:24 +0000141 result.AppendError(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 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 Turnerc1564272016-11-16 21:15:24 +0000149 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 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 Turnera4496982016-10-05 21:14:38 +0000158 sub_cmd_obj->GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 }
160 }
161
162 sub_cmd_obj->GenerateHelpText(result);
Jim Ingham79d81052018-12-21 01:45:28 +0000163 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 Stoneb9c1b512016-09-06 20:57:50 +0000168 StreamString sstr;
Jim Ingham79d81052018-12-21 01:45:28 +0000169 m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000171 command[0].c_str(), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 }
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 Turner14f6b2c2016-12-09 01:08:29 +0000185 CommandObject::LookupArgumentName(command_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 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 Turner14f6b2c2016-12-09 01:08:29 +0000192 GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name,
193 m_interpreter.GetCommandPrefix(),
194 "");
Zachary Turnerc1564272016-11-16 21:15:24 +0000195 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 result.SetStatus(eReturnStatusFailed);
197 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 }
200
201 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
Raphael Isemannae34ed22019-08-22 07:41:23 +0000204void CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 // Return the completions of the commands in the help system:
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000206 if (request.GetCursorIndex() == 0) {
Raphael Isemannae34ed22019-08-22 07:41:23 +0000207 m_interpreter.HandleCompletionMatches(request);
Raphael Isemann1153dc92019-08-22 09:02:54 +0000208 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 }
Raphael Isemann1153dc92019-08-22 09:02:54 +0000210 CommandObject *cmd_obj =
Raphael Isemann0d9a2012019-09-13 11:26:48 +0000211 m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref());
Raphael Isemann1153dc92019-08-22 09:02:54 +0000212
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 Isemannf8e733f2019-09-23 08:16:19 +0000218 request.ShiftArguments();
Raphael Isemann1153dc92019-08-22 09:02:54 +0000219 cmd_obj->HandleCompletion(request);
220 return;
221 }
222 m_interpreter.HandleCompletionMatches(request);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}