130fdc8d8SChris Lattner //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 926cac3afSEugene Zelenko #include "CommandObjectHelp.h" 1030fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 11b9c1b51eSKate Stone #include "lldb/Interpreter/CommandObjectMultiword.h" 1230fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 13b9c1b51eSKate Stone #include "lldb/Interpreter/Options.h" 1430fdc8d8SChris Lattner 1530fdc8d8SChris Lattner using namespace lldb; 1630fdc8d8SChris Lattner using namespace lldb_private; 1730fdc8d8SChris Lattner 1830fdc8d8SChris Lattner // CommandObjectHelp 1930fdc8d8SChris Lattner 20b9c1b51eSKate Stone void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage( 21d5b44036SJonas Devlieghere Stream *s, llvm::StringRef command, llvm::StringRef prefix, 22d5b44036SJonas Devlieghere llvm::StringRef subcommand, bool include_upropos, 23d5b44036SJonas Devlieghere bool include_type_lookup) { 24a49c2019SZachary Turner if (!s || command.empty()) 25a49c2019SZachary Turner return; 26a49c2019SZachary Turner 27a49c2019SZachary Turner std::string command_str = command.str(); 28a49c2019SZachary Turner std::string prefix_str = prefix.str(); 29a49c2019SZachary Turner std::string subcommand_str = subcommand.str(); 30a49c2019SZachary Turner const std::string &lookup_str = !subcommand_str.empty() ? subcommand_str : command_str; 31a49c2019SZachary Turner s->Printf("'%s' is not a known command.\n", command_str.c_str()); 32b9c1b51eSKate Stone s->Printf("Try '%shelp' to see a current list of commands.\n", 33a49c2019SZachary Turner prefix.str().c_str()); 34d5b44036SJonas Devlieghere if (include_upropos) { 3525d6072aSKate Stone s->Printf("Try '%sapropos %s' for a list of related commands.\n", 36a49c2019SZachary Turner prefix_str.c_str(), lookup_str.c_str()); 3746d4aa21SEnrico Granata } 38b9c1b51eSKate Stone if (include_type_lookup) { 39b9c1b51eSKate Stone s->Printf("Try '%stype lookup %s' for information on types, methods, " 40b9c1b51eSKate Stone "functions, modules, etc.", 41a49c2019SZachary Turner prefix_str.c_str(), lookup_str.c_str()); 4246d4aa21SEnrico Granata } 4346d4aa21SEnrico Granata } 4446d4aa21SEnrico Granata 457428a18cSKate Stone CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter) 46b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "help", "Show a list of all debugger " 47b9c1b51eSKate Stone "commands, or give details " 48b9c1b51eSKate Stone "about a specific command.", 497428a18cSKate Stone "help [<cmd-name>]"), 50b9c1b51eSKate Stone m_options() { 51405fe67fSCaroline Tice CommandArgumentEntry arg; 52405fe67fSCaroline Tice CommandArgumentData command_arg; 53405fe67fSCaroline Tice 54405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 55405fe67fSCaroline Tice command_arg.arg_type = eArgTypeCommandName; 56405fe67fSCaroline Tice command_arg.arg_repetition = eArgRepeatStar; 57405fe67fSCaroline Tice 58b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 59b9c1b51eSKate Stone // entry. 60405fe67fSCaroline Tice arg.push_back(command_arg); 61405fe67fSCaroline Tice 62405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 63405fe67fSCaroline Tice m_arguments.push_back(arg); 6430fdc8d8SChris Lattner } 6530fdc8d8SChris Lattner 6626cac3afSEugene Zelenko CommandObjectHelp::~CommandObjectHelp() = default; 6730fdc8d8SChris Lattner 688fe53c49STatyana Krasnukha static constexpr OptionDefinition g_help_options[] = { 69*6f4fb4e7SRaphael Isemann #define LLDB_OPTIONS_help 70*6f4fb4e7SRaphael Isemann #include "Options.inc" 7108633eeaSEnrico Granata }; 7208633eeaSEnrico Granata 731f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 741f0f5b5bSZachary Turner CommandObjectHelp::CommandOptions::GetDefinitions() { 7570602439SZachary Turner return llvm::makeArrayRef(g_help_options); 761f0f5b5bSZachary Turner } 771f0f5b5bSZachary Turner 78b9c1b51eSKate Stone bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) { 7930fdc8d8SChris Lattner CommandObject::CommandMap::iterator pos; 8030fdc8d8SChris Lattner CommandObject *cmd_obj; 81c7bece56SGreg Clayton const size_t argc = command.GetArgumentCount(); 8230fdc8d8SChris Lattner 8305097246SAdrian Prantl // 'help' doesn't take any arguments, other than command names. If argc is 8405097246SAdrian Prantl // 0, we show the user all commands (aliases and user commands if asked for). 8505097246SAdrian Prantl // Otherwise every argument must be the name of a command or a sub-command. 86b9c1b51eSKate Stone if (argc == 0) { 8708633eeaSEnrico Granata uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin; 8808633eeaSEnrico Granata if (m_options.m_show_aliases) 8908633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesAliases; 9008633eeaSEnrico Granata if (m_options.m_show_user_defined) 9108633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesUserDef; 92a487aa4cSKate Stone if (m_options.m_show_hidden) 93a487aa4cSKate Stone cmd_types |= CommandInterpreter::eCommandTypesHidden; 9408633eeaSEnrico Granata 9530fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishNoResult); 9608633eeaSEnrico Granata m_interpreter.GetHelp(result, cmd_types); // General help 97b9c1b51eSKate Stone } else { 98b9c1b51eSKate Stone // Get command object for the first command argument. Only search built-in 99b9c1b51eSKate Stone // command dictionary. 100279a6c26SJim Ingham StringList matches; 10114f6b2c0SZachary Turner auto command_name = command[0].ref; 10214f6b2c0SZachary Turner cmd_obj = m_interpreter.GetCommandObject(command_name, &matches); 10330fdc8d8SChris Lattner 104b9c1b51eSKate Stone if (cmd_obj != nullptr) { 105271ad29aSJim Ingham StringList matches; 10630fdc8d8SChris Lattner bool all_okay = true; 10730fdc8d8SChris Lattner CommandObject *sub_cmd_obj = cmd_obj; 108b9c1b51eSKate Stone // Loop down through sub_command dictionaries until we find the command 10997d2c401SZachary Turner // object that corresponds to the help command entered. 11046d4aa21SEnrico Granata std::string sub_command; 11197d2c401SZachary Turner for (auto &entry : command.entries().drop_front()) { 11297d2c401SZachary Turner sub_command = entry.ref; 113271ad29aSJim Ingham matches.Clear(); 114bef55ac8SEnrico Granata if (sub_cmd_obj->IsAlias()) 115b9c1b51eSKate Stone sub_cmd_obj = 116b9c1b51eSKate Stone ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get(); 117b9c1b51eSKate Stone if (!sub_cmd_obj->IsMultiwordObject()) { 11830fdc8d8SChris Lattner all_okay = false; 11997d2c401SZachary Turner break; 120b9c1b51eSKate Stone } else { 121271ad29aSJim Ingham CommandObject *found_cmd; 122b9c1b51eSKate Stone found_cmd = 123b9c1b51eSKate Stone sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches); 12497d2c401SZachary Turner if (found_cmd == nullptr || matches.GetSize() > 1) { 12530fdc8d8SChris Lattner all_okay = false; 12697d2c401SZachary Turner break; 12797d2c401SZachary Turner } else 128271ad29aSJim Ingham sub_cmd_obj = found_cmd; 12930fdc8d8SChris Lattner } 13030fdc8d8SChris Lattner } 13130fdc8d8SChris Lattner 132b9c1b51eSKate Stone if (!all_okay || (sub_cmd_obj == nullptr)) { 13330fdc8d8SChris Lattner std::string cmd_string; 13430fdc8d8SChris Lattner command.GetCommandString(cmd_string); 135b9c1b51eSKate Stone if (matches.GetSize() >= 2) { 136271ad29aSJim Ingham StreamString s; 137271ad29aSJim Ingham s.Printf("ambiguous command %s", cmd_string.c_str()); 138271ad29aSJim Ingham size_t num_matches = matches.GetSize(); 139b9c1b51eSKate Stone for (size_t match_idx = 0; match_idx < num_matches; match_idx++) { 140271ad29aSJim Ingham s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx)); 141271ad29aSJim Ingham } 142271ad29aSJim Ingham s.Printf("\n"); 143c156427dSZachary Turner result.AppendError(s.GetString()); 14430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 1459b62d1d5SEnrico Granata return false; 146b9c1b51eSKate Stone } else if (!sub_cmd_obj) { 14746d4aa21SEnrico Granata StreamString error_msg_stream; 148b9c1b51eSKate Stone GenerateAdditionalHelpAvenuesMessage( 149b9c1b51eSKate Stone &error_msg_stream, cmd_string.c_str(), 150b9c1b51eSKate Stone m_interpreter.GetCommandPrefix(), sub_command.c_str()); 151c156427dSZachary Turner result.AppendError(error_msg_stream.GetString()); 1529b62d1d5SEnrico Granata result.SetStatus(eReturnStatusFailed); 1539b62d1d5SEnrico Granata return false; 154b9c1b51eSKate Stone } else { 155b9c1b51eSKate Stone GenerateAdditionalHelpAvenuesMessage( 156b9c1b51eSKate Stone &result.GetOutputStream(), cmd_string.c_str(), 157b9c1b51eSKate Stone m_interpreter.GetCommandPrefix(), sub_command.c_str()); 158b9c1b51eSKate Stone result.GetOutputStream().Printf( 159b9c1b51eSKate Stone "\nThe closest match is '%s'. Help on it follows.\n\n", 160a449698cSZachary Turner sub_cmd_obj->GetCommandName().str().c_str()); 161e139cf23SCaroline Tice } 162c007e846SJim Ingham } 163c007e846SJim Ingham 164998255bfSGreg Clayton sub_cmd_obj->GenerateHelpText(result); 16579d8105fSJim Ingham std::string alias_full_name; 16679d8105fSJim Ingham // Don't use AliasExists here, that only checks exact name matches. If 16779d8105fSJim Ingham // the user typed a shorter unique alias name, we should still tell them 16879d8105fSJim Ingham // it was an alias. 16979d8105fSJim Ingham if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) { 170e7941795SCaroline Tice StreamString sstr; 17179d8105fSJim Ingham m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr); 172b9c1b51eSKate Stone result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n", 17314f6b2c0SZachary Turner command[0].c_str(), sstr.GetData()); 174e7941795SCaroline Tice } 175b9c1b51eSKate Stone } else if (matches.GetSize() > 0) { 176279a6c26SJim Ingham Stream &output_strm = result.GetOutputStream(); 177b9c1b51eSKate Stone output_strm.Printf("Help requested with ambiguous command name, possible " 178b9c1b51eSKate Stone "completions:\n"); 179c7bece56SGreg Clayton const size_t match_count = matches.GetSize(); 180b9c1b51eSKate Stone for (size_t i = 0; i < match_count; i++) { 181279a6c26SJim Ingham output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 182279a6c26SJim Ingham } 183b9c1b51eSKate Stone } else { 184b9c1b51eSKate Stone // Maybe the user is asking for help about a command argument rather than 185b9c1b51eSKate Stone // a command. 186b9c1b51eSKate Stone const CommandArgumentType arg_type = 18714f6b2c0SZachary Turner CommandObject::LookupArgumentName(command_name); 188b9c1b51eSKate Stone if (arg_type != eArgTypeLastArg) { 189e139cf23SCaroline Tice Stream &output_strm = result.GetOutputStream(); 190e139cf23SCaroline Tice CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter); 191e139cf23SCaroline Tice result.SetStatus(eReturnStatusSuccessFinishNoResult); 192b9c1b51eSKate Stone } else { 19346d4aa21SEnrico Granata StreamString error_msg_stream; 19414f6b2c0SZachary Turner GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name, 19514f6b2c0SZachary Turner m_interpreter.GetCommandPrefix(), 19614f6b2c0SZachary Turner ""); 197c156427dSZachary Turner result.AppendError(error_msg_stream.GetString()); 19830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 19930fdc8d8SChris Lattner } 20030fdc8d8SChris Lattner } 201e139cf23SCaroline Tice } 20230fdc8d8SChris Lattner 20330fdc8d8SChris Lattner return result.Succeeded(); 20430fdc8d8SChris Lattner } 20530fdc8d8SChris Lattner 2062443bbd4SRaphael Isemann int CommandObjectHelp::HandleCompletion(CompletionRequest &request) { 20730fdc8d8SChris Lattner // Return the completions of the commands in the help system: 2082443bbd4SRaphael Isemann if (request.GetCursorIndex() == 0) { 2092443bbd4SRaphael Isemann return m_interpreter.HandleCompletionMatches(request); 210b9c1b51eSKate Stone } else { 2112443bbd4SRaphael Isemann CommandObject *cmd_obj = 2122443bbd4SRaphael Isemann m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref); 2131d18ebf6SJim Ingham 214b9c1b51eSKate Stone // The command that they are getting help on might be ambiguous, in which 21505097246SAdrian Prantl // case we should complete that, otherwise complete with the command the 21605097246SAdrian Prantl // user is getting help on... 2171d18ebf6SJim Ingham 218b9c1b51eSKate Stone if (cmd_obj) { 2192443bbd4SRaphael Isemann request.GetParsedLine().Shift(); 2202443bbd4SRaphael Isemann request.SetCursorIndex(request.GetCursorIndex() - 1); 2212443bbd4SRaphael Isemann return cmd_obj->HandleCompletion(request); 222b9c1b51eSKate Stone } else { 2232443bbd4SRaphael Isemann return m_interpreter.HandleCompletionMatches(request); 2241d18ebf6SJim Ingham } 2251d18ebf6SJim Ingham } 22630fdc8d8SChris Lattner } 227