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