15ffd83dbSDimitry Andric //===-- CommandObjectHelp.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectHelp.h"
100b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
11fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
120b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric using namespace lldb;
150b57cec5SDimitry Andric using namespace lldb_private;
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // CommandObjectHelp
180b57cec5SDimitry Andric 
GenerateAdditionalHelpAvenuesMessage(Stream * s,llvm::StringRef command,llvm::StringRef prefix,llvm::StringRef subcommand,bool include_upropos,bool include_type_lookup)190b57cec5SDimitry Andric void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
200b57cec5SDimitry Andric     Stream *s, llvm::StringRef command, llvm::StringRef prefix,
210b57cec5SDimitry Andric     llvm::StringRef subcommand, bool include_upropos,
220b57cec5SDimitry Andric     bool include_type_lookup) {
230b57cec5SDimitry Andric   if (!s || command.empty())
240b57cec5SDimitry Andric     return;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric   std::string command_str = command.str();
270b57cec5SDimitry Andric   std::string prefix_str = prefix.str();
280b57cec5SDimitry Andric   std::string subcommand_str = subcommand.str();
29480093f4SDimitry Andric   const std::string &lookup_str =
30480093f4SDimitry Andric       !subcommand_str.empty() ? subcommand_str : command_str;
310b57cec5SDimitry Andric   s->Printf("'%s' is not a known command.\n", command_str.c_str());
320b57cec5SDimitry Andric   s->Printf("Try '%shelp' to see a current list of commands.\n",
330b57cec5SDimitry Andric             prefix.str().c_str());
340b57cec5SDimitry Andric   if (include_upropos) {
350b57cec5SDimitry Andric     s->Printf("Try '%sapropos %s' for a list of related commands.\n",
360b57cec5SDimitry Andric               prefix_str.c_str(), lookup_str.c_str());
370b57cec5SDimitry Andric   }
380b57cec5SDimitry Andric   if (include_type_lookup) {
390b57cec5SDimitry Andric     s->Printf("Try '%stype lookup %s' for information on types, methods, "
400b57cec5SDimitry Andric               "functions, modules, etc.",
410b57cec5SDimitry Andric               prefix_str.c_str(), lookup_str.c_str());
420b57cec5SDimitry Andric   }
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric 
CommandObjectHelp(CommandInterpreter & interpreter)450b57cec5SDimitry Andric CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
46480093f4SDimitry Andric     : CommandObjectParsed(interpreter, "help",
47480093f4SDimitry Andric                           "Show a list of all debugger "
480b57cec5SDimitry Andric                           "commands, or give details "
490b57cec5SDimitry Andric                           "about a specific command.",
5004eeddc0SDimitry Andric                           "help [<cmd-name>]") {
510b57cec5SDimitry Andric   CommandArgumentEntry arg;
520b57cec5SDimitry Andric   CommandArgumentData command_arg;
530b57cec5SDimitry Andric 
54349cc55cSDimitry Andric   // A list of command names forming a path to the command we want help on.
55349cc55cSDimitry Andric   // No names is allowed - in which case we dump the top-level help.
56349cc55cSDimitry Andric   command_arg.arg_type = eArgTypeCommand;
570b57cec5SDimitry Andric   command_arg.arg_repetition = eArgRepeatStar;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // There is only one variant this argument could be; put it into the argument
600b57cec5SDimitry Andric   // entry.
610b57cec5SDimitry Andric   arg.push_back(command_arg);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   // Push the data for the first argument into the m_arguments vector.
640b57cec5SDimitry Andric   m_arguments.push_back(arg);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric CommandObjectHelp::~CommandObjectHelp() = default;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric #define LLDB_OPTIONS_help
700b57cec5SDimitry Andric #include "CommandOptions.inc"
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()730b57cec5SDimitry Andric CommandObjectHelp::CommandOptions::GetDefinitions() {
74bdd1243dSDimitry Andric   return llvm::ArrayRef(g_help_options);
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)77*c9157d92SDimitry Andric void CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
780b57cec5SDimitry Andric   CommandObject::CommandMap::iterator pos;
790b57cec5SDimitry Andric   CommandObject *cmd_obj;
800b57cec5SDimitry Andric   const size_t argc = command.GetArgumentCount();
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // 'help' doesn't take any arguments, other than command names.  If argc is
830b57cec5SDimitry Andric   // 0, we show the user all commands (aliases and user commands if asked for).
840b57cec5SDimitry Andric   // Otherwise every argument must be the name of a command or a sub-command.
850b57cec5SDimitry Andric   if (argc == 0) {
860b57cec5SDimitry Andric     uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
870b57cec5SDimitry Andric     if (m_options.m_show_aliases)
880b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesAliases;
89349cc55cSDimitry Andric     if (m_options.m_show_user_defined) {
900b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesUserDef;
91349cc55cSDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesUserMW;
92349cc55cSDimitry Andric     }
930b57cec5SDimitry Andric     if (m_options.m_show_hidden)
940b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesHidden;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
970b57cec5SDimitry Andric     m_interpreter.GetHelp(result, cmd_types); // General help
980b57cec5SDimitry Andric   } else {
990b57cec5SDimitry Andric     // Get command object for the first command argument. Only search built-in
1000b57cec5SDimitry Andric     // command dictionary.
1010b57cec5SDimitry Andric     StringList matches;
1029dba64beSDimitry Andric     auto command_name = command[0].ref();
1030b57cec5SDimitry Andric     cmd_obj = m_interpreter.GetCommandObject(command_name, &matches);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     if (cmd_obj != nullptr) {
1060b57cec5SDimitry Andric       StringList matches;
1070b57cec5SDimitry Andric       bool all_okay = true;
1080b57cec5SDimitry Andric       CommandObject *sub_cmd_obj = cmd_obj;
1090b57cec5SDimitry Andric       // Loop down through sub_command dictionaries until we find the command
1100b57cec5SDimitry Andric       // object that corresponds to the help command entered.
1110b57cec5SDimitry Andric       std::string sub_command;
1120b57cec5SDimitry Andric       for (auto &entry : command.entries().drop_front()) {
1135ffd83dbSDimitry Andric         sub_command = std::string(entry.ref());
1140b57cec5SDimitry Andric         matches.Clear();
1150b57cec5SDimitry Andric         if (sub_cmd_obj->IsAlias())
1160b57cec5SDimitry Andric           sub_cmd_obj =
1170b57cec5SDimitry Andric               ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get();
1180b57cec5SDimitry Andric         if (!sub_cmd_obj->IsMultiwordObject()) {
1190b57cec5SDimitry Andric           all_okay = false;
1200b57cec5SDimitry Andric           break;
1210b57cec5SDimitry Andric         } else {
1220b57cec5SDimitry Andric           CommandObject *found_cmd;
1230b57cec5SDimitry Andric           found_cmd =
1240b57cec5SDimitry Andric               sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
1250b57cec5SDimitry Andric           if (found_cmd == nullptr || matches.GetSize() > 1) {
1260b57cec5SDimitry Andric             all_okay = false;
1270b57cec5SDimitry Andric             break;
1280b57cec5SDimitry Andric           } else
1290b57cec5SDimitry Andric             sub_cmd_obj = found_cmd;
1300b57cec5SDimitry Andric         }
1310b57cec5SDimitry Andric       }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       if (!all_okay || (sub_cmd_obj == nullptr)) {
1340b57cec5SDimitry Andric         std::string cmd_string;
1350b57cec5SDimitry Andric         command.GetCommandString(cmd_string);
1360b57cec5SDimitry Andric         if (matches.GetSize() >= 2) {
1370b57cec5SDimitry Andric           StreamString s;
1380b57cec5SDimitry Andric           s.Printf("ambiguous command %s", cmd_string.c_str());
1390b57cec5SDimitry Andric           size_t num_matches = matches.GetSize();
1400b57cec5SDimitry Andric           for (size_t match_idx = 0; match_idx < num_matches; match_idx++) {
1410b57cec5SDimitry Andric             s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx));
1420b57cec5SDimitry Andric           }
1430b57cec5SDimitry Andric           s.Printf("\n");
1440b57cec5SDimitry Andric           result.AppendError(s.GetString());
145*c9157d92SDimitry Andric           return;
1460b57cec5SDimitry Andric         } else if (!sub_cmd_obj) {
1470b57cec5SDimitry Andric           StreamString error_msg_stream;
1480b57cec5SDimitry Andric           GenerateAdditionalHelpAvenuesMessage(
1490b57cec5SDimitry Andric               &error_msg_stream, cmd_string.c_str(),
1500b57cec5SDimitry Andric               m_interpreter.GetCommandPrefix(), sub_command.c_str());
1510b57cec5SDimitry Andric           result.AppendError(error_msg_stream.GetString());
152*c9157d92SDimitry Andric           return;
1530b57cec5SDimitry Andric         } else {
1540b57cec5SDimitry Andric           GenerateAdditionalHelpAvenuesMessage(
1550b57cec5SDimitry Andric               &result.GetOutputStream(), cmd_string.c_str(),
1560b57cec5SDimitry Andric               m_interpreter.GetCommandPrefix(), sub_command.c_str());
1570b57cec5SDimitry Andric           result.GetOutputStream().Printf(
1580b57cec5SDimitry Andric               "\nThe closest match is '%s'. Help on it follows.\n\n",
1590b57cec5SDimitry Andric               sub_cmd_obj->GetCommandName().str().c_str());
1600b57cec5SDimitry Andric         }
1610b57cec5SDimitry Andric       }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric       sub_cmd_obj->GenerateHelpText(result);
1640b57cec5SDimitry Andric       std::string alias_full_name;
1650b57cec5SDimitry Andric       // Don't use AliasExists here, that only checks exact name matches.  If
1660b57cec5SDimitry Andric       // the user typed a shorter unique alias name, we should still tell them
1670b57cec5SDimitry Andric       // it was an alias.
1680b57cec5SDimitry Andric       if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) {
1690b57cec5SDimitry Andric         StreamString sstr;
1700b57cec5SDimitry Andric         m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr);
1710b57cec5SDimitry Andric         result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
1720b57cec5SDimitry Andric                                         command[0].c_str(), sstr.GetData());
1730b57cec5SDimitry Andric       }
1740b57cec5SDimitry Andric     } else if (matches.GetSize() > 0) {
1750b57cec5SDimitry Andric       Stream &output_strm = result.GetOutputStream();
1760b57cec5SDimitry Andric       output_strm.Printf("Help requested with ambiguous command name, possible "
1770b57cec5SDimitry Andric                          "completions:\n");
1780b57cec5SDimitry Andric       const size_t match_count = matches.GetSize();
1790b57cec5SDimitry Andric       for (size_t i = 0; i < match_count; i++) {
1800b57cec5SDimitry Andric         output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
1810b57cec5SDimitry Andric       }
1820b57cec5SDimitry Andric     } else {
1830b57cec5SDimitry Andric       // Maybe the user is asking for help about a command argument rather than
1840b57cec5SDimitry Andric       // a command.
1850b57cec5SDimitry Andric       const CommandArgumentType arg_type =
1860b57cec5SDimitry Andric           CommandObject::LookupArgumentName(command_name);
1870b57cec5SDimitry Andric       if (arg_type != eArgTypeLastArg) {
1880b57cec5SDimitry Andric         Stream &output_strm = result.GetOutputStream();
1890b57cec5SDimitry Andric         CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter);
1900b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1910b57cec5SDimitry Andric       } else {
1920b57cec5SDimitry Andric         StreamString error_msg_stream;
1930b57cec5SDimitry Andric         GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name,
1940b57cec5SDimitry Andric                                              m_interpreter.GetCommandPrefix(),
1950b57cec5SDimitry Andric                                              "");
1960b57cec5SDimitry Andric         result.AppendError(error_msg_stream.GetString());
1970b57cec5SDimitry Andric       }
1980b57cec5SDimitry Andric     }
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
HandleCompletion(CompletionRequest & request)2029dba64beSDimitry Andric void CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
2030b57cec5SDimitry Andric   // Return the completions of the commands in the help system:
2040b57cec5SDimitry Andric   if (request.GetCursorIndex() == 0) {
2059dba64beSDimitry Andric     m_interpreter.HandleCompletionMatches(request);
2069dba64beSDimitry Andric     return;
2079dba64beSDimitry Andric   }
2080b57cec5SDimitry Andric   CommandObject *cmd_obj =
2099dba64beSDimitry Andric       m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref());
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   // The command that they are getting help on might be ambiguous, in which
2120b57cec5SDimitry Andric   // case we should complete that, otherwise complete with the command the
2130b57cec5SDimitry Andric   // user is getting help on...
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   if (cmd_obj) {
2169dba64beSDimitry Andric     request.ShiftArguments();
2179dba64beSDimitry Andric     cmd_obj->HandleCompletion(request);
2189dba64beSDimitry Andric     return;
2190b57cec5SDimitry Andric   }
2209dba64beSDimitry Andric   m_interpreter.HandleCompletionMatches(request);
2210b57cec5SDimitry Andric }
222