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/lldb-enumerations.h"
17 #include "lldb/Interpreter/Args.h"
18 #include "lldb/Utility/Utils.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 static OptionEnumValueElement g_watch_type[] =
24 {
25     { OptionGroupWatchpoint::eWatchRead,      "read",       "Watch for read"},
26     { OptionGroupWatchpoint::eWatchWrite,     "write",      "Watch for write"},
27     { OptionGroupWatchpoint::eWatchReadWrite, "read_write", "Watch for read/write"},
28     { 0, NULL, NULL }
29 };
30 
31 static OptionEnumValueElement g_watch_size[] =
32 {
33     { 1, "1", "Watch for byte size of 1"},
34     { 2, "2", "Watch for byte size of 2"},
35     { 4, "4", "Watch for byte size of 4"},
36     { 8, "8", "Watch for byte size of 8"},
37     { 0, NULL, NULL }
38 };
39 
40 static OptionDefinition
41 g_option_table[] =
42 {
43     { LLDB_OPT_SET_1, false, "watch", 'w', required_argument, g_watch_type, 0, eArgTypeWatchType, "Specify the type of watching to perform."},
44     { LLDB_OPT_SET_1, false, "xsize", 'x', required_argument, g_watch_size, 0, eArgTypeByteSize, "Number of bytes to use to watch a region."}
45 };
46 
47 
48 bool
IsWatchSizeSupported(uint32_t watch_size)49 OptionGroupWatchpoint::IsWatchSizeSupported(uint32_t watch_size)
50 {
51     for (uint32_t i = 0; i < llvm::array_lengthof(g_watch_size); ++i)
52     {
53         if (g_watch_size[i].value == 0)
54             break;
55         if (watch_size == g_watch_size[i].value)
56             return true;
57     }
58     return false;
59 }
60 
OptionGroupWatchpoint()61 OptionGroupWatchpoint::OptionGroupWatchpoint () :
62     OptionGroup()
63 {
64 }
65 
~OptionGroupWatchpoint()66 OptionGroupWatchpoint::~OptionGroupWatchpoint ()
67 {
68 }
69 
70 Error
SetOptionValue(CommandInterpreter & interpreter,uint32_t option_idx,const char * option_arg)71 OptionGroupWatchpoint::SetOptionValue (CommandInterpreter &interpreter,
72                                        uint32_t option_idx,
73                                        const char *option_arg)
74 {
75     Error error;
76     const int short_option = g_option_table[option_idx].short_option;
77     switch (short_option)
78     {
79         case 'w':
80         {
81             WatchType tmp_watch_type;
82             tmp_watch_type = (WatchType) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
83             if (error.Success())
84             {
85                 watch_type = tmp_watch_type;
86                 watch_type_specified = true;
87             }
88             break;
89         }
90         case 'x':
91             watch_size = (uint32_t) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
92             break;
93 
94         default:
95             error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
96             break;
97     }
98 
99     return error;
100 }
101 
102 void
OptionParsingStarting(CommandInterpreter & interpreter)103 OptionGroupWatchpoint::OptionParsingStarting (CommandInterpreter &interpreter)
104 {
105     watch_type_specified = false;
106     watch_type = eWatchInvalid;
107     watch_size = 0;
108 }
109 
110 
111 const OptionDefinition*
GetDefinitions()112 OptionGroupWatchpoint::GetDefinitions ()
113 {
114     return g_option_table;
115 }
116 
117 uint32_t
GetNumDefinitions()118 OptionGroupWatchpoint::GetNumDefinitions ()
119 {
120     return llvm::array_lengthof(g_option_table);
121 }
122