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/Interpreter/Args.h"
17 #include "lldb/lldb-enumerations.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 static OptionEnumValueElement g_watch_type[] = {
23     {OptionGroupWatchpoint::eWatchRead, "read", "Watch for read"},
24     {OptionGroupWatchpoint::eWatchWrite, "write", "Watch for write"},
25     {OptionGroupWatchpoint::eWatchReadWrite, "read_write",
26      "Watch for read/write"},
27     {0, nullptr, nullptr}};
28 
29 static 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     {0, nullptr, nullptr}};
35 
36 static OptionDefinition g_option_table[] = {
37     {LLDB_OPT_SET_1, false, "watch", 'w', OptionParser::eRequiredArgument,
38      nullptr, g_watch_type, 0, eArgTypeWatchType,
39      "Specify the type of watching to perform."},
40     {LLDB_OPT_SET_1, false, "size", 's', OptionParser::eRequiredArgument,
41      nullptr, g_watch_size, 0, eArgTypeByteSize,
42      "Number of bytes to use to watch a region."}};
43 
44 bool OptionGroupWatchpoint::IsWatchSizeSupported(uint32_t watch_size) {
45   for (uint32_t i = 0; i < llvm::array_lengthof(g_watch_size); ++i) {
46     if (g_watch_size[i].value == 0)
47       break;
48     if (watch_size == g_watch_size[i].value)
49       return true;
50   }
51   return false;
52 }
53 
54 OptionGroupWatchpoint::OptionGroupWatchpoint() : OptionGroup() {}
55 
56 OptionGroupWatchpoint::~OptionGroupWatchpoint() {}
57 
58 Error OptionGroupWatchpoint::SetOptionValue(
59     uint32_t option_idx, llvm::StringRef option_arg,
60     ExecutionContext *execution_context) {
61   Error 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)Args::StringToOptionEnum(
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)Args::StringToOptionEnum(
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