blob: e39c0330b653139c92a273c3adafd82ad4406df6 [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- CommandObjectCommands.cpp -------------------------------*- C++ -*-===//
Jim Inghamebc09c32010-07-07 03:36:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Inghamebc09c32010-07-07 03:36:20 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
Greg Clayton0e5e5a72011-04-20 22:55:21 +000013#include "llvm/ADT/StringRef.h"
14
Jim Inghamebc09c32010-07-07 03:36:20 +000015// Project includes
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +000016#include "CommandObjectCommands.h"
Enrico Granata46d4aa22016-02-29 23:22:53 +000017#include "CommandObjectHelp.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000018#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000019#include "lldb/Core/IOHandler.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000020#include "lldb/Host/OptionParser.h"
Greg Claytonde164aa2011-04-20 16:37:46 +000021#include "lldb/Interpreter/Args.h"
Enrico Granata7594f142013-06-17 22:51:50 +000022#include "lldb/Interpreter/CommandHistory.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytonde164aa2011-04-20 16:37:46 +000024#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000025#include "lldb/Interpreter/CommandReturnObject.h"
Enrico Granata012d4fc2013-06-11 01:26:35 +000026#include "lldb/Interpreter/OptionValueBoolean.h"
Enrico Granata45d0e232016-03-31 01:10:54 +000027#include "lldb/Interpreter/OptionValueString.h"
Enrico Granata7594f142013-06-17 22:51:50 +000028#include "lldb/Interpreter/OptionValueUInt64.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000029#include "lldb/Interpreter/Options.h"
Enrico Granata99f0b8f2011-08-17 01:30:04 +000030#include "lldb/Interpreter/ScriptInterpreter.h"
Zachary Turner573ab902017-03-21 18:25:04 +000031#include "lldb/Utility/StringList.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000032
33using namespace lldb;
34using namespace lldb_private;
35
Jim Inghamebc09c32010-07-07 03:36:20 +000036//-------------------------------------------------------------------------
37// CommandObjectCommandsSource
38//-------------------------------------------------------------------------
39
Zachary Turner1f0f5b52016-09-22 20:22:55 +000040static OptionDefinition g_history_options[] = {
41 // clang-format off
42 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." },
43 { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to start printing history commands (or end to mean tail mode)." },
44 { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." },
45 { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clears the current command history." },
46 // clang-format on
47};
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049class CommandObjectCommandsHistory : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +000050public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 CommandObjectCommandsHistory(CommandInterpreter &interpreter)
52 : CommandObjectParsed(interpreter, "command history",
Jim Inghamaab5be02017-04-19 23:21:04 +000053 "Dump the history of commands in this session.\n"
54 "Commands in the history list can be run again "
55 "using \"!<INDEX>\". \"!-<OFFSET>\" will re-run "
56 "the command that is <OFFSET> commands from the end"
57 " of the list (counting the current command).",
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +000058 nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 m_options() {}
Jim Ingham5a988412012-06-08 21:56:10 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 ~CommandObjectCommandsHistory() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +000064
65protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 class CommandOptions : public Options {
67 public:
68 CommandOptions()
69 : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {
Jim Inghama5a97eb2011-07-12 03:12:18 +000070 }
Jim Ingham5a988412012-06-08 21:56:10 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 ~CommandOptions() override = default;
73
Zachary Turnerfe114832016-11-12 16:56:47 +000074 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 ExecutionContext *execution_context) override {
76 Error error;
77 const int short_option = m_getopt_table[option_idx].val;
78
79 switch (short_option) {
80 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +000081 error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 break;
83 case 's':
Zachary Turnerfe114832016-11-12 16:56:47 +000084 if (option_arg == "end") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 m_start_idx.SetCurrentValue(UINT64_MAX);
86 m_start_idx.SetOptionWasSet();
87 } else
Zachary Turnerfe114832016-11-12 16:56:47 +000088 error = m_start_idx.SetValueFromString(option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 eVarSetOperationAssign);
90 break;
91 case 'e':
Zachary Turnerfe114832016-11-12 16:56:47 +000092 error =
93 m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 break;
95 case 'C':
96 m_clear.SetCurrentValue(true);
97 m_clear.SetOptionWasSet();
98 break;
99 default:
100 error.SetErrorStringWithFormat("unrecognized option '%c'",
101 short_option);
102 break;
103 }
104
105 return error;
106 }
107
108 void OptionParsingStarting(ExecutionContext *execution_context) override {
109 m_start_idx.Clear();
110 m_stop_idx.Clear();
111 m_count.Clear();
112 m_clear.Clear();
113 }
114
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000115 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000116 return llvm::makeArrayRef(g_history_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118
119 // Instance variables to hold the values for command options.
120
121 OptionValueUInt64 m_start_idx;
122 OptionValueUInt64 m_stop_idx;
123 OptionValueUInt64 m_count;
124 OptionValueBoolean m_clear;
125 };
126
127 bool DoExecute(Args &command, CommandReturnObject &result) override {
128 if (m_options.m_clear.GetCurrentValue() &&
129 m_options.m_clear.OptionWasSet()) {
130 m_interpreter.GetCommandHistory().Clear();
131 result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
132 } else {
133 if (m_options.m_start_idx.OptionWasSet() &&
134 m_options.m_stop_idx.OptionWasSet() &&
135 m_options.m_count.OptionWasSet()) {
136 result.AppendError("--count, --start-index and --end-index cannot be "
137 "all specified in the same invocation");
138 result.SetStatus(lldb::eReturnStatusFailed);
139 } else {
140 std::pair<bool, uint64_t> start_idx(
141 m_options.m_start_idx.OptionWasSet(),
142 m_options.m_start_idx.GetCurrentValue());
143 std::pair<bool, uint64_t> stop_idx(
144 m_options.m_stop_idx.OptionWasSet(),
145 m_options.m_stop_idx.GetCurrentValue());
146 std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
147 m_options.m_count.GetCurrentValue());
148
149 const CommandHistory &history(m_interpreter.GetCommandHistory());
150
151 if (start_idx.first && start_idx.second == UINT64_MAX) {
152 if (count.first) {
153 start_idx.second = history.GetSize() - count.second;
154 stop_idx.second = history.GetSize() - 1;
155 } else if (stop_idx.first) {
156 start_idx.second = stop_idx.second;
157 stop_idx.second = history.GetSize() - 1;
158 } else {
159 start_idx.second = 0;
160 stop_idx.second = history.GetSize() - 1;
161 }
162 } else {
163 if (!start_idx.first && !stop_idx.first && !count.first) {
164 start_idx.second = 0;
165 stop_idx.second = history.GetSize() - 1;
166 } else if (start_idx.first) {
167 if (count.first) {
168 stop_idx.second = start_idx.second + count.second - 1;
169 } else if (!stop_idx.first) {
170 stop_idx.second = history.GetSize() - 1;
171 }
172 } else if (stop_idx.first) {
173 if (count.first) {
174 if (stop_idx.second >= count.second)
175 start_idx.second = stop_idx.second - count.second + 1;
176 else
177 start_idx.second = 0;
178 }
179 } else /* if (count.first) */
180 {
181 start_idx.second = 0;
182 stop_idx.second = count.second - 1;
183 }
184 }
185 history.Dump(result.GetOutputStream(), start_idx.second,
186 stop_idx.second);
187 }
188 }
189 return result.Succeeded();
190 }
191
192 CommandOptions m_options;
Jim Inghama5a97eb2011-07-12 03:12:18 +0000193};
194
Jim Inghama5a97eb2011-07-12 03:12:18 +0000195//-------------------------------------------------------------------------
196// CommandObjectCommandsSource
197//-------------------------------------------------------------------------
198
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000199static OptionDefinition g_source_options[] = {
200 // clang-format off
201 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." },
202 { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." },
203 { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." },
204 // clang-format on
205};
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207class CommandObjectCommandsSource : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +0000208public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 CommandObjectCommandsSource(CommandInterpreter &interpreter)
210 : CommandObjectParsed(
211 interpreter, "command source",
212 "Read and execute LLDB commands from the file <filename>.",
213 nullptr),
214 m_options() {
215 CommandArgumentEntry arg;
216 CommandArgumentData file_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 // Define the first (and only) variant of this arg.
219 file_arg.arg_type = eArgTypeFilename;
220 file_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 // There is only one variant this argument could be; put it into the
223 // argument entry.
224 arg.push_back(file_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 // Push the data for the first argument into the m_arguments vector.
227 m_arguments.push_back(arg);
228 }
Jim Ingham5a988412012-06-08 21:56:10 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 ~CommandObjectCommandsSource() override = default;
231
232 const char *GetRepeatCommand(Args &current_command_args,
233 uint32_t index) override {
234 return "";
235 }
236
237 int HandleArgumentCompletion(Args &input, int &cursor_index,
238 int &cursor_char_position,
239 OptionElementVector &opt_element_vector,
240 int match_start_point, int max_return_elements,
241 bool &word_complete,
242 StringList &matches) override {
Zachary Turner4574a892016-12-08 01:31:04 +0000243 auto completion_str = input[cursor_index].ref;
244 completion_str = completion_str.take_front(cursor_char_position);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245
246 CommandCompletions::InvokeCommonCompletionCallbacks(
247 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
Zachary Turner4574a892016-12-08 01:31:04 +0000248 completion_str, match_start_point, max_return_elements, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 word_complete, matches);
250 return matches.GetSize();
251 }
252
253 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +0000254
255protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 class CommandOptions : public Options {
257 public:
258 CommandOptions()
259 : Options(), m_stop_on_error(true), m_silent_run(false),
260 m_stop_on_continue(true) {}
Jim Inghame16c50a2011-02-18 00:54:25 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 ~CommandOptions() override = default;
Jim Inghame16c50a2011-02-18 00:54:25 +0000263
Zachary Turnerfe114832016-11-12 16:56:47 +0000264 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 ExecutionContext *execution_context) override {
266 Error error;
267 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton340b0302014-02-05 17:57:57 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 switch (short_option) {
270 case 'e':
Zachary Turnerfe114832016-11-12 16:56:47 +0000271 error = m_stop_on_error.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 break;
Greg Clayton340b0302014-02-05 17:57:57 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +0000275 error = m_stop_on_continue.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 break;
Greg Clayton340b0302014-02-05 17:57:57 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 case 's':
Zachary Turnerfe114832016-11-12 16:56:47 +0000279 error = m_silent_run.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 break;
Jim Inghame16c50a2011-02-18 00:54:25 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 default:
283 error.SetErrorStringWithFormat("unrecognized option '%c'",
284 short_option);
285 break;
286 }
Jim Inghame16c50a2011-02-18 00:54:25 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 return error;
Jim Inghamebc09c32010-07-07 03:36:20 +0000289 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 void OptionParsingStarting(ExecutionContext *execution_context) override {
292 m_stop_on_error.Clear();
293 m_silent_run.Clear();
294 m_stop_on_continue.Clear();
295 }
296
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000297 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000298 return llvm::makeArrayRef(g_source_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000299 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300
301 // Instance variables to hold the values for command options.
302
303 OptionValueBoolean m_stop_on_error;
304 OptionValueBoolean m_silent_run;
305 OptionValueBoolean m_stop_on_continue;
306 };
307
308 bool DoExecute(Args &command, CommandReturnObject &result) override {
Zachary Turner4574a892016-12-08 01:31:04 +0000309 if (command.GetArgumentCount() != 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 result.AppendErrorWithFormat(
311 "'%s' takes exactly one executable filename argument.\n",
Zachary Turnera4496982016-10-05 21:14:38 +0000312 GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 result.SetStatus(eReturnStatusFailed);
Zachary Turner4574a892016-12-08 01:31:04 +0000314 return false;
315 }
316
317 FileSpec cmd_file(command[0].ref, true);
318 ExecutionContext *exe_ctx = nullptr; // Just use the default context.
319
320 // If any options were set, then use them
321 if (m_options.m_stop_on_error.OptionWasSet() ||
322 m_options.m_silent_run.OptionWasSet() ||
323 m_options.m_stop_on_continue.OptionWasSet()) {
324 // Use user set settings
325 CommandInterpreterRunOptions options;
326 options.SetStopOnContinue(m_options.m_stop_on_continue.GetCurrentValue());
327 options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
328 options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue());
329 options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue());
330
331 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
332 } else {
333 // No options were set, inherit any settings from nested "command
334 // source" commands,
335 // or set to sane default settings...
336 CommandInterpreterRunOptions options;
337 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 }
339 return result.Succeeded();
340 }
341
342 CommandOptions m_options;
Jim Inghamebc09c32010-07-07 03:36:20 +0000343};
344
345#pragma mark CommandObjectCommandsAlias
346//-------------------------------------------------------------------------
347// CommandObjectCommandsAlias
348//-------------------------------------------------------------------------
349
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000350static OptionDefinition g_alias_options[] = {
351 // clang-format off
352 { LLDB_OPT_SET_ALL, false, "help", 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" },
353 { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" },
354 // clang-format on
355};
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357static const char *g_python_command_instructions =
358 "Enter your Python command(s). Type 'DONE' to end.\n"
359 "You must define a Python function with this signature:\n"
360 "def my_command_impl(debugger, args, result, internal_dict):\n";
Enrico Granatabe93a352011-08-16 16:49:25 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362class CommandObjectCommandsAlias : public CommandObjectRaw {
Enrico Granata45d0e232016-03-31 01:10:54 +0000363protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 class CommandOptions : public OptionGroup {
365 public:
366 CommandOptions() : OptionGroup(), m_help(), m_long_help() {}
Enrico Granata45d0e232016-03-31 01:10:54 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 ~CommandOptions() override = default;
369
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000370 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000371 return llvm::makeArrayRef(g_alias_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000372 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373
Zachary Turner8cef4b02016-09-23 17:48:13 +0000374 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 ExecutionContext *execution_context) override {
376 Error error;
377
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000378 const int short_option = GetDefinitions()[option_idx].short_option;
Zachary Turner8cef4b02016-09-23 17:48:13 +0000379 std::string option_str(option_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380
381 switch (short_option) {
382 case 'h':
Zachary Turner8cef4b02016-09-23 17:48:13 +0000383 m_help.SetCurrentValue(option_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 m_help.SetOptionWasSet();
385 break;
386
387 case 'H':
Zachary Turner8cef4b02016-09-23 17:48:13 +0000388 m_long_help.SetCurrentValue(option_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 m_long_help.SetOptionWasSet();
390 break;
391
392 default:
393 error.SetErrorStringWithFormat("invalid short option character '%c'",
394 short_option);
395 break;
396 }
397
398 return error;
Enrico Granata45d0e232016-03-31 01:10:54 +0000399 }
400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 void OptionParsingStarting(ExecutionContext *execution_context) override {
402 m_help.Clear();
403 m_long_help.Clear();
404 }
Enrico Granata45d0e232016-03-31 01:10:54 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 OptionValueString m_help;
407 OptionValueString m_long_help;
408 };
409
410 OptionGroupOptions m_option_group;
411 CommandOptions m_command_options;
412
413public:
414 Options *GetOptions() override { return &m_option_group; }
415
416 CommandObjectCommandsAlias(CommandInterpreter &interpreter)
417 : CommandObjectRaw(
418 interpreter, "command alias",
Zachary Turnera4496982016-10-05 21:14:38 +0000419 "Define a custom command in terms of an existing command."),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 m_option_group(), m_command_options() {
421 m_option_group.Append(&m_command_options);
422 m_option_group.Finalize();
423
424 SetHelpLong(
425 "'alias' allows the user to create a short-cut or abbreviation for long \
Kate Stoneea671fb2015-07-14 05:48:36 +0000426commands, multi-word commands, and commands that take particular options. \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427Below are some simple examples of how one might use the 'alias' command:"
428 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000429
430(lldb) command alias sc script
431
432 Creates the abbreviation 'sc' for the 'script' command.
433
434(lldb) command alias bp breakpoint
435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436)"
437 " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
Kate Stoneea671fb2015-07-14 05:48:36 +0000438breakpoint commands are two-word commands, the user would still need to \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
440 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000441
442(lldb) command alias bpl breakpoint list
443
444 Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446)"
447 "An alias can include some options for the command, with the values either \
Kate Stoneea671fb2015-07-14 05:48:36 +0000448filled in at the time the alias is created, or specified as positional \
449arguments, to be filled in when the alias is invoked. The following example \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450shows how to create aliases with options:"
451 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000452
453(lldb) command alias bfl breakpoint set -f %1 -l %2
454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455)"
456 " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
Kate Stoneea671fb2015-07-14 05:48:36 +0000457options already part of the alias. So if the user wants to set a breakpoint \
458by file and line without explicitly having to use the -f and -l options, the \
459user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
460for the actual arguments that will be passed when the alias command is used. \
461The number in the placeholder refers to the position/order the actual value \
462occupies when the alias is used. All the occurrences of '%1' in the alias \
463will be replaced with the first argument, all the occurrences of '%2' in the \
464alias will be replaced with the second argument, and so on. This also allows \
465actual arguments to be used multiple times within an alias (see 'process \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466launch' example below)."
467 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469)"
470 "Note: the positional arguments must substitute as whole words in the resultant \
Kate Stoneea671fb2015-07-14 05:48:36 +0000471command, so you can't at present do something like this to append the file extension \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472\".cpp\":"
473 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000474
475(lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477)"
478 "For more complex aliasing, use the \"command regex\" command instead. In the \
Kate Stoneea671fb2015-07-14 05:48:36 +0000479'bfl' case above, the actual file value will be filled in with the first argument \
480following 'bfl' and the actual line number value will be filled in with the second \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481argument. The user would use this alias as follows:"
482 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000483
484(lldb) command alias bfl breakpoint set -f %1 -l %2
485(lldb) bfl my-file.c 137
486
487This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
488
489Another example:
490
491(lldb) command alias pltty process launch -s -o %1 -e %1
492(lldb) pltty /dev/tty0
493
494 Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496)"
497 "If the user always wanted to pass the same value to a particular option, the \
Kate Stoneea671fb2015-07-14 05:48:36 +0000498alias could be defined with that value directly in the alias as a constant, \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499rather than using a positional placeholder:"
500 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000501
502(lldb) command alias bl3 breakpoint set -f %1 -l 3
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 Always sets a breakpoint on line 3 of whatever file is indicated.)");
Jim Inghamebc09c32010-07-07 03:36:20 +0000505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 CommandArgumentEntry arg1;
507 CommandArgumentEntry arg2;
508 CommandArgumentEntry arg3;
509 CommandArgumentData alias_arg;
510 CommandArgumentData cmd_arg;
511 CommandArgumentData options_arg;
Jim Inghamebc09c32010-07-07 03:36:20 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 // Define the first (and only) variant of this arg.
514 alias_arg.arg_type = eArgTypeAliasName;
515 alias_arg.arg_repetition = eArgRepeatPlain;
516
517 // There is only one variant this argument could be; put it into the
518 // argument entry.
519 arg1.push_back(alias_arg);
520
521 // Define the first (and only) variant of this arg.
522 cmd_arg.arg_type = eArgTypeCommandName;
523 cmd_arg.arg_repetition = eArgRepeatPlain;
524
525 // There is only one variant this argument could be; put it into the
526 // argument entry.
527 arg2.push_back(cmd_arg);
528
529 // Define the first (and only) variant of this arg.
530 options_arg.arg_type = eArgTypeAliasOptions;
531 options_arg.arg_repetition = eArgRepeatOptional;
532
533 // There is only one variant this argument could be; put it into the
534 // argument entry.
535 arg3.push_back(options_arg);
536
537 // Push the data for the first argument into the m_arguments vector.
538 m_arguments.push_back(arg1);
539 m_arguments.push_back(arg2);
540 m_arguments.push_back(arg3);
541 }
542
543 ~CommandObjectCommandsAlias() override = default;
Jim Inghamebc09c32010-07-07 03:36:20 +0000544
Jim Ingham5a988412012-06-08 21:56:10 +0000545protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 bool DoExecute(const char *raw_command_line,
547 CommandReturnObject &result) override {
548 if (!raw_command_line || !raw_command_line[0]) {
549 result.AppendError("'command alias' requires at least two arguments");
550 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000551 }
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
554 m_option_group.NotifyOptionParsingStarting(&exe_ctx);
Caroline Ticeca90c472011-05-06 21:37:15 +0000555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 const char *remainder = nullptr;
Enrico Granata212130a2016-03-08 05:37:15 +0000557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 if (raw_command_line[0] == '-') {
559 // We have some options and these options MUST end with --.
560 const char *end_options = nullptr;
561 const char *s = raw_command_line;
562 while (s && s[0]) {
563 end_options = ::strstr(s, "--");
564 if (end_options) {
565 end_options += 2; // Get past the "--"
566 if (::isspace(end_options[0])) {
567 remainder = end_options;
568 while (::isspace(*remainder))
569 ++remainder;
570 break;
571 }
572 }
573 s = end_options;
574 }
575
576 if (end_options) {
577 Args args(
578 llvm::StringRef(raw_command_line, end_options - raw_command_line));
579 if (!ParseOptions(args, result))
580 return false;
581
582 Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx));
583 if (error.Fail()) {
584 result.AppendError(error.AsCString());
585 result.SetStatus(eReturnStatusFailed);
586 return false;
587 }
588 }
Caroline Tice844d2302010-12-09 22:52:49 +0000589 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 if (nullptr == remainder)
591 remainder = raw_command_line;
Jim Inghamebc09c32010-07-07 03:36:20 +0000592
Zachary Turnera01bccd2016-10-05 21:14:56 +0000593 llvm::StringRef raw_command_string(remainder);
594 Args args(raw_command_string);
Jim Inghamebc09c32010-07-07 03:36:20 +0000595
Zachary Turner11eb9c62016-10-05 20:03:37 +0000596 if (args.GetArgumentCount() < 2) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 result.AppendError("'command alias' requires at least two arguments");
598 result.SetStatus(eReturnStatusFailed);
599 return false;
Jim Inghamebc09c32010-07-07 03:36:20 +0000600 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601
602 // Get the alias command.
603
Zachary Turner4574a892016-12-08 01:31:04 +0000604 auto alias_command = args[0].ref;
605 if (alias_command.startswith("-")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 result.AppendError("aliases starting with a dash are not supported");
607 if (alias_command == "--help" || alias_command == "--long-help") {
608 result.AppendWarning("if trying to pass options to 'command alias' add "
609 "a -- at the end of the options");
610 }
611 result.SetStatus(eReturnStatusFailed);
612 return false;
613 }
614
615 // Strip the new alias name off 'raw_command_string' (leave it on args,
616 // which gets passed to 'Execute', which
617 // does the stripping itself.
618 size_t pos = raw_command_string.find(alias_command);
619 if (pos == 0) {
620 raw_command_string = raw_command_string.substr(alias_command.size());
621 pos = raw_command_string.find_first_not_of(' ');
622 if ((pos != std::string::npos) && (pos > 0))
623 raw_command_string = raw_command_string.substr(pos);
624 } else {
625 result.AppendError("Error parsing command string. No alias created.");
626 result.SetStatus(eReturnStatusFailed);
627 return false;
628 }
629
630 // Verify that the command is alias-able.
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000631 if (m_interpreter.CommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 result.AppendErrorWithFormat(
633 "'%s' is a permanent debugger command and cannot be redefined.\n",
Zachary Turner4574a892016-12-08 01:31:04 +0000634 args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 result.SetStatus(eReturnStatusFailed);
636 return false;
637 }
638
639 // Get CommandObject that is being aliased. The command name is read from
Zachary Turnera01bccd2016-10-05 21:14:56 +0000640 // the front of raw_command_string. raw_command_string is returned with the
641 // name of the command object stripped off the front.
642 llvm::StringRef original_raw_command_string = raw_command_string;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 CommandObject *cmd_obj =
644 m_interpreter.GetCommandObjectForCommand(raw_command_string);
645
646 if (!cmd_obj) {
647 result.AppendErrorWithFormat("invalid command given to 'command alias'. "
648 "'%s' does not begin with a valid command."
649 " No alias created.",
Zachary Turnera01bccd2016-10-05 21:14:56 +0000650 original_raw_command_string.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 result.SetStatus(eReturnStatusFailed);
652 return false;
653 } else if (!cmd_obj->WantsRawCommandString()) {
654 // Note that args was initialized with the original command, and has not
655 // been updated to this point.
656 // Therefore can we pass it to the version of Execute that does not
657 // need/expect raw input in the alias.
658 return HandleAliasingNormalCommand(args, result);
659 } else {
660 return HandleAliasingRawCommand(alias_command, raw_command_string,
661 *cmd_obj, result);
662 }
663 return result.Succeeded();
664 }
665
Zachary Turnera01bccd2016-10-05 21:14:56 +0000666 bool HandleAliasingRawCommand(llvm::StringRef alias_command,
667 llvm::StringRef raw_command_string,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 CommandObject &cmd_obj,
669 CommandReturnObject &result) {
670 // Verify & handle any options/arguments passed to the alias command
671
672 OptionArgVectorSP option_arg_vector_sp =
673 OptionArgVectorSP(new OptionArgVector);
674
675 if (CommandObjectSP cmd_obj_sp =
676 m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) {
Zachary Turnera01bccd2016-10-05 21:14:56 +0000677 if (m_interpreter.AliasExists(alias_command) ||
678 m_interpreter.UserCommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 result.AppendWarningWithFormat(
680 "Overwriting existing definition for '%s'.\n",
Zachary Turnera01bccd2016-10-05 21:14:56 +0000681 alias_command.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 }
683 if (CommandAlias *alias = m_interpreter.AddAlias(
Zachary Turnera01bccd2016-10-05 21:14:56 +0000684 alias_command, cmd_obj_sp, raw_command_string)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 if (m_command_options.m_help.OptionWasSet())
686 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
687 if (m_command_options.m_long_help.OptionWasSet())
688 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
689 result.SetStatus(eReturnStatusSuccessFinishNoResult);
690 } else {
691 result.AppendError("Unable to create requested alias.\n");
692 result.SetStatus(eReturnStatusFailed);
693 }
694
695 } else {
696 result.AppendError("Unable to create requested alias.\n");
697 result.SetStatus(eReturnStatusFailed);
698 }
699
700 return result.Succeeded();
701 }
702
703 bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
704 size_t argc = args.GetArgumentCount();
705
706 if (argc < 2) {
707 result.AppendError("'command alias' requires at least two arguments");
708 result.SetStatus(eReturnStatusFailed);
709 return false;
710 }
711
Zachary Turner4574a892016-12-08 01:31:04 +0000712 // Save these in std::strings since we're going to shift them off.
713 const std::string alias_command(args[0].ref);
714 const std::string actual_command(args[1].ref);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715
716 args.Shift(); // Shift the alias command word off the argument vector.
717 args.Shift(); // Shift the old command word off the argument vector.
718
719 // Verify that the command is alias'able, and get the appropriate command
720 // object.
721
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000722 if (m_interpreter.CommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 result.AppendErrorWithFormat(
724 "'%s' is a permanent debugger command and cannot be redefined.\n",
725 alias_command.c_str());
726 result.SetStatus(eReturnStatusFailed);
Zachary Turner4574a892016-12-08 01:31:04 +0000727 return false;
728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729
Zachary Turner4574a892016-12-08 01:31:04 +0000730 CommandObjectSP command_obj_sp(
731 m_interpreter.GetCommandSPExact(actual_command, true));
732 CommandObjectSP subcommand_obj_sp;
733 bool use_subcommand = false;
734 if (!command_obj_sp) {
735 result.AppendErrorWithFormat("'%s' is not an existing command.\n",
736 actual_command.c_str());
737 result.SetStatus(eReturnStatusFailed);
738 return false;
739 }
740 CommandObject *cmd_obj = command_obj_sp.get();
741 CommandObject *sub_cmd_obj = nullptr;
742 OptionArgVectorSP option_arg_vector_sp =
743 OptionArgVectorSP(new OptionArgVector);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744
Zachary Turner4574a892016-12-08 01:31:04 +0000745 while (cmd_obj->IsMultiwordObject() && !args.empty()) {
746 auto sub_command = args[0].ref;
747 assert(!sub_command.empty());
748 subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
749 if (!subcommand_obj_sp) {
750 result.AppendErrorWithFormat(
751 "'%s' is not a valid sub-command of '%s'. "
752 "Unable to create alias.\n",
753 args[0].c_str(), actual_command.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 result.SetStatus(eReturnStatusFailed);
755 return false;
756 }
Zachary Turner4574a892016-12-08 01:31:04 +0000757
758 sub_cmd_obj = subcommand_obj_sp.get();
759 use_subcommand = true;
760 args.Shift(); // Shift the sub_command word off the argument vector.
761 cmd_obj = sub_cmd_obj;
762 }
763
764 // Verify & handle any options/arguments passed to the alias command
765
766 std::string args_string;
767
768 if (!args.empty()) {
769 CommandObjectSP tmp_sp =
770 m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false);
771 if (use_subcommand)
772 tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName(),
773 false);
774
775 args.GetCommandString(args_string);
776 }
777
778 if (m_interpreter.AliasExists(alias_command) ||
779 m_interpreter.UserCommandExists(alias_command)) {
780 result.AppendWarningWithFormat(
781 "Overwriting existing definition for '%s'.\n", alias_command.c_str());
782 }
783
784 if (CommandAlias *alias = m_interpreter.AddAlias(
785 alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
786 args_string)) {
787 if (m_command_options.m_help.OptionWasSet())
788 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
789 if (m_command_options.m_long_help.OptionWasSet())
790 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
791 result.SetStatus(eReturnStatusSuccessFinishNoResult);
792 } else {
793 result.AppendError("Unable to create requested alias.\n");
794 result.SetStatus(eReturnStatusFailed);
795 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 }
797
798 return result.Succeeded();
799 }
Jim Inghamebc09c32010-07-07 03:36:20 +0000800};
801
802#pragma mark CommandObjectCommandsUnalias
803//-------------------------------------------------------------------------
804// CommandObjectCommandsUnalias
805//-------------------------------------------------------------------------
806
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807class CommandObjectCommandsUnalias : public CommandObjectParsed {
Jim Inghamebc09c32010-07-07 03:36:20 +0000808public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
810 : CommandObjectParsed(
811 interpreter, "command unalias",
812 "Delete one or more custom commands defined by 'command alias'.",
813 nullptr) {
814 CommandArgumentEntry arg;
815 CommandArgumentData alias_arg;
Jim Inghamebc09c32010-07-07 03:36:20 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 // Define the first (and only) variant of this arg.
818 alias_arg.arg_type = eArgTypeAliasName;
819 alias_arg.arg_repetition = eArgRepeatPlain;
820
821 // There is only one variant this argument could be; put it into the
822 // argument entry.
823 arg.push_back(alias_arg);
824
825 // Push the data for the first argument into the m_arguments vector.
826 m_arguments.push_back(arg);
827 }
828
829 ~CommandObjectCommandsUnalias() override = default;
Jim Inghamebc09c32010-07-07 03:36:20 +0000830
Jim Ingham5a988412012-06-08 21:56:10 +0000831protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 bool DoExecute(Args &args, CommandReturnObject &result) override {
833 CommandObject::CommandMap::iterator pos;
834 CommandObject *cmd_obj;
Jim Inghamebc09c32010-07-07 03:36:20 +0000835
Zachary Turner11eb9c62016-10-05 20:03:37 +0000836 if (args.empty()) {
837 result.AppendError("must call 'unalias' with a valid alias");
838 result.SetStatus(eReturnStatusFailed);
839 return false;
840 }
841
Zachary Turner4574a892016-12-08 01:31:04 +0000842 auto command_name = args[0].ref;
Zachary Turner11eb9c62016-10-05 20:03:37 +0000843 cmd_obj = m_interpreter.GetCommandObject(command_name);
Zachary Turner4574a892016-12-08 01:31:04 +0000844 if (!cmd_obj) {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000845 result.AppendErrorWithFormat(
846 "'%s' is not a known command.\nTry 'help' to see a "
847 "current list of commands.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +0000848 args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 result.SetStatus(eReturnStatusFailed);
Zachary Turner4574a892016-12-08 01:31:04 +0000850 return false;
Jim Inghamebc09c32010-07-07 03:36:20 +0000851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852
Zachary Turner4574a892016-12-08 01:31:04 +0000853 if (m_interpreter.CommandExists(command_name)) {
854 if (cmd_obj->IsRemovable()) {
855 result.AppendErrorWithFormat(
856 "'%s' is not an alias, it is a debugger command which can be "
857 "removed using the 'command delete' command.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +0000858 args[0].c_str());
Zachary Turner4574a892016-12-08 01:31:04 +0000859 } else {
860 result.AppendErrorWithFormat(
861 "'%s' is a permanent debugger command and cannot be removed.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +0000862 args[0].c_str());
Zachary Turner4574a892016-12-08 01:31:04 +0000863 }
864 result.SetStatus(eReturnStatusFailed);
865 return false;
866 }
867
868 if (!m_interpreter.RemoveAlias(command_name)) {
869 if (m_interpreter.AliasExists(command_name))
870 result.AppendErrorWithFormat(
Zachary Turner867e7d12016-12-09 01:20:58 +0000871 "Error occurred while attempting to unalias '%s'.\n",
872 args[0].c_str());
Zachary Turner4574a892016-12-08 01:31:04 +0000873 else
874 result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +0000875 args[0].c_str());
Zachary Turner4574a892016-12-08 01:31:04 +0000876 result.SetStatus(eReturnStatusFailed);
877 return false;
878 }
879
880 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 return result.Succeeded();
882 }
Jim Inghamebc09c32010-07-07 03:36:20 +0000883};
884
Greg Claytonb5472782015-01-09 19:08:20 +0000885#pragma mark CommandObjectCommandsDelete
886//-------------------------------------------------------------------------
887// CommandObjectCommandsDelete
888//-------------------------------------------------------------------------
889
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890class CommandObjectCommandsDelete : public CommandObjectParsed {
Greg Claytonb5472782015-01-09 19:08:20 +0000891public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 CommandObjectCommandsDelete(CommandInterpreter &interpreter)
893 : CommandObjectParsed(
894 interpreter, "command delete",
895 "Delete one or more custom commands defined by 'command regex'.",
896 nullptr) {
897 CommandArgumentEntry arg;
898 CommandArgumentData alias_arg;
Greg Claytonb5472782015-01-09 19:08:20 +0000899
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 // Define the first (and only) variant of this arg.
901 alias_arg.arg_type = eArgTypeCommandName;
902 alias_arg.arg_repetition = eArgRepeatPlain;
Greg Claytonb5472782015-01-09 19:08:20 +0000903
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 // There is only one variant this argument could be; put it into the
905 // argument entry.
906 arg.push_back(alias_arg);
Greg Claytonb5472782015-01-09 19:08:20 +0000907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 // Push the data for the first argument into the m_arguments vector.
909 m_arguments.push_back(arg);
910 }
Greg Claytonb5472782015-01-09 19:08:20 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 ~CommandObjectCommandsDelete() override = default;
Greg Claytonb5472782015-01-09 19:08:20 +0000913
914protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 bool DoExecute(Args &args, CommandReturnObject &result) override {
916 CommandObject::CommandMap::iterator pos;
Greg Claytonb5472782015-01-09 19:08:20 +0000917
Zachary Turner11eb9c62016-10-05 20:03:37 +0000918 if (args.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 result.AppendErrorWithFormat("must call '%s' with one or more valid user "
920 "defined regular expression command names",
Zachary Turnera4496982016-10-05 21:14:38 +0000921 GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 result.SetStatus(eReturnStatusFailed);
Greg Claytonb5472782015-01-09 19:08:20 +0000923 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924
Zachary Turner4574a892016-12-08 01:31:04 +0000925 auto command_name = args[0].ref;
926 if (!m_interpreter.CommandExists(command_name)) {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000927 StreamString error_msg_stream;
928 const bool generate_apropos = true;
929 const bool generate_type_lookup = false;
930 CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
Zachary Turner4574a892016-12-08 01:31:04 +0000931 &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
932 generate_apropos, generate_type_lookup);
Zachary Turnerc1564272016-11-16 21:15:24 +0000933 result.AppendError(error_msg_stream.GetString());
Zachary Turner11eb9c62016-10-05 20:03:37 +0000934 result.SetStatus(eReturnStatusFailed);
Zachary Turner4574a892016-12-08 01:31:04 +0000935 return false;
Zachary Turner11eb9c62016-10-05 20:03:37 +0000936 }
937
Zachary Turner4574a892016-12-08 01:31:04 +0000938 if (!m_interpreter.RemoveCommand(command_name)) {
939 result.AppendErrorWithFormat(
940 "'%s' is a permanent debugger command and cannot be removed.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +0000941 args[0].c_str());
Zachary Turner4574a892016-12-08 01:31:04 +0000942 result.SetStatus(eReturnStatusFailed);
943 return false;
944 }
945
946 result.SetStatus(eReturnStatusSuccessFinishNoResult);
947 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 }
Greg Claytonb5472782015-01-09 19:08:20 +0000949};
950
Greg Claytonde164aa2011-04-20 16:37:46 +0000951//-------------------------------------------------------------------------
952// CommandObjectCommandsAddRegex
953//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000954
955static OptionDefinition g_regex_options[] = {
956 // clang-format off
957 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." },
958 { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." },
959 // clang-format on
960};
961
Jim Ingham5a988412012-06-08 21:56:10 +0000962#pragma mark CommandObjectCommandsAddRegex
Greg Claytonde164aa2011-04-20 16:37:46 +0000963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964class CommandObjectCommandsAddRegex : public CommandObjectParsed,
965 public IOHandlerDelegateMultiline {
Greg Claytonde164aa2011-04-20 16:37:46 +0000966public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
968 : CommandObjectParsed(
969 interpreter, "command regex", "Define a custom command in terms of "
970 "existing commands by matching "
971 "regular expressions.",
972 "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
973 IOHandlerDelegateMultiline("",
974 IOHandlerDelegate::Completion::LLDBCommand),
975 m_options() {
976 SetHelpLong(
977 R"(
978)"
979 "This command allows the user to create powerful regular expression commands \
Kate Stoneea671fb2015-07-14 05:48:36 +0000980with substitutions. The regular expressions and substitutions are specified \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981using the regular expression substitution format of:"
982 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000983
984 s/<regex>/<subst>/
985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986)"
987 "<regex> is a regular expression that can use parenthesis to capture regular \
Kate Stoneea671fb2015-07-14 05:48:36 +0000988expression input and substitute the captured matches in the output using %1 \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989for the first match, %2 for the second, and so on."
990 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992)"
993 "The regular expressions can all be specified on the command line if more than \
Kate Stoneea671fb2015-07-14 05:48:36 +0000994one argument is provided. If just the command name is provided on the command \
995line, then the regular expressions and substitutions can be entered on separate \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996lines, followed by an empty line to terminate the command definition."
997 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000998
999EXAMPLES
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001)"
1002 "The following example will define a regular expression command named 'f' that \
Kate Stoneea671fb2015-07-14 05:48:36 +00001003will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004a number follows 'f':"
1005 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
1008 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 ~CommandObjectCommandsAddRegex() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001011
Jim Ingham5a988412012-06-08 21:56:10 +00001012protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 void IOHandlerActivated(IOHandler &io_handler) override {
1014 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1015 if (output_sp) {
1016 output_sp->PutCString("Enter one of more sed substitution commands in "
1017 "the form: 's/<regex>/<subst>/'.\nTerminate the "
1018 "substitution list with an empty line.\n");
1019 output_sp->Flush();
1020 }
1021 }
1022
1023 void IOHandlerInputComplete(IOHandler &io_handler,
1024 std::string &data) override {
1025 io_handler.SetIsDone(true);
1026 if (m_regex_cmd_ap) {
1027 StringList lines;
1028 if (lines.SplitIntoLines(data)) {
1029 const size_t num_lines = lines.GetSize();
1030 bool check_only = false;
1031 for (size_t i = 0; i < num_lines; ++i) {
1032 llvm::StringRef bytes_strref(lines[i]);
1033 Error error = AppendRegexSubstitution(bytes_strref, check_only);
1034 if (error.Fail()) {
1035 if (!m_interpreter.GetDebugger()
1036 .GetCommandInterpreter()
1037 .GetBatchCommandMode()) {
1038 StreamSP out_stream =
1039 m_interpreter.GetDebugger().GetAsyncOutputStream();
1040 out_stream->Printf("error: %s\n", error.AsCString());
1041 }
1042 }
Greg Clayton44d93782014-01-27 23:43:24 +00001043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 }
1045 if (m_regex_cmd_ap->HasRegexEntries()) {
1046 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1047 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1048 }
1049 }
1050 }
1051
1052 bool DoExecute(Args &command, CommandReturnObject &result) override {
1053 const size_t argc = command.GetArgumentCount();
1054 if (argc == 0) {
1055 result.AppendError("usage: 'command regex <command-name> "
1056 "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
1057 result.SetStatus(eReturnStatusFailed);
Zachary Turner11eb9c62016-10-05 20:03:37 +00001058 return false;
1059 }
1060
1061 Error error;
Zachary Turner4574a892016-12-08 01:31:04 +00001062 auto name = command[0].ref;
1063 m_regex_cmd_ap = llvm::make_unique<CommandObjectRegexCommand>(
1064 m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 0,
1065 true);
Zachary Turner11eb9c62016-10-05 20:03:37 +00001066
1067 if (argc == 1) {
1068 Debugger &debugger = m_interpreter.GetDebugger();
1069 bool color_prompt = debugger.GetUseColor();
1070 const bool multiple_lines = true; // Get multiple lines
1071 IOHandlerSP io_handler_sp(new IOHandlerEditline(
1072 debugger, IOHandler::Type::Other,
1073 "lldb-regex", // Name of input reader for history
1074 llvm::StringRef("> "), // Prompt
1075 llvm::StringRef(), // Continuation prompt
1076 multiple_lines, color_prompt,
1077 0, // Don't show line numbers
1078 *this));
1079
1080 if (io_handler_sp) {
1081 debugger.PushIOHandler(io_handler_sp);
1082 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00001085 for (auto &entry : command.entries().drop_front()) {
Zachary Turner11eb9c62016-10-05 20:03:37 +00001086 bool check_only = false;
Zachary Turner97d2c402016-10-05 23:40:23 +00001087 error = AppendRegexSubstitution(entry.ref, check_only);
Zachary Turner11eb9c62016-10-05 20:03:37 +00001088 if (error.Fail())
1089 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 }
Zachary Turner11eb9c62016-10-05 20:03:37 +00001091
1092 if (error.Success()) {
1093 AddRegexCommandToInterpreter();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 }
Greg Clayton44d93782014-01-27 23:43:24 +00001095 }
Zachary Turner11eb9c62016-10-05 20:03:37 +00001096 if (error.Fail()) {
1097 result.AppendError(error.AsCString());
1098 result.SetStatus(eReturnStatusFailed);
1099 }
Greg Clayton44d93782014-01-27 23:43:24 +00001100
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 return result.Succeeded();
1102 }
1103
1104 Error AppendRegexSubstitution(const llvm::StringRef &regex_sed,
1105 bool check_only) {
1106 Error error;
1107
1108 if (!m_regex_cmd_ap) {
1109 error.SetErrorStringWithFormat(
1110 "invalid regular expression command object for: '%.*s'",
1111 (int)regex_sed.size(), regex_sed.data());
1112 return error;
Greg Clayton44d93782014-01-27 23:43:24 +00001113 }
Greg Clayton44d93782014-01-27 23:43:24 +00001114
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 size_t regex_sed_size = regex_sed.size();
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001116
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 if (regex_sed_size <= 1) {
1118 error.SetErrorStringWithFormat(
1119 "regular expression substitution string is too short: '%.*s'",
1120 (int)regex_sed.size(), regex_sed.data());
1121 return error;
Greg Claytonde164aa2011-04-20 16:37:46 +00001122 }
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001123
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 if (regex_sed[0] != 's') {
1125 error.SetErrorStringWithFormat("regular expression substitution string "
1126 "doesn't start with 's': '%.*s'",
1127 (int)regex_sed.size(), regex_sed.data());
1128 return error;
1129 }
1130 const size_t first_separator_char_pos = 1;
1131 // use the char that follows 's' as the regex separator character
1132 // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
1133 const char separator_char = regex_sed[first_separator_char_pos];
1134 const size_t second_separator_char_pos =
1135 regex_sed.find(separator_char, first_separator_char_pos + 1);
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001136
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 if (second_separator_char_pos == std::string::npos) {
1138 error.SetErrorStringWithFormat(
1139 "missing second '%c' separator char after '%.*s' in '%.*s'",
1140 separator_char,
1141 (int)(regex_sed.size() - first_separator_char_pos - 1),
1142 regex_sed.data() + (first_separator_char_pos + 1),
1143 (int)regex_sed.size(), regex_sed.data());
1144 return error;
1145 }
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001146
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 const size_t third_separator_char_pos =
1148 regex_sed.find(separator_char, second_separator_char_pos + 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001149
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 if (third_separator_char_pos == std::string::npos) {
1151 error.SetErrorStringWithFormat(
1152 "missing third '%c' separator char after '%.*s' in '%.*s'",
1153 separator_char,
1154 (int)(regex_sed.size() - second_separator_char_pos - 1),
1155 regex_sed.data() + (second_separator_char_pos + 1),
1156 (int)regex_sed.size(), regex_sed.data());
1157 return error;
1158 }
1159
1160 if (third_separator_char_pos != regex_sed_size - 1) {
1161 // Make sure that everything that follows the last regex
1162 // separator char
1163 if (regex_sed.find_first_not_of("\t\n\v\f\r ",
1164 third_separator_char_pos + 1) !=
1165 std::string::npos) {
1166 error.SetErrorStringWithFormat(
1167 "extra data found after the '%.*s' regular expression substitution "
1168 "string: '%.*s'",
1169 (int)third_separator_char_pos + 1, regex_sed.data(),
1170 (int)(regex_sed.size() - third_separator_char_pos - 1),
1171 regex_sed.data() + (third_separator_char_pos + 1));
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001172 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 }
1174 } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
1175 error.SetErrorStringWithFormat(
1176 "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1177 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1178 regex_sed.data());
1179 return error;
1180 } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
1181 error.SetErrorStringWithFormat(
1182 "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1183 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1184 regex_sed.data());
1185 return error;
Greg Claytonde164aa2011-04-20 16:37:46 +00001186 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187
1188 if (!check_only) {
1189 std::string regex(regex_sed.substr(first_separator_char_pos + 1,
1190 second_separator_char_pos -
1191 first_separator_char_pos - 1));
1192 std::string subst(regex_sed.substr(second_separator_char_pos + 1,
1193 third_separator_char_pos -
1194 second_separator_char_pos - 1));
1195 m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str());
Greg Claytonde164aa2011-04-20 16:37:46 +00001196 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 return error;
1198 }
1199
1200 void AddRegexCommandToInterpreter() {
1201 if (m_regex_cmd_ap) {
1202 if (m_regex_cmd_ap->HasRegexEntries()) {
1203 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1204 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1205 }
1206 }
1207 }
Greg Claytonde164aa2011-04-20 16:37:46 +00001208
Greg Claytonde164aa2011-04-20 16:37:46 +00001209private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
Greg Claytonde164aa2011-04-20 16:37:46 +00001211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 class CommandOptions : public Options {
1213 public:
1214 CommandOptions() : Options() {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001215
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 ~CommandOptions() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001217
Zachary Turnerfe114832016-11-12 16:56:47 +00001218 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 ExecutionContext *execution_context) override {
1220 Error error;
1221 const int short_option = m_getopt_table[option_idx].val;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001222
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 switch (short_option) {
1224 case 'h':
1225 m_help.assign(option_arg);
1226 break;
1227 case 's':
1228 m_syntax.assign(option_arg);
1229 break;
1230 default:
1231 error.SetErrorStringWithFormat("unrecognized option '%c'",
1232 short_option);
1233 break;
1234 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 return error;
1237 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001238
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 void OptionParsingStarting(ExecutionContext *execution_context) override {
1240 m_help.clear();
1241 m_syntax.clear();
1242 }
1243
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001244 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001245 return llvm::makeArrayRef(g_regex_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247
Zachary Turner11eb9c62016-10-05 20:03:37 +00001248 // TODO: Convert these functions to return StringRefs.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 const char *GetHelp() {
1250 return (m_help.empty() ? nullptr : m_help.c_str());
1251 }
1252
1253 const char *GetSyntax() {
1254 return (m_syntax.empty() ? nullptr : m_syntax.c_str());
1255 }
1256
1257 protected:
1258 // Instance variables to hold the values for command options.
1259
1260 std::string m_help;
1261 std::string m_syntax;
1262 };
1263
1264 Options *GetOptions() override { return &m_options; }
1265
1266 CommandOptions m_options;
Greg Claytonde164aa2011-04-20 16:37:46 +00001267};
1268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269class CommandObjectPythonFunction : public CommandObjectRaw {
Enrico Granata223383e2011-08-16 23:24:13 +00001270public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1272 std::string funct, std::string help,
1273 ScriptedCommandSynchronicity synch)
Zachary Turnera4496982016-10-05 21:14:38 +00001274 : CommandObjectRaw(interpreter, name),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) {
1276 if (!help.empty())
Zachary Turner442f6532016-11-12 20:41:02 +00001277 SetHelp(help);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 else {
1279 StreamString stream;
1280 stream.Printf("For more information run 'help %s'", name.c_str());
Zachary Turnerc1564272016-11-16 21:15:24 +00001281 SetHelp(stream.GetString());
Enrico Granata223383e2011-08-16 23:24:13 +00001282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 ~CommandObjectPythonFunction() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 bool IsRemovable() const override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +00001288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 const std::string &GetFunctionName() { return m_function_name; }
Jim Ingham5a988412012-06-08 21:56:10 +00001290
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1292
Zachary Turner442f6532016-11-12 20:41:02 +00001293 llvm::StringRef GetHelpLong() override {
1294 if (m_fetched_help_long)
1295 return CommandObjectRaw::GetHelpLong();
1296
1297 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1298 if (!scripter)
1299 return CommandObjectRaw::GetHelpLong();
1300
1301 std::string docstring;
1302 m_fetched_help_long =
1303 scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1304 if (!docstring.empty())
1305 SetHelpLong(docstring);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 return CommandObjectRaw::GetHelpLong();
1307 }
1308
Jim Ingham5a988412012-06-08 21:56:10 +00001309protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310 bool DoExecute(const char *raw_command_line,
1311 CommandReturnObject &result) override {
1312 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1313
1314 Error error;
1315
1316 result.SetStatus(eReturnStatusInvalid);
1317
1318 if (!scripter ||
1319 !scripter->RunScriptBasedCommand(m_function_name.c_str(),
1320 raw_command_line, m_synchro, result,
1321 error, m_exe_ctx)) {
1322 result.AppendError(error.AsCString());
1323 result.SetStatus(eReturnStatusFailed);
1324 } else {
1325 // Don't change the status if the command already set it...
1326 if (result.GetStatus() == eReturnStatusInvalid) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001327 if (result.GetOutputData().empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Enrico Granata223383e2011-08-16 23:24:13 +00001329 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 result.SetStatus(eReturnStatusSuccessFinishResult);
1331 }
Enrico Granata223383e2011-08-16 23:24:13 +00001332 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001333
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 return result.Succeeded();
1335 }
1336
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001337private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 std::string m_function_name;
1339 ScriptedCommandSynchronicity m_synchro;
1340 bool m_fetched_help_long;
Enrico Granata223383e2011-08-16 23:24:13 +00001341};
1342
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343class CommandObjectScriptingObject : public CommandObjectRaw {
Enrico Granata9fe00e52015-03-13 02:20:41 +00001344public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 CommandObjectScriptingObject(CommandInterpreter &interpreter,
1346 std::string name,
1347 StructuredData::GenericSP cmd_obj_sp,
1348 ScriptedCommandSynchronicity synch)
Zachary Turnera4496982016-10-05 21:14:38 +00001349 : CommandObjectRaw(interpreter, name),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350 m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false),
1351 m_fetched_help_long(false) {
1352 StreamString stream;
1353 stream.Printf("For more information run 'help %s'", name.c_str());
Zachary Turnerc1564272016-11-16 21:15:24 +00001354 SetHelp(stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter())
1356 GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
1357 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 ~CommandObjectScriptingObject() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001360
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 bool IsRemovable() const override { return true; }
Enrico Granata6f79bb22015-03-13 22:22:28 +00001362
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; }
1364
1365 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1366
Zachary Turner442f6532016-11-12 20:41:02 +00001367 llvm::StringRef GetHelp() override {
1368 if (m_fetched_help_short)
1369 return CommandObjectRaw::GetHelp();
1370 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1371 if (!scripter)
1372 return CommandObjectRaw::GetHelp();
1373 std::string docstring;
1374 m_fetched_help_short =
1375 scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
1376 if (!docstring.empty())
1377 SetHelp(docstring);
1378
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 return CommandObjectRaw::GetHelp();
1380 }
1381
Zachary Turner442f6532016-11-12 20:41:02 +00001382 llvm::StringRef GetHelpLong() override {
1383 if (m_fetched_help_long)
1384 return CommandObjectRaw::GetHelpLong();
1385
1386 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1387 if (!scripter)
1388 return CommandObjectRaw::GetHelpLong();
1389
1390 std::string docstring;
1391 m_fetched_help_long =
1392 scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
1393 if (!docstring.empty())
1394 SetHelpLong(docstring);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 return CommandObjectRaw::GetHelpLong();
1396 }
1397
Enrico Granata9fe00e52015-03-13 02:20:41 +00001398protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 bool DoExecute(const char *raw_command_line,
1400 CommandReturnObject &result) override {
1401 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1402
1403 Error error;
1404
1405 result.SetStatus(eReturnStatusInvalid);
1406
1407 if (!scripter ||
1408 !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1409 m_synchro, result, error, m_exe_ctx)) {
1410 result.AppendError(error.AsCString());
1411 result.SetStatus(eReturnStatusFailed);
1412 } else {
1413 // Don't change the status if the command already set it...
1414 if (result.GetStatus() == eReturnStatusInvalid) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001415 if (result.GetOutputData().empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Enrico Granata9fe00e52015-03-13 02:20:41 +00001417 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 result.SetStatus(eReturnStatusSuccessFinishResult);
1419 }
Enrico Granata9fe00e52015-03-13 02:20:41 +00001420 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 return result.Succeeded();
1423 }
1424
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001425private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 StructuredData::GenericSP m_cmd_obj_sp;
1427 ScriptedCommandSynchronicity m_synchro;
1428 bool m_fetched_help_short : 1;
1429 bool m_fetched_help_long : 1;
Enrico Granata9fe00e52015-03-13 02:20:41 +00001430};
1431
Enrico Granataa9dbf432011-10-17 21:45:27 +00001432//-------------------------------------------------------------------------
1433// CommandObjectCommandsScriptImport
1434//-------------------------------------------------------------------------
1435
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001436OptionDefinition g_script_import_options[] = {
1437 // clang-format off
1438 { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not." },
1439 // clang-format on
1440};
1441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442class CommandObjectCommandsScriptImport : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +00001443public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1445 : CommandObjectParsed(interpreter, "command script import",
1446 "Import a scripting module in LLDB.", nullptr),
1447 m_options() {
1448 CommandArgumentEntry arg1;
1449 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001450
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 // Define the first (and only) variant of this arg.
1452 cmd_arg.arg_type = eArgTypeFilename;
1453 cmd_arg.arg_repetition = eArgRepeatPlus;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 // There is only one variant this argument could be; put it into the
1456 // argument entry.
1457 arg1.push_back(cmd_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +00001458
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 // Push the data for the first argument into the m_arguments vector.
1460 m_arguments.push_back(arg1);
1461 }
1462
1463 ~CommandObjectCommandsScriptImport() override = default;
1464
1465 int HandleArgumentCompletion(Args &input, int &cursor_index,
1466 int &cursor_char_position,
1467 OptionElementVector &opt_element_vector,
1468 int match_start_point, int max_return_elements,
1469 bool &word_complete,
1470 StringList &matches) override {
Zachary Turnerd6a24752016-11-22 17:10:15 +00001471 llvm::StringRef completion_str = input[cursor_index].ref;
1472 completion_str = completion_str.take_front(cursor_char_position);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473
1474 CommandCompletions::InvokeCommonCompletionCallbacks(
1475 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
Zachary Turnerd6a24752016-11-22 17:10:15 +00001476 completion_str, match_start_point, max_return_elements, nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 word_complete, matches);
1478 return matches.GetSize();
1479 }
1480
1481 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +00001482
1483protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484 class CommandOptions : public Options {
1485 public:
1486 CommandOptions() : Options() {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001487
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 ~CommandOptions() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001489
Zachary Turnerfe114832016-11-12 16:56:47 +00001490 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 ExecutionContext *execution_context) override {
1492 Error error;
1493 const int short_option = m_getopt_table[option_idx].val;
Enrico Granata0a305db2011-11-07 22:57:04 +00001494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 switch (short_option) {
1496 case 'r':
1497 m_allow_reload = true;
1498 break;
1499 default:
1500 error.SetErrorStringWithFormat("unrecognized option '%c'",
1501 short_option);
1502 break;
1503 }
1504
1505 return error;
Enrico Granataa9dbf432011-10-17 21:45:27 +00001506 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507
1508 void OptionParsingStarting(ExecutionContext *execution_context) override {
1509 m_allow_reload = true;
1510 }
1511
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001512 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001513 return llvm::makeArrayRef(g_script_import_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515
1516 // Instance variables to hold the values for command options.
1517
1518 bool m_allow_reload;
1519 };
1520
1521 bool DoExecute(Args &command, CommandReturnObject &result) override {
1522 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1523 lldb::eScriptLanguagePython) {
1524 result.AppendError("only scripting language supported for module "
1525 "importing is currently Python");
1526 result.SetStatus(eReturnStatusFailed);
1527 return false;
1528 }
1529
Zachary Turner11eb9c62016-10-05 20:03:37 +00001530 if (command.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 result.AppendError("command script import needs one or more arguments");
1532 result.SetStatus(eReturnStatusFailed);
1533 return false;
1534 }
1535
Zachary Turner11eb9c62016-10-05 20:03:37 +00001536 for (auto &entry : command.entries()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 Error error;
1538
1539 const bool init_session = true;
1540 // FIXME: this is necessary because CommandObject::CheckRequirements()
Zachary Turner11eb9c62016-10-05 20:03:37 +00001541 // assumes that commands won't ever be recursively invoked, but it's
1542 // actually possible to craft a Python script that does other "command
1543 // script imports" in __lldb_init_module the real fix is to have recursive
1544 // commands possible with a CommandInvocation object separate from the
1545 // CommandObject itself, so that recursive command invocations won't stomp
1546 // on each other (wrt to execution contents, options, and more)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 m_exe_ctx.Clear();
1548 if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(
Zachary Turner11eb9c62016-10-05 20:03:37 +00001549 entry.c_str(), m_options.m_allow_reload, init_session, error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1551 } else {
1552 result.AppendErrorWithFormat("module importing failed: %s",
1553 error.AsCString());
1554 result.SetStatus(eReturnStatusFailed);
1555 }
1556 }
1557
1558 return result.Succeeded();
1559 }
1560
1561 CommandOptions m_options;
Enrico Granataa9dbf432011-10-17 21:45:27 +00001562};
Enrico Granata223383e2011-08-16 23:24:13 +00001563
1564//-------------------------------------------------------------------------
1565// CommandObjectCommandsScriptAdd
1566//-------------------------------------------------------------------------
1567
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001568static OptionEnumValueElement g_script_synchro_type[] = {
1569 {eScriptedCommandSynchronicitySynchronous, "synchronous",
1570 "Run synchronous"},
1571 {eScriptedCommandSynchronicityAsynchronous, "asynchronous",
1572 "Run asynchronous"},
1573 {eScriptedCommandSynchronicityCurrentValue, "current",
1574 "Do not alter current setting"},
1575 {0, nullptr, nullptr}};
1576
1577static OptionDefinition g_script_add_options[] = {
1578 // clang-format off
1579 { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name." },
1580 { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name." },
1581 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "The help text to display for this command." },
1582 { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, nullptr, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity, "Set the synchronicity of this command's executions with regard to LLDB event system." },
1583 // clang-format on
1584};
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1587 public IOHandlerDelegateMultiline {
Jim Ingham5a988412012-06-08 21:56:10 +00001588public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1590 : CommandObjectParsed(interpreter, "command script add",
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001591 "Add a scripted function as an LLDB command.",
1592 nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 IOHandlerDelegateMultiline("DONE"), m_options() {
1594 CommandArgumentEntry arg1;
1595 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001596
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 // Define the first (and only) variant of this arg.
1598 cmd_arg.arg_type = eArgTypeCommandName;
1599 cmd_arg.arg_repetition = eArgRepeatPlain;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001600
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 // There is only one variant this argument could be; put it into the
1602 // argument entry.
1603 arg1.push_back(cmd_arg);
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 // Push the data for the first argument into the m_arguments vector.
1606 m_arguments.push_back(arg1);
1607 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001608
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 ~CommandObjectCommandsScriptAdd() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +00001610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611 Options *GetOptions() override { return &m_options; }
Greg Clayton44d93782014-01-27 23:43:24 +00001612
Jim Ingham5a988412012-06-08 21:56:10 +00001613protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 class CommandOptions : public Options {
1615 public:
1616 CommandOptions()
1617 : Options(), m_class_name(), m_funct_name(), m_short_help(),
1618 m_synchronicity(eScriptedCommandSynchronicitySynchronous) {}
Enrico Granata223383e2011-08-16 23:24:13 +00001619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 ~CommandOptions() override = default;
1621
Zachary Turnerfe114832016-11-12 16:56:47 +00001622 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 ExecutionContext *execution_context) override {
1624 Error error;
1625 const int short_option = m_getopt_table[option_idx].val;
1626
1627 switch (short_option) {
1628 case 'f':
Zachary Turnerfe114832016-11-12 16:56:47 +00001629 if (!option_arg.empty())
1630 m_funct_name = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 break;
1632 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +00001633 if (!option_arg.empty())
1634 m_class_name = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 break;
1636 case 'h':
Zachary Turnerfe114832016-11-12 16:56:47 +00001637 if (!option_arg.empty())
1638 m_short_help = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001639 break;
1640 case 's':
1641 m_synchronicity =
1642 (ScriptedCommandSynchronicity)Args::StringToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +00001643 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 if (!error.Success())
1645 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +00001646 "unrecognized value for synchronicity '%s'",
1647 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 break;
1649 default:
1650 error.SetErrorStringWithFormat("unrecognized option '%c'",
1651 short_option);
1652 break;
1653 }
1654
1655 return error;
Enrico Granata223383e2011-08-16 23:24:13 +00001656 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657
1658 void OptionParsingStarting(ExecutionContext *execution_context) override {
1659 m_class_name.clear();
1660 m_funct_name.clear();
1661 m_short_help.clear();
1662 m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1663 }
1664
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001665 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001666 return llvm::makeArrayRef(g_script_add_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001667 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668
1669 // Instance variables to hold the values for command options.
1670
1671 std::string m_class_name;
1672 std::string m_funct_name;
Enrico Granata735152e2014-09-15 17:52:44 +00001673 std::string m_short_help;
Greg Clayton44d93782014-01-27 23:43:24 +00001674 ScriptedCommandSynchronicity m_synchronicity;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 };
1676
1677 void IOHandlerActivated(IOHandler &io_handler) override {
1678 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1679 if (output_sp) {
1680 output_sp->PutCString(g_python_command_instructions);
1681 output_sp->Flush();
1682 }
1683 }
1684
1685 void IOHandlerInputComplete(IOHandler &io_handler,
1686 std::string &data) override {
1687 StreamFileSP error_sp = io_handler.GetErrorStreamFile();
1688
1689 ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
1690 if (interpreter) {
1691
1692 StringList lines;
1693 lines.SplitIntoLines(data);
1694 if (lines.GetSize() > 0) {
1695 std::string funct_name_str;
1696 if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1697 if (funct_name_str.empty()) {
1698 error_sp->Printf("error: unable to obtain a function name, didn't "
1699 "add python command.\n");
1700 error_sp->Flush();
1701 } else {
1702 // everything should be fine now, let's add this alias
1703
1704 CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00001705 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001706 m_synchronicity));
1707
1708 if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp,
1709 true)) {
1710 error_sp->Printf("error: unable to add selected command, didn't "
1711 "add python command.\n");
1712 error_sp->Flush();
1713 }
1714 }
1715 } else {
1716 error_sp->Printf(
1717 "error: unable to create function, didn't add python command.\n");
1718 error_sp->Flush();
1719 }
1720 } else {
1721 error_sp->Printf("error: empty function, didn't add python command.\n");
1722 error_sp->Flush();
1723 }
1724 } else {
1725 error_sp->Printf(
1726 "error: script interpreter missing, didn't add python command.\n");
1727 error_sp->Flush();
1728 }
1729
1730 io_handler.SetIsDone(true);
1731 }
1732
1733protected:
1734 bool DoExecute(Args &command, CommandReturnObject &result) override {
1735 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1736 lldb::eScriptLanguagePython) {
1737 result.AppendError("only scripting language supported for scripted "
1738 "commands is currently Python");
1739 result.SetStatus(eReturnStatusFailed);
1740 return false;
1741 }
1742
Zachary Turner11eb9c62016-10-05 20:03:37 +00001743 if (command.GetArgumentCount() != 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 result.AppendError("'command script add' requires one argument");
1745 result.SetStatus(eReturnStatusFailed);
1746 return false;
1747 }
1748
1749 // Store the options in case we get multi-line input
Zachary Turner4574a892016-12-08 01:31:04 +00001750 m_cmd_name = command[0].ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 m_short_help.assign(m_options.m_short_help);
1752 m_synchronicity = m_options.m_synchronicity;
1753
1754 if (m_options.m_class_name.empty()) {
1755 if (m_options.m_funct_name.empty()) {
1756 m_interpreter.GetPythonCommandsFromIOHandler(
1757 " ", // Prompt
1758 *this, // IOHandlerDelegate
1759 true, // Run IOHandler in async mode
1760 nullptr); // Baton for the "io_handler" that will be passed back
1761 // into our IOHandlerDelegate functions
1762 } else {
1763 CommandObjectSP new_cmd(new CommandObjectPythonFunction(
1764 m_interpreter, m_cmd_name, m_options.m_funct_name,
1765 m_options.m_short_help, m_synchronicity));
1766 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1767 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1768 } else {
1769 result.AppendError("cannot add command");
1770 result.SetStatus(eReturnStatusFailed);
1771 }
1772 }
1773 } else {
1774 ScriptInterpreter *interpreter =
1775 GetCommandInterpreter().GetScriptInterpreter();
1776 if (!interpreter) {
1777 result.AppendError("cannot find ScriptInterpreter");
1778 result.SetStatus(eReturnStatusFailed);
1779 return false;
1780 }
1781
1782 auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1783 m_options.m_class_name.c_str());
1784 if (!cmd_obj_sp) {
1785 result.AppendError("cannot create helper object");
1786 result.SetStatus(eReturnStatusFailed);
1787 return false;
1788 }
1789
1790 CommandObjectSP new_cmd(new CommandObjectScriptingObject(
1791 m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
1792 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1793 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1794 } else {
1795 result.AppendError("cannot add command");
1796 result.SetStatus(eReturnStatusFailed);
1797 }
1798 }
1799
1800 return result.Succeeded();
1801 }
1802
1803 CommandOptions m_options;
1804 std::string m_cmd_name;
1805 std::string m_short_help;
1806 ScriptedCommandSynchronicity m_synchronicity;
Enrico Granata223383e2011-08-16 23:24:13 +00001807};
1808
Enrico Granata223383e2011-08-16 23:24:13 +00001809//-------------------------------------------------------------------------
1810// CommandObjectCommandsScriptList
1811//-------------------------------------------------------------------------
1812
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813class CommandObjectCommandsScriptList : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001814public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001815 CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1816 : CommandObjectParsed(interpreter, "command script list",
1817 "List defined scripted commands.", nullptr) {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001818
Kate Stoneb9c1b512016-09-06 20:57:50 +00001819 ~CommandObjectCommandsScriptList() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001820
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 bool DoExecute(Args &command, CommandReturnObject &result) override {
1822 m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1823
1824 result.SetStatus(eReturnStatusSuccessFinishResult);
1825
1826 return true;
1827 }
Enrico Granata223383e2011-08-16 23:24:13 +00001828};
1829
1830//-------------------------------------------------------------------------
1831// CommandObjectCommandsScriptClear
1832//-------------------------------------------------------------------------
1833
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834class CommandObjectCommandsScriptClear : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001835public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836 CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1837 : CommandObjectParsed(interpreter, "command script clear",
1838 "Delete all scripted commands.", nullptr) {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 ~CommandObjectCommandsScriptClear() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001841
Jim Ingham5a988412012-06-08 21:56:10 +00001842protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001843 bool DoExecute(Args &command, CommandReturnObject &result) override {
1844 m_interpreter.RemoveAllUser();
1845
1846 result.SetStatus(eReturnStatusSuccessFinishResult);
1847
1848 return true;
1849 }
Enrico Granata223383e2011-08-16 23:24:13 +00001850};
1851
1852//-------------------------------------------------------------------------
1853// CommandObjectCommandsScriptDelete
1854//-------------------------------------------------------------------------
1855
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001857public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001858 CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1859 : CommandObjectParsed(interpreter, "command script delete",
1860 "Delete a scripted command.", nullptr) {
1861 CommandArgumentEntry arg1;
1862 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001863
Kate Stoneb9c1b512016-09-06 20:57:50 +00001864 // Define the first (and only) variant of this arg.
1865 cmd_arg.arg_type = eArgTypeCommandName;
1866 cmd_arg.arg_repetition = eArgRepeatPlain;
1867
1868 // There is only one variant this argument could be; put it into the
1869 // argument entry.
1870 arg1.push_back(cmd_arg);
1871
1872 // Push the data for the first argument into the m_arguments vector.
1873 m_arguments.push_back(arg1);
1874 }
1875
1876 ~CommandObjectCommandsScriptDelete() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001877
Jim Ingham5a988412012-06-08 21:56:10 +00001878protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879 bool DoExecute(Args &command, CommandReturnObject &result) override {
1880
Zachary Turner11eb9c62016-10-05 20:03:37 +00001881 if (command.GetArgumentCount() != 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001882 result.AppendError("'command script delete' requires one argument");
1883 result.SetStatus(eReturnStatusFailed);
1884 return false;
Enrico Granata223383e2011-08-16 23:24:13 +00001885 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886
Zachary Turner4574a892016-12-08 01:31:04 +00001887 auto cmd_name = command[0].ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888
Zachary Turner4574a892016-12-08 01:31:04 +00001889 if (cmd_name.empty() || !m_interpreter.HasUserCommands() ||
1890 !m_interpreter.UserCommandExists(cmd_name)) {
Zachary Turner867e7d12016-12-09 01:20:58 +00001891 result.AppendErrorWithFormat("command %s not found", command[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001892 result.SetStatus(eReturnStatusFailed);
Zachary Turner4574a892016-12-08 01:31:04 +00001893 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001894 }
1895
Zachary Turner4574a892016-12-08 01:31:04 +00001896 m_interpreter.RemoveUser(cmd_name);
1897 result.SetStatus(eReturnStatusSuccessFinishResult);
1898 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899 }
Enrico Granata223383e2011-08-16 23:24:13 +00001900};
1901
1902#pragma mark CommandObjectMultiwordCommandsScript
1903
1904//-------------------------------------------------------------------------
1905// CommandObjectMultiwordCommandsScript
1906//-------------------------------------------------------------------------
1907
Kate Stoneb9c1b512016-09-06 20:57:50 +00001908class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
Enrico Granata223383e2011-08-16 23:24:13 +00001909public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1911 : CommandObjectMultiword(
1912 interpreter, "command script", "Commands for managing custom "
1913 "commands implemented by "
1914 "interpreter scripts.",
1915 "command script <subcommand> [<subcommand-options>]") {
1916 LoadSubCommand("add", CommandObjectSP(
1917 new CommandObjectCommandsScriptAdd(interpreter)));
1918 LoadSubCommand(
1919 "delete",
1920 CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1921 LoadSubCommand(
1922 "clear",
1923 CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1924 LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1925 interpreter)));
1926 LoadSubCommand(
1927 "import",
1928 CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1929 }
Enrico Granata223383e2011-08-16 23:24:13 +00001930
Kate Stoneb9c1b512016-09-06 20:57:50 +00001931 ~CommandObjectMultiwordCommandsScript() override = default;
Enrico Granata223383e2011-08-16 23:24:13 +00001932};
1933
Jim Inghamebc09c32010-07-07 03:36:20 +00001934#pragma mark CommandObjectMultiwordCommands
1935
1936//-------------------------------------------------------------------------
1937// CommandObjectMultiwordCommands
1938//-------------------------------------------------------------------------
1939
Kate Stoneb9c1b512016-09-06 20:57:50 +00001940CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
1941 CommandInterpreter &interpreter)
1942 : CommandObjectMultiword(interpreter, "command",
1943 "Commands for managing custom LLDB commands.",
1944 "command <subcommand> [<subcommand-options>]") {
1945 LoadSubCommand("source",
1946 CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
1947 LoadSubCommand("alias",
1948 CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
1949 LoadSubCommand("unalias", CommandObjectSP(
1950 new CommandObjectCommandsUnalias(interpreter)));
1951 LoadSubCommand("delete",
1952 CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
1953 LoadSubCommand(
1954 "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
1955 LoadSubCommand("history", CommandObjectSP(
1956 new CommandObjectCommandsHistory(interpreter)));
1957 LoadSubCommand(
1958 "script",
1959 CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
Jim Inghamebc09c32010-07-07 03:36:20 +00001960}
1961
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001962CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;