xref: /freebsd-src/contrib/llvm-project/lldb/source/Commands/CommandObjectHelp.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
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 
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>]") {
51349cc55cSDimitry Andric   // A list of command names forming a path to the command we want help on.
52349cc55cSDimitry Andric   // No names is allowed - in which case we dump the top-level help.
53*0fca6ea1SDimitry Andric   AddSimpleArgumentList(eArgTypeCommand, eArgRepeatStar);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric CommandObjectHelp::~CommandObjectHelp() = default;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #define LLDB_OPTIONS_help
590b57cec5SDimitry Andric #include "CommandOptions.inc"
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition>
620b57cec5SDimitry Andric CommandObjectHelp::CommandOptions::GetDefinitions() {
63bdd1243dSDimitry Andric   return llvm::ArrayRef(g_help_options);
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
665f757f3fSDimitry Andric void CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
670b57cec5SDimitry Andric   CommandObject::CommandMap::iterator pos;
680b57cec5SDimitry Andric   CommandObject *cmd_obj;
690b57cec5SDimitry Andric   const size_t argc = command.GetArgumentCount();
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   // 'help' doesn't take any arguments, other than command names.  If argc is
720b57cec5SDimitry Andric   // 0, we show the user all commands (aliases and user commands if asked for).
730b57cec5SDimitry Andric   // Otherwise every argument must be the name of a command or a sub-command.
740b57cec5SDimitry Andric   if (argc == 0) {
750b57cec5SDimitry Andric     uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
760b57cec5SDimitry Andric     if (m_options.m_show_aliases)
770b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesAliases;
78349cc55cSDimitry Andric     if (m_options.m_show_user_defined) {
790b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesUserDef;
80349cc55cSDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesUserMW;
81349cc55cSDimitry Andric     }
820b57cec5SDimitry Andric     if (m_options.m_show_hidden)
830b57cec5SDimitry Andric       cmd_types |= CommandInterpreter::eCommandTypesHidden;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
860b57cec5SDimitry Andric     m_interpreter.GetHelp(result, cmd_types); // General help
870b57cec5SDimitry Andric   } else {
880b57cec5SDimitry Andric     // Get command object for the first command argument. Only search built-in
890b57cec5SDimitry Andric     // command dictionary.
900b57cec5SDimitry Andric     StringList matches;
919dba64beSDimitry Andric     auto command_name = command[0].ref();
920b57cec5SDimitry Andric     cmd_obj = m_interpreter.GetCommandObject(command_name, &matches);
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric     if (cmd_obj != nullptr) {
950b57cec5SDimitry Andric       StringList matches;
960b57cec5SDimitry Andric       bool all_okay = true;
970b57cec5SDimitry Andric       CommandObject *sub_cmd_obj = cmd_obj;
980b57cec5SDimitry Andric       // Loop down through sub_command dictionaries until we find the command
990b57cec5SDimitry Andric       // object that corresponds to the help command entered.
1000b57cec5SDimitry Andric       std::string sub_command;
1010b57cec5SDimitry Andric       for (auto &entry : command.entries().drop_front()) {
1025ffd83dbSDimitry Andric         sub_command = std::string(entry.ref());
1030b57cec5SDimitry Andric         matches.Clear();
1040b57cec5SDimitry Andric         if (sub_cmd_obj->IsAlias())
1050b57cec5SDimitry Andric           sub_cmd_obj =
1060b57cec5SDimitry Andric               ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get();
1070b57cec5SDimitry Andric         if (!sub_cmd_obj->IsMultiwordObject()) {
1080b57cec5SDimitry Andric           all_okay = false;
1090b57cec5SDimitry Andric           break;
1100b57cec5SDimitry Andric         } else {
1110b57cec5SDimitry Andric           CommandObject *found_cmd;
1120b57cec5SDimitry Andric           found_cmd =
1130b57cec5SDimitry Andric               sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
1140b57cec5SDimitry Andric           if (found_cmd == nullptr || matches.GetSize() > 1) {
1150b57cec5SDimitry Andric             all_okay = false;
1160b57cec5SDimitry Andric             break;
1170b57cec5SDimitry Andric           } else
1180b57cec5SDimitry Andric             sub_cmd_obj = found_cmd;
1190b57cec5SDimitry Andric         }
1200b57cec5SDimitry Andric       }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric       if (!all_okay || (sub_cmd_obj == nullptr)) {
1230b57cec5SDimitry Andric         std::string cmd_string;
1240b57cec5SDimitry Andric         command.GetCommandString(cmd_string);
1250b57cec5SDimitry Andric         if (matches.GetSize() >= 2) {
1260b57cec5SDimitry Andric           StreamString s;
1270b57cec5SDimitry Andric           s.Printf("ambiguous command %s", cmd_string.c_str());
1280b57cec5SDimitry Andric           size_t num_matches = matches.GetSize();
1290b57cec5SDimitry Andric           for (size_t match_idx = 0; match_idx < num_matches; match_idx++) {
1300b57cec5SDimitry Andric             s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx));
1310b57cec5SDimitry Andric           }
1320b57cec5SDimitry Andric           s.Printf("\n");
1330b57cec5SDimitry Andric           result.AppendError(s.GetString());
1345f757f3fSDimitry Andric           return;
1350b57cec5SDimitry Andric         } else if (!sub_cmd_obj) {
1360b57cec5SDimitry Andric           StreamString error_msg_stream;
1370b57cec5SDimitry Andric           GenerateAdditionalHelpAvenuesMessage(
1380b57cec5SDimitry Andric               &error_msg_stream, cmd_string.c_str(),
1390b57cec5SDimitry Andric               m_interpreter.GetCommandPrefix(), sub_command.c_str());
1400b57cec5SDimitry Andric           result.AppendError(error_msg_stream.GetString());
1415f757f3fSDimitry Andric           return;
1420b57cec5SDimitry Andric         } else {
1430b57cec5SDimitry Andric           GenerateAdditionalHelpAvenuesMessage(
1440b57cec5SDimitry Andric               &result.GetOutputStream(), cmd_string.c_str(),
1450b57cec5SDimitry Andric               m_interpreter.GetCommandPrefix(), sub_command.c_str());
1460b57cec5SDimitry Andric           result.GetOutputStream().Printf(
1470b57cec5SDimitry Andric               "\nThe closest match is '%s'. Help on it follows.\n\n",
1480b57cec5SDimitry Andric               sub_cmd_obj->GetCommandName().str().c_str());
1490b57cec5SDimitry Andric         }
1500b57cec5SDimitry Andric       }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric       sub_cmd_obj->GenerateHelpText(result);
1530b57cec5SDimitry Andric       std::string alias_full_name;
1540b57cec5SDimitry Andric       // Don't use AliasExists here, that only checks exact name matches.  If
1550b57cec5SDimitry Andric       // the user typed a shorter unique alias name, we should still tell them
1560b57cec5SDimitry Andric       // it was an alias.
1570b57cec5SDimitry Andric       if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) {
1580b57cec5SDimitry Andric         StreamString sstr;
1590b57cec5SDimitry Andric         m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr);
1600b57cec5SDimitry Andric         result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
1610b57cec5SDimitry Andric                                         command[0].c_str(), sstr.GetData());
1620b57cec5SDimitry Andric       }
1630b57cec5SDimitry Andric     } else if (matches.GetSize() > 0) {
1640b57cec5SDimitry Andric       Stream &output_strm = result.GetOutputStream();
1650b57cec5SDimitry Andric       output_strm.Printf("Help requested with ambiguous command name, possible "
1660b57cec5SDimitry Andric                          "completions:\n");
1670b57cec5SDimitry Andric       const size_t match_count = matches.GetSize();
1680b57cec5SDimitry Andric       for (size_t i = 0; i < match_count; i++) {
1690b57cec5SDimitry Andric         output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
1700b57cec5SDimitry Andric       }
1710b57cec5SDimitry Andric     } else {
1720b57cec5SDimitry Andric       // Maybe the user is asking for help about a command argument rather than
1730b57cec5SDimitry Andric       // a command.
1740b57cec5SDimitry Andric       const CommandArgumentType arg_type =
1750b57cec5SDimitry Andric           CommandObject::LookupArgumentName(command_name);
1760b57cec5SDimitry Andric       if (arg_type != eArgTypeLastArg) {
1770b57cec5SDimitry Andric         Stream &output_strm = result.GetOutputStream();
1780b57cec5SDimitry Andric         CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter);
1790b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1800b57cec5SDimitry Andric       } else {
1810b57cec5SDimitry Andric         StreamString error_msg_stream;
1820b57cec5SDimitry Andric         GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name,
1830b57cec5SDimitry Andric                                              m_interpreter.GetCommandPrefix(),
1840b57cec5SDimitry Andric                                              "");
1850b57cec5SDimitry Andric         result.AppendError(error_msg_stream.GetString());
1860b57cec5SDimitry Andric       }
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric 
1919dba64beSDimitry Andric void CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
1920b57cec5SDimitry Andric   // Return the completions of the commands in the help system:
1930b57cec5SDimitry Andric   if (request.GetCursorIndex() == 0) {
1949dba64beSDimitry Andric     m_interpreter.HandleCompletionMatches(request);
1959dba64beSDimitry Andric     return;
1969dba64beSDimitry Andric   }
1970b57cec5SDimitry Andric   CommandObject *cmd_obj =
1989dba64beSDimitry Andric       m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref());
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   // The command that they are getting help on might be ambiguous, in which
2010b57cec5SDimitry Andric   // case we should complete that, otherwise complete with the command the
2020b57cec5SDimitry Andric   // user is getting help on...
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   if (cmd_obj) {
2059dba64beSDimitry Andric     request.ShiftArguments();
2069dba64beSDimitry Andric     cmd_obj->HandleCompletion(request);
2079dba64beSDimitry Andric     return;
2080b57cec5SDimitry Andric   }
2099dba64beSDimitry Andric   m_interpreter.HandleCompletionMatches(request);
2100b57cec5SDimitry Andric }
211