blob: 9ac603a436a129f1dc8fd881c069782f45614e90 [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."},
40{ LLDB_OPT_SET_1|
41 LLDB_OPT_SET_2|
42 LLDB_OPT_SET_3, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
43{ LLDB_OPT_SET_2, false, "size" ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
44{ LLDB_OPT_SET_3, false, "count" ,'c', required_argument, NULL, 0, eArgTypeCount , "The number of total items to display."},
Greg Clayton57b3c6b2011-04-27 22:04:39 +000045};
Greg Clayton57b3c6b2011-04-27 22:04:39 +000046
47uint32_t
48OptionGroupFormat::GetNumDefinitions ()
49{
Greg Claytona42880a2011-10-25 06:44:01 +000050 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
51 {
52 if (m_count.GetDefaultValue() < UINT64_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +000053 return 4;
Greg Claytona42880a2011-10-25 06:44:01 +000054 else
Greg Clayton9c236732011-10-26 00:56:27 +000055 return 3;
Greg Claytona42880a2011-10-25 06:44:01 +000056 }
Greg Clayton9c236732011-10-26 00:56:27 +000057 return 2;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000058}
59
60const OptionDefinition *
61OptionGroupFormat::GetDefinitions ()
62{
63 return g_option_table;
64}
65
66Error
67OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
Greg Clayton56bbdaf2011-04-28 20:55:26 +000068 uint32_t option_idx,
69 const char *option_arg)
Greg Clayton57b3c6b2011-04-27 22:04:39 +000070{
71 Error error;
72 char short_option = (char) g_option_table[option_idx].short_option;
73
74 switch (short_option)
75 {
76 case 'f':
77 error = m_format.SetValueFromCString (option_arg);
78 break;
79
Greg Claytona42880a2011-10-25 06:44:01 +000080 case 'c':
81 if (m_count.GetDefaultValue() == 0)
82 {
83 error.SetErrorString ("--count option is disabled");
84 }
85 else
86 {
87 error = m_count.SetValueFromCString (option_arg);
88 if (m_count.GetCurrentValue() == 0)
89 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
90 }
91 break;
92
93 case 's':
94 if (m_byte_size.GetDefaultValue() == 0)
95 {
96 error.SetErrorString ("--size option is disabled");
97 }
98 else
99 {
100 error = m_byte_size.SetValueFromCString (option_arg);
101 if (m_byte_size.GetCurrentValue() == 0)
102 error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
103 }
104 break;
105
Greg Clayton9c236732011-10-26 00:56:27 +0000106 case 'G':
107 {
108 char *end = NULL;
109 const char *gdb_format_cstr = option_arg;
110 uint64_t count = 0;
111 if (::isdigit (gdb_format_cstr[0]))
112 {
113 count = strtoull (gdb_format_cstr, &end, 0);
114
115 if (option_arg != end)
116 gdb_format_cstr = end; // We have a valid count, advance the string position
117 else
118 count = 0;
119 }
120
121 Format format = SetFormatUsingGDBFormatLetter (gdb_format_cstr[0]);
122 if (format != eFormatInvalid)
123 ++gdb_format_cstr;
124
125 uint32_t byte_size = SetByteSizeUsingGDBSizeLetter (gdb_format_cstr[0]);
Greg Clayton902b5be2011-10-26 04:32:38 +0000126 if (byte_size > 0)
Greg Clayton9c236732011-10-26 00:56:27 +0000127 ++gdb_format_cstr;
128
129 // We the first character of the "gdb_format_cstr" is not the
130 // NULL terminator, we didn't consume the entire string and
131 // something is wrong. Also, if none of the format, size or count
132 // was specified correctly, then abort.
133 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
134 {
135 // Nothing got set correctly
136 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
137 return error;
138 }
139
140 // At least one of the format, size or count was set correctly.
141 // Anything that wasn't set correctly should be set to the
142 // previous default
143 if (format == eFormatInvalid)
144 format = SetFormatUsingGDBFormatLetter (m_prev_gdb_format);
145
146 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
147 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
148 if (byte_size_enabled)
149 {
150 // Byte size is enabled
151 if (byte_size == 0)
152 byte_size = SetByteSizeUsingGDBSizeLetter (m_prev_gdb_size);
153 }
154 else
155 {
156 // Byte size is disabled, make sure it wasn't specified
157 if (byte_size > 0)
158 {
159 error.SetErrorString ("this command doesn't support specifying a byte size");
160 return error;
161 }
162 }
163
164 if (count_enabled)
165 {
166 // Count is enabled and was not set, set it to the default
167 if (count == 0)
168 count = m_count.GetDefaultValue();
169 }
170 else
171 {
172 // Count is disabled, make sure it wasn't specified
173 if (count > 0)
174 {
175 error.SetErrorString ("this command doesn't support specifying a count");
176 return error;
177 }
178 }
179
180 m_format.SetCurrentValue (format);
181 m_format.SetOptionWasSet ();
182 if (byte_size_enabled)
183 {
184 m_byte_size.SetCurrentValue (byte_size);
185 m_byte_size.SetOptionWasSet ();
186 }
187 if (count_enabled)
188 {
189 m_count.SetCurrentValue(count);
190 m_count.SetOptionWasSet ();
191 }
192 }
193 break;
194
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000195 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000196 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000197 break;
198 }
199
200 return error;
201}
202
Greg Clayton9c236732011-10-26 00:56:27 +0000203Format
204OptionGroupFormat::SetFormatUsingGDBFormatLetter (char format_letter)
205{
206 Format format = eFormatInvalid;
207 switch (format_letter)
208 {
209 case 'o': format = eFormatOctal; break;
210 case 'x': format = eFormatHex; break;
211 case 'd': format = eFormatDecimal; break;
212 case 'u': format = eFormatUnsigned; break;
213 case 't': format = eFormatBinary; break;
214 case 'f': format = eFormatFloat; break;
215 case 'a': format = eFormatHex; break; // TODO: add a new format: eFormatAddress
216 case 'i': format = eFormatHex; break; // TODO: add a new format: eFormatInstruction
217 case 'c': format = eFormatChar; break;
218 case 's': format = eFormatCString; break;
219 case 'T': format = eFormatOSType; break;
220 case 'A': format = eFormatHex; break; // TODO: add a new format: eFormatHexFloat
221 default: break;
222 }
223 if (format != eFormatInvalid)
224 m_prev_gdb_format = format_letter;
225 return format;
226}
227
228uint32_t
229OptionGroupFormat::SetByteSizeUsingGDBSizeLetter (char size_letter)
230{
231 uint32_t byte_size = 0;
232 switch (size_letter)
233 {
234 case 'b': // byte
235 byte_size = 1;
236 break;
237
238 case 'h': // halfword
239 byte_size = 2;
240 break;
241
242 case 'w': // word
243 byte_size = 4;
244 break;
245
246 case 'g': // giant
247 byte_size = 8;
248 break;
249
250 default:
251 break;
252 }
253 if (byte_size)
254 m_prev_gdb_size = size_letter;
255 return byte_size;
256}
257
258
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000259void
260OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
261{
262 m_format.Clear();
Greg Claytona42880a2011-10-25 06:44:01 +0000263 m_byte_size.Clear();
264 m_count.Clear();
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000265}