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 686f4fb4e7SRaphael Isemann #define LLDB_OPTIONS_help 69c5a2d747SRaphael Isemann #include "CommandOptions.inc" 7008633eeaSEnrico Granata 711f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 721f0f5b5bSZachary Turner CommandObjectHelp::CommandOptions::GetDefinitions() { 7370602439SZachary Turner return llvm::makeArrayRef(g_help_options); 741f0f5b5bSZachary Turner } 751f0f5b5bSZachary Turner 76b9c1b51eSKate Stone bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) { 7730fdc8d8SChris Lattner CommandObject::CommandMap::iterator pos; 7830fdc8d8SChris Lattner CommandObject *cmd_obj; 79c7bece56SGreg Clayton const size_t argc = command.GetArgumentCount(); 8030fdc8d8SChris Lattner 8105097246SAdrian Prantl // 'help' doesn't take any arguments, other than command names. If argc is 8205097246SAdrian Prantl // 0, we show the user all commands (aliases and user commands if asked for). 8305097246SAdrian Prantl // Otherwise every argument must be the name of a command or a sub-command. 84b9c1b51eSKate Stone if (argc == 0) { 8508633eeaSEnrico Granata uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin; 8608633eeaSEnrico Granata if (m_options.m_show_aliases) 8708633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesAliases; 8808633eeaSEnrico Granata if (m_options.m_show_user_defined) 8908633eeaSEnrico Granata cmd_types |= CommandInterpreter::eCommandTypesUserDef; 90a487aa4cSKate Stone if (m_options.m_show_hidden) 91a487aa4cSKate Stone cmd_types |= CommandInterpreter::eCommandTypesHidden; 9208633eeaSEnrico Granata 9330fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishNoResult); 9408633eeaSEnrico Granata m_interpreter.GetHelp(result, cmd_types); // General help 95b9c1b51eSKate Stone } else { 96b9c1b51eSKate Stone // Get command object for the first command argument. Only search built-in 97b9c1b51eSKate Stone // command dictionary. 98279a6c26SJim Ingham StringList matches; 9914f6b2c0SZachary Turner auto command_name = command[0].ref; 10014f6b2c0SZachary Turner cmd_obj = m_interpreter.GetCommandObject(command_name, &matches); 10130fdc8d8SChris Lattner 102b9c1b51eSKate Stone if (cmd_obj != nullptr) { 103271ad29aSJim Ingham StringList matches; 10430fdc8d8SChris Lattner bool all_okay = true; 10530fdc8d8SChris Lattner CommandObject *sub_cmd_obj = cmd_obj; 106b9c1b51eSKate Stone // Loop down through sub_command dictionaries until we find the command 10797d2c401SZachary Turner // object that corresponds to the help command entered. 10846d4aa21SEnrico Granata std::string sub_command; 10997d2c401SZachary Turner for (auto &entry : command.entries().drop_front()) { 11097d2c401SZachary Turner sub_command = entry.ref; 111271ad29aSJim Ingham matches.Clear(); 112bef55ac8SEnrico Granata if (sub_cmd_obj->IsAlias()) 113b9c1b51eSKate Stone sub_cmd_obj = 114b9c1b51eSKate Stone ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get(); 115b9c1b51eSKate Stone if (!sub_cmd_obj->IsMultiwordObject()) { 11630fdc8d8SChris Lattner all_okay = false; 11797d2c401SZachary Turner break; 118b9c1b51eSKate Stone } else { 119271ad29aSJim Ingham CommandObject *found_cmd; 120b9c1b51eSKate Stone found_cmd = 121b9c1b51eSKate Stone sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches); 12297d2c401SZachary Turner if (found_cmd == nullptr || matches.GetSize() > 1) { 12330fdc8d8SChris Lattner all_okay = false; 12497d2c401SZachary Turner break; 12597d2c401SZachary Turner } else 126271ad29aSJim Ingham sub_cmd_obj = found_cmd; 12730fdc8d8SChris Lattner } 12830fdc8d8SChris Lattner } 12930fdc8d8SChris Lattner 130b9c1b51eSKate Stone if (!all_okay || (sub_cmd_obj == nullptr)) { 13130fdc8d8SChris Lattner std::string cmd_string; 13230fdc8d8SChris Lattner command.GetCommandString(cmd_string); 133b9c1b51eSKate Stone if (matches.GetSize() >= 2) { 134271ad29aSJim Ingham StreamString s; 135271ad29aSJim Ingham s.Printf("ambiguous command %s", cmd_string.c_str()); 136271ad29aSJim Ingham size_t num_matches = matches.GetSize(); 137b9c1b51eSKate Stone for (size_t match_idx = 0; match_idx < num_matches; match_idx++) { 138271ad29aSJim Ingham s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx)); 139271ad29aSJim Ingham } 140271ad29aSJim Ingham s.Printf("\n"); 141c156427dSZachary Turner result.AppendError(s.GetString()); 14230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 1439b62d1d5SEnrico Granata return false; 144b9c1b51eSKate Stone } else if (!sub_cmd_obj) { 14546d4aa21SEnrico Granata StreamString error_msg_stream; 146b9c1b51eSKate Stone GenerateAdditionalHelpAvenuesMessage( 147b9c1b51eSKate Stone &error_msg_stream, cmd_string.c_str(), 148b9c1b51eSKate Stone m_interpreter.GetCommandPrefix(), sub_command.c_str()); 149c156427dSZachary Turner result.AppendError(error_msg_stream.GetString()); 1509b62d1d5SEnrico Granata result.SetStatus(eReturnStatusFailed); 1519b62d1d5SEnrico Granata return false; 152b9c1b51eSKate Stone } else { 153b9c1b51eSKate Stone GenerateAdditionalHelpAvenuesMessage( 154b9c1b51eSKate Stone &result.GetOutputStream(), cmd_string.c_str(), 155b9c1b51eSKate Stone m_interpreter.GetCommandPrefix(), sub_command.c_str()); 156b9c1b51eSKate Stone result.GetOutputStream().Printf( 157b9c1b51eSKate Stone "\nThe closest match is '%s'. Help on it follows.\n\n", 158a449698cSZachary Turner sub_cmd_obj->GetCommandName().str().c_str()); 159e139cf23SCaroline Tice } 160c007e846SJim Ingham } 161c007e846SJim Ingham 162998255bfSGreg Clayton sub_cmd_obj->GenerateHelpText(result); 16379d8105fSJim Ingham std::string alias_full_name; 16479d8105fSJim Ingham // Don't use AliasExists here, that only checks exact name matches. If 16579d8105fSJim Ingham // the user typed a shorter unique alias name, we should still tell them 16679d8105fSJim Ingham // it was an alias. 16779d8105fSJim Ingham if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) { 168e7941795SCaroline Tice StreamString sstr; 16979d8105fSJim Ingham m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr); 170b9c1b51eSKate Stone result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n", 17114f6b2c0SZachary Turner command[0].c_str(), sstr.GetData()); 172e7941795SCaroline Tice } 173b9c1b51eSKate Stone } else if (matches.GetSize() > 0) { 174279a6c26SJim Ingham Stream &output_strm = result.GetOutputStream(); 175b9c1b51eSKate Stone output_strm.Printf("Help requested with ambiguous command name, possible " 176b9c1b51eSKate Stone "completions:\n"); 177c7bece56SGreg Clayton const size_t match_count = matches.GetSize(); 178b9c1b51eSKate Stone for (size_t i = 0; i < match_count; i++) { 179279a6c26SJim Ingham output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 180279a6c26SJim Ingham } 181b9c1b51eSKate Stone } else { 182b9c1b51eSKate Stone // Maybe the user is asking for help about a command argument rather than 183b9c1b51eSKate Stone // a command. 184b9c1b51eSKate Stone const CommandArgumentType arg_type = 18514f6b2c0SZachary Turner CommandObject::LookupArgumentName(command_name); 186b9c1b51eSKate Stone if (arg_type != eArgTypeLastArg) { 187e139cf23SCaroline Tice Stream &output_strm = result.GetOutputStream(); 188e139cf23SCaroline Tice CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter); 189e139cf23SCaroline Tice result.SetStatus(eReturnStatusSuccessFinishNoResult); 190b9c1b51eSKate Stone } else { 19146d4aa21SEnrico Granata StreamString error_msg_stream; 19214f6b2c0SZachary Turner GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name, 19314f6b2c0SZachary Turner m_interpreter.GetCommandPrefix(), 19414f6b2c0SZachary Turner ""); 195c156427dSZachary Turner result.AppendError(error_msg_stream.GetString()); 19630fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 19730fdc8d8SChris Lattner } 19830fdc8d8SChris Lattner } 199e139cf23SCaroline Tice } 20030fdc8d8SChris Lattner 20130fdc8d8SChris Lattner return result.Succeeded(); 20230fdc8d8SChris Lattner } 20330fdc8d8SChris Lattner 204*ae34ed2cSRaphael Isemann void CommandObjectHelp::HandleCompletion(CompletionRequest &request) { 20530fdc8d8SChris Lattner // Return the completions of the commands in the help system: 2062443bbd4SRaphael Isemann if (request.GetCursorIndex() == 0) { 207*ae34ed2cSRaphael Isemann m_interpreter.HandleCompletionMatches(request); 208b9c1b51eSKate Stone } else { 2092443bbd4SRaphael Isemann CommandObject *cmd_obj = 2102443bbd4SRaphael Isemann m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref); 2111d18ebf6SJim Ingham 212b9c1b51eSKate Stone // The command that they are getting help on might be ambiguous, in which 21305097246SAdrian Prantl // case we should complete that, otherwise complete with the command the 21405097246SAdrian Prantl // user is getting help on... 2151d18ebf6SJim Ingham 216b9c1b51eSKate Stone if (cmd_obj) { 2172443bbd4SRaphael Isemann request.GetParsedLine().Shift(); 2182443bbd4SRaphael Isemann request.SetCursorIndex(request.GetCursorIndex() - 1); 219*ae34ed2cSRaphael Isemann cmd_obj->HandleCompletion(request); 220b9c1b51eSKate Stone } else { 221*ae34ed2cSRaphael Isemann m_interpreter.HandleCompletionMatches(request); 2221d18ebf6SJim Ingham } 2231d18ebf6SJim Ingham } 22430fdc8d8SChris Lattner } 225