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 
120b57cec5SDimitry Andric #include <vector>
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h"
170b57cec5SDimitry Andric #include "lldb/Breakpoint/WatchpointList.h"
180b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
190b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
200b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
210b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
240b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
250b57cec5SDimitry Andric #include "lldb/Target/Target.h"
260b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace lldb;
290b57cec5SDimitry Andric using namespace lldb_private;
300b57cec5SDimitry Andric 
AddWatchpointDescription(Stream * s,Watchpoint * wp,lldb::DescriptionLevel level)310b57cec5SDimitry Andric static void AddWatchpointDescription(Stream *s, Watchpoint *wp,
320b57cec5SDimitry Andric                                      lldb::DescriptionLevel level) {
330b57cec5SDimitry Andric   s->IndentMore();
340b57cec5SDimitry Andric   wp->GetDescription(s, level);
350b57cec5SDimitry Andric   s->IndentLess();
360b57cec5SDimitry Andric   s->EOL();
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
CheckTargetForWatchpointOperations(Target * target,CommandReturnObject & result)390b57cec5SDimitry Andric static bool CheckTargetForWatchpointOperations(Target *target,
400b57cec5SDimitry Andric                                                CommandReturnObject &result) {
410b57cec5SDimitry Andric   bool process_is_valid =
420b57cec5SDimitry Andric       target->GetProcessSP() && target->GetProcessSP()->IsAlive();
430b57cec5SDimitry Andric   if (!process_is_valid) {
445ffd83dbSDimitry Andric     result.AppendError("There's no process or it is not alive.");
450b57cec5SDimitry Andric     return false;
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric   // Target passes our checks, return true.
480b57cec5SDimitry Andric   return true;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric // Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
520b57cec5SDimitry Andric static const char *RSA[4] = {"-", "to", "To", "TO"};
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // Return the index to RSA if found; otherwise -1 is returned.
WithRSAIndex(llvm::StringRef Arg)550b57cec5SDimitry Andric static int32_t WithRSAIndex(llvm::StringRef Arg) {
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   uint32_t i;
580b57cec5SDimitry Andric   for (i = 0; i < 4; ++i)
590b57cec5SDimitry Andric     if (Arg.find(RSA[i]) != llvm::StringRef::npos)
600b57cec5SDimitry Andric       return i;
610b57cec5SDimitry Andric   return -1;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric // Return true if wp_ids is successfully populated with the watch ids. False
650b57cec5SDimitry Andric // otherwise.
VerifyWatchpointIDs(Target * target,Args & args,std::vector<uint32_t> & wp_ids)660b57cec5SDimitry Andric bool CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
670b57cec5SDimitry Andric     Target *target, Args &args, std::vector<uint32_t> &wp_ids) {
680b57cec5SDimitry Andric   // Pre-condition: args.GetArgumentCount() > 0.
690b57cec5SDimitry Andric   if (args.GetArgumentCount() == 0) {
700b57cec5SDimitry Andric     if (target == nullptr)
710b57cec5SDimitry Andric       return false;
720b57cec5SDimitry Andric     WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
730b57cec5SDimitry Andric     if (watch_sp) {
740b57cec5SDimitry Andric       wp_ids.push_back(watch_sp->GetID());
750b57cec5SDimitry Andric       return true;
760b57cec5SDimitry Andric     } else
770b57cec5SDimitry Andric       return false;
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   llvm::StringRef Minus("-");
810b57cec5SDimitry Andric   std::vector<llvm::StringRef> StrRefArgs;
820b57cec5SDimitry Andric   llvm::StringRef first;
830b57cec5SDimitry Andric   llvm::StringRef second;
840b57cec5SDimitry Andric   size_t i;
850b57cec5SDimitry Andric   int32_t idx;
860b57cec5SDimitry Andric   // Go through the arguments and make a canonical form of arg list containing
870b57cec5SDimitry Andric   // only numbers with possible "-" in between.
880b57cec5SDimitry Andric   for (auto &entry : args.entries()) {
899dba64beSDimitry Andric     if ((idx = WithRSAIndex(entry.ref())) == -1) {
909dba64beSDimitry Andric       StrRefArgs.push_back(entry.ref());
910b57cec5SDimitry Andric       continue;
920b57cec5SDimitry Andric     }
930b57cec5SDimitry Andric     // The Arg contains the range specifier, split it, then.
949dba64beSDimitry Andric     std::tie(first, second) = entry.ref().split(RSA[idx]);
950b57cec5SDimitry Andric     if (!first.empty())
960b57cec5SDimitry Andric       StrRefArgs.push_back(first);
970b57cec5SDimitry Andric     StrRefArgs.push_back(Minus);
980b57cec5SDimitry Andric     if (!second.empty())
990b57cec5SDimitry Andric       StrRefArgs.push_back(second);
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric   // Now process the canonical list and fill in the vector of uint32_t's. If
1020b57cec5SDimitry Andric   // there is any error, return false and the client should ignore wp_ids.
1030b57cec5SDimitry Andric   uint32_t beg, end, id;
1040b57cec5SDimitry Andric   size_t size = StrRefArgs.size();
1050b57cec5SDimitry Andric   bool in_range = false;
1060b57cec5SDimitry Andric   for (i = 0; i < size; ++i) {
1070b57cec5SDimitry Andric     llvm::StringRef Arg = StrRefArgs[i];
1080b57cec5SDimitry Andric     if (in_range) {
1090b57cec5SDimitry Andric       // Look for the 'end' of the range.  Note StringRef::getAsInteger()
1100b57cec5SDimitry Andric       // returns true to signify error while parsing.
1110b57cec5SDimitry Andric       if (Arg.getAsInteger(0, end))
1120b57cec5SDimitry Andric         return false;
1130b57cec5SDimitry Andric       // Found a range!  Now append the elements.
1140b57cec5SDimitry Andric       for (id = beg; id <= end; ++id)
1150b57cec5SDimitry Andric         wp_ids.push_back(id);
1160b57cec5SDimitry Andric       in_range = false;
1170b57cec5SDimitry Andric       continue;
1180b57cec5SDimitry Andric     }
1190b57cec5SDimitry Andric     if (i < (size - 1) && StrRefArgs[i + 1] == Minus) {
1200b57cec5SDimitry Andric       if (Arg.getAsInteger(0, beg))
1210b57cec5SDimitry Andric         return false;
1220b57cec5SDimitry Andric       // Turn on the in_range flag, we are looking for end of range next.
1230b57cec5SDimitry Andric       ++i;
1240b57cec5SDimitry Andric       in_range = true;
1250b57cec5SDimitry Andric       continue;
1260b57cec5SDimitry Andric     }
1270b57cec5SDimitry Andric     // Otherwise, we have a simple ID.  Just append it.
1280b57cec5SDimitry Andric     if (Arg.getAsInteger(0, beg))
1290b57cec5SDimitry Andric       return false;
1300b57cec5SDimitry Andric     wp_ids.push_back(beg);
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   // It is an error if after the loop, we're still in_range.
1340b57cec5SDimitry Andric   return !in_range;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric // CommandObjectWatchpointList
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric // CommandObjectWatchpointList::Options
1400b57cec5SDimitry Andric #pragma mark List::CommandOptions
1410b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_list
1420b57cec5SDimitry Andric #include "CommandOptions.inc"
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric #pragma mark List
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric class CommandObjectWatchpointList : public CommandObjectParsed {
1470b57cec5SDimitry Andric public:
CommandObjectWatchpointList(CommandInterpreter & interpreter)1480b57cec5SDimitry Andric   CommandObjectWatchpointList(CommandInterpreter &interpreter)
1490b57cec5SDimitry Andric       : CommandObjectParsed(
1500b57cec5SDimitry Andric             interpreter, "watchpoint list",
1519dba64beSDimitry Andric             "List all watchpoints at configurable levels of detail.", nullptr,
1529dba64beSDimitry Andric             eCommandRequiresTarget),
1530b57cec5SDimitry Andric         m_options() {
1540b57cec5SDimitry Andric     CommandArgumentEntry arg;
1550b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
1560b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
1570b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
1580b57cec5SDimitry Andric     // arguments vector.
1590b57cec5SDimitry Andric     m_arguments.push_back(arg);
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   ~CommandObjectWatchpointList() override = default;
1630b57cec5SDimitry Andric 
GetOptions()1640b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   class CommandOptions : public Options {
1670b57cec5SDimitry Andric   public:
CommandOptions()168*5f7ddb14SDimitry Andric     CommandOptions() : Options() {}
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric     ~CommandOptions() override = default;
1710b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)1720b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1730b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
1740b57cec5SDimitry Andric       Status error;
1750b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric       switch (short_option) {
1780b57cec5SDimitry Andric       case 'b':
1790b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelBrief;
1800b57cec5SDimitry Andric         break;
1810b57cec5SDimitry Andric       case 'f':
1820b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelFull;
1830b57cec5SDimitry Andric         break;
1840b57cec5SDimitry Andric       case 'v':
1850b57cec5SDimitry Andric         m_level = lldb::eDescriptionLevelVerbose;
1860b57cec5SDimitry Andric         break;
1870b57cec5SDimitry Andric       default:
1889dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
1890b57cec5SDimitry Andric       }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric       return error;
1920b57cec5SDimitry Andric     }
1930b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)1940b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
1950b57cec5SDimitry Andric       m_level = lldb::eDescriptionLevelFull;
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric 
GetDefinitions()1980b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1990b57cec5SDimitry Andric       return llvm::makeArrayRef(g_watchpoint_list_options);
2000b57cec5SDimitry Andric     }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
2030b57cec5SDimitry Andric 
204*5f7ddb14SDimitry Andric     lldb::DescriptionLevel m_level = lldb::eDescriptionLevelBrief;
2050b57cec5SDimitry Andric   };
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)2080b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
2099dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric     if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) {
2120b57cec5SDimitry Andric       uint32_t num_supported_hardware_watchpoints;
2130b57cec5SDimitry Andric       Status error = target->GetProcessSP()->GetWatchpointSupportInfo(
2140b57cec5SDimitry Andric           num_supported_hardware_watchpoints);
2150b57cec5SDimitry Andric       if (error.Success())
2160b57cec5SDimitry Andric         result.AppendMessageWithFormat(
2170b57cec5SDimitry Andric             "Number of supported hardware watchpoints: %u\n",
2180b57cec5SDimitry Andric             num_supported_hardware_watchpoints);
2190b57cec5SDimitry Andric     }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
2240b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     if (num_watchpoints == 0) {
2290b57cec5SDimitry Andric       result.AppendMessage("No watchpoints currently set.");
2300b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2310b57cec5SDimitry Andric       return true;
2320b57cec5SDimitry Andric     }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
2370b57cec5SDimitry Andric       // No watchpoint selected; show info about all currently set watchpoints.
2380b57cec5SDimitry Andric       result.AppendMessage("Current watchpoints:");
2390b57cec5SDimitry Andric       for (size_t i = 0; i < num_watchpoints; ++i) {
2400b57cec5SDimitry Andric         Watchpoint *wp = watchpoints.GetByIndex(i).get();
2410b57cec5SDimitry Andric         AddWatchpointDescription(&output_stream, wp, m_options.m_level);
2420b57cec5SDimitry Andric       }
2430b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2440b57cec5SDimitry Andric     } else {
2450b57cec5SDimitry Andric       // Particular watchpoints selected; enable them.
2460b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
2470b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
2480b57cec5SDimitry Andric               target, command, wp_ids)) {
2490b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
2500b57cec5SDimitry Andric         return false;
2510b57cec5SDimitry Andric       }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric       const size_t size = wp_ids.size();
2540b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i) {
2550b57cec5SDimitry Andric         Watchpoint *wp = watchpoints.FindByID(wp_ids[i]).get();
2560b57cec5SDimitry Andric         if (wp)
2570b57cec5SDimitry Andric           AddWatchpointDescription(&output_stream, wp, m_options.m_level);
2580b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
2590b57cec5SDimitry Andric       }
2600b57cec5SDimitry Andric     }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric     return result.Succeeded();
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 
289af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)290af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
291af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
292af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
293af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eWatchPointIDCompletion,
294af732203SDimitry Andric         request, nullptr);
295af732203SDimitry Andric   }
296af732203SDimitry Andric 
2970b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)2980b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
2999dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
3000b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
3010b57cec5SDimitry Andric       return false;
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.");
3120b57cec5SDimitry Andric       return false;
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.");
3280b57cec5SDimitry Andric         return false;
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     return result.Succeeded();
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric };
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric // CommandObjectWatchpointDisable
3450b57cec5SDimitry Andric #pragma mark Disable
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric class CommandObjectWatchpointDisable : public CommandObjectParsed {
3480b57cec5SDimitry Andric public:
CommandObjectWatchpointDisable(CommandInterpreter & interpreter)3490b57cec5SDimitry Andric   CommandObjectWatchpointDisable(CommandInterpreter &interpreter)
3500b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint disable",
3510b57cec5SDimitry Andric                             "Disable the specified watchpoint(s) without "
3520b57cec5SDimitry Andric                             "removing it/them.  If no watchpoints are "
3530b57cec5SDimitry Andric                             "specified, disable them all.",
3549dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget) {
3550b57cec5SDimitry Andric     CommandArgumentEntry arg;
3560b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
3570b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
3580b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
3590b57cec5SDimitry Andric     // arguments vector.
3600b57cec5SDimitry Andric     m_arguments.push_back(arg);
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   ~CommandObjectWatchpointDisable() override = default;
3640b57cec5SDimitry Andric 
365af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)366af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
367af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
368af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
369af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eWatchPointIDCompletion,
370af732203SDimitry Andric         request, nullptr);
371af732203SDimitry Andric   }
372af732203SDimitry Andric 
3730b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)3740b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
3759dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
3760b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
3770b57cec5SDimitry Andric       return false;
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
3800b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
3830b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric     if (num_watchpoints == 0) {
3860b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be disabled.");
3870b57cec5SDimitry Andric       return false;
3880b57cec5SDimitry Andric     }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
3910b57cec5SDimitry Andric       // No watchpoint selected; disable all currently set watchpoints.
3920b57cec5SDimitry Andric       if (target->DisableAllWatchpoints()) {
3930b57cec5SDimitry Andric         result.AppendMessageWithFormat("All watchpoints disabled. (%" PRIu64
3940b57cec5SDimitry Andric                                        " watchpoints)\n",
3950b57cec5SDimitry Andric                                        (uint64_t)num_watchpoints);
3960b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishNoResult);
3970b57cec5SDimitry Andric       } else {
3980b57cec5SDimitry Andric         result.AppendError("Disable all watchpoints failed\n");
3990b57cec5SDimitry Andric       }
4000b57cec5SDimitry Andric     } else {
4010b57cec5SDimitry Andric       // Particular watchpoints selected; disable them.
4020b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
4030b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
4040b57cec5SDimitry Andric               target, command, wp_ids)) {
4050b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
4060b57cec5SDimitry Andric         return false;
4070b57cec5SDimitry Andric       }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric       int count = 0;
4100b57cec5SDimitry Andric       const size_t size = wp_ids.size();
4110b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i)
4120b57cec5SDimitry Andric         if (target->DisableWatchpointByID(wp_ids[i]))
4130b57cec5SDimitry Andric           ++count;
4140b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints disabled.\n", count);
4150b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
4160b57cec5SDimitry Andric     }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric     return result.Succeeded();
4190b57cec5SDimitry Andric   }
4200b57cec5SDimitry Andric };
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric // CommandObjectWatchpointDelete
423480093f4SDimitry Andric #define LLDB_OPTIONS_watchpoint_delete
424480093f4SDimitry Andric #include "CommandOptions.inc"
425480093f4SDimitry Andric 
426480093f4SDimitry Andric // CommandObjectWatchpointDelete
4270b57cec5SDimitry Andric #pragma mark Delete
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric class CommandObjectWatchpointDelete : public CommandObjectParsed {
4300b57cec5SDimitry Andric public:
CommandObjectWatchpointDelete(CommandInterpreter & interpreter)4310b57cec5SDimitry Andric   CommandObjectWatchpointDelete(CommandInterpreter &interpreter)
4320b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint delete",
4330b57cec5SDimitry Andric                             "Delete the specified watchpoint(s).  If no "
4340b57cec5SDimitry Andric                             "watchpoints are specified, delete them all.",
435480093f4SDimitry Andric                             nullptr, eCommandRequiresTarget),
436480093f4SDimitry Andric         m_options() {
4370b57cec5SDimitry Andric     CommandArgumentEntry arg;
4380b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
4390b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
4400b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
4410b57cec5SDimitry Andric     // arguments vector.
4420b57cec5SDimitry Andric     m_arguments.push_back(arg);
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   ~CommandObjectWatchpointDelete() override = default;
4460b57cec5SDimitry Andric 
447af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)448af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
449af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
450af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
451af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eWatchPointIDCompletion,
452af732203SDimitry Andric         request, nullptr);
453af732203SDimitry Andric   }
454af732203SDimitry Andric 
GetOptions()455480093f4SDimitry Andric   Options *GetOptions() override { return &m_options; }
456480093f4SDimitry Andric 
457480093f4SDimitry Andric   class CommandOptions : public Options {
458480093f4SDimitry Andric   public:
CommandOptions()459*5f7ddb14SDimitry Andric     CommandOptions() : Options() {}
460480093f4SDimitry Andric 
461480093f4SDimitry Andric     ~CommandOptions() override = default;
462480093f4SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)463480093f4SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
464480093f4SDimitry Andric                           ExecutionContext *execution_context) override {
465480093f4SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
466480093f4SDimitry Andric 
467480093f4SDimitry Andric       switch (short_option) {
468480093f4SDimitry Andric       case 'f':
469480093f4SDimitry Andric         m_force = true;
470480093f4SDimitry Andric         break;
471480093f4SDimitry Andric       default:
472480093f4SDimitry Andric         llvm_unreachable("Unimplemented option");
473480093f4SDimitry Andric       }
474480093f4SDimitry Andric 
475480093f4SDimitry Andric       return {};
476480093f4SDimitry Andric     }
477480093f4SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)478480093f4SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
479480093f4SDimitry Andric       m_force = false;
480480093f4SDimitry Andric     }
481480093f4SDimitry Andric 
GetDefinitions()482480093f4SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
483480093f4SDimitry Andric       return llvm::makeArrayRef(g_watchpoint_delete_options);
484480093f4SDimitry Andric     }
485480093f4SDimitry Andric 
486480093f4SDimitry Andric     // Instance variables to hold the values for command options.
487*5f7ddb14SDimitry Andric     bool m_force = false;
488480093f4SDimitry Andric   };
489480093f4SDimitry Andric 
4900b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)4910b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
4929dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
4930b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
4940b57cec5SDimitry Andric       return false;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
4970b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric     if (num_watchpoints == 0) {
5040b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be deleted.");
5050b57cec5SDimitry Andric       return false;
5060b57cec5SDimitry Andric     }
5070b57cec5SDimitry Andric 
508480093f4SDimitry Andric     if (command.empty()) {
509480093f4SDimitry Andric       if (!m_options.m_force &&
510480093f4SDimitry Andric           !m_interpreter.Confirm(
5110b57cec5SDimitry Andric               "About to delete all watchpoints, do you want to do that?",
5120b57cec5SDimitry Andric               true)) {
5130b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
5140b57cec5SDimitry Andric       } else {
5150b57cec5SDimitry Andric         target->RemoveAllWatchpoints();
5160b57cec5SDimitry Andric         result.AppendMessageWithFormat("All watchpoints removed. (%" PRIu64
5170b57cec5SDimitry Andric                                        " watchpoints)\n",
5180b57cec5SDimitry Andric                                        (uint64_t)num_watchpoints);
5190b57cec5SDimitry Andric       }
5200b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
521480093f4SDimitry Andric       return result.Succeeded();
522480093f4SDimitry Andric     }
523480093f4SDimitry Andric 
5240b57cec5SDimitry Andric     // Particular watchpoints selected; delete them.
5250b57cec5SDimitry Andric     std::vector<uint32_t> wp_ids;
526480093f4SDimitry Andric     if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command,
527480093f4SDimitry Andric                                                                wp_ids)) {
5280b57cec5SDimitry Andric       result.AppendError("Invalid watchpoints specification.");
5290b57cec5SDimitry Andric       return false;
5300b57cec5SDimitry Andric     }
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric     int count = 0;
5330b57cec5SDimitry Andric     const size_t size = wp_ids.size();
5340b57cec5SDimitry Andric     for (size_t i = 0; i < size; ++i)
5350b57cec5SDimitry Andric       if (target->RemoveWatchpointByID(wp_ids[i]))
5360b57cec5SDimitry Andric         ++count;
5370b57cec5SDimitry Andric     result.AppendMessageWithFormat("%d watchpoints deleted.\n", count);
5380b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishNoResult);
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric     return result.Succeeded();
5410b57cec5SDimitry Andric   }
542480093f4SDimitry Andric 
543480093f4SDimitry Andric private:
544480093f4SDimitry Andric   CommandOptions m_options;
5450b57cec5SDimitry Andric };
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric // CommandObjectWatchpointIgnore
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric #pragma mark Ignore::CommandOptions
5500b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_ignore
5510b57cec5SDimitry Andric #include "CommandOptions.inc"
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric class CommandObjectWatchpointIgnore : public CommandObjectParsed {
5540b57cec5SDimitry Andric public:
CommandObjectWatchpointIgnore(CommandInterpreter & interpreter)5550b57cec5SDimitry Andric   CommandObjectWatchpointIgnore(CommandInterpreter &interpreter)
5560b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "watchpoint ignore",
5570b57cec5SDimitry Andric                             "Set ignore count on the specified watchpoint(s).  "
5580b57cec5SDimitry Andric                             "If no watchpoints are specified, set them all.",
5599dba64beSDimitry Andric                             nullptr, eCommandRequiresTarget),
5600b57cec5SDimitry Andric         m_options() {
5610b57cec5SDimitry Andric     CommandArgumentEntry arg;
5620b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
5630b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
5640b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
5650b57cec5SDimitry Andric     // arguments vector.
5660b57cec5SDimitry Andric     m_arguments.push_back(arg);
5670b57cec5SDimitry Andric   }
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   ~CommandObjectWatchpointIgnore() override = default;
5700b57cec5SDimitry Andric 
571af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)572af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
573af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
574af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
575af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eWatchPointIDCompletion,
576af732203SDimitry Andric         request, nullptr);
577af732203SDimitry Andric   }
578af732203SDimitry Andric 
GetOptions()5790b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   class CommandOptions : public Options {
5820b57cec5SDimitry Andric   public:
CommandOptions()583*5f7ddb14SDimitry Andric     CommandOptions() : Options() {}
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric     ~CommandOptions() override = default;
5860b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)5870b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
5880b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
5890b57cec5SDimitry Andric       Status error;
5900b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric       switch (short_option) {
5930b57cec5SDimitry Andric       case 'i':
5940b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, m_ignore_count))
5950b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid ignore count '%s'",
5960b57cec5SDimitry Andric                                          option_arg.str().c_str());
5970b57cec5SDimitry Andric         break;
5980b57cec5SDimitry Andric       default:
5999dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
6000b57cec5SDimitry Andric       }
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric       return error;
6030b57cec5SDimitry Andric     }
6040b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)6050b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
6060b57cec5SDimitry Andric       m_ignore_count = 0;
6070b57cec5SDimitry Andric     }
6080b57cec5SDimitry Andric 
GetDefinitions()6090b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
6100b57cec5SDimitry Andric       return llvm::makeArrayRef(g_watchpoint_ignore_options);
6110b57cec5SDimitry Andric     }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
6140b57cec5SDimitry Andric 
615*5f7ddb14SDimitry Andric     uint32_t m_ignore_count = 0;
6160b57cec5SDimitry Andric   };
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)6190b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
6209dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
6210b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
6220b57cec5SDimitry Andric       return false;
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
6250b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric     if (num_watchpoints == 0) {
6320b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be ignored.");
6330b57cec5SDimitry Andric       return false;
6340b57cec5SDimitry Andric     }
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
6370b57cec5SDimitry Andric       target->IgnoreAllWatchpoints(m_options.m_ignore_count);
6380b57cec5SDimitry Andric       result.AppendMessageWithFormat("All watchpoints ignored. (%" PRIu64
6390b57cec5SDimitry Andric                                      " watchpoints)\n",
6400b57cec5SDimitry Andric                                      (uint64_t)num_watchpoints);
6410b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
6420b57cec5SDimitry Andric     } else {
6430b57cec5SDimitry Andric       // Particular watchpoints selected; ignore them.
6440b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
6450b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
6460b57cec5SDimitry Andric               target, command, wp_ids)) {
6470b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
6480b57cec5SDimitry Andric         return false;
6490b57cec5SDimitry Andric       }
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric       int count = 0;
6520b57cec5SDimitry Andric       const size_t size = wp_ids.size();
6530b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i)
6540b57cec5SDimitry Andric         if (target->IgnoreWatchpointByID(wp_ids[i], m_options.m_ignore_count))
6550b57cec5SDimitry Andric           ++count;
6560b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints ignored.\n", count);
6570b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
6580b57cec5SDimitry Andric     }
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric     return result.Succeeded();
6610b57cec5SDimitry Andric   }
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric private:
6640b57cec5SDimitry Andric   CommandOptions m_options;
6650b57cec5SDimitry Andric };
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric // CommandObjectWatchpointModify
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric #pragma mark Modify::CommandOptions
6700b57cec5SDimitry Andric #define LLDB_OPTIONS_watchpoint_modify
6710b57cec5SDimitry Andric #include "CommandOptions.inc"
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric #pragma mark Modify
6740b57cec5SDimitry Andric 
6750b57cec5SDimitry Andric class CommandObjectWatchpointModify : public CommandObjectParsed {
6760b57cec5SDimitry Andric public:
CommandObjectWatchpointModify(CommandInterpreter & interpreter)6770b57cec5SDimitry Andric   CommandObjectWatchpointModify(CommandInterpreter &interpreter)
6780b57cec5SDimitry Andric       : CommandObjectParsed(
6790b57cec5SDimitry Andric             interpreter, "watchpoint modify",
6800b57cec5SDimitry Andric             "Modify the options on a watchpoint or set of watchpoints in the "
6810b57cec5SDimitry Andric             "executable.  "
6820b57cec5SDimitry Andric             "If no watchpoint is specified, act on the last created "
6830b57cec5SDimitry Andric             "watchpoint.  "
6840b57cec5SDimitry Andric             "Passing an empty argument clears the modification.",
6859dba64beSDimitry Andric             nullptr, eCommandRequiresTarget),
6860b57cec5SDimitry Andric         m_options() {
6870b57cec5SDimitry Andric     CommandArgumentEntry arg;
6880b57cec5SDimitry Andric     CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID,
6890b57cec5SDimitry Andric                                       eArgTypeWatchpointIDRange);
6900b57cec5SDimitry Andric     // Add the entry for the first argument for this command to the object's
6910b57cec5SDimitry Andric     // arguments vector.
6920b57cec5SDimitry Andric     m_arguments.push_back(arg);
6930b57cec5SDimitry Andric   }
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric   ~CommandObjectWatchpointModify() override = default;
6960b57cec5SDimitry Andric 
697af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)698af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
699af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
700af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
701af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eWatchPointIDCompletion,
702af732203SDimitry Andric         request, nullptr);
703af732203SDimitry Andric   }
704af732203SDimitry Andric 
GetOptions()7050b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric   class CommandOptions : public Options {
7080b57cec5SDimitry Andric   public:
CommandOptions()709*5f7ddb14SDimitry Andric     CommandOptions() : Options(), m_condition() {}
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric     ~CommandOptions() override = default;
7120b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)7130b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7140b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7150b57cec5SDimitry Andric       Status error;
7160b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric       switch (short_option) {
7190b57cec5SDimitry Andric       case 'c':
7205ffd83dbSDimitry Andric         m_condition = std::string(option_arg);
7210b57cec5SDimitry Andric         m_condition_passed = true;
7220b57cec5SDimitry Andric         break;
7230b57cec5SDimitry Andric       default:
7249dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7250b57cec5SDimitry Andric       }
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric       return error;
7280b57cec5SDimitry Andric     }
7290b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)7300b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7310b57cec5SDimitry Andric       m_condition.clear();
7320b57cec5SDimitry Andric       m_condition_passed = false;
7330b57cec5SDimitry Andric     }
7340b57cec5SDimitry Andric 
GetDefinitions()7350b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
7360b57cec5SDimitry Andric       return llvm::makeArrayRef(g_watchpoint_modify_options);
7370b57cec5SDimitry Andric     }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric     std::string m_condition;
742*5f7ddb14SDimitry Andric     bool m_condition_passed = false;
7430b57cec5SDimitry Andric   };
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)7460b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
7479dba64beSDimitry Andric     Target *target = &GetSelectedTarget();
7480b57cec5SDimitry Andric     if (!CheckTargetForWatchpointOperations(target, result))
7490b57cec5SDimitry Andric       return false;
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
7520b57cec5SDimitry Andric     target->GetWatchpointList().GetListMutex(lock);
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric     const WatchpointList &watchpoints = target->GetWatchpointList();
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric     size_t num_watchpoints = watchpoints.GetSize();
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric     if (num_watchpoints == 0) {
7590b57cec5SDimitry Andric       result.AppendError("No watchpoints exist to be modified.");
7600b57cec5SDimitry Andric       return false;
7610b57cec5SDimitry Andric     }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
7640b57cec5SDimitry Andric       WatchpointSP wp_sp = target->GetLastCreatedWatchpoint();
7650b57cec5SDimitry Andric       wp_sp->SetCondition(m_options.m_condition.c_str());
7660b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
7670b57cec5SDimitry Andric     } else {
7680b57cec5SDimitry Andric       // Particular watchpoints selected; set condition on them.
7690b57cec5SDimitry Andric       std::vector<uint32_t> wp_ids;
7700b57cec5SDimitry Andric       if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
7710b57cec5SDimitry Andric               target, command, wp_ids)) {
7720b57cec5SDimitry Andric         result.AppendError("Invalid watchpoints specification.");
7730b57cec5SDimitry Andric         return false;
7740b57cec5SDimitry Andric       }
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric       int count = 0;
7770b57cec5SDimitry Andric       const size_t size = wp_ids.size();
7780b57cec5SDimitry Andric       for (size_t i = 0; i < size; ++i) {
7790b57cec5SDimitry Andric         WatchpointSP wp_sp = watchpoints.FindByID(wp_ids[i]);
7800b57cec5SDimitry Andric         if (wp_sp) {
7810b57cec5SDimitry Andric           wp_sp->SetCondition(m_options.m_condition.c_str());
7820b57cec5SDimitry Andric           ++count;
7830b57cec5SDimitry Andric         }
7840b57cec5SDimitry Andric       }
7850b57cec5SDimitry Andric       result.AppendMessageWithFormat("%d watchpoints modified.\n", count);
7860b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
7870b57cec5SDimitry Andric     }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric     return result.Succeeded();
7900b57cec5SDimitry Andric   }
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric private:
7930b57cec5SDimitry Andric   CommandOptions m_options;
7940b57cec5SDimitry Andric };
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric // CommandObjectWatchpointSetVariable
7970b57cec5SDimitry Andric #pragma mark SetVariable
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric class CommandObjectWatchpointSetVariable : public CommandObjectParsed {
8000b57cec5SDimitry Andric public:
CommandObjectWatchpointSetVariable(CommandInterpreter & interpreter)8010b57cec5SDimitry Andric   CommandObjectWatchpointSetVariable(CommandInterpreter &interpreter)
8020b57cec5SDimitry Andric       : CommandObjectParsed(
8030b57cec5SDimitry Andric             interpreter, "watchpoint set variable",
8040b57cec5SDimitry Andric             "Set a watchpoint on a variable. "
8050b57cec5SDimitry Andric             "Use the '-w' option to specify the type of watchpoint and "
8060b57cec5SDimitry Andric             "the '-s' option to specify the byte size to watch for. "
8070b57cec5SDimitry Andric             "If no '-w' option is specified, it defaults to write. "
8080b57cec5SDimitry Andric             "If no '-s' option is specified, it defaults to the variable's "
8090b57cec5SDimitry Andric             "byte size. "
8100b57cec5SDimitry Andric             "Note that there are limited hardware resources for watchpoints. "
8110b57cec5SDimitry Andric             "If watchpoint setting fails, consider disable/delete existing "
8120b57cec5SDimitry Andric             "ones "
8130b57cec5SDimitry Andric             "to free up resources.",
8140b57cec5SDimitry Andric             nullptr,
8150b57cec5SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
8160b57cec5SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
8170b57cec5SDimitry Andric         m_option_group(), m_option_watchpoint() {
8180b57cec5SDimitry Andric     SetHelpLong(
8190b57cec5SDimitry Andric         R"(
8200b57cec5SDimitry Andric Examples:
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric (lldb) watchpoint set variable -w read_write my_global_var
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric )"
8250b57cec5SDimitry Andric         "    Watches my_global_var for read/write access, with the region to watch \
8260b57cec5SDimitry Andric corresponding to the byte size of the data type.");
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric     CommandArgumentEntry arg;
8290b57cec5SDimitry Andric     CommandArgumentData var_name_arg;
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric     // Define the only variant of this arg.
8320b57cec5SDimitry Andric     var_name_arg.arg_type = eArgTypeVarName;
8330b57cec5SDimitry Andric     var_name_arg.arg_repetition = eArgRepeatPlain;
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric     // Push the variant into the argument entry.
8360b57cec5SDimitry Andric     arg.push_back(var_name_arg);
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric     // Push the data for the only argument into the m_arguments vector.
8390b57cec5SDimitry Andric     m_arguments.push_back(arg);
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric     // Absorb the '-w' and '-s' options into our option group.
8420b57cec5SDimitry Andric     m_option_group.Append(&m_option_watchpoint, LLDB_OPT_SET_ALL,
8430b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
8440b57cec5SDimitry Andric     m_option_group.Finalize();
8450b57cec5SDimitry Andric   }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   ~CommandObjectWatchpointSetVariable() override = default;
8480b57cec5SDimitry Andric 
849af732203SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)850af732203SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
851af732203SDimitry Andric                            OptionElementVector &opt_element_vector) override {
852af732203SDimitry Andric     if (request.GetCursorIndex() != 0)
853af732203SDimitry Andric       return;
854af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
855af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion,
856af732203SDimitry Andric         request, nullptr);
857af732203SDimitry Andric   }
858af732203SDimitry Andric 
GetOptions()8590b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric protected:
GetVariableCallback(void * baton,const char * name,VariableList & variable_list)8620b57cec5SDimitry Andric   static size_t GetVariableCallback(void *baton, const char *name,
8630b57cec5SDimitry Andric                                     VariableList &variable_list) {
8649dba64beSDimitry Andric     size_t old_size = variable_list.GetSize();
8650b57cec5SDimitry Andric     Target *target = static_cast<Target *>(baton);
8669dba64beSDimitry Andric     if (target)
8679dba64beSDimitry Andric       target->GetImages().FindGlobalVariables(ConstString(name), UINT32_MAX,
8689dba64beSDimitry Andric                                               variable_list);
8699dba64beSDimitry Andric     return variable_list.GetSize() - old_size;
8700b57cec5SDimitry Andric   }
8710b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)8720b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
8730b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
8740b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
8750b57cec5SDimitry Andric 
8760b57cec5SDimitry Andric     // If no argument is present, issue an error message.  There's no way to
8770b57cec5SDimitry Andric     // set a watchpoint.
8780b57cec5SDimitry Andric     if (command.GetArgumentCount() <= 0) {
879*5f7ddb14SDimitry Andric       result.AppendError("required argument missing; "
880*5f7ddb14SDimitry Andric                          "specify your program variable to watch for");
8810b57cec5SDimitry Andric       return false;
8820b57cec5SDimitry Andric     }
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric     // If no '-w' is specified, default to '-w write'.
8850b57cec5SDimitry Andric     if (!m_option_watchpoint.watch_type_specified) {
8860b57cec5SDimitry Andric       m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite;
8870b57cec5SDimitry Andric     }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric     // We passed the sanity check for the command. Proceed to set the
8900b57cec5SDimitry Andric     // watchpoint now.
8910b57cec5SDimitry Andric     lldb::addr_t addr = 0;
8920b57cec5SDimitry Andric     size_t size = 0;
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric     VariableSP var_sp;
8950b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
8960b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric     // A simple watch variable gesture allows only one argument.
8990b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
900*5f7ddb14SDimitry Andric       result.AppendError("specify exactly one variable to watch for");
9010b57cec5SDimitry Andric       return false;
9020b57cec5SDimitry Andric     }
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric     // Things have checked out ok...
9050b57cec5SDimitry Andric     Status error;
9060b57cec5SDimitry Andric     uint32_t expr_path_options =
9070b57cec5SDimitry Andric         StackFrame::eExpressionPathOptionCheckPtrVsMember |
9080b57cec5SDimitry Andric         StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
9090b57cec5SDimitry Andric     valobj_sp = frame->GetValueForVariableExpressionPath(
9100b57cec5SDimitry Andric         command.GetArgumentAtIndex(0), eNoDynamicValues, expr_path_options,
9110b57cec5SDimitry Andric         var_sp, error);
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric     if (!valobj_sp) {
9140b57cec5SDimitry Andric       // Not in the frame; let's check the globals.
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric       VariableList variable_list;
9170b57cec5SDimitry Andric       ValueObjectList valobj_list;
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric       Status error(Variable::GetValuesForVariableExpressionPath(
9200b57cec5SDimitry Andric           command.GetArgumentAtIndex(0),
9210b57cec5SDimitry Andric           m_exe_ctx.GetBestExecutionContextScope(), GetVariableCallback, target,
9220b57cec5SDimitry Andric           variable_list, valobj_list));
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric       if (valobj_list.GetSize())
9250b57cec5SDimitry Andric         valobj_sp = valobj_list.GetValueObjectAtIndex(0);
9260b57cec5SDimitry Andric     }
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric     CompilerType compiler_type;
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric     if (valobj_sp) {
9310b57cec5SDimitry Andric       AddressType addr_type;
9320b57cec5SDimitry Andric       addr = valobj_sp->GetAddressOf(false, &addr_type);
9330b57cec5SDimitry Andric       if (addr_type == eAddressTypeLoad) {
9340b57cec5SDimitry Andric         // We're in business.
9350b57cec5SDimitry Andric         // Find out the size of this variable.
9360b57cec5SDimitry Andric         size = m_option_watchpoint.watch_size == 0
937af732203SDimitry Andric                    ? valobj_sp->GetByteSize().getValueOr(0)
9380b57cec5SDimitry Andric                    : m_option_watchpoint.watch_size;
9390b57cec5SDimitry Andric       }
9400b57cec5SDimitry Andric       compiler_type = valobj_sp->GetCompilerType();
9410b57cec5SDimitry Andric     } else {
9420b57cec5SDimitry Andric       const char *error_cstr = error.AsCString(nullptr);
9430b57cec5SDimitry Andric       if (error_cstr)
944*5f7ddb14SDimitry Andric         result.AppendError(error_cstr);
9450b57cec5SDimitry Andric       else
946*5f7ddb14SDimitry Andric         result.AppendErrorWithFormat("unable to find any variable "
947*5f7ddb14SDimitry Andric                                      "expression path that matches '%s'",
9480b57cec5SDimitry Andric                                      command.GetArgumentAtIndex(0));
9490b57cec5SDimitry Andric       return false;
9500b57cec5SDimitry Andric     }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric     // Now it's time to create the watchpoint.
9530b57cec5SDimitry Andric     uint32_t watch_type = m_option_watchpoint.watch_type;
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric     error.Clear();
9560b57cec5SDimitry Andric     Watchpoint *wp =
9570b57cec5SDimitry Andric         target->CreateWatchpoint(addr, size, &compiler_type, watch_type, error)
9580b57cec5SDimitry Andric             .get();
9590b57cec5SDimitry Andric     if (wp) {
9600b57cec5SDimitry Andric       wp->SetWatchSpec(command.GetArgumentAtIndex(0));
9610b57cec5SDimitry Andric       wp->SetWatchVariable(true);
9620b57cec5SDimitry Andric       if (var_sp && var_sp->GetDeclaration().GetFile()) {
9630b57cec5SDimitry Andric         StreamString ss;
9640b57cec5SDimitry Andric         // True to show fullpath for declaration file.
9650b57cec5SDimitry Andric         var_sp->GetDeclaration().DumpStopContext(&ss, true);
9665ffd83dbSDimitry Andric         wp->SetDeclInfo(std::string(ss.GetString()));
9670b57cec5SDimitry Andric       }
9680b57cec5SDimitry Andric       output_stream.Printf("Watchpoint created: ");
9690b57cec5SDimitry Andric       wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
9700b57cec5SDimitry Andric       output_stream.EOL();
9710b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
9720b57cec5SDimitry Andric     } else {
9730b57cec5SDimitry Andric       result.AppendErrorWithFormat(
9740b57cec5SDimitry Andric           "Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64
9750b57cec5SDimitry Andric           ", variable expression='%s').\n",
9760b57cec5SDimitry Andric           addr, (uint64_t)size, command.GetArgumentAtIndex(0));
9770b57cec5SDimitry Andric       if (error.AsCString(nullptr))
9780b57cec5SDimitry Andric         result.AppendError(error.AsCString());
9790b57cec5SDimitry Andric     }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric     return result.Succeeded();
9820b57cec5SDimitry Andric   }
9830b57cec5SDimitry Andric 
9840b57cec5SDimitry Andric private:
9850b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
9860b57cec5SDimitry Andric   OptionGroupWatchpoint m_option_watchpoint;
9870b57cec5SDimitry Andric };
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric // CommandObjectWatchpointSetExpression
9900b57cec5SDimitry Andric #pragma mark Set
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric class CommandObjectWatchpointSetExpression : public CommandObjectRaw {
9930b57cec5SDimitry Andric public:
CommandObjectWatchpointSetExpression(CommandInterpreter & interpreter)9940b57cec5SDimitry Andric   CommandObjectWatchpointSetExpression(CommandInterpreter &interpreter)
9950b57cec5SDimitry Andric       : CommandObjectRaw(
9960b57cec5SDimitry Andric             interpreter, "watchpoint set expression",
9970b57cec5SDimitry Andric             "Set a watchpoint on an address by supplying an expression. "
9980b57cec5SDimitry Andric             "Use the '-w' option to specify the type of watchpoint and "
9990b57cec5SDimitry Andric             "the '-s' option to specify the byte size to watch for. "
10000b57cec5SDimitry Andric             "If no '-w' option is specified, it defaults to write. "
10010b57cec5SDimitry Andric             "If no '-s' option is specified, it defaults to the target's "
10020b57cec5SDimitry Andric             "pointer byte size. "
10030b57cec5SDimitry Andric             "Note that there are limited hardware resources for watchpoints. "
10040b57cec5SDimitry Andric             "If watchpoint setting fails, consider disable/delete existing "
10050b57cec5SDimitry Andric             "ones "
10060b57cec5SDimitry Andric             "to free up resources.",
10070b57cec5SDimitry Andric             "",
10080b57cec5SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
10090b57cec5SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
10100b57cec5SDimitry Andric         m_option_group(), m_option_watchpoint() {
10110b57cec5SDimitry Andric     SetHelpLong(
10120b57cec5SDimitry Andric         R"(
10130b57cec5SDimitry Andric Examples:
10140b57cec5SDimitry Andric 
10150b57cec5SDimitry Andric (lldb) watchpoint set expression -w write -s 1 -- foo + 32
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric     Watches write access for the 1-byte region pointed to by the address 'foo + 32')");
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric     CommandArgumentEntry arg;
10200b57cec5SDimitry Andric     CommandArgumentData expression_arg;
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric     // Define the only variant of this arg.
10230b57cec5SDimitry Andric     expression_arg.arg_type = eArgTypeExpression;
10240b57cec5SDimitry Andric     expression_arg.arg_repetition = eArgRepeatPlain;
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric     // Push the only variant into the argument entry.
10270b57cec5SDimitry Andric     arg.push_back(expression_arg);
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric     // Push the data for the only argument into the m_arguments vector.
10300b57cec5SDimitry Andric     m_arguments.push_back(arg);
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric     // Absorb the '-w' and '-s' options into our option group.
10330b57cec5SDimitry Andric     m_option_group.Append(&m_option_watchpoint, LLDB_OPT_SET_ALL,
10340b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
10350b57cec5SDimitry Andric     m_option_group.Finalize();
10360b57cec5SDimitry Andric   }
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric   ~CommandObjectWatchpointSetExpression() override = default;
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric   // Overrides base class's behavior where WantsCompletion =
10410b57cec5SDimitry Andric   // !WantsRawCommandString.
WantsCompletion()10420b57cec5SDimitry Andric   bool WantsCompletion() override { return true; }
10430b57cec5SDimitry Andric 
GetOptions()10440b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric protected:
DoExecute(llvm::StringRef raw_command,CommandReturnObject & result)10470b57cec5SDimitry Andric   bool DoExecute(llvm::StringRef raw_command,
10480b57cec5SDimitry Andric                  CommandReturnObject &result) override {
10490b57cec5SDimitry Andric     auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
10500b57cec5SDimitry Andric     m_option_group.NotifyOptionParsingStarting(
10510b57cec5SDimitry Andric         &exe_ctx); // This is a raw command, so notify the option group
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric     Target *target = GetDebugger().GetSelectedTarget().get();
10540b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric     OptionsWithRaw args(raw_command);
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric     llvm::StringRef expr = args.GetRawPart();
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric     if (args.HasArgs())
10610b57cec5SDimitry Andric       if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group,
10620b57cec5SDimitry Andric                                  exe_ctx))
10630b57cec5SDimitry Andric         return false;
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric     // If no argument is present, issue an error message.  There's no way to
10660b57cec5SDimitry Andric     // set a watchpoint.
10670b57cec5SDimitry Andric     if (raw_command.trim().empty()) {
1068*5f7ddb14SDimitry Andric       result.AppendError("required argument missing; specify an expression "
1069*5f7ddb14SDimitry Andric                          "to evaluate into the address to watch for");
10700b57cec5SDimitry Andric       return false;
10710b57cec5SDimitry Andric     }
10720b57cec5SDimitry Andric 
10730b57cec5SDimitry Andric     // If no '-w' is specified, default to '-w write'.
10740b57cec5SDimitry Andric     if (!m_option_watchpoint.watch_type_specified) {
10750b57cec5SDimitry Andric       m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchWrite;
10760b57cec5SDimitry Andric     }
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric     // We passed the sanity check for the command. Proceed to set the
10790b57cec5SDimitry Andric     // watchpoint now.
10800b57cec5SDimitry Andric     lldb::addr_t addr = 0;
10810b57cec5SDimitry Andric     size_t size = 0;
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric     // Use expression evaluation to arrive at the address to watch.
10860b57cec5SDimitry Andric     EvaluateExpressionOptions options;
10870b57cec5SDimitry Andric     options.SetCoerceToId(false);
10880b57cec5SDimitry Andric     options.SetUnwindOnError(true);
10890b57cec5SDimitry Andric     options.SetKeepInMemory(false);
10900b57cec5SDimitry Andric     options.SetTryAllThreads(true);
10910b57cec5SDimitry Andric     options.SetTimeout(llvm::None);
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric     ExpressionResults expr_result =
10940b57cec5SDimitry Andric         target->EvaluateExpression(expr, frame, valobj_sp, options);
10950b57cec5SDimitry Andric     if (expr_result != eExpressionCompleted) {
1096*5f7ddb14SDimitry Andric       result.AppendError("expression evaluation of address to watch failed");
1097*5f7ddb14SDimitry Andric       result.AppendErrorWithFormat("expression evaluated: \n%s", expr.data());
10985ffd83dbSDimitry Andric       if (valobj_sp && !valobj_sp->GetError().Success())
1099*5f7ddb14SDimitry Andric         result.AppendError(valobj_sp->GetError().AsCString());
11000b57cec5SDimitry Andric       return false;
11010b57cec5SDimitry Andric     }
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric     // Get the address to watch.
11040b57cec5SDimitry Andric     bool success = false;
11050b57cec5SDimitry Andric     addr = valobj_sp->GetValueAsUnsigned(0, &success);
11060b57cec5SDimitry Andric     if (!success) {
1107*5f7ddb14SDimitry Andric       result.AppendError("expression did not evaluate to an address");
11080b57cec5SDimitry Andric       return false;
11090b57cec5SDimitry Andric     }
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric     if (m_option_watchpoint.watch_size != 0)
11120b57cec5SDimitry Andric       size = m_option_watchpoint.watch_size;
11130b57cec5SDimitry Andric     else
11140b57cec5SDimitry Andric       size = target->GetArchitecture().GetAddressByteSize();
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric     // Now it's time to create the watchpoint.
11170b57cec5SDimitry Andric     uint32_t watch_type = m_option_watchpoint.watch_type;
11180b57cec5SDimitry Andric 
11190b57cec5SDimitry Andric     // Fetch the type from the value object, the type of the watched object is
11200b57cec5SDimitry Andric     // the pointee type
11210b57cec5SDimitry Andric     /// of the expression, so convert to that if we  found a valid type.
11220b57cec5SDimitry Andric     CompilerType compiler_type(valobj_sp->GetCompilerType());
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric     Status error;
11250b57cec5SDimitry Andric     Watchpoint *wp =
11260b57cec5SDimitry Andric         target->CreateWatchpoint(addr, size, &compiler_type, watch_type, error)
11270b57cec5SDimitry Andric             .get();
11280b57cec5SDimitry Andric     if (wp) {
11290b57cec5SDimitry Andric       Stream &output_stream = result.GetOutputStream();
11300b57cec5SDimitry Andric       output_stream.Printf("Watchpoint created: ");
11310b57cec5SDimitry Andric       wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
11320b57cec5SDimitry Andric       output_stream.EOL();
11330b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
11340b57cec5SDimitry Andric     } else {
11350b57cec5SDimitry Andric       result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64
11360b57cec5SDimitry Andric                                    ", size=%" PRIu64 ").\n",
11370b57cec5SDimitry Andric                                    addr, (uint64_t)size);
11380b57cec5SDimitry Andric       if (error.AsCString(nullptr))
11390b57cec5SDimitry Andric         result.AppendError(error.AsCString());
11400b57cec5SDimitry Andric     }
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric     return result.Succeeded();
11430b57cec5SDimitry Andric   }
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric private:
11460b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
11470b57cec5SDimitry Andric   OptionGroupWatchpoint m_option_watchpoint;
11480b57cec5SDimitry Andric };
11490b57cec5SDimitry Andric 
11500b57cec5SDimitry Andric // CommandObjectWatchpointSet
11510b57cec5SDimitry Andric #pragma mark Set
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric class CommandObjectWatchpointSet : public CommandObjectMultiword {
11540b57cec5SDimitry Andric public:
CommandObjectWatchpointSet(CommandInterpreter & interpreter)11550b57cec5SDimitry Andric   CommandObjectWatchpointSet(CommandInterpreter &interpreter)
11560b57cec5SDimitry Andric       : CommandObjectMultiword(
11570b57cec5SDimitry Andric             interpreter, "watchpoint set", "Commands for setting a watchpoint.",
11580b57cec5SDimitry Andric             "watchpoint set <subcommand> [<subcommand-options>]") {
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric     LoadSubCommand(
11610b57cec5SDimitry Andric         "variable",
11620b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectWatchpointSetVariable(interpreter)));
11630b57cec5SDimitry Andric     LoadSubCommand(
11640b57cec5SDimitry Andric         "expression",
11650b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectWatchpointSetExpression(interpreter)));
11660b57cec5SDimitry Andric   }
11670b57cec5SDimitry Andric 
11680b57cec5SDimitry Andric   ~CommandObjectWatchpointSet() override = default;
11690b57cec5SDimitry Andric };
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric // CommandObjectMultiwordWatchpoint
11720b57cec5SDimitry Andric #pragma mark MultiwordWatchpoint
11730b57cec5SDimitry Andric 
CommandObjectMultiwordWatchpoint(CommandInterpreter & interpreter)11740b57cec5SDimitry Andric CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(
11750b57cec5SDimitry Andric     CommandInterpreter &interpreter)
11760b57cec5SDimitry Andric     : CommandObjectMultiword(interpreter, "watchpoint",
11770b57cec5SDimitry Andric                              "Commands for operating on watchpoints.",
11780b57cec5SDimitry Andric                              "watchpoint <subcommand> [<command-options>]") {
11790b57cec5SDimitry Andric   CommandObjectSP list_command_object(
11800b57cec5SDimitry Andric       new CommandObjectWatchpointList(interpreter));
11810b57cec5SDimitry Andric   CommandObjectSP enable_command_object(
11820b57cec5SDimitry Andric       new CommandObjectWatchpointEnable(interpreter));
11830b57cec5SDimitry Andric   CommandObjectSP disable_command_object(
11840b57cec5SDimitry Andric       new CommandObjectWatchpointDisable(interpreter));
11850b57cec5SDimitry Andric   CommandObjectSP delete_command_object(
11860b57cec5SDimitry Andric       new CommandObjectWatchpointDelete(interpreter));
11870b57cec5SDimitry Andric   CommandObjectSP ignore_command_object(
11880b57cec5SDimitry Andric       new CommandObjectWatchpointIgnore(interpreter));
11890b57cec5SDimitry Andric   CommandObjectSP command_command_object(
11900b57cec5SDimitry Andric       new CommandObjectWatchpointCommand(interpreter));
11910b57cec5SDimitry Andric   CommandObjectSP modify_command_object(
11920b57cec5SDimitry Andric       new CommandObjectWatchpointModify(interpreter));
11930b57cec5SDimitry Andric   CommandObjectSP set_command_object(
11940b57cec5SDimitry Andric       new CommandObjectWatchpointSet(interpreter));
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric   list_command_object->SetCommandName("watchpoint list");
11970b57cec5SDimitry Andric   enable_command_object->SetCommandName("watchpoint enable");
11980b57cec5SDimitry Andric   disable_command_object->SetCommandName("watchpoint disable");
11990b57cec5SDimitry Andric   delete_command_object->SetCommandName("watchpoint delete");
12000b57cec5SDimitry Andric   ignore_command_object->SetCommandName("watchpoint ignore");
12010b57cec5SDimitry Andric   command_command_object->SetCommandName("watchpoint command");
12020b57cec5SDimitry Andric   modify_command_object->SetCommandName("watchpoint modify");
12030b57cec5SDimitry Andric   set_command_object->SetCommandName("watchpoint set");
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric   LoadSubCommand("list", list_command_object);
12060b57cec5SDimitry Andric   LoadSubCommand("enable", enable_command_object);
12070b57cec5SDimitry Andric   LoadSubCommand("disable", disable_command_object);
12080b57cec5SDimitry Andric   LoadSubCommand("delete", delete_command_object);
12090b57cec5SDimitry Andric   LoadSubCommand("ignore", ignore_command_object);
12100b57cec5SDimitry Andric   LoadSubCommand("command", command_command_object);
12110b57cec5SDimitry Andric   LoadSubCommand("modify", modify_command_object);
12120b57cec5SDimitry Andric   LoadSubCommand("set", set_command_object);
12130b57cec5SDimitry Andric }
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint() = default;
1216