blob: 135b9c790f09a9f234ea9b0a8f3b502272e73299 [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
41static OptionEnumValueElement g_script_option_enumeration[4] = {
42 {eScriptLanguageNone, "command",
43 "Commands are in the lldb command interpreter language"},
44 {eScriptLanguagePython, "python", "Commands are in the Python language."},
45 {eSortOrderByName, "default-script",
46 "Commands are in the default scripting language."},
47 {0, nullptr, nullptr}};
48
49static OptionDefinition g_watchpoint_command_add_options[] = {
50 // clang-format off
51 { 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." },
52 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Specify whether watchpoint command execution should terminate on error." },
53 { 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." },
54 { 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." }
55 // clang-format on
56};
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
59 public IOHandlerDelegateMultiline {
Johnny Chene9a56272012-08-09 23:09:42 +000060public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
62 : CommandObjectParsed(interpreter, "add",
63 "Add a set of LLDB commands to a watchpoint, to be "
64 "executed whenever the watchpoint is hit.",
65 nullptr),
66 IOHandlerDelegateMultiline("DONE",
67 IOHandlerDelegate::Completion::LLDBCommand),
68 m_options() {
69 SetHelpLong(
70 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000071General information about entering watchpoint commands
72------------------------------------------------------
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074)"
75 "This command will prompt for commands to be executed when the specified \
Kate Stoneea671fb2015-07-14 05:48:36 +000076watchpoint is hit. Each command is typed on its own line following the '> ' \
Kate Stoneb9c1b512016-09-06 20:57:50 +000077prompt until 'DONE' is entered."
78 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080)"
81 "Syntactic errors may not be detected when initially entered, and many \
Kate Stoneea671fb2015-07-14 05:48:36 +000082malformed commands can silently fail when executed. If your watchpoint commands \
Kate Stoneb9c1b512016-09-06 20:57:50 +000083do not appear to be executing, double-check the command syntax."
84 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086)"
87 "Note: You may enter any debugger command exactly as you would at the debugger \
Kate Stoneea671fb2015-07-14 05:48:36 +000088prompt. There is no limit to the number of commands supplied, but do NOT enter \
Kate Stoneb9c1b512016-09-06 20:57:50 +000089more than one command per line."
90 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000091
92Special information about PYTHON watchpoint commands
93----------------------------------------------------
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095)"
96 "You may enter either one or more lines of Python, including function \
Kate Stoneea671fb2015-07-14 05:48:36 +000097definitions or calls to functions that will have been imported by the time \
98the code executes. Single line watchpoint commands will be interpreted 'as is' \
99when the watchpoint is hit. Multiple lines of Python will be wrapped in a \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100generated function, and a call to the function will be attached to the watchpoint."
101 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000102
103This auto-generated function is passed in three arguments:
104
105 frame: an lldb.SBFrame object for the frame which hit the watchpoint.
106
107 wp: the watchpoint that was hit.
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109)"
110 "When specifying a python function with the --python-function option, you need \
111to supply the function name prepended by the module name:"
112 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000113
114 --python-function myutils.watchpoint_callback
115
116The function itself must have the following prototype:
117
118def watchpoint_callback(frame, wp):
119 # Your code goes here
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121)"
122 "The arguments are the same as the arguments passed to generated functions as \
Kate Stoneea671fb2015-07-14 05:48:36 +0000123described above. Note that the global variable 'lldb.frame' will NOT be updated when \
124this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
125can get you to the thread via frame.GetThread(), the thread can get you to the \
126process via thread.GetProcess(), and the process can get you back to the target \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127via process.GetTarget()."
128 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130)"
131 "Important Note: As Python code gets collected into functions, access to global \
Kate Stoneea671fb2015-07-14 05:48:36 +0000132variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133Python syntax, including indentation, when entering Python watchpoint commands."
134 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000135
136Example Python one-line watchpoint command:
137
138(lldb) watchpoint command add -s python 1
139Enter your Python command(s). Type 'DONE' to end.
140> print "Hit this watchpoint!"
141> DONE
142
143As a convenience, this also works for a short Python one-liner:
144
145(lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
146(lldb) run
147Launching '.../a.out' (x86_64)
148(lldb) Fri Sep 10 12:17:45 2010
149Process 21778 Stopped
150* thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
151 36
152 37 int c(int val)
153 38 {
154 39 -> return val + 3;
155 40 }
156 41
157 42 int main (int argc, char const *argv[])
158
159Example multiple line Python watchpoint command, using function definition:
160
161(lldb) watchpoint command add -s python 1
162Enter your Python command(s). Type 'DONE' to end.
163> def watchpoint_output (wp_no):
164> out_string = "Hit watchpoint number " + repr (wp_no)
165> print out_string
166> return True
167> watchpoint_output (1)
168> DONE
169
170Example multiple line Python watchpoint command, using 'loose' Python:
171
172(lldb) watchpoint command add -s p 1
173Enter your Python command(s). Type 'DONE' to end.
174> global wp_count
175> wp_count = wp_count + 1
176> print "Hit this watchpoint " + repr(wp_count) + " times!"
177> DONE
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179)"
180 "In this case, since there is a reference to a global variable, \
Kate Stoneea671fb2015-07-14 05:48:36 +0000181'wp_count', you will also need to make sure 'wp_count' exists and is \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182initialized:"
183 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000184
185(lldb) script
186>>> wp_count = 0
187>>> quit()
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189)"
190 "Final Note: A warning that no watchpoint command was generated when there \
191are no syntax errors may indicate that a function was declared but never called.");
Johnny Chene9a56272012-08-09 23:09:42 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 CommandArgumentEntry arg;
194 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 // Define the first (and only) variant of this arg.
197 wp_id_arg.arg_type = eArgTypeWatchpointID;
198 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 // There is only one variant this argument could be; put it into the
201 // argument entry.
202 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 // Push the data for the first argument into the m_arguments vector.
205 m_arguments.push_back(arg);
206 }
207
208 ~CommandObjectWatchpointCommandAdd() override = default;
209
210 Options *GetOptions() override { return &m_options; }
211
212 void IOHandlerActivated(IOHandler &io_handler) override {
213 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
214 if (output_sp) {
215 output_sp->PutCString(
216 "Enter your debugger command(s). Type 'DONE' to end.\n");
217 output_sp->Flush();
218 }
219 }
220
221 void IOHandlerInputComplete(IOHandler &io_handler,
222 std::string &line) override {
223 io_handler.SetIsDone(true);
224
225 // The WatchpointOptions object is owned by the watchpoint or watchpoint
226 // location
227 WatchpointOptions *wp_options =
228 (WatchpointOptions *)io_handler.GetUserData();
229 if (wp_options) {
230 std::unique_ptr<WatchpointOptions::CommandData> data_ap(
231 new WatchpointOptions::CommandData());
232 if (data_ap) {
233 data_ap->user_source.SplitIntoLines(line);
Zachary Turner4e4fbe82016-09-13 17:53:38 +0000234 auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
235 std::move(data_ap));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
237 }
238 }
239 }
240
241 void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
242 CommandReturnObject &result) {
243 m_interpreter.GetLLDBCommandsFromIOHandler(
244 "> ", // Prompt
245 *this, // IOHandlerDelegate
246 true, // Run IOHandler in async mode
247 wp_options); // Baton for the "io_handler" that will be passed back into
248 // our IOHandlerDelegate functions
249 }
250
251 /// Set a one-liner as the callback for the watchpoint.
252 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
253 const char *oneliner) {
254 std::unique_ptr<WatchpointOptions::CommandData> data_ap(
255 new WatchpointOptions::CommandData());
256
Adrian Prantl05097242018-04-30 16:49:04 +0000257 // It's necessary to set both user_source and script_source to the
258 // oneliner. The former is used to generate callback description (as in
259 // watchpoint command list) while the latter is used for Python to
260 // interpret during the actual callback.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 data_ap->user_source.AppendString(oneliner);
262 data_ap->script_source.assign(oneliner);
263 data_ap->stop_on_error = m_options.m_stop_on_error;
264
Zachary Turner4e4fbe82016-09-13 17:53:38 +0000265 auto baton_sp =
266 std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_ap));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
268 }
269
270 static bool
271 WatchpointOptionsCallbackFunction(void *baton,
272 StoppointCallbackContext *context,
273 lldb::user_id_t watch_id) {
274 bool ret_value = true;
275 if (baton == nullptr)
276 return true;
277
278 WatchpointOptions::CommandData *data =
279 (WatchpointOptions::CommandData *)baton;
280 StringList &commands = data->user_source;
281
282 if (commands.GetSize() > 0) {
283 ExecutionContext exe_ctx(context->exe_ctx_ref);
284 Target *target = exe_ctx.GetTargetPtr();
285 if (target) {
286 CommandReturnObject result;
287 Debugger &debugger = target->GetDebugger();
288 // Rig up the results secondary output stream to the debugger's, so the
Adrian Prantl05097242018-04-30 16:49:04 +0000289 // output will come out synchronously if the debugger is set up that
290 // way.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291
292 StreamSP output_stream(debugger.GetAsyncOutputStream());
293 StreamSP error_stream(debugger.GetAsyncErrorStream());
294 result.SetImmediateOutputStream(output_stream);
295 result.SetImmediateErrorStream(error_stream);
296
297 CommandInterpreterRunOptions options;
298 options.SetStopOnContinue(true);
299 options.SetStopOnError(data->stop_on_error);
300 options.SetEchoCommands(false);
301 options.SetPrintResults(true);
302 options.SetAddToHistory(false);
303
304 debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
305 options, result);
306 result.GetImmediateOutputStream()->Flush();
307 result.GetImmediateErrorStream()->Flush();
308 }
309 }
310 return ret_value;
311 }
312
313 class CommandOptions : public Options {
314 public:
315 CommandOptions()
316 : Options(), m_use_commands(false), m_use_script_language(false),
317 m_script_language(eScriptLanguageNone), m_use_one_liner(false),
318 m_one_liner(), m_function_name() {}
319
320 ~CommandOptions() override = default;
321
Zachary Turner97206d52017-05-12 04:51:55 +0000322 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
323 ExecutionContext *execution_context) override {
324 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 const int short_option = m_getopt_table[option_idx].val;
326
327 switch (short_option) {
328 case 'o':
329 m_use_one_liner = true;
330 m_one_liner = option_arg;
331 break;
332
333 case 's':
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000334 m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +0000335 option_arg, GetDefinitions()[option_idx].enum_values,
336 eScriptLanguageNone, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337
338 m_use_script_language = (m_script_language == eScriptLanguagePython ||
339 m_script_language == eScriptLanguageDefault);
340 break;
341
342 case 'e': {
343 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000344 m_stop_on_error =
345 OptionArgParser::ToBoolean(option_arg, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (!success)
347 error.SetErrorStringWithFormat(
Zachary Turnerfe114832016-11-12 16:56:47 +0000348 "invalid value for stop-on-error: \"%s\"",
349 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 } break;
351
352 case 'F':
353 m_use_one_liner = false;
354 m_use_script_language = true;
355 m_function_name.assign(option_arg);
356 break;
357
358 default:
359 break;
360 }
361 return error;
Johnny Chene9a56272012-08-09 23:09:42 +0000362 }
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 void OptionParsingStarting(ExecutionContext *execution_context) override {
365 m_use_commands = true;
366 m_use_script_language = false;
367 m_script_language = eScriptLanguageNone;
Johnny Chene9a56272012-08-09 23:09:42 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 m_use_one_liner = false;
370 m_stop_on_error = true;
371 m_one_liner.clear();
372 m_function_name.clear();
Johnny Chene9a56272012-08-09 23:09:42 +0000373 }
374
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000375 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000376 return llvm::makeArrayRef(g_watchpoint_command_add_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000377 }
Johnny Chene9a56272012-08-09 23:09:42 +0000378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 // Instance variables to hold the values for command options.
Johnny Chene9a56272012-08-09 23:09:42 +0000380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 bool m_use_commands;
382 bool m_use_script_language;
383 lldb::ScriptLanguage m_script_language;
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 // Instance variables to hold the values for one_liner options.
386 bool m_use_one_liner;
387 std::string m_one_liner;
388 bool m_stop_on_error;
389 std::string m_function_name;
390 };
Johnny Chene9a56272012-08-09 23:09:42 +0000391
392protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 bool DoExecute(Args &command, CommandReturnObject &result) override {
394 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 if (target == nullptr) {
397 result.AppendError("There is not a current executable; there are no "
398 "watchpoints to which to add commands");
399 result.SetStatus(eReturnStatusFailed);
400 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000401 }
402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 const WatchpointList &watchpoints = target->GetWatchpointList();
404 size_t num_watchpoints = watchpoints.GetSize();
405
406 if (num_watchpoints == 0) {
407 result.AppendError("No watchpoints exist to have commands added");
408 result.SetStatus(eReturnStatusFailed);
409 return false;
410 }
411
412 if (!m_options.m_use_script_language &&
413 !m_options.m_function_name.empty()) {
414 result.AppendError("need to enable scripting to have a function run as a "
415 "watchpoint command");
416 result.SetStatus(eReturnStatusFailed);
417 return false;
418 }
419
420 std::vector<uint32_t> valid_wp_ids;
421 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
422 valid_wp_ids)) {
423 result.AppendError("Invalid watchpoints specification.");
424 result.SetStatus(eReturnStatusFailed);
425 return false;
426 }
427
428 result.SetStatus(eReturnStatusSuccessFinishNoResult);
429 const size_t count = valid_wp_ids.size();
430 for (size_t i = 0; i < count; ++i) {
431 uint32_t cur_wp_id = valid_wp_ids.at(i);
432 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
433 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
434 // Sanity check wp first.
435 if (wp == nullptr)
436 continue;
437
438 WatchpointOptions *wp_options = wp->GetOptions();
439 // Skip this watchpoint if wp_options is not good.
440 if (wp_options == nullptr)
441 continue;
442
Adrian Prantl05097242018-04-30 16:49:04 +0000443 // If we are using script language, get the script interpreter in order
444 // to set or collect command callback. Otherwise, call the methods
445 // associated with this object.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 if (m_options.m_use_script_language) {
447 // Special handling for one-liner specified inline.
448 if (m_options.m_use_one_liner) {
449 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
450 wp_options, m_options.m_one_liner.c_str());
451 }
Adrian Prantl05097242018-04-30 16:49:04 +0000452 // Special handling for using a Python function by name instead of
453 // extending the watchpoint callback data structures, we just
454 // automatize what the user would do manually: make their watchpoint
455 // command be a function call
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 else if (!m_options.m_function_name.empty()) {
457 std::string oneliner(m_options.m_function_name);
458 oneliner += "(frame, wp, internal_dict)";
459 m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
460 wp_options, oneliner.c_str());
461 } else {
462 m_interpreter.GetScriptInterpreter()
463 ->CollectDataForWatchpointCommandCallback(wp_options, result);
464 }
465 } else {
466 // Special handling for one-liner specified inline.
467 if (m_options.m_use_one_liner)
468 SetWatchpointCommandCallback(wp_options,
469 m_options.m_one_liner.c_str());
470 else
471 CollectDataForWatchpointCommandCallback(wp_options, result);
472 }
473 }
474 }
475
476 return result.Succeeded();
477 }
478
Johnny Chene9a56272012-08-09 23:09:42 +0000479private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 CommandOptions m_options;
Johnny Chene9a56272012-08-09 23:09:42 +0000481};
482
Johnny Chene9a56272012-08-09 23:09:42 +0000483//-------------------------------------------------------------------------
484// CommandObjectWatchpointCommandDelete
485//-------------------------------------------------------------------------
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
Johnny Chene9a56272012-08-09 23:09:42 +0000488public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
490 : CommandObjectParsed(interpreter, "delete",
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000491 "Delete the set of commands from a watchpoint.",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 nullptr) {
493 CommandArgumentEntry arg;
494 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 // Define the first (and only) variant of this arg.
497 wp_id_arg.arg_type = eArgTypeWatchpointID;
498 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 // There is only one variant this argument could be; put it into the
501 // argument entry.
502 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 // Push the data for the first argument into the m_arguments vector.
505 m_arguments.push_back(arg);
506 }
Johnny Chene9a56272012-08-09 23:09:42 +0000507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 ~CommandObjectWatchpointCommandDelete() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000509
510protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 bool DoExecute(Args &command, CommandReturnObject &result) override {
512 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 if (target == nullptr) {
515 result.AppendError("There is not a current executable; there are no "
516 "watchpoints from which to delete commands");
517 result.SetStatus(eReturnStatusFailed);
518 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520
521 const WatchpointList &watchpoints = target->GetWatchpointList();
522 size_t num_watchpoints = watchpoints.GetSize();
523
524 if (num_watchpoints == 0) {
525 result.AppendError("No watchpoints exist to have commands deleted");
526 result.SetStatus(eReturnStatusFailed);
527 return false;
528 }
529
530 if (command.GetArgumentCount() == 0) {
531 result.AppendError(
532 "No watchpoint specified from which to delete the commands");
533 result.SetStatus(eReturnStatusFailed);
534 return false;
535 }
536
537 std::vector<uint32_t> valid_wp_ids;
538 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
539 valid_wp_ids)) {
540 result.AppendError("Invalid watchpoints specification.");
541 result.SetStatus(eReturnStatusFailed);
542 return false;
543 }
544
545 result.SetStatus(eReturnStatusSuccessFinishNoResult);
546 const size_t count = valid_wp_ids.size();
547 for (size_t i = 0; i < count; ++i) {
548 uint32_t cur_wp_id = valid_wp_ids.at(i);
549 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
550 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
551 if (wp)
552 wp->ClearCallback();
553 } else {
554 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
555 result.SetStatus(eReturnStatusFailed);
556 return false;
557 }
558 }
559 return result.Succeeded();
560 }
Johnny Chene9a56272012-08-09 23:09:42 +0000561};
562
563//-------------------------------------------------------------------------
564// CommandObjectWatchpointCommandList
565//-------------------------------------------------------------------------
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567class CommandObjectWatchpointCommandList : public CommandObjectParsed {
Johnny Chene9a56272012-08-09 23:09:42 +0000568public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
570 : CommandObjectParsed(interpreter, "list", "List the script or set of "
571 "commands to be executed when "
572 "the watchpoint is hit.",
573 nullptr) {
574 CommandArgumentEntry arg;
575 CommandArgumentData wp_id_arg;
Johnny Chene9a56272012-08-09 23:09:42 +0000576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 // Define the first (and only) variant of this arg.
578 wp_id_arg.arg_type = eArgTypeWatchpointID;
579 wp_id_arg.arg_repetition = eArgRepeatPlain;
Johnny Chene9a56272012-08-09 23:09:42 +0000580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 // There is only one variant this argument could be; put it into the
582 // argument entry.
583 arg.push_back(wp_id_arg);
Johnny Chene9a56272012-08-09 23:09:42 +0000584
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 // Push the data for the first argument into the m_arguments vector.
586 m_arguments.push_back(arg);
587 }
Johnny Chene9a56272012-08-09 23:09:42 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 ~CommandObjectWatchpointCommandList() override = default;
Johnny Chene9a56272012-08-09 23:09:42 +0000590
591protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 bool DoExecute(Args &command, CommandReturnObject &result) override {
593 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chene9a56272012-08-09 23:09:42 +0000594
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 if (target == nullptr) {
596 result.AppendError("There is not a current executable; there are no "
597 "watchpoints for which to list commands");
598 result.SetStatus(eReturnStatusFailed);
599 return false;
Johnny Chene9a56272012-08-09 23:09:42 +0000600 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601
602 const WatchpointList &watchpoints = target->GetWatchpointList();
603 size_t num_watchpoints = watchpoints.GetSize();
604
605 if (num_watchpoints == 0) {
606 result.AppendError("No watchpoints exist for which to list commands");
607 result.SetStatus(eReturnStatusFailed);
608 return false;
609 }
610
611 if (command.GetArgumentCount() == 0) {
612 result.AppendError(
613 "No watchpoint specified for which to list the commands");
614 result.SetStatus(eReturnStatusFailed);
615 return false;
616 }
617
618 std::vector<uint32_t> valid_wp_ids;
619 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
620 valid_wp_ids)) {
621 result.AppendError("Invalid watchpoints specification.");
622 result.SetStatus(eReturnStatusFailed);
623 return false;
624 }
625
626 result.SetStatus(eReturnStatusSuccessFinishNoResult);
627 const size_t count = valid_wp_ids.size();
628 for (size_t i = 0; i < count; ++i) {
629 uint32_t cur_wp_id = valid_wp_ids.at(i);
630 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
631 Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
632
633 if (wp) {
634 const WatchpointOptions *wp_options = wp->GetOptions();
635 if (wp_options) {
636 // Get the callback baton associated with the current watchpoint.
637 const Baton *baton = wp_options->GetBaton();
638 if (baton) {
639 result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
640 result.GetOutputStream().IndentMore();
641 baton->GetDescription(&result.GetOutputStream(),
642 eDescriptionLevelFull);
643 result.GetOutputStream().IndentLess();
644 } else {
645 result.AppendMessageWithFormat(
646 "Watchpoint %u does not have an associated command.\n",
647 cur_wp_id);
648 }
649 }
650 result.SetStatus(eReturnStatusSuccessFinishResult);
651 } else {
652 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
653 cur_wp_id);
654 result.SetStatus(eReturnStatusFailed);
655 }
656 }
657 }
658
659 return result.Succeeded();
660 }
Johnny Chene9a56272012-08-09 23:09:42 +0000661};
662
663//-------------------------------------------------------------------------
664// CommandObjectWatchpointCommand
665//-------------------------------------------------------------------------
666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
668 CommandInterpreter &interpreter)
669 : CommandObjectMultiword(
670 interpreter, "command",
671 "Commands for adding, removing and examining LLDB commands "
Bruce Mitchener4ebdee02018-05-29 09:10:46 +0000672 "executed when the watchpoint is hit (watchpoint 'commands').",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
674 CommandObjectSP add_command_object(
675 new CommandObjectWatchpointCommandAdd(interpreter));
676 CommandObjectSP delete_command_object(
677 new CommandObjectWatchpointCommandDelete(interpreter));
678 CommandObjectSP list_command_object(
679 new CommandObjectWatchpointCommandList(interpreter));
Johnny Chene9a56272012-08-09 23:09:42 +0000680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 add_command_object->SetCommandName("watchpoint command add");
682 delete_command_object->SetCommandName("watchpoint command delete");
683 list_command_object->SetCommandName("watchpoint command list");
Johnny Chene9a56272012-08-09 23:09:42 +0000684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 LoadSubCommand("add", add_command_object);
686 LoadSubCommand("delete", delete_command_object);
687 LoadSubCommand("list", list_command_object);
Johnny Chene9a56272012-08-09 23:09:42 +0000688}
689
Eugene Zelenko49bcfd82016-02-23 01:43:44 +0000690CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;