xref: /freebsd/contrib/llvm-project/lldb/source/Interpreter/OptionGroupWatchpoint.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 //===-- OptionGroupWatchpoint.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Interpreter/OptionGroupWatchpoint.h"
10 
11 #include "lldb/Host/OptionParser.h"
12 #include "lldb/Interpreter/OptionArgParser.h"
13 #include "lldb/lldb-enumerations.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 static constexpr OptionEnumValueElement g_watch_type[] = {
19     {
20         OptionGroupWatchpoint::eWatchRead,
21         "read",
22         "Watch for read",
23     },
24     {
25         OptionGroupWatchpoint::eWatchWrite,
26         "write",
27         "Watch for write",
28     },
29     {
30         OptionGroupWatchpoint::eWatchReadWrite,
31         "read_write",
32         "Watch for read/write",
33     },
34 };
35 
36 static constexpr OptionEnumValueElement g_watch_size[] = {
37     {
38         1,
39         "1",
40         "Watch for byte size of 1",
41     },
42     {
43         2,
44         "2",
45         "Watch for byte size of 2",
46     },
47     {
48         4,
49         "4",
50         "Watch for byte size of 4",
51     },
52     {
53         8,
54         "8",
55         "Watch for byte size of 8",
56     },
57 };
58 
59 static constexpr OptionDefinition g_option_table[] = {
60     {LLDB_OPT_SET_1, false, "watch", 'w', OptionParser::eRequiredArgument,
61      nullptr, OptionEnumValues(g_watch_type), 0, eArgTypeWatchType,
62      "Specify the type of watching to perform."},
63     {LLDB_OPT_SET_1, false, "size", 's', OptionParser::eRequiredArgument,
64      nullptr, OptionEnumValues(g_watch_size), 0, eArgTypeByteSize,
65      "Number of bytes to use to watch a region."}};
66 
67 bool OptionGroupWatchpoint::IsWatchSizeSupported(uint32_t watch_size) {
68   for (const auto& size : g_watch_size) {
69     if (0  == size.value)
70       break;
71     if (watch_size == size.value)
72       return true;
73   }
74   return false;
75 }
76 
77 Status
78 OptionGroupWatchpoint::SetOptionValue(uint32_t option_idx,
79                                       llvm::StringRef option_arg,
80                                       ExecutionContext *execution_context) {
81   Status error;
82   const int short_option = g_option_table[option_idx].short_option;
83   switch (short_option) {
84   case 'w': {
85     WatchType tmp_watch_type;
86     tmp_watch_type = (WatchType)OptionArgParser::ToOptionEnum(
87         option_arg, g_option_table[option_idx].enum_values, 0, error);
88     if (error.Success()) {
89       watch_type = tmp_watch_type;
90       watch_type_specified = true;
91     }
92     break;
93   }
94   case 's':
95     watch_size = (uint32_t)OptionArgParser::ToOptionEnum(
96         option_arg, g_option_table[option_idx].enum_values, 0, error);
97     break;
98 
99   default:
100     llvm_unreachable("Unimplemented option");
101   }
102 
103   return error;
104 }
105 
106 void OptionGroupWatchpoint::OptionParsingStarting(
107     ExecutionContext *execution_context) {
108   watch_type_specified = false;
109   watch_type = eWatchInvalid;
110   watch_size = 0;
111 }
112 
113 llvm::ArrayRef<OptionDefinition> OptionGroupWatchpoint::GetDefinitions() {
114   return llvm::makeArrayRef(g_option_table);
115 }
116