blob: cc6df7538ba310be97d1d9e2aba5517dcc59f1f5 [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 "CommandObjectWatchpointCommand.h"
17#include "CommandObjectWatchpoint.h"
Greg Clayton44d93782014-01-27 23:43:24 +000018#include "lldb/Core/IOHandler.h"
Johnny Chene9a56272012-08-09 23:09:42 +000019#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Breakpoint/Watchpoint.h"
24#include "lldb/Breakpoint/StoppointCallbackContext.h"
25#include "lldb/Core/State.h"
26
Johnny Chene9a56272012-08-09 23:09:42 +000027using namespace lldb;
28using namespace lldb_private;
29
30//-------------------------------------------------------------------------
31// CommandObjectWatchpointCommandAdd
32//-------------------------------------------------------------------------
33
Greg Clayton44d93782014-01-27 23:43:24 +000034class CommandObjectWatchpointCommandAdd :
35 public CommandObjectParsed,
36 public IOHandlerDelegateMultiline
Johnny Chene9a56272012-08-09 23:09:42 +000037{
38public:
Kate Stone7428a182016-07-14 22:03:10 +000039 CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
40 : CommandObjectParsed(
41 interpreter, "add",
42 "Add a set of LLDB commands to a watchpoint, to be executed whenever the watchpoint is hit.", nullptr),
43 IOHandlerDelegateMultiline("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Todd Fialae1cfbc72016-08-11 23:51:28 +000044 m_options()
Johnny Chene9a56272012-08-09 23:09:42 +000045 {
46 SetHelpLong (
Kate Stoneea671fb2015-07-14 05:48:36 +000047R"(
48General information about entering watchpoint commands
49------------------------------------------------------
50
51)" "This command will prompt for commands to be executed when the specified \
52watchpoint is hit. Each command is typed on its own line following the '> ' \
53prompt until 'DONE' is entered." R"(
54
55)" "Syntactic errors may not be detected when initially entered, and many \
56malformed commands can silently fail when executed. If your watchpoint commands \
57do not appear to be executing, double-check the command syntax." R"(
58
59)" "Note: You may enter any debugger command exactly as you would at the debugger \
60prompt. There is no limit to the number of commands supplied, but do NOT enter \
61more than one command per line." R"(
62
63Special information about PYTHON watchpoint commands
64----------------------------------------------------
65
66)" "You may enter either one or more lines of Python, including function \
67definitions or calls to functions that will have been imported by the time \
68the code executes. Single line watchpoint commands will be interpreted 'as is' \
69when the watchpoint is hit. Multiple lines of Python will be wrapped in a \
70generated function, and a call to the function will be attached to the watchpoint." R"(
71
72This auto-generated function is passed in three arguments:
73
74 frame: an lldb.SBFrame object for the frame which hit the watchpoint.
75
76 wp: the watchpoint that was hit.
77
78)" "When specifying a python function with the --python-function option, you need \
79to supply the function name prepended by the module name:" R"(
80
81 --python-function myutils.watchpoint_callback
82
83The function itself must have the following prototype:
84
85def watchpoint_callback(frame, wp):
86 # Your code goes here
87
88)" "The arguments are the same as the arguments passed to generated functions as \
89described above. Note that the global variable 'lldb.frame' will NOT be updated when \
90this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
91can get you to the thread via frame.GetThread(), the thread can get you to the \
92process via thread.GetProcess(), and the process can get you back to the target \
93via process.GetTarget()." R"(
94
95)" "Important Note: As Python code gets collected into functions, access to global \
96variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
97Python syntax, including indentation, when entering Python watchpoint commands." R"(
98
99Example Python one-line watchpoint command:
100
101(lldb) watchpoint command add -s python 1
102Enter your Python command(s). Type 'DONE' to end.
103> print "Hit this watchpoint!"
104> DONE
105
106As a convenience, this also works for a short Python one-liner:
107
108(lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
109(lldb) run
110Launching '.../a.out' (x86_64)
111(lldb) Fri Sep 10 12:17:45 2010
112Process 21778 Stopped
113* thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
114 36
115 37 int c(int val)
116 38 {
117 39 -> return val + 3;
118 40 }
119 41
120 42 int main (int argc, char const *argv[])
121
122Example multiple line Python watchpoint command, using function definition:
123
124(lldb) watchpoint command add -s python 1
125Enter your Python command(s). Type 'DONE' to end.
126> def watchpoint_output (wp_no):
127> out_string = "Hit watchpoint number " + repr (wp_no)
128> print out_string
129> return True
130> watchpoint_output (1)
131> DONE
132
133Example multiple line Python watchpoint command, using 'loose' Python:
134
135(lldb) watchpoint command add -s p 1
136Enter your Python command(s). Type 'DONE' to end.
137> global wp_count
138> wp_count = wp_count + 1
139> print "Hit this watchpoint " + repr(wp_count) + " times!"
140> DONE
141
142)" "In this case, since there is a reference to a global variable, \
143'wp_count', you will also need to make sure 'wp_count' exists and is \
144initialized:" R"(
145
146(lldb) script
147>>> wp_count = 0
148>>> quit()
149
150)" "Final Note: A warning that no watchpoint command was generated when there \
151are no syntax errors may indicate that a function was declared but never called."
152 );
Johnny Chene9a56272012-08-09 23:09:42 +0000153
154 CommandArgumentEntry arg;
155 CommandArgumentData wp_id_arg;
156
157 // Define the first (and only) variant of this arg.
158 wp_id_arg.arg_type = eArgTypeWatchpointID;
159 wp_id_arg.arg_repetition = eArgRepeatPlain;
160
161 // There is only one variant this argument could be; put it into the argument entry.
162 arg.push_back (wp_id_arg);
163
164 // Push the data for the first argument into the m_arguments vector.
165 m_arguments.push_back (arg);
166 }
167
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000168 ~CommandObjectWatchpointCommandAdd() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000169
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000170 Options *
171 GetOptions () override
Johnny Chene9a56272012-08-09 23:09:42 +0000172 {
173 return &m_options;
174 }
175
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000176 void
177 IOHandlerActivated (IOHandler &io_handler) override
Greg Clayton44d93782014-01-27 23:43:24 +0000178 {
179 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
180 if (output_sp)
181 {
182 output_sp->PutCString("Enter your debugger command(s). Type 'DONE' to end.\n");
183 output_sp->Flush();
184 }
185 }
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000186
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000187 void
188 IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override
Greg Clayton44d93782014-01-27 23:43:24 +0000189 {
190 io_handler.SetIsDone(true);
191
192 // The WatchpointOptions object is owned by the watchpoint or watchpoint location
193 WatchpointOptions *wp_options = (WatchpointOptions *) io_handler.GetUserData();
194 if (wp_options)
195 {
196 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000197 if (data_ap)
Greg Clayton44d93782014-01-27 23:43:24 +0000198 {
199 data_ap->user_source.SplitIntoLines(line);
200 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
201 wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp);
202 }
203 }
204 }
205
Johnny Chene9a56272012-08-09 23:09:42 +0000206 void
207 CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
208 CommandReturnObject &result)
209 {
Greg Clayton44d93782014-01-27 23:43:24 +0000210 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
211 *this, // IOHandlerDelegate
212 true, // Run IOHandler in async mode
213 wp_options); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
Johnny Chene9a56272012-08-09 23:09:42 +0000214 }
215
216 /// Set a one-liner as the callback for the watchpoint.
217 void
218 SetWatchpointCommandCallback (WatchpointOptions *wp_options,
219 const char *oneliner)
220 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000221 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
Johnny Chene9a56272012-08-09 23:09:42 +0000222
223 // It's necessary to set both user_source and script_source to the oneliner.
224 // The former is used to generate callback description (as in watchpoint command list)
225 // while the latter is used for Python to interpret during the actual callback.
226 data_ap->user_source.AppendString (oneliner);
227 data_ap->script_source.assign (oneliner);
228 data_ap->stop_on_error = m_options.m_stop_on_error;
229
230 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
231 wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp);
Johnny Chene9a56272012-08-09 23:09:42 +0000232 }
Johnny Chene9a56272012-08-09 23:09:42 +0000233
234 static bool
235 WatchpointOptionsCallbackFunction (void *baton,
236 StoppointCallbackContext *context,
237 lldb::user_id_t watch_id)
238 {
239 bool ret_value = true;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000240 if (baton == nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000241 return true;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000242
Johnny Chene9a56272012-08-09 23:09:42 +0000243 WatchpointOptions::CommandData *data = (WatchpointOptions::CommandData *) baton;
244 StringList &commands = data->user_source;
245
246 if (commands.GetSize() > 0)
247 {
248 ExecutionContext exe_ctx (context->exe_ctx_ref);
249 Target *target = exe_ctx.GetTargetPtr();
250 if (target)
251 {
252 CommandReturnObject result;
253 Debugger &debugger = target->GetDebugger();
254 // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously
255 // if the debugger is set up that way.
256
257 StreamSP output_stream (debugger.GetAsyncOutputStream());
258 StreamSP error_stream (debugger.GetAsyncErrorStream());
259 result.SetImmediateOutputStream (output_stream);
260 result.SetImmediateErrorStream (error_stream);
261
Jim Ingham26c7bf92014-10-11 00:38:27 +0000262 CommandInterpreterRunOptions options;
263 options.SetStopOnContinue (true);
264 options.SetStopOnError (data->stop_on_error);
265 options.SetEchoCommands (false);
266 options.SetPrintResults (true);
267 options.SetAddToHistory (false);
Johnny Chene9a56272012-08-09 23:09:42 +0000268
269 debugger.GetCommandInterpreter().HandleCommands (commands,
270 &exe_ctx,
Jim Ingham26c7bf92014-10-11 00:38:27 +0000271 options,
Johnny Chene9a56272012-08-09 23:09:42 +0000272 result);
273 result.GetImmediateOutputStream()->Flush();
274 result.GetImmediateErrorStream()->Flush();
275 }
276 }
277 return ret_value;
278 }
279
280 class CommandOptions : public Options
281 {
282 public:
Todd Fialae1cfbc72016-08-11 23:51:28 +0000283 CommandOptions() :
284 Options(),
Johnny Chene9a56272012-08-09 23:09:42 +0000285 m_use_commands (false),
286 m_use_script_language (false),
287 m_script_language (eScriptLanguageNone),
288 m_use_one_liner (false),
289 m_one_liner(),
290 m_function_name()
291 {
292 }
293
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000294 ~CommandOptions() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000295
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000296 Error
Todd Fialae1cfbc72016-08-11 23:51:28 +0000297 SetOptionValue(uint32_t option_idx, const char *option_arg,
298 ExecutionContext *execution_context) override
Johnny Chene9a56272012-08-09 23:09:42 +0000299 {
300 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000301 const int short_option = m_getopt_table[option_idx].val;
Johnny Chene9a56272012-08-09 23:09:42 +0000302
303 switch (short_option)
304 {
305 case 'o':
306 m_use_one_liner = true;
307 m_one_liner = option_arg;
308 break;
309
310 case 's':
311 m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg,
312 g_option_table[option_idx].enum_values,
313 eScriptLanguageNone,
314 error);
315
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000316 m_use_script_language =
317 (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault);
Johnny Chene9a56272012-08-09 23:09:42 +0000318 break;
319
320 case 'e':
321 {
322 bool success = false;
323 m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
324 if (!success)
325 error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg);
326 }
327 break;
328
329 case 'F':
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000330 m_use_one_liner = false;
331 m_use_script_language = true;
332 m_function_name.assign(option_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000333 break;
334
335 default:
336 break;
337 }
338 return error;
339 }
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000340
Johnny Chene9a56272012-08-09 23:09:42 +0000341 void
Todd Fialae1cfbc72016-08-11 23:51:28 +0000342 OptionParsingStarting(ExecutionContext *execution_context) override
Johnny Chene9a56272012-08-09 23:09:42 +0000343 {
344 m_use_commands = true;
345 m_use_script_language = false;
346 m_script_language = eScriptLanguageNone;
347
348 m_use_one_liner = false;
349 m_stop_on_error = true;
350 m_one_liner.clear();
351 m_function_name.clear();
352 }
353
354 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000355 GetDefinitions () override
Johnny Chene9a56272012-08-09 23:09:42 +0000356 {
357 return g_option_table;
358 }
359
360 // Options table: Required for subclasses of Options.
361
362 static OptionDefinition g_option_table[];
363
364 // Instance variables to hold the values for command options.
365
366 bool m_use_commands;
367 bool m_use_script_language;
368 lldb::ScriptLanguage m_script_language;
369
370 // Instance variables to hold the values for one_liner options.
371 bool m_use_one_liner;
372 std::string m_one_liner;
373 bool m_stop_on_error;
374 std::string m_function_name;
375 };
376
377protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000378 bool
379 DoExecute (Args& command, CommandReturnObject &result) override
Johnny Chene9a56272012-08-09 23:09:42 +0000380 {
381 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
382
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000383 if (target == nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000384 {
385 result.AppendError ("There is not a current executable; there are no watchpoints to which to add commands");
386 result.SetStatus (eReturnStatusFailed);
387 return false;
388 }
389
390 const WatchpointList &watchpoints = target->GetWatchpointList();
391 size_t num_watchpoints = watchpoints.GetSize();
392
393 if (num_watchpoints == 0)
394 {
395 result.AppendError ("No watchpoints exist to have commands added");
396 result.SetStatus (eReturnStatusFailed);
397 return false;
398 }
399
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000400 if (!m_options.m_use_script_language && !m_options.m_function_name.empty())
Johnny Chene9a56272012-08-09 23:09:42 +0000401 {
402 result.AppendError ("need to enable scripting to have a function run as a watchpoint command");
403 result.SetStatus (eReturnStatusFailed);
404 return false;
405 }
406
407 std::vector<uint32_t> valid_wp_ids;
Jim Inghamb0b45132013-07-02 02:09:46 +0000408 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids))
Johnny Chene9a56272012-08-09 23:09:42 +0000409 {
410 result.AppendError("Invalid watchpoints specification.");
411 result.SetStatus(eReturnStatusFailed);
412 return false;
413 }
414
415 result.SetStatus(eReturnStatusSuccessFinishNoResult);
416 const size_t count = valid_wp_ids.size();
417 for (size_t i = 0; i < count; ++i)
418 {
419 uint32_t cur_wp_id = valid_wp_ids.at (i);
420 if (cur_wp_id != LLDB_INVALID_WATCH_ID)
421 {
422 Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get();
423 // Sanity check wp first.
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000424 if (wp == nullptr) continue;
Johnny Chene9a56272012-08-09 23:09:42 +0000425
426 WatchpointOptions *wp_options = wp->GetOptions();
427 // Skip this watchpoint if wp_options is not good.
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000428 if (wp_options == nullptr) continue;
Johnny Chene9a56272012-08-09 23:09:42 +0000429
430 // If we are using script language, get the script interpreter
431 // in order to set or collect command callback. Otherwise, call
432 // the methods associated with this object.
433 if (m_options.m_use_script_language)
434 {
435 // Special handling for one-liner specified inline.
436 if (m_options.m_use_one_liner)
437 {
438 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options,
439 m_options.m_one_liner.c_str());
440 }
441 // Special handling for using a Python function by name
442 // instead of extending the watchpoint callback data structures, we just automatize
443 // what the user would do manually: make their watchpoint command be a function call
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000444 else if (!m_options.m_function_name.empty())
Johnny Chene9a56272012-08-09 23:09:42 +0000445 {
446 std::string oneliner(m_options.m_function_name);
447 oneliner += "(frame, wp, internal_dict)";
448 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options,
449 oneliner.c_str());
450 }
451 else
452 {
453 m_interpreter.GetScriptInterpreter()->CollectDataForWatchpointCommandCallback (wp_options,
454 result);
455 }
456 }
457 else
458 {
459 // Special handling for one-liner specified inline.
460 if (m_options.m_use_one_liner)
461 SetWatchpointCommandCallback (wp_options,
462 m_options.m_one_liner.c_str());
463 else
464 CollectDataForWatchpointCommandCallback (wp_options,
465 result);
466 }
467 }
468 }
469
470 return result.Succeeded();
471 }
472
473private:
474 CommandOptions m_options;
Johnny Chene9a56272012-08-09 23:09:42 +0000475};
476
Johnny Chene9a56272012-08-09 23:09:42 +0000477// FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting
478// language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper.
479
480static OptionEnumValueElement
481g_script_option_enumeration[4] =
482{
483 { eScriptLanguageNone, "command", "Commands are in the lldb command interpreter language"},
484 { eScriptLanguagePython, "python", "Commands are in the Python language."},
485 { eSortOrderByName, "default-script", "Commands are in the default scripting language."},
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000486 { 0, nullptr, nullptr }
Johnny Chene9a56272012-08-09 23:09:42 +0000487};
488
489OptionDefinition
490CommandObjectWatchpointCommandAdd::CommandOptions::g_option_table[] =
491{
Kate Stoneac9c3a62016-08-26 23:28:47 +0000492 // clang-format off
493 {LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner, "Specify a one-line watchpoint command inline. Be sure to surround it with quotes."},
494 {LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Specify whether watchpoint command execution should terminate on error."},
495 {LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone, "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."},
496 {LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, 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."},
497 {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
498 // clang-format on
Johnny Chene9a56272012-08-09 23:09:42 +0000499};
500
501//-------------------------------------------------------------------------
502// CommandObjectWatchpointCommandDelete
503//-------------------------------------------------------------------------
504
505class CommandObjectWatchpointCommandDelete : public CommandObjectParsed
506{
507public:
508 CommandObjectWatchpointCommandDelete (CommandInterpreter &interpreter) :
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000509 CommandObjectParsed(interpreter,
510 "delete",
511 "Delete the set of commands from a watchpoint.",
512 nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000513 {
514 CommandArgumentEntry arg;
515 CommandArgumentData wp_id_arg;
516
517 // Define the first (and only) variant of this arg.
518 wp_id_arg.arg_type = eArgTypeWatchpointID;
519 wp_id_arg.arg_repetition = eArgRepeatPlain;
520
521 // There is only one variant this argument could be; put it into the argument entry.
522 arg.push_back (wp_id_arg);
523
524 // Push the data for the first argument into the m_arguments vector.
525 m_arguments.push_back (arg);
526 }
527
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000528 ~CommandObjectWatchpointCommandDelete() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000529
530protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000531 bool
532 DoExecute (Args& command, CommandReturnObject &result) override
Johnny Chene9a56272012-08-09 23:09:42 +0000533 {
534 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
535
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000536 if (target == nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000537 {
538 result.AppendError ("There is not a current executable; there are no watchpoints from which to delete commands");
539 result.SetStatus (eReturnStatusFailed);
540 return false;
541 }
542
543 const WatchpointList &watchpoints = target->GetWatchpointList();
544 size_t num_watchpoints = watchpoints.GetSize();
545
546 if (num_watchpoints == 0)
547 {
548 result.AppendError ("No watchpoints exist to have commands deleted");
549 result.SetStatus (eReturnStatusFailed);
550 return false;
551 }
552
553 if (command.GetArgumentCount() == 0)
554 {
555 result.AppendError ("No watchpoint specified from which to delete the commands");
556 result.SetStatus (eReturnStatusFailed);
557 return false;
558 }
559
560 std::vector<uint32_t> valid_wp_ids;
Jim Inghamb0b45132013-07-02 02:09:46 +0000561 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids))
Johnny Chene9a56272012-08-09 23:09:42 +0000562 {
563 result.AppendError("Invalid watchpoints specification.");
564 result.SetStatus(eReturnStatusFailed);
565 return false;
566 }
567
568 result.SetStatus(eReturnStatusSuccessFinishNoResult);
569 const size_t count = valid_wp_ids.size();
570 for (size_t i = 0; i < count; ++i)
571 {
572 uint32_t cur_wp_id = valid_wp_ids.at (i);
573 if (cur_wp_id != LLDB_INVALID_WATCH_ID)
574 {
575 Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get();
576 if (wp)
577 wp->ClearCallback();
578 }
579 else
580 {
581 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
582 cur_wp_id);
583 result.SetStatus (eReturnStatusFailed);
584 return false;
585 }
586 }
587 return result.Succeeded();
588 }
589};
590
591//-------------------------------------------------------------------------
592// CommandObjectWatchpointCommandList
593//-------------------------------------------------------------------------
594
595class CommandObjectWatchpointCommandList : public CommandObjectParsed
596{
597public:
598 CommandObjectWatchpointCommandList (CommandInterpreter &interpreter) :
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000599 CommandObjectParsed(interpreter,
600 "list",
601 "List the script or set of commands to be executed when the watchpoint is hit.",
602 nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000603 {
604 CommandArgumentEntry arg;
605 CommandArgumentData wp_id_arg;
606
607 // Define the first (and only) variant of this arg.
608 wp_id_arg.arg_type = eArgTypeWatchpointID;
609 wp_id_arg.arg_repetition = eArgRepeatPlain;
610
611 // There is only one variant this argument could be; put it into the argument entry.
612 arg.push_back (wp_id_arg);
613
614 // Push the data for the first argument into the m_arguments vector.
615 m_arguments.push_back (arg);
616 }
617
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000618 ~CommandObjectWatchpointCommandList() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000619
620protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000621 bool
622 DoExecute (Args& command, CommandReturnObject &result) override
Johnny Chene9a56272012-08-09 23:09:42 +0000623 {
624 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
625
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000626 if (target == nullptr)
Johnny Chene9a56272012-08-09 23:09:42 +0000627 {
628 result.AppendError ("There is not a current executable; there are no watchpoints for which to list commands");
629 result.SetStatus (eReturnStatusFailed);
630 return false;
631 }
632
633 const WatchpointList &watchpoints = target->GetWatchpointList();
634 size_t num_watchpoints = watchpoints.GetSize();
635
636 if (num_watchpoints == 0)
637 {
638 result.AppendError ("No watchpoints exist for which to list commands");
639 result.SetStatus (eReturnStatusFailed);
640 return false;
641 }
642
643 if (command.GetArgumentCount() == 0)
644 {
645 result.AppendError ("No watchpoint specified for which to list the commands");
646 result.SetStatus (eReturnStatusFailed);
647 return false;
648 }
649
650 std::vector<uint32_t> valid_wp_ids;
Jim Inghamb0b45132013-07-02 02:09:46 +0000651 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids))
Johnny Chene9a56272012-08-09 23:09:42 +0000652 {
653 result.AppendError("Invalid watchpoints specification.");
654 result.SetStatus(eReturnStatusFailed);
655 return false;
656 }
657
658 result.SetStatus(eReturnStatusSuccessFinishNoResult);
659 const size_t count = valid_wp_ids.size();
660 for (size_t i = 0; i < count; ++i)
661 {
662 uint32_t cur_wp_id = valid_wp_ids.at (i);
663 if (cur_wp_id != LLDB_INVALID_WATCH_ID)
664 {
665 Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get();
666
667 if (wp)
668 {
669 const WatchpointOptions *wp_options = wp->GetOptions();
670 if (wp_options)
671 {
672 // Get the callback baton associated with the current watchpoint.
673 const Baton *baton = wp_options->GetBaton();
674 if (baton)
675 {
676 result.GetOutputStream().Printf ("Watchpoint %u:\n", cur_wp_id);
677 result.GetOutputStream().IndentMore ();
678 baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull);
679 result.GetOutputStream().IndentLess ();
680 }
681 else
682 {
683 result.AppendMessageWithFormat ("Watchpoint %u does not have an associated command.\n",
684 cur_wp_id);
685 }
686 }
687 result.SetStatus (eReturnStatusSuccessFinishResult);
688 }
689 else
690 {
691 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
692 result.SetStatus (eReturnStatusFailed);
693 }
694 }
695 }
696
697 return result.Succeeded();
698 }
699};
700
701//-------------------------------------------------------------------------
702// CommandObjectWatchpointCommand
703//-------------------------------------------------------------------------
704
Kate Stone7428a182016-07-14 22:03:10 +0000705CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(CommandInterpreter &interpreter)
706 : CommandObjectMultiword(interpreter, "command", "Commands for adding, removing and examining LLDB commands "
707 "executed when the watchpoint is hit (watchpoint 'commmands').",
708 "command <sub-command> [<sub-command-options>] <watchpoint-id>")
Johnny Chene9a56272012-08-09 23:09:42 +0000709{
Johnny Chene9a56272012-08-09 23:09:42 +0000710 CommandObjectSP add_command_object (new CommandObjectWatchpointCommandAdd (interpreter));
711 CommandObjectSP delete_command_object (new CommandObjectWatchpointCommandDelete (interpreter));
712 CommandObjectSP list_command_object (new CommandObjectWatchpointCommandList (interpreter));
713
714 add_command_object->SetCommandName ("watchpoint command add");
715 delete_command_object->SetCommandName ("watchpoint command delete");
716 list_command_object->SetCommandName ("watchpoint command list");
717
Greg Clayton03da4cc2013-04-19 21:31:16 +0000718 LoadSubCommand ("add", add_command_object);
719 LoadSubCommand ("delete", delete_command_object);
720 LoadSubCommand ("list", list_command_object);
Johnny Chene9a56272012-08-09 23:09:42 +0000721}
722
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000723CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;