LLDB mainline
CommandObjectWatchpointCommand.cpp
Go to the documentation of this file.
1//===-- CommandObjectWatchpointCommand.cpp --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <vector>
10
15#include "lldb/Core/IOHandler.h"
22#include "lldb/Target/Target.h"
23#include "lldb/lldb-forward.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28#define LLDB_OPTIONS_watchpoint_command_add
29#include "CommandOptions.inc"
30
33public:
35 : CommandObjectParsed(interpreter, "add",
36 "Add a set of LLDB commands to a watchpoint, to be "
37 "executed whenever the watchpoint is hit. "
38 "The commands added to the watchpoint replace any "
39 "commands previously added to it.",
40 nullptr, eCommandRequiresTarget),
44 R"(
45General information about entering watchpoint commands
46------------------------------------------------------
47
48)"
49 "This command will prompt for commands to be executed when the specified \
50watchpoint is hit. Each command is typed on its own line following the '> ' \
51prompt until 'DONE' is entered."
52 R"(
53
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."
58 R"(
59
60)"
61 "Note: You may enter any debugger command exactly as you would at the debugger \
62prompt. There is no limit to the number of commands supplied, but do NOT enter \
63more than one command per line."
64 R"(
65
66Special information about PYTHON watchpoint commands
67----------------------------------------------------
68
69)"
70 "You may enter either one or more lines of Python, including function \
71definitions or calls to functions that will have been imported by the time \
72the code executes. Single line watchpoint commands will be interpreted 'as is' \
73when the watchpoint is hit. Multiple lines of Python will be wrapped in a \
74generated function, and a call to the function will be attached to the watchpoint."
75 R"(
76
77This auto-generated function is passed in three arguments:
78
79 frame: an lldb.SBFrame object for the frame which hit the watchpoint.
80
81 wp: the watchpoint that was hit.
82
83)"
84 "When specifying a python function with the --python-function option, you need \
85to supply the function name prepended by the module name:"
86 R"(
88 --python-function myutils.watchpoint_callback
90The function itself must have the following prototype:
92def watchpoint_callback(frame, wp):
93 # Your code goes here
94
95)"
96 "The arguments are the same as the arguments passed to generated functions as \
97described above. Note that the global variable 'lldb.frame' will NOT be updated when \
98this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
99can get you to the thread via frame.GetThread(), the thread can get you to the \
100process via thread.GetProcess(), and the process can get you back to the target \
101via process.GetTarget()."
102 R"(
103
104)"
105 "Important Note: As Python code gets collected into functions, access to global \
106variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
107Python syntax, including indentation, when entering Python watchpoint commands."
108 R"(
109
110Example Python one-line watchpoint command:
111
112(lldb) watchpoint command add -s python 1
113Enter your Python command(s). Type 'DONE' to end.
114> print "Hit this watchpoint!"
115> DONE
116
117As a convenience, this also works for a short Python one-liner:
118
119(lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
120(lldb) run
121Launching '.../a.out' (x86_64)
122(lldb) Fri Sep 10 12:17:45 2010
123Process 21778 Stopped
124* thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
125 36
126 37 int c(int val)
127 38 {
128 39 -> return val + 3;
129 40 }
130 41
131 42 int main (int argc, char const *argv[])
133Example multiple line Python watchpoint command, using function definition:
134
135(lldb) watchpoint command add -s python 1
136Enter your Python command(s). Type 'DONE' to end.
137> def watchpoint_output (wp_no):
138> out_string = "Hit watchpoint number " + repr (wp_no)
139> print out_string
140> return True
141> watchpoint_output (1)
142> DONE
143
144Example multiple line Python watchpoint command, using 'loose' Python:
145
146(lldb) watchpoint command add -s p 1
147Enter your Python command(s). Type 'DONE' to end.
148> global wp_count
149> wp_count = wp_count + 1
150> print "Hit this watchpoint " + repr(wp_count) + " times!"
151> DONE
152
153)"
154 "In this case, since there is a reference to a global variable, \
155'wp_count', you will also need to make sure 'wp_count' exists and is \
156initialized:"
157 R"(
158
159(lldb) script
160>>> wp_count = 0
161>>> quit()
162
163)"
164 "Final Note: A warning that no watchpoint command was generated when there \
165are no syntax errors may indicate that a function was declared but never called.");
166
168 }
169
170 ~CommandObjectWatchpointCommandAdd() override = default;
171
172 Options *GetOptions() override { return &m_options; }
173
174 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
175 if (interactive) {
176 if (lldb::LockableStreamFileSP output_sp =
177 io_handler.GetOutputStreamFileSP()) {
178 LockedStreamFile locked_stream = output_sp->Lock();
179 locked_stream.PutCString(
180 "Enter your debugger command(s). Type 'DONE' to end.\n");
181 }
182 }
183 }
184
185 void IOHandlerInputComplete(IOHandler &io_handler,
186 std::string &line) override {
187 io_handler.SetIsDone(true);
188
189 // The WatchpointOptions object is owned by the watchpoint or watchpoint
190 // location
191 WatchpointOptions *wp_options =
193 if (wp_options) {
194 std::unique_ptr<WatchpointOptions::CommandData> data_up(
196 if (data_up) {
197 data_up->user_source.SplitIntoLines(line);
198 auto baton_sp = std::make_shared<WatchpointOptions::CommandBaton>(
199 std::move(data_up));
200 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
201 }
202 }
203 }
204
205 void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
206 CommandReturnObject &result) {
207 m_interpreter.GetLLDBCommandsFromIOHandler(
208 "> ", // Prompt
209 *this, // IOHandlerDelegate
210 wp_options); // Baton for the "io_handler" that will be passed back into
211 // our IOHandlerDelegate functions
212 }
213
214 /// Set a one-liner as the callback for the watchpoint.
215 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
216 const char *oneliner) {
217 std::unique_ptr<WatchpointOptions::CommandData> data_up(
218 new WatchpointOptions::CommandData());
219
220 // It's necessary to set both user_source and script_source to the
221 // oneliner. The former is used to generate callback description (as in
222 // watchpoint command list) while the latter is used for Python to
223 // interpret during the actual callback.
224 data_up->user_source.AppendString(oneliner);
225 data_up->script_source.assign(oneliner);
226 data_up->stop_on_error = m_options.m_stop_on_error;
227
228 auto baton_sp =
229 std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
230 wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
231 }
232
233 static bool
235 StoppointCallbackContext *context,
236 lldb::user_id_t watch_id) {
237 bool ret_value = true;
238 if (baton == nullptr)
239 return true;
240
241 WatchpointOptions::CommandData *data =
242 (WatchpointOptions::CommandData *)baton;
243 StringList &commands = data->user_source;
244
245 if (commands.GetSize() > 0) {
246 ExecutionContext exe_ctx(context->exe_ctx_ref);
247 Target *target = exe_ctx.GetTargetPtr();
248 if (target) {
249 Debugger &debugger = target->GetDebugger();
250 CommandReturnObject result(debugger.GetUseColor());
251
252 // Rig up the results secondary output stream to the debugger's, so the
253 // output will come out synchronously if the debugger is set up that
254 // way.
259 options.SetStopOnContinue(true);
260 options.SetStopOnError(data->stop_on_error);
261 options.SetEchoCommands(false);
262 options.SetPrintResults(true);
263 options.SetPrintErrors(true);
264 options.SetAddToHistory(false);
266 debugger.GetCommandInterpreter().HandleCommands(commands, exe_ctx,
267 options, result);
268 result.GetImmediateOutputStream()->Flush();
269 result.GetImmediateErrorStream()->Flush();
272 return ret_value;
273 }
274
275 class CommandOptions : public Options {
276 public:
277 CommandOptions() = default;
278
279 ~CommandOptions() override = default;
280
281 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
282 ExecutionContext *execution_context) override {
284 const int short_option = m_getopt_table[option_idx].val;
285
286 switch (short_option) {
287 case 'o':
288 m_use_one_liner = true;
289 m_one_liner = std::string(option_arg);
290 break;
291
292 case 's':
294 option_arg, GetDefinitions()[option_idx].enum_values,
296
297 switch (m_script_language) {
301 break;
304 m_use_script_language = false;
305 break;
306 }
307 break;
308
309 case 'e': {
310 bool success = false;
312 OptionArgParser::ToBoolean(option_arg, false, &success);
313 if (!success)
315 "invalid value for stop-on-error: \"{0}\"", option_arg);
316 } break;
317
318 case 'F':
319 m_use_one_liner = false;
320 m_function_name.assign(std::string(option_arg));
321 break;
322
323 default:
324 llvm_unreachable("Unimplemented option");
325 }
326 return error;
327 }
328
329 void OptionParsingStarting(ExecutionContext *execution_context) override {
330 m_use_commands = true;
331 m_use_script_language = false;
333
334 m_use_one_liner = false;
335 m_stop_on_error = true;
336 m_one_liner.clear();
337 m_function_name.clear();
338 }
339
340 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
341 return llvm::ArrayRef(g_watchpoint_command_add_options);
342 }
343
344 // Instance variables to hold the values for command options.
345
346 bool m_use_commands = false;
347 bool m_use_script_language = false;
349
350 // Instance variables to hold the values for one_liner options.
351 bool m_use_one_liner = false;
352 std::string m_one_liner;
354 std::string m_function_name;
355 };
356
357protected:
358 void DoExecute(Args &command, CommandReturnObject &result) override {
359 Target &target = GetTarget();
360
361 const WatchpointList &watchpoints = target.GetWatchpointList();
362 size_t num_watchpoints = watchpoints.GetSize();
363
364 if (num_watchpoints == 0) {
365 result.AppendError("No watchpoints exist to have commands added");
366 return;
367 }
368
369 if (!m_options.m_function_name.empty()) {
370 if (!m_options.m_use_script_language) {
371 m_options.m_script_language = GetDebugger().GetScriptLanguage();
372 m_options.m_use_script_language = true;
373 }
374 }
375
376 std::vector<uint32_t> valid_wp_ids;
378 valid_wp_ids)) {
379 result.AppendError("invalid watchpoints specification");
380 return;
381 }
382
384 const size_t count = valid_wp_ids.size();
385 for (size_t i = 0; i < count; ++i) {
386 uint32_t cur_wp_id = valid_wp_ids.at(i);
387 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
388 Watchpoint *wp = target.GetWatchpointList().FindByID(cur_wp_id).get();
389 // Sanity check wp first.
390 if (wp == nullptr)
391 continue;
392
393 WatchpointOptions *wp_options = wp->GetOptions();
394 // Skip this watchpoint if wp_options is not good.
395 if (wp_options == nullptr)
396 continue;
397
398 // If we are using script language, get the script interpreter in order
399 // to set or collect command callback. Otherwise, call the methods
400 // associated with this object.
401 if (m_options.m_use_script_language) {
402 ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(
403 /*can_create=*/true, m_options.m_script_language);
404 // Special handling for one-liner specified inline.
405 if (m_options.m_use_one_liner) {
406 script_interp->SetWatchpointCommandCallback(
407 wp_options, m_options.m_one_liner.c_str(),
408 /*is_callback=*/false);
409 }
410 // Special handling for using a Python function by name instead of
411 // extending the watchpoint callback data structures, we just
412 // automatize what the user would do manually: make their watchpoint
413 // command be a function call
414 else if (!m_options.m_function_name.empty()) {
415 std::string function_signature = m_options.m_function_name;
416 function_signature += "(frame, wp, internal_dict)";
417 script_interp->SetWatchpointCommandCallback(
418 wp_options, function_signature.c_str(), /*is_callback=*/true);
419 } else {
420 script_interp->CollectDataForWatchpointCommandCallback(wp_options,
421 result);
422 }
423 } else {
424 // Special handling for one-liner specified inline.
425 if (m_options.m_use_one_liner)
427 m_options.m_one_liner.c_str());
428 else
429 CollectDataForWatchpointCommandCallback(wp_options, result);
430 }
431 }
432 }
433 }
434
435private:
437};
438
439// CommandObjectWatchpointCommandDelete
440
442public:
444 : CommandObjectParsed(interpreter, "delete",
445 "Delete the set of commands from a watchpoint.",
446 nullptr, eCommandRequiresTarget) {
448 }
449
451
452protected:
453 void DoExecute(Args &command, CommandReturnObject &result) override {
454 Target &target = GetTarget();
455
456 const WatchpointList &watchpoints = target.GetWatchpointList();
457 size_t num_watchpoints = watchpoints.GetSize();
458
459 if (num_watchpoints == 0) {
460 result.AppendError("No watchpoints exist to have commands deleted");
461 return;
462 }
463
464 if (command.GetArgumentCount() == 0) {
465 result.AppendError(
466 "No watchpoint specified from which to delete the commands");
467 return;
468 }
469
470 std::vector<uint32_t> valid_wp_ids;
472 valid_wp_ids)) {
473 result.AppendError("invalid watchpoints specification");
474 return;
475 }
476
478 const size_t count = valid_wp_ids.size();
479 for (size_t i = 0; i < count; ++i) {
480 uint32_t cur_wp_id = valid_wp_ids.at(i);
481 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
482 Watchpoint *wp = target.GetWatchpointList().FindByID(cur_wp_id).get();
483 if (wp)
484 wp->ClearCallback();
485 } else {
486 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
487 return;
488 }
489 }
490 }
491};
492
493// CommandObjectWatchpointCommandList
494
496public:
498 : CommandObjectParsed(interpreter, "list",
499 "List the script or set of commands to be executed "
500 "when the watchpoint is hit.",
501 nullptr, eCommandRequiresTarget) {
503 }
504
506
507protected:
508 void DoExecute(Args &command, CommandReturnObject &result) override {
509 Target &target = GetTarget();
510
511 const WatchpointList &watchpoints = target.GetWatchpointList();
512 size_t num_watchpoints = watchpoints.GetSize();
513
514 if (num_watchpoints == 0) {
515 result.AppendError("No watchpoints exist for which to list commands");
516 return;
517 }
518
519 if (command.GetArgumentCount() == 0) {
520 result.AppendError(
521 "No watchpoint specified for which to list the commands");
522 return;
523 }
524
525 std::vector<uint32_t> valid_wp_ids;
527 valid_wp_ids)) {
528 result.AppendError("invalid watchpoints specification");
529 return;
530 }
531
533 const size_t count = valid_wp_ids.size();
534 for (size_t i = 0; i < count; ++i) {
535 uint32_t cur_wp_id = valid_wp_ids.at(i);
536 if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
537 Watchpoint *wp = target.GetWatchpointList().FindByID(cur_wp_id).get();
538
539 if (wp) {
540 const WatchpointOptions *wp_options = wp->GetOptions();
541 if (wp_options) {
542 // Get the callback baton associated with the current watchpoint.
543 const Baton *baton = wp_options->GetBaton();
544 if (baton) {
545 result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
549 2);
550 } else {
552 "Watchpoint %u does not have an associated command.\n",
553 cur_wp_id);
554 }
555 }
557 } else {
558 result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
559 cur_wp_id);
560 }
561 }
562 }
563 }
564};
565
566// CommandObjectWatchpointCommand
567
569 CommandInterpreter &interpreter)
571 interpreter, "command",
572 "Commands for adding, removing and examining LLDB commands "
573 "executed when the watchpoint is hit (watchpoint 'commands').",
574 "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
575 CommandObjectSP add_command_object(
576 new CommandObjectWatchpointCommandAdd(interpreter));
577 CommandObjectSP delete_command_object(
578 new CommandObjectWatchpointCommandDelete(interpreter));
579 CommandObjectSP list_command_object(
580 new CommandObjectWatchpointCommandList(interpreter));
581
582 add_command_object->SetCommandName("watchpoint command add");
583 delete_command_object->SetCommandName("watchpoint command delete");
584 list_command_object->SetCommandName("watchpoint command list");
585
586 LoadSubCommand("add", add_command_object);
587 LoadSubCommand("delete", delete_command_object);
588 LoadSubCommand("list", list_command_object);
589}
590
static llvm::raw_ostream & error(Stream &strm)
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *oneliner)
Set a one-liner as the callback for the watchpoint.
CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
static bool WatchpointOptionsCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
~CommandObjectWatchpointCommandAdd() override=default
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
void DoExecute(Args &command, CommandReturnObject &result) override
void IOHandlerInputComplete(IOHandler &io_handler, std::string &line) override
Called when a line or lines have been retrieved.
void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result)
CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
~CommandObjectWatchpointCommandDelete() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
~CommandObjectWatchpointCommandList() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
A class designed to wrap callback batons so they can cleanup any acquired resources.
Definition Baton.h:35
virtual void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, unsigned indentation) const =0
void HandleCommands(const StringList &commands, const ExecutionContext &context, const CommandInterpreterRunOptions &options, CommandReturnObject &result)
Execute a list of commands in sequence.
static bool VerifyWatchpointIDs(Target &target, Args &args, std::vector< uint32_t > &wp_ids)
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
CommandObjectMultiword(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
virtual void SetHelpLong(llvm::StringRef str)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
CommandInterpreter & m_interpreter
void SetImmediateErrorStream(const lldb::StreamSP &stream_sp)
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void SetImmediateOutputStream(const lldb::StreamSP &stream_sp)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void AppendMessageWithFormat(const char *format,...) __attribute__((format(printf
lldb::StreamSP GetImmediateErrorStream() const
lldb::StreamSP GetImmediateOutputStream() const
A class to manage flag bits.
Definition Debugger.h:80
CommandInterpreter & GetCommandInterpreter()
Definition Debugger.h:163
lldb::StreamUP GetAsyncErrorStream()
bool GetUseColor() const
Definition Debugger.cpp:452
lldb::ScriptLanguage GetScriptLanguage() const
Definition Debugger.cpp:364
lldb::StreamUP GetAsyncOutputStream()
ScriptInterpreter * GetScriptInterpreter(bool can_create=true, std::optional< lldb::ScriptLanguage > language={})
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
IOHandlerDelegateMultiline(llvm::StringRef end_line, Completion completion=Completion::None)
Definition IOHandler.h:289
A delegate class for use with IOHandler subclasses.
Definition IOHandler.h:184
lldb::LockableStreamFileSP GetOutputStreamFileSP()
Definition IOHandler.cpp:93
void SetIsDone(bool b)
Definition IOHandler.h:81
A command line option parsing protocol class.
Definition Options.h:58
std::vector< Option > m_getopt_table
Definition Options.h:198
virtual void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *user_input, bool is_callback)
Set a one-liner as the callback for the watchpoint.
virtual void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result)
An error handling class.
Definition Status.h:118
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:400
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
unsigned GetIndentLevel() const
Get the current indentation level.
Definition Stream.cpp:187
Debugger & GetDebugger() const
Definition Target.h:1097
WatchpointList & GetWatchpointList()
Definition Target.h:807
This class is used by Watchpoint to manage a list of watchpoints,.
lldb::WatchpointSP FindByID(lldb::watch_id_t watchID) const
Returns a shared pointer to the watchpoint with id watchID, const version.
size_t GetSize() const
Returns the number of elements in this watchpoint list.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
void SetCallback(WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the watchpoint option set.
Baton * GetBaton()
Fetch the baton from the callback.
WatchpointOptions * GetOptions()
Returns the WatchpointOptions structure set for this watchpoint.
Definition Watchpoint.h:138
#define LLDB_INVALID_WATCH_ID
A class that represents a running process on the host machine.
ScriptLanguage
Script interpreter types.
@ eScriptLanguageUnknown
@ eScriptLanguageNone
@ eScriptLanguagePython
@ eDescriptionLevelFull
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeWatchpointID
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::LockableStreamFile > LockableStreamFileSP
static int64_t ToOptionEnum(llvm::StringRef s, const OptionEnumValues &enum_values, int32_t fail_value, Status &error)
static bool ToBoolean(llvm::StringRef s, bool fail_value, bool *success_ptr)