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 "CommandObjectWatchpointCommand.h" 17e9a5627eSJohnny Chen #include "CommandObjectWatchpoint.h" 1844d93782SGreg Clayton #include "lldb/Core/IOHandler.h" 19e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandInterpreter.h" 20e9a5627eSJohnny Chen #include "lldb/Interpreter/CommandReturnObject.h" 21e9a5627eSJohnny Chen #include "lldb/Target/Target.h" 22e9a5627eSJohnny Chen #include "lldb/Target/Thread.h" 23e9a5627eSJohnny Chen #include "lldb/Breakpoint/Watchpoint.h" 24e9a5627eSJohnny Chen #include "lldb/Breakpoint/StoppointCallbackContext.h" 25e9a5627eSJohnny Chen #include "lldb/Core/State.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 3444d93782SGreg Clayton class CommandObjectWatchpointCommandAdd : 3544d93782SGreg Clayton public CommandObjectParsed, 3644d93782SGreg Clayton public IOHandlerDelegateMultiline 37e9a5627eSJohnny Chen { 38e9a5627eSJohnny Chen public: 39*7428a18cSKate Stone CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) 40*7428a18cSKate Stone : CommandObjectParsed( 41*7428a18cSKate Stone interpreter, "add", 42*7428a18cSKate Stone "Add a set of LLDB commands to a watchpoint, to be executed whenever the watchpoint is hit.", nullptr), 43c3d874a5SGreg Clayton IOHandlerDelegateMultiline("DONE", IOHandlerDelegate::Completion::LLDBCommand), 44e9a5627eSJohnny Chen m_options(interpreter) 45e9a5627eSJohnny Chen { 46e9a5627eSJohnny Chen SetHelpLong ( 47ea671fbdSKate Stone R"( 48ea671fbdSKate Stone General information about entering watchpoint commands 49ea671fbdSKate Stone ------------------------------------------------------ 50ea671fbdSKate Stone 51ea671fbdSKate 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 '> ' \ 53ea671fbdSKate Stone prompt until 'DONE' is entered." R"( 54ea671fbdSKate Stone 55ea671fbdSKate Stone )" "Syntactic errors may not be detected when initially entered, and many \ 56ea671fbdSKate Stone malformed commands can silently fail when executed. If your watchpoint commands \ 57ea671fbdSKate Stone do not appear to be executing, double-check the command syntax." R"( 58ea671fbdSKate Stone 59ea671fbdSKate Stone )" "Note: You may enter any debugger command exactly as you would at the debugger \ 60ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 61ea671fbdSKate Stone more than one command per line." R"( 62ea671fbdSKate Stone 63ea671fbdSKate Stone Special information about PYTHON watchpoint commands 64ea671fbdSKate Stone ---------------------------------------------------- 65ea671fbdSKate Stone 66ea671fbdSKate Stone )" "You may enter either one or more lines of Python, including function \ 67ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 68ea671fbdSKate Stone the code executes. Single line watchpoint commands will be interpreted 'as is' \ 69ea671fbdSKate Stone when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ 70ea671fbdSKate Stone generated function, and a call to the function will be attached to the watchpoint." R"( 71ea671fbdSKate Stone 72ea671fbdSKate Stone This auto-generated function is passed in three arguments: 73ea671fbdSKate Stone 74ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit the watchpoint. 75ea671fbdSKate Stone 76ea671fbdSKate Stone wp: the watchpoint that was hit. 77ea671fbdSKate Stone 78ea671fbdSKate Stone )" "When specifying a python function with the --python-function option, you need \ 79ea671fbdSKate Stone to supply the function name prepended by the module name:" R"( 80ea671fbdSKate Stone 81ea671fbdSKate Stone --python-function myutils.watchpoint_callback 82ea671fbdSKate Stone 83ea671fbdSKate Stone The function itself must have the following prototype: 84ea671fbdSKate Stone 85ea671fbdSKate Stone def watchpoint_callback(frame, wp): 86ea671fbdSKate Stone # Your code goes here 87ea671fbdSKate Stone 88ea671fbdSKate Stone )" "The arguments are the same as the arguments passed to generated functions as \ 89ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 90ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 91ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 92ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 93ea671fbdSKate Stone via process.GetTarget()." R"( 94ea671fbdSKate Stone 95ea671fbdSKate Stone )" "Important Note: As Python code gets collected into functions, access to global \ 96ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 97ea671fbdSKate Stone Python syntax, including indentation, when entering Python watchpoint commands." R"( 98ea671fbdSKate Stone 99ea671fbdSKate Stone Example Python one-line watchpoint command: 100ea671fbdSKate Stone 101ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 102ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 103ea671fbdSKate Stone > print "Hit this watchpoint!" 104ea671fbdSKate Stone > DONE 105ea671fbdSKate Stone 106ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 107ea671fbdSKate Stone 108ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' 109ea671fbdSKate Stone (lldb) run 110ea671fbdSKate Stone Launching '.../a.out' (x86_64) 111ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 112ea671fbdSKate Stone Process 21778 Stopped 113ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread 114ea671fbdSKate Stone 36 115ea671fbdSKate Stone 37 int c(int val) 116ea671fbdSKate Stone 38 { 117ea671fbdSKate Stone 39 -> return val + 3; 118ea671fbdSKate Stone 40 } 119ea671fbdSKate Stone 41 120ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 121ea671fbdSKate Stone 122ea671fbdSKate Stone Example multiple line Python watchpoint command, using function definition: 123ea671fbdSKate Stone 124ea671fbdSKate Stone (lldb) watchpoint command add -s python 1 125ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 126ea671fbdSKate Stone > def watchpoint_output (wp_no): 127ea671fbdSKate Stone > out_string = "Hit watchpoint number " + repr (wp_no) 128ea671fbdSKate Stone > print out_string 129ea671fbdSKate Stone > return True 130ea671fbdSKate Stone > watchpoint_output (1) 131ea671fbdSKate Stone > DONE 132ea671fbdSKate Stone 133ea671fbdSKate Stone Example multiple line Python watchpoint command, using 'loose' Python: 134ea671fbdSKate Stone 135ea671fbdSKate Stone (lldb) watchpoint command add -s p 1 136ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 137ea671fbdSKate Stone > global wp_count 138ea671fbdSKate Stone > wp_count = wp_count + 1 139ea671fbdSKate Stone > print "Hit this watchpoint " + repr(wp_count) + " times!" 140ea671fbdSKate Stone > DONE 141ea671fbdSKate Stone 142ea671fbdSKate Stone )" "In this case, since there is a reference to a global variable, \ 143ea671fbdSKate Stone 'wp_count', you will also need to make sure 'wp_count' exists and is \ 144ea671fbdSKate Stone initialized:" R"( 145ea671fbdSKate Stone 146ea671fbdSKate Stone (lldb) script 147ea671fbdSKate Stone >>> wp_count = 0 148ea671fbdSKate Stone >>> quit() 149ea671fbdSKate Stone 150ea671fbdSKate Stone )" "Final Note: A warning that no watchpoint command was generated when there \ 151ea671fbdSKate Stone are no syntax errors may indicate that a function was declared but never called." 152ea671fbdSKate Stone ); 153e9a5627eSJohnny Chen 154e9a5627eSJohnny Chen CommandArgumentEntry arg; 155e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 156e9a5627eSJohnny Chen 157e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 158e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 159e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 160e9a5627eSJohnny Chen 161e9a5627eSJohnny Chen // There is only one variant this argument could be; put it into the argument entry. 162e9a5627eSJohnny Chen arg.push_back (wp_id_arg); 163e9a5627eSJohnny Chen 164e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 165e9a5627eSJohnny Chen m_arguments.push_back (arg); 166e9a5627eSJohnny Chen } 167e9a5627eSJohnny Chen 16849bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandAdd() override = default; 169e9a5627eSJohnny Chen 17013d21e9aSBruce Mitchener Options * 17113d21e9aSBruce Mitchener GetOptions () override 172e9a5627eSJohnny Chen { 173e9a5627eSJohnny Chen return &m_options; 174e9a5627eSJohnny Chen } 175e9a5627eSJohnny Chen 17613d21e9aSBruce Mitchener void 17713d21e9aSBruce Mitchener IOHandlerActivated (IOHandler &io_handler) override 17844d93782SGreg Clayton { 17944d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 18044d93782SGreg Clayton if (output_sp) 18144d93782SGreg Clayton { 18244d93782SGreg Clayton output_sp->PutCString("Enter your debugger command(s). Type 'DONE' to end.\n"); 18344d93782SGreg Clayton output_sp->Flush(); 18444d93782SGreg Clayton } 18544d93782SGreg Clayton } 18644d93782SGreg Clayton 18713d21e9aSBruce Mitchener void 18813d21e9aSBruce Mitchener IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override 18944d93782SGreg Clayton { 19044d93782SGreg Clayton io_handler.SetIsDone(true); 19144d93782SGreg Clayton 19244d93782SGreg Clayton // The WatchpointOptions object is owned by the watchpoint or watchpoint location 19344d93782SGreg Clayton WatchpointOptions *wp_options = (WatchpointOptions *) io_handler.GetUserData(); 19444d93782SGreg Clayton if (wp_options) 19544d93782SGreg Clayton { 19644d93782SGreg Clayton std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); 19749bcfd80SEugene Zelenko if (data_ap) 19844d93782SGreg Clayton { 19944d93782SGreg Clayton data_ap->user_source.SplitIntoLines(line); 20044d93782SGreg Clayton BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); 20144d93782SGreg Clayton wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp); 20244d93782SGreg Clayton } 20344d93782SGreg Clayton } 20444d93782SGreg Clayton } 20544d93782SGreg Clayton 206e9a5627eSJohnny Chen void 207e9a5627eSJohnny Chen CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, 208e9a5627eSJohnny Chen CommandReturnObject &result) 209e9a5627eSJohnny Chen { 21044d93782SGreg Clayton m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt 21144d93782SGreg Clayton *this, // IOHandlerDelegate 21244d93782SGreg Clayton true, // Run IOHandler in async mode 21344d93782SGreg Clayton wp_options); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions 214e9a5627eSJohnny Chen } 215e9a5627eSJohnny Chen 216e9a5627eSJohnny Chen /// Set a one-liner as the callback for the watchpoint. 217e9a5627eSJohnny Chen void 218e9a5627eSJohnny Chen SetWatchpointCommandCallback (WatchpointOptions *wp_options, 219e9a5627eSJohnny Chen const char *oneliner) 220e9a5627eSJohnny Chen { 2217b0992d9SGreg Clayton std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); 222e9a5627eSJohnny Chen 223e9a5627eSJohnny Chen // It's necessary to set both user_source and script_source to the oneliner. 224e9a5627eSJohnny Chen // The former is used to generate callback description (as in watchpoint command list) 225e9a5627eSJohnny Chen // while the latter is used for Python to interpret during the actual callback. 226e9a5627eSJohnny Chen data_ap->user_source.AppendString (oneliner); 227e9a5627eSJohnny Chen data_ap->script_source.assign (oneliner); 228e9a5627eSJohnny Chen data_ap->stop_on_error = m_options.m_stop_on_error; 229e9a5627eSJohnny Chen 230e9a5627eSJohnny Chen BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); 231e9a5627eSJohnny Chen wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp); 232e9a5627eSJohnny Chen } 233e9a5627eSJohnny Chen 234e9a5627eSJohnny Chen static bool 235e9a5627eSJohnny Chen WatchpointOptionsCallbackFunction (void *baton, 236e9a5627eSJohnny Chen StoppointCallbackContext *context, 237e9a5627eSJohnny Chen lldb::user_id_t watch_id) 238e9a5627eSJohnny Chen { 239e9a5627eSJohnny Chen bool ret_value = true; 24049bcfd80SEugene Zelenko if (baton == nullptr) 241e9a5627eSJohnny Chen return true; 242e9a5627eSJohnny Chen 243e9a5627eSJohnny Chen WatchpointOptions::CommandData *data = (WatchpointOptions::CommandData *) baton; 244e9a5627eSJohnny Chen StringList &commands = data->user_source; 245e9a5627eSJohnny Chen 246e9a5627eSJohnny Chen if (commands.GetSize() > 0) 247e9a5627eSJohnny Chen { 248e9a5627eSJohnny Chen ExecutionContext exe_ctx (context->exe_ctx_ref); 249e9a5627eSJohnny Chen Target *target = exe_ctx.GetTargetPtr(); 250e9a5627eSJohnny Chen if (target) 251e9a5627eSJohnny Chen { 252e9a5627eSJohnny Chen CommandReturnObject result; 253e9a5627eSJohnny Chen Debugger &debugger = target->GetDebugger(); 254e9a5627eSJohnny Chen // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously 255e9a5627eSJohnny Chen // if the debugger is set up that way. 256e9a5627eSJohnny Chen 257e9a5627eSJohnny Chen StreamSP output_stream (debugger.GetAsyncOutputStream()); 258e9a5627eSJohnny Chen StreamSP error_stream (debugger.GetAsyncErrorStream()); 259e9a5627eSJohnny Chen result.SetImmediateOutputStream (output_stream); 260e9a5627eSJohnny Chen result.SetImmediateErrorStream (error_stream); 261e9a5627eSJohnny Chen 26226c7bf93SJim Ingham CommandInterpreterRunOptions options; 26326c7bf93SJim Ingham options.SetStopOnContinue (true); 26426c7bf93SJim Ingham options.SetStopOnError (data->stop_on_error); 26526c7bf93SJim Ingham options.SetEchoCommands (false); 26626c7bf93SJim Ingham options.SetPrintResults (true); 26726c7bf93SJim Ingham options.SetAddToHistory (false); 268e9a5627eSJohnny Chen 269e9a5627eSJohnny Chen debugger.GetCommandInterpreter().HandleCommands (commands, 270e9a5627eSJohnny Chen &exe_ctx, 27126c7bf93SJim Ingham options, 272e9a5627eSJohnny Chen result); 273e9a5627eSJohnny Chen result.GetImmediateOutputStream()->Flush(); 274e9a5627eSJohnny Chen result.GetImmediateErrorStream()->Flush(); 275e9a5627eSJohnny Chen } 276e9a5627eSJohnny Chen } 277e9a5627eSJohnny Chen return ret_value; 278e9a5627eSJohnny Chen } 279e9a5627eSJohnny Chen 280e9a5627eSJohnny Chen class CommandOptions : public Options 281e9a5627eSJohnny Chen { 282e9a5627eSJohnny Chen public: 283e9a5627eSJohnny Chen CommandOptions (CommandInterpreter &interpreter) : 284e9a5627eSJohnny Chen Options (interpreter), 285e9a5627eSJohnny Chen m_use_commands (false), 286e9a5627eSJohnny Chen m_use_script_language (false), 287e9a5627eSJohnny Chen m_script_language (eScriptLanguageNone), 288e9a5627eSJohnny Chen m_use_one_liner (false), 289e9a5627eSJohnny Chen m_one_liner(), 290e9a5627eSJohnny Chen m_function_name() 291e9a5627eSJohnny Chen { 292e9a5627eSJohnny Chen } 293e9a5627eSJohnny Chen 29449bcfd80SEugene Zelenko ~CommandOptions() override = default; 295e9a5627eSJohnny Chen 29613d21e9aSBruce Mitchener Error 29713d21e9aSBruce Mitchener SetOptionValue (uint32_t option_idx, const char *option_arg) override 298e9a5627eSJohnny Chen { 299e9a5627eSJohnny Chen Error error; 3003bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 301e9a5627eSJohnny Chen 302e9a5627eSJohnny Chen switch (short_option) 303e9a5627eSJohnny Chen { 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': 310e9a5627eSJohnny Chen m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg, 311e9a5627eSJohnny Chen g_option_table[option_idx].enum_values, 312e9a5627eSJohnny Chen eScriptLanguageNone, 313e9a5627eSJohnny Chen error); 314e9a5627eSJohnny Chen 31549bcfd80SEugene Zelenko m_use_script_language = 31649bcfd80SEugene Zelenko (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault); 317e9a5627eSJohnny Chen break; 318e9a5627eSJohnny Chen 319e9a5627eSJohnny Chen case 'e': 320e9a5627eSJohnny Chen { 321e9a5627eSJohnny Chen bool success = false; 322e9a5627eSJohnny Chen m_stop_on_error = Args::StringToBoolean(option_arg, false, &success); 323e9a5627eSJohnny Chen if (!success) 324e9a5627eSJohnny Chen error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg); 325e9a5627eSJohnny Chen } 326e9a5627eSJohnny Chen break; 327e9a5627eSJohnny Chen 328e9a5627eSJohnny Chen case 'F': 329e9a5627eSJohnny Chen m_use_one_liner = false; 330e9a5627eSJohnny Chen m_use_script_language = true; 331e9a5627eSJohnny Chen m_function_name.assign(option_arg); 332e9a5627eSJohnny Chen break; 333e9a5627eSJohnny Chen 334e9a5627eSJohnny Chen default: 335e9a5627eSJohnny Chen break; 336e9a5627eSJohnny Chen } 337e9a5627eSJohnny Chen return error; 338e9a5627eSJohnny Chen } 33949bcfd80SEugene Zelenko 340e9a5627eSJohnny Chen void 34113d21e9aSBruce Mitchener OptionParsingStarting () override 342e9a5627eSJohnny Chen { 343e9a5627eSJohnny Chen m_use_commands = true; 344e9a5627eSJohnny Chen m_use_script_language = false; 345e9a5627eSJohnny Chen m_script_language = eScriptLanguageNone; 346e9a5627eSJohnny Chen 347e9a5627eSJohnny Chen m_use_one_liner = false; 348e9a5627eSJohnny Chen m_stop_on_error = true; 349e9a5627eSJohnny Chen m_one_liner.clear(); 350e9a5627eSJohnny Chen m_function_name.clear(); 351e9a5627eSJohnny Chen } 352e9a5627eSJohnny Chen 353e9a5627eSJohnny Chen const OptionDefinition* 35413d21e9aSBruce Mitchener GetDefinitions () override 355e9a5627eSJohnny Chen { 356e9a5627eSJohnny Chen return g_option_table; 357e9a5627eSJohnny Chen } 358e9a5627eSJohnny Chen 359e9a5627eSJohnny Chen // Options table: Required for subclasses of Options. 360e9a5627eSJohnny Chen 361e9a5627eSJohnny Chen static OptionDefinition g_option_table[]; 362e9a5627eSJohnny Chen 363e9a5627eSJohnny Chen // Instance variables to hold the values for command options. 364e9a5627eSJohnny Chen 365e9a5627eSJohnny Chen bool m_use_commands; 366e9a5627eSJohnny Chen bool m_use_script_language; 367e9a5627eSJohnny Chen lldb::ScriptLanguage m_script_language; 368e9a5627eSJohnny Chen 369e9a5627eSJohnny Chen // Instance variables to hold the values for one_liner options. 370e9a5627eSJohnny Chen bool m_use_one_liner; 371e9a5627eSJohnny Chen std::string m_one_liner; 372e9a5627eSJohnny Chen bool m_stop_on_error; 373e9a5627eSJohnny Chen std::string m_function_name; 374e9a5627eSJohnny Chen }; 375e9a5627eSJohnny Chen 376e9a5627eSJohnny Chen protected: 37713d21e9aSBruce Mitchener bool 37813d21e9aSBruce Mitchener DoExecute (Args& command, CommandReturnObject &result) override 379e9a5627eSJohnny Chen { 380e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 381e9a5627eSJohnny Chen 38249bcfd80SEugene Zelenko if (target == nullptr) 383e9a5627eSJohnny Chen { 384e9a5627eSJohnny Chen result.AppendError ("There is not a current executable; there are no watchpoints to which to add commands"); 385e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 386e9a5627eSJohnny Chen return false; 387e9a5627eSJohnny Chen } 388e9a5627eSJohnny Chen 389e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 390e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 391e9a5627eSJohnny Chen 392e9a5627eSJohnny Chen if (num_watchpoints == 0) 393e9a5627eSJohnny Chen { 394e9a5627eSJohnny Chen result.AppendError ("No watchpoints exist to have commands added"); 395e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 396e9a5627eSJohnny Chen return false; 397e9a5627eSJohnny Chen } 398e9a5627eSJohnny Chen 39949bcfd80SEugene Zelenko if (!m_options.m_use_script_language && !m_options.m_function_name.empty()) 400e9a5627eSJohnny Chen { 401e9a5627eSJohnny Chen result.AppendError ("need to enable scripting to have a function run as a watchpoint command"); 402e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 403e9a5627eSJohnny Chen return false; 404e9a5627eSJohnny Chen } 405e9a5627eSJohnny Chen 406e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 407b0b4513eSJim Ingham if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) 408e9a5627eSJohnny Chen { 409e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 410e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 411e9a5627eSJohnny Chen return false; 412e9a5627eSJohnny Chen } 413e9a5627eSJohnny Chen 414e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 415e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 416e9a5627eSJohnny Chen for (size_t i = 0; i < count; ++i) 417e9a5627eSJohnny Chen { 418e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at (i); 419e9a5627eSJohnny Chen if (cur_wp_id != LLDB_INVALID_WATCH_ID) 420e9a5627eSJohnny Chen { 421e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); 422e9a5627eSJohnny Chen // Sanity check wp first. 42349bcfd80SEugene Zelenko if (wp == nullptr) continue; 424e9a5627eSJohnny Chen 425e9a5627eSJohnny Chen WatchpointOptions *wp_options = wp->GetOptions(); 426e9a5627eSJohnny Chen // Skip this watchpoint if wp_options is not good. 42749bcfd80SEugene Zelenko if (wp_options == nullptr) continue; 428e9a5627eSJohnny Chen 429e9a5627eSJohnny Chen // If we are using script language, get the script interpreter 430e9a5627eSJohnny Chen // in order to set or collect command callback. Otherwise, call 431e9a5627eSJohnny Chen // the methods associated with this object. 432e9a5627eSJohnny Chen if (m_options.m_use_script_language) 433e9a5627eSJohnny Chen { 434e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 435e9a5627eSJohnny Chen if (m_options.m_use_one_liner) 436e9a5627eSJohnny Chen { 437e9a5627eSJohnny Chen m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options, 438e9a5627eSJohnny Chen m_options.m_one_liner.c_str()); 439e9a5627eSJohnny Chen } 440e9a5627eSJohnny Chen // Special handling for using a Python function by name 441e9a5627eSJohnny Chen // instead of extending the watchpoint callback data structures, we just automatize 442e9a5627eSJohnny Chen // what the user would do manually: make their watchpoint command be a function call 44349bcfd80SEugene Zelenko else if (!m_options.m_function_name.empty()) 444e9a5627eSJohnny Chen { 445e9a5627eSJohnny Chen std::string oneliner(m_options.m_function_name); 446e9a5627eSJohnny Chen oneliner += "(frame, wp, internal_dict)"; 447e9a5627eSJohnny Chen m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options, 448e9a5627eSJohnny Chen oneliner.c_str()); 449e9a5627eSJohnny Chen } 450e9a5627eSJohnny Chen else 451e9a5627eSJohnny Chen { 452e9a5627eSJohnny Chen m_interpreter.GetScriptInterpreter()->CollectDataForWatchpointCommandCallback (wp_options, 453e9a5627eSJohnny Chen result); 454e9a5627eSJohnny Chen } 455e9a5627eSJohnny Chen } 456e9a5627eSJohnny Chen else 457e9a5627eSJohnny Chen { 458e9a5627eSJohnny Chen // Special handling for one-liner specified inline. 459e9a5627eSJohnny Chen if (m_options.m_use_one_liner) 460e9a5627eSJohnny Chen SetWatchpointCommandCallback (wp_options, 461e9a5627eSJohnny Chen m_options.m_one_liner.c_str()); 462e9a5627eSJohnny Chen else 463e9a5627eSJohnny Chen CollectDataForWatchpointCommandCallback (wp_options, 464e9a5627eSJohnny Chen result); 465e9a5627eSJohnny Chen } 466e9a5627eSJohnny Chen } 467e9a5627eSJohnny Chen } 468e9a5627eSJohnny Chen 469e9a5627eSJohnny Chen return result.Succeeded(); 470e9a5627eSJohnny Chen } 471e9a5627eSJohnny Chen 472e9a5627eSJohnny Chen private: 473e9a5627eSJohnny Chen CommandOptions m_options; 474e9a5627eSJohnny Chen }; 475e9a5627eSJohnny Chen 476e9a5627eSJohnny Chen // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting 477e9a5627eSJohnny Chen // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper. 478e9a5627eSJohnny Chen 479e9a5627eSJohnny Chen static OptionEnumValueElement 480e9a5627eSJohnny Chen g_script_option_enumeration[4] = 481e9a5627eSJohnny Chen { 482e9a5627eSJohnny Chen { eScriptLanguageNone, "command", "Commands are in the lldb command interpreter language"}, 483e9a5627eSJohnny Chen { eScriptLanguagePython, "python", "Commands are in the Python language."}, 484e9a5627eSJohnny Chen { eSortOrderByName, "default-script", "Commands are in the default scripting language."}, 48549bcfd80SEugene Zelenko { 0, nullptr, nullptr } 486e9a5627eSJohnny Chen }; 487e9a5627eSJohnny Chen 488e9a5627eSJohnny Chen OptionDefinition 489e9a5627eSJohnny Chen CommandObjectWatchpointCommandAdd::CommandOptions::g_option_table[] = 490e9a5627eSJohnny Chen { 49149bcfd80SEugene Zelenko { LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner, 492e9a5627eSJohnny Chen "Specify a one-line watchpoint command inline. Be sure to surround it with quotes." }, 493e9a5627eSJohnny Chen 49449bcfd80SEugene Zelenko { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, 495e9a5627eSJohnny Chen "Specify whether watchpoint command execution should terminate on error." }, 496e9a5627eSJohnny Chen 49749bcfd80SEugene Zelenko { LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone, 498e9a5627eSJohnny Chen "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."}, 499e9a5627eSJohnny Chen 50049bcfd80SEugene Zelenko { LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, 501e9a5627eSJohnny Chen "Give the name of a Python function to run as command for this watchpoint. Be sure to give a module name if appropriate."}, 502e9a5627eSJohnny Chen 50349bcfd80SEugene Zelenko { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr } 504e9a5627eSJohnny Chen }; 505e9a5627eSJohnny Chen 506e9a5627eSJohnny Chen //------------------------------------------------------------------------- 507e9a5627eSJohnny Chen // CommandObjectWatchpointCommandDelete 508e9a5627eSJohnny Chen //------------------------------------------------------------------------- 509e9a5627eSJohnny Chen 510e9a5627eSJohnny Chen class CommandObjectWatchpointCommandDelete : public CommandObjectParsed 511e9a5627eSJohnny Chen { 512e9a5627eSJohnny Chen public: 513e9a5627eSJohnny Chen CommandObjectWatchpointCommandDelete (CommandInterpreter &interpreter) : 514e9a5627eSJohnny Chen CommandObjectParsed(interpreter, 515e9a5627eSJohnny Chen "delete", 516e9a5627eSJohnny Chen "Delete the set of commands from a watchpoint.", 51749bcfd80SEugene Zelenko nullptr) 518e9a5627eSJohnny Chen { 519e9a5627eSJohnny Chen CommandArgumentEntry arg; 520e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 521e9a5627eSJohnny Chen 522e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 523e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 524e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 525e9a5627eSJohnny Chen 526e9a5627eSJohnny Chen // There is only one variant this argument could be; put it into the argument entry. 527e9a5627eSJohnny Chen arg.push_back (wp_id_arg); 528e9a5627eSJohnny Chen 529e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 530e9a5627eSJohnny Chen m_arguments.push_back (arg); 531e9a5627eSJohnny Chen } 532e9a5627eSJohnny Chen 53349bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandDelete() override = default; 534e9a5627eSJohnny Chen 535e9a5627eSJohnny Chen protected: 53613d21e9aSBruce Mitchener bool 53713d21e9aSBruce Mitchener DoExecute (Args& command, CommandReturnObject &result) override 538e9a5627eSJohnny Chen { 539e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 540e9a5627eSJohnny Chen 54149bcfd80SEugene Zelenko if (target == nullptr) 542e9a5627eSJohnny Chen { 543e9a5627eSJohnny Chen result.AppendError ("There is not a current executable; there are no watchpoints from which to delete commands"); 544e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 545e9a5627eSJohnny Chen return false; 546e9a5627eSJohnny Chen } 547e9a5627eSJohnny Chen 548e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 549e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 550e9a5627eSJohnny Chen 551e9a5627eSJohnny Chen if (num_watchpoints == 0) 552e9a5627eSJohnny Chen { 553e9a5627eSJohnny Chen result.AppendError ("No watchpoints exist to have commands deleted"); 554e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 555e9a5627eSJohnny Chen return false; 556e9a5627eSJohnny Chen } 557e9a5627eSJohnny Chen 558e9a5627eSJohnny Chen if (command.GetArgumentCount() == 0) 559e9a5627eSJohnny Chen { 560e9a5627eSJohnny Chen result.AppendError ("No watchpoint specified from which to delete the commands"); 561e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 562e9a5627eSJohnny Chen return false; 563e9a5627eSJohnny Chen } 564e9a5627eSJohnny Chen 565e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 566b0b4513eSJim Ingham if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) 567e9a5627eSJohnny Chen { 568e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 569e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 570e9a5627eSJohnny Chen return false; 571e9a5627eSJohnny Chen } 572e9a5627eSJohnny Chen 573e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 574e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 575e9a5627eSJohnny Chen for (size_t i = 0; i < count; ++i) 576e9a5627eSJohnny Chen { 577e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at (i); 578e9a5627eSJohnny Chen if (cur_wp_id != LLDB_INVALID_WATCH_ID) 579e9a5627eSJohnny Chen { 580e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); 581e9a5627eSJohnny Chen if (wp) 582e9a5627eSJohnny Chen wp->ClearCallback(); 583e9a5627eSJohnny Chen } 584e9a5627eSJohnny Chen else 585e9a5627eSJohnny Chen { 586e9a5627eSJohnny Chen result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", 587e9a5627eSJohnny Chen cur_wp_id); 588e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 589e9a5627eSJohnny Chen return false; 590e9a5627eSJohnny Chen } 591e9a5627eSJohnny Chen } 592e9a5627eSJohnny Chen return result.Succeeded(); 593e9a5627eSJohnny Chen } 594e9a5627eSJohnny Chen }; 595e9a5627eSJohnny Chen 596e9a5627eSJohnny Chen //------------------------------------------------------------------------- 597e9a5627eSJohnny Chen // CommandObjectWatchpointCommandList 598e9a5627eSJohnny Chen //------------------------------------------------------------------------- 599e9a5627eSJohnny Chen 600e9a5627eSJohnny Chen class CommandObjectWatchpointCommandList : public CommandObjectParsed 601e9a5627eSJohnny Chen { 602e9a5627eSJohnny Chen public: 603e9a5627eSJohnny Chen CommandObjectWatchpointCommandList (CommandInterpreter &interpreter) : 604e9a5627eSJohnny Chen CommandObjectParsed(interpreter, 605e9a5627eSJohnny Chen "list", 606e9a5627eSJohnny Chen "List the script or set of commands to be executed when the watchpoint is hit.", 60749bcfd80SEugene Zelenko nullptr) 608e9a5627eSJohnny Chen { 609e9a5627eSJohnny Chen CommandArgumentEntry arg; 610e9a5627eSJohnny Chen CommandArgumentData wp_id_arg; 611e9a5627eSJohnny Chen 612e9a5627eSJohnny Chen // Define the first (and only) variant of this arg. 613e9a5627eSJohnny Chen wp_id_arg.arg_type = eArgTypeWatchpointID; 614e9a5627eSJohnny Chen wp_id_arg.arg_repetition = eArgRepeatPlain; 615e9a5627eSJohnny Chen 616e9a5627eSJohnny Chen // There is only one variant this argument could be; put it into the argument entry. 617e9a5627eSJohnny Chen arg.push_back (wp_id_arg); 618e9a5627eSJohnny Chen 619e9a5627eSJohnny Chen // Push the data for the first argument into the m_arguments vector. 620e9a5627eSJohnny Chen m_arguments.push_back (arg); 621e9a5627eSJohnny Chen } 622e9a5627eSJohnny Chen 62349bcfd80SEugene Zelenko ~CommandObjectWatchpointCommandList() override = default; 624e9a5627eSJohnny Chen 625e9a5627eSJohnny Chen protected: 62613d21e9aSBruce Mitchener bool 62713d21e9aSBruce Mitchener DoExecute (Args& command, CommandReturnObject &result) override 628e9a5627eSJohnny Chen { 629e9a5627eSJohnny Chen Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 630e9a5627eSJohnny Chen 63149bcfd80SEugene Zelenko if (target == nullptr) 632e9a5627eSJohnny Chen { 633e9a5627eSJohnny Chen result.AppendError ("There is not a current executable; there are no watchpoints for which to list commands"); 634e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 635e9a5627eSJohnny Chen return false; 636e9a5627eSJohnny Chen } 637e9a5627eSJohnny Chen 638e9a5627eSJohnny Chen const WatchpointList &watchpoints = target->GetWatchpointList(); 639e9a5627eSJohnny Chen size_t num_watchpoints = watchpoints.GetSize(); 640e9a5627eSJohnny Chen 641e9a5627eSJohnny Chen if (num_watchpoints == 0) 642e9a5627eSJohnny Chen { 643e9a5627eSJohnny Chen result.AppendError ("No watchpoints exist for which to list commands"); 644e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 645e9a5627eSJohnny Chen return false; 646e9a5627eSJohnny Chen } 647e9a5627eSJohnny Chen 648e9a5627eSJohnny Chen if (command.GetArgumentCount() == 0) 649e9a5627eSJohnny Chen { 650e9a5627eSJohnny Chen result.AppendError ("No watchpoint specified for which to list the commands"); 651e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 652e9a5627eSJohnny Chen return false; 653e9a5627eSJohnny Chen } 654e9a5627eSJohnny Chen 655e9a5627eSJohnny Chen std::vector<uint32_t> valid_wp_ids; 656b0b4513eSJim Ingham if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) 657e9a5627eSJohnny Chen { 658e9a5627eSJohnny Chen result.AppendError("Invalid watchpoints specification."); 659e9a5627eSJohnny Chen result.SetStatus(eReturnStatusFailed); 660e9a5627eSJohnny Chen return false; 661e9a5627eSJohnny Chen } 662e9a5627eSJohnny Chen 663e9a5627eSJohnny Chen result.SetStatus(eReturnStatusSuccessFinishNoResult); 664e9a5627eSJohnny Chen const size_t count = valid_wp_ids.size(); 665e9a5627eSJohnny Chen for (size_t i = 0; i < count; ++i) 666e9a5627eSJohnny Chen { 667e9a5627eSJohnny Chen uint32_t cur_wp_id = valid_wp_ids.at (i); 668e9a5627eSJohnny Chen if (cur_wp_id != LLDB_INVALID_WATCH_ID) 669e9a5627eSJohnny Chen { 670e9a5627eSJohnny Chen Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); 671e9a5627eSJohnny Chen 672e9a5627eSJohnny Chen if (wp) 673e9a5627eSJohnny Chen { 674e9a5627eSJohnny Chen const WatchpointOptions *wp_options = wp->GetOptions(); 675e9a5627eSJohnny Chen if (wp_options) 676e9a5627eSJohnny Chen { 677e9a5627eSJohnny Chen // Get the callback baton associated with the current watchpoint. 678e9a5627eSJohnny Chen const Baton *baton = wp_options->GetBaton(); 679e9a5627eSJohnny Chen if (baton) 680e9a5627eSJohnny Chen { 681e9a5627eSJohnny Chen result.GetOutputStream().Printf ("Watchpoint %u:\n", cur_wp_id); 682e9a5627eSJohnny Chen result.GetOutputStream().IndentMore (); 683e9a5627eSJohnny Chen baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull); 684e9a5627eSJohnny Chen result.GetOutputStream().IndentLess (); 685e9a5627eSJohnny Chen } 686e9a5627eSJohnny Chen else 687e9a5627eSJohnny Chen { 688e9a5627eSJohnny Chen result.AppendMessageWithFormat ("Watchpoint %u does not have an associated command.\n", 689e9a5627eSJohnny Chen cur_wp_id); 690e9a5627eSJohnny Chen } 691e9a5627eSJohnny Chen } 692e9a5627eSJohnny Chen result.SetStatus (eReturnStatusSuccessFinishResult); 693e9a5627eSJohnny Chen } 694e9a5627eSJohnny Chen else 695e9a5627eSJohnny Chen { 696e9a5627eSJohnny Chen result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); 697e9a5627eSJohnny Chen result.SetStatus (eReturnStatusFailed); 698e9a5627eSJohnny Chen } 699e9a5627eSJohnny Chen } 700e9a5627eSJohnny Chen } 701e9a5627eSJohnny Chen 702e9a5627eSJohnny Chen return result.Succeeded(); 703e9a5627eSJohnny Chen } 704e9a5627eSJohnny Chen }; 705e9a5627eSJohnny Chen 706e9a5627eSJohnny Chen //------------------------------------------------------------------------- 707e9a5627eSJohnny Chen // CommandObjectWatchpointCommand 708e9a5627eSJohnny Chen //------------------------------------------------------------------------- 709e9a5627eSJohnny Chen 710*7428a18cSKate Stone CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(CommandInterpreter &interpreter) 711*7428a18cSKate Stone : CommandObjectMultiword(interpreter, "command", "Commands for adding, removing and examining LLDB commands " 712*7428a18cSKate Stone "executed when the watchpoint is hit (watchpoint 'commmands').", 713e9a5627eSJohnny Chen "command <sub-command> [<sub-command-options>] <watchpoint-id>") 714e9a5627eSJohnny Chen { 715e9a5627eSJohnny Chen CommandObjectSP add_command_object (new CommandObjectWatchpointCommandAdd (interpreter)); 716e9a5627eSJohnny Chen CommandObjectSP delete_command_object (new CommandObjectWatchpointCommandDelete (interpreter)); 717e9a5627eSJohnny Chen CommandObjectSP list_command_object (new CommandObjectWatchpointCommandList (interpreter)); 718e9a5627eSJohnny Chen 719e9a5627eSJohnny Chen add_command_object->SetCommandName ("watchpoint command add"); 720e9a5627eSJohnny Chen delete_command_object->SetCommandName ("watchpoint command delete"); 721e9a5627eSJohnny Chen list_command_object->SetCommandName ("watchpoint command list"); 722e9a5627eSJohnny Chen 72303da4cc2SGreg Clayton LoadSubCommand ("add", add_command_object); 72403da4cc2SGreg Clayton LoadSubCommand ("delete", delete_command_object); 72503da4cc2SGreg Clayton LoadSubCommand ("list", list_command_object); 726e9a5627eSJohnny Chen } 727e9a5627eSJohnny Chen 72849bcfd80SEugene Zelenko CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; 729