Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1 | //===-- CommandObjectApropos.cpp ---------------------------------*- C++ |
| 2 | //-*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 10 | #include "CommandObjectApropos.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 12 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "lldb/Interpreter/Options.h" |
| 14 | #include "lldb/Interpreter/Property.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | //------------------------------------------------------------------------- |
| 21 | // CommandObjectApropos |
| 22 | //------------------------------------------------------------------------- |
| 23 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 24 | CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | : CommandObjectParsed( |
| 26 | interpreter, "apropos", |
| 27 | "List debugger commands related to a word or subject.", nullptr) { |
| 28 | CommandArgumentEntry arg; |
| 29 | CommandArgumentData search_word_arg; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | // Define the first (and only) variant of this arg. |
| 32 | search_word_arg.arg_type = eArgTypeSearchWord; |
| 33 | search_word_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | // There is only one variant this argument could be; put it into the argument |
| 36 | // entry. |
| 37 | arg.push_back(search_word_arg); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | // Push the data for the first argument into the m_arguments vector. |
| 40 | m_arguments.push_back(arg); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 43 | CommandObjectApropos::~CommandObjectApropos() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) { |
| 46 | const size_t argc = args.GetArgumentCount(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | if (argc == 1) { |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 49 | auto search_word = args[0].ref; |
| 50 | if (!search_word.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 51 | // The bulk of the work must be done inside the Command Interpreter, |
| 52 | // since the command dictionary is private. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | StringList commands_found; |
| 54 | StringList commands_help; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | m_interpreter.FindCommandsForApropos(search_word, commands_found, |
| 57 | commands_help, true, true, true); |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | if (commands_found.GetSize() == 0) { |
| 60 | result.AppendMessageWithFormat("No commands found pertaining to '%s'. " |
| 61 | "Try 'help' to see a complete list of " |
| 62 | "debugger commands.\n", |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 63 | args[0].c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | } else { |
| 65 | if (commands_found.GetSize() > 0) { |
| 66 | result.AppendMessageWithFormat( |
Zachary Turner | 867e7d1 | 2016-12-09 01:20:58 +0000 | [diff] [blame] | 67 | "The following commands may relate to '%s':\n", args[0].c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | size_t max_len = 0; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | for (size_t i = 0; i < commands_found.GetSize(); ++i) { |
| 71 | size_t len = strlen(commands_found.GetStringAtIndex(i)); |
| 72 | if (len > max_len) |
| 73 | max_len = len; |
| 74 | } |
| 75 | |
| 76 | for (size_t i = 0; i < commands_found.GetSize(); ++i) |
| 77 | m_interpreter.OutputFormattedHelpText( |
| 78 | result.GetOutputStream(), commands_found.GetStringAtIndex(i), |
| 79 | "--", commands_help.GetStringAtIndex(i), max_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | std::vector<const Property *> properties; |
| 84 | const size_t num_properties = |
| 85 | m_interpreter.GetDebugger().Apropos(search_word, properties); |
| 86 | if (num_properties) { |
| 87 | const bool dump_qualified_name = true; |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 88 | result.AppendMessageWithFormatv( |
| 89 | "\nThe following settings variables may relate to '{0}': \n\n", |
| 90 | args[0].ref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | for (size_t i = 0; i < num_properties; ++i) |
| 92 | properties[i]->DumpDescription( |
| 93 | m_interpreter, result.GetOutputStream(), 0, dump_qualified_name); |
| 94 | } |
| 95 | |
| 96 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 97 | } else { |
| 98 | result.AppendError("'' is not a valid search word.\n"); |
| 99 | result.SetStatus(eReturnStatusFailed); |
| 100 | } |
| 101 | } else { |
| 102 | result.AppendError("'apropos' must be called with exactly one argument.\n"); |
| 103 | result.SetStatus(eReturnStatusFailed); |
| 104 | } |
| 105 | |
| 106 | return result.Succeeded(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | } |