180814287SRaphael Isemann //===-- CommandObjectCommands.cpp -----------------------------------------===//
2ebc09c36SJim Ingham //
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
6ebc09c36SJim Ingham //
7ebc09c36SJim Ingham //===----------------------------------------------------------------------===//
8ebc09c36SJim Ingham
96e3d8e7fSEugene Zelenko #include "CommandObjectCommands.h"
1046d4aa21SEnrico Granata #include "CommandObjectHelp.h"
119390b346SJonas Devlieghere #include "CommandObjectRegexCommand.h"
12ebc09c36SJim Ingham #include "lldb/Core/Debugger.h"
1344d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
147594f14fSEnrico Granata #include "lldb/Interpreter/CommandHistory.h"
15ebc09c36SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
16*7ced9fffSJonas Devlieghere #include "lldb/Interpreter/CommandOptionArgumentTable.h"
17ebc09c36SJim Ingham #include "lldb/Interpreter/CommandReturnObject.h"
1847cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
19012d4fcaSEnrico Granata #include "lldb/Interpreter/OptionValueBoolean.h"
2045d0e238SEnrico Granata #include "lldb/Interpreter/OptionValueString.h"
217594f14fSEnrico Granata #include "lldb/Interpreter/OptionValueUInt64.h"
22ebc09c36SJim Ingham #include "lldb/Interpreter/Options.h"
2399f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
24145d95c9SPavel Labath #include "lldb/Utility/Args.h"
25573ab909SZachary Turner #include "lldb/Utility/StringList.h"
269390b346SJonas Devlieghere #include "llvm/ADT/StringRef.h"
27ebc09c36SJim Ingham
28ebc09c36SJim Ingham using namespace lldb;
29ebc09c36SJim Ingham using namespace lldb_private;
30ebc09c36SJim Ingham
31ebc09c36SJim Ingham // CommandObjectCommandsSource
32ebc09c36SJim Ingham
3364becc11SRaphael Isemann #define LLDB_OPTIONS_source
3464becc11SRaphael Isemann #include "CommandOptions.inc"
351f0f5b5bSZachary Turner
36b9c1b51eSKate Stone class CommandObjectCommandsSource : public CommandObjectParsed {
375a988416SJim Ingham public:
CommandObjectCommandsSource(CommandInterpreter & interpreter)387428a18cSKate Stone CommandObjectCommandsSource(CommandInterpreter &interpreter)
39b9c1b51eSKate Stone : CommandObjectParsed(
40b9c1b51eSKate Stone interpreter, "command source",
41b9c1b51eSKate Stone "Read and execute LLDB commands from the file <filename>.",
42abb0ed44SKazu Hirata nullptr) {
435a988416SJim Ingham CommandArgumentEntry arg;
445a988416SJim Ingham CommandArgumentData file_arg;
455a988416SJim Ingham
465a988416SJim Ingham // Define the first (and only) variant of this arg.
475a988416SJim Ingham file_arg.arg_type = eArgTypeFilename;
485a988416SJim Ingham file_arg.arg_repetition = eArgRepeatPlain;
495a988416SJim Ingham
50b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
51b9c1b51eSKate Stone // argument entry.
525a988416SJim Ingham arg.push_back(file_arg);
535a988416SJim Ingham
545a988416SJim Ingham // Push the data for the first argument into the m_arguments vector.
555a988416SJim Ingham m_arguments.push_back(arg);
565a988416SJim Ingham }
575a988416SJim Ingham
586e3d8e7fSEugene Zelenko ~CommandObjectCommandsSource() override = default;
595a988416SJim Ingham
GetRepeatCommand(Args & current_command_args,uint32_t index)60635f03feSJim Ingham llvm::Optional<std::string> GetRepeatCommand(Args ¤t_command_args,
61b9c1b51eSKate Stone uint32_t index) override {
62635f03feSJim Ingham return std::string("");
635a988416SJim Ingham }
645a988416SJim Ingham
65ae34ed2cSRaphael Isemann void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)66ae34ed2cSRaphael Isemann HandleArgumentCompletion(CompletionRequest &request,
672443bbd4SRaphael Isemann OptionElementVector &opt_element_vector) override {
68b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks(
69b9c1b51eSKate Stone GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
70a2e76c0bSRaphael Isemann request, nullptr);
715a988416SJim Ingham }
725a988416SJim Ingham
GetOptions()73b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; }
745a988416SJim Ingham
755a988416SJim Ingham protected:
76b9c1b51eSKate Stone class CommandOptions : public Options {
77e16c50a1SJim Ingham public:
CommandOptions()78b9c1b51eSKate Stone CommandOptions()
79abb0ed44SKazu Hirata : m_stop_on_error(true), m_silent_run(false), m_stop_on_continue(true),
80abb0ed44SKazu Hirata m_cmd_relative_to_command_file(false) {}
81e16c50a1SJim Ingham
826e3d8e7fSEugene Zelenko ~CommandOptions() override = default;
83e16c50a1SJim Ingham
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)8497206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
85b9c1b51eSKate Stone ExecutionContext *execution_context) override {
8697206d57SZachary Turner Status error;
873bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val;
88e16c50a1SJim Ingham
89b9c1b51eSKate Stone switch (short_option) {
90e16c50a1SJim Ingham case 'e':
91fe11483bSZachary Turner error = m_stop_on_error.SetValueFromString(option_arg);
92e16c50a1SJim Ingham break;
93340b0309SGreg Clayton
94e16c50a1SJim Ingham case 'c':
95fe11483bSZachary Turner error = m_stop_on_continue.SetValueFromString(option_arg);
96e16c50a1SJim Ingham break;
97340b0309SGreg Clayton
983bf3b966SJim Ingham case 'C':
993bf3b966SJim Ingham m_cmd_relative_to_command_file = true;
1003bf3b966SJim Ingham break;
1013bf3b966SJim Ingham
10260986174SMichael Sartain case 's':
103fe11483bSZachary Turner error = m_silent_run.SetValueFromString(option_arg);
10460986174SMichael Sartain break;
105340b0309SGreg Clayton
106e16c50a1SJim Ingham default:
10736162014SRaphael Isemann llvm_unreachable("Unimplemented option");
108e16c50a1SJim Ingham }
109e16c50a1SJim Ingham
110e16c50a1SJim Ingham return error;
111e16c50a1SJim Ingham }
112e16c50a1SJim Ingham
OptionParsingStarting(ExecutionContext * execution_context)113b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override {
114012d4fcaSEnrico Granata m_stop_on_error.Clear();
115340b0309SGreg Clayton m_silent_run.Clear();
116340b0309SGreg Clayton m_stop_on_continue.Clear();
1173bf3b966SJim Ingham m_cmd_relative_to_command_file.Clear();
118e16c50a1SJim Ingham }
119e16c50a1SJim Ingham
GetDefinitions()1201f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
12170602439SZachary Turner return llvm::makeArrayRef(g_source_options);
1221f0f5b5bSZachary Turner }
123e16c50a1SJim Ingham
124e16c50a1SJim Ingham // Instance variables to hold the values for command options.
125e16c50a1SJim Ingham
126012d4fcaSEnrico Granata OptionValueBoolean m_stop_on_error;
127340b0309SGreg Clayton OptionValueBoolean m_silent_run;
128340b0309SGreg Clayton OptionValueBoolean m_stop_on_continue;
1293bf3b966SJim Ingham OptionValueBoolean m_cmd_relative_to_command_file;
130e16c50a1SJim Ingham };
131e16c50a1SJim Ingham
DoExecute(Args & command,CommandReturnObject & result)132b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
1334574a890SZachary Turner if (command.GetArgumentCount() != 1) {
1344574a890SZachary Turner result.AppendErrorWithFormat(
1354574a890SZachary Turner "'%s' takes exactly one executable filename argument.\n",
1364574a890SZachary Turner GetCommandName().str().c_str());
1374574a890SZachary Turner return false;
1384574a890SZachary Turner }
139ebc09c36SJim Ingham
1403bf3b966SJim Ingham FileSpec source_dir = {};
1413bf3b966SJim Ingham if (m_options.m_cmd_relative_to_command_file) {
1423bf3b966SJim Ingham source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir();
1433bf3b966SJim Ingham if (!source_dir) {
1443bf3b966SJim Ingham result.AppendError("command source -C can only be specified "
1453bf3b966SJim Ingham "from a command file");
1463bf3b966SJim Ingham result.SetStatus(eReturnStatusFailed);
1473bf3b966SJim Ingham return false;
1483bf3b966SJim Ingham }
1493bf3b966SJim Ingham }
1503bf3b966SJim Ingham
1510d9a201eSRaphael Isemann FileSpec cmd_file(command[0].ref());
1523bf3b966SJim Ingham if (source_dir) {
1533bf3b966SJim Ingham // Prepend the source_dir to the cmd_file path:
1543bf3b966SJim Ingham if (!cmd_file.IsRelative()) {
1553bf3b966SJim Ingham result.AppendError("command source -C can only be used "
1563bf3b966SJim Ingham "with a relative path.");
1573bf3b966SJim Ingham result.SetStatus(eReturnStatusFailed);
1583bf3b966SJim Ingham return false;
1593bf3b966SJim Ingham }
1603bf3b966SJim Ingham cmd_file.MakeAbsolute(source_dir);
1613bf3b966SJim Ingham }
1623bf3b966SJim Ingham
1638f3be7a3SJonas Devlieghere FileSystem::Instance().Resolve(cmd_file);
164ebc09c36SJim Ingham
16536de94cfSTatyana Krasnukha CommandInterpreterRunOptions options;
166340b0309SGreg Clayton // If any options were set, then use them
167340b0309SGreg Clayton if (m_options.m_stop_on_error.OptionWasSet() ||
168340b0309SGreg Clayton m_options.m_silent_run.OptionWasSet() ||
169b9c1b51eSKate Stone m_options.m_stop_on_continue.OptionWasSet()) {
1701c19b74cSJonas Devlieghere if (m_options.m_stop_on_continue.OptionWasSet())
1711c19b74cSJonas Devlieghere options.SetStopOnContinue(
1721c19b74cSJonas Devlieghere m_options.m_stop_on_continue.GetCurrentValue());
1731c19b74cSJonas Devlieghere
1741c19b74cSJonas Devlieghere if (m_options.m_stop_on_error.OptionWasSet())
17526c7bf93SJim Ingham options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
176c678ed77SStefan Granitz
177c678ed77SStefan Granitz // Individual silent setting is override for global command echo settings.
178c678ed77SStefan Granitz if (m_options.m_silent_run.GetCurrentValue()) {
179c678ed77SStefan Granitz options.SetSilent(true);
180c678ed77SStefan Granitz } else {
181c678ed77SStefan Granitz options.SetPrintResults(true);
182c0b48ab6SJonas Devlieghere options.SetPrintErrors(true);
183c678ed77SStefan Granitz options.SetEchoCommands(m_interpreter.GetEchoCommands());
184c678ed77SStefan Granitz options.SetEchoCommentCommands(m_interpreter.GetEchoCommentCommands());
185c678ed77SStefan Granitz }
186122a4ebdSPavel Labath }
18736de94cfSTatyana Krasnukha
18836de94cfSTatyana Krasnukha m_interpreter.HandleCommandsFromFile(cmd_file, options, result);
189ebc09c36SJim Ingham return result.Succeeded();
190ebc09c36SJim Ingham }
1916e3d8e7fSEugene Zelenko
1925a988416SJim Ingham CommandOptions m_options;
193ebc09c36SJim Ingham };
194ebc09c36SJim Ingham
195ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias
196ebc09c36SJim Ingham // CommandObjectCommandsAlias
197ebc09c36SJim Ingham
19864becc11SRaphael Isemann #define LLDB_OPTIONS_alias
19964becc11SRaphael Isemann #include "CommandOptions.inc"
2001f0f5b5bSZachary Turner
201b9c1b51eSKate Stone static const char *g_python_command_instructions =
202b9c1b51eSKate Stone "Enter your Python command(s). Type 'DONE' to end.\n"
203be93a35aSEnrico Granata "You must define a Python function with this signature:\n"
20444d93782SGreg Clayton "def my_command_impl(debugger, args, result, internal_dict):\n";
205be93a35aSEnrico Granata
206b9c1b51eSKate Stone class CommandObjectCommandsAlias : public CommandObjectRaw {
20745d0e238SEnrico Granata protected:
208b9c1b51eSKate Stone class CommandOptions : public OptionGroup {
209ebc09c36SJim Ingham public:
21024f9a2f5SShafik Yaghmour CommandOptions() = default;
21145d0e238SEnrico Granata
21245d0e238SEnrico Granata ~CommandOptions() override = default;
21345d0e238SEnrico Granata
GetDefinitions()2141f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
21570602439SZachary Turner return llvm::makeArrayRef(g_alias_options);
2161f0f5b5bSZachary Turner }
21745d0e238SEnrico Granata
SetOptionValue(uint32_t option_idx,llvm::StringRef option_value,ExecutionContext * execution_context)21897206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
219b9c1b51eSKate Stone ExecutionContext *execution_context) override {
22097206d57SZachary Turner Status error;
22145d0e238SEnrico Granata
2221f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option;
2238cef4b0bSZachary Turner std::string option_str(option_value);
22445d0e238SEnrico Granata
225b9c1b51eSKate Stone switch (short_option) {
22645d0e238SEnrico Granata case 'h':
2278cef4b0bSZachary Turner m_help.SetCurrentValue(option_str);
22845d0e238SEnrico Granata m_help.SetOptionWasSet();
22945d0e238SEnrico Granata break;
23045d0e238SEnrico Granata
23145d0e238SEnrico Granata case 'H':
2328cef4b0bSZachary Turner m_long_help.SetCurrentValue(option_str);
23345d0e238SEnrico Granata m_long_help.SetOptionWasSet();
23445d0e238SEnrico Granata break;
23545d0e238SEnrico Granata
23645d0e238SEnrico Granata default:
23736162014SRaphael Isemann llvm_unreachable("Unimplemented option");
23845d0e238SEnrico Granata }
23945d0e238SEnrico Granata
24045d0e238SEnrico Granata return error;
24145d0e238SEnrico Granata }
24245d0e238SEnrico Granata
OptionParsingStarting(ExecutionContext * execution_context)243b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override {
24445d0e238SEnrico Granata m_help.Clear();
24545d0e238SEnrico Granata m_long_help.Clear();
24645d0e238SEnrico Granata }
24745d0e238SEnrico Granata
24845d0e238SEnrico Granata OptionValueString m_help;
24945d0e238SEnrico Granata OptionValueString m_long_help;
25045d0e238SEnrico Granata };
25145d0e238SEnrico Granata
25245d0e238SEnrico Granata OptionGroupOptions m_option_group;
25345d0e238SEnrico Granata CommandOptions m_command_options;
25445d0e238SEnrico Granata
25545d0e238SEnrico Granata public:
GetOptions()256b9c1b51eSKate Stone Options *GetOptions() override { return &m_option_group; }
25745d0e238SEnrico Granata
CommandObjectCommandsAlias(CommandInterpreter & interpreter)2587428a18cSKate Stone CommandObjectCommandsAlias(CommandInterpreter &interpreter)
259b9c1b51eSKate Stone : CommandObjectRaw(
260b9c1b51eSKate Stone interpreter, "command alias",
261abb0ed44SKazu Hirata "Define a custom command in terms of an existing command.") {
26245d0e238SEnrico Granata m_option_group.Append(&m_command_options);
26345d0e238SEnrico Granata m_option_group.Finalize();
26445d0e238SEnrico Granata
265ebc09c36SJim Ingham SetHelpLong(
266ea671fbdSKate Stone "'alias' allows the user to create a short-cut or abbreviation for long \
267ea671fbdSKate Stone commands, multi-word commands, and commands that take particular options. \
268b9c1b51eSKate Stone Below are some simple examples of how one might use the 'alias' command:"
269b9c1b51eSKate Stone R"(
270ea671fbdSKate Stone
271ea671fbdSKate Stone (lldb) command alias sc script
272ea671fbdSKate Stone
273ea671fbdSKate Stone Creates the abbreviation 'sc' for the 'script' command.
274ea671fbdSKate Stone
275ea671fbdSKate Stone (lldb) command alias bp breakpoint
276ea671fbdSKate Stone
277b9c1b51eSKate Stone )"
278b9c1b51eSKate Stone " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
279ea671fbdSKate Stone breakpoint commands are two-word commands, the user would still need to \
280b9c1b51eSKate Stone enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
281b9c1b51eSKate Stone R"(
282ea671fbdSKate Stone
283ea671fbdSKate Stone (lldb) command alias bpl breakpoint list
284ea671fbdSKate Stone
285ea671fbdSKate Stone Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
286ea671fbdSKate Stone
287b9c1b51eSKate Stone )"
288b9c1b51eSKate Stone "An alias can include some options for the command, with the values either \
289ea671fbdSKate Stone filled in at the time the alias is created, or specified as positional \
290ea671fbdSKate Stone arguments, to be filled in when the alias is invoked. The following example \
291b9c1b51eSKate Stone shows how to create aliases with options:"
292b9c1b51eSKate Stone R"(
293ea671fbdSKate Stone
294ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
295ea671fbdSKate Stone
296b9c1b51eSKate Stone )"
297b9c1b51eSKate Stone " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
298ea671fbdSKate Stone options already part of the alias. So if the user wants to set a breakpoint \
299ea671fbdSKate Stone by file and line without explicitly having to use the -f and -l options, the \
300ea671fbdSKate Stone user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
301ea671fbdSKate Stone for the actual arguments that will be passed when the alias command is used. \
302ea671fbdSKate Stone The number in the placeholder refers to the position/order the actual value \
303ea671fbdSKate Stone occupies when the alias is used. All the occurrences of '%1' in the alias \
304ea671fbdSKate Stone will be replaced with the first argument, all the occurrences of '%2' in the \
305ea671fbdSKate Stone alias will be replaced with the second argument, and so on. This also allows \
306ea671fbdSKate Stone actual arguments to be used multiple times within an alias (see 'process \
307b9c1b51eSKate Stone launch' example below)."
308b9c1b51eSKate Stone R"(
309ea671fbdSKate Stone
310b9c1b51eSKate Stone )"
311b9c1b51eSKate Stone "Note: the positional arguments must substitute as whole words in the resultant \
312ea671fbdSKate Stone command, so you can't at present do something like this to append the file extension \
313b9c1b51eSKate Stone \".cpp\":"
314b9c1b51eSKate Stone R"(
315ea671fbdSKate Stone
316ea671fbdSKate Stone (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
317ea671fbdSKate Stone
318b9c1b51eSKate Stone )"
319b9c1b51eSKate Stone "For more complex aliasing, use the \"command regex\" command instead. In the \
320ea671fbdSKate Stone 'bfl' case above, the actual file value will be filled in with the first argument \
321ea671fbdSKate Stone following 'bfl' and the actual line number value will be filled in with the second \
322b9c1b51eSKate Stone argument. The user would use this alias as follows:"
323b9c1b51eSKate Stone R"(
324ea671fbdSKate Stone
325ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
326ea671fbdSKate Stone (lldb) bfl my-file.c 137
327ea671fbdSKate Stone
328ea671fbdSKate Stone This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
329ea671fbdSKate Stone
330ea671fbdSKate Stone Another example:
331ea671fbdSKate Stone
332ea671fbdSKate Stone (lldb) command alias pltty process launch -s -o %1 -e %1
333ea671fbdSKate Stone (lldb) pltty /dev/tty0
334ea671fbdSKate Stone
335ea671fbdSKate Stone Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
336ea671fbdSKate Stone
337b9c1b51eSKate Stone )"
338b9c1b51eSKate Stone "If the user always wanted to pass the same value to a particular option, the \
339ea671fbdSKate Stone alias could be defined with that value directly in the alias as a constant, \
340b9c1b51eSKate Stone rather than using a positional placeholder:"
341b9c1b51eSKate Stone R"(
342ea671fbdSKate Stone
343ea671fbdSKate Stone (lldb) command alias bl3 breakpoint set -f %1 -l 3
344ea671fbdSKate Stone
345b9c1b51eSKate Stone Always sets a breakpoint on line 3 of whatever file is indicated.)");
346ebc09c36SJim Ingham
347405fe67fSCaroline Tice CommandArgumentEntry arg1;
348405fe67fSCaroline Tice CommandArgumentEntry arg2;
349405fe67fSCaroline Tice CommandArgumentEntry arg3;
350405fe67fSCaroline Tice CommandArgumentData alias_arg;
351405fe67fSCaroline Tice CommandArgumentData cmd_arg;
352405fe67fSCaroline Tice CommandArgumentData options_arg;
353405fe67fSCaroline Tice
354405fe67fSCaroline Tice // Define the first (and only) variant of this arg.
355405fe67fSCaroline Tice alias_arg.arg_type = eArgTypeAliasName;
356405fe67fSCaroline Tice alias_arg.arg_repetition = eArgRepeatPlain;
357405fe67fSCaroline Tice
358b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
359b9c1b51eSKate Stone // argument entry.
360405fe67fSCaroline Tice arg1.push_back(alias_arg);
361405fe67fSCaroline Tice
362405fe67fSCaroline Tice // Define the first (and only) variant of this arg.
363405fe67fSCaroline Tice cmd_arg.arg_type = eArgTypeCommandName;
364405fe67fSCaroline Tice cmd_arg.arg_repetition = eArgRepeatPlain;
365405fe67fSCaroline Tice
366b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
367b9c1b51eSKate Stone // argument entry.
368405fe67fSCaroline Tice arg2.push_back(cmd_arg);
369405fe67fSCaroline Tice
370405fe67fSCaroline Tice // Define the first (and only) variant of this arg.
371405fe67fSCaroline Tice options_arg.arg_type = eArgTypeAliasOptions;
372405fe67fSCaroline Tice options_arg.arg_repetition = eArgRepeatOptional;
373405fe67fSCaroline Tice
374b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
375b9c1b51eSKate Stone // argument entry.
376405fe67fSCaroline Tice arg3.push_back(options_arg);
377405fe67fSCaroline Tice
378405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector.
379405fe67fSCaroline Tice m_arguments.push_back(arg1);
380405fe67fSCaroline Tice m_arguments.push_back(arg2);
381405fe67fSCaroline Tice m_arguments.push_back(arg3);
382ebc09c36SJim Ingham }
383ebc09c36SJim Ingham
3846e3d8e7fSEugene Zelenko ~CommandObjectCommandsAlias() override = default;
385ebc09c36SJim Ingham
3865a988416SJim Ingham protected:
DoExecute(llvm::StringRef raw_command_line,CommandReturnObject & result)3874d51a902SRaphael Isemann bool DoExecute(llvm::StringRef raw_command_line,
388b9c1b51eSKate Stone CommandReturnObject &result) override {
3894d51a902SRaphael Isemann if (raw_command_line.empty()) {
390d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments");
39145d0e238SEnrico Granata return false;
39245d0e238SEnrico Granata }
39345d0e238SEnrico Granata
394e1cfbc79STodd Fiala ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
395e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx);
39645d0e238SEnrico Granata
3973a0e1270SRaphael Isemann OptionsWithRaw args_with_suffix(raw_command_line);
39845d0e238SEnrico Granata
3993a0e1270SRaphael Isemann if (args_with_suffix.HasArgs())
4003a0e1270SRaphael Isemann if (!ParseOptionsAndNotify(args_with_suffix.GetArgs(), result,
4013a0e1270SRaphael Isemann m_option_group, exe_ctx))
40245d0e238SEnrico Granata return false;
40345d0e238SEnrico Granata
404daed98e5SShivam Mittal llvm::StringRef raw_command_string = args_with_suffix.GetRawPart();
405a01bccdbSZachary Turner Args args(raw_command_string);
406844d2303SCaroline Tice
40711eb9c64SZachary Turner if (args.GetArgumentCount() < 2) {
408d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments");
409844d2303SCaroline Tice return false;
410844d2303SCaroline Tice }
411844d2303SCaroline Tice
412844d2303SCaroline Tice // Get the alias command.
413844d2303SCaroline Tice
4140d9a201eSRaphael Isemann auto alias_command = args[0].ref();
4154574a890SZachary Turner if (alias_command.startswith("-")) {
416d72e412fSEnrico Granata result.AppendError("aliases starting with a dash are not supported");
417b9c1b51eSKate Stone if (alias_command == "--help" || alias_command == "--long-help") {
418b9c1b51eSKate Stone result.AppendWarning("if trying to pass options to 'command alias' add "
419b9c1b51eSKate Stone "a -- at the end of the options");
420d72e412fSEnrico Granata }
421d72e412fSEnrico Granata return false;
422d72e412fSEnrico Granata }
423844d2303SCaroline Tice
424b9c1b51eSKate Stone // Strip the new alias name off 'raw_command_string' (leave it on args,
42505097246SAdrian Prantl // which gets passed to 'Execute', which does the stripping itself.
426844d2303SCaroline Tice size_t pos = raw_command_string.find(alias_command);
427b9c1b51eSKate Stone if (pos == 0) {
428844d2303SCaroline Tice raw_command_string = raw_command_string.substr(alias_command.size());
429844d2303SCaroline Tice pos = raw_command_string.find_first_not_of(' ');
430844d2303SCaroline Tice if ((pos != std::string::npos) && (pos > 0))
431844d2303SCaroline Tice raw_command_string = raw_command_string.substr(pos);
432b9c1b51eSKate Stone } else {
433844d2303SCaroline Tice result.AppendError("Error parsing command string. No alias created.");
434844d2303SCaroline Tice return false;
435844d2303SCaroline Tice }
436844d2303SCaroline Tice
437844d2303SCaroline Tice // Verify that the command is alias-able.
438771ef6d4SMalcolm Parsons if (m_interpreter.CommandExists(alias_command)) {
439b9c1b51eSKate Stone result.AppendErrorWithFormat(
440b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be redefined.\n",
4414574a890SZachary Turner args[0].c_str());
442844d2303SCaroline Tice return false;
443844d2303SCaroline Tice }
444844d2303SCaroline Tice
445c5011aedSJim Ingham if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
446c5011aedSJim Ingham result.AppendErrorWithFormat(
447c5011aedSJim Ingham "'%s' is a user container command and cannot be overwritten.\n"
448c5011aedSJim Ingham "Delete it first with 'command container delete'\n",
449c5011aedSJim Ingham args[0].c_str());
450c5011aedSJim Ingham return false;
451c5011aedSJim Ingham }
452c5011aedSJim Ingham
453b9c1b51eSKate Stone // Get CommandObject that is being aliased. The command name is read from
454a01bccdbSZachary Turner // the front of raw_command_string. raw_command_string is returned with the
455a01bccdbSZachary Turner // name of the command object stripped off the front.
456a01bccdbSZachary Turner llvm::StringRef original_raw_command_string = raw_command_string;
457b9c1b51eSKate Stone CommandObject *cmd_obj =
458b9c1b51eSKate Stone m_interpreter.GetCommandObjectForCommand(raw_command_string);
459844d2303SCaroline Tice
460b9c1b51eSKate Stone if (!cmd_obj) {
461b9c1b51eSKate Stone result.AppendErrorWithFormat("invalid command given to 'command alias'. "
462b9c1b51eSKate Stone "'%s' does not begin with a valid command."
463b9c1b51eSKate Stone " No alias created.",
464a01bccdbSZachary Turner original_raw_command_string.str().c_str());
465844d2303SCaroline Tice return false;
466b9c1b51eSKate Stone } else if (!cmd_obj->WantsRawCommandString()) {
467b9c1b51eSKate Stone // Note that args was initialized with the original command, and has not
46805097246SAdrian Prantl // been updated to this point. Therefore can we pass it to the version of
46905097246SAdrian Prantl // Execute that does not need/expect raw input in the alias.
4705a988416SJim Ingham return HandleAliasingNormalCommand(args, result);
471b9c1b51eSKate Stone } else {
472b9c1b51eSKate Stone return HandleAliasingRawCommand(alias_command, raw_command_string,
473b9c1b51eSKate Stone *cmd_obj, result);
4745a988416SJim Ingham }
4755a988416SJim Ingham return result.Succeeded();
4765a988416SJim Ingham }
4775a988416SJim Ingham
HandleAliasingRawCommand(llvm::StringRef alias_command,llvm::StringRef raw_command_string,CommandObject & cmd_obj,CommandReturnObject & result)478a01bccdbSZachary Turner bool HandleAliasingRawCommand(llvm::StringRef alias_command,
479a01bccdbSZachary Turner llvm::StringRef raw_command_string,
480b9c1b51eSKate Stone CommandObject &cmd_obj,
481b9c1b51eSKate Stone CommandReturnObject &result) {
482844d2303SCaroline Tice // Verify & handle any options/arguments passed to the alias command
483844d2303SCaroline Tice
484b9c1b51eSKate Stone OptionArgVectorSP option_arg_vector_sp =
485b9c1b51eSKate Stone OptionArgVectorSP(new OptionArgVector);
486844d2303SCaroline Tice
487b9515041SDave Lee const bool include_aliases = true;
488b9515041SDave Lee if (CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact(
489b9515041SDave Lee cmd_obj.GetCommandName(), include_aliases)) {
490a01bccdbSZachary Turner if (m_interpreter.AliasExists(alias_command) ||
491a01bccdbSZachary Turner m_interpreter.UserCommandExists(alias_command)) {
492b9c1b51eSKate Stone result.AppendWarningWithFormat(
493b9c1b51eSKate Stone "Overwriting existing definition for '%s'.\n",
494a01bccdbSZachary Turner alias_command.str().c_str());
495844d2303SCaroline Tice }
496b9c1b51eSKate Stone if (CommandAlias *alias = m_interpreter.AddAlias(
497a01bccdbSZachary Turner alias_command, cmd_obj_sp, raw_command_string)) {
49845d0e238SEnrico Granata if (m_command_options.m_help.OptionWasSet())
49945d0e238SEnrico Granata alias->SetHelp(m_command_options.m_help.GetCurrentValue());
50045d0e238SEnrico Granata if (m_command_options.m_long_help.OptionWasSet())
50145d0e238SEnrico Granata alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
502844d2303SCaroline Tice result.SetStatus(eReturnStatusSuccessFinishNoResult);
503b9c1b51eSKate Stone } else {
504472362e6SCaroline Tice result.AppendError("Unable to create requested alias.\n");
505472362e6SCaroline Tice }
506212130acSEnrico Granata
507b9c1b51eSKate Stone } else {
508212130acSEnrico Granata result.AppendError("Unable to create requested alias.\n");
509212130acSEnrico Granata }
510212130acSEnrico Granata
511844d2303SCaroline Tice return result.Succeeded();
512844d2303SCaroline Tice }
513ebc09c36SJim Ingham
HandleAliasingNormalCommand(Args & args,CommandReturnObject & result)514b9c1b51eSKate Stone bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
515867b185dSCaroline Tice size_t argc = args.GetArgumentCount();
516ebc09c36SJim Ingham
517b9c1b51eSKate Stone if (argc < 2) {
518d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments");
519ebc09c36SJim Ingham return false;
520ebc09c36SJim Ingham }
521ebc09c36SJim Ingham
5224574a890SZachary Turner // Save these in std::strings since we're going to shift them off.
523adcd0268SBenjamin Kramer const std::string alias_command(std::string(args[0].ref()));
524adcd0268SBenjamin Kramer const std::string actual_command(std::string(args[1].ref()));
525ebc09c36SJim Ingham
526ebc09c36SJim Ingham args.Shift(); // Shift the alias command word off the argument vector.
527ebc09c36SJim Ingham args.Shift(); // Shift the old command word off the argument vector.
528ebc09c36SJim Ingham
529b9c1b51eSKate Stone // Verify that the command is alias'able, and get the appropriate command
530b9c1b51eSKate Stone // object.
531ebc09c36SJim Ingham
532771ef6d4SMalcolm Parsons if (m_interpreter.CommandExists(alias_command)) {
533b9c1b51eSKate Stone result.AppendErrorWithFormat(
534b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be redefined.\n",
535ebc09c36SJim Ingham alias_command.c_str());
5364574a890SZachary Turner return false;
5374574a890SZachary Turner }
5384574a890SZachary Turner
539c5011aedSJim Ingham if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
540c5011aedSJim Ingham result.AppendErrorWithFormat(
541c5011aedSJim Ingham "'%s' is user container command and cannot be overwritten.\n"
542c5011aedSJim Ingham "Delete it first with 'command container delete'",
543c5011aedSJim Ingham alias_command.c_str());
544c5011aedSJim Ingham return false;
545c5011aedSJim Ingham }
546c5011aedSJim Ingham
547b9c1b51eSKate Stone CommandObjectSP command_obj_sp(
548a449698cSZachary Turner m_interpreter.GetCommandSPExact(actual_command, true));
549ebc09c36SJim Ingham CommandObjectSP subcommand_obj_sp;
550ebc09c36SJim Ingham bool use_subcommand = false;
5514574a890SZachary Turner if (!command_obj_sp) {
5524574a890SZachary Turner result.AppendErrorWithFormat("'%s' is not an existing command.\n",
5534574a890SZachary Turner actual_command.c_str());
5544574a890SZachary Turner return false;
5554574a890SZachary Turner }
556ebc09c36SJim Ingham CommandObject *cmd_obj = command_obj_sp.get();
5576e3d8e7fSEugene Zelenko CommandObject *sub_cmd_obj = nullptr;
558b9c1b51eSKate Stone OptionArgVectorSP option_arg_vector_sp =
559b9c1b51eSKate Stone OptionArgVectorSP(new OptionArgVector);
560ebc09c36SJim Ingham
56111eb9c64SZachary Turner while (cmd_obj->IsMultiwordObject() && !args.empty()) {
5620d9a201eSRaphael Isemann auto sub_command = args[0].ref();
56311eb9c64SZachary Turner assert(!sub_command.empty());
5644574a890SZachary Turner subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
5654574a890SZachary Turner if (!subcommand_obj_sp) {
566b9c1b51eSKate Stone result.AppendErrorWithFormat(
567b9c1b51eSKate Stone "'%s' is not a valid sub-command of '%s'. "
568f415eeb4SCaroline Tice "Unable to create alias.\n",
5694574a890SZachary Turner args[0].c_str(), actual_command.c_str());
570ebc09c36SJim Ingham return false;
571ebc09c36SJim Ingham }
5724574a890SZachary Turner
5734574a890SZachary Turner sub_cmd_obj = subcommand_obj_sp.get();
5744574a890SZachary Turner use_subcommand = true;
5754574a890SZachary Turner args.Shift(); // Shift the sub_command word off the argument vector.
5764574a890SZachary Turner cmd_obj = sub_cmd_obj;
577ebc09c36SJim Ingham }
578ebc09c36SJim Ingham
579ebc09c36SJim Ingham // Verify & handle any options/arguments passed to the alias command
580ebc09c36SJim Ingham
581212130acSEnrico Granata std::string args_string;
582212130acSEnrico Granata
58311eb9c64SZachary Turner if (!args.empty()) {
584b9c1b51eSKate Stone CommandObjectSP tmp_sp =
585a9448872SJonas Devlieghere m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName());
586ebc09c36SJim Ingham if (use_subcommand)
587a9448872SJonas Devlieghere tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName());
588ca90c47eSCaroline Tice
589ca90c47eSCaroline Tice args.GetCommandString(args_string);
590867b185dSCaroline Tice }
591ebc09c36SJim Ingham
592771ef6d4SMalcolm Parsons if (m_interpreter.AliasExists(alias_command) ||
593771ef6d4SMalcolm Parsons m_interpreter.UserCommandExists(alias_command)) {
594b9c1b51eSKate Stone result.AppendWarningWithFormat(
5954574a890SZachary Turner "Overwriting existing definition for '%s'.\n", alias_command.c_str());
596ebc09c36SJim Ingham }
597ebc09c36SJim Ingham
598b9c1b51eSKate Stone if (CommandAlias *alias = m_interpreter.AddAlias(
5994574a890SZachary Turner alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
600771ef6d4SMalcolm Parsons args_string)) {
60145d0e238SEnrico Granata if (m_command_options.m_help.OptionWasSet())
60245d0e238SEnrico Granata alias->SetHelp(m_command_options.m_help.GetCurrentValue());
60345d0e238SEnrico Granata if (m_command_options.m_long_help.OptionWasSet())
60445d0e238SEnrico Granata alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
605ebc09c36SJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
606b9c1b51eSKate Stone } else {
607212130acSEnrico Granata result.AppendError("Unable to create requested alias.\n");
608212130acSEnrico Granata return false;
609212130acSEnrico Granata }
610ebc09c36SJim Ingham
611ebc09c36SJim Ingham return result.Succeeded();
612ebc09c36SJim Ingham }
613ebc09c36SJim Ingham };
614ebc09c36SJim Ingham
615ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias
616ebc09c36SJim Ingham // CommandObjectCommandsUnalias
617ebc09c36SJim Ingham
618b9c1b51eSKate Stone class CommandObjectCommandsUnalias : public CommandObjectParsed {
619ebc09c36SJim Ingham public:
CommandObjectCommandsUnalias(CommandInterpreter & interpreter)6207428a18cSKate Stone CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
621b9c1b51eSKate Stone : CommandObjectParsed(
622b9c1b51eSKate Stone interpreter, "command unalias",
623b9c1b51eSKate Stone "Delete one or more custom commands defined by 'command alias'.",
624b9c1b51eSKate Stone nullptr) {
625405fe67fSCaroline Tice CommandArgumentEntry arg;
626405fe67fSCaroline Tice CommandArgumentData alias_arg;
627405fe67fSCaroline Tice
628405fe67fSCaroline Tice // Define the first (and only) variant of this arg.
629405fe67fSCaroline Tice alias_arg.arg_type = eArgTypeAliasName;
630405fe67fSCaroline Tice alias_arg.arg_repetition = eArgRepeatPlain;
631405fe67fSCaroline Tice
632b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
633b9c1b51eSKate Stone // argument entry.
634405fe67fSCaroline Tice arg.push_back(alias_arg);
635405fe67fSCaroline Tice
636405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector.
637405fe67fSCaroline Tice m_arguments.push_back(arg);
638ebc09c36SJim Ingham }
639ebc09c36SJim Ingham
6406e3d8e7fSEugene Zelenko ~CommandObjectCommandsUnalias() override = default;
641ebc09c36SJim Ingham
64231fd64acSGongyu Deng void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)64331fd64acSGongyu Deng HandleArgumentCompletion(CompletionRequest &request,
64431fd64acSGongyu Deng OptionElementVector &opt_element_vector) override {
64531fd64acSGongyu Deng if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
64631fd64acSGongyu Deng return;
64731fd64acSGongyu Deng
64831fd64acSGongyu Deng for (const auto &ent : m_interpreter.GetAliases()) {
64931fd64acSGongyu Deng request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
65031fd64acSGongyu Deng }
65131fd64acSGongyu Deng }
65231fd64acSGongyu Deng
6535a988416SJim Ingham protected:
DoExecute(Args & args,CommandReturnObject & result)654b9c1b51eSKate Stone bool DoExecute(Args &args, CommandReturnObject &result) override {
655ebc09c36SJim Ingham CommandObject::CommandMap::iterator pos;
656ebc09c36SJim Ingham CommandObject *cmd_obj;
657ebc09c36SJim Ingham
65811eb9c64SZachary Turner if (args.empty()) {
65911eb9c64SZachary Turner result.AppendError("must call 'unalias' with a valid alias");
66011eb9c64SZachary Turner return false;
66111eb9c64SZachary Turner }
66211eb9c64SZachary Turner
6630d9a201eSRaphael Isemann auto command_name = args[0].ref();
664a7015092SGreg Clayton cmd_obj = m_interpreter.GetCommandObject(command_name);
6654574a890SZachary Turner if (!cmd_obj) {
6664574a890SZachary Turner result.AppendErrorWithFormat(
6674574a890SZachary Turner "'%s' is not a known command.\nTry 'help' to see a "
6684574a890SZachary Turner "current list of commands.\n",
669867e7d17SZachary Turner args[0].c_str());
6704574a890SZachary Turner return false;
6714574a890SZachary Turner }
6724574a890SZachary Turner
673b9c1b51eSKate Stone if (m_interpreter.CommandExists(command_name)) {
674b9c1b51eSKate Stone if (cmd_obj->IsRemovable()) {
675b9c1b51eSKate Stone result.AppendErrorWithFormat(
676b9c1b51eSKate Stone "'%s' is not an alias, it is a debugger command which can be "
677b9c1b51eSKate Stone "removed using the 'command delete' command.\n",
678867e7d17SZachary Turner args[0].c_str());
679b9c1b51eSKate Stone } else {
680b9c1b51eSKate Stone result.AppendErrorWithFormat(
681b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be removed.\n",
682867e7d17SZachary Turner args[0].c_str());
683b547278cSGreg Clayton }
6844574a890SZachary Turner return false;
6854574a890SZachary Turner }
6864574a890SZachary Turner
687b9c1b51eSKate Stone if (!m_interpreter.RemoveAlias(command_name)) {
688a7015092SGreg Clayton if (m_interpreter.AliasExists(command_name))
689b9c1b51eSKate Stone result.AppendErrorWithFormat(
690867e7d17SZachary Turner "Error occurred while attempting to unalias '%s'.\n",
691867e7d17SZachary Turner args[0].c_str());
692ebc09c36SJim Ingham else
693b9c1b51eSKate Stone result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
694867e7d17SZachary Turner args[0].c_str());
6954574a890SZachary Turner return false;
696ebc09c36SJim Ingham }
697ebc09c36SJim Ingham
6984574a890SZachary Turner result.SetStatus(eReturnStatusSuccessFinishNoResult);
699ebc09c36SJim Ingham return result.Succeeded();
700ebc09c36SJim Ingham }
701ebc09c36SJim Ingham };
702ebc09c36SJim Ingham
703b547278cSGreg Clayton #pragma mark CommandObjectCommandsDelete
704b547278cSGreg Clayton // CommandObjectCommandsDelete
705b547278cSGreg Clayton
706b9c1b51eSKate Stone class CommandObjectCommandsDelete : public CommandObjectParsed {
707b547278cSGreg Clayton public:
CommandObjectCommandsDelete(CommandInterpreter & interpreter)7087428a18cSKate Stone CommandObjectCommandsDelete(CommandInterpreter &interpreter)
709b9c1b51eSKate Stone : CommandObjectParsed(
710b9c1b51eSKate Stone interpreter, "command delete",
711b9c1b51eSKate Stone "Delete one or more custom commands defined by 'command regex'.",
712b9c1b51eSKate Stone nullptr) {
713b547278cSGreg Clayton CommandArgumentEntry arg;
714b547278cSGreg Clayton CommandArgumentData alias_arg;
715b547278cSGreg Clayton
716b547278cSGreg Clayton // Define the first (and only) variant of this arg.
717b547278cSGreg Clayton alias_arg.arg_type = eArgTypeCommandName;
718b547278cSGreg Clayton alias_arg.arg_repetition = eArgRepeatPlain;
719b547278cSGreg Clayton
720b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
721b9c1b51eSKate Stone // argument entry.
722b547278cSGreg Clayton arg.push_back(alias_arg);
723b547278cSGreg Clayton
724b547278cSGreg Clayton // Push the data for the first argument into the m_arguments vector.
725b547278cSGreg Clayton m_arguments.push_back(arg);
726b547278cSGreg Clayton }
727b547278cSGreg Clayton
7286e3d8e7fSEugene Zelenko ~CommandObjectCommandsDelete() override = default;
729b547278cSGreg Clayton
73031fd64acSGongyu Deng void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)73131fd64acSGongyu Deng HandleArgumentCompletion(CompletionRequest &request,
73231fd64acSGongyu Deng OptionElementVector &opt_element_vector) override {
73331fd64acSGongyu Deng if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
73431fd64acSGongyu Deng return;
73531fd64acSGongyu Deng
73631fd64acSGongyu Deng for (const auto &ent : m_interpreter.GetCommands()) {
73731fd64acSGongyu Deng if (ent.second->IsRemovable())
73831fd64acSGongyu Deng request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
73931fd64acSGongyu Deng }
74031fd64acSGongyu Deng }
74131fd64acSGongyu Deng
742b547278cSGreg Clayton protected:
DoExecute(Args & args,CommandReturnObject & result)743b9c1b51eSKate Stone bool DoExecute(Args &args, CommandReturnObject &result) override {
744b547278cSGreg Clayton CommandObject::CommandMap::iterator pos;
745b547278cSGreg Clayton
74611eb9c64SZachary Turner if (args.empty()) {
74711eb9c64SZachary Turner result.AppendErrorWithFormat("must call '%s' with one or more valid user "
74811eb9c64SZachary Turner "defined regular expression command names",
749a449698cSZachary Turner GetCommandName().str().c_str());
750d77ea5b2SRaphael Isemann return false;
75111eb9c64SZachary Turner }
75211eb9c64SZachary Turner
7530d9a201eSRaphael Isemann auto command_name = args[0].ref();
7544574a890SZachary Turner if (!m_interpreter.CommandExists(command_name)) {
75546d4aa21SEnrico Granata StreamString error_msg_stream;
756d5b44036SJonas Devlieghere const bool generate_upropos = true;
75746d4aa21SEnrico Granata const bool generate_type_lookup = false;
758b9c1b51eSKate Stone CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
7594574a890SZachary Turner &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
760d5b44036SJonas Devlieghere generate_upropos, generate_type_lookup);
761c156427dSZachary Turner result.AppendError(error_msg_stream.GetString());
7624574a890SZachary Turner return false;
763b547278cSGreg Clayton }
764b547278cSGreg Clayton
7654574a890SZachary Turner if (!m_interpreter.RemoveCommand(command_name)) {
7664574a890SZachary Turner result.AppendErrorWithFormat(
7674574a890SZachary Turner "'%s' is a permanent debugger command and cannot be removed.\n",
768867e7d17SZachary Turner args[0].c_str());
7694574a890SZachary Turner return false;
7704574a890SZachary Turner }
7714574a890SZachary Turner
7724574a890SZachary Turner result.SetStatus(eReturnStatusSuccessFinishNoResult);
7734574a890SZachary Turner return true;
774b547278cSGreg Clayton }
775b547278cSGreg Clayton };
776b547278cSGreg Clayton
777de164aaaSGreg Clayton // CommandObjectCommandsAddRegex
7781f0f5b5bSZachary Turner
77964becc11SRaphael Isemann #define LLDB_OPTIONS_regex
78064becc11SRaphael Isemann #include "CommandOptions.inc"
7811f0f5b5bSZachary Turner
7825a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex
783de164aaaSGreg Clayton
784b9c1b51eSKate Stone class CommandObjectCommandsAddRegex : public CommandObjectParsed,
785b9c1b51eSKate Stone public IOHandlerDelegateMultiline {
786de164aaaSGreg Clayton public:
CommandObjectCommandsAddRegex(CommandInterpreter & interpreter)7877428a18cSKate Stone CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
788b9c1b51eSKate Stone : CommandObjectParsed(
789a925974bSAdrian Prantl interpreter, "command regex",
790a925974bSAdrian Prantl "Define a custom command in terms of "
791b9c1b51eSKate Stone "existing commands by matching "
792b9c1b51eSKate Stone "regular expressions.",
7930e5e5a79SGreg Clayton "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
794b9c1b51eSKate Stone IOHandlerDelegateMultiline("",
795abb0ed44SKazu Hirata IOHandlerDelegate::Completion::LLDBCommand) {
796b9c1b51eSKate Stone SetHelpLong(
797b9c1b51eSKate Stone R"(
798b9c1b51eSKate Stone )"
799b9c1b51eSKate Stone "This command allows the user to create powerful regular expression commands \
800ea671fbdSKate Stone with substitutions. The regular expressions and substitutions are specified \
801b9c1b51eSKate Stone using the regular expression substitution format of:"
802b9c1b51eSKate Stone R"(
803ea671fbdSKate Stone
804ea671fbdSKate Stone s/<regex>/<subst>/
805ea671fbdSKate Stone
806b9c1b51eSKate Stone )"
807b9c1b51eSKate Stone "<regex> is a regular expression that can use parenthesis to capture regular \
808ea671fbdSKate Stone expression input and substitute the captured matches in the output using %1 \
809b9c1b51eSKate Stone for the first match, %2 for the second, and so on."
810b9c1b51eSKate Stone R"(
811ea671fbdSKate Stone
812b9c1b51eSKate Stone )"
813b9c1b51eSKate Stone "The regular expressions can all be specified on the command line if more than \
814ea671fbdSKate Stone one argument is provided. If just the command name is provided on the command \
815ea671fbdSKate Stone line, then the regular expressions and substitutions can be entered on separate \
816b9c1b51eSKate Stone lines, followed by an empty line to terminate the command definition."
817b9c1b51eSKate Stone R"(
818ea671fbdSKate Stone
819ea671fbdSKate Stone EXAMPLES
820ea671fbdSKate Stone
821b9c1b51eSKate Stone )"
822b9c1b51eSKate Stone "The following example will define a regular expression command named 'f' that \
823ea671fbdSKate Stone will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
824b9c1b51eSKate Stone a number follows 'f':"
825b9c1b51eSKate Stone R"(
826ea671fbdSKate Stone
827b9c1b51eSKate Stone (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
828c1b07d61SJim Ingham CommandArgumentData thread_arg{eArgTypeSEDStylePair, eArgRepeatOptional};
829c1b07d61SJim Ingham m_arguments.push_back({thread_arg});
830de164aaaSGreg Clayton }
831de164aaaSGreg Clayton
8326e3d8e7fSEugene Zelenko ~CommandObjectCommandsAddRegex() override = default;
833de164aaaSGreg Clayton
8345a988416SJim Ingham protected:
IOHandlerActivated(IOHandler & io_handler,bool interactive)8350affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
8367ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
8370affb582SDave Lee if (output_sp && interactive) {
8380affb582SDave Lee output_sp->PutCString("Enter one or more sed substitution commands in "
839b9c1b51eSKate Stone "the form: 's/<regex>/<subst>/'.\nTerminate the "
840b9c1b51eSKate Stone "substitution list with an empty line.\n");
84144d93782SGreg Clayton output_sp->Flush();
84244d93782SGreg Clayton }
84344d93782SGreg Clayton }
84444d93782SGreg Clayton
IOHandlerInputComplete(IOHandler & io_handler,std::string & data)845b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler,
846b9c1b51eSKate Stone std::string &data) override {
84744d93782SGreg Clayton io_handler.SetIsDone(true);
848d5b44036SJonas Devlieghere if (m_regex_cmd_up) {
84944d93782SGreg Clayton StringList lines;
850b9c1b51eSKate Stone if (lines.SplitIntoLines(data)) {
85144d93782SGreg Clayton bool check_only = false;
8524c78b788SRaphael Isemann for (const std::string &line : lines) {
8534c78b788SRaphael Isemann Status error = AppendRegexSubstitution(line, check_only);
854b9c1b51eSKate Stone if (error.Fail()) {
85557179860SJonas Devlieghere if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
85657179860SJonas Devlieghere StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
85744d93782SGreg Clayton out_stream->Printf("error: %s\n", error.AsCString());
85844d93782SGreg Clayton }
85944d93782SGreg Clayton }
86044d93782SGreg Clayton }
86144d93782SGreg Clayton }
862d5b44036SJonas Devlieghere if (m_regex_cmd_up->HasRegexEntries()) {
863d5b44036SJonas Devlieghere CommandObjectSP cmd_sp(m_regex_cmd_up.release());
86444d93782SGreg Clayton m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
86544d93782SGreg Clayton }
86644d93782SGreg Clayton }
86744d93782SGreg Clayton }
86844d93782SGreg Clayton
DoExecute(Args & command,CommandReturnObject & result)869b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
8705a988416SJim Ingham const size_t argc = command.GetArgumentCount();
871b9c1b51eSKate Stone if (argc == 0) {
872b9c1b51eSKate Stone result.AppendError("usage: 'command regex <command-name> "
873b9c1b51eSKate Stone "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
87411eb9c64SZachary Turner return false;
87511eb9c64SZachary Turner }
87611eb9c64SZachary Turner
87797206d57SZachary Turner Status error;
8780d9a201eSRaphael Isemann auto name = command[0].ref();
879a8f3ae7cSJonas Devlieghere m_regex_cmd_up = std::make_unique<CommandObjectRegexCommand>(
8804574a890SZachary Turner m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 0,
8814574a890SZachary Turner true);
8820e5e5a79SGreg Clayton
883b9c1b51eSKate Stone if (argc == 1) {
88457179860SJonas Devlieghere Debugger &debugger = GetDebugger();
885e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor();
88644d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines
887b9c1b51eSKate Stone IOHandlerSP io_handler_sp(new IOHandlerEditline(
888b9c1b51eSKate Stone debugger, IOHandler::Type::Other,
88973d80faaSGreg Clayton "lldb-regex", // Name of input reader for history
890514d8cd8SZachary Turner llvm::StringRef("> "), // Prompt
891514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt
892b9c1b51eSKate Stone multiple_lines, color_prompt,
893f6913cd7SGreg Clayton 0, // Don't show line numbers
894d77c2e09SJonas Devlieghere *this, nullptr));
89544d93782SGreg Clayton
896b9c1b51eSKate Stone if (io_handler_sp) {
8977ce2de2cSJonas Devlieghere debugger.RunIOHandlerAsync(io_handler_sp);
898de164aaaSGreg Clayton result.SetStatus(eReturnStatusSuccessFinishNoResult);
899de164aaaSGreg Clayton }
900b9c1b51eSKate Stone } else {
90197d2c401SZachary Turner for (auto &entry : command.entries().drop_front()) {
90244d93782SGreg Clayton bool check_only = false;
9030d9a201eSRaphael Isemann error = AppendRegexSubstitution(entry.ref(), check_only);
9040e5e5a79SGreg Clayton if (error.Fail())
9050e5e5a79SGreg Clayton break;
9060e5e5a79SGreg Clayton }
9070e5e5a79SGreg Clayton
908b9c1b51eSKate Stone if (error.Success()) {
9090e5e5a79SGreg Clayton AddRegexCommandToInterpreter();
9100e5e5a79SGreg Clayton }
9110e5e5a79SGreg Clayton }
912b9c1b51eSKate Stone if (error.Fail()) {
9130e5e5a79SGreg Clayton result.AppendError(error.AsCString());
914de164aaaSGreg Clayton }
9150e5e5a79SGreg Clayton
916de164aaaSGreg Clayton return result.Succeeded();
917de164aaaSGreg Clayton }
918de164aaaSGreg Clayton
AppendRegexSubstitution(const llvm::StringRef & regex_sed,bool check_only)91997206d57SZachary Turner Status AppendRegexSubstitution(const llvm::StringRef ®ex_sed,
920b9c1b51eSKate Stone bool check_only) {
92197206d57SZachary Turner Status error;
9220e5e5a79SGreg Clayton
923d5b44036SJonas Devlieghere if (!m_regex_cmd_up) {
924b9c1b51eSKate Stone error.SetErrorStringWithFormat(
925b9c1b51eSKate Stone "invalid regular expression command object for: '%.*s'",
926b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data());
9270e5e5a79SGreg Clayton return error;
928de164aaaSGreg Clayton }
9290e5e5a79SGreg Clayton
9300e5e5a79SGreg Clayton size_t regex_sed_size = regex_sed.size();
9310e5e5a79SGreg Clayton
932b9c1b51eSKate Stone if (regex_sed_size <= 1) {
933b9c1b51eSKate Stone error.SetErrorStringWithFormat(
934b9c1b51eSKate Stone "regular expression substitution string is too short: '%.*s'",
935b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data());
9360e5e5a79SGreg Clayton return error;
9370e5e5a79SGreg Clayton }
9380e5e5a79SGreg Clayton
939b9c1b51eSKate Stone if (regex_sed[0] != 's') {
940b9c1b51eSKate Stone error.SetErrorStringWithFormat("regular expression substitution string "
941b9c1b51eSKate Stone "doesn't start with 's': '%.*s'",
942b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data());
9430e5e5a79SGreg Clayton return error;
9440e5e5a79SGreg Clayton }
9450e5e5a79SGreg Clayton const size_t first_separator_char_pos = 1;
94605097246SAdrian Prantl // use the char that follows 's' as the regex separator character so we can
94705097246SAdrian Prantl // have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
9480e5e5a79SGreg Clayton const char separator_char = regex_sed[first_separator_char_pos];
949b9c1b51eSKate Stone const size_t second_separator_char_pos =
950b9c1b51eSKate Stone regex_sed.find(separator_char, first_separator_char_pos + 1);
9510e5e5a79SGreg Clayton
952b9c1b51eSKate Stone if (second_separator_char_pos == std::string::npos) {
953b9c1b51eSKate Stone error.SetErrorStringWithFormat(
954b9c1b51eSKate Stone "missing second '%c' separator char after '%.*s' in '%.*s'",
9550e5e5a79SGreg Clayton separator_char,
9560e5e5a79SGreg Clayton (int)(regex_sed.size() - first_separator_char_pos - 1),
957ea508635SGreg Clayton regex_sed.data() + (first_separator_char_pos + 1),
958b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data());
9590e5e5a79SGreg Clayton return error;
9600e5e5a79SGreg Clayton }
9610e5e5a79SGreg Clayton
962b9c1b51eSKate Stone const size_t third_separator_char_pos =
963b9c1b51eSKate Stone regex_sed.find(separator_char, second_separator_char_pos + 1);
9640e5e5a79SGreg Clayton
965b9c1b51eSKate Stone if (third_separator_char_pos == std::string::npos) {
966b9c1b51eSKate Stone error.SetErrorStringWithFormat(
967b9c1b51eSKate Stone "missing third '%c' separator char after '%.*s' in '%.*s'",
9680e5e5a79SGreg Clayton separator_char,
9690e5e5a79SGreg Clayton (int)(regex_sed.size() - second_separator_char_pos - 1),
970ea508635SGreg Clayton regex_sed.data() + (second_separator_char_pos + 1),
971b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data());
9720e5e5a79SGreg Clayton return error;
9730e5e5a79SGreg Clayton }
9740e5e5a79SGreg Clayton
975b9c1b51eSKate Stone if (third_separator_char_pos != regex_sed_size - 1) {
97605097246SAdrian Prantl // Make sure that everything that follows the last regex separator char
977b9c1b51eSKate Stone if (regex_sed.find_first_not_of("\t\n\v\f\r ",
978b9c1b51eSKate Stone third_separator_char_pos + 1) !=
979b9c1b51eSKate Stone std::string::npos) {
980b9c1b51eSKate Stone error.SetErrorStringWithFormat(
981b9c1b51eSKate Stone "extra data found after the '%.*s' regular expression substitution "
982b9c1b51eSKate Stone "string: '%.*s'",
983b9c1b51eSKate Stone (int)third_separator_char_pos + 1, regex_sed.data(),
9840e5e5a79SGreg Clayton (int)(regex_sed.size() - third_separator_char_pos - 1),
9850e5e5a79SGreg Clayton regex_sed.data() + (third_separator_char_pos + 1));
9860e5e5a79SGreg Clayton return error;
9870e5e5a79SGreg Clayton }
988b9c1b51eSKate Stone } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
989b9c1b51eSKate Stone error.SetErrorStringWithFormat(
990b9c1b51eSKate Stone "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
991b9c1b51eSKate Stone separator_char, separator_char, separator_char, (int)regex_sed.size(),
9920e5e5a79SGreg Clayton regex_sed.data());
9930e5e5a79SGreg Clayton return error;
994b9c1b51eSKate Stone } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
995b9c1b51eSKate Stone error.SetErrorStringWithFormat(
996b9c1b51eSKate Stone "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
997b9c1b51eSKate Stone separator_char, separator_char, separator_char, (int)regex_sed.size(),
9980e5e5a79SGreg Clayton regex_sed.data());
9990e5e5a79SGreg Clayton return error;
10000e5e5a79SGreg Clayton }
100144d93782SGreg Clayton
1002b9c1b51eSKate Stone if (!check_only) {
1003adcd0268SBenjamin Kramer std::string regex(std::string(regex_sed.substr(
1004adcd0268SBenjamin Kramer first_separator_char_pos + 1,
1005adcd0268SBenjamin Kramer second_separator_char_pos - first_separator_char_pos - 1)));
1006adcd0268SBenjamin Kramer std::string subst(std::string(regex_sed.substr(
1007adcd0268SBenjamin Kramer second_separator_char_pos + 1,
1008adcd0268SBenjamin Kramer third_separator_char_pos - second_separator_char_pos - 1)));
100943224195SRaphael Isemann m_regex_cmd_up->AddRegexCommand(regex, subst);
101044d93782SGreg Clayton }
10110e5e5a79SGreg Clayton return error;
1012de164aaaSGreg Clayton }
1013de164aaaSGreg Clayton
AddRegexCommandToInterpreter()1014b9c1b51eSKate Stone void AddRegexCommandToInterpreter() {
1015d5b44036SJonas Devlieghere if (m_regex_cmd_up) {
1016d5b44036SJonas Devlieghere if (m_regex_cmd_up->HasRegexEntries()) {
1017d5b44036SJonas Devlieghere CommandObjectSP cmd_sp(m_regex_cmd_up.release());
1018de164aaaSGreg Clayton m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1019de164aaaSGreg Clayton }
1020de164aaaSGreg Clayton }
1021de164aaaSGreg Clayton }
1022de164aaaSGreg Clayton
1023de164aaaSGreg Clayton private:
1024d5b44036SJonas Devlieghere std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_up;
1025de164aaaSGreg Clayton
1026b9c1b51eSKate Stone class CommandOptions : public Options {
1027de164aaaSGreg Clayton public:
102824f9a2f5SShafik Yaghmour CommandOptions() = default;
1029de164aaaSGreg Clayton
10306e3d8e7fSEugene Zelenko ~CommandOptions() override = default;
1031de164aaaSGreg Clayton
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)103297206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1033b9c1b51eSKate Stone ExecutionContext *execution_context) override {
103497206d57SZachary Turner Status error;
10353bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val;
1036de164aaaSGreg Clayton
1037b9c1b51eSKate Stone switch (short_option) {
1038de164aaaSGreg Clayton case 'h':
1039adcd0268SBenjamin Kramer m_help.assign(std::string(option_arg));
1040de164aaaSGreg Clayton break;
1041de164aaaSGreg Clayton case 's':
1042adcd0268SBenjamin Kramer m_syntax.assign(std::string(option_arg));
1043de164aaaSGreg Clayton break;
1044de164aaaSGreg Clayton default:
104536162014SRaphael Isemann llvm_unreachable("Unimplemented option");
1046de164aaaSGreg Clayton }
1047de164aaaSGreg Clayton
1048de164aaaSGreg Clayton return error;
1049de164aaaSGreg Clayton }
1050de164aaaSGreg Clayton
OptionParsingStarting(ExecutionContext * execution_context)1051b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override {
1052de164aaaSGreg Clayton m_help.clear();
1053de164aaaSGreg Clayton m_syntax.clear();
1054de164aaaSGreg Clayton }
1055de164aaaSGreg Clayton
GetDefinitions()10561f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
105770602439SZachary Turner return llvm::makeArrayRef(g_regex_options);
10581f0f5b5bSZachary Turner }
1059de164aaaSGreg Clayton
GetHelp()1060daed98e5SShivam Mittal llvm::StringRef GetHelp() { return m_help; }
10616e3d8e7fSEugene Zelenko
GetSyntax()1062daed98e5SShivam Mittal llvm::StringRef GetSyntax() { return m_syntax; }
10636e3d8e7fSEugene Zelenko
1064de164aaaSGreg Clayton protected:
10656e3d8e7fSEugene Zelenko // Instance variables to hold the values for command options.
10666e3d8e7fSEugene Zelenko
1067de164aaaSGreg Clayton std::string m_help;
1068de164aaaSGreg Clayton std::string m_syntax;
1069de164aaaSGreg Clayton };
1070de164aaaSGreg Clayton
GetOptions()1071b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; }
1072de164aaaSGreg Clayton
10735a988416SJim Ingham CommandOptions m_options;
1074de164aaaSGreg Clayton };
1075de164aaaSGreg Clayton
1076b9c1b51eSKate Stone class CommandObjectPythonFunction : public CommandObjectRaw {
1077223383edSEnrico Granata public:
CommandObjectPythonFunction(CommandInterpreter & interpreter,std::string name,std::string funct,std::string help,ScriptedCommandSynchronicity synch)1078b9c1b51eSKate Stone CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1079b9c1b51eSKate Stone std::string funct, std::string help,
1080b9c1b51eSKate Stone ScriptedCommandSynchronicity synch)
1081a925974bSAdrian Prantl : CommandObjectRaw(interpreter, name), m_function_name(funct),
108228c878aeSShafik Yaghmour m_synchro(synch) {
1083735152e3SEnrico Granata if (!help.empty())
1084442f6530SZachary Turner SetHelp(help);
1085b9c1b51eSKate Stone else {
1086735152e3SEnrico Granata StreamString stream;
1087735152e3SEnrico Granata stream.Printf("For more information run 'help %s'", name.c_str());
1088c156427dSZachary Turner SetHelp(stream.GetString());
1089735152e3SEnrico Granata }
1090223383edSEnrico Granata }
1091223383edSEnrico Granata
10926e3d8e7fSEugene Zelenko ~CommandObjectPythonFunction() override = default;
1093223383edSEnrico Granata
IsRemovable() const1094b9c1b51eSKate Stone bool IsRemovable() const override { return true; }
10955a988416SJim Ingham
GetFunctionName()1096b9c1b51eSKate Stone const std::string &GetFunctionName() { return m_function_name; }
10975a988416SJim Ingham
GetSynchronicity()1098b9c1b51eSKate Stone ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
10995a988416SJim Ingham
GetHelpLong()1100442f6530SZachary Turner llvm::StringRef GetHelpLong() override {
1101442f6530SZachary Turner if (m_fetched_help_long)
1102442f6530SZachary Turner return CommandObjectRaw::GetHelpLong();
1103442f6530SZachary Turner
11042b29b432SJonas Devlieghere ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1105442f6530SZachary Turner if (!scripter)
1106442f6530SZachary Turner return CommandObjectRaw::GetHelpLong();
1107442f6530SZachary Turner
1108fac939e9SEnrico Granata std::string docstring;
1109442f6530SZachary Turner m_fetched_help_long =
1110442f6530SZachary Turner scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1111fac939e9SEnrico Granata if (!docstring.empty())
1112442f6530SZachary Turner SetHelpLong(docstring);
1113fac939e9SEnrico Granata return CommandObjectRaw::GetHelpLong();
1114fac939e9SEnrico Granata }
1115fac939e9SEnrico Granata
11165a988416SJim Ingham protected:
DoExecute(llvm::StringRef raw_command_line,CommandReturnObject & result)11174d51a902SRaphael Isemann bool DoExecute(llvm::StringRef raw_command_line,
1118b9c1b51eSKate Stone CommandReturnObject &result) override {
11192b29b432SJonas Devlieghere ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1120223383edSEnrico Granata
112197206d57SZachary Turner Status error;
1122223383edSEnrico Granata
112370f11f88SJim Ingham result.SetStatus(eReturnStatusInvalid);
112470f11f88SJim Ingham
1125a925974bSAdrian Prantl if (!scripter || !scripter->RunScriptBasedCommand(
1126a925974bSAdrian Prantl m_function_name.c_str(), raw_command_line, m_synchro,
1127a925974bSAdrian Prantl result, error, m_exe_ctx)) {
1128223383edSEnrico Granata result.AppendError(error.AsCString());
1129b9c1b51eSKate Stone } else {
113070f11f88SJim Ingham // Don't change the status if the command already set it...
1131b9c1b51eSKate Stone if (result.GetStatus() == eReturnStatusInvalid) {
1132c156427dSZachary Turner if (result.GetOutputData().empty())
1133223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult);
113470f11f88SJim Ingham else
113570f11f88SJim Ingham result.SetStatus(eReturnStatusSuccessFinishResult);
113670f11f88SJim Ingham }
113770f11f88SJim Ingham }
1138223383edSEnrico Granata
1139223383edSEnrico Granata return result.Succeeded();
1140223383edSEnrico Granata }
1141223383edSEnrico Granata
11426e3d8e7fSEugene Zelenko private:
11436e3d8e7fSEugene Zelenko std::string m_function_name;
11446e3d8e7fSEugene Zelenko ScriptedCommandSynchronicity m_synchro;
114528c878aeSShafik Yaghmour bool m_fetched_help_long = false;
1146223383edSEnrico Granata };
1147223383edSEnrico Granata
1148b9c1b51eSKate Stone class CommandObjectScriptingObject : public CommandObjectRaw {
11499fe00e52SEnrico Granata public:
CommandObjectScriptingObject(CommandInterpreter & interpreter,std::string name,StructuredData::GenericSP cmd_obj_sp,ScriptedCommandSynchronicity synch)11509fe00e52SEnrico Granata CommandObjectScriptingObject(CommandInterpreter &interpreter,
11519fe00e52SEnrico Granata std::string name,
11520641ca1aSZachary Turner StructuredData::GenericSP cmd_obj_sp,
1153b9c1b51eSKate Stone ScriptedCommandSynchronicity synch)
1154a925974bSAdrian Prantl : CommandObjectRaw(interpreter, name), m_cmd_obj_sp(cmd_obj_sp),
1155a925974bSAdrian Prantl m_synchro(synch), m_fetched_help_short(false),
1156b9c1b51eSKate Stone m_fetched_help_long(false) {
11579fe00e52SEnrico Granata StreamString stream;
11589fe00e52SEnrico Granata stream.Printf("For more information run 'help %s'", name.c_str());
1159c156427dSZachary Turner SetHelp(stream.GetString());
11602b29b432SJonas Devlieghere if (ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter())
1161e87764f2SEnrico Granata GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
11629fe00e52SEnrico Granata }
11639fe00e52SEnrico Granata
11646e3d8e7fSEugene Zelenko ~CommandObjectScriptingObject() override = default;
11659fe00e52SEnrico Granata
IsRemovable() const1166b9c1b51eSKate Stone bool IsRemovable() const override { return true; }
11679fe00e52SEnrico Granata
GetSynchronicity()1168b9c1b51eSKate Stone ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
11699fe00e52SEnrico Granata
GetHelp()1170442f6530SZachary Turner llvm::StringRef GetHelp() override {
1171442f6530SZachary Turner if (m_fetched_help_short)
1172442f6530SZachary Turner return CommandObjectRaw::GetHelp();
11732b29b432SJonas Devlieghere ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1174442f6530SZachary Turner if (!scripter)
1175442f6530SZachary Turner return CommandObjectRaw::GetHelp();
11766f79bb2dSEnrico Granata std::string docstring;
1177b9c1b51eSKate Stone m_fetched_help_short =
1178b9c1b51eSKate Stone scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
11796f79bb2dSEnrico Granata if (!docstring.empty())
1180442f6530SZachary Turner SetHelp(docstring);
1181442f6530SZachary Turner
11826f79bb2dSEnrico Granata return CommandObjectRaw::GetHelp();
11836f79bb2dSEnrico Granata }
11846f79bb2dSEnrico Granata
GetHelpLong()1185442f6530SZachary Turner llvm::StringRef GetHelpLong() override {
1186442f6530SZachary Turner if (m_fetched_help_long)
1187442f6530SZachary Turner return CommandObjectRaw::GetHelpLong();
1188442f6530SZachary Turner
11892b29b432SJonas Devlieghere ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1190442f6530SZachary Turner if (!scripter)
1191442f6530SZachary Turner return CommandObjectRaw::GetHelpLong();
1192442f6530SZachary Turner
11936f79bb2dSEnrico Granata std::string docstring;
1194b9c1b51eSKate Stone m_fetched_help_long =
1195b9c1b51eSKate Stone scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
11966f79bb2dSEnrico Granata if (!docstring.empty())
1197442f6530SZachary Turner SetHelpLong(docstring);
11989fe00e52SEnrico Granata return CommandObjectRaw::GetHelpLong();
11999fe00e52SEnrico Granata }
12009fe00e52SEnrico Granata
12019fe00e52SEnrico Granata protected:
DoExecute(llvm::StringRef raw_command_line,CommandReturnObject & result)12024d51a902SRaphael Isemann bool DoExecute(llvm::StringRef raw_command_line,
1203b9c1b51eSKate Stone CommandReturnObject &result) override {
12042b29b432SJonas Devlieghere ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
12059fe00e52SEnrico Granata
120697206d57SZachary Turner Status error;
12079fe00e52SEnrico Granata
12089fe00e52SEnrico Granata result.SetStatus(eReturnStatusInvalid);
12099fe00e52SEnrico Granata
1210b9c1b51eSKate Stone if (!scripter ||
1211b9c1b51eSKate Stone !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1212b9c1b51eSKate Stone m_synchro, result, error, m_exe_ctx)) {
12139fe00e52SEnrico Granata result.AppendError(error.AsCString());
1214b9c1b51eSKate Stone } else {
12159fe00e52SEnrico Granata // Don't change the status if the command already set it...
1216b9c1b51eSKate Stone if (result.GetStatus() == eReturnStatusInvalid) {
1217c156427dSZachary Turner if (result.GetOutputData().empty())
12189fe00e52SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult);
12199fe00e52SEnrico Granata else
12209fe00e52SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult);
12219fe00e52SEnrico Granata }
12229fe00e52SEnrico Granata }
12239fe00e52SEnrico Granata
12249fe00e52SEnrico Granata return result.Succeeded();
12259fe00e52SEnrico Granata }
12269fe00e52SEnrico Granata
12276e3d8e7fSEugene Zelenko private:
12286e3d8e7fSEugene Zelenko StructuredData::GenericSP m_cmd_obj_sp;
12296e3d8e7fSEugene Zelenko ScriptedCommandSynchronicity m_synchro;
12306e3d8e7fSEugene Zelenko bool m_fetched_help_short : 1;
12316e3d8e7fSEugene Zelenko bool m_fetched_help_long : 1;
12329fe00e52SEnrico Granata };
12339fe00e52SEnrico Granata
1234a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport
123564becc11SRaphael Isemann #define LLDB_OPTIONS_script_import
123664becc11SRaphael Isemann #include "CommandOptions.inc"
12371f0f5b5bSZachary Turner
1238b9c1b51eSKate Stone class CommandObjectCommandsScriptImport : public CommandObjectParsed {
12395a988416SJim Ingham public:
CommandObjectCommandsScriptImport(CommandInterpreter & interpreter)1240b9c1b51eSKate Stone CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1241b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script import",
1242abb0ed44SKazu Hirata "Import a scripting module in LLDB.", nullptr) {
12435a988416SJim Ingham CommandArgumentEntry arg1;
12445a988416SJim Ingham CommandArgumentData cmd_arg;
12455a988416SJim Ingham
12465a988416SJim Ingham // Define the first (and only) variant of this arg.
12475a988416SJim Ingham cmd_arg.arg_type = eArgTypeFilename;
12483b00e35bSEnrico Granata cmd_arg.arg_repetition = eArgRepeatPlus;
12495a988416SJim Ingham
1250b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
1251b9c1b51eSKate Stone // argument entry.
12525a988416SJim Ingham arg1.push_back(cmd_arg);
12535a988416SJim Ingham
12545a988416SJim Ingham // Push the data for the first argument into the m_arguments vector.
12555a988416SJim Ingham m_arguments.push_back(arg1);
12565a988416SJim Ingham }
12575a988416SJim Ingham
12586e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptImport() override = default;
12595a988416SJim Ingham
1260ae34ed2cSRaphael Isemann void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)1261ae34ed2cSRaphael Isemann HandleArgumentCompletion(CompletionRequest &request,
12622443bbd4SRaphael Isemann OptionElementVector &opt_element_vector) override {
1263b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks(
1264b9c1b51eSKate Stone GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1265a2e76c0bSRaphael Isemann request, nullptr);
12665a988416SJim Ingham }
12675a988416SJim Ingham
GetOptions()1268b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; }
12695a988416SJim Ingham
12705a988416SJim Ingham protected:
1271b9c1b51eSKate Stone class CommandOptions : public Options {
12720a305db7SEnrico Granata public:
127324f9a2f5SShafik Yaghmour CommandOptions() = default;
12740a305db7SEnrico Granata
12756e3d8e7fSEugene Zelenko ~CommandOptions() override = default;
12760a305db7SEnrico Granata
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)127797206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1278b9c1b51eSKate Stone ExecutionContext *execution_context) override {
127997206d57SZachary Turner Status error;
12803bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val;
12810a305db7SEnrico Granata
1282b9c1b51eSKate Stone switch (short_option) {
12830a305db7SEnrico Granata case 'r':
128415625112SJonas Devlieghere // NO-OP
12850a305db7SEnrico Granata break;
128600bb397bSJonas Devlieghere case 'c':
128700bb397bSJonas Devlieghere relative_to_command_file = true;
128800bb397bSJonas Devlieghere break;
1289f9517353SJonas Devlieghere case 's':
1290f9517353SJonas Devlieghere silent = true;
1291f9517353SJonas Devlieghere break;
12920a305db7SEnrico Granata default:
129336162014SRaphael Isemann llvm_unreachable("Unimplemented option");
12940a305db7SEnrico Granata }
12950a305db7SEnrico Granata
12960a305db7SEnrico Granata return error;
12970a305db7SEnrico Granata }
12980a305db7SEnrico Granata
OptionParsingStarting(ExecutionContext * execution_context)1299b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override {
130000bb397bSJonas Devlieghere relative_to_command_file = false;
13010a305db7SEnrico Granata }
13020a305db7SEnrico Granata
GetDefinitions()13031f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
130470602439SZachary Turner return llvm::makeArrayRef(g_script_import_options);
13051f0f5b5bSZachary Turner }
130600bb397bSJonas Devlieghere bool relative_to_command_file = false;
1307f9517353SJonas Devlieghere bool silent = false;
13080a305db7SEnrico Granata };
13090a305db7SEnrico Granata
DoExecute(Args & command,CommandReturnObject & result)1310b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
131111eb9c64SZachary Turner if (command.empty()) {
13123b00e35bSEnrico Granata result.AppendError("command script import needs one or more arguments");
1313a9dbf432SEnrico Granata return false;
1314a9dbf432SEnrico Granata }
1315a9dbf432SEnrico Granata
131600bb397bSJonas Devlieghere FileSpec source_dir = {};
131700bb397bSJonas Devlieghere if (m_options.relative_to_command_file) {
131800bb397bSJonas Devlieghere source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir();
131900bb397bSJonas Devlieghere if (!source_dir) {
132000bb397bSJonas Devlieghere result.AppendError("command script import -c can only be specified "
132100bb397bSJonas Devlieghere "from a command file");
132200bb397bSJonas Devlieghere return false;
132300bb397bSJonas Devlieghere }
132400bb397bSJonas Devlieghere }
132500bb397bSJonas Devlieghere
132611eb9c64SZachary Turner for (auto &entry : command.entries()) {
132797206d57SZachary Turner Status error;
1328a9dbf432SEnrico Granata
1329f9517353SJonas Devlieghere LoadScriptOptions options;
1330f9517353SJonas Devlieghere options.SetInitSession(true);
1331f9517353SJonas Devlieghere options.SetSilent(m_options.silent);
1332f9517353SJonas Devlieghere
1333b9c1b51eSKate Stone // FIXME: this is necessary because CommandObject::CheckRequirements()
133411eb9c64SZachary Turner // assumes that commands won't ever be recursively invoked, but it's
133511eb9c64SZachary Turner // actually possible to craft a Python script that does other "command
133605097246SAdrian Prantl // script imports" in __lldb_init_module the real fix is to have
133705097246SAdrian Prantl // recursive commands possible with a CommandInvocation object separate
133805097246SAdrian Prantl // from the CommandObject itself, so that recursive command invocations
133905097246SAdrian Prantl // won't stomp on each other (wrt to execution contents, options, and
134005097246SAdrian Prantl // more)
1341078551c7SEnrico Granata m_exe_ctx.Clear();
13422b29b432SJonas Devlieghere if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
1343f9517353SJonas Devlieghere entry.c_str(), options, error, /*module_sp=*/nullptr,
1344f9517353SJonas Devlieghere source_dir)) {
1345a9dbf432SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult);
1346b9c1b51eSKate Stone } else {
1347b9c1b51eSKate Stone result.AppendErrorWithFormat("module importing failed: %s",
1348b9c1b51eSKate Stone error.AsCString());
1349a9dbf432SEnrico Granata }
13503b00e35bSEnrico Granata }
1351a9dbf432SEnrico Granata
1352a9dbf432SEnrico Granata return result.Succeeded();
1353a9dbf432SEnrico Granata }
13540a305db7SEnrico Granata
13555a988416SJim Ingham CommandOptions m_options;
1356a9dbf432SEnrico Granata };
1357223383edSEnrico Granata
135864becc11SRaphael Isemann #define LLDB_OPTIONS_script_add
135964becc11SRaphael Isemann #include "CommandOptions.inc"
13601f0f5b5bSZachary Turner
1361b9c1b51eSKate Stone class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1362b9c1b51eSKate Stone public IOHandlerDelegateMultiline {
13635a988416SJim Ingham public:
CommandObjectCommandsScriptAdd(CommandInterpreter & interpreter)1364b9c1b51eSKate Stone CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1365b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script add",
13665a988416SJim Ingham "Add a scripted function as an LLDB command.",
1367c5011aedSJim Ingham "Add a scripted function as an lldb command. "
1368c5011aedSJim Ingham "If you provide a single argument, the command "
1369c5011aedSJim Ingham "will be added at the root level of the command "
1370c5011aedSJim Ingham "hierarchy. If there are more arguments they "
1371c5011aedSJim Ingham "must be a path to a user-added container "
1372c5011aedSJim Ingham "command, and the last element will be the new "
1373c5011aedSJim Ingham "command name."),
1374abb0ed44SKazu Hirata IOHandlerDelegateMultiline("DONE") {
13755a988416SJim Ingham CommandArgumentEntry arg1;
13765a988416SJim Ingham CommandArgumentData cmd_arg;
13775a988416SJim Ingham
1378c5011aedSJim Ingham // This is one or more command names, which form the path to the command
1379c5011aedSJim Ingham // you want to add.
1380c5011aedSJim Ingham cmd_arg.arg_type = eArgTypeCommand;
1381c5011aedSJim Ingham cmd_arg.arg_repetition = eArgRepeatPlus;
13825a988416SJim Ingham
1383b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
1384b9c1b51eSKate Stone // argument entry.
13855a988416SJim Ingham arg1.push_back(cmd_arg);
13865a988416SJim Ingham
13875a988416SJim Ingham // Push the data for the first argument into the m_arguments vector.
13885a988416SJim Ingham m_arguments.push_back(arg1);
13895a988416SJim Ingham }
13905a988416SJim Ingham
13916e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptAdd() override = default;
13925a988416SJim Ingham
GetOptions()1393b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; }
13945a988416SJim Ingham
1395c5011aedSJim Ingham void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)1396c5011aedSJim Ingham HandleArgumentCompletion(CompletionRequest &request,
1397c5011aedSJim Ingham OptionElementVector &opt_element_vector) override {
1398c5011aedSJim Ingham CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request,
1399c5011aedSJim Ingham opt_element_vector);
1400c5011aedSJim Ingham }
1401c5011aedSJim Ingham
14025a988416SJim Ingham protected:
1403b9c1b51eSKate Stone class CommandOptions : public Options {
1404223383edSEnrico Granata public:
140524f9a2f5SShafik Yaghmour CommandOptions() = default;
1406223383edSEnrico Granata
14076e3d8e7fSEugene Zelenko ~CommandOptions() override = default;
1408223383edSEnrico Granata
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)140997206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1410b9c1b51eSKate Stone ExecutionContext *execution_context) override {
141197206d57SZachary Turner Status error;
14123bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val;
1413223383edSEnrico Granata
1414b9c1b51eSKate Stone switch (short_option) {
1415223383edSEnrico Granata case 'f':
1416fe11483bSZachary Turner if (!option_arg.empty())
1417adcd0268SBenjamin Kramer m_funct_name = std::string(option_arg);
1418735152e3SEnrico Granata break;
14199fe00e52SEnrico Granata case 'c':
1420fe11483bSZachary Turner if (!option_arg.empty())
1421adcd0268SBenjamin Kramer m_class_name = std::string(option_arg);
14229fe00e52SEnrico Granata break;
1423735152e3SEnrico Granata case 'h':
1424fe11483bSZachary Turner if (!option_arg.empty())
1425adcd0268SBenjamin Kramer m_short_help = std::string(option_arg);
1426223383edSEnrico Granata break;
1427c5011aedSJim Ingham case 'o':
14281f7b58f2SJim Ingham m_overwrite_lazy = eLazyBoolYes;
1429c5011aedSJim Ingham break;
14300a305db7SEnrico Granata case 's':
1431b9c1b51eSKate Stone m_synchronicity =
143247cbf4a0SPavel Labath (ScriptedCommandSynchronicity)OptionArgParser::ToOptionEnum(
1433fe11483bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
14340a305db7SEnrico Granata if (!error.Success())
1435b9c1b51eSKate Stone error.SetErrorStringWithFormat(
1436fe11483bSZachary Turner "unrecognized value for synchronicity '%s'",
1437fe11483bSZachary Turner option_arg.str().c_str());
14380a305db7SEnrico Granata break;
1439223383edSEnrico Granata default:
144036162014SRaphael Isemann llvm_unreachable("Unimplemented option");
1441223383edSEnrico Granata }
1442223383edSEnrico Granata
1443223383edSEnrico Granata return error;
1444223383edSEnrico Granata }
1445223383edSEnrico Granata
OptionParsingStarting(ExecutionContext * execution_context)1446b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override {
14479fe00e52SEnrico Granata m_class_name.clear();
1448735152e3SEnrico Granata m_funct_name.clear();
1449735152e3SEnrico Granata m_short_help.clear();
14501f7b58f2SJim Ingham m_overwrite_lazy = eLazyBoolCalculate;
145144d93782SGreg Clayton m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1452223383edSEnrico Granata }
1453223383edSEnrico Granata
GetDefinitions()14541f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
145570602439SZachary Turner return llvm::makeArrayRef(g_script_add_options);
14561f0f5b5bSZachary Turner }
1457223383edSEnrico Granata
1458223383edSEnrico Granata // Instance variables to hold the values for command options.
1459223383edSEnrico Granata
14609fe00e52SEnrico Granata std::string m_class_name;
1461223383edSEnrico Granata std::string m_funct_name;
1462735152e3SEnrico Granata std::string m_short_help;
14632a84a861SMartin Storsjö LazyBool m_overwrite_lazy = eLazyBoolCalculate;
14649494c510SJonas Devlieghere ScriptedCommandSynchronicity m_synchronicity =
14659494c510SJonas Devlieghere eScriptedCommandSynchronicitySynchronous;
1466223383edSEnrico Granata };
1467223383edSEnrico Granata
IOHandlerActivated(IOHandler & io_handler,bool interactive)14680affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
14697ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
14700affb582SDave Lee if (output_sp && interactive) {
147144d93782SGreg Clayton output_sp->PutCString(g_python_command_instructions);
147244d93782SGreg Clayton output_sp->Flush();
1473223383edSEnrico Granata }
1474223383edSEnrico Granata }
1475223383edSEnrico Granata
IOHandlerInputComplete(IOHandler & io_handler,std::string & data)1476b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler,
1477b9c1b51eSKate Stone std::string &data) override {
14787ca15ba7SLawrence D'Anna StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
147944d93782SGreg Clayton
14802b29b432SJonas Devlieghere ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
1481b9c1b51eSKate Stone if (interpreter) {
148244d93782SGreg Clayton StringList lines;
148344d93782SGreg Clayton lines.SplitIntoLines(data);
1484b9c1b51eSKate Stone if (lines.GetSize() > 0) {
1485a73b7df7SEnrico Granata std::string funct_name_str;
1486b9c1b51eSKate Stone if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1487b9c1b51eSKate Stone if (funct_name_str.empty()) {
1488b9c1b51eSKate Stone error_sp->Printf("error: unable to obtain a function name, didn't "
1489b9c1b51eSKate Stone "add python command.\n");
149044d93782SGreg Clayton error_sp->Flush();
1491b9c1b51eSKate Stone } else {
1492223383edSEnrico Granata // everything should be fine now, let's add this alias
1493223383edSEnrico Granata
1494b9c1b51eSKate Stone CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
1495771ef6d4SMalcolm Parsons m_interpreter, m_cmd_name, funct_name_str, m_short_help,
149644d93782SGreg Clayton m_synchronicity));
1497c5011aedSJim Ingham if (!m_container) {
1498c5011aedSJim Ingham Status error = m_interpreter.AddUserCommand(
1499c5011aedSJim Ingham m_cmd_name, command_obj_sp, m_overwrite);
1500c5011aedSJim Ingham if (error.Fail()) {
1501c5011aedSJim Ingham error_sp->Printf("error: unable to add selected command: '%s'",
1502c5011aedSJim Ingham error.AsCString());
150344d93782SGreg Clayton error_sp->Flush();
1504223383edSEnrico Granata }
1505c5011aedSJim Ingham } else {
1506c5011aedSJim Ingham llvm::Error llvm_error = m_container->LoadUserSubcommand(
1507c5011aedSJim Ingham m_cmd_name, command_obj_sp, m_overwrite);
1508c5011aedSJim Ingham if (llvm_error) {
1509c5011aedSJim Ingham error_sp->Printf("error: unable to add selected command: '%s'",
1510c5011aedSJim Ingham llvm::toString(std::move(llvm_error)).c_str());
1511c5011aedSJim Ingham error_sp->Flush();
1512c5011aedSJim Ingham }
1513c5011aedSJim Ingham }
1514223383edSEnrico Granata }
1515b9c1b51eSKate Stone } else {
1516b9c1b51eSKate Stone error_sp->Printf(
1517c5011aedSJim Ingham "error: unable to create function, didn't add python command\n");
151844d93782SGreg Clayton error_sp->Flush();
151944d93782SGreg Clayton }
1520b9c1b51eSKate Stone } else {
1521c5011aedSJim Ingham error_sp->Printf("error: empty function, didn't add python command\n");
152244d93782SGreg Clayton error_sp->Flush();
152344d93782SGreg Clayton }
1524b9c1b51eSKate Stone } else {
1525b9c1b51eSKate Stone error_sp->Printf(
1526c5011aedSJim Ingham "error: script interpreter missing, didn't add python command\n");
152744d93782SGreg Clayton error_sp->Flush();
152844d93782SGreg Clayton }
152944d93782SGreg Clayton
153044d93782SGreg Clayton io_handler.SetIsDone(true);
153144d93782SGreg Clayton }
1532223383edSEnrico Granata
DoExecute(Args & command,CommandReturnObject & result)1533b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
153457179860SJonas Devlieghere if (GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) {
1535b9c1b51eSKate Stone result.AppendError("only scripting language supported for scripted "
1536b9c1b51eSKate Stone "commands is currently Python");
153799f0b8f9SEnrico Granata return false;
153899f0b8f9SEnrico Granata }
153999f0b8f9SEnrico Granata
1540c5011aedSJim Ingham if (command.GetArgumentCount() == 0) {
1541c5011aedSJim Ingham result.AppendError("'command script add' requires at least one argument");
1542c5011aedSJim Ingham return false;
1543c5011aedSJim Ingham }
15441f7b58f2SJim Ingham // Store the options in case we get multi-line input, also figure out the
15451f7b58f2SJim Ingham // default if not user supplied:
15461f7b58f2SJim Ingham switch (m_options.m_overwrite_lazy) {
15471f7b58f2SJim Ingham case eLazyBoolCalculate:
15481f7b58f2SJim Ingham m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite();
15491f7b58f2SJim Ingham break;
15501f7b58f2SJim Ingham case eLazyBoolYes:
15511f7b58f2SJim Ingham m_overwrite = true;
15521f7b58f2SJim Ingham break;
15531f7b58f2SJim Ingham case eLazyBoolNo:
15541f7b58f2SJim Ingham m_overwrite = false;
15551f7b58f2SJim Ingham }
15561f7b58f2SJim Ingham
1557c5011aedSJim Ingham Status path_error;
1558c5011aedSJim Ingham m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
1559c5011aedSJim Ingham command, true, path_error);
1560c5011aedSJim Ingham
1561c5011aedSJim Ingham if (path_error.Fail()) {
1562c5011aedSJim Ingham result.AppendErrorWithFormat("error in command path: %s",
1563c5011aedSJim Ingham path_error.AsCString());
1564223383edSEnrico Granata return false;
1565223383edSEnrico Granata }
1566223383edSEnrico Granata
1567c5011aedSJim Ingham if (!m_container) {
1568c5011aedSJim Ingham // This is getting inserted into the root of the interpreter.
1569adcd0268SBenjamin Kramer m_cmd_name = std::string(command[0].ref());
1570c5011aedSJim Ingham } else {
1571c5011aedSJim Ingham size_t num_args = command.GetArgumentCount();
1572c5011aedSJim Ingham m_cmd_name = std::string(command[num_args - 1].ref());
1573c5011aedSJim Ingham }
1574c5011aedSJim Ingham
1575735152e3SEnrico Granata m_short_help.assign(m_options.m_short_help);
157644d93782SGreg Clayton m_synchronicity = m_options.m_synchronicity;
1577223383edSEnrico Granata
1578c5011aedSJim Ingham // Handle the case where we prompt for the script code first:
1579c5011aedSJim Ingham if (m_options.m_class_name.empty() && m_options.m_funct_name.empty()) {
1580c5011aedSJim Ingham m_interpreter.GetPythonCommandsFromIOHandler(" ", // Prompt
1581a6faf851SJonas Devlieghere *this); // IOHandlerDelegate
1582c5011aedSJim Ingham return result.Succeeded();
1583c5011aedSJim Ingham }
1584c5011aedSJim Ingham
1585c5011aedSJim Ingham CommandObjectSP new_cmd_sp;
1586c5011aedSJim Ingham if (m_options.m_class_name.empty()) {
1587c5011aedSJim Ingham new_cmd_sp.reset(new CommandObjectPythonFunction(
1588b9c1b51eSKate Stone m_interpreter, m_cmd_name, m_options.m_funct_name,
1589b9c1b51eSKate Stone m_options.m_short_help, m_synchronicity));
1590b9c1b51eSKate Stone } else {
15912b29b432SJonas Devlieghere ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
1592b9c1b51eSKate Stone if (!interpreter) {
15939fe00e52SEnrico Granata result.AppendError("cannot find ScriptInterpreter");
15949fe00e52SEnrico Granata return false;
15959fe00e52SEnrico Granata }
15969fe00e52SEnrico Granata
1597b9c1b51eSKate Stone auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1598b9c1b51eSKate Stone m_options.m_class_name.c_str());
1599b9c1b51eSKate Stone if (!cmd_obj_sp) {
16009fe00e52SEnrico Granata result.AppendError("cannot create helper object");
16019fe00e52SEnrico Granata return false;
16029fe00e52SEnrico Granata }
16039fe00e52SEnrico Granata
1604c5011aedSJim Ingham new_cmd_sp.reset(new CommandObjectScriptingObject(
1605b9c1b51eSKate Stone m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
16069fe00e52SEnrico Granata }
1607223383edSEnrico Granata
1608c5011aedSJim Ingham // Assume we're going to succeed...
1609c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
1610c5011aedSJim Ingham if (!m_container) {
1611c5011aedSJim Ingham Status add_error =
1612c5011aedSJim Ingham m_interpreter.AddUserCommand(m_cmd_name, new_cmd_sp, m_overwrite);
1613c5011aedSJim Ingham if (add_error.Fail())
1614c5011aedSJim Ingham result.AppendErrorWithFormat("cannot add command: %s",
1615c5011aedSJim Ingham add_error.AsCString());
1616c5011aedSJim Ingham } else {
1617c5011aedSJim Ingham llvm::Error llvm_error =
1618c5011aedSJim Ingham m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
1619c5011aedSJim Ingham if (llvm_error)
1620c5011aedSJim Ingham result.AppendErrorWithFormat("cannot add command: %s",
1621c5011aedSJim Ingham llvm::toString(std::move(llvm_error)).c_str());
1622c5011aedSJim Ingham }
1623223383edSEnrico Granata return result.Succeeded();
1624223383edSEnrico Granata }
16255a988416SJim Ingham
16265a988416SJim Ingham CommandOptions m_options;
162744d93782SGreg Clayton std::string m_cmd_name;
1628c5011aedSJim Ingham CommandObjectMultiword *m_container = nullptr;
1629735152e3SEnrico Granata std::string m_short_help;
16302a84a861SMartin Storsjö bool m_overwrite = false;
1631e4a03b26SJonas Devlieghere ScriptedCommandSynchronicity m_synchronicity =
1632e4a03b26SJonas Devlieghere eScriptedCommandSynchronicitySynchronous;
1633223383edSEnrico Granata };
1634223383edSEnrico Granata
1635223383edSEnrico Granata // CommandObjectCommandsScriptList
1636223383edSEnrico Granata
1637b9c1b51eSKate Stone class CommandObjectCommandsScriptList : public CommandObjectParsed {
1638223383edSEnrico Granata public:
CommandObjectCommandsScriptList(CommandInterpreter & interpreter)1639b9c1b51eSKate Stone CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1640b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script list",
1641c5011aedSJim Ingham "List defined top-level scripted commands.",
1642c5011aedSJim Ingham nullptr) {}
1643223383edSEnrico Granata
16446e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptList() override = default;
1645223383edSEnrico Granata
DoExecute(Args & command,CommandReturnObject & result)1646b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
1647b9c1b51eSKate Stone m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1648223383edSEnrico Granata
1649223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult);
1650223383edSEnrico Granata
1651223383edSEnrico Granata return true;
1652223383edSEnrico Granata }
1653223383edSEnrico Granata };
1654223383edSEnrico Granata
1655223383edSEnrico Granata // CommandObjectCommandsScriptClear
1656223383edSEnrico Granata
1657b9c1b51eSKate Stone class CommandObjectCommandsScriptClear : public CommandObjectParsed {
1658223383edSEnrico Granata public:
CommandObjectCommandsScriptClear(CommandInterpreter & interpreter)1659b9c1b51eSKate Stone CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1660b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script clear",
1661b9c1b51eSKate Stone "Delete all scripted commands.", nullptr) {}
1662223383edSEnrico Granata
16636e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptClear() override = default;
1664223383edSEnrico Granata
16655a988416SJim Ingham protected:
DoExecute(Args & command,CommandReturnObject & result)1666b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
1667223383edSEnrico Granata m_interpreter.RemoveAllUser();
1668223383edSEnrico Granata
1669223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult);
1670223383edSEnrico Granata
1671223383edSEnrico Granata return true;
1672223383edSEnrico Granata }
1673223383edSEnrico Granata };
1674223383edSEnrico Granata
1675223383edSEnrico Granata // CommandObjectCommandsScriptDelete
1676223383edSEnrico Granata
1677b9c1b51eSKate Stone class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
1678223383edSEnrico Granata public:
CommandObjectCommandsScriptDelete(CommandInterpreter & interpreter)1679b9c1b51eSKate Stone CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1680c5011aedSJim Ingham : CommandObjectParsed(
1681c5011aedSJim Ingham interpreter, "command script delete",
1682c5011aedSJim Ingham "Delete a scripted command by specifying the path to the command.",
1683c5011aedSJim Ingham nullptr) {
1684223383edSEnrico Granata CommandArgumentEntry arg1;
1685223383edSEnrico Granata CommandArgumentData cmd_arg;
1686223383edSEnrico Granata
1687c5011aedSJim Ingham // This is a list of command names forming the path to the command
1688c5011aedSJim Ingham // to be deleted.
1689c5011aedSJim Ingham cmd_arg.arg_type = eArgTypeCommand;
1690c5011aedSJim Ingham cmd_arg.arg_repetition = eArgRepeatPlus;
1691223383edSEnrico Granata
1692b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the
1693b9c1b51eSKate Stone // argument entry.
1694223383edSEnrico Granata arg1.push_back(cmd_arg);
1695223383edSEnrico Granata
1696223383edSEnrico Granata // Push the data for the first argument into the m_arguments vector.
1697223383edSEnrico Granata m_arguments.push_back(arg1);
1698223383edSEnrico Granata }
1699223383edSEnrico Granata
17006e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptDelete() override = default;
1701223383edSEnrico Granata
17022e8f304fSGongyu Deng void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)17032e8f304fSGongyu Deng HandleArgumentCompletion(CompletionRequest &request,
17042e8f304fSGongyu Deng OptionElementVector &opt_element_vector) override {
1705c5011aedSJim Ingham CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request,
1706c5011aedSJim Ingham opt_element_vector);
17072e8f304fSGongyu Deng }
17082e8f304fSGongyu Deng
17095a988416SJim Ingham protected:
DoExecute(Args & command,CommandReturnObject & result)1710b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override {
1711223383edSEnrico Granata
1712c5011aedSJim Ingham llvm::StringRef root_cmd = command[0].ref();
1713c5011aedSJim Ingham size_t num_args = command.GetArgumentCount();
1714c5011aedSJim Ingham
1715c5011aedSJim Ingham if (root_cmd.empty()) {
1716c5011aedSJim Ingham result.AppendErrorWithFormat("empty root command name");
1717c5011aedSJim Ingham return false;
1718c5011aedSJim Ingham }
1719c5011aedSJim Ingham if (!m_interpreter.HasUserCommands() &&
1720c5011aedSJim Ingham !m_interpreter.HasUserMultiwordCommands()) {
1721c5011aedSJim Ingham result.AppendErrorWithFormat("can only delete user defined commands, "
1722c5011aedSJim Ingham "but no user defined commands found");
1723223383edSEnrico Granata return false;
1724223383edSEnrico Granata }
1725223383edSEnrico Granata
1726c5011aedSJim Ingham CommandObjectSP cmd_sp = m_interpreter.GetCommandSPExact(root_cmd);
1727c5011aedSJim Ingham if (!cmd_sp) {
1728c5011aedSJim Ingham result.AppendErrorWithFormat("command '%s' not found.",
1729c5011aedSJim Ingham command[0].c_str());
1730c5011aedSJim Ingham return false;
1731c5011aedSJim Ingham }
1732c5011aedSJim Ingham if (!cmd_sp->IsUserCommand()) {
1733c5011aedSJim Ingham result.AppendErrorWithFormat("command '%s' is not a user command.",
1734c5011aedSJim Ingham command[0].c_str());
1735c5011aedSJim Ingham return false;
1736c5011aedSJim Ingham }
1737c5011aedSJim Ingham if (cmd_sp->GetAsMultiwordCommand() && num_args == 1) {
1738c5011aedSJim Ingham result.AppendErrorWithFormat("command '%s' is a multi-word command.\n "
1739c5011aedSJim Ingham "Delete with \"command container delete\"",
1740c5011aedSJim Ingham command[0].c_str());
17414574a890SZachary Turner return false;
1742223383edSEnrico Granata }
1743223383edSEnrico Granata
1744c5011aedSJim Ingham if (command.GetArgumentCount() == 1) {
1745c5011aedSJim Ingham m_interpreter.RemoveUser(root_cmd);
1746c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishResult);
1747c5011aedSJim Ingham return true;
1748c5011aedSJim Ingham }
1749c5011aedSJim Ingham // We're deleting a command from a multiword command. Verify the command
1750c5011aedSJim Ingham // path:
1751c5011aedSJim Ingham Status error;
1752c5011aedSJim Ingham CommandObjectMultiword *container =
1753c5011aedSJim Ingham GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
1754c5011aedSJim Ingham error);
1755c5011aedSJim Ingham if (error.Fail()) {
1756c5011aedSJim Ingham result.AppendErrorWithFormat("could not resolve command path: %s",
1757c5011aedSJim Ingham error.AsCString());
1758c5011aedSJim Ingham return false;
1759c5011aedSJim Ingham }
1760c5011aedSJim Ingham if (!container) {
1761c5011aedSJim Ingham // This means that command only had a leaf command, so the container is
1762c5011aedSJim Ingham // the root. That should have been handled above.
1763c5011aedSJim Ingham result.AppendErrorWithFormat("could not find a container for '%s'",
1764c5011aedSJim Ingham command[0].c_str());
1765c5011aedSJim Ingham return false;
1766c5011aedSJim Ingham }
1767c5011aedSJim Ingham const char *leaf_cmd = command[num_args - 1].c_str();
1768c5011aedSJim Ingham llvm::Error llvm_error = container->RemoveUserSubcommand(leaf_cmd,
1769c5011aedSJim Ingham /* multiword not okay */ false);
1770c5011aedSJim Ingham if (llvm_error) {
1771c5011aedSJim Ingham result.AppendErrorWithFormat("could not delete command '%s': %s",
1772c5011aedSJim Ingham leaf_cmd,
1773c5011aedSJim Ingham llvm::toString(std::move(llvm_error)).c_str());
1774c5011aedSJim Ingham return false;
1775c5011aedSJim Ingham }
1776c5011aedSJim Ingham
1777c5011aedSJim Ingham Stream &out_stream = result.GetOutputStream();
1778c5011aedSJim Ingham
1779c5011aedSJim Ingham out_stream << "Deleted command:";
1780c5011aedSJim Ingham for (size_t idx = 0; idx < num_args; idx++) {
1781c5011aedSJim Ingham out_stream << ' ';
1782c5011aedSJim Ingham out_stream << command[idx].c_str();
1783c5011aedSJim Ingham }
1784c5011aedSJim Ingham out_stream << '\n';
17854574a890SZachary Turner result.SetStatus(eReturnStatusSuccessFinishResult);
17864574a890SZachary Turner return true;
1787223383edSEnrico Granata }
1788223383edSEnrico Granata };
1789223383edSEnrico Granata
1790223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript
1791223383edSEnrico Granata
1792223383edSEnrico Granata // CommandObjectMultiwordCommandsScript
1793223383edSEnrico Granata
1794b9c1b51eSKate Stone class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
1795223383edSEnrico Granata public:
CommandObjectMultiwordCommandsScript(CommandInterpreter & interpreter)17967428a18cSKate Stone CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1797b9c1b51eSKate Stone : CommandObjectMultiword(
1798a925974bSAdrian Prantl interpreter, "command script",
1799a925974bSAdrian Prantl "Commands for managing custom "
1800b9c1b51eSKate Stone "commands implemented by "
1801b9c1b51eSKate Stone "interpreter scripts.",
1802b9c1b51eSKate Stone "command script <subcommand> [<subcommand-options>]") {
1803b9c1b51eSKate Stone LoadSubCommand("add", CommandObjectSP(
1804b9c1b51eSKate Stone new CommandObjectCommandsScriptAdd(interpreter)));
1805b9c1b51eSKate Stone LoadSubCommand(
1806b9c1b51eSKate Stone "delete",
1807b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1808b9c1b51eSKate Stone LoadSubCommand(
1809b9c1b51eSKate Stone "clear",
1810b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1811b9c1b51eSKate Stone LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1812b9c1b51eSKate Stone interpreter)));
1813b9c1b51eSKate Stone LoadSubCommand(
1814b9c1b51eSKate Stone "import",
1815b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1816223383edSEnrico Granata }
1817223383edSEnrico Granata
18186e3d8e7fSEugene Zelenko ~CommandObjectMultiwordCommandsScript() override = default;
1819223383edSEnrico Granata };
1820223383edSEnrico Granata
1821c5011aedSJim Ingham #pragma mark CommandObjectCommandContainer
1822c5011aedSJim Ingham #define LLDB_OPTIONS_container_add
1823c5011aedSJim Ingham #include "CommandOptions.inc"
1824c5011aedSJim Ingham
1825c5011aedSJim Ingham class CommandObjectCommandsContainerAdd : public CommandObjectParsed {
1826c5011aedSJim Ingham public:
CommandObjectCommandsContainerAdd(CommandInterpreter & interpreter)1827c5011aedSJim Ingham CommandObjectCommandsContainerAdd(CommandInterpreter &interpreter)
1828c5011aedSJim Ingham : CommandObjectParsed(
1829c5011aedSJim Ingham interpreter, "command container add",
1830c5011aedSJim Ingham "Add a container command to lldb. Adding to built-"
1831c5011aedSJim Ingham "in container commands is not allowed.",
1832c5011aedSJim Ingham "command container add [[path1]...] container-name") {
1833c5011aedSJim Ingham CommandArgumentEntry arg1;
1834c5011aedSJim Ingham CommandArgumentData cmd_arg;
1835c5011aedSJim Ingham
1836c5011aedSJim Ingham // This is one or more command names, which form the path to the command
1837c5011aedSJim Ingham // you want to add.
1838c5011aedSJim Ingham cmd_arg.arg_type = eArgTypeCommand;
1839c5011aedSJim Ingham cmd_arg.arg_repetition = eArgRepeatPlus;
1840c5011aedSJim Ingham
1841c5011aedSJim Ingham // There is only one variant this argument could be; put it into the
1842c5011aedSJim Ingham // argument entry.
1843c5011aedSJim Ingham arg1.push_back(cmd_arg);
1844c5011aedSJim Ingham
1845c5011aedSJim Ingham // Push the data for the first argument into the m_arguments vector.
1846c5011aedSJim Ingham m_arguments.push_back(arg1);
1847c5011aedSJim Ingham }
1848c5011aedSJim Ingham
1849c5011aedSJim Ingham ~CommandObjectCommandsContainerAdd() override = default;
1850c5011aedSJim Ingham
GetOptions()1851c5011aedSJim Ingham Options *GetOptions() override { return &m_options; }
1852c5011aedSJim Ingham
1853c5011aedSJim Ingham void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)1854c5011aedSJim Ingham HandleArgumentCompletion(CompletionRequest &request,
1855c5011aedSJim Ingham OptionElementVector &opt_element_vector) override {
1856c5011aedSJim Ingham CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request,
1857c5011aedSJim Ingham opt_element_vector);
1858c5011aedSJim Ingham }
1859c5011aedSJim Ingham
1860c5011aedSJim Ingham protected:
1861c5011aedSJim Ingham class CommandOptions : public Options {
1862c5011aedSJim Ingham public:
186324f9a2f5SShafik Yaghmour CommandOptions() = default;
1864c5011aedSJim Ingham
1865c5011aedSJim Ingham ~CommandOptions() override = default;
1866c5011aedSJim Ingham
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)1867c5011aedSJim Ingham Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1868c5011aedSJim Ingham ExecutionContext *execution_context) override {
1869c5011aedSJim Ingham Status error;
1870c5011aedSJim Ingham const int short_option = m_getopt_table[option_idx].val;
1871c5011aedSJim Ingham
1872c5011aedSJim Ingham switch (short_option) {
1873c5011aedSJim Ingham case 'h':
1874c5011aedSJim Ingham if (!option_arg.empty())
1875c5011aedSJim Ingham m_short_help = std::string(option_arg);
1876c5011aedSJim Ingham break;
1877c5011aedSJim Ingham case 'o':
1878c5011aedSJim Ingham m_overwrite = true;
1879c5011aedSJim Ingham break;
1880c5011aedSJim Ingham case 'H':
1881c5011aedSJim Ingham if (!option_arg.empty())
1882c5011aedSJim Ingham m_long_help = std::string(option_arg);
1883c5011aedSJim Ingham break;
1884c5011aedSJim Ingham default:
1885c5011aedSJim Ingham llvm_unreachable("Unimplemented option");
1886c5011aedSJim Ingham }
1887c5011aedSJim Ingham
1888c5011aedSJim Ingham return error;
1889c5011aedSJim Ingham }
1890c5011aedSJim Ingham
OptionParsingStarting(ExecutionContext * execution_context)1891c5011aedSJim Ingham void OptionParsingStarting(ExecutionContext *execution_context) override {
1892c5011aedSJim Ingham m_short_help.clear();
1893c5011aedSJim Ingham m_long_help.clear();
1894c5011aedSJim Ingham m_overwrite = false;
1895c5011aedSJim Ingham }
1896c5011aedSJim Ingham
GetDefinitions()1897c5011aedSJim Ingham llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1898c5011aedSJim Ingham return llvm::makeArrayRef(g_container_add_options);
1899c5011aedSJim Ingham }
1900c5011aedSJim Ingham
1901c5011aedSJim Ingham // Instance variables to hold the values for command options.
1902c5011aedSJim Ingham
1903c5011aedSJim Ingham std::string m_short_help;
1904c5011aedSJim Ingham std::string m_long_help;
1905c5011aedSJim Ingham bool m_overwrite = false;
1906c5011aedSJim Ingham };
DoExecute(Args & command,CommandReturnObject & result)1907c5011aedSJim Ingham bool DoExecute(Args &command, CommandReturnObject &result) override {
1908c5011aedSJim Ingham size_t num_args = command.GetArgumentCount();
1909c5011aedSJim Ingham
1910c5011aedSJim Ingham if (num_args == 0) {
1911c5011aedSJim Ingham result.AppendError("no command was specified");
1912c5011aedSJim Ingham return false;
1913c5011aedSJim Ingham }
1914c5011aedSJim Ingham
1915c5011aedSJim Ingham if (num_args == 1) {
1916c5011aedSJim Ingham // We're adding this as a root command, so use the interpreter.
1917c5011aedSJim Ingham const char *cmd_name = command.GetArgumentAtIndex(0);
1918c5011aedSJim Ingham auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
1919c5011aedSJim Ingham GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
1920c5011aedSJim Ingham m_options.m_long_help.c_str()));
1921c5011aedSJim Ingham cmd_sp->GetAsMultiwordCommand()->SetRemovable(true);
1922c5011aedSJim Ingham Status add_error = GetCommandInterpreter().AddUserCommand(
1923c5011aedSJim Ingham cmd_name, cmd_sp, m_options.m_overwrite);
1924c5011aedSJim Ingham if (add_error.Fail()) {
1925c5011aedSJim Ingham result.AppendErrorWithFormat("error adding command: %s",
1926c5011aedSJim Ingham add_error.AsCString());
1927c5011aedSJim Ingham return false;
1928c5011aedSJim Ingham }
1929c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
1930c5011aedSJim Ingham return true;
1931c5011aedSJim Ingham }
1932c5011aedSJim Ingham
1933c5011aedSJim Ingham // We're adding this to a subcommand, first find the subcommand:
1934c5011aedSJim Ingham Status path_error;
1935c5011aedSJim Ingham CommandObjectMultiword *add_to_me =
1936c5011aedSJim Ingham GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
1937c5011aedSJim Ingham path_error);
1938c5011aedSJim Ingham
1939c5011aedSJim Ingham if (!add_to_me) {
1940c5011aedSJim Ingham result.AppendErrorWithFormat("error adding command: %s",
1941c5011aedSJim Ingham path_error.AsCString());
1942c5011aedSJim Ingham return false;
1943c5011aedSJim Ingham }
1944c5011aedSJim Ingham
1945c5011aedSJim Ingham const char *cmd_name = command.GetArgumentAtIndex(num_args - 1);
1946c5011aedSJim Ingham auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
1947c5011aedSJim Ingham GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
1948c5011aedSJim Ingham m_options.m_long_help.c_str()));
1949c5011aedSJim Ingham llvm::Error llvm_error =
1950c5011aedSJim Ingham add_to_me->LoadUserSubcommand(cmd_name, cmd_sp, m_options.m_overwrite);
1951c5011aedSJim Ingham if (llvm_error) {
1952c5011aedSJim Ingham result.AppendErrorWithFormat("error adding subcommand: %s",
1953c5011aedSJim Ingham llvm::toString(std::move(llvm_error)).c_str());
1954c5011aedSJim Ingham return false;
1955c5011aedSJim Ingham }
1956c5011aedSJim Ingham
1957c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
1958c5011aedSJim Ingham return true;
1959c5011aedSJim Ingham }
1960c5011aedSJim Ingham
1961c5011aedSJim Ingham private:
1962c5011aedSJim Ingham CommandOptions m_options;
1963c5011aedSJim Ingham };
1964c5011aedSJim Ingham
1965c5011aedSJim Ingham #define LLDB_OPTIONS_multiword_delete
1966c5011aedSJim Ingham #include "CommandOptions.inc"
1967c5011aedSJim Ingham class CommandObjectCommandsContainerDelete : public CommandObjectParsed {
1968c5011aedSJim Ingham public:
CommandObjectCommandsContainerDelete(CommandInterpreter & interpreter)1969c5011aedSJim Ingham CommandObjectCommandsContainerDelete(CommandInterpreter &interpreter)
1970c5011aedSJim Ingham : CommandObjectParsed(
1971c5011aedSJim Ingham interpreter, "command container delete",
1972c5011aedSJim Ingham "Delete a container command previously added to "
1973c5011aedSJim Ingham "lldb.",
1974c5011aedSJim Ingham "command container delete [[path1] ...] container-cmd") {
1975c5011aedSJim Ingham CommandArgumentEntry arg1;
1976c5011aedSJim Ingham CommandArgumentData cmd_arg;
1977c5011aedSJim Ingham
1978c5011aedSJim Ingham // This is one or more command names, which form the path to the command
1979c5011aedSJim Ingham // you want to add.
1980c5011aedSJim Ingham cmd_arg.arg_type = eArgTypeCommand;
1981c5011aedSJim Ingham cmd_arg.arg_repetition = eArgRepeatPlus;
1982c5011aedSJim Ingham
1983c5011aedSJim Ingham // There is only one variant this argument could be; put it into the
1984c5011aedSJim Ingham // argument entry.
1985c5011aedSJim Ingham arg1.push_back(cmd_arg);
1986c5011aedSJim Ingham
1987c5011aedSJim Ingham // Push the data for the first argument into the m_arguments vector.
1988c5011aedSJim Ingham m_arguments.push_back(arg1);
1989c5011aedSJim Ingham }
1990c5011aedSJim Ingham
1991c5011aedSJim Ingham ~CommandObjectCommandsContainerDelete() override = default;
1992c5011aedSJim Ingham
1993c5011aedSJim Ingham void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)1994c5011aedSJim Ingham HandleArgumentCompletion(CompletionRequest &request,
1995c5011aedSJim Ingham OptionElementVector &opt_element_vector) override {
1996c5011aedSJim Ingham CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request,
1997c5011aedSJim Ingham opt_element_vector);
1998c5011aedSJim Ingham }
1999c5011aedSJim Ingham
2000c5011aedSJim Ingham protected:
DoExecute(Args & command,CommandReturnObject & result)2001c5011aedSJim Ingham bool DoExecute(Args &command, CommandReturnObject &result) override {
2002c5011aedSJim Ingham size_t num_args = command.GetArgumentCount();
2003c5011aedSJim Ingham
2004c5011aedSJim Ingham if (num_args == 0) {
2005c5011aedSJim Ingham result.AppendError("No command was specified.");
2006c5011aedSJim Ingham return false;
2007c5011aedSJim Ingham }
2008c5011aedSJim Ingham
2009c5011aedSJim Ingham if (num_args == 1) {
2010c5011aedSJim Ingham // We're removing a root command, so we need to delete it from the
2011c5011aedSJim Ingham // interpreter.
2012c5011aedSJim Ingham const char *cmd_name = command.GetArgumentAtIndex(0);
2013c5011aedSJim Ingham // Let's do a little more work here so we can do better error reporting.
2014c5011aedSJim Ingham CommandInterpreter &interp = GetCommandInterpreter();
2015c5011aedSJim Ingham CommandObjectSP cmd_sp = interp.GetCommandSPExact(cmd_name);
2016c5011aedSJim Ingham if (!cmd_sp) {
2017c5011aedSJim Ingham result.AppendErrorWithFormat("container command %s doesn't exist.",
2018c5011aedSJim Ingham cmd_name);
2019c5011aedSJim Ingham return false;
2020c5011aedSJim Ingham }
2021c5011aedSJim Ingham if (!cmd_sp->IsUserCommand()) {
2022c5011aedSJim Ingham result.AppendErrorWithFormat(
2023c5011aedSJim Ingham "container command %s is not a user command", cmd_name);
2024c5011aedSJim Ingham return false;
2025c5011aedSJim Ingham }
2026c5011aedSJim Ingham if (!cmd_sp->GetAsMultiwordCommand()) {
2027c5011aedSJim Ingham result.AppendErrorWithFormat("command %s is not a container command",
2028c5011aedSJim Ingham cmd_name);
2029c5011aedSJim Ingham return false;
2030c5011aedSJim Ingham }
2031c5011aedSJim Ingham
2032c5011aedSJim Ingham bool did_remove = GetCommandInterpreter().RemoveUserMultiword(cmd_name);
2033c5011aedSJim Ingham if (!did_remove) {
2034c5011aedSJim Ingham result.AppendErrorWithFormat("error removing command %s.", cmd_name);
2035c5011aedSJim Ingham return false;
2036c5011aedSJim Ingham }
2037c5011aedSJim Ingham
2038c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
2039c5011aedSJim Ingham return true;
2040c5011aedSJim Ingham }
2041c5011aedSJim Ingham
2042c5011aedSJim Ingham // We're removing a subcommand, first find the subcommand's owner:
2043c5011aedSJim Ingham Status path_error;
2044c5011aedSJim Ingham CommandObjectMultiword *container =
2045c5011aedSJim Ingham GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
2046c5011aedSJim Ingham path_error);
2047c5011aedSJim Ingham
2048c5011aedSJim Ingham if (!container) {
2049c5011aedSJim Ingham result.AppendErrorWithFormat("error removing container command: %s",
2050c5011aedSJim Ingham path_error.AsCString());
2051c5011aedSJim Ingham return false;
2052c5011aedSJim Ingham }
2053c5011aedSJim Ingham const char *leaf = command.GetArgumentAtIndex(num_args - 1);
2054c5011aedSJim Ingham llvm::Error llvm_error =
2055c5011aedSJim Ingham container->RemoveUserSubcommand(leaf, /* multiword okay */ true);
2056c5011aedSJim Ingham if (llvm_error) {
2057c5011aedSJim Ingham result.AppendErrorWithFormat("error removing container command: %s",
2058c5011aedSJim Ingham llvm::toString(std::move(llvm_error)).c_str());
2059c5011aedSJim Ingham return false;
2060c5011aedSJim Ingham }
2061c5011aedSJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult);
2062c5011aedSJim Ingham return true;
2063c5011aedSJim Ingham }
2064c5011aedSJim Ingham };
2065c5011aedSJim Ingham
2066c5011aedSJim Ingham class CommandObjectCommandContainer : public CommandObjectMultiword {
2067c5011aedSJim Ingham public:
CommandObjectCommandContainer(CommandInterpreter & interpreter)2068c5011aedSJim Ingham CommandObjectCommandContainer(CommandInterpreter &interpreter)
2069c5011aedSJim Ingham : CommandObjectMultiword(
2070c5011aedSJim Ingham interpreter, "command container",
2071c5011aedSJim Ingham "Commands for adding container commands to lldb. "
2072c5011aedSJim Ingham "Container commands are containers for other commands. You can "
20738ba14214SLuboš Luňák "add nested container commands by specifying a command path, "
2074c5011aedSJim Ingham "but you can't add commands into the built-in command hierarchy.",
2075c5011aedSJim Ingham "command container <subcommand> [<subcommand-options>]") {
2076c5011aedSJim Ingham LoadSubCommand("add", CommandObjectSP(new CommandObjectCommandsContainerAdd(
2077c5011aedSJim Ingham interpreter)));
2078c5011aedSJim Ingham LoadSubCommand(
2079c5011aedSJim Ingham "delete",
2080c5011aedSJim Ingham CommandObjectSP(new CommandObjectCommandsContainerDelete(interpreter)));
2081c5011aedSJim Ingham }
2082c5011aedSJim Ingham
2083c5011aedSJim Ingham ~CommandObjectCommandContainer() override = default;
2084c5011aedSJim Ingham };
2085c5011aedSJim Ingham
2086ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands
2087ebc09c36SJim Ingham
2088ebc09c36SJim Ingham // CommandObjectMultiwordCommands
2089ebc09c36SJim Ingham
CommandObjectMultiwordCommands(CommandInterpreter & interpreter)2090b9c1b51eSKate Stone CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
2091b9c1b51eSKate Stone CommandInterpreter &interpreter)
2092b9c1b51eSKate Stone : CommandObjectMultiword(interpreter, "command",
2093b9c1b51eSKate Stone "Commands for managing custom LLDB commands.",
2094b9c1b51eSKate Stone "command <subcommand> [<subcommand-options>]") {
2095b9c1b51eSKate Stone LoadSubCommand("source",
2096b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
2097b9c1b51eSKate Stone LoadSubCommand("alias",
2098b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
2099b9c1b51eSKate Stone LoadSubCommand("unalias", CommandObjectSP(
2100b9c1b51eSKate Stone new CommandObjectCommandsUnalias(interpreter)));
2101b9c1b51eSKate Stone LoadSubCommand("delete",
2102b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
2103c5011aedSJim Ingham LoadSubCommand("container", CommandObjectSP(new CommandObjectCommandContainer(
2104c5011aedSJim Ingham interpreter)));
2105b9c1b51eSKate Stone LoadSubCommand(
2106b9c1b51eSKate Stone "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
2107b9c1b51eSKate Stone LoadSubCommand(
2108b9c1b51eSKate Stone "script",
2109b9c1b51eSKate Stone CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
2110ebc09c36SJim Ingham }
2111ebc09c36SJim Ingham
21126e3d8e7fSEugene Zelenko CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;
2113