1*80814287SRaphael Isemann //===-- OptionGroupWatchpoint.cpp -----------------------------------------===//
2b1d7529eSJohnny Chen //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b1d7529eSJohnny Chen //
7b1d7529eSJohnny Chen //===----------------------------------------------------------------------===//
8b1d7529eSJohnny Chen 
9b1d7529eSJohnny Chen #include "lldb/Interpreter/OptionGroupWatchpoint.h"
10b1d7529eSJohnny Chen 
113eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h"
1247cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
13b9c1b51eSKate Stone #include "lldb/lldb-enumerations.h"
14b1d7529eSJohnny Chen 
15b1d7529eSJohnny Chen using namespace lldb;
16b1d7529eSJohnny Chen using namespace lldb_private;
17b1d7529eSJohnny Chen 
188fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_watch_type[] = {
19e063ecccSJonas Devlieghere     {
20e063ecccSJonas Devlieghere         OptionGroupWatchpoint::eWatchRead,
21e063ecccSJonas Devlieghere         "read",
22e063ecccSJonas Devlieghere         "Watch for read",
23e063ecccSJonas Devlieghere     },
24e063ecccSJonas Devlieghere     {
25e063ecccSJonas Devlieghere         OptionGroupWatchpoint::eWatchWrite,
26e063ecccSJonas Devlieghere         "write",
27e063ecccSJonas Devlieghere         "Watch for write",
28e063ecccSJonas Devlieghere     },
29e063ecccSJonas Devlieghere     {
30e063ecccSJonas Devlieghere         OptionGroupWatchpoint::eWatchReadWrite,
31e063ecccSJonas Devlieghere         "read_write",
32e063ecccSJonas Devlieghere         "Watch for read/write",
33e063ecccSJonas Devlieghere     },
34e063ecccSJonas Devlieghere };
35b1d7529eSJohnny Chen 
368fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_watch_size[] = {
37e063ecccSJonas Devlieghere     {
38e063ecccSJonas Devlieghere         1,
39e063ecccSJonas Devlieghere         "1",
40e063ecccSJonas Devlieghere         "Watch for byte size of 1",
41e063ecccSJonas Devlieghere     },
42e063ecccSJonas Devlieghere     {
43e063ecccSJonas Devlieghere         2,
44e063ecccSJonas Devlieghere         "2",
45e063ecccSJonas Devlieghere         "Watch for byte size of 2",
46e063ecccSJonas Devlieghere     },
47e063ecccSJonas Devlieghere     {
48e063ecccSJonas Devlieghere         4,
49e063ecccSJonas Devlieghere         "4",
50e063ecccSJonas Devlieghere         "Watch for byte size of 4",
51e063ecccSJonas Devlieghere     },
52e063ecccSJonas Devlieghere     {
53e063ecccSJonas Devlieghere         8,
54e063ecccSJonas Devlieghere         "8",
55e063ecccSJonas Devlieghere         "Watch for byte size of 8",
56e063ecccSJonas Devlieghere     },
57e063ecccSJonas Devlieghere };
58b62a3be1SJohnny Chen 
598fe53c49STatyana Krasnukha static constexpr OptionDefinition g_option_table[] = {
60b9c1b51eSKate Stone     {LLDB_OPT_SET_1, false, "watch", 'w', OptionParser::eRequiredArgument,
618fe53c49STatyana Krasnukha      nullptr, OptionEnumValues(g_watch_type), 0, eArgTypeWatchType,
62b9c1b51eSKate Stone      "Specify the type of watching to perform."},
63b9c1b51eSKate Stone     {LLDB_OPT_SET_1, false, "size", 's', OptionParser::eRequiredArgument,
648fe53c49STatyana Krasnukha      nullptr, OptionEnumValues(g_watch_size), 0, eArgTypeByteSize,
65b9c1b51eSKate Stone      "Number of bytes to use to watch a region."}};
66b1d7529eSJohnny Chen 
IsWatchSizeSupported(uint32_t watch_size)67b9c1b51eSKate Stone bool OptionGroupWatchpoint::IsWatchSizeSupported(uint32_t watch_size) {
688fe53c49STatyana Krasnukha   for (const auto& size : g_watch_size) {
698fe53c49STatyana Krasnukha     if (0  == size.value)
703cb41e82SJohnny Chen       break;
718fe53c49STatyana Krasnukha     if (watch_size == size.value)
723cb41e82SJohnny Chen       return true;
733cb41e82SJohnny Chen   }
743cb41e82SJohnny Chen   return false;
753cb41e82SJohnny Chen }
763cb41e82SJohnny Chen 
7797206d57SZachary Turner Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)7897206d57SZachary Turner OptionGroupWatchpoint::SetOptionValue(uint32_t option_idx,
7997206d57SZachary Turner                                       llvm::StringRef option_arg,
80b9c1b51eSKate Stone                                       ExecutionContext *execution_context) {
8197206d57SZachary Turner   Status error;
823bcdfc0eSGreg Clayton   const int short_option = g_option_table[option_idx].short_option;
83b9c1b51eSKate Stone   switch (short_option) {
84b9c1b51eSKate Stone   case 'w': {
85c6462317SJim Ingham     WatchType tmp_watch_type;
8647cbf4a0SPavel Labath     tmp_watch_type = (WatchType)OptionArgParser::ToOptionEnum(
87b9c1b51eSKate Stone         option_arg, g_option_table[option_idx].enum_values, 0, error);
88b9c1b51eSKate Stone     if (error.Success()) {
89c6462317SJim Ingham       watch_type = tmp_watch_type;
902ffa754aSJohnny Chen       watch_type_specified = true;
91c6462317SJim Ingham     }
92b1d7529eSJohnny Chen     break;
93c6462317SJim Ingham   }
947e9793f5SSean Callanan   case 's':
9547cbf4a0SPavel Labath     watch_size = (uint32_t)OptionArgParser::ToOptionEnum(
96b9c1b51eSKate Stone         option_arg, g_option_table[option_idx].enum_values, 0, error);
97b62a3be1SJohnny Chen     break;
98cf0e4f0dSGreg Clayton 
99b1d7529eSJohnny Chen   default:
10036162014SRaphael Isemann     llvm_unreachable("Unimplemented option");
101b1d7529eSJohnny Chen   }
102b1d7529eSJohnny Chen 
103b1d7529eSJohnny Chen   return error;
104b1d7529eSJohnny Chen }
105b1d7529eSJohnny Chen 
OptionParsingStarting(ExecutionContext * execution_context)106b9c1b51eSKate Stone void OptionGroupWatchpoint::OptionParsingStarting(
107b9c1b51eSKate Stone     ExecutionContext *execution_context) {
1082ffa754aSJohnny Chen   watch_type_specified = false;
109887062aeSJohnny Chen   watch_type = eWatchInvalid;
110b62a3be1SJohnny Chen   watch_size = 0;
111b1d7529eSJohnny Chen }
112b1d7529eSJohnny Chen 
GetDefinitions()1131f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> OptionGroupWatchpoint::GetDefinitions() {
11470602439SZachary Turner   return llvm::makeArrayRef(g_option_table);
115b1d7529eSJohnny Chen }
116