blob: 1106b37720c4ce2cf6799e1d0113a95ab1d1d9b4 [file] [log] [blame]
Jim Inghamebc09c32010-07-07 03:36:20 +00001//===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===//
2//
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"
Enrico Granatabe93a352011-08-16 16:49:25 +000020#include "lldb/Core/StringList.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"
Jim Inghamebc09c32010-07-07 03:36:20 +000031
32using namespace lldb;
33using namespace lldb_private;
34
Jim Inghamebc09c32010-07-07 03:36:20 +000035//-------------------------------------------------------------------------
36// CommandObjectCommandsSource
37//-------------------------------------------------------------------------
38
Zachary Turner1f0f5b52016-09-22 20:22:55 +000039static OptionDefinition g_history_options[] = {
40 // clang-format off
41 { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." },
42 { 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)." },
43 { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." },
44 { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clears the current command history." },
45 // clang-format on
46};
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048class CommandObjectCommandsHistory : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +000049public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 CommandObjectCommandsHistory(CommandInterpreter &interpreter)
51 : CommandObjectParsed(interpreter, "command history",
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +000052 "Dump the history of commands in this session.",
53 nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 m_options() {}
Jim Ingham5a988412012-06-08 21:56:10 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 ~CommandObjectCommandsHistory() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +000059
60protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 class CommandOptions : public Options {
62 public:
63 CommandOptions()
64 : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {
Jim Inghama5a97eb2011-07-12 03:12:18 +000065 }
Jim Ingham5a988412012-06-08 21:56:10 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 ~CommandOptions() override = default;
68
Zachary Turnerfe114832016-11-12 16:56:47 +000069 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 ExecutionContext *execution_context) override {
71 Error error;
72 const int short_option = m_getopt_table[option_idx].val;
73
74 switch (short_option) {
75 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +000076 error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 break;
78 case 's':
Zachary Turnerfe114832016-11-12 16:56:47 +000079 if (option_arg == "end") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 m_start_idx.SetCurrentValue(UINT64_MAX);
81 m_start_idx.SetOptionWasSet();
82 } else
Zachary Turnerfe114832016-11-12 16:56:47 +000083 error = m_start_idx.SetValueFromString(option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 eVarSetOperationAssign);
85 break;
86 case 'e':
Zachary Turnerfe114832016-11-12 16:56:47 +000087 error =
88 m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 break;
90 case 'C':
91 m_clear.SetCurrentValue(true);
92 m_clear.SetOptionWasSet();
93 break;
94 default:
95 error.SetErrorStringWithFormat("unrecognized option '%c'",
96 short_option);
97 break;
98 }
99
100 return error;
101 }
102
103 void OptionParsingStarting(ExecutionContext *execution_context) override {
104 m_start_idx.Clear();
105 m_stop_idx.Clear();
106 m_count.Clear();
107 m_clear.Clear();
108 }
109
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000110 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000111 return llvm::makeArrayRef(g_history_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000112 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113
114 // Instance variables to hold the values for command options.
115
116 OptionValueUInt64 m_start_idx;
117 OptionValueUInt64 m_stop_idx;
118 OptionValueUInt64 m_count;
119 OptionValueBoolean m_clear;
120 };
121
122 bool DoExecute(Args &command, CommandReturnObject &result) override {
123 if (m_options.m_clear.GetCurrentValue() &&
124 m_options.m_clear.OptionWasSet()) {
125 m_interpreter.GetCommandHistory().Clear();
126 result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
127 } else {
128 if (m_options.m_start_idx.OptionWasSet() &&
129 m_options.m_stop_idx.OptionWasSet() &&
130 m_options.m_count.OptionWasSet()) {
131 result.AppendError("--count, --start-index and --end-index cannot be "
132 "all specified in the same invocation");
133 result.SetStatus(lldb::eReturnStatusFailed);
134 } else {
135 std::pair<bool, uint64_t> start_idx(
136 m_options.m_start_idx.OptionWasSet(),
137 m_options.m_start_idx.GetCurrentValue());
138 std::pair<bool, uint64_t> stop_idx(
139 m_options.m_stop_idx.OptionWasSet(),
140 m_options.m_stop_idx.GetCurrentValue());
141 std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
142 m_options.m_count.GetCurrentValue());
143
144 const CommandHistory &history(m_interpreter.GetCommandHistory());
145
146 if (start_idx.first && start_idx.second == UINT64_MAX) {
147 if (count.first) {
148 start_idx.second = history.GetSize() - count.second;
149 stop_idx.second = history.GetSize() - 1;
150 } else if (stop_idx.first) {
151 start_idx.second = stop_idx.second;
152 stop_idx.second = history.GetSize() - 1;
153 } else {
154 start_idx.second = 0;
155 stop_idx.second = history.GetSize() - 1;
156 }
157 } else {
158 if (!start_idx.first && !stop_idx.first && !count.first) {
159 start_idx.second = 0;
160 stop_idx.second = history.GetSize() - 1;
161 } else if (start_idx.first) {
162 if (count.first) {
163 stop_idx.second = start_idx.second + count.second - 1;
164 } else if (!stop_idx.first) {
165 stop_idx.second = history.GetSize() - 1;
166 }
167 } else if (stop_idx.first) {
168 if (count.first) {
169 if (stop_idx.second >= count.second)
170 start_idx.second = stop_idx.second - count.second + 1;
171 else
172 start_idx.second = 0;
173 }
174 } else /* if (count.first) */
175 {
176 start_idx.second = 0;
177 stop_idx.second = count.second - 1;
178 }
179 }
180 history.Dump(result.GetOutputStream(), start_idx.second,
181 stop_idx.second);
182 }
183 }
184 return result.Succeeded();
185 }
186
187 CommandOptions m_options;
Jim Inghama5a97eb2011-07-12 03:12:18 +0000188};
189
Jim Inghama5a97eb2011-07-12 03:12:18 +0000190//-------------------------------------------------------------------------
191// CommandObjectCommandsSource
192//-------------------------------------------------------------------------
193
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000194static OptionDefinition g_source_options[] = {
195 // clang-format off
196 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." },
197 { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." },
198 { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." },
199 // clang-format on
200};
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202class CommandObjectCommandsSource : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +0000203public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 CommandObjectCommandsSource(CommandInterpreter &interpreter)
205 : CommandObjectParsed(
206 interpreter, "command source",
207 "Read and execute LLDB commands from the file <filename>.",
208 nullptr),
209 m_options() {
210 CommandArgumentEntry arg;
211 CommandArgumentData file_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 // Define the first (and only) variant of this arg.
214 file_arg.arg_type = eArgTypeFilename;
215 file_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 // There is only one variant this argument could be; put it into the
218 // argument entry.
219 arg.push_back(file_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 // Push the data for the first argument into the m_arguments vector.
222 m_arguments.push_back(arg);
223 }
Jim Ingham5a988412012-06-08 21:56:10 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 ~CommandObjectCommandsSource() override = default;
226
227 const char *GetRepeatCommand(Args &current_command_args,
228 uint32_t index) override {
229 return "";
230 }
231
232 int HandleArgumentCompletion(Args &input, int &cursor_index,
233 int &cursor_char_position,
234 OptionElementVector &opt_element_vector,
235 int match_start_point, int max_return_elements,
236 bool &word_complete,
237 StringList &matches) override {
238 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
239 completion_str.erase(cursor_char_position);
240
241 CommandCompletions::InvokeCommonCompletionCallbacks(
242 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
243 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
244 word_complete, matches);
245 return matches.GetSize();
246 }
247
248 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +0000249
250protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 class CommandOptions : public Options {
252 public:
253 CommandOptions()
254 : Options(), m_stop_on_error(true), m_silent_run(false),
255 m_stop_on_continue(true) {}
Jim Inghame16c50a2011-02-18 00:54:25 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 ~CommandOptions() override = default;
Jim Inghame16c50a2011-02-18 00:54:25 +0000258
Zachary Turnerfe114832016-11-12 16:56:47 +0000259 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 ExecutionContext *execution_context) override {
261 Error error;
262 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton340b0302014-02-05 17:57:57 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 switch (short_option) {
265 case 'e':
Zachary Turnerfe114832016-11-12 16:56:47 +0000266 error = m_stop_on_error.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 break;
Greg Clayton340b0302014-02-05 17:57:57 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +0000270 error = m_stop_on_continue.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 break;
Greg Clayton340b0302014-02-05 17:57:57 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 case 's':
Zachary Turnerfe114832016-11-12 16:56:47 +0000274 error = m_silent_run.SetValueFromString(option_arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 break;
Jim Inghame16c50a2011-02-18 00:54:25 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 default:
278 error.SetErrorStringWithFormat("unrecognized option '%c'",
279 short_option);
280 break;
281 }
Jim Inghame16c50a2011-02-18 00:54:25 +0000282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 return error;
Jim Inghamebc09c32010-07-07 03:36:20 +0000284 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 void OptionParsingStarting(ExecutionContext *execution_context) override {
287 m_stop_on_error.Clear();
288 m_silent_run.Clear();
289 m_stop_on_continue.Clear();
290 }
291
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000292 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000293 return llvm::makeArrayRef(g_source_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295
296 // Instance variables to hold the values for command options.
297
298 OptionValueBoolean m_stop_on_error;
299 OptionValueBoolean m_silent_run;
300 OptionValueBoolean m_stop_on_continue;
301 };
302
303 bool DoExecute(Args &command, CommandReturnObject &result) override {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000304 if (command.GetArgumentCount() == 1) {
305 llvm::StringRef filename = command.GetArgumentAtIndex(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306
307 FileSpec cmd_file(filename, true);
308 ExecutionContext *exe_ctx = nullptr; // Just use the default context.
309
310 // If any options were set, then use them
311 if (m_options.m_stop_on_error.OptionWasSet() ||
312 m_options.m_silent_run.OptionWasSet() ||
313 m_options.m_stop_on_continue.OptionWasSet()) {
314 // Use user set settings
315 CommandInterpreterRunOptions options;
316 options.SetStopOnContinue(
317 m_options.m_stop_on_continue.GetCurrentValue());
318 options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
319 options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue());
320 options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue());
321
322 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options,
323 result);
324 } else {
325 // No options were set, inherit any settings from nested "command
326 // source" commands,
327 // or set to sane default settings...
328 CommandInterpreterRunOptions options;
329 m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options,
330 result);
331 }
332 } else {
333 result.AppendErrorWithFormat(
334 "'%s' takes exactly one executable filename argument.\n",
Zachary Turnera4496982016-10-05 21:14:38 +0000335 GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 result.SetStatus(eReturnStatusFailed);
337 }
338 return result.Succeeded();
339 }
340
341 CommandOptions m_options;
Jim Inghamebc09c32010-07-07 03:36:20 +0000342};
343
344#pragma mark CommandObjectCommandsAlias
345//-------------------------------------------------------------------------
346// CommandObjectCommandsAlias
347//-------------------------------------------------------------------------
348
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000349static OptionDefinition g_alias_options[] = {
350 // clang-format off
351 { LLDB_OPT_SET_ALL, false, "help", 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" },
352 { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" },
353 // clang-format on
354};
355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356static const char *g_python_command_instructions =
357 "Enter your Python command(s). Type 'DONE' to end.\n"
358 "You must define a Python function with this signature:\n"
359 "def my_command_impl(debugger, args, result, internal_dict):\n";
Enrico Granatabe93a352011-08-16 16:49:25 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361class CommandObjectCommandsAlias : public CommandObjectRaw {
Enrico Granata45d0e232016-03-31 01:10:54 +0000362protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 class CommandOptions : public OptionGroup {
364 public:
365 CommandOptions() : OptionGroup(), m_help(), m_long_help() {}
Enrico Granata45d0e232016-03-31 01:10:54 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 ~CommandOptions() override = default;
368
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000369 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000370 return llvm::makeArrayRef(g_alias_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000371 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372
Zachary Turner8cef4b02016-09-23 17:48:13 +0000373 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 ExecutionContext *execution_context) override {
375 Error error;
376
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000377 const int short_option = GetDefinitions()[option_idx].short_option;
Zachary Turner8cef4b02016-09-23 17:48:13 +0000378 std::string option_str(option_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379
380 switch (short_option) {
381 case 'h':
Zachary Turner8cef4b02016-09-23 17:48:13 +0000382 m_help.SetCurrentValue(option_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 m_help.SetOptionWasSet();
384 break;
385
386 case 'H':
Zachary Turner8cef4b02016-09-23 17:48:13 +0000387 m_long_help.SetCurrentValue(option_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 m_long_help.SetOptionWasSet();
389 break;
390
391 default:
392 error.SetErrorStringWithFormat("invalid short option character '%c'",
393 short_option);
394 break;
395 }
396
397 return error;
Enrico Granata45d0e232016-03-31 01:10:54 +0000398 }
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 void OptionParsingStarting(ExecutionContext *execution_context) override {
401 m_help.Clear();
402 m_long_help.Clear();
403 }
Enrico Granata45d0e232016-03-31 01:10:54 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 OptionValueString m_help;
406 OptionValueString m_long_help;
407 };
408
409 OptionGroupOptions m_option_group;
410 CommandOptions m_command_options;
411
412public:
413 Options *GetOptions() override { return &m_option_group; }
414
415 CommandObjectCommandsAlias(CommandInterpreter &interpreter)
416 : CommandObjectRaw(
417 interpreter, "command alias",
Zachary Turnera4496982016-10-05 21:14:38 +0000418 "Define a custom command in terms of an existing command."),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 m_option_group(), m_command_options() {
420 m_option_group.Append(&m_command_options);
421 m_option_group.Finalize();
422
423 SetHelpLong(
424 "'alias' allows the user to create a short-cut or abbreviation for long \
Kate Stoneea671fb2015-07-14 05:48:36 +0000425commands, multi-word commands, and commands that take particular options. \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426Below are some simple examples of how one might use the 'alias' command:"
427 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000428
429(lldb) command alias sc script
430
431 Creates the abbreviation 'sc' for the 'script' command.
432
433(lldb) command alias bp breakpoint
434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435)"
436 " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
Kate Stoneea671fb2015-07-14 05:48:36 +0000437breakpoint commands are two-word commands, the user would still need to \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
439 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000440
441(lldb) command alias bpl breakpoint list
442
443 Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445)"
446 "An alias can include some options for the command, with the values either \
Kate Stoneea671fb2015-07-14 05:48:36 +0000447filled in at the time the alias is created, or specified as positional \
448arguments, to be filled in when the alias is invoked. The following example \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449shows how to create aliases with options:"
450 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000451
452(lldb) command alias bfl breakpoint set -f %1 -l %2
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454)"
455 " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
Kate Stoneea671fb2015-07-14 05:48:36 +0000456options already part of the alias. So if the user wants to set a breakpoint \
457by file and line without explicitly having to use the -f and -l options, the \
458user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
459for the actual arguments that will be passed when the alias command is used. \
460The number in the placeholder refers to the position/order the actual value \
461occupies when the alias is used. All the occurrences of '%1' in the alias \
462will be replaced with the first argument, all the occurrences of '%2' in the \
463alias will be replaced with the second argument, and so on. This also allows \
464actual arguments to be used multiple times within an alias (see 'process \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465launch' example below)."
466 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468)"
469 "Note: the positional arguments must substitute as whole words in the resultant \
Kate Stoneea671fb2015-07-14 05:48:36 +0000470command, so you can't at present do something like this to append the file extension \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471\".cpp\":"
472 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000473
474(lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
475
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476)"
477 "For more complex aliasing, use the \"command regex\" command instead. In the \
Kate Stoneea671fb2015-07-14 05:48:36 +0000478'bfl' case above, the actual file value will be filled in with the first argument \
479following 'bfl' and the actual line number value will be filled in with the second \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480argument. The user would use this alias as follows:"
481 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000482
483(lldb) command alias bfl breakpoint set -f %1 -l %2
484(lldb) bfl my-file.c 137
485
486This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
487
488Another example:
489
490(lldb) command alias pltty process launch -s -o %1 -e %1
491(lldb) pltty /dev/tty0
492
493 Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495)"
496 "If the user always wanted to pass the same value to a particular option, the \
Kate Stoneea671fb2015-07-14 05:48:36 +0000497alias could be defined with that value directly in the alias as a constant, \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498rather than using a positional placeholder:"
499 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000500
501(lldb) command alias bl3 breakpoint set -f %1 -l 3
502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 Always sets a breakpoint on line 3 of whatever file is indicated.)");
Jim Inghamebc09c32010-07-07 03:36:20 +0000504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 CommandArgumentEntry arg1;
506 CommandArgumentEntry arg2;
507 CommandArgumentEntry arg3;
508 CommandArgumentData alias_arg;
509 CommandArgumentData cmd_arg;
510 CommandArgumentData options_arg;
Jim Inghamebc09c32010-07-07 03:36:20 +0000511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 // Define the first (and only) variant of this arg.
513 alias_arg.arg_type = eArgTypeAliasName;
514 alias_arg.arg_repetition = eArgRepeatPlain;
515
516 // There is only one variant this argument could be; put it into the
517 // argument entry.
518 arg1.push_back(alias_arg);
519
520 // Define the first (and only) variant of this arg.
521 cmd_arg.arg_type = eArgTypeCommandName;
522 cmd_arg.arg_repetition = eArgRepeatPlain;
523
524 // There is only one variant this argument could be; put it into the
525 // argument entry.
526 arg2.push_back(cmd_arg);
527
528 // Define the first (and only) variant of this arg.
529 options_arg.arg_type = eArgTypeAliasOptions;
530 options_arg.arg_repetition = eArgRepeatOptional;
531
532 // There is only one variant this argument could be; put it into the
533 // argument entry.
534 arg3.push_back(options_arg);
535
536 // Push the data for the first argument into the m_arguments vector.
537 m_arguments.push_back(arg1);
538 m_arguments.push_back(arg2);
539 m_arguments.push_back(arg3);
540 }
541
542 ~CommandObjectCommandsAlias() override = default;
Jim Inghamebc09c32010-07-07 03:36:20 +0000543
Jim Ingham5a988412012-06-08 21:56:10 +0000544protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 bool DoExecute(const char *raw_command_line,
546 CommandReturnObject &result) override {
547 if (!raw_command_line || !raw_command_line[0]) {
548 result.AppendError("'command alias' requires at least two arguments");
549 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000550 }
551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
553 m_option_group.NotifyOptionParsingStarting(&exe_ctx);
Caroline Ticeca90c472011-05-06 21:37:15 +0000554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 const char *remainder = nullptr;
Enrico Granata212130a2016-03-08 05:37:15 +0000556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 if (raw_command_line[0] == '-') {
558 // We have some options and these options MUST end with --.
559 const char *end_options = nullptr;
560 const char *s = raw_command_line;
561 while (s && s[0]) {
562 end_options = ::strstr(s, "--");
563 if (end_options) {
564 end_options += 2; // Get past the "--"
565 if (::isspace(end_options[0])) {
566 remainder = end_options;
567 while (::isspace(*remainder))
568 ++remainder;
569 break;
570 }
571 }
572 s = end_options;
573 }
574
575 if (end_options) {
576 Args args(
577 llvm::StringRef(raw_command_line, end_options - raw_command_line));
578 if (!ParseOptions(args, result))
579 return false;
580
581 Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx));
582 if (error.Fail()) {
583 result.AppendError(error.AsCString());
584 result.SetStatus(eReturnStatusFailed);
585 return false;
586 }
587 }
Caroline Tice844d2302010-12-09 22:52:49 +0000588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (nullptr == remainder)
590 remainder = raw_command_line;
Jim Inghamebc09c32010-07-07 03:36:20 +0000591
Zachary Turnera01bccd2016-10-05 21:14:56 +0000592 llvm::StringRef raw_command_string(remainder);
593 Args args(raw_command_string);
Jim Inghamebc09c32010-07-07 03:36:20 +0000594
Zachary Turner11eb9c62016-10-05 20:03:37 +0000595 if (args.GetArgumentCount() < 2) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 result.AppendError("'command alias' requires at least two arguments");
597 result.SetStatus(eReturnStatusFailed);
598 return false;
Jim Inghamebc09c32010-07-07 03:36:20 +0000599 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600
601 // Get the alias command.
602
Zachary Turner11eb9c62016-10-05 20:03:37 +0000603 // TODO: Convert this function to use StringRef. Requires converting
604 // GetCommandObjectForCommand.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 const std::string alias_command = args.GetArgumentAtIndex(0);
606 if (alias_command.size() > 1 && alias_command[0] == '-') {
607 result.AppendError("aliases starting with a dash are not supported");
608 if (alias_command == "--help" || alias_command == "--long-help") {
609 result.AppendWarning("if trying to pass options to 'command alias' add "
610 "a -- at the end of the options");
611 }
612 result.SetStatus(eReturnStatusFailed);
613 return false;
614 }
615
616 // Strip the new alias name off 'raw_command_string' (leave it on args,
617 // which gets passed to 'Execute', which
618 // does the stripping itself.
619 size_t pos = raw_command_string.find(alias_command);
620 if (pos == 0) {
621 raw_command_string = raw_command_string.substr(alias_command.size());
622 pos = raw_command_string.find_first_not_of(' ');
623 if ((pos != std::string::npos) && (pos > 0))
624 raw_command_string = raw_command_string.substr(pos);
625 } else {
626 result.AppendError("Error parsing command string. No alias created.");
627 result.SetStatus(eReturnStatusFailed);
628 return false;
629 }
630
631 // Verify that the command is alias-able.
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000632 if (m_interpreter.CommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 result.AppendErrorWithFormat(
634 "'%s' is a permanent debugger command and cannot be redefined.\n",
635 alias_command.c_str());
636 result.SetStatus(eReturnStatusFailed);
637 return false;
638 }
639
640 // Get CommandObject that is being aliased. The command name is read from
Zachary Turnera01bccd2016-10-05 21:14:56 +0000641 // the front of raw_command_string. raw_command_string is returned with the
642 // name of the command object stripped off the front.
643 llvm::StringRef original_raw_command_string = raw_command_string;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 CommandObject *cmd_obj =
645 m_interpreter.GetCommandObjectForCommand(raw_command_string);
646
647 if (!cmd_obj) {
648 result.AppendErrorWithFormat("invalid command given to 'command alias'. "
649 "'%s' does not begin with a valid command."
650 " No alias created.",
Zachary Turnera01bccd2016-10-05 21:14:56 +0000651 original_raw_command_string.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 result.SetStatus(eReturnStatusFailed);
653 return false;
654 } else if (!cmd_obj->WantsRawCommandString()) {
655 // Note that args was initialized with the original command, and has not
656 // been updated to this point.
657 // Therefore can we pass it to the version of Execute that does not
658 // need/expect raw input in the alias.
659 return HandleAliasingNormalCommand(args, result);
660 } else {
661 return HandleAliasingRawCommand(alias_command, raw_command_string,
662 *cmd_obj, result);
663 }
664 return result.Succeeded();
665 }
666
Zachary Turnera01bccd2016-10-05 21:14:56 +0000667 bool HandleAliasingRawCommand(llvm::StringRef alias_command,
668 llvm::StringRef raw_command_string,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 CommandObject &cmd_obj,
670 CommandReturnObject &result) {
671 // Verify & handle any options/arguments passed to the alias command
672
673 OptionArgVectorSP option_arg_vector_sp =
674 OptionArgVectorSP(new OptionArgVector);
675
676 if (CommandObjectSP cmd_obj_sp =
677 m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) {
Zachary Turnera01bccd2016-10-05 21:14:56 +0000678 if (m_interpreter.AliasExists(alias_command) ||
679 m_interpreter.UserCommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 result.AppendWarningWithFormat(
681 "Overwriting existing definition for '%s'.\n",
Zachary Turnera01bccd2016-10-05 21:14:56 +0000682 alias_command.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 }
684 if (CommandAlias *alias = m_interpreter.AddAlias(
Zachary Turnera01bccd2016-10-05 21:14:56 +0000685 alias_command, cmd_obj_sp, raw_command_string)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 if (m_command_options.m_help.OptionWasSet())
687 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
688 if (m_command_options.m_long_help.OptionWasSet())
689 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
690 result.SetStatus(eReturnStatusSuccessFinishNoResult);
691 } else {
692 result.AppendError("Unable to create requested alias.\n");
693 result.SetStatus(eReturnStatusFailed);
694 }
695
696 } else {
697 result.AppendError("Unable to create requested alias.\n");
698 result.SetStatus(eReturnStatusFailed);
699 }
700
701 return result.Succeeded();
702 }
703
704 bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
705 size_t argc = args.GetArgumentCount();
706
707 if (argc < 2) {
708 result.AppendError("'command alias' requires at least two arguments");
709 result.SetStatus(eReturnStatusFailed);
710 return false;
711 }
712
Zachary Turner11eb9c62016-10-05 20:03:37 +0000713 // TODO: Convert these to StringRefs. Should convert other dependent
714 // functions (CommandExists, UserCommandExists, AliasExists, AddAlias,
715 // etc at the same time.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716 const std::string alias_command = args.GetArgumentAtIndex(0);
717 const std::string actual_command = args.GetArgumentAtIndex(1);
718
719 args.Shift(); // Shift the alias command word off the argument vector.
720 args.Shift(); // Shift the old command word off the argument vector.
721
722 // Verify that the command is alias'able, and get the appropriate command
723 // object.
724
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000725 if (m_interpreter.CommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 result.AppendErrorWithFormat(
727 "'%s' is a permanent debugger command and cannot be redefined.\n",
728 alias_command.c_str());
729 result.SetStatus(eReturnStatusFailed);
730 } else {
731 CommandObjectSP command_obj_sp(
Zachary Turnera4496982016-10-05 21:14:38 +0000732 m_interpreter.GetCommandSPExact(actual_command, true));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 CommandObjectSP subcommand_obj_sp;
734 bool use_subcommand = false;
735 if (command_obj_sp) {
736 CommandObject *cmd_obj = command_obj_sp.get();
737 CommandObject *sub_cmd_obj = nullptr;
738 OptionArgVectorSP option_arg_vector_sp =
739 OptionArgVectorSP(new OptionArgVector);
740
Zachary Turner11eb9c62016-10-05 20:03:37 +0000741 while (cmd_obj->IsMultiwordObject() && !args.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 if (argc >= 3) {
Zachary Turnera4496982016-10-05 21:14:38 +0000743 const std::string sub_command = args.GetArgumentAtIndex(0);
Zachary Turner11eb9c62016-10-05 20:03:37 +0000744 assert(!sub_command.empty());
745 subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command.data());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 if (subcommand_obj_sp) {
747 sub_cmd_obj = subcommand_obj_sp.get();
748 use_subcommand = true;
749 args.Shift(); // Shift the sub_command word off the argument
750 // vector.
751 cmd_obj = sub_cmd_obj;
752 } else {
753 result.AppendErrorWithFormat(
754 "'%s' is not a valid sub-command of '%s'. "
755 "Unable to create alias.\n",
Zachary Turnera4496982016-10-05 21:14:38 +0000756 sub_command.c_str(), actual_command.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 result.SetStatus(eReturnStatusFailed);
758 return false;
759 }
760 }
761 }
762
763 // Verify & handle any options/arguments passed to the alias command
764
765 std::string args_string;
766
Zachary Turner11eb9c62016-10-05 20:03:37 +0000767 if (!args.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 CommandObjectSP tmp_sp =
769 m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false);
770 if (use_subcommand)
771 tmp_sp = m_interpreter.GetCommandSPExact(
772 sub_cmd_obj->GetCommandName(), false);
773
774 args.GetCommandString(args_string);
775 }
776
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000777 if (m_interpreter.AliasExists(alias_command) ||
778 m_interpreter.UserCommandExists(alias_command)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 result.AppendWarningWithFormat(
780 "Overwriting existing definition for '%s'.\n",
781 alias_command.c_str());
782 }
783
784 if (CommandAlias *alias = m_interpreter.AddAlias(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000785 alias_command,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 use_subcommand ? subcommand_obj_sp : command_obj_sp,
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000787 args_string)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 if (m_command_options.m_help.OptionWasSet())
789 alias->SetHelp(m_command_options.m_help.GetCurrentValue());
790 if (m_command_options.m_long_help.OptionWasSet())
791 alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
792 result.SetStatus(eReturnStatusSuccessFinishNoResult);
793 } else {
794 result.AppendError("Unable to create requested alias.\n");
795 result.SetStatus(eReturnStatusFailed);
796 return false;
797 }
798 } else {
799 result.AppendErrorWithFormat("'%s' is not an existing command.\n",
800 actual_command.c_str());
801 result.SetStatus(eReturnStatusFailed);
802 return false;
803 }
804 }
805
806 return result.Succeeded();
807 }
Jim Inghamebc09c32010-07-07 03:36:20 +0000808};
809
810#pragma mark CommandObjectCommandsUnalias
811//-------------------------------------------------------------------------
812// CommandObjectCommandsUnalias
813//-------------------------------------------------------------------------
814
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815class CommandObjectCommandsUnalias : public CommandObjectParsed {
Jim Inghamebc09c32010-07-07 03:36:20 +0000816public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
818 : CommandObjectParsed(
819 interpreter, "command unalias",
820 "Delete one or more custom commands defined by 'command alias'.",
821 nullptr) {
822 CommandArgumentEntry arg;
823 CommandArgumentData alias_arg;
Jim Inghamebc09c32010-07-07 03:36:20 +0000824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 // Define the first (and only) variant of this arg.
826 alias_arg.arg_type = eArgTypeAliasName;
827 alias_arg.arg_repetition = eArgRepeatPlain;
828
829 // There is only one variant this argument could be; put it into the
830 // argument entry.
831 arg.push_back(alias_arg);
832
833 // Push the data for the first argument into the m_arguments vector.
834 m_arguments.push_back(arg);
835 }
836
837 ~CommandObjectCommandsUnalias() override = default;
Jim Inghamebc09c32010-07-07 03:36:20 +0000838
Jim Ingham5a988412012-06-08 21:56:10 +0000839protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 bool DoExecute(Args &args, CommandReturnObject &result) override {
841 CommandObject::CommandMap::iterator pos;
842 CommandObject *cmd_obj;
Jim Inghamebc09c32010-07-07 03:36:20 +0000843
Zachary Turner11eb9c62016-10-05 20:03:37 +0000844 if (args.empty()) {
845 result.AppendError("must call 'unalias' with a valid alias");
846 result.SetStatus(eReturnStatusFailed);
847 return false;
848 }
849
850 // TODO: Convert this function to return a StringRef. Should also convert
851 // dependent functions GetCommandObject, CommandExists, RemoveAlias,
852 // AliasExists, etc.
853 const char *command_name = args.GetArgumentAtIndex(0);
854 cmd_obj = m_interpreter.GetCommandObject(command_name);
855 if (cmd_obj) {
856 if (m_interpreter.CommandExists(command_name)) {
857 if (cmd_obj->IsRemovable()) {
858 result.AppendErrorWithFormat(
859 "'%s' is not an alias, it is a debugger command which can be "
860 "removed using the 'command delete' command.\n",
861 command_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 } else {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000863 result.AppendErrorWithFormat(
864 "'%s' is a permanent debugger command and cannot be removed.\n",
865 command_name);
Jim Inghamebc09c32010-07-07 03:36:20 +0000866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 result.SetStatus(eReturnStatusFailed);
Zachary Turner11eb9c62016-10-05 20:03:37 +0000868 } else {
869 if (!m_interpreter.RemoveAlias(command_name)) {
870 if (m_interpreter.AliasExists(command_name))
871 result.AppendErrorWithFormat(
872 "Error occurred while attempting to unalias '%s'.\n",
873 command_name);
874 else
875 result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
876 command_name);
877 result.SetStatus(eReturnStatusFailed);
878 } else
879 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 }
881 } else {
Zachary Turner11eb9c62016-10-05 20:03:37 +0000882 result.AppendErrorWithFormat(
883 "'%s' is not a known command.\nTry 'help' to see a "
884 "current list of commands.\n",
885 command_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 result.SetStatus(eReturnStatusFailed);
Jim Inghamebc09c32010-07-07 03:36:20 +0000887 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888
889 return result.Succeeded();
890 }
Jim Inghamebc09c32010-07-07 03:36:20 +0000891};
892
Greg Claytonb5472782015-01-09 19:08:20 +0000893#pragma mark CommandObjectCommandsDelete
894//-------------------------------------------------------------------------
895// CommandObjectCommandsDelete
896//-------------------------------------------------------------------------
897
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898class CommandObjectCommandsDelete : public CommandObjectParsed {
Greg Claytonb5472782015-01-09 19:08:20 +0000899public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 CommandObjectCommandsDelete(CommandInterpreter &interpreter)
901 : CommandObjectParsed(
902 interpreter, "command delete",
903 "Delete one or more custom commands defined by 'command regex'.",
904 nullptr) {
905 CommandArgumentEntry arg;
906 CommandArgumentData alias_arg;
Greg Claytonb5472782015-01-09 19:08:20 +0000907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 // Define the first (and only) variant of this arg.
909 alias_arg.arg_type = eArgTypeCommandName;
910 alias_arg.arg_repetition = eArgRepeatPlain;
Greg Claytonb5472782015-01-09 19:08:20 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 // There is only one variant this argument could be; put it into the
913 // argument entry.
914 arg.push_back(alias_arg);
Greg Claytonb5472782015-01-09 19:08:20 +0000915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 // Push the data for the first argument into the m_arguments vector.
917 m_arguments.push_back(arg);
918 }
Greg Claytonb5472782015-01-09 19:08:20 +0000919
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 ~CommandObjectCommandsDelete() override = default;
Greg Claytonb5472782015-01-09 19:08:20 +0000921
922protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 bool DoExecute(Args &args, CommandReturnObject &result) override {
924 CommandObject::CommandMap::iterator pos;
Greg Claytonb5472782015-01-09 19:08:20 +0000925
Zachary Turner11eb9c62016-10-05 20:03:37 +0000926 if (args.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 result.AppendErrorWithFormat("must call '%s' with one or more valid user "
928 "defined regular expression command names",
Zachary Turnera4496982016-10-05 21:14:38 +0000929 GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 result.SetStatus(eReturnStatusFailed);
Greg Claytonb5472782015-01-09 19:08:20 +0000931 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932
Zachary Turner11eb9c62016-10-05 20:03:37 +0000933 // TODO: Convert this to accept a stringRef.
934 const char *command_name = args.GetArgumentAtIndex(0);
935 if (m_interpreter.CommandExists(command_name)) {
936 if (m_interpreter.RemoveCommand(command_name)) {
937 result.SetStatus(eReturnStatusSuccessFinishNoResult);
938 } else {
939 result.AppendErrorWithFormat(
940 "'%s' is a permanent debugger command and cannot be removed.\n",
941 command_name);
942 result.SetStatus(eReturnStatusFailed);
943 }
944 } else {
945 StreamString error_msg_stream;
946 const bool generate_apropos = true;
947 const bool generate_type_lookup = false;
948 CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
949 &error_msg_stream, command_name, nullptr, nullptr, generate_apropos,
950 generate_type_lookup);
951 result.AppendErrorWithFormat("%s", error_msg_stream.GetData());
952 result.SetStatus(eReturnStatusFailed);
953 }
954
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 return result.Succeeded();
956 }
Greg Claytonb5472782015-01-09 19:08:20 +0000957};
958
Greg Claytonde164aa2011-04-20 16:37:46 +0000959//-------------------------------------------------------------------------
960// CommandObjectCommandsAddRegex
961//-------------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000962
963static OptionDefinition g_regex_options[] = {
964 // clang-format off
965 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." },
966 { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." },
967 // clang-format on
968};
969
Jim Ingham5a988412012-06-08 21:56:10 +0000970#pragma mark CommandObjectCommandsAddRegex
Greg Claytonde164aa2011-04-20 16:37:46 +0000971
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972class CommandObjectCommandsAddRegex : public CommandObjectParsed,
973 public IOHandlerDelegateMultiline {
Greg Claytonde164aa2011-04-20 16:37:46 +0000974public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
976 : CommandObjectParsed(
977 interpreter, "command regex", "Define a custom command in terms of "
978 "existing commands by matching "
979 "regular expressions.",
980 "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
981 IOHandlerDelegateMultiline("",
982 IOHandlerDelegate::Completion::LLDBCommand),
983 m_options() {
984 SetHelpLong(
985 R"(
986)"
987 "This command allows the user to create powerful regular expression commands \
Kate Stoneea671fb2015-07-14 05:48:36 +0000988with substitutions. The regular expressions and substitutions are specified \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989using the regular expression substitution format of:"
990 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000991
992 s/<regex>/<subst>/
993
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994)"
995 "<regex> is a regular expression that can use parenthesis to capture regular \
Kate Stoneea671fb2015-07-14 05:48:36 +0000996expression input and substitute the captured matches in the output using %1 \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997for the first match, %2 for the second, and so on."
998 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000)"
1001 "The regular expressions can all be specified on the command line if more than \
Kate Stoneea671fb2015-07-14 05:48:36 +00001002one argument is provided. If just the command name is provided on the command \
1003line, then the regular expressions and substitutions can be entered on separate \
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004lines, followed by an empty line to terminate the command definition."
1005 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +00001006
1007EXAMPLES
1008
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009)"
1010 "The following example will define a regular expression command named 'f' that \
Kate Stoneea671fb2015-07-14 05:48:36 +00001011will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012a number follows 'f':"
1013 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +00001014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
1016 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 ~CommandObjectCommandsAddRegex() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001019
Jim Ingham5a988412012-06-08 21:56:10 +00001020protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 void IOHandlerActivated(IOHandler &io_handler) override {
1022 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1023 if (output_sp) {
1024 output_sp->PutCString("Enter one of more sed substitution commands in "
1025 "the form: 's/<regex>/<subst>/'.\nTerminate the "
1026 "substitution list with an empty line.\n");
1027 output_sp->Flush();
1028 }
1029 }
1030
1031 void IOHandlerInputComplete(IOHandler &io_handler,
1032 std::string &data) override {
1033 io_handler.SetIsDone(true);
1034 if (m_regex_cmd_ap) {
1035 StringList lines;
1036 if (lines.SplitIntoLines(data)) {
1037 const size_t num_lines = lines.GetSize();
1038 bool check_only = false;
1039 for (size_t i = 0; i < num_lines; ++i) {
1040 llvm::StringRef bytes_strref(lines[i]);
1041 Error error = AppendRegexSubstitution(bytes_strref, check_only);
1042 if (error.Fail()) {
1043 if (!m_interpreter.GetDebugger()
1044 .GetCommandInterpreter()
1045 .GetBatchCommandMode()) {
1046 StreamSP out_stream =
1047 m_interpreter.GetDebugger().GetAsyncOutputStream();
1048 out_stream->Printf("error: %s\n", error.AsCString());
1049 }
1050 }
Greg Clayton44d93782014-01-27 23:43:24 +00001051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 }
1053 if (m_regex_cmd_ap->HasRegexEntries()) {
1054 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1055 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1056 }
1057 }
1058 }
1059
1060 bool DoExecute(Args &command, CommandReturnObject &result) override {
1061 const size_t argc = command.GetArgumentCount();
1062 if (argc == 0) {
1063 result.AppendError("usage: 'command regex <command-name> "
1064 "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
1065 result.SetStatus(eReturnStatusFailed);
Zachary Turner11eb9c62016-10-05 20:03:37 +00001066 return false;
1067 }
1068
1069 Error error;
1070 const char *name = command.GetArgumentAtIndex(0);
1071 m_regex_cmd_ap.reset(
1072 new CommandObjectRegexCommand(m_interpreter, name, m_options.GetHelp(),
1073 m_options.GetSyntax(), 10, 0, true));
1074
1075 if (argc == 1) {
1076 Debugger &debugger = m_interpreter.GetDebugger();
1077 bool color_prompt = debugger.GetUseColor();
1078 const bool multiple_lines = true; // Get multiple lines
1079 IOHandlerSP io_handler_sp(new IOHandlerEditline(
1080 debugger, IOHandler::Type::Other,
1081 "lldb-regex", // Name of input reader for history
1082 llvm::StringRef("> "), // Prompt
1083 llvm::StringRef(), // Continuation prompt
1084 multiple_lines, color_prompt,
1085 0, // Don't show line numbers
1086 *this));
1087
1088 if (io_handler_sp) {
1089 debugger.PushIOHandler(io_handler_sp);
1090 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00001093 for (auto &entry : command.entries().drop_front()) {
Zachary Turner11eb9c62016-10-05 20:03:37 +00001094 bool check_only = false;
Zachary Turner97d2c402016-10-05 23:40:23 +00001095 error = AppendRegexSubstitution(entry.ref, check_only);
Zachary Turner11eb9c62016-10-05 20:03:37 +00001096 if (error.Fail())
1097 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 }
Zachary Turner11eb9c62016-10-05 20:03:37 +00001099
1100 if (error.Success()) {
1101 AddRegexCommandToInterpreter();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 }
Greg Clayton44d93782014-01-27 23:43:24 +00001103 }
Zachary Turner11eb9c62016-10-05 20:03:37 +00001104 if (error.Fail()) {
1105 result.AppendError(error.AsCString());
1106 result.SetStatus(eReturnStatusFailed);
1107 }
Greg Clayton44d93782014-01-27 23:43:24 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 return result.Succeeded();
1110 }
1111
1112 Error AppendRegexSubstitution(const llvm::StringRef &regex_sed,
1113 bool check_only) {
1114 Error error;
1115
1116 if (!m_regex_cmd_ap) {
1117 error.SetErrorStringWithFormat(
1118 "invalid regular expression command object for: '%.*s'",
1119 (int)regex_sed.size(), regex_sed.data());
1120 return error;
Greg Clayton44d93782014-01-27 23:43:24 +00001121 }
Greg Clayton44d93782014-01-27 23:43:24 +00001122
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 size_t regex_sed_size = regex_sed.size();
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001124
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 if (regex_sed_size <= 1) {
1126 error.SetErrorStringWithFormat(
1127 "regular expression substitution string is too short: '%.*s'",
1128 (int)regex_sed.size(), regex_sed.data());
1129 return error;
Greg Claytonde164aa2011-04-20 16:37:46 +00001130 }
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 if (regex_sed[0] != 's') {
1133 error.SetErrorStringWithFormat("regular expression substitution string "
1134 "doesn't start with 's': '%.*s'",
1135 (int)regex_sed.size(), regex_sed.data());
1136 return error;
1137 }
1138 const size_t first_separator_char_pos = 1;
1139 // use the char that follows 's' as the regex separator character
1140 // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
1141 const char separator_char = regex_sed[first_separator_char_pos];
1142 const size_t second_separator_char_pos =
1143 regex_sed.find(separator_char, first_separator_char_pos + 1);
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 if (second_separator_char_pos == std::string::npos) {
1146 error.SetErrorStringWithFormat(
1147 "missing second '%c' separator char after '%.*s' in '%.*s'",
1148 separator_char,
1149 (int)(regex_sed.size() - first_separator_char_pos - 1),
1150 regex_sed.data() + (first_separator_char_pos + 1),
1151 (int)regex_sed.size(), regex_sed.data());
1152 return error;
1153 }
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001154
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155 const size_t third_separator_char_pos =
1156 regex_sed.find(separator_char, second_separator_char_pos + 1);
Greg Clayton44d93782014-01-27 23:43:24 +00001157
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158 if (third_separator_char_pos == std::string::npos) {
1159 error.SetErrorStringWithFormat(
1160 "missing third '%c' separator char after '%.*s' in '%.*s'",
1161 separator_char,
1162 (int)(regex_sed.size() - second_separator_char_pos - 1),
1163 regex_sed.data() + (second_separator_char_pos + 1),
1164 (int)regex_sed.size(), regex_sed.data());
1165 return error;
1166 }
1167
1168 if (third_separator_char_pos != regex_sed_size - 1) {
1169 // Make sure that everything that follows the last regex
1170 // separator char
1171 if (regex_sed.find_first_not_of("\t\n\v\f\r ",
1172 third_separator_char_pos + 1) !=
1173 std::string::npos) {
1174 error.SetErrorStringWithFormat(
1175 "extra data found after the '%.*s' regular expression substitution "
1176 "string: '%.*s'",
1177 (int)third_separator_char_pos + 1, regex_sed.data(),
1178 (int)(regex_sed.size() - third_separator_char_pos - 1),
1179 regex_sed.data() + (third_separator_char_pos + 1));
Greg Clayton0e5e5a72011-04-20 22:55:21 +00001180 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181 }
1182 } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
1183 error.SetErrorStringWithFormat(
1184 "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1185 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1186 regex_sed.data());
1187 return error;
1188 } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
1189 error.SetErrorStringWithFormat(
1190 "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1191 separator_char, separator_char, separator_char, (int)regex_sed.size(),
1192 regex_sed.data());
1193 return error;
Greg Claytonde164aa2011-04-20 16:37:46 +00001194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195
1196 if (!check_only) {
1197 std::string regex(regex_sed.substr(first_separator_char_pos + 1,
1198 second_separator_char_pos -
1199 first_separator_char_pos - 1));
1200 std::string subst(regex_sed.substr(second_separator_char_pos + 1,
1201 third_separator_char_pos -
1202 second_separator_char_pos - 1));
1203 m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str());
Greg Claytonde164aa2011-04-20 16:37:46 +00001204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 return error;
1206 }
1207
1208 void AddRegexCommandToInterpreter() {
1209 if (m_regex_cmd_ap) {
1210 if (m_regex_cmd_ap->HasRegexEntries()) {
1211 CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1212 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1213 }
1214 }
1215 }
Greg Claytonde164aa2011-04-20 16:37:46 +00001216
Greg Claytonde164aa2011-04-20 16:37:46 +00001217private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
Greg Claytonde164aa2011-04-20 16:37:46 +00001219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 class CommandOptions : public Options {
1221 public:
1222 CommandOptions() : Options() {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001223
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 ~CommandOptions() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001225
Zachary Turnerfe114832016-11-12 16:56:47 +00001226 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001227 ExecutionContext *execution_context) override {
1228 Error error;
1229 const int short_option = m_getopt_table[option_idx].val;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001230
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 switch (short_option) {
1232 case 'h':
1233 m_help.assign(option_arg);
1234 break;
1235 case 's':
1236 m_syntax.assign(option_arg);
1237 break;
1238 default:
1239 error.SetErrorStringWithFormat("unrecognized option '%c'",
1240 short_option);
1241 break;
1242 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 return error;
1245 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 void OptionParsingStarting(ExecutionContext *execution_context) override {
1248 m_help.clear();
1249 m_syntax.clear();
1250 }
1251
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001252 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001253 return llvm::makeArrayRef(g_regex_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255
Zachary Turner11eb9c62016-10-05 20:03:37 +00001256 // TODO: Convert these functions to return StringRefs.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 const char *GetHelp() {
1258 return (m_help.empty() ? nullptr : m_help.c_str());
1259 }
1260
1261 const char *GetSyntax() {
1262 return (m_syntax.empty() ? nullptr : m_syntax.c_str());
1263 }
1264
1265 protected:
1266 // Instance variables to hold the values for command options.
1267
1268 std::string m_help;
1269 std::string m_syntax;
1270 };
1271
1272 Options *GetOptions() override { return &m_options; }
1273
1274 CommandOptions m_options;
Greg Claytonde164aa2011-04-20 16:37:46 +00001275};
1276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277class CommandObjectPythonFunction : public CommandObjectRaw {
Enrico Granata223383e2011-08-16 23:24:13 +00001278public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1280 std::string funct, std::string help,
1281 ScriptedCommandSynchronicity synch)
Zachary Turnera4496982016-10-05 21:14:38 +00001282 : CommandObjectRaw(interpreter, name),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) {
1284 if (!help.empty())
1285 SetHelp(help.c_str());
1286 else {
1287 StreamString stream;
1288 stream.Printf("For more information run 'help %s'", name.c_str());
1289 SetHelp(stream.GetData());
Enrico Granata223383e2011-08-16 23:24:13 +00001290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 ~CommandObjectPythonFunction() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 bool IsRemovable() const override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +00001296
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297 const std::string &GetFunctionName() { return m_function_name; }
Jim Ingham5a988412012-06-08 21:56:10 +00001298
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1300
1301 const char *GetHelpLong() override {
1302 if (!m_fetched_help_long) {
1303 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1304 if (scripter) {
1305 std::string docstring;
1306 m_fetched_help_long = scripter->GetDocumentationForItem(
1307 m_function_name.c_str(), docstring);
1308 if (!docstring.empty())
1309 SetHelpLong(docstring.c_str());
1310 }
Jim Ingham5a988412012-06-08 21:56:10 +00001311 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 return CommandObjectRaw::GetHelpLong();
1313 }
1314
Jim Ingham5a988412012-06-08 21:56:10 +00001315protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316 bool DoExecute(const char *raw_command_line,
1317 CommandReturnObject &result) override {
1318 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1319
1320 Error error;
1321
1322 result.SetStatus(eReturnStatusInvalid);
1323
1324 if (!scripter ||
1325 !scripter->RunScriptBasedCommand(m_function_name.c_str(),
1326 raw_command_line, m_synchro, result,
1327 error, m_exe_ctx)) {
1328 result.AppendError(error.AsCString());
1329 result.SetStatus(eReturnStatusFailed);
1330 } else {
1331 // Don't change the status if the command already set it...
1332 if (result.GetStatus() == eReturnStatusInvalid) {
1333 if (result.GetOutputData() == nullptr ||
1334 result.GetOutputData()[0] == '\0')
1335 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Enrico Granata223383e2011-08-16 23:24:13 +00001336 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 result.SetStatus(eReturnStatusSuccessFinishResult);
1338 }
Enrico Granata223383e2011-08-16 23:24:13 +00001339 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001340
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341 return result.Succeeded();
1342 }
1343
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001344private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 std::string m_function_name;
1346 ScriptedCommandSynchronicity m_synchro;
1347 bool m_fetched_help_long;
Enrico Granata223383e2011-08-16 23:24:13 +00001348};
1349
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350class CommandObjectScriptingObject : public CommandObjectRaw {
Enrico Granata9fe00e52015-03-13 02:20:41 +00001351public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 CommandObjectScriptingObject(CommandInterpreter &interpreter,
1353 std::string name,
1354 StructuredData::GenericSP cmd_obj_sp,
1355 ScriptedCommandSynchronicity synch)
Zachary Turnera4496982016-10-05 21:14:38 +00001356 : CommandObjectRaw(interpreter, name),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false),
1358 m_fetched_help_long(false) {
1359 StreamString stream;
1360 stream.Printf("For more information run 'help %s'", name.c_str());
1361 SetHelp(stream.GetData());
1362 if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter())
1363 GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
1364 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001365
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 ~CommandObjectScriptingObject() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001367
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 bool IsRemovable() const override { return true; }
Enrico Granata6f79bb22015-03-13 22:22:28 +00001369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; }
1371
1372 ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
1373
1374 const char *GetHelp() override {
1375 if (!m_fetched_help_short) {
1376 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1377 if (scripter) {
1378 std::string docstring;
1379 m_fetched_help_short =
1380 scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
1381 if (!docstring.empty())
1382 SetHelp(docstring.c_str());
1383 }
Enrico Granata6f79bb22015-03-13 22:22:28 +00001384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 return CommandObjectRaw::GetHelp();
1386 }
1387
1388 const char *GetHelpLong() override {
1389 if (!m_fetched_help_long) {
1390 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1391 if (scripter) {
1392 std::string docstring;
1393 m_fetched_help_long =
1394 scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
1395 if (!docstring.empty())
1396 SetHelpLong(docstring.c_str());
1397 }
Enrico Granata9fe00e52015-03-13 02:20:41 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 return CommandObjectRaw::GetHelpLong();
1400 }
1401
Enrico Granata9fe00e52015-03-13 02:20:41 +00001402protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 bool DoExecute(const char *raw_command_line,
1404 CommandReturnObject &result) override {
1405 ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1406
1407 Error error;
1408
1409 result.SetStatus(eReturnStatusInvalid);
1410
1411 if (!scripter ||
1412 !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1413 m_synchro, result, error, m_exe_ctx)) {
1414 result.AppendError(error.AsCString());
1415 result.SetStatus(eReturnStatusFailed);
1416 } else {
1417 // Don't change the status if the command already set it...
1418 if (result.GetStatus() == eReturnStatusInvalid) {
1419 if (result.GetOutputData() == nullptr ||
1420 result.GetOutputData()[0] == '\0')
1421 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Enrico Granata9fe00e52015-03-13 02:20:41 +00001422 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 result.SetStatus(eReturnStatusSuccessFinishResult);
1424 }
Enrico Granata9fe00e52015-03-13 02:20:41 +00001425 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001426
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 return result.Succeeded();
1428 }
1429
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001430private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 StructuredData::GenericSP m_cmd_obj_sp;
1432 ScriptedCommandSynchronicity m_synchro;
1433 bool m_fetched_help_short : 1;
1434 bool m_fetched_help_long : 1;
Enrico Granata9fe00e52015-03-13 02:20:41 +00001435};
1436
Enrico Granataa9dbf432011-10-17 21:45:27 +00001437//-------------------------------------------------------------------------
1438// CommandObjectCommandsScriptImport
1439//-------------------------------------------------------------------------
1440
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001441OptionDefinition g_script_import_options[] = {
1442 // clang-format off
1443 { 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." },
1444 // clang-format on
1445};
1446
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447class CommandObjectCommandsScriptImport : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +00001448public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449 CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1450 : CommandObjectParsed(interpreter, "command script import",
1451 "Import a scripting module in LLDB.", nullptr),
1452 m_options() {
1453 CommandArgumentEntry arg1;
1454 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 // Define the first (and only) variant of this arg.
1457 cmd_arg.arg_type = eArgTypeFilename;
1458 cmd_arg.arg_repetition = eArgRepeatPlus;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001459
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 // There is only one variant this argument could be; put it into the
1461 // argument entry.
1462 arg1.push_back(cmd_arg);
Todd Fialae1cfbc72016-08-11 23:51:28 +00001463
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464 // Push the data for the first argument into the m_arguments vector.
1465 m_arguments.push_back(arg1);
1466 }
1467
1468 ~CommandObjectCommandsScriptImport() override = default;
1469
1470 int HandleArgumentCompletion(Args &input, int &cursor_index,
1471 int &cursor_char_position,
1472 OptionElementVector &opt_element_vector,
1473 int match_start_point, int max_return_elements,
1474 bool &word_complete,
1475 StringList &matches) override {
1476 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
1477 completion_str.erase(cursor_char_position);
1478
1479 CommandCompletions::InvokeCommonCompletionCallbacks(
1480 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1481 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
1482 word_complete, matches);
1483 return matches.GetSize();
1484 }
1485
1486 Options *GetOptions() override { return &m_options; }
Jim Ingham5a988412012-06-08 21:56:10 +00001487
1488protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489 class CommandOptions : public Options {
1490 public:
1491 CommandOptions() : Options() {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 ~CommandOptions() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001494
Zachary Turnerfe114832016-11-12 16:56:47 +00001495 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 ExecutionContext *execution_context) override {
1497 Error error;
1498 const int short_option = m_getopt_table[option_idx].val;
Enrico Granata0a305db2011-11-07 22:57:04 +00001499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 switch (short_option) {
1501 case 'r':
1502 m_allow_reload = true;
1503 break;
1504 default:
1505 error.SetErrorStringWithFormat("unrecognized option '%c'",
1506 short_option);
1507 break;
1508 }
1509
1510 return error;
Enrico Granataa9dbf432011-10-17 21:45:27 +00001511 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512
1513 void OptionParsingStarting(ExecutionContext *execution_context) override {
1514 m_allow_reload = true;
1515 }
1516
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001517 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001518 return llvm::makeArrayRef(g_script_import_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520
1521 // Instance variables to hold the values for command options.
1522
1523 bool m_allow_reload;
1524 };
1525
1526 bool DoExecute(Args &command, CommandReturnObject &result) override {
1527 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1528 lldb::eScriptLanguagePython) {
1529 result.AppendError("only scripting language supported for module "
1530 "importing is currently Python");
1531 result.SetStatus(eReturnStatusFailed);
1532 return false;
1533 }
1534
Zachary Turner11eb9c62016-10-05 20:03:37 +00001535 if (command.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536 result.AppendError("command script import needs one or more arguments");
1537 result.SetStatus(eReturnStatusFailed);
1538 return false;
1539 }
1540
Zachary Turner11eb9c62016-10-05 20:03:37 +00001541 for (auto &entry : command.entries()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 Error error;
1543
1544 const bool init_session = true;
1545 // FIXME: this is necessary because CommandObject::CheckRequirements()
Zachary Turner11eb9c62016-10-05 20:03:37 +00001546 // assumes that commands won't ever be recursively invoked, but it's
1547 // actually possible to craft a Python script that does other "command
1548 // script imports" in __lldb_init_module the real fix is to have recursive
1549 // commands possible with a CommandInvocation object separate from the
1550 // CommandObject itself, so that recursive command invocations won't stomp
1551 // on each other (wrt to execution contents, options, and more)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 m_exe_ctx.Clear();
1553 if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(
Zachary Turner11eb9c62016-10-05 20:03:37 +00001554 entry.c_str(), m_options.m_allow_reload, init_session, error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1556 } else {
1557 result.AppendErrorWithFormat("module importing failed: %s",
1558 error.AsCString());
1559 result.SetStatus(eReturnStatusFailed);
1560 }
1561 }
1562
1563 return result.Succeeded();
1564 }
1565
1566 CommandOptions m_options;
Enrico Granataa9dbf432011-10-17 21:45:27 +00001567};
Enrico Granata223383e2011-08-16 23:24:13 +00001568
1569//-------------------------------------------------------------------------
1570// CommandObjectCommandsScriptAdd
1571//-------------------------------------------------------------------------
1572
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001573static OptionEnumValueElement g_script_synchro_type[] = {
1574 {eScriptedCommandSynchronicitySynchronous, "synchronous",
1575 "Run synchronous"},
1576 {eScriptedCommandSynchronicityAsynchronous, "asynchronous",
1577 "Run asynchronous"},
1578 {eScriptedCommandSynchronicityCurrentValue, "current",
1579 "Do not alter current setting"},
1580 {0, nullptr, nullptr}};
1581
1582static OptionDefinition g_script_add_options[] = {
1583 // clang-format off
1584 { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name." },
1585 { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name." },
1586 { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "The help text to display for this command." },
1587 { 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." },
1588 // clang-format on
1589};
1590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1592 public IOHandlerDelegateMultiline {
Jim Ingham5a988412012-06-08 21:56:10 +00001593public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594 CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1595 : CommandObjectParsed(interpreter, "command script add",
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001596 "Add a scripted function as an LLDB command.",
1597 nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 IOHandlerDelegateMultiline("DONE"), m_options() {
1599 CommandArgumentEntry arg1;
1600 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001601
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 // Define the first (and only) variant of this arg.
1603 cmd_arg.arg_type = eArgTypeCommandName;
1604 cmd_arg.arg_repetition = eArgRepeatPlain;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001605
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 // There is only one variant this argument could be; put it into the
1607 // argument entry.
1608 arg1.push_back(cmd_arg);
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001609
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 // Push the data for the first argument into the m_arguments vector.
1611 m_arguments.push_back(arg1);
1612 }
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 ~CommandObjectCommandsScriptAdd() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 Options *GetOptions() override { return &m_options; }
Greg Clayton44d93782014-01-27 23:43:24 +00001617
Jim Ingham5a988412012-06-08 21:56:10 +00001618protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 class CommandOptions : public Options {
1620 public:
1621 CommandOptions()
1622 : Options(), m_class_name(), m_funct_name(), m_short_help(),
1623 m_synchronicity(eScriptedCommandSynchronicitySynchronous) {}
Enrico Granata223383e2011-08-16 23:24:13 +00001624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 ~CommandOptions() override = default;
1626
Zachary Turnerfe114832016-11-12 16:56:47 +00001627 Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 ExecutionContext *execution_context) override {
1629 Error error;
1630 const int short_option = m_getopt_table[option_idx].val;
1631
1632 switch (short_option) {
1633 case 'f':
Zachary Turnerfe114832016-11-12 16:56:47 +00001634 if (!option_arg.empty())
1635 m_funct_name = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 break;
1637 case 'c':
Zachary Turnerfe114832016-11-12 16:56:47 +00001638 if (!option_arg.empty())
1639 m_class_name = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 break;
1641 case 'h':
Zachary Turnerfe114832016-11-12 16:56:47 +00001642 if (!option_arg.empty())
1643 m_short_help = option_arg;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 break;
1645 case 's':
1646 m_synchronicity =
1647 (ScriptedCommandSynchronicity)Args::StringToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +00001648 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 if (!error.Success())
1650 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +00001651 "unrecognized value for synchronicity '%s'",
1652 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 break;
1654 default:
1655 error.SetErrorStringWithFormat("unrecognized option '%c'",
1656 short_option);
1657 break;
1658 }
1659
1660 return error;
Enrico Granata223383e2011-08-16 23:24:13 +00001661 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662
1663 void OptionParsingStarting(ExecutionContext *execution_context) override {
1664 m_class_name.clear();
1665 m_funct_name.clear();
1666 m_short_help.clear();
1667 m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1668 }
1669
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001670 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00001671 return llvm::makeArrayRef(g_script_add_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673
1674 // Instance variables to hold the values for command options.
1675
1676 std::string m_class_name;
1677 std::string m_funct_name;
Enrico Granata735152e2014-09-15 17:52:44 +00001678 std::string m_short_help;
Greg Clayton44d93782014-01-27 23:43:24 +00001679 ScriptedCommandSynchronicity m_synchronicity;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 };
1681
1682 void IOHandlerActivated(IOHandler &io_handler) override {
1683 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1684 if (output_sp) {
1685 output_sp->PutCString(g_python_command_instructions);
1686 output_sp->Flush();
1687 }
1688 }
1689
1690 void IOHandlerInputComplete(IOHandler &io_handler,
1691 std::string &data) override {
1692 StreamFileSP error_sp = io_handler.GetErrorStreamFile();
1693
1694 ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
1695 if (interpreter) {
1696
1697 StringList lines;
1698 lines.SplitIntoLines(data);
1699 if (lines.GetSize() > 0) {
1700 std::string funct_name_str;
1701 if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1702 if (funct_name_str.empty()) {
1703 error_sp->Printf("error: unable to obtain a function name, didn't "
1704 "add python command.\n");
1705 error_sp->Flush();
1706 } else {
1707 // everything should be fine now, let's add this alias
1708
1709 CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00001710 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711 m_synchronicity));
1712
1713 if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp,
1714 true)) {
1715 error_sp->Printf("error: unable to add selected command, didn't "
1716 "add python command.\n");
1717 error_sp->Flush();
1718 }
1719 }
1720 } else {
1721 error_sp->Printf(
1722 "error: unable to create function, didn't add python command.\n");
1723 error_sp->Flush();
1724 }
1725 } else {
1726 error_sp->Printf("error: empty function, didn't add python command.\n");
1727 error_sp->Flush();
1728 }
1729 } else {
1730 error_sp->Printf(
1731 "error: script interpreter missing, didn't add python command.\n");
1732 error_sp->Flush();
1733 }
1734
1735 io_handler.SetIsDone(true);
1736 }
1737
1738protected:
1739 bool DoExecute(Args &command, CommandReturnObject &result) override {
1740 if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1741 lldb::eScriptLanguagePython) {
1742 result.AppendError("only scripting language supported for scripted "
1743 "commands is currently Python");
1744 result.SetStatus(eReturnStatusFailed);
1745 return false;
1746 }
1747
Zachary Turner11eb9c62016-10-05 20:03:37 +00001748 if (command.GetArgumentCount() != 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 result.AppendError("'command script add' requires one argument");
1750 result.SetStatus(eReturnStatusFailed);
1751 return false;
1752 }
1753
1754 // Store the options in case we get multi-line input
1755 m_cmd_name = command.GetArgumentAtIndex(0);
1756 m_short_help.assign(m_options.m_short_help);
1757 m_synchronicity = m_options.m_synchronicity;
1758
1759 if (m_options.m_class_name.empty()) {
1760 if (m_options.m_funct_name.empty()) {
1761 m_interpreter.GetPythonCommandsFromIOHandler(
1762 " ", // Prompt
1763 *this, // IOHandlerDelegate
1764 true, // Run IOHandler in async mode
1765 nullptr); // Baton for the "io_handler" that will be passed back
1766 // into our IOHandlerDelegate functions
1767 } else {
1768 CommandObjectSP new_cmd(new CommandObjectPythonFunction(
1769 m_interpreter, m_cmd_name, m_options.m_funct_name,
1770 m_options.m_short_help, m_synchronicity));
1771 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1772 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1773 } else {
1774 result.AppendError("cannot add command");
1775 result.SetStatus(eReturnStatusFailed);
1776 }
1777 }
1778 } else {
1779 ScriptInterpreter *interpreter =
1780 GetCommandInterpreter().GetScriptInterpreter();
1781 if (!interpreter) {
1782 result.AppendError("cannot find ScriptInterpreter");
1783 result.SetStatus(eReturnStatusFailed);
1784 return false;
1785 }
1786
1787 auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1788 m_options.m_class_name.c_str());
1789 if (!cmd_obj_sp) {
1790 result.AppendError("cannot create helper object");
1791 result.SetStatus(eReturnStatusFailed);
1792 return false;
1793 }
1794
1795 CommandObjectSP new_cmd(new CommandObjectScriptingObject(
1796 m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
1797 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1798 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1799 } else {
1800 result.AppendError("cannot add command");
1801 result.SetStatus(eReturnStatusFailed);
1802 }
1803 }
1804
1805 return result.Succeeded();
1806 }
1807
1808 CommandOptions m_options;
1809 std::string m_cmd_name;
1810 std::string m_short_help;
1811 ScriptedCommandSynchronicity m_synchronicity;
Enrico Granata223383e2011-08-16 23:24:13 +00001812};
1813
Enrico Granata223383e2011-08-16 23:24:13 +00001814//-------------------------------------------------------------------------
1815// CommandObjectCommandsScriptList
1816//-------------------------------------------------------------------------
1817
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818class CommandObjectCommandsScriptList : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001819public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1821 : CommandObjectParsed(interpreter, "command script list",
1822 "List defined scripted commands.", nullptr) {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001823
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 ~CommandObjectCommandsScriptList() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001825
Kate Stoneb9c1b512016-09-06 20:57:50 +00001826 bool DoExecute(Args &command, CommandReturnObject &result) override {
1827 m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1828
1829 result.SetStatus(eReturnStatusSuccessFinishResult);
1830
1831 return true;
1832 }
Enrico Granata223383e2011-08-16 23:24:13 +00001833};
1834
1835//-------------------------------------------------------------------------
1836// CommandObjectCommandsScriptClear
1837//-------------------------------------------------------------------------
1838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839class CommandObjectCommandsScriptClear : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001840public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001841 CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1842 : CommandObjectParsed(interpreter, "command script clear",
1843 "Delete all scripted commands.", nullptr) {}
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001844
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845 ~CommandObjectCommandsScriptClear() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001846
Jim Ingham5a988412012-06-08 21:56:10 +00001847protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 bool DoExecute(Args &command, CommandReturnObject &result) override {
1849 m_interpreter.RemoveAllUser();
1850
1851 result.SetStatus(eReturnStatusSuccessFinishResult);
1852
1853 return true;
1854 }
Enrico Granata223383e2011-08-16 23:24:13 +00001855};
1856
1857//-------------------------------------------------------------------------
1858// CommandObjectCommandsScriptDelete
1859//-------------------------------------------------------------------------
1860
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
Enrico Granata223383e2011-08-16 23:24:13 +00001862public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001863 CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1864 : CommandObjectParsed(interpreter, "command script delete",
1865 "Delete a scripted command.", nullptr) {
1866 CommandArgumentEntry arg1;
1867 CommandArgumentData cmd_arg;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001868
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 // Define the first (and only) variant of this arg.
1870 cmd_arg.arg_type = eArgTypeCommandName;
1871 cmd_arg.arg_repetition = eArgRepeatPlain;
1872
1873 // There is only one variant this argument could be; put it into the
1874 // argument entry.
1875 arg1.push_back(cmd_arg);
1876
1877 // Push the data for the first argument into the m_arguments vector.
1878 m_arguments.push_back(arg1);
1879 }
1880
1881 ~CommandObjectCommandsScriptDelete() override = default;
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001882
Jim Ingham5a988412012-06-08 21:56:10 +00001883protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884 bool DoExecute(Args &command, CommandReturnObject &result) override {
1885
Zachary Turner11eb9c62016-10-05 20:03:37 +00001886 if (command.GetArgumentCount() != 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 result.AppendError("'command script delete' requires one argument");
1888 result.SetStatus(eReturnStatusFailed);
1889 return false;
Enrico Granata223383e2011-08-16 23:24:13 +00001890 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891
1892 const char *cmd_name = command.GetArgumentAtIndex(0);
1893
1894 if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() &&
1895 m_interpreter.UserCommandExists(cmd_name)) {
1896 m_interpreter.RemoveUser(cmd_name);
1897 result.SetStatus(eReturnStatusSuccessFinishResult);
1898 } else {
1899 result.AppendErrorWithFormat("command %s not found", cmd_name);
1900 result.SetStatus(eReturnStatusFailed);
1901 }
1902
1903 return result.Succeeded();
1904 }
Enrico Granata223383e2011-08-16 23:24:13 +00001905};
1906
1907#pragma mark CommandObjectMultiwordCommandsScript
1908
1909//-------------------------------------------------------------------------
1910// CommandObjectMultiwordCommandsScript
1911//-------------------------------------------------------------------------
1912
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
Enrico Granata223383e2011-08-16 23:24:13 +00001914public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1916 : CommandObjectMultiword(
1917 interpreter, "command script", "Commands for managing custom "
1918 "commands implemented by "
1919 "interpreter scripts.",
1920 "command script <subcommand> [<subcommand-options>]") {
1921 LoadSubCommand("add", CommandObjectSP(
1922 new CommandObjectCommandsScriptAdd(interpreter)));
1923 LoadSubCommand(
1924 "delete",
1925 CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1926 LoadSubCommand(
1927 "clear",
1928 CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1929 LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1930 interpreter)));
1931 LoadSubCommand(
1932 "import",
1933 CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1934 }
Enrico Granata223383e2011-08-16 23:24:13 +00001935
Kate Stoneb9c1b512016-09-06 20:57:50 +00001936 ~CommandObjectMultiwordCommandsScript() override = default;
Enrico Granata223383e2011-08-16 23:24:13 +00001937};
1938
Jim Inghamebc09c32010-07-07 03:36:20 +00001939#pragma mark CommandObjectMultiwordCommands
1940
1941//-------------------------------------------------------------------------
1942// CommandObjectMultiwordCommands
1943//-------------------------------------------------------------------------
1944
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
1946 CommandInterpreter &interpreter)
1947 : CommandObjectMultiword(interpreter, "command",
1948 "Commands for managing custom LLDB commands.",
1949 "command <subcommand> [<subcommand-options>]") {
1950 LoadSubCommand("source",
1951 CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
1952 LoadSubCommand("alias",
1953 CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
1954 LoadSubCommand("unalias", CommandObjectSP(
1955 new CommandObjectCommandsUnalias(interpreter)));
1956 LoadSubCommand("delete",
1957 CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
1958 LoadSubCommand(
1959 "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
1960 LoadSubCommand("history", CommandObjectSP(
1961 new CommandObjectCommandsHistory(interpreter)));
1962 LoadSubCommand(
1963 "script",
1964 CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
Jim Inghamebc09c32010-07-07 03:36:20 +00001965}
1966
Eugene Zelenko6e3d8e72016-02-22 23:46:47 +00001967CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;