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