blob: 46ce80a8ea80af12fb5ccc30802344f08d5c8d98 [file] [log] [blame]
Greg Clayton57b3c6b2011-04-27 22:04:39 +00001//===-- OptionGroupFormat.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
Johnny Chena0f34692011-05-13 20:21:08 +000010#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000011
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Johnny Chen4003f572011-09-10 00:48:33 +000016#include "lldb/Utility/Utils.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000017
18using namespace lldb;
19using namespace lldb_private;
20
Greg Claytona42880a2011-10-25 06:44:01 +000021OptionGroupFormat::OptionGroupFormat (lldb::Format default_format,
22 uint64_t default_byte_size,
23 uint64_t default_count) :
24 m_format (default_format, default_format),
25 m_byte_size (default_byte_size, default_byte_size),
Greg Clayton9c236732011-10-26 00:56:27 +000026 m_count (default_count, default_count),
27 m_prev_gdb_format('x'),
28 m_prev_gdb_size('w')
Greg Clayton57b3c6b2011-04-27 22:04:39 +000029{
30}
31
32OptionGroupFormat::~OptionGroupFormat ()
33{
34}
35
36static OptionDefinition
37g_option_table[] =
38{
Greg Clayton9c236732011-10-26 00:56:27 +000039{ LLDB_OPT_SET_1, false, "format" ,'f', required_argument, NULL, 0, eArgTypeFormat , "Specify a format to be used for display."},
Greg Clayton24a6bd92011-10-27 17:55:14 +000040{ LLDB_OPT_SET_2, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
41{ LLDB_OPT_SET_3, false, "size" ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
42{ LLDB_OPT_SET_4, false, "count" ,'c', required_argument, NULL, 0, eArgTypeCount , "The number of total items to display."},
Greg Clayton57b3c6b2011-04-27 22:04:39 +000043};
Greg Clayton57b3c6b2011-04-27 22:04:39 +000044
45uint32_t
46OptionGroupFormat::GetNumDefinitions ()
47{
Greg Claytona42880a2011-10-25 06:44:01 +000048 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
49 {
50 if (m_count.GetDefaultValue() < UINT64_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +000051 return 4;
Greg Claytona42880a2011-10-25 06:44:01 +000052 else
Greg Clayton9c236732011-10-26 00:56:27 +000053 return 3;
Greg Claytona42880a2011-10-25 06:44:01 +000054 }
Greg Clayton9c236732011-10-26 00:56:27 +000055 return 2;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000056}
57
58const OptionDefinition *
59OptionGroupFormat::GetDefinitions ()
60{
61 return g_option_table;
62}
63
64Error
65OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
Greg Clayton56bbdaf2011-04-28 20:55:26 +000066 uint32_t option_idx,
67 const char *option_arg)
Greg Clayton57b3c6b2011-04-27 22:04:39 +000068{
69 Error error;
70 char short_option = (char) g_option_table[option_idx].short_option;
71
72 switch (short_option)
73 {
74 case 'f':
75 error = m_format.SetValueFromCString (option_arg);
76 break;
77
Greg Claytona42880a2011-10-25 06:44:01 +000078 case 'c':
79 if (m_count.GetDefaultValue() == 0)
80 {
81 error.SetErrorString ("--count option is disabled");
82 }
83 else
84 {
85 error = m_count.SetValueFromCString (option_arg);
86 if (m_count.GetCurrentValue() == 0)
87 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
88 }
89 break;
90
91 case 's':
92 if (m_byte_size.GetDefaultValue() == 0)
93 {
94 error.SetErrorString ("--size option is disabled");
95 }
96 else
97 {
98 error = m_byte_size.SetValueFromCString (option_arg);
99 if (m_byte_size.GetCurrentValue() == 0)
100 error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
101 }
102 break;
103
Greg Clayton9c236732011-10-26 00:56:27 +0000104 case 'G':
105 {
106 char *end = NULL;
107 const char *gdb_format_cstr = option_arg;
108 uint64_t count = 0;
109 if (::isdigit (gdb_format_cstr[0]))
110 {
111 count = strtoull (gdb_format_cstr, &end, 0);
112
113 if (option_arg != end)
114 gdb_format_cstr = end; // We have a valid count, advance the string position
115 else
116 count = 0;
117 }
118
119 Format format = SetFormatUsingGDBFormatLetter (gdb_format_cstr[0]);
120 if (format != eFormatInvalid)
121 ++gdb_format_cstr;
122
123 uint32_t byte_size = SetByteSizeUsingGDBSizeLetter (gdb_format_cstr[0]);
Greg Clayton902b5be2011-10-26 04:32:38 +0000124 if (byte_size > 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000125 ++gdb_format_cstr;
126
127 // We the first character of the "gdb_format_cstr" is not the
128 // NULL terminator, we didn't consume the entire string and
129 // something is wrong. Also, if none of the format, size or count
130 // was specified correctly, then abort.
131 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
132 {
133 // Nothing got set correctly
134 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
135 return error;
136 }
137
138 // At least one of the format, size or count was set correctly.
139 // Anything that wasn't set correctly should be set to the
140 // previous default
141 if (format == eFormatInvalid)
142 format = SetFormatUsingGDBFormatLetter (m_prev_gdb_format);
143
144 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
145 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
146 if (byte_size_enabled)
147 {
148 // Byte size is enabled
149 if (byte_size == 0)
150 byte_size = SetByteSizeUsingGDBSizeLetter (m_prev_gdb_size);
151 }
152 else
153 {
154 // Byte size is disabled, make sure it wasn't specified
155 if (byte_size > 0)
156 {
157 error.SetErrorString ("this command doesn't support specifying a byte size");
158 return error;
159 }
160 }
161
162 if (count_enabled)
163 {
164 // Count is enabled and was not set, set it to the default
165 if (count == 0)
166 count = m_count.GetDefaultValue();
167 }
168 else
169 {
170 // Count is disabled, make sure it wasn't specified
171 if (count > 0)
172 {
173 error.SetErrorString ("this command doesn't support specifying a count");
174 return error;
175 }
176 }
177
178 m_format.SetCurrentValue (format);
179 m_format.SetOptionWasSet ();
180 if (byte_size_enabled)
181 {
182 m_byte_size.SetCurrentValue (byte_size);
183 m_byte_size.SetOptionWasSet ();
184 }
185 if (count_enabled)
186 {
187 m_count.SetCurrentValue(count);
188 m_count.SetOptionWasSet ();
189 }
190 }
191 break;
192
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000193 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000194 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000195 break;
196 }
197
198 return error;
199}
200
Greg Clayton9c236732011-10-26 00:56:27 +0000201Format
202OptionGroupFormat::SetFormatUsingGDBFormatLetter (char format_letter)
203{
204 Format format = eFormatInvalid;
205 switch (format_letter)
206 {
207 case 'o': format = eFormatOctal; break;
208 case 'x': format = eFormatHex; break;
209 case 'd': format = eFormatDecimal; break;
210 case 'u': format = eFormatUnsigned; break;
211 case 't': format = eFormatBinary; break;
212 case 'f': format = eFormatFloat; break;
213 case 'a': format = eFormatHex; break; // TODO: add a new format: eFormatAddress
214 case 'i': format = eFormatHex; break; // TODO: add a new format: eFormatInstruction
215 case 'c': format = eFormatChar; break;
216 case 's': format = eFormatCString; break;
217 case 'T': format = eFormatOSType; break;
218 case 'A': format = eFormatHex; break; // TODO: add a new format: eFormatHexFloat
219 default: break;
220 }
221 if (format != eFormatInvalid)
222 m_prev_gdb_format = format_letter;
223 return format;
224}
225
226uint32_t
227OptionGroupFormat::SetByteSizeUsingGDBSizeLetter (char size_letter)
228{
229 uint32_t byte_size = 0;
230 switch (size_letter)
231 {
232 case 'b': // byte
233 byte_size = 1;
234 break;
235
236 case 'h': // halfword
237 byte_size = 2;
238 break;
239
240 case 'w': // word
241 byte_size = 4;
242 break;
243
244 case 'g': // giant
245 byte_size = 8;
246 break;
247
248 default:
249 break;
250 }
251 if (byte_size)
252 m_prev_gdb_size = size_letter;
253 return byte_size;
254}
255
256
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000257void
258OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
259{
260 m_format.Clear();
Greg Claytona42880a2011-10-25 06:44:01 +0000261 m_byte_size.Clear();
262 m_count.Clear();
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000263}