1 //===-- CommandObjectWatchpoint.cpp -----------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CommandObjectWatchpoint.h" 11 #include "CommandObjectWatchpointCommand.h" 12 13 // C Includes 14 // C++ Includes 15 // Other libraries and framework includes 16 // Project includes 17 #include "lldb/Breakpoint/Watchpoint.h" 18 #include "lldb/Breakpoint/WatchpointList.h" 19 #include "lldb/Core/StreamString.h" 20 #include "lldb/Core/ValueObject.h" 21 #include "lldb/Core/ValueObjectVariable.h" 22 #include "lldb/Host/StringConvert.h" 23 #include "lldb/Interpreter/CommandInterpreter.h" 24 #include "lldb/Interpreter/CommandReturnObject.h" 25 #include "lldb/Interpreter/CommandCompletions.h" 26 #include "lldb/Symbol/Variable.h" 27 #include "lldb/Symbol/VariableList.h" 28 #include "lldb/Target/StackFrame.h" 29 #include "lldb/Target/Target.h" 30 31 #include "llvm/ADT/StringRef.h" 32 33 #include <vector> 34 35 using namespace lldb; 36 using namespace lldb_private; 37 38 static void 39 AddWatchpointDescription(Stream *s, Watchpoint *wp, lldb::DescriptionLevel level) 40 { 41 s->IndentMore(); 42 wp->GetDescription(s, level); 43 s->IndentLess(); 44 s->EOL(); 45 } 46 47 static bool 48 CheckTargetForWatchpointOperations(Target *target, CommandReturnObject &result) 49 { 50 if (target == NULL) 51 { 52 result.AppendError ("Invalid target. No existing target or watchpoints."); 53 result.SetStatus (eReturnStatusFailed); 54 return false; 55 } 56 bool process_is_valid = target->GetProcessSP() && target->GetProcessSP()->IsAlive(); 57 if (!process_is_valid) 58 { 59 result.AppendError ("Thre's no process or it is not alive."); 60 result.SetStatus (eReturnStatusFailed); 61 return false; 62 } 63 // Target passes our checks, return true. 64 return true; 65 } 66 67 // Equivalent class: {"-", "to", "To", "TO"} of range specifier array. 68 static const char* RSA[4] = { "-", "to", "To", "TO" }; 69 70 // Return the index to RSA if found; otherwise -1 is returned. 71 static int32_t 72 WithRSAIndex(llvm::StringRef &Arg) 73 { 74 75 uint32_t i; 76 for (i = 0; i < 4; ++i) 77 if (Arg.find(RSA[i]) != llvm::StringRef::npos) 78 return i; 79 return -1; 80 } 81 82 // Return true if wp_ids is successfully populated with the watch ids. 83 // False otherwise. 84 bool 85 CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(Target *target, Args &args, std::vector<uint32_t> &wp_ids) 86 { 87 // Pre-condition: args.GetArgumentCount() > 0. 88 if (args.GetArgumentCount() == 0) 89 { 90 if (target == NULL) 91 return false; 92 WatchpointSP watch_sp = target->GetLastCreatedWatchpoint(); 93 if (watch_sp) 94 { 95 wp_ids.push_back(watch_sp->GetID()); 96 return true; 97 } 98 else 99 return false; 100 } 101 102 llvm::StringRef Minus("-"); 103 std::vector<llvm::StringRef> StrRefArgs; 104 std::pair<llvm::StringRef, llvm::StringRef> Pair; 105 size_t i; 106 int32_t idx; 107 // Go through the argments and make a canonical form of arg list containing 108 // only numbers with possible "-" in between. 109 for (i = 0; i < args.GetArgumentCount(); ++i) { 110 llvm::StringRef Arg(args.GetArgumentAtIndex(i)); 111 if ((idx = WithRSAIndex(Arg)) == -1) { 112 StrRefArgs.push_back(Arg); 113 continue; 114 } 115 // The Arg contains the range specifier, split it, then. 116 Pair = Arg.split(RSA[idx]); 117 if (!Pair.first.empty()) 118 StrRefArgs.push_back(Pair.first); 119 StrRefArgs.push_back(Minus); 120 if (!Pair.second.empty()) 121 StrRefArgs.push_back(Pair.second); 122 } 123 // Now process the canonical list and fill in the vector of uint32_t's. 124 // If there is any error, return false and the client should ignore wp_ids. 125 uint32_t beg, end, id; 126 size_t size = StrRefArgs.size(); 127 bool in_range = false; 128 for (i = 0; i < size; ++i) { 129 llvm::StringRef Arg = StrRefArgs[i]; 130 if (in_range) { 131 // Look for the 'end' of the range. Note StringRef::getAsInteger() 132 // returns true to signify error while parsing. 133 if (Arg.getAsInteger(0, end)) 134 return false; 135 // Found a range! Now append the elements. 136 for (id = beg; id <= end; ++id) 137 wp_ids.push_back(id); 138 in_range = false; 139 continue; 140 } 141 if (i < (size - 1) && StrRefArgs[i+1] == Minus) { 142 if (Arg.getAsInteger(0, beg)) 143 return false; 144 // Turn on the in_range flag, we are looking for end of range next. 145 ++i; in_range = true; 146 continue; 147 } 148 // Otherwise, we have a simple ID. Just append it. 149 if (Arg.getAsInteger(0, beg)) 150 return false; 151 wp_ids.push_back(beg); 152 } 153 // It is an error if after the loop, we're still in_range. 154 if (in_range) 155 return false; 156 157 return true; // Success! 158 } 159 160 //------------------------------------------------------------------------- 161 // CommandObjectWatchpointList 162 //------------------------------------------------------------------------- 163 #pragma mark List 164 165 class CommandObjectWatchpointList : public CommandObjectParsed 166 { 167 public: 168 CommandObjectWatchpointList (CommandInterpreter &interpreter) : 169 CommandObjectParsed (interpreter, 170 "watchpoint list", 171 "List all watchpoints at configurable levels of detail.", 172 NULL), 173 m_options(interpreter) 174 { 175 CommandArgumentEntry arg; 176 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 177 // Add the entry for the first argument for this command to the object's arguments vector. 178 m_arguments.push_back(arg); 179 } 180 181 virtual 182 ~CommandObjectWatchpointList () {} 183 184 virtual Options * 185 GetOptions () 186 { 187 return &m_options; 188 } 189 190 class CommandOptions : public Options 191 { 192 public: 193 194 CommandOptions (CommandInterpreter &interpreter) : 195 Options(interpreter), 196 m_level(lldb::eDescriptionLevelBrief) // Watchpoint List defaults to brief descriptions 197 { 198 } 199 200 virtual 201 ~CommandOptions () {} 202 203 virtual Error 204 SetOptionValue (uint32_t option_idx, const char *option_arg) 205 { 206 Error error; 207 const int short_option = m_getopt_table[option_idx].val; 208 209 switch (short_option) 210 { 211 case 'b': 212 m_level = lldb::eDescriptionLevelBrief; 213 break; 214 case 'f': 215 m_level = lldb::eDescriptionLevelFull; 216 break; 217 case 'v': 218 m_level = lldb::eDescriptionLevelVerbose; 219 break; 220 default: 221 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 222 break; 223 } 224 225 return error; 226 } 227 228 void 229 OptionParsingStarting () 230 { 231 m_level = lldb::eDescriptionLevelFull; 232 } 233 234 const OptionDefinition * 235 GetDefinitions () 236 { 237 return g_option_table; 238 } 239 240 241 // Options table: Required for subclasses of Options. 242 243 static OptionDefinition g_option_table[]; 244 245 // Instance variables to hold the values for command options. 246 247 lldb::DescriptionLevel m_level; 248 }; 249 250 protected: 251 virtual bool 252 DoExecute (Args& command, CommandReturnObject &result) 253 { 254 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 255 if (target == NULL) 256 { 257 result.AppendError ("Invalid target. No current target or watchpoints."); 258 result.SetStatus (eReturnStatusSuccessFinishNoResult); 259 return true; 260 } 261 262 if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) 263 { 264 uint32_t num_supported_hardware_watchpoints; 265 Error error = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints); 266 if (error.Success()) 267 result.AppendMessageWithFormat("Number of supported hardware watchpoints: %u\n", 268 num_supported_hardware_watchpoints); 269 } 270 271 const WatchpointList &watchpoints = target->GetWatchpointList(); 272 Mutex::Locker locker; 273 target->GetWatchpointList().GetListMutex(locker); 274 275 size_t num_watchpoints = watchpoints.GetSize(); 276 277 if (num_watchpoints == 0) 278 { 279 result.AppendMessage("No watchpoints currently set."); 280 result.SetStatus(eReturnStatusSuccessFinishNoResult); 281 return true; 282 } 283 284 Stream &output_stream = result.GetOutputStream(); 285 286 if (command.GetArgumentCount() == 0) 287 { 288 // No watchpoint selected; show info about all currently set watchpoints. 289 result.AppendMessage ("Current watchpoints:"); 290 for (size_t i = 0; i < num_watchpoints; ++i) 291 { 292 Watchpoint *wp = watchpoints.GetByIndex(i).get(); 293 AddWatchpointDescription(&output_stream, wp, m_options.m_level); 294 } 295 result.SetStatus(eReturnStatusSuccessFinishNoResult); 296 } 297 else 298 { 299 // Particular watchpoints selected; enable them. 300 std::vector<uint32_t> wp_ids; 301 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 302 { 303 result.AppendError("Invalid watchpoints specification."); 304 result.SetStatus(eReturnStatusFailed); 305 return false; 306 } 307 308 const size_t size = wp_ids.size(); 309 for (size_t i = 0; i < size; ++i) 310 { 311 Watchpoint *wp = watchpoints.FindByID(wp_ids[i]).get(); 312 if (wp) 313 AddWatchpointDescription(&output_stream, wp, m_options.m_level); 314 result.SetStatus(eReturnStatusSuccessFinishNoResult); 315 } 316 } 317 318 return result.Succeeded(); 319 } 320 321 private: 322 CommandOptions m_options; 323 }; 324 325 //------------------------------------------------------------------------- 326 // CommandObjectWatchpointList::Options 327 //------------------------------------------------------------------------- 328 #pragma mark List::CommandOptions 329 OptionDefinition 330 CommandObjectWatchpointList::CommandOptions::g_option_table[] = 331 { 332 { LLDB_OPT_SET_1, false, "brief", 'b', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, 333 "Give a brief description of the watchpoint (no location info)."}, 334 335 { LLDB_OPT_SET_2, false, "full", 'f', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, 336 "Give a full description of the watchpoint and its locations."}, 337 338 { LLDB_OPT_SET_3, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, 339 "Explain everything we know about the watchpoint (for debugging debugger bugs)." }, 340 341 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } 342 }; 343 344 //------------------------------------------------------------------------- 345 // CommandObjectWatchpointEnable 346 //------------------------------------------------------------------------- 347 #pragma mark Enable 348 349 class CommandObjectWatchpointEnable : public CommandObjectParsed 350 { 351 public: 352 CommandObjectWatchpointEnable (CommandInterpreter &interpreter) : 353 CommandObjectParsed (interpreter, 354 "enable", 355 "Enable the specified disabled watchpoint(s). If no watchpoints are specified, enable all of them.", 356 NULL) 357 { 358 CommandArgumentEntry arg; 359 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 360 // Add the entry for the first argument for this command to the object's arguments vector. 361 m_arguments.push_back(arg); 362 } 363 364 virtual 365 ~CommandObjectWatchpointEnable () {} 366 367 protected: 368 virtual bool 369 DoExecute (Args& command, 370 CommandReturnObject &result) 371 { 372 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 373 if (!CheckTargetForWatchpointOperations(target, result)) 374 return false; 375 376 Mutex::Locker locker; 377 target->GetWatchpointList().GetListMutex(locker); 378 379 const WatchpointList &watchpoints = target->GetWatchpointList(); 380 381 size_t num_watchpoints = watchpoints.GetSize(); 382 383 if (num_watchpoints == 0) 384 { 385 result.AppendError("No watchpoints exist to be enabled."); 386 result.SetStatus(eReturnStatusFailed); 387 return false; 388 } 389 390 if (command.GetArgumentCount() == 0) 391 { 392 // No watchpoint selected; enable all currently set watchpoints. 393 target->EnableAllWatchpoints(); 394 result.AppendMessageWithFormat("All watchpoints enabled. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints); 395 result.SetStatus(eReturnStatusSuccessFinishNoResult); 396 } 397 else 398 { 399 // Particular watchpoints selected; enable them. 400 std::vector<uint32_t> wp_ids; 401 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 402 { 403 result.AppendError("Invalid watchpoints specification."); 404 result.SetStatus(eReturnStatusFailed); 405 return false; 406 } 407 408 int count = 0; 409 const size_t size = wp_ids.size(); 410 for (size_t i = 0; i < size; ++i) 411 if (target->EnableWatchpointByID(wp_ids[i])) 412 ++count; 413 result.AppendMessageWithFormat("%d watchpoints enabled.\n", count); 414 result.SetStatus(eReturnStatusSuccessFinishNoResult); 415 } 416 417 return result.Succeeded(); 418 } 419 420 private: 421 }; 422 423 //------------------------------------------------------------------------- 424 // CommandObjectWatchpointDisable 425 //------------------------------------------------------------------------- 426 #pragma mark Disable 427 428 class CommandObjectWatchpointDisable : public CommandObjectParsed 429 { 430 public: 431 CommandObjectWatchpointDisable (CommandInterpreter &interpreter) : 432 CommandObjectParsed (interpreter, 433 "watchpoint disable", 434 "Disable the specified watchpoint(s) without removing it/them. If no watchpoints are specified, disable them all.", 435 NULL) 436 { 437 CommandArgumentEntry arg; 438 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 439 // Add the entry for the first argument for this command to the object's arguments vector. 440 m_arguments.push_back(arg); 441 } 442 443 444 virtual 445 ~CommandObjectWatchpointDisable () {} 446 447 protected: 448 virtual bool 449 DoExecute (Args& command, CommandReturnObject &result) 450 { 451 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 452 if (!CheckTargetForWatchpointOperations(target, result)) 453 return false; 454 455 Mutex::Locker locker; 456 target->GetWatchpointList().GetListMutex(locker); 457 458 const WatchpointList &watchpoints = target->GetWatchpointList(); 459 size_t num_watchpoints = watchpoints.GetSize(); 460 461 if (num_watchpoints == 0) 462 { 463 result.AppendError("No watchpoints exist to be disabled."); 464 result.SetStatus(eReturnStatusFailed); 465 return false; 466 } 467 468 if (command.GetArgumentCount() == 0) 469 { 470 // No watchpoint selected; disable all currently set watchpoints. 471 if (target->DisableAllWatchpoints()) 472 { 473 result.AppendMessageWithFormat("All watchpoints disabled. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints); 474 result.SetStatus(eReturnStatusSuccessFinishNoResult); 475 } 476 else 477 { 478 result.AppendError("Disable all watchpoints failed\n"); 479 result.SetStatus(eReturnStatusFailed); 480 } 481 } 482 else 483 { 484 // Particular watchpoints selected; disable them. 485 std::vector<uint32_t> wp_ids; 486 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 487 { 488 result.AppendError("Invalid watchpoints specification."); 489 result.SetStatus(eReturnStatusFailed); 490 return false; 491 } 492 493 int count = 0; 494 const size_t size = wp_ids.size(); 495 for (size_t i = 0; i < size; ++i) 496 if (target->DisableWatchpointByID(wp_ids[i])) 497 ++count; 498 result.AppendMessageWithFormat("%d watchpoints disabled.\n", count); 499 result.SetStatus(eReturnStatusSuccessFinishNoResult); 500 } 501 502 return result.Succeeded(); 503 } 504 505 }; 506 507 //------------------------------------------------------------------------- 508 // CommandObjectWatchpointDelete 509 //------------------------------------------------------------------------- 510 #pragma mark Delete 511 512 class CommandObjectWatchpointDelete : public CommandObjectParsed 513 { 514 public: 515 CommandObjectWatchpointDelete (CommandInterpreter &interpreter) : 516 CommandObjectParsed(interpreter, 517 "watchpoint delete", 518 "Delete the specified watchpoint(s). If no watchpoints are specified, delete them all.", 519 NULL) 520 { 521 CommandArgumentEntry arg; 522 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 523 // Add the entry for the first argument for this command to the object's arguments vector. 524 m_arguments.push_back(arg); 525 } 526 527 virtual 528 ~CommandObjectWatchpointDelete () {} 529 530 protected: 531 virtual bool 532 DoExecute (Args& command, CommandReturnObject &result) 533 { 534 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 535 if (!CheckTargetForWatchpointOperations(target, result)) 536 return false; 537 538 Mutex::Locker locker; 539 target->GetWatchpointList().GetListMutex(locker); 540 541 const WatchpointList &watchpoints = target->GetWatchpointList(); 542 543 size_t num_watchpoints = watchpoints.GetSize(); 544 545 if (num_watchpoints == 0) 546 { 547 result.AppendError("No watchpoints exist to be deleted."); 548 result.SetStatus(eReturnStatusFailed); 549 return false; 550 } 551 552 if (command.GetArgumentCount() == 0) 553 { 554 if (!m_interpreter.Confirm("About to delete all watchpoints, do you want to do that?", true)) 555 { 556 result.AppendMessage("Operation cancelled..."); 557 } 558 else 559 { 560 target->RemoveAllWatchpoints(); 561 result.AppendMessageWithFormat("All watchpoints removed. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints); 562 } 563 result.SetStatus (eReturnStatusSuccessFinishNoResult); 564 } 565 else 566 { 567 // Particular watchpoints selected; delete them. 568 std::vector<uint32_t> wp_ids; 569 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 570 { 571 result.AppendError("Invalid watchpoints specification."); 572 result.SetStatus(eReturnStatusFailed); 573 return false; 574 } 575 576 int count = 0; 577 const size_t size = wp_ids.size(); 578 for (size_t i = 0; i < size; ++i) 579 if (target->RemoveWatchpointByID(wp_ids[i])) 580 ++count; 581 result.AppendMessageWithFormat("%d watchpoints deleted.\n",count); 582 result.SetStatus (eReturnStatusSuccessFinishNoResult); 583 } 584 585 return result.Succeeded(); 586 } 587 588 }; 589 590 //------------------------------------------------------------------------- 591 // CommandObjectWatchpointIgnore 592 //------------------------------------------------------------------------- 593 594 class CommandObjectWatchpointIgnore : public CommandObjectParsed 595 { 596 public: 597 CommandObjectWatchpointIgnore (CommandInterpreter &interpreter) : 598 CommandObjectParsed (interpreter, 599 "watchpoint ignore", 600 "Set ignore count on the specified watchpoint(s). If no watchpoints are specified, set them all.", 601 NULL), 602 m_options (interpreter) 603 { 604 CommandArgumentEntry arg; 605 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 606 // Add the entry for the first argument for this command to the object's arguments vector. 607 m_arguments.push_back(arg); 608 } 609 610 virtual 611 ~CommandObjectWatchpointIgnore () {} 612 613 virtual Options * 614 GetOptions () 615 { 616 return &m_options; 617 } 618 619 class CommandOptions : public Options 620 { 621 public: 622 623 CommandOptions (CommandInterpreter &interpreter) : 624 Options (interpreter), 625 m_ignore_count (0) 626 { 627 } 628 629 virtual 630 ~CommandOptions () {} 631 632 virtual Error 633 SetOptionValue (uint32_t option_idx, const char *option_arg) 634 { 635 Error error; 636 const int short_option = m_getopt_table[option_idx].val; 637 638 switch (short_option) 639 { 640 case 'i': 641 { 642 m_ignore_count = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0); 643 if (m_ignore_count == UINT32_MAX) 644 error.SetErrorStringWithFormat ("invalid ignore count '%s'", option_arg); 645 } 646 break; 647 default: 648 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 649 break; 650 } 651 652 return error; 653 } 654 655 void 656 OptionParsingStarting () 657 { 658 m_ignore_count = 0; 659 } 660 661 const OptionDefinition * 662 GetDefinitions () 663 { 664 return g_option_table; 665 } 666 667 668 // Options table: Required for subclasses of Options. 669 670 static OptionDefinition g_option_table[]; 671 672 // Instance variables to hold the values for command options. 673 674 uint32_t m_ignore_count; 675 }; 676 677 protected: 678 virtual bool 679 DoExecute (Args& command, 680 CommandReturnObject &result) 681 { 682 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 683 if (!CheckTargetForWatchpointOperations(target, result)) 684 return false; 685 686 Mutex::Locker locker; 687 target->GetWatchpointList().GetListMutex(locker); 688 689 const WatchpointList &watchpoints = target->GetWatchpointList(); 690 691 size_t num_watchpoints = watchpoints.GetSize(); 692 693 if (num_watchpoints == 0) 694 { 695 result.AppendError("No watchpoints exist to be ignored."); 696 result.SetStatus(eReturnStatusFailed); 697 return false; 698 } 699 700 if (command.GetArgumentCount() == 0) 701 { 702 target->IgnoreAllWatchpoints(m_options.m_ignore_count); 703 result.AppendMessageWithFormat("All watchpoints ignored. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints); 704 result.SetStatus (eReturnStatusSuccessFinishNoResult); 705 } 706 else 707 { 708 // Particular watchpoints selected; ignore them. 709 std::vector<uint32_t> wp_ids; 710 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 711 { 712 result.AppendError("Invalid watchpoints specification."); 713 result.SetStatus(eReturnStatusFailed); 714 return false; 715 } 716 717 int count = 0; 718 const size_t size = wp_ids.size(); 719 for (size_t i = 0; i < size; ++i) 720 if (target->IgnoreWatchpointByID(wp_ids[i], m_options.m_ignore_count)) 721 ++count; 722 result.AppendMessageWithFormat("%d watchpoints ignored.\n",count); 723 result.SetStatus (eReturnStatusSuccessFinishNoResult); 724 } 725 726 return result.Succeeded(); 727 } 728 729 private: 730 CommandOptions m_options; 731 }; 732 733 #pragma mark Ignore::CommandOptions 734 OptionDefinition 735 CommandObjectWatchpointIgnore::CommandOptions::g_option_table[] = 736 { 737 { LLDB_OPT_SET_ALL, true, "ignore-count", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCount, "Set the number of times this watchpoint is skipped before stopping." }, 738 { 0, false, NULL, 0 , 0, NULL, NULL, 0, eArgTypeNone, NULL } 739 }; 740 741 742 //------------------------------------------------------------------------- 743 // CommandObjectWatchpointModify 744 //------------------------------------------------------------------------- 745 #pragma mark Modify 746 747 class CommandObjectWatchpointModify : public CommandObjectParsed 748 { 749 public: 750 751 CommandObjectWatchpointModify (CommandInterpreter &interpreter) : 752 CommandObjectParsed (interpreter, 753 "watchpoint modify", 754 "Modify the options on a watchpoint or set of watchpoints in the executable. " 755 "If no watchpoint is specified, act on the last created watchpoint. " 756 "Passing an empty argument clears the modification.", 757 NULL), 758 m_options (interpreter) 759 { 760 CommandArgumentEntry arg; 761 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); 762 // Add the entry for the first argument for this command to the object's arguments vector. 763 m_arguments.push_back (arg); 764 } 765 766 virtual 767 ~CommandObjectWatchpointModify () {} 768 769 virtual Options * 770 GetOptions () 771 { 772 return &m_options; 773 } 774 775 class CommandOptions : public Options 776 { 777 public: 778 779 CommandOptions (CommandInterpreter &interpreter) : 780 Options (interpreter), 781 m_condition (), 782 m_condition_passed (false) 783 { 784 } 785 786 virtual 787 ~CommandOptions () {} 788 789 virtual Error 790 SetOptionValue (uint32_t option_idx, const char *option_arg) 791 { 792 Error error; 793 const int short_option = m_getopt_table[option_idx].val; 794 795 switch (short_option) 796 { 797 case 'c': 798 if (option_arg != NULL) 799 m_condition.assign (option_arg); 800 else 801 m_condition.clear(); 802 m_condition_passed = true; 803 break; 804 default: 805 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 806 break; 807 } 808 809 return error; 810 } 811 812 void 813 OptionParsingStarting () 814 { 815 m_condition.clear(); 816 m_condition_passed = false; 817 } 818 819 const OptionDefinition* 820 GetDefinitions () 821 { 822 return g_option_table; 823 } 824 825 // Options table: Required for subclasses of Options. 826 827 static OptionDefinition g_option_table[]; 828 829 // Instance variables to hold the values for command options. 830 831 std::string m_condition; 832 bool m_condition_passed; 833 }; 834 835 protected: 836 virtual bool 837 DoExecute (Args& command, CommandReturnObject &result) 838 { 839 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 840 if (!CheckTargetForWatchpointOperations(target, result)) 841 return false; 842 843 Mutex::Locker locker; 844 target->GetWatchpointList().GetListMutex(locker); 845 846 const WatchpointList &watchpoints = target->GetWatchpointList(); 847 848 size_t num_watchpoints = watchpoints.GetSize(); 849 850 if (num_watchpoints == 0) 851 { 852 result.AppendError("No watchpoints exist to be modified."); 853 result.SetStatus(eReturnStatusFailed); 854 return false; 855 } 856 857 if (command.GetArgumentCount() == 0) 858 { 859 WatchpointSP wp_sp = target->GetLastCreatedWatchpoint(); 860 wp_sp->SetCondition(m_options.m_condition.c_str()); 861 result.SetStatus (eReturnStatusSuccessFinishNoResult); 862 } 863 else 864 { 865 // Particular watchpoints selected; set condition on them. 866 std::vector<uint32_t> wp_ids; 867 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids)) 868 { 869 result.AppendError("Invalid watchpoints specification."); 870 result.SetStatus(eReturnStatusFailed); 871 return false; 872 } 873 874 int count = 0; 875 const size_t size = wp_ids.size(); 876 for (size_t i = 0; i < size; ++i) 877 { 878 WatchpointSP wp_sp = watchpoints.FindByID(wp_ids[i]); 879 if (wp_sp) 880 { 881 wp_sp->SetCondition(m_options.m_condition.c_str()); 882 ++count; 883 } 884 } 885 result.AppendMessageWithFormat("%d watchpoints modified.\n",count); 886 result.SetStatus (eReturnStatusSuccessFinishNoResult); 887 } 888 889 return result.Succeeded(); 890 } 891 892 private: 893 CommandOptions m_options; 894 }; 895 896 #pragma mark Modify::CommandOptions 897 OptionDefinition 898 CommandObjectWatchpointModify::CommandOptions::g_option_table[] = 899 { 900 { LLDB_OPT_SET_ALL, false, "condition", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeExpression, "The watchpoint stops only if this condition expression evaluates to true."}, 901 { 0, false, NULL, 0 , 0, NULL, NULL, 0, eArgTypeNone, NULL } 902 }; 903 904 //------------------------------------------------------------------------- 905 // CommandObjectWatchpointSetVariable 906 //------------------------------------------------------------------------- 907 #pragma mark SetVariable 908 909 class CommandObjectWatchpointSetVariable : public CommandObjectParsed 910 { 911 public: 912 913 CommandObjectWatchpointSetVariable (CommandInterpreter &interpreter) : 914 CommandObjectParsed (interpreter, 915 "watchpoint set variable", 916 "Set a watchpoint on a variable. " 917 "Use the '-w' option to specify the type of watchpoint and " 918 "the '-x' option to specify the byte size to watch for. " 919 "If no '-w' option is specified, it defaults to write. " 920 "If no '-x' option is specified, it defaults to the variable's " 921 "byte size. " 922 "Note that there are limited hardware resources for watchpoints. " 923 "If watchpoint setting fails, consider disable/delete existing ones " 924 "to free up resources.", 925 NULL, 926 eCommandRequiresFrame | 927 eCommandTryTargetAPILock | 928 eCommandProcessMustBeLaunched | 929 eCommandProcessMustBePaused ), 930 m_option_group (interpreter), 931 m_option_watchpoint () 932 { 933 SetHelpLong( 934 R"( 935 Examples: 936 937 (lldb) watchpoint set variable -w read_write my_global_var 938 939 )" " Watches my_global_var for read/write access, with the region to watch \ 940 corresponding to the byte size of the data type." 941 ); 942 943 CommandArgumentEntry arg; 944 CommandArgumentData var_name_arg; 945 946 // Define the only variant of this arg. 947 var_name_arg.arg_type = eArgTypeVarName; 948 var_name_arg.arg_repetition = eArgRepeatPlain; 949 950 // Push the variant into the argument entry. 951 arg.push_back (var_name_arg); 952 953 // Push the data for the only argument into the m_arguments vector. 954 m_arguments.push_back (arg); 955 956 // Absorb the '-w' and '-x' options into our option group. 957 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 958 m_option_group.Finalize(); 959 } 960 961 virtual 962 ~CommandObjectWatchpointSetVariable () {} 963 964 virtual Options * 965 GetOptions () 966 { 967 return &m_option_group; 968 } 969 970 protected: 971 static size_t GetVariableCallback (void *baton, 972 const char *name, 973 VariableList &variable_list) 974 { 975 Target *target = static_cast<Target *>(baton); 976 if (target) 977 { 978 return target->GetImages().FindGlobalVariables (ConstString(name), 979 true, 980 UINT32_MAX, 981 variable_list); 982 } 983 return 0; 984 } 985 986 virtual bool 987 DoExecute (Args& command, CommandReturnObject &result) 988 { 989 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 990 StackFrame *frame = m_exe_ctx.GetFramePtr(); 991 992 // If no argument is present, issue an error message. There's no way to set a watchpoint. 993 if (command.GetArgumentCount() <= 0) 994 { 995 result.GetErrorStream().Printf("error: required argument missing; specify your program variable to watch for\n"); 996 result.SetStatus(eReturnStatusFailed); 997 return false; 998 } 999 1000 // If no '-w' is specified, default to '-w write'. 1001 if (!m_option_watchpoint.watch_type_specified) 1002 { 1003 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite; 1004 } 1005 1006 // We passed the sanity check for the command. 1007 // Proceed to set the watchpoint now. 1008 lldb::addr_t addr = 0; 1009 size_t size = 0; 1010 1011 VariableSP var_sp; 1012 ValueObjectSP valobj_sp; 1013 Stream &output_stream = result.GetOutputStream(); 1014 1015 // A simple watch variable gesture allows only one argument. 1016 if (command.GetArgumentCount() != 1) 1017 { 1018 result.GetErrorStream().Printf("error: specify exactly one variable to watch for\n"); 1019 result.SetStatus(eReturnStatusFailed); 1020 return false; 1021 } 1022 1023 // Things have checked out ok... 1024 Error error; 1025 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember | 1026 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess; 1027 valobj_sp = frame->GetValueForVariableExpressionPath (command.GetArgumentAtIndex(0), 1028 eNoDynamicValues, 1029 expr_path_options, 1030 var_sp, 1031 error); 1032 1033 if (!valobj_sp) 1034 { 1035 // Not in the frame; let's check the globals. 1036 1037 VariableList variable_list; 1038 ValueObjectList valobj_list; 1039 1040 Error error (Variable::GetValuesForVariableExpressionPath (command.GetArgumentAtIndex(0), 1041 m_exe_ctx.GetBestExecutionContextScope(), 1042 GetVariableCallback, 1043 target, 1044 variable_list, 1045 valobj_list)); 1046 1047 if (valobj_list.GetSize()) 1048 valobj_sp = valobj_list.GetValueObjectAtIndex(0); 1049 } 1050 1051 ClangASTType clang_type; 1052 1053 if (valobj_sp) 1054 { 1055 AddressType addr_type; 1056 addr = valobj_sp->GetAddressOf(false, &addr_type); 1057 if (addr_type == eAddressTypeLoad) 1058 { 1059 // We're in business. 1060 // Find out the size of this variable. 1061 size = m_option_watchpoint.watch_size == 0 ? valobj_sp->GetByteSize() 1062 : m_option_watchpoint.watch_size; 1063 } 1064 clang_type = valobj_sp->GetClangType(); 1065 } 1066 else 1067 { 1068 const char *error_cstr = error.AsCString(NULL); 1069 if (error_cstr) 1070 result.GetErrorStream().Printf("error: %s\n", error_cstr); 1071 else 1072 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", 1073 command.GetArgumentAtIndex(0)); 1074 return false; 1075 } 1076 1077 // Now it's time to create the watchpoint. 1078 uint32_t watch_type = m_option_watchpoint.watch_type; 1079 1080 error.Clear(); 1081 Watchpoint *wp = target->CreateWatchpoint(addr, size, &clang_type, watch_type, error).get(); 1082 if (wp) 1083 { 1084 wp->SetWatchSpec(command.GetArgumentAtIndex(0)); 1085 wp->SetWatchVariable(true); 1086 if (var_sp && var_sp->GetDeclaration().GetFile()) 1087 { 1088 StreamString ss; 1089 // True to show fullpath for declaration file. 1090 var_sp->GetDeclaration().DumpStopContext(&ss, true); 1091 wp->SetDeclInfo(ss.GetString()); 1092 } 1093 output_stream.Printf("Watchpoint created: "); 1094 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull); 1095 output_stream.EOL(); 1096 result.SetStatus(eReturnStatusSuccessFinishResult); 1097 } 1098 else 1099 { 1100 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64 ", variable expression='%s').\n", 1101 addr, (uint64_t)size, command.GetArgumentAtIndex(0)); 1102 if (error.AsCString(NULL)) 1103 result.AppendError(error.AsCString()); 1104 result.SetStatus(eReturnStatusFailed); 1105 } 1106 1107 return result.Succeeded(); 1108 } 1109 1110 private: 1111 OptionGroupOptions m_option_group; 1112 OptionGroupWatchpoint m_option_watchpoint; 1113 }; 1114 1115 //------------------------------------------------------------------------- 1116 // CommandObjectWatchpointSetExpression 1117 //------------------------------------------------------------------------- 1118 #pragma mark Set 1119 1120 class CommandObjectWatchpointSetExpression : public CommandObjectRaw 1121 { 1122 public: 1123 1124 CommandObjectWatchpointSetExpression (CommandInterpreter &interpreter) : 1125 CommandObjectRaw (interpreter, 1126 "watchpoint set expression", 1127 "Set a watchpoint on an address by supplying an expression. " 1128 "Use the '-w' option to specify the type of watchpoint and " 1129 "the '-x' option to specify the byte size to watch for. " 1130 "If no '-w' option is specified, it defaults to write. " 1131 "If no '-x' option is specified, it defaults to the target's " 1132 "pointer byte size. " 1133 "Note that there are limited hardware resources for watchpoints. " 1134 "If watchpoint setting fails, consider disable/delete existing ones " 1135 "to free up resources.", 1136 NULL, 1137 eCommandRequiresFrame | 1138 eCommandTryTargetAPILock | 1139 eCommandProcessMustBeLaunched | 1140 eCommandProcessMustBePaused ), 1141 m_option_group (interpreter), 1142 m_option_watchpoint () 1143 { 1144 SetHelpLong( 1145 R"( 1146 Examples: 1147 1148 (lldb) watchpoint set expression -w write -x 1 -- foo + 32 1149 1150 Watches write access for the 1-byte region pointed to by the address 'foo + 32')" 1151 ); 1152 1153 CommandArgumentEntry arg; 1154 CommandArgumentData expression_arg; 1155 1156 // Define the only variant of this arg. 1157 expression_arg.arg_type = eArgTypeExpression; 1158 expression_arg.arg_repetition = eArgRepeatPlain; 1159 1160 // Push the only variant into the argument entry. 1161 arg.push_back (expression_arg); 1162 1163 // Push the data for the only argument into the m_arguments vector. 1164 m_arguments.push_back (arg); 1165 1166 // Absorb the '-w' and '-x' options into our option group. 1167 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 1168 m_option_group.Finalize(); 1169 } 1170 1171 1172 virtual 1173 ~CommandObjectWatchpointSetExpression () {} 1174 1175 // Overrides base class's behavior where WantsCompletion = !WantsRawCommandString. 1176 virtual bool 1177 WantsCompletion() { return true; } 1178 1179 virtual Options * 1180 GetOptions () 1181 { 1182 return &m_option_group; 1183 } 1184 1185 protected: 1186 virtual bool 1187 DoExecute (const char *raw_command, CommandReturnObject &result) 1188 { 1189 m_option_group.NotifyOptionParsingStarting(); // This is a raw command, so notify the option group 1190 1191 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 1192 StackFrame *frame = m_exe_ctx.GetFramePtr(); 1193 1194 Args command(raw_command); 1195 const char *expr = NULL; 1196 if (raw_command[0] == '-') 1197 { 1198 // We have some options and these options MUST end with --. 1199 const char *end_options = NULL; 1200 const char *s = raw_command; 1201 while (s && s[0]) 1202 { 1203 end_options = ::strstr (s, "--"); 1204 if (end_options) 1205 { 1206 end_options += 2; // Get past the "--" 1207 if (::isspace (end_options[0])) 1208 { 1209 expr = end_options; 1210 while (::isspace (*expr)) 1211 ++expr; 1212 break; 1213 } 1214 } 1215 s = end_options; 1216 } 1217 1218 if (end_options) 1219 { 1220 Args args (llvm::StringRef(raw_command, end_options - raw_command)); 1221 if (!ParseOptions (args, result)) 1222 return false; 1223 1224 Error error (m_option_group.NotifyOptionParsingFinished()); 1225 if (error.Fail()) 1226 { 1227 result.AppendError (error.AsCString()); 1228 result.SetStatus (eReturnStatusFailed); 1229 return false; 1230 } 1231 } 1232 } 1233 1234 if (expr == NULL) 1235 expr = raw_command; 1236 1237 // If no argument is present, issue an error message. There's no way to set a watchpoint. 1238 if (command.GetArgumentCount() == 0) 1239 { 1240 result.GetErrorStream().Printf("error: required argument missing; specify an expression to evaulate into the address to watch for\n"); 1241 result.SetStatus(eReturnStatusFailed); 1242 return false; 1243 } 1244 1245 // If no '-w' is specified, default to '-w write'. 1246 if (!m_option_watchpoint.watch_type_specified) 1247 { 1248 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite; 1249 } 1250 1251 // We passed the sanity check for the command. 1252 // Proceed to set the watchpoint now. 1253 lldb::addr_t addr = 0; 1254 size_t size = 0; 1255 1256 ValueObjectSP valobj_sp; 1257 1258 // Use expression evaluation to arrive at the address to watch. 1259 EvaluateExpressionOptions options; 1260 options.SetCoerceToId(false); 1261 options.SetUnwindOnError(true); 1262 options.SetKeepInMemory(false); 1263 options.SetTryAllThreads(true); 1264 options.SetTimeoutUsec(0); 1265 1266 ExpressionResults expr_result = target->EvaluateExpression (expr, 1267 frame, 1268 valobj_sp, 1269 options); 1270 if (expr_result != eExpressionCompleted) 1271 { 1272 result.GetErrorStream().Printf("error: expression evaluation of address to watch failed\n"); 1273 result.GetErrorStream().Printf("expression evaluated: %s\n", expr); 1274 result.SetStatus(eReturnStatusFailed); 1275 return false; 1276 } 1277 1278 // Get the address to watch. 1279 bool success = false; 1280 addr = valobj_sp->GetValueAsUnsigned(0, &success); 1281 if (!success) 1282 { 1283 result.GetErrorStream().Printf("error: expression did not evaluate to an address\n"); 1284 result.SetStatus(eReturnStatusFailed); 1285 return false; 1286 } 1287 1288 if (m_option_watchpoint.watch_size != 0) 1289 size = m_option_watchpoint.watch_size; 1290 else 1291 size = target->GetArchitecture().GetAddressByteSize(); 1292 1293 // Now it's time to create the watchpoint. 1294 uint32_t watch_type = m_option_watchpoint.watch_type; 1295 1296 // Fetch the type from the value object, the type of the watched object is the pointee type 1297 /// of the expression, so convert to that if we found a valid type. 1298 ClangASTType clang_type(valobj_sp->GetClangType()); 1299 1300 Error error; 1301 Watchpoint *wp = target->CreateWatchpoint(addr, size, &clang_type, watch_type, error).get(); 1302 if (wp) 1303 { 1304 Stream &output_stream = result.GetOutputStream(); 1305 output_stream.Printf("Watchpoint created: "); 1306 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull); 1307 output_stream.EOL(); 1308 result.SetStatus(eReturnStatusSuccessFinishResult); 1309 } 1310 else 1311 { 1312 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64 ").\n", 1313 addr, (uint64_t)size); 1314 if (error.AsCString(NULL)) 1315 result.AppendError(error.AsCString()); 1316 result.SetStatus(eReturnStatusFailed); 1317 } 1318 1319 return result.Succeeded(); 1320 } 1321 1322 private: 1323 OptionGroupOptions m_option_group; 1324 OptionGroupWatchpoint m_option_watchpoint; 1325 }; 1326 1327 //------------------------------------------------------------------------- 1328 // CommandObjectWatchpointSet 1329 //------------------------------------------------------------------------- 1330 #pragma mark Set 1331 1332 class CommandObjectWatchpointSet : public CommandObjectMultiword 1333 { 1334 public: 1335 1336 CommandObjectWatchpointSet (CommandInterpreter &interpreter) : 1337 CommandObjectMultiword (interpreter, 1338 "watchpoint set", 1339 "A set of commands for setting a watchpoint.", 1340 "watchpoint set <subcommand> [<subcommand-options>]") 1341 { 1342 1343 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectWatchpointSetVariable (interpreter))); 1344 LoadSubCommand ("expression", CommandObjectSP (new CommandObjectWatchpointSetExpression (interpreter))); 1345 } 1346 1347 1348 virtual 1349 ~CommandObjectWatchpointSet () {} 1350 1351 }; 1352 1353 //------------------------------------------------------------------------- 1354 // CommandObjectMultiwordWatchpoint 1355 //------------------------------------------------------------------------- 1356 #pragma mark MultiwordWatchpoint 1357 1358 CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(CommandInterpreter &interpreter) : 1359 CommandObjectMultiword (interpreter, 1360 "watchpoint", 1361 "A set of commands for operating on watchpoints.", 1362 "watchpoint <command> [<command-options>]") 1363 { 1364 CommandObjectSP list_command_object (new CommandObjectWatchpointList (interpreter)); 1365 CommandObjectSP enable_command_object (new CommandObjectWatchpointEnable (interpreter)); 1366 CommandObjectSP disable_command_object (new CommandObjectWatchpointDisable (interpreter)); 1367 CommandObjectSP delete_command_object (new CommandObjectWatchpointDelete (interpreter)); 1368 CommandObjectSP ignore_command_object (new CommandObjectWatchpointIgnore (interpreter)); 1369 CommandObjectSP command_command_object (new CommandObjectWatchpointCommand (interpreter)); 1370 CommandObjectSP modify_command_object (new CommandObjectWatchpointModify (interpreter)); 1371 CommandObjectSP set_command_object (new CommandObjectWatchpointSet (interpreter)); 1372 1373 list_command_object->SetCommandName ("watchpoint list"); 1374 enable_command_object->SetCommandName("watchpoint enable"); 1375 disable_command_object->SetCommandName("watchpoint disable"); 1376 delete_command_object->SetCommandName("watchpoint delete"); 1377 ignore_command_object->SetCommandName("watchpoint ignore"); 1378 command_command_object->SetCommandName ("watchpoint command"); 1379 modify_command_object->SetCommandName("watchpoint modify"); 1380 set_command_object->SetCommandName("watchpoint set"); 1381 1382 LoadSubCommand ("list", list_command_object); 1383 LoadSubCommand ("enable", enable_command_object); 1384 LoadSubCommand ("disable", disable_command_object); 1385 LoadSubCommand ("delete", delete_command_object); 1386 LoadSubCommand ("ignore", ignore_command_object); 1387 LoadSubCommand ("command", command_command_object); 1388 LoadSubCommand ("modify", modify_command_object); 1389 LoadSubCommand ("set", set_command_object); 1390 } 1391 1392 CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint() 1393 { 1394 } 1395 1396