1 //===-- OptionGroupWatchpoint.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionGroupWatchpoint.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Host/OptionParser.h"
17 #include "lldb/Interpreter/OptionArgParser.h"
18 #include "lldb/lldb-enumerations.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 static constexpr OptionEnumValueElement g_watch_type[] = {
24     {OptionGroupWatchpoint::eWatchRead, "read", "Watch for read"},
25     {OptionGroupWatchpoint::eWatchWrite, "write", "Watch for write"},
26     {OptionGroupWatchpoint::eWatchReadWrite, "read_write",
27      "Watch for read/write"} };
28 
29 static constexpr OptionEnumValueElement g_watch_size[] = {
30     {1, "1", "Watch for byte size of 1"},
31     {2, "2", "Watch for byte size of 2"},
32     {4, "4", "Watch for byte size of 4"},
33     {8, "8", "Watch for byte size of 8"} };
34 
35 static constexpr OptionDefinition g_option_table[] = {
36     {LLDB_OPT_SET_1, false, "watch", 'w', OptionParser::eRequiredArgument,
37      nullptr, OptionEnumValues(g_watch_type), 0, eArgTypeWatchType,
38      "Specify the type of watching to perform."},
39     {LLDB_OPT_SET_1, false, "size", 's', OptionParser::eRequiredArgument,
40      nullptr, OptionEnumValues(g_watch_size), 0, eArgTypeByteSize,
41      "Number of bytes to use to watch a region."}};
42 
43 bool OptionGroupWatchpoint::IsWatchSizeSupported(uint32_t watch_size) {
44   for (const auto& size : g_watch_size) {
45     if (0  == size.value)
46       break;
47     if (watch_size == size.value)
48       return true;
49   }
50   return false;
51 }
52 
53 OptionGroupWatchpoint::OptionGroupWatchpoint() : OptionGroup() {}
54 
55 OptionGroupWatchpoint::~OptionGroupWatchpoint() {}
56 
57 Status
58 OptionGroupWatchpoint::SetOptionValue(uint32_t option_idx,
59                                       llvm::StringRef option_arg,
60                                       ExecutionContext *execution_context) {
61   Status error;
62   const int short_option = g_option_table[option_idx].short_option;
63   switch (short_option) {
64   case 'w': {
65     WatchType tmp_watch_type;
66     tmp_watch_type = (WatchType)OptionArgParser::ToOptionEnum(
67         option_arg, g_option_table[option_idx].enum_values, 0, error);
68     if (error.Success()) {
69       watch_type = tmp_watch_type;
70       watch_type_specified = true;
71     }
72     break;
73   }
74   case 's':
75     watch_size = (uint32_t)OptionArgParser::ToOptionEnum(
76         option_arg, g_option_table[option_idx].enum_values, 0, error);
77     break;
78 
79   default:
80     error.SetErrorStringWithFormat("unrecognized short option '%c'",
81                                    short_option);
82     break;
83   }
84 
85   return error;
86 }
87 
88 void OptionGroupWatchpoint::OptionParsingStarting(
89     ExecutionContext *execution_context) {
90   watch_type_specified = false;
91   watch_type = eWatchInvalid;
92   watch_size = 0;
93 }
94 
95 llvm::ArrayRef<OptionDefinition> OptionGroupWatchpoint::GetDefinitions() {
96   return llvm::makeArrayRef(g_option_table);
97 }
98