1e9a5627eSJohnny Chen //===-- CommandObjectWatchpointCommand.cpp ----------------------*- C++ -*-===//
2e9a5627eSJohnny Chen //
3e9a5627eSJohnny Chen //                     The LLVM Compiler Infrastructure
4e9a5627eSJohnny Chen //
5e9a5627eSJohnny Chen // This file is distributed under the University of Illinois Open Source
6e9a5627eSJohnny Chen // License. See LICENSE.TXT for details.
7e9a5627eSJohnny Chen //
8e9a5627eSJohnny Chen //===----------------------------------------------------------------------===//
9e9a5627eSJohnny Chen 
10e9a5627eSJohnny Chen // C Includes
11e9a5627eSJohnny Chen // C++ Includes
1249bcfd80SEugene Zelenko #include <vector>
13e9a5627eSJohnny Chen 
1449bcfd80SEugene Zelenko // Other libraries and framework includes
1549bcfd80SEugene Zelenko // Project includes
16e9a5627eSJohnny Chen #include "CommandObjectWatchpoint.h"
17*b9c1b51eSKate Stone #include "CommandObjectWatchpointCommand.h"
18*b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h"
19*b9c1b51eSKate Stone #include "lldb/Breakpoint/Watchpoint.h"
2044d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
21*b9c1b51eSKate Stone #include "lldb/Core/State.h"
22e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandInterpreter.h"
23e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandReturnObject.h"
24e9a5627eSJohnny Chen #include "lldb/Target/Target.h"
25e9a5627eSJohnny Chen #include "lldb/Target/Thread.h"
26e9a5627eSJohnny Chen 
27e9a5627eSJohnny Chen using namespace lldb;
28e9a5627eSJohnny Chen using namespace lldb_private;
29e9a5627eSJohnny Chen 
30e9a5627eSJohnny Chen //-------------------------------------------------------------------------
31e9a5627eSJohnny Chen // CommandObjectWatchpointCommandAdd
32e9a5627eSJohnny Chen //-------------------------------------------------------------------------
33e9a5627eSJohnny Chen 
34*b9c1b51eSKate Stone class CommandObjectWatchpointCommandAdd : public CommandObjectParsed,
35*b9c1b51eSKate Stone                                           public IOHandlerDelegateMultiline {
36e9a5627eSJohnny Chen public:
377428a18cSKate Stone   CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter)
38*b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "add",
39*b9c1b51eSKate Stone                             "Add a set of LLDB commands to a watchpoint, to be "
40*b9c1b51eSKate Stone                             "executed whenever the watchpoint is hit.",
41*b9c1b51eSKate Stone                             nullptr),
42*b9c1b51eSKate Stone         IOHandlerDelegateMultiline("DONE",
43*b9c1b51eSKate Stone                                    IOHandlerDelegate::Completion::LLDBCommand),
44*b9c1b51eSKate Stone         m_options() {
45e9a5627eSJohnny Chen     SetHelpLong(
46ea671fbdSKate Stone         R"(
47ea671fbdSKate Stone General information about entering watchpoint commands
48ea671fbdSKate Stone ------------------------------------------------------
49ea671fbdSKate Stone 
50*b9c1b51eSKate Stone )"
51*b9c1b51eSKate Stone         "This command will prompt for commands to be executed when the specified \
52ea671fbdSKate Stone watchpoint is hit.  Each command is typed on its own line following the '> ' \
53*b9c1b51eSKate Stone prompt until 'DONE' is entered."
54*b9c1b51eSKate Stone         R"(
55ea671fbdSKate Stone 
56*b9c1b51eSKate Stone )"
57*b9c1b51eSKate Stone         "Syntactic errors may not be detected when initially entered, and many \
58ea671fbdSKate Stone malformed commands can silently fail when executed.  If your watchpoint commands \
59*b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax."
60*b9c1b51eSKate Stone         R"(
61ea671fbdSKate Stone 
62*b9c1b51eSKate Stone )"
63*b9c1b51eSKate Stone         "Note: You may enter any debugger command exactly as you would at the debugger \
64ea671fbdSKate Stone prompt.  There is no limit to the number of commands supplied, but do NOT enter \
65*b9c1b51eSKate Stone more than one command per line."
66*b9c1b51eSKate Stone         R"(
67ea671fbdSKate Stone 
68ea671fbdSKate Stone Special information about PYTHON watchpoint commands
69ea671fbdSKate Stone ----------------------------------------------------
70ea671fbdSKate Stone 
71*b9c1b51eSKate Stone )"
72*b9c1b51eSKate Stone         "You may enter either one or more lines of Python, including function \
73ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \
74ea671fbdSKate Stone the code executes.  Single line watchpoint commands will be interpreted 'as is' \
75ea671fbdSKate Stone when the watchpoint is hit.  Multiple lines of Python will be wrapped in a \
76*b9c1b51eSKate Stone generated function, and a call to the function will be attached to the watchpoint."
77*b9c1b51eSKate Stone         R"(
78ea671fbdSKate Stone 
79ea671fbdSKate Stone This auto-generated function is passed in three arguments:
80ea671fbdSKate Stone 
81ea671fbdSKate Stone     frame:  an lldb.SBFrame object for the frame which hit the watchpoint.
82ea671fbdSKate Stone 
83ea671fbdSKate Stone     wp:     the watchpoint that was hit.
84ea671fbdSKate Stone 
85*b9c1b51eSKate Stone )"
86*b9c1b51eSKate Stone         "When specifying a python function with the --python-function option, you need \
87*b9c1b51eSKate Stone to supply the function name prepended by the module name:"
88*b9c1b51eSKate Stone         R"(
89ea671fbdSKate Stone 
90ea671fbdSKate Stone     --python-function myutils.watchpoint_callback
91ea671fbdSKate Stone 
92ea671fbdSKate Stone The function itself must have the following prototype:
93ea671fbdSKate Stone 
94ea671fbdSKate Stone def watchpoint_callback(frame, wp):
95ea671fbdSKate Stone   # Your code goes here
96ea671fbdSKate Stone 
97*b9c1b51eSKate Stone )"
98*b9c1b51eSKate Stone         "The arguments are the same as the arguments passed to generated functions as \
99ea671fbdSKate Stone described above.  Note that the global variable 'lldb.frame' will NOT be updated when \
100ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
101ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \
102ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \
103*b9c1b51eSKate Stone via process.GetTarget()."
104*b9c1b51eSKate Stone         R"(
105ea671fbdSKate Stone 
106*b9c1b51eSKate Stone )"
107*b9c1b51eSKate Stone         "Important Note: As Python code gets collected into functions, access to global \
108ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword.  Be sure to use correct \
109*b9c1b51eSKate Stone Python syntax, including indentation, when entering Python watchpoint commands."
110*b9c1b51eSKate Stone         R"(
111ea671fbdSKate Stone 
112ea671fbdSKate Stone Example Python one-line watchpoint command:
113ea671fbdSKate Stone 
114ea671fbdSKate Stone (lldb) watchpoint command add -s python 1
115ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
116ea671fbdSKate Stone > print "Hit this watchpoint!"
117ea671fbdSKate Stone > DONE
118ea671fbdSKate Stone 
119ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner:
120ea671fbdSKate Stone 
121ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()'
122ea671fbdSKate Stone (lldb) run
123ea671fbdSKate Stone Launching '.../a.out'  (x86_64)
124ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010
125ea671fbdSKate Stone Process 21778 Stopped
126ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread
127ea671fbdSKate Stone   36
128ea671fbdSKate Stone   37   	int c(int val)
129ea671fbdSKate Stone   38   	{
130ea671fbdSKate Stone   39 ->	    return val + 3;
131ea671fbdSKate Stone   40   	}
132ea671fbdSKate Stone   41
133ea671fbdSKate Stone   42   	int main (int argc, char const *argv[])
134ea671fbdSKate Stone 
135ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition:
136ea671fbdSKate Stone 
137ea671fbdSKate Stone (lldb) watchpoint command add -s python 1
138ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
139ea671fbdSKate Stone > def watchpoint_output (wp_no):
140ea671fbdSKate Stone >     out_string = "Hit watchpoint number " + repr (wp_no)
141ea671fbdSKate Stone >     print out_string
142ea671fbdSKate Stone >     return True
143ea671fbdSKate Stone > watchpoint_output (1)
144ea671fbdSKate Stone > DONE
145ea671fbdSKate Stone 
146ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python:
147ea671fbdSKate Stone 
148ea671fbdSKate Stone (lldb) watchpoint command add -s p 1
149ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end.
150ea671fbdSKate Stone > global wp_count
151ea671fbdSKate Stone > wp_count = wp_count + 1
152ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!"
153ea671fbdSKate Stone > DONE
154ea671fbdSKate Stone 
155*b9c1b51eSKate Stone )"
156*b9c1b51eSKate Stone         "In this case, since there is a reference to a global variable, \
157ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \
158*b9c1b51eSKate Stone initialized:"
159*b9c1b51eSKate Stone         R"(
160ea671fbdSKate Stone 
161ea671fbdSKate Stone (lldb) script
162ea671fbdSKate Stone >>> wp_count = 0
163ea671fbdSKate Stone >>> quit()
164ea671fbdSKate Stone 
165*b9c1b51eSKate Stone )"
166*b9c1b51eSKate Stone         "Final Note: A warning that no watchpoint command was generated when there \
167*b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called.");
168e9a5627eSJohnny Chen 
169e9a5627eSJohnny Chen     CommandArgumentEntry arg;
170e9a5627eSJohnny Chen     CommandArgumentData wp_id_arg;
171e9a5627eSJohnny Chen 
172e9a5627eSJohnny Chen     // Define the first (and only) variant of this arg.
173e9a5627eSJohnny Chen     wp_id_arg.arg_type = eArgTypeWatchpointID;
174e9a5627eSJohnny Chen     wp_id_arg.arg_repetition = eArgRepeatPlain;
175e9a5627eSJohnny Chen 
176*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
177*b9c1b51eSKate Stone     // argument entry.
178e9a5627eSJohnny Chen     arg.push_back(wp_id_arg);
179e9a5627eSJohnny Chen 
180e9a5627eSJohnny Chen     // Push the data for the first argument into the m_arguments vector.
181e9a5627eSJohnny Chen     m_arguments.push_back(arg);
182e9a5627eSJohnny Chen   }
183e9a5627eSJohnny Chen 
18449bcfd80SEugene Zelenko   ~CommandObjectWatchpointCommandAdd() override = default;
185e9a5627eSJohnny Chen 
186*b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
187e9a5627eSJohnny Chen 
188*b9c1b51eSKate Stone   void IOHandlerActivated(IOHandler &io_handler) override {
18944d93782SGreg Clayton     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
190*b9c1b51eSKate Stone     if (output_sp) {
191*b9c1b51eSKate Stone       output_sp->PutCString(
192*b9c1b51eSKate Stone           "Enter your debugger command(s).  Type 'DONE' to end.\n");
19344d93782SGreg Clayton       output_sp->Flush();
19444d93782SGreg Clayton     }
19544d93782SGreg Clayton   }
19644d93782SGreg Clayton 
197*b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
198*b9c1b51eSKate Stone                               std::string &line) override {
19944d93782SGreg Clayton     io_handler.SetIsDone(true);
20044d93782SGreg Clayton 
201*b9c1b51eSKate Stone     // The WatchpointOptions object is owned by the watchpoint or watchpoint
202*b9c1b51eSKate Stone     // location
203*b9c1b51eSKate Stone     WatchpointOptions *wp_options =
204*b9c1b51eSKate Stone         (WatchpointOptions *)io_handler.GetUserData();
205*b9c1b51eSKate Stone     if (wp_options) {
206*b9c1b51eSKate Stone       std::unique_ptr<WatchpointOptions::CommandData> data_ap(
207*b9c1b51eSKate Stone           new WatchpointOptions::CommandData());
208*b9c1b51eSKate Stone       if (data_ap) {
20944d93782SGreg Clayton         data_ap->user_source.SplitIntoLines(line);
210*b9c1b51eSKate Stone         BatonSP baton_sp(
211*b9c1b51eSKate Stone             new WatchpointOptions::CommandBaton(data_ap.release()));
21244d93782SGreg Clayton         wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
21344d93782SGreg Clayton       }
21444d93782SGreg Clayton     }
21544d93782SGreg Clayton   }
21644d93782SGreg Clayton 
217*b9c1b51eSKate Stone   void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
218*b9c1b51eSKate Stone                                                CommandReturnObject &result) {
219*b9c1b51eSKate Stone     m_interpreter.GetLLDBCommandsFromIOHandler(
220*b9c1b51eSKate Stone         "> ",        // Prompt
22144d93782SGreg Clayton         *this,       // IOHandlerDelegate
22244d93782SGreg Clayton         true,        // Run IOHandler in async mode
223*b9c1b51eSKate Stone         wp_options); // Baton for the "io_handler" that will be passed back into
224*b9c1b51eSKate Stone                      // our IOHandlerDelegate functions
225e9a5627eSJohnny Chen   }
226e9a5627eSJohnny Chen 
227e9a5627eSJohnny Chen   /// Set a one-liner as the callback for the watchpoint.
228*b9c1b51eSKate Stone   void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
229*b9c1b51eSKate Stone                                     const char *oneliner) {
230*b9c1b51eSKate Stone     std::unique_ptr<WatchpointOptions::CommandData> data_ap(
231*b9c1b51eSKate Stone         new WatchpointOptions::CommandData());
232e9a5627eSJohnny Chen 
233e9a5627eSJohnny Chen     // It's necessary to set both user_source and script_source to the oneliner.
234*b9c1b51eSKate Stone     // The former is used to generate callback description (as in watchpoint
235*b9c1b51eSKate Stone     // command list)
236*b9c1b51eSKate Stone     // while the latter is used for Python to interpret during the actual
237*b9c1b51eSKate Stone     // callback.
238e9a5627eSJohnny Chen     data_ap->user_source.AppendString(oneliner);
239e9a5627eSJohnny Chen     data_ap->script_source.assign(oneliner);
240e9a5627eSJohnny Chen     data_ap->stop_on_error = m_options.m_stop_on_error;
241e9a5627eSJohnny Chen 
242e9a5627eSJohnny Chen     BatonSP baton_sp(new WatchpointOptions::CommandBaton(data_ap.release()));
243e9a5627eSJohnny Chen     wp_options->SetCallback(WatchpointOptionsCallbackFunction, baton_sp);
244e9a5627eSJohnny Chen   }
245e9a5627eSJohnny Chen 
246e9a5627eSJohnny Chen   static bool
247e9a5627eSJohnny Chen   WatchpointOptionsCallbackFunction(void *baton,
248e9a5627eSJohnny Chen                                     StoppointCallbackContext *context,
249*b9c1b51eSKate Stone                                     lldb::user_id_t watch_id) {
250e9a5627eSJohnny Chen     bool ret_value = true;
25149bcfd80SEugene Zelenko     if (baton == nullptr)
252e9a5627eSJohnny Chen       return true;
253e9a5627eSJohnny Chen 
254*b9c1b51eSKate Stone     WatchpointOptions::CommandData *data =
255*b9c1b51eSKate Stone         (WatchpointOptions::CommandData *)baton;
256e9a5627eSJohnny Chen     StringList &commands = data->user_source;
257e9a5627eSJohnny Chen 
258*b9c1b51eSKate Stone     if (commands.GetSize() > 0) {
259e9a5627eSJohnny Chen       ExecutionContext exe_ctx(context->exe_ctx_ref);
260e9a5627eSJohnny Chen       Target *target = exe_ctx.GetTargetPtr();
261*b9c1b51eSKate Stone       if (target) {
262e9a5627eSJohnny Chen         CommandReturnObject result;
263e9a5627eSJohnny Chen         Debugger &debugger = target->GetDebugger();
264*b9c1b51eSKate Stone         // Rig up the results secondary output stream to the debugger's, so the
265*b9c1b51eSKate Stone         // output will come out synchronously
266e9a5627eSJohnny Chen         // if the debugger is set up that way.
267e9a5627eSJohnny Chen 
268e9a5627eSJohnny Chen         StreamSP output_stream(debugger.GetAsyncOutputStream());
269e9a5627eSJohnny Chen         StreamSP error_stream(debugger.GetAsyncErrorStream());
270e9a5627eSJohnny Chen         result.SetImmediateOutputStream(output_stream);
271e9a5627eSJohnny Chen         result.SetImmediateErrorStream(error_stream);
272e9a5627eSJohnny Chen 
27326c7bf93SJim Ingham         CommandInterpreterRunOptions options;
27426c7bf93SJim Ingham         options.SetStopOnContinue(true);
27526c7bf93SJim Ingham         options.SetStopOnError(data->stop_on_error);
27626c7bf93SJim Ingham         options.SetEchoCommands(false);
27726c7bf93SJim Ingham         options.SetPrintResults(true);
27826c7bf93SJim Ingham         options.SetAddToHistory(false);
279e9a5627eSJohnny Chen 
280*b9c1b51eSKate Stone         debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
281*b9c1b51eSKate Stone                                                         options, result);
282e9a5627eSJohnny Chen         result.GetImmediateOutputStream()->Flush();
283e9a5627eSJohnny Chen         result.GetImmediateErrorStream()->Flush();
284e9a5627eSJohnny Chen       }
285e9a5627eSJohnny Chen     }
286e9a5627eSJohnny Chen     return ret_value;
287e9a5627eSJohnny Chen   }
288e9a5627eSJohnny Chen 
289*b9c1b51eSKate Stone   class CommandOptions : public Options {
290e9a5627eSJohnny Chen   public:
291*b9c1b51eSKate Stone     CommandOptions()
292*b9c1b51eSKate Stone         : Options(), m_use_commands(false), m_use_script_language(false),
293*b9c1b51eSKate Stone           m_script_language(eScriptLanguageNone), m_use_one_liner(false),
294*b9c1b51eSKate Stone           m_one_liner(), m_function_name() {}
295e9a5627eSJohnny Chen 
29649bcfd80SEugene Zelenko     ~CommandOptions() override = default;
297e9a5627eSJohnny Chen 
298*b9c1b51eSKate Stone     Error SetOptionValue(uint32_t option_idx, const char *option_arg,
299*b9c1b51eSKate Stone                          ExecutionContext *execution_context) override {
300e9a5627eSJohnny Chen       Error error;
3013bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
302e9a5627eSJohnny Chen 
303*b9c1b51eSKate Stone       switch (short_option) {
304e9a5627eSJohnny Chen       case 'o':
305e9a5627eSJohnny Chen         m_use_one_liner = true;
306e9a5627eSJohnny Chen         m_one_liner = option_arg;
307e9a5627eSJohnny Chen         break;
308e9a5627eSJohnny Chen 
309e9a5627eSJohnny Chen       case 's':
310*b9c1b51eSKate Stone         m_script_language = (lldb::ScriptLanguage)Args::StringToOptionEnum(
311*b9c1b51eSKate Stone             option_arg, g_option_table[option_idx].enum_values,
312*b9c1b51eSKate Stone             eScriptLanguageNone, error);
313e9a5627eSJohnny Chen 
314*b9c1b51eSKate Stone         m_use_script_language = (m_script_language == eScriptLanguagePython ||
315*b9c1b51eSKate Stone                                  m_script_language == eScriptLanguageDefault);
316e9a5627eSJohnny Chen         break;
317e9a5627eSJohnny Chen 
318*b9c1b51eSKate Stone       case 'e': {
319e9a5627eSJohnny Chen         bool success = false;
320e9a5627eSJohnny Chen         m_stop_on_error = Args::StringToBoolean(option_arg, false, &success);
321e9a5627eSJohnny Chen         if (!success)
322*b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
323*b9c1b51eSKate Stone               "invalid value for stop-on-error: \"%s\"", option_arg);
324*b9c1b51eSKate Stone       } break;
325e9a5627eSJohnny Chen 
326e9a5627eSJohnny Chen       case 'F':
327e9a5627eSJohnny Chen         m_use_one_liner = false;
328e9a5627eSJohnny Chen         m_use_script_language = true;
329e9a5627eSJohnny Chen         m_function_name.assign(option_arg);
330e9a5627eSJohnny Chen         break;
331e9a5627eSJohnny Chen 
332e9a5627eSJohnny Chen       default:
333e9a5627eSJohnny Chen         break;
334e9a5627eSJohnny Chen       }
335e9a5627eSJohnny Chen       return error;
336e9a5627eSJohnny Chen     }
33749bcfd80SEugene Zelenko 
338*b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
339e9a5627eSJohnny Chen       m_use_commands = true;
340e9a5627eSJohnny Chen       m_use_script_language = false;
341e9a5627eSJohnny Chen       m_script_language = eScriptLanguageNone;
342e9a5627eSJohnny Chen 
343e9a5627eSJohnny Chen       m_use_one_liner = false;
344e9a5627eSJohnny Chen       m_stop_on_error = true;
345e9a5627eSJohnny Chen       m_one_liner.clear();
346e9a5627eSJohnny Chen       m_function_name.clear();
347e9a5627eSJohnny Chen     }
348e9a5627eSJohnny Chen 
349*b9c1b51eSKate Stone     const OptionDefinition *GetDefinitions() override { return g_option_table; }
350e9a5627eSJohnny Chen 
351e9a5627eSJohnny Chen     // Options table: Required for subclasses of Options.
352e9a5627eSJohnny Chen 
353e9a5627eSJohnny Chen     static OptionDefinition g_option_table[];
354e9a5627eSJohnny Chen 
355e9a5627eSJohnny Chen     // Instance variables to hold the values for command options.
356e9a5627eSJohnny Chen 
357e9a5627eSJohnny Chen     bool m_use_commands;
358e9a5627eSJohnny Chen     bool m_use_script_language;
359e9a5627eSJohnny Chen     lldb::ScriptLanguage m_script_language;
360e9a5627eSJohnny Chen 
361e9a5627eSJohnny Chen     // Instance variables to hold the values for one_liner options.
362e9a5627eSJohnny Chen     bool m_use_one_liner;
363e9a5627eSJohnny Chen     std::string m_one_liner;
364e9a5627eSJohnny Chen     bool m_stop_on_error;
365e9a5627eSJohnny Chen     std::string m_function_name;
366e9a5627eSJohnny Chen   };
367e9a5627eSJohnny Chen 
368e9a5627eSJohnny Chen protected:
369*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
370e9a5627eSJohnny Chen     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
371e9a5627eSJohnny Chen 
372*b9c1b51eSKate Stone     if (target == nullptr) {
373*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
374*b9c1b51eSKate Stone                          "watchpoints to which to add commands");
375e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
376e9a5627eSJohnny Chen       return false;
377e9a5627eSJohnny Chen     }
378e9a5627eSJohnny Chen 
379e9a5627eSJohnny Chen     const WatchpointList &watchpoints = target->GetWatchpointList();
380e9a5627eSJohnny Chen     size_t num_watchpoints = watchpoints.GetSize();
381e9a5627eSJohnny Chen 
382*b9c1b51eSKate Stone     if (num_watchpoints == 0) {
383e9a5627eSJohnny Chen       result.AppendError("No watchpoints exist to have commands added");
384e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
385e9a5627eSJohnny Chen       return false;
386e9a5627eSJohnny Chen     }
387e9a5627eSJohnny Chen 
388*b9c1b51eSKate Stone     if (!m_options.m_use_script_language &&
389*b9c1b51eSKate Stone         !m_options.m_function_name.empty()) {
390*b9c1b51eSKate Stone       result.AppendError("need to enable scripting to have a function run as a "
391*b9c1b51eSKate Stone                          "watchpoint command");
392e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
393e9a5627eSJohnny Chen       return false;
394e9a5627eSJohnny Chen     }
395e9a5627eSJohnny Chen 
396e9a5627eSJohnny Chen     std::vector<uint32_t> valid_wp_ids;
397*b9c1b51eSKate Stone     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
398*b9c1b51eSKate Stone                                                                valid_wp_ids)) {
399e9a5627eSJohnny Chen       result.AppendError("Invalid watchpoints specification.");
400e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
401e9a5627eSJohnny Chen       return false;
402e9a5627eSJohnny Chen     }
403e9a5627eSJohnny Chen 
404e9a5627eSJohnny Chen     result.SetStatus(eReturnStatusSuccessFinishNoResult);
405e9a5627eSJohnny Chen     const size_t count = valid_wp_ids.size();
406*b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
407e9a5627eSJohnny Chen       uint32_t cur_wp_id = valid_wp_ids.at(i);
408*b9c1b51eSKate Stone       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
409e9a5627eSJohnny Chen         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
410e9a5627eSJohnny Chen         // Sanity check wp first.
411*b9c1b51eSKate Stone         if (wp == nullptr)
412*b9c1b51eSKate Stone           continue;
413e9a5627eSJohnny Chen 
414e9a5627eSJohnny Chen         WatchpointOptions *wp_options = wp->GetOptions();
415e9a5627eSJohnny Chen         // Skip this watchpoint if wp_options is not good.
416*b9c1b51eSKate Stone         if (wp_options == nullptr)
417*b9c1b51eSKate Stone           continue;
418e9a5627eSJohnny Chen 
419e9a5627eSJohnny Chen         // If we are using script language, get the script interpreter
420e9a5627eSJohnny Chen         // in order to set or collect command callback.  Otherwise, call
421e9a5627eSJohnny Chen         // the methods associated with this object.
422*b9c1b51eSKate Stone         if (m_options.m_use_script_language) {
423e9a5627eSJohnny Chen           // Special handling for one-liner specified inline.
424*b9c1b51eSKate Stone           if (m_options.m_use_one_liner) {
425*b9c1b51eSKate Stone             m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
426*b9c1b51eSKate Stone                 wp_options, m_options.m_one_liner.c_str());
427e9a5627eSJohnny Chen           }
428e9a5627eSJohnny Chen           // Special handling for using a Python function by name
429*b9c1b51eSKate Stone           // instead of extending the watchpoint callback data structures, we
430*b9c1b51eSKate Stone           // just automatize
431*b9c1b51eSKate Stone           // what the user would do manually: make their watchpoint command be a
432*b9c1b51eSKate Stone           // function call
433*b9c1b51eSKate Stone           else if (!m_options.m_function_name.empty()) {
434e9a5627eSJohnny Chen             std::string oneliner(m_options.m_function_name);
435e9a5627eSJohnny Chen             oneliner += "(frame, wp, internal_dict)";
436*b9c1b51eSKate Stone             m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback(
437*b9c1b51eSKate Stone                 wp_options, oneliner.c_str());
438*b9c1b51eSKate Stone           } else {
439*b9c1b51eSKate Stone             m_interpreter.GetScriptInterpreter()
440*b9c1b51eSKate Stone                 ->CollectDataForWatchpointCommandCallback(wp_options, result);
441e9a5627eSJohnny Chen           }
442*b9c1b51eSKate Stone         } else {
443e9a5627eSJohnny Chen           // Special handling for one-liner specified inline.
444e9a5627eSJohnny Chen           if (m_options.m_use_one_liner)
445e9a5627eSJohnny Chen             SetWatchpointCommandCallback(wp_options,
446e9a5627eSJohnny Chen                                          m_options.m_one_liner.c_str());
447e9a5627eSJohnny Chen           else
448*b9c1b51eSKate Stone             CollectDataForWatchpointCommandCallback(wp_options, result);
449e9a5627eSJohnny Chen         }
450e9a5627eSJohnny Chen       }
451e9a5627eSJohnny Chen     }
452e9a5627eSJohnny Chen 
453e9a5627eSJohnny Chen     return result.Succeeded();
454e9a5627eSJohnny Chen   }
455e9a5627eSJohnny Chen 
456e9a5627eSJohnny Chen private:
457e9a5627eSJohnny Chen   CommandOptions m_options;
458e9a5627eSJohnny Chen };
459e9a5627eSJohnny Chen 
460*b9c1b51eSKate Stone // FIXME: "script-type" needs to have its contents determined dynamically, so
461*b9c1b51eSKate Stone // somebody can add a new scripting
462*b9c1b51eSKate Stone // language to lldb and have it pickable here without having to change this
463*b9c1b51eSKate Stone // enumeration by hand and rebuild lldb proper.
464e9a5627eSJohnny Chen 
465*b9c1b51eSKate Stone static OptionEnumValueElement g_script_option_enumeration[4] = {
466*b9c1b51eSKate Stone     {eScriptLanguageNone, "command",
467*b9c1b51eSKate Stone      "Commands are in the lldb command interpreter language"},
468e9a5627eSJohnny Chen     {eScriptLanguagePython, "python", "Commands are in the Python language."},
469*b9c1b51eSKate Stone     {eSortOrderByName, "default-script",
470*b9c1b51eSKate Stone      "Commands are in the default scripting language."},
471*b9c1b51eSKate Stone     {0, nullptr, nullptr}};
472e9a5627eSJohnny Chen 
473e9a5627eSJohnny Chen OptionDefinition
474*b9c1b51eSKate Stone     CommandObjectWatchpointCommandAdd::CommandOptions::g_option_table[] = {
475ac9c3a62SKate Stone         // clang-format off
476ac9c3a62SKate Stone   {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."},
477ac9c3a62SKate Stone   {LLDB_OPT_SET_ALL, false, "stop-on-error",   'e', OptionParser::eRequiredArgument, nullptr, nullptr,                     0, eArgTypeBoolean,        "Specify whether watchpoint command execution should terminate on error."},
478ac9c3a62SKate Stone   {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."},
479ac9c3a62SKate Stone   {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."},
48049bcfd80SEugene Zelenko   {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
481ac9c3a62SKate Stone         // clang-format on
482e9a5627eSJohnny Chen };
483e9a5627eSJohnny Chen 
484e9a5627eSJohnny Chen //-------------------------------------------------------------------------
485e9a5627eSJohnny Chen // CommandObjectWatchpointCommandDelete
486e9a5627eSJohnny Chen //-------------------------------------------------------------------------
487e9a5627eSJohnny Chen 
488*b9c1b51eSKate Stone class CommandObjectWatchpointCommandDelete : public CommandObjectParsed {
489e9a5627eSJohnny Chen public:
490*b9c1b51eSKate Stone   CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter)
491*b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "delete",
492e9a5627eSJohnny Chen                             "Delete the set of commands from a watchpoint.",
493*b9c1b51eSKate Stone                             nullptr) {
494e9a5627eSJohnny Chen     CommandArgumentEntry arg;
495e9a5627eSJohnny Chen     CommandArgumentData wp_id_arg;
496e9a5627eSJohnny Chen 
497e9a5627eSJohnny Chen     // Define the first (and only) variant of this arg.
498e9a5627eSJohnny Chen     wp_id_arg.arg_type = eArgTypeWatchpointID;
499e9a5627eSJohnny Chen     wp_id_arg.arg_repetition = eArgRepeatPlain;
500e9a5627eSJohnny Chen 
501*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
502*b9c1b51eSKate Stone     // argument entry.
503e9a5627eSJohnny Chen     arg.push_back(wp_id_arg);
504e9a5627eSJohnny Chen 
505e9a5627eSJohnny Chen     // Push the data for the first argument into the m_arguments vector.
506e9a5627eSJohnny Chen     m_arguments.push_back(arg);
507e9a5627eSJohnny Chen   }
508e9a5627eSJohnny Chen 
50949bcfd80SEugene Zelenko   ~CommandObjectWatchpointCommandDelete() override = default;
510e9a5627eSJohnny Chen 
511e9a5627eSJohnny Chen protected:
512*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
513e9a5627eSJohnny Chen     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
514e9a5627eSJohnny Chen 
515*b9c1b51eSKate Stone     if (target == nullptr) {
516*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
517*b9c1b51eSKate Stone                          "watchpoints from which to delete commands");
518e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
519e9a5627eSJohnny Chen       return false;
520e9a5627eSJohnny Chen     }
521e9a5627eSJohnny Chen 
522e9a5627eSJohnny Chen     const WatchpointList &watchpoints = target->GetWatchpointList();
523e9a5627eSJohnny Chen     size_t num_watchpoints = watchpoints.GetSize();
524e9a5627eSJohnny Chen 
525*b9c1b51eSKate Stone     if (num_watchpoints == 0) {
526e9a5627eSJohnny Chen       result.AppendError("No watchpoints exist to have commands deleted");
527e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
528e9a5627eSJohnny Chen       return false;
529e9a5627eSJohnny Chen     }
530e9a5627eSJohnny Chen 
531*b9c1b51eSKate Stone     if (command.GetArgumentCount() == 0) {
532*b9c1b51eSKate Stone       result.AppendError(
533*b9c1b51eSKate Stone           "No watchpoint specified from which to delete the commands");
534e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
535e9a5627eSJohnny Chen       return false;
536e9a5627eSJohnny Chen     }
537e9a5627eSJohnny Chen 
538e9a5627eSJohnny Chen     std::vector<uint32_t> valid_wp_ids;
539*b9c1b51eSKate Stone     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
540*b9c1b51eSKate Stone                                                                valid_wp_ids)) {
541e9a5627eSJohnny Chen       result.AppendError("Invalid watchpoints specification.");
542e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
543e9a5627eSJohnny Chen       return false;
544e9a5627eSJohnny Chen     }
545e9a5627eSJohnny Chen 
546e9a5627eSJohnny Chen     result.SetStatus(eReturnStatusSuccessFinishNoResult);
547e9a5627eSJohnny Chen     const size_t count = valid_wp_ids.size();
548*b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
549e9a5627eSJohnny Chen       uint32_t cur_wp_id = valid_wp_ids.at(i);
550*b9c1b51eSKate Stone       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
551e9a5627eSJohnny Chen         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
552e9a5627eSJohnny Chen         if (wp)
553e9a5627eSJohnny Chen           wp->ClearCallback();
554*b9c1b51eSKate Stone       } else {
555*b9c1b51eSKate Stone         result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id);
556e9a5627eSJohnny Chen         result.SetStatus(eReturnStatusFailed);
557e9a5627eSJohnny Chen         return false;
558e9a5627eSJohnny Chen       }
559e9a5627eSJohnny Chen     }
560e9a5627eSJohnny Chen     return result.Succeeded();
561e9a5627eSJohnny Chen   }
562e9a5627eSJohnny Chen };
563e9a5627eSJohnny Chen 
564e9a5627eSJohnny Chen //-------------------------------------------------------------------------
565e9a5627eSJohnny Chen // CommandObjectWatchpointCommandList
566e9a5627eSJohnny Chen //-------------------------------------------------------------------------
567e9a5627eSJohnny Chen 
568*b9c1b51eSKate Stone class CommandObjectWatchpointCommandList : public CommandObjectParsed {
569e9a5627eSJohnny Chen public:
570*b9c1b51eSKate Stone   CommandObjectWatchpointCommandList(CommandInterpreter &interpreter)
571*b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list", "List the script or set of "
572*b9c1b51eSKate Stone                                                  "commands to be executed when "
573*b9c1b51eSKate Stone                                                  "the watchpoint is hit.",
574*b9c1b51eSKate Stone                             nullptr) {
575e9a5627eSJohnny Chen     CommandArgumentEntry arg;
576e9a5627eSJohnny Chen     CommandArgumentData wp_id_arg;
577e9a5627eSJohnny Chen 
578e9a5627eSJohnny Chen     // Define the first (and only) variant of this arg.
579e9a5627eSJohnny Chen     wp_id_arg.arg_type = eArgTypeWatchpointID;
580e9a5627eSJohnny Chen     wp_id_arg.arg_repetition = eArgRepeatPlain;
581e9a5627eSJohnny Chen 
582*b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
583*b9c1b51eSKate Stone     // argument entry.
584e9a5627eSJohnny Chen     arg.push_back(wp_id_arg);
585e9a5627eSJohnny Chen 
586e9a5627eSJohnny Chen     // Push the data for the first argument into the m_arguments vector.
587e9a5627eSJohnny Chen     m_arguments.push_back(arg);
588e9a5627eSJohnny Chen   }
589e9a5627eSJohnny Chen 
59049bcfd80SEugene Zelenko   ~CommandObjectWatchpointCommandList() override = default;
591e9a5627eSJohnny Chen 
592e9a5627eSJohnny Chen protected:
593*b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
594e9a5627eSJohnny Chen     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
595e9a5627eSJohnny Chen 
596*b9c1b51eSKate Stone     if (target == nullptr) {
597*b9c1b51eSKate Stone       result.AppendError("There is not a current executable; there are no "
598*b9c1b51eSKate Stone                          "watchpoints for which to list commands");
599e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
600e9a5627eSJohnny Chen       return false;
601e9a5627eSJohnny Chen     }
602e9a5627eSJohnny Chen 
603e9a5627eSJohnny Chen     const WatchpointList &watchpoints = target->GetWatchpointList();
604e9a5627eSJohnny Chen     size_t num_watchpoints = watchpoints.GetSize();
605e9a5627eSJohnny Chen 
606*b9c1b51eSKate Stone     if (num_watchpoints == 0) {
607e9a5627eSJohnny Chen       result.AppendError("No watchpoints exist for which to list commands");
608e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
609e9a5627eSJohnny Chen       return false;
610e9a5627eSJohnny Chen     }
611e9a5627eSJohnny Chen 
612*b9c1b51eSKate Stone     if (command.GetArgumentCount() == 0) {
613*b9c1b51eSKate Stone       result.AppendError(
614*b9c1b51eSKate Stone           "No watchpoint specified for which to list the commands");
615e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
616e9a5627eSJohnny Chen       return false;
617e9a5627eSJohnny Chen     }
618e9a5627eSJohnny Chen 
619e9a5627eSJohnny Chen     std::vector<uint32_t> valid_wp_ids;
620*b9c1b51eSKate Stone     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
621*b9c1b51eSKate Stone                                                                valid_wp_ids)) {
622e9a5627eSJohnny Chen       result.AppendError("Invalid watchpoints specification.");
623e9a5627eSJohnny Chen       result.SetStatus(eReturnStatusFailed);
624e9a5627eSJohnny Chen       return false;
625e9a5627eSJohnny Chen     }
626e9a5627eSJohnny Chen 
627e9a5627eSJohnny Chen     result.SetStatus(eReturnStatusSuccessFinishNoResult);
628e9a5627eSJohnny Chen     const size_t count = valid_wp_ids.size();
629*b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
630e9a5627eSJohnny Chen       uint32_t cur_wp_id = valid_wp_ids.at(i);
631*b9c1b51eSKate Stone       if (cur_wp_id != LLDB_INVALID_WATCH_ID) {
632e9a5627eSJohnny Chen         Watchpoint *wp = target->GetWatchpointList().FindByID(cur_wp_id).get();
633e9a5627eSJohnny Chen 
634*b9c1b51eSKate Stone         if (wp) {
635e9a5627eSJohnny Chen           const WatchpointOptions *wp_options = wp->GetOptions();
636*b9c1b51eSKate Stone           if (wp_options) {
637e9a5627eSJohnny Chen             // Get the callback baton associated with the current watchpoint.
638e9a5627eSJohnny Chen             const Baton *baton = wp_options->GetBaton();
639*b9c1b51eSKate Stone             if (baton) {
640e9a5627eSJohnny Chen               result.GetOutputStream().Printf("Watchpoint %u:\n", cur_wp_id);
641e9a5627eSJohnny Chen               result.GetOutputStream().IndentMore();
642*b9c1b51eSKate Stone               baton->GetDescription(&result.GetOutputStream(),
643*b9c1b51eSKate Stone                                     eDescriptionLevelFull);
644e9a5627eSJohnny Chen               result.GetOutputStream().IndentLess();
645*b9c1b51eSKate Stone             } else {
646*b9c1b51eSKate Stone               result.AppendMessageWithFormat(
647*b9c1b51eSKate Stone                   "Watchpoint %u does not have an associated command.\n",
648e9a5627eSJohnny Chen                   cur_wp_id);
649e9a5627eSJohnny Chen             }
650e9a5627eSJohnny Chen           }
651e9a5627eSJohnny Chen           result.SetStatus(eReturnStatusSuccessFinishResult);
652*b9c1b51eSKate Stone         } else {
653*b9c1b51eSKate Stone           result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n",
654*b9c1b51eSKate Stone                                        cur_wp_id);
655e9a5627eSJohnny Chen           result.SetStatus(eReturnStatusFailed);
656e9a5627eSJohnny Chen         }
657e9a5627eSJohnny Chen       }
658e9a5627eSJohnny Chen     }
659e9a5627eSJohnny Chen 
660e9a5627eSJohnny Chen     return result.Succeeded();
661e9a5627eSJohnny Chen   }
662e9a5627eSJohnny Chen };
663e9a5627eSJohnny Chen 
664e9a5627eSJohnny Chen //-------------------------------------------------------------------------
665e9a5627eSJohnny Chen // CommandObjectWatchpointCommand
666e9a5627eSJohnny Chen //-------------------------------------------------------------------------
667e9a5627eSJohnny Chen 
668*b9c1b51eSKate Stone CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(
669*b9c1b51eSKate Stone     CommandInterpreter &interpreter)
670*b9c1b51eSKate Stone     : CommandObjectMultiword(
671*b9c1b51eSKate Stone           interpreter, "command",
672*b9c1b51eSKate Stone           "Commands for adding, removing and examining LLDB commands "
6737428a18cSKate Stone           "executed when the watchpoint is hit (watchpoint 'commmands').",
674*b9c1b51eSKate Stone           "command <sub-command> [<sub-command-options>] <watchpoint-id>") {
675*b9c1b51eSKate Stone   CommandObjectSP add_command_object(
676*b9c1b51eSKate Stone       new CommandObjectWatchpointCommandAdd(interpreter));
677*b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
678*b9c1b51eSKate Stone       new CommandObjectWatchpointCommandDelete(interpreter));
679*b9c1b51eSKate Stone   CommandObjectSP list_command_object(
680*b9c1b51eSKate Stone       new CommandObjectWatchpointCommandList(interpreter));
681e9a5627eSJohnny Chen 
682e9a5627eSJohnny Chen   add_command_object->SetCommandName("watchpoint command add");
683e9a5627eSJohnny Chen   delete_command_object->SetCommandName("watchpoint command delete");
684e9a5627eSJohnny Chen   list_command_object->SetCommandName("watchpoint command list");
685e9a5627eSJohnny Chen 
68603da4cc2SGreg Clayton   LoadSubCommand("add", add_command_object);
68703da4cc2SGreg Clayton   LoadSubCommand("delete", delete_command_object);
68803da4cc2SGreg Clayton   LoadSubCommand("list", list_command_object);
689e9a5627eSJohnny Chen }
690e9a5627eSJohnny Chen 
69149bcfd80SEugene Zelenko CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default;
692