1 //===-- CommandObjectWatchpoint.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectWatchpoint.h"
13 #include "CommandObjectWatchpointCommand.h"
17 // Other libraries and framework includes
19 #include "lldb/Breakpoint/Watchpoint.h"
20 #include "lldb/Breakpoint/WatchpointList.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Core/ValueObject.h"
23 #include "lldb/Core/ValueObjectVariable.h"
24 #include "lldb/Interpreter/CommandInterpreter.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Interpreter/CommandCompletions.h"
27 #include "lldb/Symbol/Variable.h"
28 #include "lldb/Symbol/VariableList.h"
29 #include "lldb/Target/Target.h"
31 #include "llvm/ADT/StringRef.h"
36 using namespace lldb_private;
39 AddWatchpointDescription(Stream *s, Watchpoint *wp, lldb::DescriptionLevel level)
42 wp->GetDescription(s, level);
48 CheckTargetForWatchpointOperations(Target *target, CommandReturnObject &result)
52 result.AppendError ("Invalid target. No existing target or watchpoints.");
53 result.SetStatus (eReturnStatusFailed);
56 bool process_is_valid = target->GetProcessSP() && target->GetProcessSP()->IsAlive();
57 if (!process_is_valid)
59 result.AppendError ("Thre's no process or it is not alive.");
60 result.SetStatus (eReturnStatusFailed);
63 // Target passes our checks, return true.
67 // Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
68 static const char* RSA[4] = { "-", "to", "To", "TO" };
70 // Return the index to RSA if found; otherwise -1 is returned.
72 WithRSAIndex(llvm::StringRef &Arg)
76 for (i = 0; i < 4; ++i)
77 if (Arg.find(RSA[i]) != llvm::StringRef::npos)
82 // Return true if wp_ids is successfully populated with the watch ids.
85 CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(Target *target, Args &args, std::vector<uint32_t> &wp_ids)
87 // Pre-condition: args.GetArgumentCount() > 0.
88 if (args.GetArgumentCount() == 0)
92 WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
95 wp_ids.push_back(watch_sp->GetID());
102 llvm::StringRef Minus("-");
103 std::vector<llvm::StringRef> StrRefArgs;
104 std::pair<llvm::StringRef, llvm::StringRef> Pair;
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);
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);
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];
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))
135 // Found a range! Now append the elements.
136 for (id = beg; id <= end; ++id)
137 wp_ids.push_back(id);
141 if (i < (size - 1) && StrRefArgs[i+1] == Minus) {
142 if (Arg.getAsInteger(0, beg))
144 // Turn on the in_range flag, we are looking for end of range next.
145 ++i; in_range = true;
148 // Otherwise, we have a simple ID. Just append it.
149 if (Arg.getAsInteger(0, beg))
151 wp_ids.push_back(beg);
153 // It is an error if after the loop, we're still in_range.
157 return true; // Success!
160 //-------------------------------------------------------------------------
161 // CommandObjectWatchpointList
162 //-------------------------------------------------------------------------
165 class CommandObjectWatchpointList : public CommandObjectParsed
168 CommandObjectWatchpointList (CommandInterpreter &interpreter) :
169 CommandObjectParsed (interpreter,
171 "List all watchpoints at configurable levels of detail.",
173 m_options(interpreter)
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);
182 ~CommandObjectWatchpointList () {}
190 class CommandOptions : public Options
194 CommandOptions (CommandInterpreter &interpreter) :
195 Options(interpreter),
196 m_level(lldb::eDescriptionLevelBrief) // Watchpoint List defaults to brief descriptions
201 ~CommandOptions () {}
204 SetOptionValue (uint32_t option_idx, const char *option_arg)
207 const int short_option = m_getopt_table[option_idx].val;
209 switch (short_option)
212 m_level = lldb::eDescriptionLevelBrief;
215 m_level = lldb::eDescriptionLevelFull;
218 m_level = lldb::eDescriptionLevelVerbose;
221 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
229 OptionParsingStarting ()
231 m_level = lldb::eDescriptionLevelFull;
234 const OptionDefinition *
237 return g_option_table;
241 // Options table: Required for subclasses of Options.
243 static OptionDefinition g_option_table[];
245 // Instance variables to hold the values for command options.
247 lldb::DescriptionLevel m_level;
252 DoExecute (Args& command, CommandReturnObject &result)
254 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
257 result.AppendError ("Invalid target. No current target or watchpoints.");
258 result.SetStatus (eReturnStatusSuccessFinishNoResult);
262 if (target->GetProcessSP() && target->GetProcessSP()->IsAlive())
264 uint32_t num_supported_hardware_watchpoints;
265 Error error = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
267 result.AppendMessageWithFormat("Number of supported hardware watchpoints: %u\n",
268 num_supported_hardware_watchpoints);
271 const WatchpointList &watchpoints = target->GetWatchpointList();
272 Mutex::Locker locker;
273 target->GetWatchpointList().GetListMutex(locker);
275 size_t num_watchpoints = watchpoints.GetSize();
277 if (num_watchpoints == 0)
279 result.AppendMessage("No watchpoints currently set.");
280 result.SetStatus(eReturnStatusSuccessFinishNoResult);
284 Stream &output_stream = result.GetOutputStream();
286 if (command.GetArgumentCount() == 0)
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)
292 Watchpoint *wp = watchpoints.GetByIndex(i).get();
293 AddWatchpointDescription(&output_stream, wp, m_options.m_level);
295 result.SetStatus(eReturnStatusSuccessFinishNoResult);
299 // Particular watchpoints selected; enable them.
300 std::vector<uint32_t> wp_ids;
301 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
303 result.AppendError("Invalid watchpoints specification.");
304 result.SetStatus(eReturnStatusFailed);
308 const size_t size = wp_ids.size();
309 for (size_t i = 0; i < size; ++i)
311 Watchpoint *wp = watchpoints.FindByID(wp_ids[i]).get();
313 AddWatchpointDescription(&output_stream, wp, m_options.m_level);
314 result.SetStatus(eReturnStatusSuccessFinishNoResult);
318 return result.Succeeded();
322 CommandOptions m_options;
325 //-------------------------------------------------------------------------
326 // CommandObjectWatchpointList::Options
327 //-------------------------------------------------------------------------
328 #pragma mark List::CommandOptions
330 CommandObjectWatchpointList::CommandOptions::g_option_table[] =
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)."},
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."},
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)." },
341 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
344 //-------------------------------------------------------------------------
345 // CommandObjectWatchpointEnable
346 //-------------------------------------------------------------------------
349 class CommandObjectWatchpointEnable : public CommandObjectParsed
352 CommandObjectWatchpointEnable (CommandInterpreter &interpreter) :
353 CommandObjectParsed (interpreter,
355 "Enable the specified disabled watchpoint(s). If no watchpoints are specified, enable all of them.",
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);
365 ~CommandObjectWatchpointEnable () {}
369 DoExecute (Args& command,
370 CommandReturnObject &result)
372 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
373 if (!CheckTargetForWatchpointOperations(target, result))
376 Mutex::Locker locker;
377 target->GetWatchpointList().GetListMutex(locker);
379 const WatchpointList &watchpoints = target->GetWatchpointList();
381 size_t num_watchpoints = watchpoints.GetSize();
383 if (num_watchpoints == 0)
385 result.AppendError("No watchpoints exist to be enabled.");
386 result.SetStatus(eReturnStatusFailed);
390 if (command.GetArgumentCount() == 0)
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);
399 // Particular watchpoints selected; enable them.
400 std::vector<uint32_t> wp_ids;
401 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
403 result.AppendError("Invalid watchpoints specification.");
404 result.SetStatus(eReturnStatusFailed);
409 const size_t size = wp_ids.size();
410 for (size_t i = 0; i < size; ++i)
411 if (target->EnableWatchpointByID(wp_ids[i]))
413 result.AppendMessageWithFormat("%d watchpoints enabled.\n", count);
414 result.SetStatus(eReturnStatusSuccessFinishNoResult);
417 return result.Succeeded();
423 //-------------------------------------------------------------------------
424 // CommandObjectWatchpointDisable
425 //-------------------------------------------------------------------------
428 class CommandObjectWatchpointDisable : public CommandObjectParsed
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.",
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);
445 ~CommandObjectWatchpointDisable () {}
449 DoExecute (Args& command, CommandReturnObject &result)
451 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
452 if (!CheckTargetForWatchpointOperations(target, result))
455 Mutex::Locker locker;
456 target->GetWatchpointList().GetListMutex(locker);
458 const WatchpointList &watchpoints = target->GetWatchpointList();
459 size_t num_watchpoints = watchpoints.GetSize();
461 if (num_watchpoints == 0)
463 result.AppendError("No watchpoints exist to be disabled.");
464 result.SetStatus(eReturnStatusFailed);
468 if (command.GetArgumentCount() == 0)
470 // No watchpoint selected; disable all currently set watchpoints.
471 if (target->DisableAllWatchpoints())
473 result.AppendMessageWithFormat("All watchpoints disabled. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints);
474 result.SetStatus(eReturnStatusSuccessFinishNoResult);
478 result.AppendError("Disable all watchpoints failed\n");
479 result.SetStatus(eReturnStatusFailed);
484 // Particular watchpoints selected; disable them.
485 std::vector<uint32_t> wp_ids;
486 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
488 result.AppendError("Invalid watchpoints specification.");
489 result.SetStatus(eReturnStatusFailed);
494 const size_t size = wp_ids.size();
495 for (size_t i = 0; i < size; ++i)
496 if (target->DisableWatchpointByID(wp_ids[i]))
498 result.AppendMessageWithFormat("%d watchpoints disabled.\n", count);
499 result.SetStatus(eReturnStatusSuccessFinishNoResult);
502 return result.Succeeded();
507 //-------------------------------------------------------------------------
508 // CommandObjectWatchpointDelete
509 //-------------------------------------------------------------------------
512 class CommandObjectWatchpointDelete : public CommandObjectParsed
515 CommandObjectWatchpointDelete (CommandInterpreter &interpreter) :
516 CommandObjectParsed(interpreter,
518 "Delete the specified watchpoint(s). If no watchpoints are specified, delete them all.",
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);
528 ~CommandObjectWatchpointDelete () {}
532 DoExecute (Args& command, CommandReturnObject &result)
534 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
535 if (!CheckTargetForWatchpointOperations(target, result))
538 Mutex::Locker locker;
539 target->GetWatchpointList().GetListMutex(locker);
541 const WatchpointList &watchpoints = target->GetWatchpointList();
543 size_t num_watchpoints = watchpoints.GetSize();
545 if (num_watchpoints == 0)
547 result.AppendError("No watchpoints exist to be deleted.");
548 result.SetStatus(eReturnStatusFailed);
552 if (command.GetArgumentCount() == 0)
554 if (!m_interpreter.Confirm("About to delete all watchpoints, do you want to do that?", true))
556 result.AppendMessage("Operation cancelled...");
560 target->RemoveAllWatchpoints();
561 result.AppendMessageWithFormat("All watchpoints removed. (%" PRIu64 " watchpoints)\n", (uint64_t)num_watchpoints);
563 result.SetStatus (eReturnStatusSuccessFinishNoResult);
567 // Particular watchpoints selected; delete them.
568 std::vector<uint32_t> wp_ids;
569 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
571 result.AppendError("Invalid watchpoints specification.");
572 result.SetStatus(eReturnStatusFailed);
577 const size_t size = wp_ids.size();
578 for (size_t i = 0; i < size; ++i)
579 if (target->RemoveWatchpointByID(wp_ids[i]))
581 result.AppendMessageWithFormat("%d watchpoints deleted.\n",count);
582 result.SetStatus (eReturnStatusSuccessFinishNoResult);
585 return result.Succeeded();
590 //-------------------------------------------------------------------------
591 // CommandObjectWatchpointIgnore
592 //-------------------------------------------------------------------------
594 class CommandObjectWatchpointIgnore : public CommandObjectParsed
597 CommandObjectWatchpointIgnore (CommandInterpreter &interpreter) :
598 CommandObjectParsed (interpreter,
600 "Set ignore count on the specified watchpoint(s). If no watchpoints are specified, set them all.",
602 m_options (interpreter)
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);
611 ~CommandObjectWatchpointIgnore () {}
619 class CommandOptions : public Options
623 CommandOptions (CommandInterpreter &interpreter) :
624 Options (interpreter),
630 ~CommandOptions () {}
633 SetOptionValue (uint32_t option_idx, const char *option_arg)
636 const int short_option = m_getopt_table[option_idx].val;
638 switch (short_option)
642 m_ignore_count = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
643 if (m_ignore_count == UINT32_MAX)
644 error.SetErrorStringWithFormat ("invalid ignore count '%s'", option_arg);
648 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
656 OptionParsingStarting ()
661 const OptionDefinition *
664 return g_option_table;
668 // Options table: Required for subclasses of Options.
670 static OptionDefinition g_option_table[];
672 // Instance variables to hold the values for command options.
674 uint32_t m_ignore_count;
679 DoExecute (Args& command,
680 CommandReturnObject &result)
682 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
683 if (!CheckTargetForWatchpointOperations(target, result))
686 Mutex::Locker locker;
687 target->GetWatchpointList().GetListMutex(locker);
689 const WatchpointList &watchpoints = target->GetWatchpointList();
691 size_t num_watchpoints = watchpoints.GetSize();
693 if (num_watchpoints == 0)
695 result.AppendError("No watchpoints exist to be ignored.");
696 result.SetStatus(eReturnStatusFailed);
700 if (command.GetArgumentCount() == 0)
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);
708 // Particular watchpoints selected; ignore them.
709 std::vector<uint32_t> wp_ids;
710 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
712 result.AppendError("Invalid watchpoints specification.");
713 result.SetStatus(eReturnStatusFailed);
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))
722 result.AppendMessageWithFormat("%d watchpoints ignored.\n",count);
723 result.SetStatus (eReturnStatusSuccessFinishNoResult);
726 return result.Succeeded();
730 CommandOptions m_options;
733 #pragma mark Ignore::CommandOptions
735 CommandObjectWatchpointIgnore::CommandOptions::g_option_table[] =
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 }
742 //-------------------------------------------------------------------------
743 // CommandObjectWatchpointModify
744 //-------------------------------------------------------------------------
747 class CommandObjectWatchpointModify : public CommandObjectParsed
751 CommandObjectWatchpointModify (CommandInterpreter &interpreter) :
752 CommandObjectParsed (interpreter,
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.",
758 m_options (interpreter)
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);
767 ~CommandObjectWatchpointModify () {}
775 class CommandOptions : public Options
779 CommandOptions (CommandInterpreter &interpreter) :
780 Options (interpreter),
782 m_condition_passed (false)
787 ~CommandOptions () {}
790 SetOptionValue (uint32_t option_idx, const char *option_arg)
793 const int short_option = m_getopt_table[option_idx].val;
795 switch (short_option)
798 if (option_arg != NULL)
799 m_condition.assign (option_arg);
802 m_condition_passed = true;
805 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
813 OptionParsingStarting ()
816 m_condition_passed = false;
819 const OptionDefinition*
822 return g_option_table;
825 // Options table: Required for subclasses of Options.
827 static OptionDefinition g_option_table[];
829 // Instance variables to hold the values for command options.
831 std::string m_condition;
832 bool m_condition_passed;
837 DoExecute (Args& command, CommandReturnObject &result)
839 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
840 if (!CheckTargetForWatchpointOperations(target, result))
843 Mutex::Locker locker;
844 target->GetWatchpointList().GetListMutex(locker);
846 const WatchpointList &watchpoints = target->GetWatchpointList();
848 size_t num_watchpoints = watchpoints.GetSize();
850 if (num_watchpoints == 0)
852 result.AppendError("No watchpoints exist to be modified.");
853 result.SetStatus(eReturnStatusFailed);
857 if (command.GetArgumentCount() == 0)
859 WatchpointSP wp_sp = target->GetLastCreatedWatchpoint();
860 wp_sp->SetCondition(m_options.m_condition.c_str());
861 result.SetStatus (eReturnStatusSuccessFinishNoResult);
865 // Particular watchpoints selected; set condition on them.
866 std::vector<uint32_t> wp_ids;
867 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, wp_ids))
869 result.AppendError("Invalid watchpoints specification.");
870 result.SetStatus(eReturnStatusFailed);
875 const size_t size = wp_ids.size();
876 for (size_t i = 0; i < size; ++i)
878 WatchpointSP wp_sp = watchpoints.FindByID(wp_ids[i]);
881 wp_sp->SetCondition(m_options.m_condition.c_str());
885 result.AppendMessageWithFormat("%d watchpoints modified.\n",count);
886 result.SetStatus (eReturnStatusSuccessFinishNoResult);
889 return result.Succeeded();
893 CommandOptions m_options;
896 #pragma mark Modify::CommandOptions
898 CommandObjectWatchpointModify::CommandOptions::g_option_table[] =
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 }
904 //-------------------------------------------------------------------------
905 // CommandObjectWatchpointSetVariable
906 //-------------------------------------------------------------------------
907 #pragma mark SetVariable
909 class CommandObjectWatchpointSetVariable : public CommandObjectParsed
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 "
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.",
927 eFlagTryTargetAPILock |
928 eFlagProcessMustBeLaunched |
929 eFlagProcessMustBePaused ),
930 m_option_group (interpreter),
931 m_option_watchpoint ()
936 watchpoint set variable -w read_write my_global_var \n\
937 # Watch my_global_var for read/write access, with the region to watch corresponding to the byte size of the data type.\n");
939 CommandArgumentEntry arg;
940 CommandArgumentData var_name_arg;
942 // Define the only variant of this arg.
943 var_name_arg.arg_type = eArgTypeVarName;
944 var_name_arg.arg_repetition = eArgRepeatPlain;
946 // Push the variant into the argument entry.
947 arg.push_back (var_name_arg);
949 // Push the data for the only argument into the m_arguments vector.
950 m_arguments.push_back (arg);
952 // Absorb the '-w' and '-x' options into our option group.
953 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
954 m_option_group.Finalize();
958 ~CommandObjectWatchpointSetVariable () {}
963 return &m_option_group;
967 static size_t GetVariableCallback (void *baton,
969 VariableList &variable_list)
971 Target *target = static_cast<Target *>(baton);
974 return target->GetImages().FindGlobalVariables (ConstString(name),
983 DoExecute (Args& command, CommandReturnObject &result)
985 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
986 StackFrame *frame = m_exe_ctx.GetFramePtr();
988 // If no argument is present, issue an error message. There's no way to set a watchpoint.
989 if (command.GetArgumentCount() <= 0)
991 result.GetErrorStream().Printf("error: required argument missing; specify your program variable to watch for\n");
992 result.SetStatus(eReturnStatusFailed);
996 // If no '-w' is specified, default to '-w write'.
997 if (!m_option_watchpoint.watch_type_specified)
999 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite;
1002 // We passed the sanity check for the command.
1003 // Proceed to set the watchpoint now.
1004 lldb::addr_t addr = 0;
1008 ValueObjectSP valobj_sp;
1009 Stream &output_stream = result.GetOutputStream();
1011 // A simple watch variable gesture allows only one argument.
1012 if (command.GetArgumentCount() != 1)
1014 result.GetErrorStream().Printf("error: specify exactly one variable to watch for\n");
1015 result.SetStatus(eReturnStatusFailed);
1019 // Things have checked out ok...
1021 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
1022 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
1023 valobj_sp = frame->GetValueForVariableExpressionPath (command.GetArgumentAtIndex(0),
1031 // Not in the frame; let's check the globals.
1033 VariableList variable_list;
1034 ValueObjectList valobj_list;
1036 Error error (Variable::GetValuesForVariableExpressionPath (command.GetArgumentAtIndex(0),
1037 m_exe_ctx.GetBestExecutionContextScope(),
1038 GetVariableCallback,
1043 if (valobj_list.GetSize())
1044 valobj_sp = valobj_list.GetValueObjectAtIndex(0);
1047 ClangASTType clang_type;
1051 AddressType addr_type;
1052 addr = valobj_sp->GetAddressOf(false, &addr_type);
1053 if (addr_type == eAddressTypeLoad)
1055 // We're in business.
1056 // Find out the size of this variable.
1057 size = m_option_watchpoint.watch_size == 0 ? valobj_sp->GetByteSize()
1058 : m_option_watchpoint.watch_size;
1060 clang_type = valobj_sp->GetClangType();
1064 const char *error_cstr = error.AsCString(NULL);
1066 result.GetErrorStream().Printf("error: %s\n", error_cstr);
1068 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n",
1069 command.GetArgumentAtIndex(0));
1073 // Now it's time to create the watchpoint.
1074 uint32_t watch_type = m_option_watchpoint.watch_type;
1077 Watchpoint *wp = target->CreateWatchpoint(addr, size, &clang_type, watch_type, error).get();
1080 wp->SetWatchSpec(command.GetArgumentAtIndex(0));
1081 wp->SetWatchVariable(true);
1082 if (var_sp && var_sp->GetDeclaration().GetFile())
1085 // True to show fullpath for declaration file.
1086 var_sp->GetDeclaration().DumpStopContext(&ss, true);
1087 wp->SetDeclInfo(ss.GetString());
1089 output_stream.Printf("Watchpoint created: ");
1090 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
1091 output_stream.EOL();
1092 result.SetStatus(eReturnStatusSuccessFinishResult);
1096 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64 ", variable expression='%s').\n",
1097 addr, (uint64_t)size, command.GetArgumentAtIndex(0));
1098 if (error.AsCString(NULL))
1099 result.AppendError(error.AsCString());
1100 result.SetStatus(eReturnStatusFailed);
1103 return result.Succeeded();
1107 OptionGroupOptions m_option_group;
1108 OptionGroupWatchpoint m_option_watchpoint;
1111 //-------------------------------------------------------------------------
1112 // CommandObjectWatchpointSetExpression
1113 //-------------------------------------------------------------------------
1116 class CommandObjectWatchpointSetExpression : public CommandObjectRaw
1120 CommandObjectWatchpointSetExpression (CommandInterpreter &interpreter) :
1121 CommandObjectRaw (interpreter,
1122 "watchpoint set expression",
1123 "Set a watchpoint on an address by supplying an expression. "
1124 "Use the '-w' option to specify the type of watchpoint and "
1125 "the '-x' option to specify the byte size to watch for. "
1126 "If no '-w' option is specified, it defaults to write. "
1127 "If no '-x' option is specified, it defaults to the target's "
1128 "pointer byte size. "
1129 "Note that there are limited hardware resources for watchpoints. "
1130 "If watchpoint setting fails, consider disable/delete existing ones "
1131 "to free up resources.",
1133 eFlagRequiresFrame |
1134 eFlagTryTargetAPILock |
1135 eFlagProcessMustBeLaunched |
1136 eFlagProcessMustBePaused ),
1137 m_option_group (interpreter),
1138 m_option_watchpoint ()
1143 watchpoint set expression -w write -x 1 -- foo + 32\n\
1144 # Watch write access for the 1-byte region pointed to by the address 'foo + 32'.\n");
1146 CommandArgumentEntry arg;
1147 CommandArgumentData expression_arg;
1149 // Define the only variant of this arg.
1150 expression_arg.arg_type = eArgTypeExpression;
1151 expression_arg.arg_repetition = eArgRepeatPlain;
1153 // Push the only variant into the argument entry.
1154 arg.push_back (expression_arg);
1156 // Push the data for the only argument into the m_arguments vector.
1157 m_arguments.push_back (arg);
1159 // Absorb the '-w' and '-x' options into our option group.
1160 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
1161 m_option_group.Finalize();
1166 ~CommandObjectWatchpointSetExpression () {}
1168 // Overrides base class's behavior where WantsCompletion = !WantsRawCommandString.
1170 WantsCompletion() { return true; }
1175 return &m_option_group;
1180 DoExecute (const char *raw_command, CommandReturnObject &result)
1182 m_option_group.NotifyOptionParsingStarting(); // This is a raw command, so notify the option group
1184 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1185 StackFrame *frame = m_exe_ctx.GetFramePtr();
1187 Args command(raw_command);
1188 const char *expr = NULL;
1189 if (raw_command[0] == '-')
1191 // We have some options and these options MUST end with --.
1192 const char *end_options = NULL;
1193 const char *s = raw_command;
1196 end_options = ::strstr (s, "--");
1199 end_options += 2; // Get past the "--"
1200 if (::isspace (end_options[0]))
1203 while (::isspace (*expr))
1213 Args args (raw_command, end_options - raw_command);
1214 if (!ParseOptions (args, result))
1217 Error error (m_option_group.NotifyOptionParsingFinished());
1220 result.AppendError (error.AsCString());
1221 result.SetStatus (eReturnStatusFailed);
1230 // If no argument is present, issue an error message. There's no way to set a watchpoint.
1231 if (command.GetArgumentCount() == 0)
1233 result.GetErrorStream().Printf("error: required argument missing; specify an expression to evaulate into the address to watch for\n");
1234 result.SetStatus(eReturnStatusFailed);
1238 // If no '-w' is specified, default to '-w write'.
1239 if (!m_option_watchpoint.watch_type_specified)
1241 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite;
1244 // We passed the sanity check for the command.
1245 // Proceed to set the watchpoint now.
1246 lldb::addr_t addr = 0;
1249 ValueObjectSP valobj_sp;
1251 // Use expression evaluation to arrive at the address to watch.
1252 EvaluateExpressionOptions options;
1253 options.SetCoerceToId(false);
1254 options.SetUnwindOnError(true);
1255 options.SetKeepInMemory(false);
1256 options.SetTryAllThreads(true);
1257 options.SetTimeoutUsec(0);
1259 ExpressionResults expr_result = target->EvaluateExpression (expr,
1263 if (expr_result != eExpressionCompleted)
1265 result.GetErrorStream().Printf("error: expression evaluation of address to watch failed\n");
1266 result.GetErrorStream().Printf("expression evaluated: %s\n", expr);
1267 result.SetStatus(eReturnStatusFailed);
1271 // Get the address to watch.
1272 bool success = false;
1273 addr = valobj_sp->GetValueAsUnsigned(0, &success);
1276 result.GetErrorStream().Printf("error: expression did not evaluate to an address\n");
1277 result.SetStatus(eReturnStatusFailed);
1281 if (m_option_watchpoint.watch_size != 0)
1282 size = m_option_watchpoint.watch_size;
1284 size = target->GetArchitecture().GetAddressByteSize();
1286 // Now it's time to create the watchpoint.
1287 uint32_t watch_type = m_option_watchpoint.watch_type;
1289 // Fetch the type from the value object, the type of the watched object is the pointee type
1290 /// of the expression, so convert to that if we found a valid type.
1291 ClangASTType clang_type(valobj_sp->GetClangType());
1294 Watchpoint *wp = target->CreateWatchpoint(addr, size, &clang_type, watch_type, error).get();
1297 Stream &output_stream = result.GetOutputStream();
1298 output_stream.Printf("Watchpoint created: ");
1299 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
1300 output_stream.EOL();
1301 result.SetStatus(eReturnStatusSuccessFinishResult);
1305 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64 ").\n",
1306 addr, (uint64_t)size);
1307 if (error.AsCString(NULL))
1308 result.AppendError(error.AsCString());
1309 result.SetStatus(eReturnStatusFailed);
1312 return result.Succeeded();
1316 OptionGroupOptions m_option_group;
1317 OptionGroupWatchpoint m_option_watchpoint;
1320 //-------------------------------------------------------------------------
1321 // CommandObjectWatchpointSet
1322 //-------------------------------------------------------------------------
1325 class CommandObjectWatchpointSet : public CommandObjectMultiword
1329 CommandObjectWatchpointSet (CommandInterpreter &interpreter) :
1330 CommandObjectMultiword (interpreter,
1332 "A set of commands for setting a watchpoint.",
1333 "watchpoint set <subcommand> [<subcommand-options>]")
1336 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectWatchpointSetVariable (interpreter)));
1337 LoadSubCommand ("expression", CommandObjectSP (new CommandObjectWatchpointSetExpression (interpreter)));
1342 ~CommandObjectWatchpointSet () {}
1346 //-------------------------------------------------------------------------
1347 // CommandObjectMultiwordWatchpoint
1348 //-------------------------------------------------------------------------
1349 #pragma mark MultiwordWatchpoint
1351 CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(CommandInterpreter &interpreter) :
1352 CommandObjectMultiword (interpreter,
1354 "A set of commands for operating on watchpoints.",
1355 "watchpoint <command> [<command-options>]")
1357 CommandObjectSP list_command_object (new CommandObjectWatchpointList (interpreter));
1358 CommandObjectSP enable_command_object (new CommandObjectWatchpointEnable (interpreter));
1359 CommandObjectSP disable_command_object (new CommandObjectWatchpointDisable (interpreter));
1360 CommandObjectSP delete_command_object (new CommandObjectWatchpointDelete (interpreter));
1361 CommandObjectSP ignore_command_object (new CommandObjectWatchpointIgnore (interpreter));
1362 CommandObjectSP command_command_object (new CommandObjectWatchpointCommand (interpreter));
1363 CommandObjectSP modify_command_object (new CommandObjectWatchpointModify (interpreter));
1364 CommandObjectSP set_command_object (new CommandObjectWatchpointSet (interpreter));
1366 list_command_object->SetCommandName ("watchpoint list");
1367 enable_command_object->SetCommandName("watchpoint enable");
1368 disable_command_object->SetCommandName("watchpoint disable");
1369 delete_command_object->SetCommandName("watchpoint delete");
1370 ignore_command_object->SetCommandName("watchpoint ignore");
1371 command_command_object->SetCommandName ("watchpoint command");
1372 modify_command_object->SetCommandName("watchpoint modify");
1373 set_command_object->SetCommandName("watchpoint set");
1375 LoadSubCommand ("list", list_command_object);
1376 LoadSubCommand ("enable", enable_command_object);
1377 LoadSubCommand ("disable", disable_command_object);
1378 LoadSubCommand ("delete", delete_command_object);
1379 LoadSubCommand ("ignore", ignore_command_object);
1380 LoadSubCommand ("command", command_command_object);
1381 LoadSubCommand ("modify", modify_command_object);
1382 LoadSubCommand ("set", set_command_object);
1385 CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint()