15ffd83dbSDimitry Andric //===-- CommandObjectWatchpoint.cpp ---------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectWatchpoint.h"
100b57cec5SDimitry Andric #include "CommandObjectWatchpointCommand.h"
110b57cec5SDimitry Andric 
12fe013be4SDimitry Andric #include <memory>
130b57cec5SDimitry Andric #include <vector>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h"
180b57cec5SDimitry Andric #include "lldb/Breakpoint/WatchpointList.h"
190b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
200b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
210b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
22fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
230b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
24fe013be4SDimitry Andric #include "lldb/Symbol/Function.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
260b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
270b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
280b57cec5SDimitry Andric #include "lldb/Target/Target.h"
290b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using namespace lldb;
320b57cec5SDimitry Andric using namespace lldb_private;
330b57cec5SDimitry Andric 
AddWatchpointDescription(Stream & s,Watchpoint & wp,lldb::DescriptionLevel level)34fe013be4SDimitry Andric static void AddWatchpointDescription(Stream &s, Watchpoint &wp,
350b57cec5SDimitry Andric                                      lldb::DescriptionLevel level) {
36fe013be4SDimitry Andric   s.IndentMore();
37fe013be4SDimitry Andric   wp.GetDescription(&s, level);
38fe013be4SDimitry Andric   s.IndentLess();
39fe013be4SDimitry Andric   s.EOL();
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
CheckTargetForWatchpointOperations(Target * target,CommandReturnObject & result)420b57cec5SDimitry Andric static bool CheckTargetForWatchpointOperations(Target *target,
430b57cec5SDimitry Andric                                                CommandReturnObject &result) {
440b57cec5SDimitry Andric   bool process_is_valid =
450b57cec5SDimitry Andric       target->GetProcessSP() && target->GetProcessSP()->IsAlive();
460b57cec5SDimitry Andric   if (!process_is_valid) {
475ffd83dbSDimitry Andric     result.AppendError("There's no process or it is not alive.");
480b57cec5SDimitry Andric     return false;
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric   // Target passes our checks, return true.
510b57cec5SDimitry Andric   return true;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
550b57cec5SDimitry Andric static const char *RSA[4] = {"-", "to", "To", "TO"};
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric // Return the index to RSA if found; otherwise -1 is returned.
WithRSAIndex(llvm::StringRef Arg)580b57cec5SDimitry Andric static int32_t WithRSAIndex(llvm::StringRef Arg) {
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   uint32_t i;
610b57cec5SDimitry Andric   for (i = 0; i < 4; ++i)
62349cc55cSDimitry Andric     if (Arg.contains(RSA[i]))
630b57cec5SDimitry Andric       return i;
640b57cec5SDimitry Andric   return -1;
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric // Return true if wp_ids is successfully populated with the watch ids. False
680b57cec5SDimitry Andric // otherwise.
VerifyWatchpointIDs(Target * target,Args & args,std::vector<uint32_t> & wp_ids)690b57cec5SDimitry Andric bool CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
700b57cec5SDimitry Andric     Target *target, Args &args, std::vector<uint32_t> &wp_ids) {
710b57cec5SDimitry Andric   // Pre-condition: args.GetArgumentCount() > 0.
720b57cec5SDimitry Andric   if (args.GetArgumentCount() == 0) {
730b57cec5SDimitry Andric     if (target == nullptr)
740b57cec5SDimitry Andric       return false;
750b57cec5SDimitry Andric     WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
760b57cec5SDimitry Andric     if (watch_sp) {
770b57cec5SDimitry Andric       wp_ids.push_back(watch_sp->GetID());
780b57cec5SDimitry Andric       return true;
790b57cec5SDimitry Andric     } else
800b57cec5SDimitry Andric       return false;
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   llvm::StringRef Minus("-");
840b57cec5SDimitry Andric   std::vector<llvm::StringRef> StrRefArgs;
850b57cec5SDimitry Andric   llvm::StringRef first;
860b57cec5SDimitry Andric   llvm::StringRef second;
870b57cec5SDimitry Andric   size_t i;
880b57cec5SDimitry Andric   int32_t idx;
890b57cec5SDimitry Andric   // Go through the arguments and make a canonical form of arg list containing
900b57cec5SDimitry Andric   // only numbers with possible "-" in between.
910b57cec5SDimitry Andric   for (auto &entry : args.entries()) {
929dba64beSDimitry Andric     if ((idx = WithRSAIndex(entry.ref())) == -1) {
939dba64beSDimitry Andric       StrRefArgs.push_back(entry.ref());
940b57cec5SDimitry Andric       continue;
950b57cec5SDimitry Andric     }
960b57cec5SDimitry Andric     // The Arg contains the range specifier, split it, then.
979dba64beSDimitry Andric     std::tie(first, second) = entry.ref().split(RSA[idx]);
980b57cec5SDimitry Andric     if (!first.empty())
990b57cec5SDimitry Andric       StrRefArgs.push_back(first);
1000b57cec5SDimitry Andric     StrRefArgs.push_back(Minus);
1010b57cec5SDimitry Andric     if (!second.empty())
1020b57cec5SDimitry Andric       StrRefArgs.push_back(second);
1030b57cec5SDimitry Andric   }
1040b57cec5SDimitry Andric   // Now process the canonical list and fill in the vector of uint32_t's. If
1050b57cec5SDimitry Andric   // there is any error, return false and the client should ignore wp_ids.
1060b57cec5SDimitry Andric   uint32_t beg, end, id;
1070b57cec5SDimitry Andric   size_t size = StrRefArgs.size();
1080b57cec5SDimitry Andric   bool in_range = false;
1090b57cec5SDimitry Andric   for (i = 0; i < size; ++i) {
1100b57cec5SDimitry Andric     llvm::StringRef Arg = StrRefArgs[i];
1110b57cec5SDimitry Andric     if (in_range) {
1120b57cec5SDimitry Andric       // Look for the 'end' of the range.  Note StringRef::getAsInteger()
1130b57cec5SDimitry Andric       // returns true to signify error while parsing.
1140b57cec5SDimitry Andric       if (Arg.getAsInteger(0, end))
1150b57cec5SDimitry Andric         return false;
1160b57cec5SDimitry Andric       // Found a range!  Now append the elements.
1170b57cec5SDimitry Andric       for (id = beg; id <= end; ++id)
1180b57cec5SDimitry Andric         wp_ids.push_back(id);
1190b57cec5SDimitry Andric       in_range = false;
1200b57cec5SDimitry Andric       continue;
1210b57cec5SDimitry Andric     }
1220b57cec5SDimitry Andric     if (i < (size - 1) && StrRefArgs[i + 1] == Minus) {
1230b57cec5SDimitry Andric       if (Arg.getAsInteger(0, beg))
1240b57cec5SDimitry Andric         return false;
1250b57cec5SDimitry Andric       // Turn on the in_range flag, we are looking for end of range next.
1260b57cec5SDimitry Andric       ++i;
1270b57cec5SDimitry Andric       in_range = true;
1280b57cec5SDimitry Andric       continue;
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric     // Otherwise, we have a simple ID.  Just append it.
1310b57cec5SDimitry Andric     if (Arg.getAsInteger(0, beg))
1320b57cec5SDimitry Andric       return false;
1330b57cec5SDimitry Andric     wp_ids.push_back(beg);
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   // It is an error if after the loop, we're still in_range.
1370b57cec5SDimitry Andric   return !in_range;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric // CommandObjectWatchpointList
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric // CommandObjectWatchpointList::Options
1430b57cec5SDimitry Andric #pragma mark List::CommandOptions
1440b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_list
1450b57cec5SDimitry Andric #include "CommandOptions.inc"
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric #pragma mark List
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric class CommandObjectWatchpointList : public CommandObjectParsed {
1500b57cec5SDimitry Andric public:
CommandObjectWatchpointList(CommandInterpreter & interpreter)1510b57cec5SDimitry Andric   CommandObjectWatchpointList(CommandInterpreter &interpreter)
1520b57cec5SDimitry Andric       : CommandObjectParsed(
1530b57cec5SDimitry Andric             interpreter, "watchpoint list",
1549dba64beSDimitry Andric             "List all watchpoints at configurable levels of detail.", nullptr,
15504eeddc0SDimitry Andric             eCommandRequiresTarget) {
1560b57cec5SDimitry Andric     CommandArgumentEntry arg;
1570b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
1580b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
1590b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
1600b57cec5SDimitry Andric     // arguments vector.
1610b57cec5SDimitry Andric     m_arguments.push_back(arg);
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   ~CommandObjectWatchpointList() override = default;
1650b57cec5SDimitry Andric 
GetOptions()1660b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   class CommandOptions : public Options {
1690b57cec5SDimitry Andric   public:
17081ad6265SDimitry Andric     CommandOptions() = default;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric     ~CommandOptions() override = default;
1730b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)1740b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1750b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1760b57cec5SDimitry Andric       Status error;
1770b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric       switch (short_option) {
1800b57cec5SDimitry Andric       case 'b':
1810b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelBrief;
1820b57cec5SDimitry Andric         break;
1830b57cec5SDimitry Andric       case 'f':
1840b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelFull;
1850b57cec5SDimitry Andric         break;
1860b57cec5SDimitry Andric       case 'v':
1870b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelVerbose;
1880b57cec5SDimitry Andric         break;
1890b57cec5SDimitry Andric       default:
1909dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1910b57cec5SDimitry Andric       }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric       return error;
1940b57cec5SDimitry Andric     }
1950b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)1960b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1970b57cec5SDimitry Andric       m_level = lldb::eDescriptionLevelFull;
1980b57cec5SDimitry Andric     }
1990b57cec5SDimitry Andric 
GetDefinitions()2000b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
201bdd1243dSDimitry Andric       return llvm::ArrayRef(g_watchpoint_list_options);
2020b57cec5SDimitry Andric     }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
2050b57cec5SDimitry Andric 
206fe6060f1SDimitry Andric     lldb::DescriptionLevel m_level = lldb::eDescriptionLevelBrief;
2070b57cec5SDimitry Andric   };
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)210*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
2119dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric     if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) {
214fe013be4SDimitry Andric       std::optional<uint32_t> num_supported_hardware_watchpoints =
215fe013be4SDimitry Andric           target->GetProcessSP()->GetWatchpointSlotCount();
216fe013be4SDimitry Andric 
217fe013be4SDimitry Andric       if (num_supported_hardware_watchpoints)
2180b57cec5SDimitry Andric         result.AppendMessageWithFormat(
2190b57cec5SDimitry Andric             "Number of supported hardware watchpoints: %u\n",
220fe013be4SDimitry Andric             *num_supported_hardware_watchpoints);
2210b57cec5SDimitry Andric     }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
2260b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     if (num_watchpoints == 0) {
2310b57cec5SDimitry Andric       result.AppendMessage("No watchpoints currently set.");
2320b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
233*c9157d92SDimitry Andric       return;
2340b57cec5SDimitry Andric     }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
2390b57cec5SDimitry Andric       // No watchpoint selected; show info about all currently set watchpoints.
2400b57cec5SDimitry Andric       result.AppendMessage("Current watchpoints:");
2410b57cec5SDimitry Andric       for (size_t i = 0; i < num_watchpoints; ++i) {
242fe013be4SDimitry Andric         WatchpointSP watch_sp = watchpoints.GetByIndex(i);
243fe013be4SDimitry Andric         AddWatchpointDescription(output_stream, *watch_sp, m_options.m_level);
2440b57cec5SDimitry Andric       }
2450b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2460b57cec5SDimitry Andric     } else {
2470b57cec5SDimitry Andric       // Particular watchpoints selected; enable them.
2480b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
2490b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
2500b57cec5SDimitry Andric               target, command, wp_ids)) {
2510b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
252*c9157d92SDimitry Andric         return;
2530b57cec5SDimitry Andric       }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric       const size_t size = wp_ids.size();
2560b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i) {
257fe013be4SDimitry Andric         WatchpointSP watch_sp = watchpoints.FindByID(wp_ids[i]);
258fe013be4SDimitry Andric         if (watch_sp)
259fe013be4SDimitry Andric           AddWatchpointDescription(output_stream, *watch_sp, m_options.m_level);
2600b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
2610b57cec5SDimitry Andric       }
2620b57cec5SDimitry Andric     }
2630b57cec5SDimitry Andric   }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric private:
2660b57cec5SDimitry Andric   CommandOptions m_options;
2670b57cec5SDimitry Andric };
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric // CommandObjectWatchpointEnable
2700b57cec5SDimitry Andric #pragma mark Enable
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric class CommandObjectWatchpointEnable : public CommandObjectParsed {
2730b57cec5SDimitry Andric public:
CommandObjectWatchpointEnable(CommandInterpreter & interpreter)2740b57cec5SDimitry Andric   CommandObjectWatchpointEnable(CommandInterpreter &interpreter)
2750b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "enable",
2760b57cec5SDimitry Andric                             "Enable the specified disabled watchpoint(s). If "
2770b57cec5SDimitry Andric                             "no watchpoints are specified, enable all of them.",
2789dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget) {
2790b57cec5SDimitry Andric     CommandArgumentEntry arg;
2800b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
2810b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
2820b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
2830b57cec5SDimitry Andric     // arguments vector.
2840b57cec5SDimitry Andric     m_arguments.push_back(arg);
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   ~CommandObjectWatchpointEnable() override = default;
2880b57cec5SDimitry Andric 
289e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)290e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
291e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
292fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
293fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eWatchpointIDCompletion, request,
294fe013be4SDimitry Andric         nullptr);
295e8d8bef9SDimitry Andric   }
296e8d8bef9SDimitry Andric 
2970b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)298*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
2999dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
3000b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
301*c9157d92SDimitry Andric       return;
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
3040b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric     if (num_watchpoints == 0) {
3110b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be enabled.");
312*c9157d92SDimitry Andric       return;
3130b57cec5SDimitry Andric     }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
3160b57cec5SDimitry Andric       // No watchpoint selected; enable all currently set watchpoints.
3170b57cec5SDimitry Andric       target->EnableAllWatchpoints();
3180b57cec5SDimitry Andric       result.AppendMessageWithFormat("All watchpoints enabled. (%" PRIu64
3190b57cec5SDimitry Andric                                      " watchpoints)\n",
3200b57cec5SDimitry Andric                                      (uint64_t)num_watchpoints);
3210b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
3220b57cec5SDimitry Andric     } else {
3230b57cec5SDimitry Andric       // Particular watchpoints selected; enable them.
3240b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
3250b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
3260b57cec5SDimitry Andric               target, command, wp_ids)) {
3270b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
328*c9157d92SDimitry Andric         return;
3290b57cec5SDimitry Andric       }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric       int count = 0;
3320b57cec5SDimitry Andric       const size_t size = wp_ids.size();
3330b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i)
3340b57cec5SDimitry Andric         if (target->EnableWatchpointByID(wp_ids[i]))
3350b57cec5SDimitry Andric           ++count;
3360b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints enabled.\n", count);
3370b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
3380b57cec5SDimitry Andric     }
3390b57cec5SDimitry Andric   }
3400b57cec5SDimitry Andric };
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric // CommandObjectWatchpointDisable
3430b57cec5SDimitry Andric #pragma mark Disable
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric class CommandObjectWatchpointDisable : public CommandObjectParsed {
3460b57cec5SDimitry Andric public:
CommandObjectWatchpointDisable(CommandInterpreter & interpreter)3470b57cec5SDimitry Andric   CommandObjectWatchpointDisable(CommandInterpreter &interpreter)
3480b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint disable",
3490b57cec5SDimitry Andric                             "Disable the specified watchpoint(s) without "
3500b57cec5SDimitry Andric                             "removing it/them.  If no watchpoints are "
3510b57cec5SDimitry Andric                             "specified, disable them all.",
3529dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget) {
3530b57cec5SDimitry Andric     CommandArgumentEntry arg;
3540b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
3550b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
3560b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
3570b57cec5SDimitry Andric     // arguments vector.
3580b57cec5SDimitry Andric     m_arguments.push_back(arg);
3590b57cec5SDimitry Andric   }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   ~CommandObjectWatchpointDisable() override = default;
3620b57cec5SDimitry Andric 
363e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)364e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
365e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
366fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
367fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eWatchpointIDCompletion, request,
368fe013be4SDimitry Andric         nullptr);
369e8d8bef9SDimitry Andric   }
370e8d8bef9SDimitry Andric 
3710b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)372*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
3739dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
3740b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
375*c9157d92SDimitry Andric       return;
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
3780b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
3810b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric     if (num_watchpoints == 0) {
3840b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be disabled.");
385*c9157d92SDimitry Andric       return;
3860b57cec5SDimitry Andric     }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
3890b57cec5SDimitry Andric       // No watchpoint selected; disable all currently set watchpoints.
3900b57cec5SDimitry Andric       if (target->DisableAllWatchpoints()) {
3910b57cec5SDimitry Andric         result.AppendMessageWithFormat("All watchpoints disabled. (%" PRIu64
3920b57cec5SDimitry Andric                                        " watchpoints)\n",
3930b57cec5SDimitry Andric                                        (uint64_t)num_watchpoints);
3940b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
3950b57cec5SDimitry Andric       } else {
3960b57cec5SDimitry Andric         result.AppendError("Disable all watchpoints failed\n");
3970b57cec5SDimitry Andric       }
3980b57cec5SDimitry Andric     } else {
3990b57cec5SDimitry Andric       // Particular watchpoints selected; disable them.
4000b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
4010b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
4020b57cec5SDimitry Andric               target, command, wp_ids)) {
4030b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
404*c9157d92SDimitry Andric         return;
4050b57cec5SDimitry Andric       }
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric       int count = 0;
4080b57cec5SDimitry Andric       const size_t size = wp_ids.size();
4090b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i)
4100b57cec5SDimitry Andric         if (target->DisableWatchpointByID(wp_ids[i]))
4110b57cec5SDimitry Andric           ++count;
4120b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints disabled.\n", count);
4130b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
4140b57cec5SDimitry Andric     }
4150b57cec5SDimitry Andric   }
4160b57cec5SDimitry Andric };
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric // CommandObjectWatchpointDelete
419480093f4SDimitry Andric #define LLDB_OPTIONS_watchpoint_delete
420480093f4SDimitry Andric #include "CommandOptions.inc"
421480093f4SDimitry Andric 
422480093f4SDimitry Andric // CommandObjectWatchpointDelete
4230b57cec5SDimitry Andric #pragma mark Delete
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric class CommandObjectWatchpointDelete : public CommandObjectParsed {
4260b57cec5SDimitry Andric public:
CommandObjectWatchpointDelete(CommandInterpreter & interpreter)4270b57cec5SDimitry Andric   CommandObjectWatchpointDelete(CommandInterpreter &interpreter)
4280b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint delete",
4290b57cec5SDimitry Andric                             "Delete the specified watchpoint(s).  If no "
4300b57cec5SDimitry Andric                             "watchpoints are specified, delete them all.",
43104eeddc0SDimitry Andric                             nullptr, eCommandRequiresTarget) {
4320b57cec5SDimitry Andric     CommandArgumentEntry arg;
4330b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
4340b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
4350b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
4360b57cec5SDimitry Andric     // arguments vector.
4370b57cec5SDimitry Andric     m_arguments.push_back(arg);
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   ~CommandObjectWatchpointDelete() override = default;
4410b57cec5SDimitry Andric 
442e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)443e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
444e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
445fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
446fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eWatchpointIDCompletion, request,
447fe013be4SDimitry Andric         nullptr);
448e8d8bef9SDimitry Andric   }
449e8d8bef9SDimitry Andric 
GetOptions()450480093f4SDimitry Andric   Options *GetOptions() override { return &m_options; }
451480093f4SDimitry Andric 
452480093f4SDimitry Andric   class CommandOptions : public Options {
453480093f4SDimitry Andric   public:
45481ad6265SDimitry Andric     CommandOptions() = default;
455480093f4SDimitry Andric 
456480093f4SDimitry Andric     ~CommandOptions() override = default;
457480093f4SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)458480093f4SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
459480093f4SDimitry Andric                           ExecutionContext *execution_context) override {
460480093f4SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
461480093f4SDimitry Andric 
462480093f4SDimitry Andric       switch (short_option) {
463480093f4SDimitry Andric       case 'f':
464480093f4SDimitry Andric         m_force = true;
465480093f4SDimitry Andric         break;
466480093f4SDimitry Andric       default:
467480093f4SDimitry Andric         llvm_unreachable("Unimplemented option");
468480093f4SDimitry Andric       }
469480093f4SDimitry Andric 
470480093f4SDimitry Andric       return {};
471480093f4SDimitry Andric     }
472480093f4SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)473480093f4SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
474480093f4SDimitry Andric       m_force = false;
475480093f4SDimitry Andric     }
476480093f4SDimitry Andric 
GetDefinitions()477480093f4SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
478bdd1243dSDimitry Andric       return llvm::ArrayRef(g_watchpoint_delete_options);
479480093f4SDimitry Andric     }
480480093f4SDimitry Andric 
481480093f4SDimitry Andric     // Instance variables to hold the values for command options.
482fe6060f1SDimitry Andric     bool m_force = false;
483480093f4SDimitry Andric   };
484480093f4SDimitry Andric 
4850b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)486*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
4879dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
4880b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
489*c9157d92SDimitry Andric       return;
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
4920b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric     if (num_watchpoints == 0) {
4990b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be deleted.");
500*c9157d92SDimitry Andric       return;
5010b57cec5SDimitry Andric     }
5020b57cec5SDimitry Andric 
503480093f4SDimitry Andric     if (command.empty()) {
504480093f4SDimitry Andric       if (!m_options.m_force &&
505480093f4SDimitry Andric           !m_interpreter.Confirm(
5060b57cec5SDimitry Andric               "About to delete all watchpoints, do you want to do that?",
5070b57cec5SDimitry Andric               true)) {
5080b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
5090b57cec5SDimitry Andric       } else {
5100b57cec5SDimitry Andric         target->RemoveAllWatchpoints();
5110b57cec5SDimitry Andric         result.AppendMessageWithFormat("All watchpoints removed. (%" PRIu64
5120b57cec5SDimitry Andric                                        " watchpoints)\n",
5130b57cec5SDimitry Andric                                        (uint64_t)num_watchpoints);
5140b57cec5SDimitry Andric       }
5150b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
516*c9157d92SDimitry Andric       return;
517480093f4SDimitry Andric     }
518480093f4SDimitry Andric 
5190b57cec5SDimitry Andric     // Particular watchpoints selected; delete them.
5200b57cec5SDimitry Andric     std::vector<uint32_t> wp_ids;
521480093f4SDimitry Andric     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
522480093f4SDimitry Andric                                                                wp_ids)) {
5230b57cec5SDimitry Andric       result.AppendError("Invalid watchpoints specification.");
524*c9157d92SDimitry Andric       return;
5250b57cec5SDimitry Andric     }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric     int count = 0;
5280b57cec5SDimitry Andric     const size_t size = wp_ids.size();
5290b57cec5SDimitry Andric     for (size_t i = 0; i < size; ++i)
5300b57cec5SDimitry Andric       if (target->RemoveWatchpointByID(wp_ids[i]))
5310b57cec5SDimitry Andric         ++count;
5320b57cec5SDimitry Andric     result.AppendMessageWithFormat("%d watchpoints deleted.\n", count);
5330b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
5340b57cec5SDimitry Andric   }
535480093f4SDimitry Andric 
536480093f4SDimitry Andric private:
537480093f4SDimitry Andric   CommandOptions m_options;
5380b57cec5SDimitry Andric };
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric // CommandObjectWatchpointIgnore
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric #pragma mark Ignore::CommandOptions
5430b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_ignore
5440b57cec5SDimitry Andric #include "CommandOptions.inc"
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric class CommandObjectWatchpointIgnore : public CommandObjectParsed {
5470b57cec5SDimitry Andric public:
CommandObjectWatchpointIgnore(CommandInterpreter & interpreter)5480b57cec5SDimitry Andric   CommandObjectWatchpointIgnore(CommandInterpreter &interpreter)
5490b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint ignore",
5500b57cec5SDimitry Andric                             "Set ignore count on the specified watchpoint(s).  "
5510b57cec5SDimitry Andric                             "If no watchpoints are specified, set them all.",
55204eeddc0SDimitry Andric                             nullptr, eCommandRequiresTarget) {
5530b57cec5SDimitry Andric     CommandArgumentEntry arg;
5540b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
5550b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
5560b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
5570b57cec5SDimitry Andric     // arguments vector.
5580b57cec5SDimitry Andric     m_arguments.push_back(arg);
5590b57cec5SDimitry Andric   }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   ~CommandObjectWatchpointIgnore() override = default;
5620b57cec5SDimitry Andric 
563e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)564e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
565e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
566fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
567fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eWatchpointIDCompletion, request,
568fe013be4SDimitry Andric         nullptr);
569e8d8bef9SDimitry Andric   }
570e8d8bef9SDimitry Andric 
GetOptions()5710b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   class CommandOptions : public Options {
5740b57cec5SDimitry Andric   public:
57581ad6265SDimitry Andric     CommandOptions() = default;
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     ~CommandOptions() override = default;
5780b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)5790b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
5800b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
5810b57cec5SDimitry Andric       Status error;
5820b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric       switch (short_option) {
5850b57cec5SDimitry Andric       case 'i':
5860b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_ignore_count))
5870b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid ignore count '%s'",
5880b57cec5SDimitry Andric                                          option_arg.str().c_str());
5890b57cec5SDimitry Andric         break;
5900b57cec5SDimitry Andric       default:
5919dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
5920b57cec5SDimitry Andric       }
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric       return error;
5950b57cec5SDimitry Andric     }
5960b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)5970b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
5980b57cec5SDimitry Andric       m_ignore_count = 0;
5990b57cec5SDimitry Andric     }
6000b57cec5SDimitry Andric 
GetDefinitions()6010b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
602bdd1243dSDimitry Andric       return llvm::ArrayRef(g_watchpoint_ignore_options);
6030b57cec5SDimitry Andric     }
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
6060b57cec5SDimitry Andric 
607fe6060f1SDimitry Andric     uint32_t m_ignore_count = 0;
6080b57cec5SDimitry Andric   };
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)611*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
6129dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
6130b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
614*c9157d92SDimitry Andric       return;
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
6170b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric     if (num_watchpoints == 0) {
6240b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be ignored.");
625*c9157d92SDimitry Andric       return;
6260b57cec5SDimitry Andric     }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
6290b57cec5SDimitry Andric       target->IgnoreAllWatchpoints(m_options.m_ignore_count);
6300b57cec5SDimitry Andric       result.AppendMessageWithFormat("All watchpoints ignored. (%" PRIu64
6310b57cec5SDimitry Andric                                      " watchpoints)\n",
6320b57cec5SDimitry Andric                                      (uint64_t)num_watchpoints);
6330b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
6340b57cec5SDimitry Andric     } else {
6350b57cec5SDimitry Andric       // Particular watchpoints selected; ignore them.
6360b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
6370b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
6380b57cec5SDimitry Andric               target, command, wp_ids)) {
6390b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
640*c9157d92SDimitry Andric         return;
6410b57cec5SDimitry Andric       }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric       int count = 0;
6440b57cec5SDimitry Andric       const size_t size = wp_ids.size();
6450b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i)
6460b57cec5SDimitry Andric         if (target->IgnoreWatchpointByID(wp_ids[i], m_options.m_ignore_count))
6470b57cec5SDimitry Andric           ++count;
6480b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints ignored.\n", count);
6490b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
6500b57cec5SDimitry Andric     }
6510b57cec5SDimitry Andric   }
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric private:
6540b57cec5SDimitry Andric   CommandOptions m_options;
6550b57cec5SDimitry Andric };
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric // CommandObjectWatchpointModify
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric #pragma mark Modify::CommandOptions
6600b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_modify
6610b57cec5SDimitry Andric #include "CommandOptions.inc"
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric #pragma mark Modify
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric class CommandObjectWatchpointModify : public CommandObjectParsed {
6660b57cec5SDimitry Andric public:
CommandObjectWatchpointModify(CommandInterpreter & interpreter)6670b57cec5SDimitry Andric   CommandObjectWatchpointModify(CommandInterpreter &interpreter)
6680b57cec5SDimitry Andric       : CommandObjectParsed(
6690b57cec5SDimitry Andric             interpreter, "watchpoint modify",
6700b57cec5SDimitry Andric             "Modify the options on a watchpoint or set of watchpoints in the "
6710b57cec5SDimitry Andric             "executable.  "
6720b57cec5SDimitry Andric             "If no watchpoint is specified, act on the last created "
6730b57cec5SDimitry Andric             "watchpoint.  "
6740b57cec5SDimitry Andric             "Passing an empty argument clears the modification.",
67504eeddc0SDimitry Andric             nullptr, eCommandRequiresTarget) {
6760b57cec5SDimitry Andric     CommandArgumentEntry arg;
6770b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
6780b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
6790b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
6800b57cec5SDimitry Andric     // arguments vector.
6810b57cec5SDimitry Andric     m_arguments.push_back(arg);
6820b57cec5SDimitry Andric   }
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric   ~CommandObjectWatchpointModify() override = default;
6850b57cec5SDimitry Andric 
686e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)687e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
688e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
689fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
690fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eWatchpointIDCompletion, request,
691fe013be4SDimitry Andric         nullptr);
692e8d8bef9SDimitry Andric   }
693e8d8bef9SDimitry Andric 
GetOptions()6940b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric   class CommandOptions : public Options {
6970b57cec5SDimitry Andric   public:
69881ad6265SDimitry Andric     CommandOptions() = default;
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric     ~CommandOptions() override = default;
7010b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)7020b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7030b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7040b57cec5SDimitry Andric       Status error;
7050b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric       switch (short_option) {
7080b57cec5SDimitry Andric       case 'c':
7095ffd83dbSDimitry Andric         m_condition = std::string(option_arg);
7100b57cec5SDimitry Andric         m_condition_passed = true;
7110b57cec5SDimitry Andric         break;
7120b57cec5SDimitry Andric       default:
7139dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7140b57cec5SDimitry Andric       }
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric       return error;
7170b57cec5SDimitry Andric     }
7180b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)7190b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7200b57cec5SDimitry Andric       m_condition.clear();
7210b57cec5SDimitry Andric       m_condition_passed = false;
7220b57cec5SDimitry Andric     }
7230b57cec5SDimitry Andric 
GetDefinitions()7240b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
725bdd1243dSDimitry Andric       return llvm::ArrayRef(g_watchpoint_modify_options);
7260b57cec5SDimitry Andric     }
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric     std::string m_condition;
731fe6060f1SDimitry Andric     bool m_condition_passed = false;
7320b57cec5SDimitry Andric   };
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)735*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
7369dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
7370b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
738*c9157d92SDimitry Andric       return;
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
7410b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric     if (num_watchpoints == 0) {
7480b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be modified.");
749*c9157d92SDimitry Andric       return;
7500b57cec5SDimitry Andric     }
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
753fe013be4SDimitry Andric       WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
754fe013be4SDimitry Andric       watch_sp->SetCondition(m_options.m_condition.c_str());
7550b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
7560b57cec5SDimitry Andric     } else {
7570b57cec5SDimitry Andric       // Particular watchpoints selected; set condition on them.
7580b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
7590b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
7600b57cec5SDimitry Andric               target, command, wp_ids)) {
7610b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
762*c9157d92SDimitry Andric         return;
7630b57cec5SDimitry Andric       }
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric       int count = 0;
7660b57cec5SDimitry Andric       const size_t size = wp_ids.size();
7670b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i) {
768fe013be4SDimitry Andric         WatchpointSP watch_sp = watchpoints.FindByID(wp_ids[i]);
769fe013be4SDimitry Andric         if (watch_sp) {
770fe013be4SDimitry Andric           watch_sp->SetCondition(m_options.m_condition.c_str());
7710b57cec5SDimitry Andric           ++count;
7720b57cec5SDimitry Andric         }
7730b57cec5SDimitry Andric       }
7740b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints modified.\n", count);
7750b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
7760b57cec5SDimitry Andric     }
7770b57cec5SDimitry Andric   }
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric private:
7800b57cec5SDimitry Andric   CommandOptions m_options;
7810b57cec5SDimitry Andric };
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric // CommandObjectWatchpointSetVariable
7840b57cec5SDimitry Andric #pragma mark SetVariable
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric class CommandObjectWatchpointSetVariable : public CommandObjectParsed {
7870b57cec5SDimitry Andric public:
CommandObjectWatchpointSetVariable(CommandInterpreter & interpreter)7880b57cec5SDimitry Andric   CommandObjectWatchpointSetVariable(CommandInterpreter &interpreter)
7890b57cec5SDimitry Andric       : CommandObjectParsed(
7900b57cec5SDimitry Andric             interpreter, "watchpoint set variable",
7910b57cec5SDimitry Andric             "Set a watchpoint on a variable. "
7920b57cec5SDimitry Andric             "Use the '-w' option to specify the type of watchpoint and "
7930b57cec5SDimitry Andric             "the '-s' option to specify the byte size to watch for. "
794*c9157d92SDimitry Andric             "If no '-w' option is specified, it defaults to modify. "
7950b57cec5SDimitry Andric             "If no '-s' option is specified, it defaults to the variable's "
7960b57cec5SDimitry Andric             "byte size. "
7970b57cec5SDimitry Andric             "Note that there are limited hardware resources for watchpoints. "
7980b57cec5SDimitry Andric             "If watchpoint setting fails, consider disable/delete existing "
7990b57cec5SDimitry Andric             "ones "
8000b57cec5SDimitry Andric             "to free up resources.",
8010b57cec5SDimitry Andric             nullptr,
8020b57cec5SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
80304eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
8040b57cec5SDimitry Andric     SetHelpLong(
8050b57cec5SDimitry Andric         R"(
8060b57cec5SDimitry Andric Examples:
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric (lldb) watchpoint set variable -w read_write my_global_var
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric )"
8110b57cec5SDimitry Andric         "    Watches my_global_var for read/write access, with the region to watch \
8120b57cec5SDimitry Andric corresponding to the byte size of the data type.");
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric     CommandArgumentEntry arg;
8150b57cec5SDimitry Andric     CommandArgumentData var_name_arg;
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric     // Define the only variant of this arg.
8180b57cec5SDimitry Andric     var_name_arg.arg_type = eArgTypeVarName;
8190b57cec5SDimitry Andric     var_name_arg.arg_repetition = eArgRepeatPlain;
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric     // Push the variant into the argument entry.
8220b57cec5SDimitry Andric     arg.push_back(var_name_arg);
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric     // Push the data for the only argument into the m_arguments vector.
8250b57cec5SDimitry Andric     m_arguments.push_back(arg);
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     // Absorb the '-w' and '-s' options into our option group.
828fe013be4SDimitry Andric     m_option_group.Append(&m_option_watchpoint, LLDB_OPT_SET_1, LLDB_OPT_SET_1);
8290b57cec5SDimitry Andric     m_option_group.Finalize();
8300b57cec5SDimitry Andric   }
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric   ~CommandObjectWatchpointSetVariable() override = default;
8330b57cec5SDimitry Andric 
834e8d8bef9SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)835e8d8bef9SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
836e8d8bef9SDimitry Andric                            OptionElementVector &opt_element_vector) override {
837e8d8bef9SDimitry Andric     if (request.GetCursorIndex() != 0)
838e8d8bef9SDimitry Andric       return;
839fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
840fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eVariablePathCompletion, request,
841fe013be4SDimitry Andric         nullptr);
842e8d8bef9SDimitry Andric   }
843e8d8bef9SDimitry Andric 
GetOptions()8440b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric protected:
GetVariableCallback(void * baton,const char * name,VariableList & variable_list)8470b57cec5SDimitry Andric   static size_t GetVariableCallback(void *baton, const char *name,
8480b57cec5SDimitry Andric                                     VariableList &variable_list) {
8499dba64beSDimitry Andric     size_t old_size = variable_list.GetSize();
8500b57cec5SDimitry Andric     Target *target = static_cast<Target *>(baton);
8519dba64beSDimitry Andric     if (target)
8529dba64beSDimitry Andric       target->GetImages().FindGlobalVariables(ConstString(name), UINT32_MAX,
8539dba64beSDimitry Andric                                               variable_list);
8549dba64beSDimitry Andric     return variable_list.GetSize() - old_size;
8550b57cec5SDimitry Andric   }
8560b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)857*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
8580b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
8590b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric     // If no argument is present, issue an error message.  There's no way to
8620b57cec5SDimitry Andric     // set a watchpoint.
8630b57cec5SDimitry Andric     if (command.GetArgumentCount() <= 0) {
864fe6060f1SDimitry Andric       result.AppendError("required argument missing; "
865fe6060f1SDimitry Andric                          "specify your program variable to watch for");
866*c9157d92SDimitry Andric       return;
8670b57cec5SDimitry Andric     }
8680b57cec5SDimitry Andric 
869*c9157d92SDimitry Andric     // If no '-w' is specified, default to '-w modify'.
8700b57cec5SDimitry Andric     if (!m_option_watchpoint.watch_type_specified) {
871*c9157d92SDimitry Andric       m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchModify;
8720b57cec5SDimitry Andric     }
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric     // We passed the sanity check for the command. Proceed to set the
8750b57cec5SDimitry Andric     // watchpoint now.
8760b57cec5SDimitry Andric     lldb::addr_t addr = 0;
8770b57cec5SDimitry Andric     size_t size = 0;
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric     VariableSP var_sp;
8800b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
8810b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric     // A simple watch variable gesture allows only one argument.
8840b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
885fe6060f1SDimitry Andric       result.AppendError("specify exactly one variable to watch for");
886*c9157d92SDimitry Andric       return;
8870b57cec5SDimitry Andric     }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric     // Things have checked out ok...
8900b57cec5SDimitry Andric     Status error;
8910b57cec5SDimitry Andric     uint32_t expr_path_options =
8920b57cec5SDimitry Andric         StackFrame::eExpressionPathOptionCheckPtrVsMember |
8930b57cec5SDimitry Andric         StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
8940b57cec5SDimitry Andric     valobj_sp = frame->GetValueForVariableExpressionPath(
8950b57cec5SDimitry Andric         command.GetArgumentAtIndex(0), eNoDynamicValues, expr_path_options,
8960b57cec5SDimitry Andric         var_sp, error);
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric     if (!valobj_sp) {
8990b57cec5SDimitry Andric       // Not in the frame; let's check the globals.
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric       VariableList variable_list;
9020b57cec5SDimitry Andric       ValueObjectList valobj_list;
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric       Status error(Variable::GetValuesForVariableExpressionPath(
9050b57cec5SDimitry Andric           command.GetArgumentAtIndex(0),
9060b57cec5SDimitry Andric           m_exe_ctx.GetBestExecutionContextScope(), GetVariableCallback, target,
9070b57cec5SDimitry Andric           variable_list, valobj_list));
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric       if (valobj_list.GetSize())
9100b57cec5SDimitry Andric         valobj_sp = valobj_list.GetValueObjectAtIndex(0);
9110b57cec5SDimitry Andric     }
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric     CompilerType compiler_type;
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric     if (valobj_sp) {
9160b57cec5SDimitry Andric       AddressType addr_type;
9170b57cec5SDimitry Andric       addr = valobj_sp->GetAddressOf(false, &addr_type);
9180b57cec5SDimitry Andric       if (addr_type == eAddressTypeLoad) {
9190b57cec5SDimitry Andric         // We're in business.
9200b57cec5SDimitry Andric         // Find out the size of this variable.
921*c9157d92SDimitry Andric         size = m_option_watchpoint.watch_size.GetCurrentValue() == 0
92281ad6265SDimitry Andric                    ? valobj_sp->GetByteSize().value_or(0)
923*c9157d92SDimitry Andric                    : m_option_watchpoint.watch_size.GetCurrentValue();
9240b57cec5SDimitry Andric       }
9250b57cec5SDimitry Andric       compiler_type = valobj_sp->GetCompilerType();
9260b57cec5SDimitry Andric     } else {
9270b57cec5SDimitry Andric       const char *error_cstr = error.AsCString(nullptr);
9280b57cec5SDimitry Andric       if (error_cstr)
929fe6060f1SDimitry Andric         result.AppendError(error_cstr);
9300b57cec5SDimitry Andric       else
931fe6060f1SDimitry Andric         result.AppendErrorWithFormat("unable to find any variable "
932fe6060f1SDimitry Andric                                      "expression path that matches '%s'",
9330b57cec5SDimitry Andric                                      command.GetArgumentAtIndex(0));
934*c9157d92SDimitry Andric       return;
9350b57cec5SDimitry Andric     }
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric     // Now it's time to create the watchpoint.
938*c9157d92SDimitry Andric     uint32_t watch_type = 0;
939*c9157d92SDimitry Andric     switch (m_option_watchpoint.watch_type) {
940*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchModify:
941*c9157d92SDimitry Andric       watch_type |= LLDB_WATCH_TYPE_MODIFY;
942*c9157d92SDimitry Andric       break;
943*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchRead:
944*c9157d92SDimitry Andric       watch_type |= LLDB_WATCH_TYPE_READ;
945*c9157d92SDimitry Andric       break;
946*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchReadWrite:
947*c9157d92SDimitry Andric       watch_type |= LLDB_WATCH_TYPE_READ | LLDB_WATCH_TYPE_WRITE;
948*c9157d92SDimitry Andric       break;
949*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchWrite:
950*c9157d92SDimitry Andric       watch_type |= LLDB_WATCH_TYPE_WRITE;
951*c9157d92SDimitry Andric       break;
952*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchInvalid:
953*c9157d92SDimitry Andric       break;
954*c9157d92SDimitry Andric     };
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric     error.Clear();
957fe013be4SDimitry Andric     WatchpointSP watch_sp =
958fe013be4SDimitry Andric         target->CreateWatchpoint(addr, size, &compiler_type, watch_type, error);
959fe013be4SDimitry Andric     if (!watch_sp) {
9600b57cec5SDimitry Andric       result.AppendErrorWithFormat(
9610b57cec5SDimitry Andric           "Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64
9620b57cec5SDimitry Andric           ", variable expression='%s').\n",
963fe013be4SDimitry Andric           addr, static_cast<uint64_t>(size), command.GetArgumentAtIndex(0));
964fe013be4SDimitry Andric       if (const char *error_message = error.AsCString(nullptr))
965fe013be4SDimitry Andric         result.AppendError(error_message);
966*c9157d92SDimitry Andric       return;
9670b57cec5SDimitry Andric     }
9680b57cec5SDimitry Andric 
969fe013be4SDimitry Andric     watch_sp->SetWatchSpec(command.GetArgumentAtIndex(0));
970fe013be4SDimitry Andric     watch_sp->SetWatchVariable(true);
971fe013be4SDimitry Andric     if (var_sp) {
972fe013be4SDimitry Andric       if (var_sp->GetDeclaration().GetFile()) {
973fe013be4SDimitry Andric         StreamString ss;
974fe013be4SDimitry Andric         // True to show fullpath for declaration file.
975fe013be4SDimitry Andric         var_sp->GetDeclaration().DumpStopContext(&ss, true);
976fe013be4SDimitry Andric         watch_sp->SetDeclInfo(std::string(ss.GetString()));
977fe013be4SDimitry Andric       }
978fe013be4SDimitry Andric       if (var_sp->GetScope() == eValueTypeVariableLocal)
979fe013be4SDimitry Andric         watch_sp->SetupVariableWatchpointDisabler(m_exe_ctx.GetFrameSP());
980fe013be4SDimitry Andric     }
981fe013be4SDimitry Andric     output_stream.Printf("Watchpoint created: ");
982fe013be4SDimitry Andric     watch_sp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
983fe013be4SDimitry Andric     output_stream.EOL();
984fe013be4SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9850b57cec5SDimitry Andric   }
9860b57cec5SDimitry Andric 
9870b57cec5SDimitry Andric private:
9880b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
9890b57cec5SDimitry Andric   OptionGroupWatchpoint m_option_watchpoint;
9900b57cec5SDimitry Andric };
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric // CommandObjectWatchpointSetExpression
9930b57cec5SDimitry Andric #pragma mark Set
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric class CommandObjectWatchpointSetExpression : public CommandObjectRaw {
9960b57cec5SDimitry Andric public:
CommandObjectWatchpointSetExpression(CommandInterpreter & interpreter)9970b57cec5SDimitry Andric   CommandObjectWatchpointSetExpression(CommandInterpreter &interpreter)
9980b57cec5SDimitry Andric       : CommandObjectRaw(
9990b57cec5SDimitry Andric             interpreter, "watchpoint set expression",
10000b57cec5SDimitry Andric             "Set a watchpoint on an address by supplying an expression. "
1001fe013be4SDimitry Andric             "Use the '-l' option to specify the language of the expression. "
10020b57cec5SDimitry Andric             "Use the '-w' option to specify the type of watchpoint and "
10030b57cec5SDimitry Andric             "the '-s' option to specify the byte size to watch for. "
1004*c9157d92SDimitry Andric             "If no '-w' option is specified, it defaults to modify. "
10050b57cec5SDimitry Andric             "If no '-s' option is specified, it defaults to the target's "
10060b57cec5SDimitry Andric             "pointer byte size. "
10070b57cec5SDimitry Andric             "Note that there are limited hardware resources for watchpoints. "
10080b57cec5SDimitry Andric             "If watchpoint setting fails, consider disable/delete existing "
10090b57cec5SDimitry Andric             "ones "
10100b57cec5SDimitry Andric             "to free up resources.",
10110b57cec5SDimitry Andric             "",
10120b57cec5SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
101304eeddc0SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
10140b57cec5SDimitry Andric     SetHelpLong(
10150b57cec5SDimitry Andric         R"(
10160b57cec5SDimitry Andric Examples:
10170b57cec5SDimitry Andric 
1018*c9157d92SDimitry Andric (lldb) watchpoint set expression -w modify -s 1 -- foo + 32
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric     Watches write access for the 1-byte region pointed to by the address 'foo + 32')");
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric     CommandArgumentEntry arg;
10230b57cec5SDimitry Andric     CommandArgumentData expression_arg;
10240b57cec5SDimitry Andric 
10250b57cec5SDimitry Andric     // Define the only variant of this arg.
10260b57cec5SDimitry Andric     expression_arg.arg_type = eArgTypeExpression;
10270b57cec5SDimitry Andric     expression_arg.arg_repetition = eArgRepeatPlain;
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric     // Push the only variant into the argument entry.
10300b57cec5SDimitry Andric     arg.push_back(expression_arg);
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric     // Push the data for the only argument into the m_arguments vector.
10330b57cec5SDimitry Andric     m_arguments.push_back(arg);
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric     // Absorb the '-w' and '-s' options into our option group.
10360b57cec5SDimitry Andric     m_option_group.Append(&m_option_watchpoint, LLDB_OPT_SET_ALL,
10370b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
10380b57cec5SDimitry Andric     m_option_group.Finalize();
10390b57cec5SDimitry Andric   }
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric   ~CommandObjectWatchpointSetExpression() override = default;
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric   // Overrides base class's behavior where WantsCompletion =
10440b57cec5SDimitry Andric   // !WantsRawCommandString.
WantsCompletion()10450b57cec5SDimitry Andric   bool WantsCompletion() override { return true; }
10460b57cec5SDimitry Andric 
GetOptions()10470b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric protected:
DoExecute(llvm::StringRef raw_command,CommandReturnObject & result)1050*c9157d92SDimitry Andric   void DoExecute(llvm::StringRef raw_command,
10510b57cec5SDimitry Andric                  CommandReturnObject &result) override {
10520b57cec5SDimitry Andric     auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
10530b57cec5SDimitry Andric     m_option_group.NotifyOptionParsingStarting(
10540b57cec5SDimitry Andric         &exe_ctx); // This is a raw command, so notify the option group
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
10570b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric     OptionsWithRaw args(raw_command);
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric     llvm::StringRef expr = args.GetRawPart();
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric     if (args.HasArgs())
10640b57cec5SDimitry Andric       if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group,
10650b57cec5SDimitry Andric                                  exe_ctx))
1066*c9157d92SDimitry Andric         return;
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric     // If no argument is present, issue an error message.  There's no way to
10690b57cec5SDimitry Andric     // set a watchpoint.
10700b57cec5SDimitry Andric     if (raw_command.trim().empty()) {
1071fe6060f1SDimitry Andric       result.AppendError("required argument missing; specify an expression "
1072fe6060f1SDimitry Andric                          "to evaluate into the address to watch for");
1073*c9157d92SDimitry Andric       return;
10740b57cec5SDimitry Andric     }
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric     // If no '-w' is specified, default to '-w write'.
10770b57cec5SDimitry Andric     if (!m_option_watchpoint.watch_type_specified) {
1078*c9157d92SDimitry Andric       m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchModify;
10790b57cec5SDimitry Andric     }
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric     // We passed the sanity check for the command. Proceed to set the
10820b57cec5SDimitry Andric     // watchpoint now.
10830b57cec5SDimitry Andric     lldb::addr_t addr = 0;
10840b57cec5SDimitry Andric     size_t size = 0;
10850b57cec5SDimitry Andric 
10860b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric     // Use expression evaluation to arrive at the address to watch.
10890b57cec5SDimitry Andric     EvaluateExpressionOptions options;
10900b57cec5SDimitry Andric     options.SetCoerceToId(false);
10910b57cec5SDimitry Andric     options.SetUnwindOnError(true);
10920b57cec5SDimitry Andric     options.SetKeepInMemory(false);
10930b57cec5SDimitry Andric     options.SetTryAllThreads(true);
1094bdd1243dSDimitry Andric     options.SetTimeout(std::nullopt);
1095fe013be4SDimitry Andric     if (m_option_watchpoint.language_type != eLanguageTypeUnknown)
1096fe013be4SDimitry Andric       options.SetLanguage(m_option_watchpoint.language_type);
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric     ExpressionResults expr_result =
10990b57cec5SDimitry Andric         target->EvaluateExpression(expr, frame, valobj_sp, options);
11000b57cec5SDimitry Andric     if (expr_result != eExpressionCompleted) {
1101fe6060f1SDimitry Andric       result.AppendError("expression evaluation of address to watch failed");
1102fe6060f1SDimitry Andric       result.AppendErrorWithFormat("expression evaluated: \n%s", expr.data());
11035ffd83dbSDimitry Andric       if (valobj_sp && !valobj_sp->GetError().Success())
1104fe6060f1SDimitry Andric         result.AppendError(valobj_sp->GetError().AsCString());
1105*c9157d92SDimitry Andric       return;
11060b57cec5SDimitry Andric     }
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric     // Get the address to watch.
11090b57cec5SDimitry Andric     bool success = false;
11100b57cec5SDimitry Andric     addr = valobj_sp->GetValueAsUnsigned(0, &success);
11110b57cec5SDimitry Andric     if (!success) {
1112fe6060f1SDimitry Andric       result.AppendError("expression did not evaluate to an address");
1113*c9157d92SDimitry Andric       return;
11140b57cec5SDimitry Andric     }
11150b57cec5SDimitry Andric 
1116*c9157d92SDimitry Andric     if (m_option_watchpoint.watch_size.GetCurrentValue() != 0)
1117*c9157d92SDimitry Andric       size = m_option_watchpoint.watch_size.GetCurrentValue();
11180b57cec5SDimitry Andric     else
11190b57cec5SDimitry Andric       size = target->GetArchitecture().GetAddressByteSize();
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric     // Now it's time to create the watchpoint.
1122*c9157d92SDimitry Andric     uint32_t watch_type;
1123*c9157d92SDimitry Andric     switch (m_option_watchpoint.watch_type) {
1124*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchRead:
1125*c9157d92SDimitry Andric       watch_type = LLDB_WATCH_TYPE_READ;
1126*c9157d92SDimitry Andric       break;
1127*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchWrite:
1128*c9157d92SDimitry Andric       watch_type = LLDB_WATCH_TYPE_WRITE;
1129*c9157d92SDimitry Andric       break;
1130*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchModify:
1131*c9157d92SDimitry Andric       watch_type = LLDB_WATCH_TYPE_MODIFY;
1132*c9157d92SDimitry Andric       break;
1133*c9157d92SDimitry Andric     case OptionGroupWatchpoint::eWatchReadWrite:
1134*c9157d92SDimitry Andric       watch_type = LLDB_WATCH_TYPE_READ | LLDB_WATCH_TYPE_WRITE;
1135*c9157d92SDimitry Andric       break;
1136*c9157d92SDimitry Andric     default:
1137*c9157d92SDimitry Andric       watch_type = LLDB_WATCH_TYPE_MODIFY;
1138*c9157d92SDimitry Andric     }
11390b57cec5SDimitry Andric 
11400b57cec5SDimitry Andric     // Fetch the type from the value object, the type of the watched object is
11410b57cec5SDimitry Andric     // the pointee type
11420b57cec5SDimitry Andric     /// of the expression, so convert to that if we  found a valid type.
11430b57cec5SDimitry Andric     CompilerType compiler_type(valobj_sp->GetCompilerType());
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric     Status error;
1146fe013be4SDimitry Andric     WatchpointSP watch_sp =
1147fe013be4SDimitry Andric         target->CreateWatchpoint(addr, size, &compiler_type, watch_type, error);
1148fe013be4SDimitry Andric     if (watch_sp) {
1149fe013be4SDimitry Andric       watch_sp->SetWatchSpec(std::string(expr));
11500b57cec5SDimitry Andric       Stream &output_stream = result.GetOutputStream();
11510b57cec5SDimitry Andric       output_stream.Printf("Watchpoint created: ");
1152fe013be4SDimitry Andric       watch_sp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
11530b57cec5SDimitry Andric       output_stream.EOL();
11540b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
11550b57cec5SDimitry Andric     } else {
11560b57cec5SDimitry Andric       result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64
11570b57cec5SDimitry Andric                                    ", size=%" PRIu64 ").\n",
11580b57cec5SDimitry Andric                                    addr, (uint64_t)size);
11590b57cec5SDimitry Andric       if (error.AsCString(nullptr))
11600b57cec5SDimitry Andric         result.AppendError(error.AsCString());
11610b57cec5SDimitry Andric     }
11620b57cec5SDimitry Andric   }
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric private:
11650b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
11660b57cec5SDimitry Andric   OptionGroupWatchpoint m_option_watchpoint;
11670b57cec5SDimitry Andric };
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric // CommandObjectWatchpointSet
11700b57cec5SDimitry Andric #pragma mark Set
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric class CommandObjectWatchpointSet : public CommandObjectMultiword {
11730b57cec5SDimitry Andric public:
CommandObjectWatchpointSet(CommandInterpreter & interpreter)11740b57cec5SDimitry Andric   CommandObjectWatchpointSet(CommandInterpreter &interpreter)
11750b57cec5SDimitry Andric       : CommandObjectMultiword(
11760b57cec5SDimitry Andric             interpreter, "watchpoint set", "Commands for setting a watchpoint.",
11770b57cec5SDimitry Andric             "watchpoint set <subcommand> [<subcommand-options>]") {
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric     LoadSubCommand(
11800b57cec5SDimitry Andric         "variable",
11810b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectWatchpointSetVariable(interpreter)));
11820b57cec5SDimitry Andric     LoadSubCommand(
11830b57cec5SDimitry Andric         "expression",
11840b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectWatchpointSetExpression(interpreter)));
11850b57cec5SDimitry Andric   }
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric   ~CommandObjectWatchpointSet() override = default;
11880b57cec5SDimitry Andric };
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric // CommandObjectMultiwordWatchpoint
11910b57cec5SDimitry Andric #pragma mark MultiwordWatchpoint
11920b57cec5SDimitry Andric 
CommandObjectMultiwordWatchpoint(CommandInterpreter & interpreter)11930b57cec5SDimitry Andric CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(
11940b57cec5SDimitry Andric     CommandInterpreter &interpreter)
11950b57cec5SDimitry Andric     : CommandObjectMultiword(interpreter, "watchpoint",
11960b57cec5SDimitry Andric                              "Commands for operating on watchpoints.",
11970b57cec5SDimitry Andric                              "watchpoint <subcommand> [<command-options>]") {
11980b57cec5SDimitry Andric   CommandObjectSP list_command_object(
11990b57cec5SDimitry Andric       new CommandObjectWatchpointList(interpreter));
12000b57cec5SDimitry Andric   CommandObjectSP enable_command_object(
12010b57cec5SDimitry Andric       new CommandObjectWatchpointEnable(interpreter));
12020b57cec5SDimitry Andric   CommandObjectSP disable_command_object(
12030b57cec5SDimitry Andric       new CommandObjectWatchpointDisable(interpreter));
12040b57cec5SDimitry Andric   CommandObjectSP delete_command_object(
12050b57cec5SDimitry Andric       new CommandObjectWatchpointDelete(interpreter));
12060b57cec5SDimitry Andric   CommandObjectSP ignore_command_object(
12070b57cec5SDimitry Andric       new CommandObjectWatchpointIgnore(interpreter));
12080b57cec5SDimitry Andric   CommandObjectSP command_command_object(
12090b57cec5SDimitry Andric       new CommandObjectWatchpointCommand(interpreter));
12100b57cec5SDimitry Andric   CommandObjectSP modify_command_object(
12110b57cec5SDimitry Andric       new CommandObjectWatchpointModify(interpreter));
12120b57cec5SDimitry Andric   CommandObjectSP set_command_object(
12130b57cec5SDimitry Andric       new CommandObjectWatchpointSet(interpreter));
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric   list_command_object->SetCommandName("watchpoint list");
12160b57cec5SDimitry Andric   enable_command_object->SetCommandName("watchpoint enable");
12170b57cec5SDimitry Andric   disable_command_object->SetCommandName("watchpoint disable");
12180b57cec5SDimitry Andric   delete_command_object->SetCommandName("watchpoint delete");
12190b57cec5SDimitry Andric   ignore_command_object->SetCommandName("watchpoint ignore");
12200b57cec5SDimitry Andric   command_command_object->SetCommandName("watchpoint command");
12210b57cec5SDimitry Andric   modify_command_object->SetCommandName("watchpoint modify");
12220b57cec5SDimitry Andric   set_command_object->SetCommandName("watchpoint set");
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   LoadSubCommand("list", list_command_object);
12250b57cec5SDimitry Andric   LoadSubCommand("enable", enable_command_object);
12260b57cec5SDimitry Andric   LoadSubCommand("disable", disable_command_object);
12270b57cec5SDimitry Andric   LoadSubCommand("delete", delete_command_object);
12280b57cec5SDimitry Andric   LoadSubCommand("ignore", ignore_command_object);
12290b57cec5SDimitry Andric   LoadSubCommand("command", command_command_object);
12300b57cec5SDimitry Andric   LoadSubCommand("modify", modify_command_object);
12310b57cec5SDimitry Andric   LoadSubCommand("set", set_command_object);
12320b57cec5SDimitry Andric }
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint() = default;
1235