blob: 653cdbfd6879d60d6ecbf4d65788d145944a010f [file] [log] [blame]
Johnny Chene9a56272012-08-09 23:09:42 +00001//===-- CommandObjectWatchpointCommand.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
10// C Includes
11// C++ Includes
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000012#include <vector>
Johnny Chene9a56272012-08-09 23:09:42 +000013
Eugene Zelenko49bcfd82016-02-23 01:43:44 +000014// Other libraries and framework includes
15// Project includes
Johnny Chene9a56272012-08-09 23:09:42 +000016#include "CommandObjectWatchpoint.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "CommandObjectWatchpointCommand.h"
18#include "lldb/Breakpoint/StoppointCallbackContext.h"
19#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton44d93782014-01-27 23:43:24 +000020#include "lldb/Core/IOHandler.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000021#include "lldb/Host/OptionParser.h"
Johnny Chene9a56272012-08-09 23:09:42 +000022#include "lldb/Interpreter/CommandInterpreter.h"
23#include "lldb/Interpreter/CommandReturnObject.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000024#include "lldb/Interpreter/OptionArgParser.h"
Johnny Chene9a56272012-08-09 23:09:42 +000025#include "lldb/Target/Target.h"
26#include "lldb/Target/Thread.h"
Pavel Labathd821c992018-08-07 11:07:21 +000027#include "lldb/Utility/State.h"
Johnny Chene9a56272012-08-09 23:09:42 +000028
Johnny Chene9a56272012-08-09 23:09:42 +000029using namespace lldb;
30using namespace lldb_private;
31
32//-------------------------------------------------------------------------
33// CommandObjectWatchpointCommandAdd
34//-------------------------------------------------------------------------
35
Zachary Turner1f0f5b52016-09-22 20:22:55 +000036// FIXME: "script-type" needs to have its contents determined dynamically, so
37// somebody can add a new scripting
38// language to lldb and have it pickable here without having to change this
39// enumeration by hand and rebuild lldb proper.
40
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000041static constexpr OptionEnumValueElement g_script_option_enumeration[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +000042 {eScriptLanguageNone, "command",
43 "Commands are in the lldb command interpreter language"},
44 {eScriptLanguagePython, "python", "Commands are in the Python language."},
45 {eSortOrderByName, "default-script",
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000046 "Commands are in the default scripting language."} };
Zachary Turner1f0f5b52016-09-22 20:22:55 +000047
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000048static constexpr OptionEnumValues ScriptOptionEnum() {
49 return OptionEnumValues(g_script_option_enumeration);
50}
51
52static constexpr OptionDefinition g_watchpoint_command_add_options[] = {
Zachary Turner1f0f5b52016-09-22 20:22:55 +000053 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000054 { LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeOneLiner, "Specify a one-line watchpoint command inline. Be sure to surround it with quotes." },
55 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Specify whether watchpoint command execution should terminate on error." },
56 { LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, ScriptOptionEnum(), 0, eArgTypeNone, "Specify the language for the commands - if none is specified, the lldb command interpreter will be used." },
57 { LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this watchpoint. Be sure to give a module name if appropriate." }
Zachary Turner1f0f5b52016-09-22 20:22:55 +000058 // clang-format on
59};
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
62 public IOHandlerDelegateMultiline {
Johnny Chene9a56272012-08-09 23:09:42 +000063public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
65 : CommandObjectParsed(interpreter, "add",
66 "Add a set of LLDB commands to a watchpoint, to be "
67 "executed whenever the watchpoint is hit.",
68 nullptr),
69 IOHandlerDelegateMultiline("DONE",
70 IOHandlerDelegate::Completion::LLDBCommand),
71 m_options() {
72 SetHelpLong(
73 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000074General information about entering watchpoint commands
75------------------------------------------------------
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077)"
78 "This command will prompt for commands to be executed when the specified \
Kate Stoneea671fb2015-07-14 05:48:36 +000079watchpoint is hit. Each command is typed on its own line following the '> ' \
Kate Stoneb9c1b512016-09-06 20:57:50 +000080prompt until 'DONE' is entered."
81 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083)"
84 "Syntactic errors may not be detected when initially entered, and many \
Kate Stoneea671fb2015-07-14 05:48:36 +000085malformed commands can silently fail when executed. If your watchpoint commands \
Kate Stoneb9c1b512016-09-06 20:57:50 +000086do not appear to be executing, double-check the command syntax."
87 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089)"
90 "Note: You may enter any debugger command exactly as you would at the debugger \
Kate Stoneea671fb2015-07-14 05:48:36 +000091prompt. There is no limit to the number of commands supplied, but do NOT enter \
Kate Stoneb9c1b512016-09-06 20:57:50 +000092more than one command per line."
93 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000094
95Special information about PYTHON watchpoint commands
96----------------------------------------------------
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098)"
99 "You may enter either one or more lines of Python, including function \
Kate Stoneea671fb2015-07-14 05:48:36 +0000100definitions or calls to functions that will have been imported by the time \
101the code executes. Single line watchpoint commands will be interpreted 'as is' \
102when the watchpoint is hit. Multiple lines of Python will be wrapped in a \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103generated function, and a call to the function will be attached to the watchpoint."
104 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000105
106This auto-generated function is passed in three arguments:
107
108 frame: an lldb.SBFrame object for the frame which hit the watchpoint.
109
110 wp: the watchpoint that was hit.
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112)"
113 "When specifying a python function with the --python-function option, you need \
114to supply the function name prepended by the module name:"
115 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000116
117 --python-function myutils.watchpoint_callback
118
119The function itself must have the following prototype:
120
121def watchpoint_callback(frame, wp):
122 # Your code goes here
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124)"
125 "The arguments are the same as the arguments passed to generated functions as \
Kate Stoneea671fb2015-07-14 05:48:36 +0000126described above. Note that the global variable 'lldb.frame' will NOT be updated when \
127this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
128can get you to the thread via frame.GetThread(), the thread can get you to the \
129process via thread.GetProcess(), and the process can get you back to the target \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130via process.GetTarget()."
131 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133)"
134 "Important Note: As Python code gets collected into functions, access to global \
Kate Stoneea671fb2015-07-14 05:48:36 +0000135variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136Python syntax, including indentation, when entering Python watchpoint commands."
137 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000138
139Example Python one-line watchpoint command:
140
141(lldb) watchpoint command add -s python 1
142Enter your Python command(s). Type 'DONE' to end.
143> print "Hit this watchpoint!"
144> DONE
145
146As a convenience, this also works for a short Python one-liner:
147
148(lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
149(lldb) run
150Launching '.../a.out' (x86_64)
151(lldb) Fri Sep 10 12:17:45 2010
152Process 21778 Stopped
153* thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
154 36
155 37 int c(int val)
156 38 {
157 39 -> return val + 3;
158 40 }
159 41
160 42 int main (int argc, char const *argv[])
161
162Example multiple line Python watchpoint command, using function definition:
163
164(lldb) watchpoint command add -s python 1
165Enter your Python command(s). Type 'DONE' to end.
166> def watchpoint_output (wp_no):
167> out_string = "Hit watchpoint number " + repr (wp_no)
168> print out_string
169> return True
170> watchpoint_output (1)
171> DONE
172
173Example multiple line Python watchpoint command, using 'loose' Python:
174
175(lldb) watchpoint command add -s p 1
176Enter your Python command(s). Type 'DONE' to end.
177> global wp_count
178> wp_count = wp_count + 1
179> print "Hit this watchpoint " + repr(wp_count) + " times!"
180> DONE
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182)"
183 "In this case, since there is a reference to a global variable, \
Kate Stoneea671fb2015-07-14 05:48:36 +0000184'wp_count', you will also need to make sure 'wp_count' exists and is \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185initialized:"
186 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000187
188(lldb) script
189>>> wp_count = 0
190>>> quit()
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192)"
193 "Final Note: A warning that no watchpoint command was generated when there \
194are no syntax errors may indicate that a function was declared but never called.");
Johnny Chene9a56272012-08-09 23:09:42 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 CommandArgumentEntry arg;
197 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 // Define the first (and only) variant of this arg.
200 wp_id_arg.arg_type = eArgTypeWatchpointID;
201 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 // There is only one variant this argument could be; put it into the
204 // argument entry.
205 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 // Push the data for the first argument into the m_arguments vector.
208 m_arguments.push_back(arg);
209 }
210
211 ~CommandObjectWatchpointCommandAdd() override = default;
212
213 Options *GetOptions() override { return &m_options; }
214
215 void IOHandlerActivated(IOHandler &io_handler) override {
216 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
217 if (output_sp) {
218 output_sp->PutCString(
219 "Enter your debugger command(s). Type 'DONE' to end.\n");
220 output_sp->Flush();
221 }
222 }
223
224 void IOHandlerInputComplete(IOHandler &io_handler,
225 std::string &line) override {
226 io_handler.SetIsDone(true);
227
228 // The WatchpointOptions object is owned by the watchpoint or watchpoint
229 // location
230 WatchpointOptions *wp_options =
231 (WatchpointOptions *)io_handler.GetUserData();
232 if (wp_options) {
233 std::unique_ptr<WatchpointOptions::CommandData> data_ap(
234 new WatchpointOptions::CommandData());
235 if (data_ap) {
236 data_ap->user_source.SplitIntoLines(line);
Zachary Turner4e4fbe82016-09-13 17:53:38 +0000237 auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
238 std::move(data_ap));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
240 }
241 }
242 }
243
244 void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
245 CommandReturnObject &result) {
246 m_interpreter.GetLLDBCommandsFromIOHandler(
247 "> ", // Prompt
248 *this, // IOHandlerDelegate
249 true, // Run IOHandler in async mode
250 wp_options); // Baton for the "io_handler" that will be passed back into
251 // our IOHandlerDelegate functions
252 }
253
254 /// Set a one-liner as the callback for the watchpoint.
255 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
256 const char *oneliner) {
257 std::unique_ptr<WatchpointOptions::CommandData> data_ap(
258 new WatchpointOptions::CommandData());
259
Adrian Prantl05097242018-04-30 16:49:04 +0000260 // It's necessary to set both user_source and script_source to the
261 // oneliner. The former is used to generate callback description (as in
262 // watchpoint command list) while the latter is used for Python to
263 // interpret during the actual callback.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 data_ap->user_source.AppendString(oneliner);
265 data_ap->script_source.assign(oneliner);
266 data_ap->stop_on_error = m_options.m_stop_on_error;
267
Zachary Turner4e4fbe82016-09-13 17:53:38 +0000268 auto baton_sp =
269 std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_ap));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
271 }
272
273 static bool
274 WatchpointOptionsCallbackFunction(void *baton,
275 StoppointCallbackContext *context,
276 lldb::user_id_t watch_id) {
277 bool ret_value = true;
278 if (baton == nullptr)
279 return true;
280
281 WatchpointOptions::CommandData *data =
282 (WatchpointOptions::CommandData *)baton;
283 StringList &commands = data->user_source;
284
285 if (commands.GetSize() > 0) {
286 ExecutionContext exe_ctx(context->exe_ctx_ref);
287 Target *target = exe_ctx.GetTargetPtr();
288 if (target) {
289 CommandReturnObject result;
290 Debugger &debugger = target->GetDebugger();
291 // Rig up the results secondary output stream to the debugger's, so the
Adrian Prantl05097242018-04-30 16:49:04 +0000292 // output will come out synchronously if the debugger is set up that
293 // way.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294
295 StreamSP output_stream(debugger.GetAsyncOutputStream());
296 StreamSP error_stream(debugger.GetAsyncErrorStream());
297 result.SetImmediateOutputStream(output_stream);
298 result.SetImmediateErrorStream(error_stream);
299
300 CommandInterpreterRunOptions options;
301 options.SetStopOnContinue(true);
302 options.SetStopOnError(data->stop_on_error);
303 options.SetEchoCommands(false);
304 options.SetPrintResults(true);
305 options.SetAddToHistory(false);
306
307 debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
308 options, result);
309 result.GetImmediateOutputStream()->Flush();
310 result.GetImmediateErrorStream()->Flush();
311 }
312 }
313 return ret_value;
314 }
315
316 class CommandOptions : public Options {
317 public:
318 CommandOptions()
319 : Options(), m_use_commands(false), m_use_script_language(false),
320 m_script_language(eScriptLanguageNone), m_use_one_liner(false),
321 m_one_liner(), m_function_name() {}
322
323 ~CommandOptions() override = default;
324
Zachary Turner97206d52017-05-12 04:51:55 +0000325 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
326 ExecutionContext *execution_context) override {
327 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 const int short_option = m_getopt_table[option_idx].val;
329
330 switch (short_option) {
331 case 'o':
332 m_use_one_liner = true;
333 m_one_liner = option_arg;
334 break;
335
336 case 's':
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000337 m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +0000338 option_arg, GetDefinitions()[option_idx].enum_values,
339 eScriptLanguageNone, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340
341 m_use_script_language = (m_script_language == eScriptLanguagePython ||
342 m_script_language == eScriptLanguageDefault);
343 break;
344
345 case 'e': {
346 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000347 m_stop_on_error =
348 OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 if (!success)
350 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +0000351 "invalid value for stop-on-error: \"%s\"",
352 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 } break;
354
355 case 'F':
356 m_use_one_liner = false;
357 m_use_script_language = true;
358 m_function_name.assign(option_arg);
359 break;
360
361 default:
362 break;
363 }
364 return error;
Johnny Chene9a56272012-08-09 23:09:42 +0000365 }
366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 void OptionParsingStarting(ExecutionContext *execution_context) override {
368 m_use_commands = true;
369 m_use_script_language = false;
370 m_script_language = eScriptLanguageNone;
Johnny Chene9a56272012-08-09 23:09:42 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 m_use_one_liner = false;
373 m_stop_on_error = true;
374 m_one_liner.clear();
375 m_function_name.clear();
Johnny Chene9a56272012-08-09 23:09:42 +0000376 }
377
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000378 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000379 return llvm::makeArrayRef(g_watchpoint_command_add_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000380 }
Johnny Chene9a56272012-08-09 23:09:42 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 // Instance variables to hold the values for command options.
Johnny Chene9a56272012-08-09 23:09:42 +0000383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 bool m_use_commands;
385 bool m_use_script_language;
386 lldb::ScriptLanguage m_script_language;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 // Instance variables to hold the values for one_liner options.
389 bool m_use_one_liner;
390 std::string m_one_liner;
391 bool m_stop_on_error;
392 std::string m_function_name;
393 };
Johnny Chene9a56272012-08-09 23:09:42 +0000394
395protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 bool DoExecute(Args &command, CommandReturnObject &result) override {
397 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 if (target == nullptr) {
400 result.AppendError("There is not a current executable; there are no "
401 "watchpoints to which to add commands");
402 result.SetStatus(eReturnStatusFailed);
403 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000404 }
405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 const WatchpointList &watchpoints = target->GetWatchpointList();
407 size_t num_watchpoints = watchpoints.GetSize();
408
409 if (num_watchpoints == 0) {
410 result.AppendError("No watchpoints exist to have commands added");
411 result.SetStatus(eReturnStatusFailed);
412 return false;
413 }
414
415 if (!m_options.m_use_script_language &&
416 !m_options.m_function_name.empty()) {
417 result.AppendError("need to enable scripting to have a function run as a "
418 "watchpoint command");
419 result.SetStatus(eReturnStatusFailed);
420 return false;
421 }
422
423 std::vector<uint32_t> valid_wp_ids;
424 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
425 valid_wp_ids)) {
426 result.AppendError("Invalid watchpoints specification.");
427 result.SetStatus(eReturnStatusFailed);
428 return false;
429 }
430
431 result.SetStatus(eReturnStatusSuccessFinishNoResult);
432 const size_t count = valid_wp_ids.size();
433 for (size_t i = 0; i < count; ++i) {
434 uint32_t cur_wp_id = valid_wp_ids.at(i);
435 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
436 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
437 // Sanity check wp first.
438 if (wp == nullptr)
439 continue;
440
441 WatchpointOptions *wp_options = wp->GetOptions();
442 // Skip this watchpoint if wp_options is not good.
443 if (wp_options == nullptr)
444 continue;
445
Adrian Prantl05097242018-04-30 16:49:04 +0000446 // If we are using script language, get the script interpreter in order
447 // to set or collect command callback. Otherwise, call the methods
448 // associated with this object.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 if (m_options.m_use_script_language) {
450 // Special handling for one-liner specified inline.
451 if (m_options.m_use_one_liner) {
452 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
453 wp_options, m_options.m_one_liner.c_str());
454 }
Adrian Prantl05097242018-04-30 16:49:04 +0000455 // Special handling for using a Python function by name instead of
456 // extending the watchpoint callback data structures, we just
457 // automatize what the user would do manually: make their watchpoint
458 // command be a function call
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 else if (!m_options.m_function_name.empty()) {
460 std::string oneliner(m_options.m_function_name);
461 oneliner += "(frame, wp, internal_dict)";
462 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
463 wp_options, oneliner.c_str());
464 } else {
465 m_interpreter.GetScriptInterpreter()
466 ->CollectDataForWatchpointCommandCallback(wp_options, result);
467 }
468 } else {
469 // Special handling for one-liner specified inline.
470 if (m_options.m_use_one_liner)
471 SetWatchpointCommandCallback(wp_options,
472 m_options.m_one_liner.c_str());
473 else
474 CollectDataForWatchpointCommandCallback(wp_options, result);
475 }
476 }
477 }
478
479 return result.Succeeded();
480 }
481
Johnny Chene9a56272012-08-09 23:09:42 +0000482private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 CommandOptions m_options;
Johnny Chene9a56272012-08-09 23:09:42 +0000484};
485
Johnny Chene9a56272012-08-09 23:09:42 +0000486//-------------------------------------------------------------------------
487// CommandObjectWatchpointCommandDelete
488//-------------------------------------------------------------------------
489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
Johnny Chene9a56272012-08-09 23:09:42 +0000491public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
493 : CommandObjectParsed(interpreter, "delete",
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000494 "Delete the set of commands from a watchpoint.",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 nullptr) {
496 CommandArgumentEntry arg;
497 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 // Define the first (and only) variant of this arg.
500 wp_id_arg.arg_type = eArgTypeWatchpointID;
501 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 // There is only one variant this argument could be; put it into the
504 // argument entry.
505 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 // Push the data for the first argument into the m_arguments vector.
508 m_arguments.push_back(arg);
509 }
Johnny Chene9a56272012-08-09 23:09:42 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 ~CommandObjectWatchpointCommandDelete() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000512
513protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 bool DoExecute(Args &command, CommandReturnObject &result) override {
515 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 if (target == nullptr) {
518 result.AppendError("There is not a current executable; there are no "
519 "watchpoints from which to delete commands");
520 result.SetStatus(eReturnStatusFailed);
521 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000522 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523
524 const WatchpointList &watchpoints = target->GetWatchpointList();
525 size_t num_watchpoints = watchpoints.GetSize();
526
527 if (num_watchpoints == 0) {
528 result.AppendError("No watchpoints exist to have commands deleted");
529 result.SetStatus(eReturnStatusFailed);
530 return false;
531 }
532
533 if (command.GetArgumentCount() == 0) {
534 result.AppendError(
535 "No watchpoint specified from which to delete the commands");
536 result.SetStatus(eReturnStatusFailed);
537 return false;
538 }
539
540 std::vector<uint32_t> valid_wp_ids;
541 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
542 valid_wp_ids)) {
543 result.AppendError("Invalid watchpoints specification.");
544 result.SetStatus(eReturnStatusFailed);
545 return false;
546 }
547
548 result.SetStatus(eReturnStatusSuccessFinishNoResult);
549 const size_t count = valid_wp_ids.size();
550 for (size_t i = 0; i < count; ++i) {
551 uint32_t cur_wp_id = valid_wp_ids.at(i);
552 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
553 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
554 if (wp)
555 wp->ClearCallback();
556 } else {
557 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
558 result.SetStatus(eReturnStatusFailed);
559 return false;
560 }
561 }
562 return result.Succeeded();
563 }
Johnny Chene9a56272012-08-09 23:09:42 +0000564};
565
566//-------------------------------------------------------------------------
567// CommandObjectWatchpointCommandList
568//-------------------------------------------------------------------------
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570class CommandObjectWatchpointCommandList : public CommandObjectParsed {
Johnny Chene9a56272012-08-09 23:09:42 +0000571public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
573 : CommandObjectParsed(interpreter, "list", "List the script or set of "
574 "commands to be executed when "
575 "the watchpoint is hit.",
576 nullptr) {
577 CommandArgumentEntry arg;
578 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000579
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 // Define the first (and only) variant of this arg.
581 wp_id_arg.arg_type = eArgTypeWatchpointID;
582 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 // There is only one variant this argument could be; put it into the
585 // argument entry.
586 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 // Push the data for the first argument into the m_arguments vector.
589 m_arguments.push_back(arg);
590 }
Johnny Chene9a56272012-08-09 23:09:42 +0000591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 ~CommandObjectWatchpointCommandList() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000593
594protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 bool DoExecute(Args &command, CommandReturnObject &result) override {
596 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 if (target == nullptr) {
599 result.AppendError("There is not a current executable; there are no "
600 "watchpoints for which to list commands");
601 result.SetStatus(eReturnStatusFailed);
602 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000603 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604
605 const WatchpointList &watchpoints = target->GetWatchpointList();
606 size_t num_watchpoints = watchpoints.GetSize();
607
608 if (num_watchpoints == 0) {
609 result.AppendError("No watchpoints exist for which to list commands");
610 result.SetStatus(eReturnStatusFailed);
611 return false;
612 }
613
614 if (command.GetArgumentCount() == 0) {
615 result.AppendError(
616 "No watchpoint specified for which to list the commands");
617 result.SetStatus(eReturnStatusFailed);
618 return false;
619 }
620
621 std::vector<uint32_t> valid_wp_ids;
622 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
623 valid_wp_ids)) {
624 result.AppendError("Invalid watchpoints specification.");
625 result.SetStatus(eReturnStatusFailed);
626 return false;
627 }
628
629 result.SetStatus(eReturnStatusSuccessFinishNoResult);
630 const size_t count = valid_wp_ids.size();
631 for (size_t i = 0; i < count; ++i) {
632 uint32_t cur_wp_id = valid_wp_ids.at(i);
633 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
634 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
635
636 if (wp) {
637 const WatchpointOptions *wp_options = wp->GetOptions();
638 if (wp_options) {
639 // Get the callback baton associated with the current watchpoint.
640 const Baton *baton = wp_options->GetBaton();
641 if (baton) {
642 result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
643 result.GetOutputStream().IndentMore();
644 baton->GetDescription(&result.GetOutputStream(),
645 eDescriptionLevelFull);
646 result.GetOutputStream().IndentLess();
647 } else {
648 result.AppendMessageWithFormat(
649 "Watchpoint %u does not have an associated command.\n",
650 cur_wp_id);
651 }
652 }
653 result.SetStatus(eReturnStatusSuccessFinishResult);
654 } else {
655 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
656 cur_wp_id);
657 result.SetStatus(eReturnStatusFailed);
658 }
659 }
660 }
661
662 return result.Succeeded();
663 }
Johnny Chene9a56272012-08-09 23:09:42 +0000664};
665
666//-------------------------------------------------------------------------
667// CommandObjectWatchpointCommand
668//-------------------------------------------------------------------------
669
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
671 CommandInterpreter &interpreter)
672 : CommandObjectMultiword(
673 interpreter, "command",
674 "Commands for adding, removing and examining LLDB commands "
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000675 "executed when the watchpoint is hit (watchpoint 'commands').",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
677 CommandObjectSP add_command_object(
678 new CommandObjectWatchpointCommandAdd(interpreter));
679 CommandObjectSP delete_command_object(
680 new CommandObjectWatchpointCommandDelete(interpreter));
681 CommandObjectSP list_command_object(
682 new CommandObjectWatchpointCommandList(interpreter));
Johnny Chene9a56272012-08-09 23:09:42 +0000683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 add_command_object->SetCommandName("watchpoint command add");
685 delete_command_object->SetCommandName("watchpoint command delete");
686 list_command_object->SetCommandName("watchpoint command list");
Johnny Chene9a56272012-08-09 23:09:42 +0000687
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 LoadSubCommand("add", add_command_object);
689 LoadSubCommand("delete", delete_command_object);
690 LoadSubCommand("list", list_command_object);
Johnny Chene9a56272012-08-09 23:09:42 +0000691}
692
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000693CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;