blob: 944bb18107d1bd6a275c6efc03db049af77461bc [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
Greg Claytonc340ff82012-12-15 01:44:51 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Interpreter/CommandInterpreter.h"
18#include "lldb/Target/ExecutionContext.h"
19#include "lldb/Target/Target.h"
Johnny Chen4003f572011-09-10 00:48:33 +000020#include "lldb/Utility/Utils.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Greg Claytona42880a2011-10-25 06:44:01 +000025OptionGroupFormat::OptionGroupFormat (lldb::Format default_format,
26 uint64_t default_byte_size,
27 uint64_t default_count) :
28 m_format (default_format, default_format),
29 m_byte_size (default_byte_size, default_byte_size),
Greg Clayton9c236732011-10-26 00:56:27 +000030 m_count (default_count, default_count),
31 m_prev_gdb_format('x'),
32 m_prev_gdb_size('w')
Greg Clayton57b3c6b2011-04-27 22:04:39 +000033{
34}
35
36OptionGroupFormat::~OptionGroupFormat ()
37{
38}
39
40static OptionDefinition
41g_option_table[] =
42{
Greg Clayton9c236732011-10-26 00:56:27 +000043{ 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 +000044{ LLDB_OPT_SET_2, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
45{ LLDB_OPT_SET_3, false, "size" ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
46{ 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 +000047};
Greg Clayton57b3c6b2011-04-27 22:04:39 +000048
49uint32_t
50OptionGroupFormat::GetNumDefinitions ()
51{
Greg Claytona42880a2011-10-25 06:44:01 +000052 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
53 {
54 if (m_count.GetDefaultValue() < UINT64_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +000055 return 4;
Greg Claytona42880a2011-10-25 06:44:01 +000056 else
Greg Clayton9c236732011-10-26 00:56:27 +000057 return 3;
Greg Claytona42880a2011-10-25 06:44:01 +000058 }
Greg Clayton9c236732011-10-26 00:56:27 +000059 return 2;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000060}
61
62const OptionDefinition *
63OptionGroupFormat::GetDefinitions ()
64{
65 return g_option_table;
66}
67
68Error
69OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
Greg Clayton56bbdaf2011-04-28 20:55:26 +000070 uint32_t option_idx,
71 const char *option_arg)
Greg Clayton57b3c6b2011-04-27 22:04:39 +000072{
73 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +000074 const int short_option = g_option_table[option_idx].short_option;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000075
76 switch (short_option)
77 {
78 case 'f':
79 error = m_format.SetValueFromCString (option_arg);
80 break;
81
Greg Claytona42880a2011-10-25 06:44:01 +000082 case 'c':
83 if (m_count.GetDefaultValue() == 0)
84 {
85 error.SetErrorString ("--count option is disabled");
86 }
87 else
88 {
89 error = m_count.SetValueFromCString (option_arg);
90 if (m_count.GetCurrentValue() == 0)
91 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
92 }
93 break;
94
95 case 's':
96 if (m_byte_size.GetDefaultValue() == 0)
97 {
98 error.SetErrorString ("--size option is disabled");
99 }
100 else
101 {
102 error = m_byte_size.SetValueFromCString (option_arg);
103 if (m_byte_size.GetCurrentValue() == 0)
104 error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
105 }
106 break;
107
Greg Clayton9c236732011-10-26 00:56:27 +0000108 case 'G':
109 {
110 char *end = NULL;
111 const char *gdb_format_cstr = option_arg;
112 uint64_t count = 0;
113 if (::isdigit (gdb_format_cstr[0]))
114 {
115 count = strtoull (gdb_format_cstr, &end, 0);
116
117 if (option_arg != end)
118 gdb_format_cstr = end; // We have a valid count, advance the string position
119 else
120 count = 0;
121 }
122
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000123 Format format = eFormatDefault;
124 uint32_t byte_size = 0;
125
Greg Claytonc340ff82012-12-15 01:44:51 +0000126 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size))
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000127 {
Greg Clayton9c236732011-10-26 00:56:27 +0000128 ++gdb_format_cstr;
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000129 }
Greg Clayton9c236732011-10-26 00:56:27 +0000130
131 // We the first character of the "gdb_format_cstr" is not the
132 // NULL terminator, we didn't consume the entire string and
133 // something is wrong. Also, if none of the format, size or count
134 // was specified correctly, then abort.
135 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
136 {
137 // Nothing got set correctly
138 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
139 return error;
140 }
141
142 // At least one of the format, size or count was set correctly.
143 // Anything that wasn't set correctly should be set to the
144 // previous default
145 if (format == eFormatInvalid)
Greg Claytonc340ff82012-12-15 01:44:51 +0000146 ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size);
Greg Clayton9c236732011-10-26 00:56:27 +0000147
148 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
149 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
150 if (byte_size_enabled)
151 {
152 // Byte size is enabled
153 if (byte_size == 0)
Greg Claytonc340ff82012-12-15 01:44:51 +0000154 ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
Greg Clayton9c236732011-10-26 00:56:27 +0000155 }
156 else
157 {
158 // Byte size is disabled, make sure it wasn't specified
159 if (byte_size > 0)
160 {
161 error.SetErrorString ("this command doesn't support specifying a byte size");
162 return error;
163 }
164 }
165
166 if (count_enabled)
167 {
Jim Inghame5f12972012-08-23 21:16:25 +0000168 // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
Greg Clayton9c236732011-10-26 00:56:27 +0000169 if (count == 0)
Jim Inghame5f12972012-08-23 21:16:25 +0000170 count = 1;
Greg Clayton9c236732011-10-26 00:56:27 +0000171 }
172 else
173 {
174 // Count is disabled, make sure it wasn't specified
175 if (count > 0)
176 {
177 error.SetErrorString ("this command doesn't support specifying a count");
178 return error;
179 }
180 }
181
182 m_format.SetCurrentValue (format);
183 m_format.SetOptionWasSet ();
184 if (byte_size_enabled)
185 {
186 m_byte_size.SetCurrentValue (byte_size);
187 m_byte_size.SetOptionWasSet ();
188 }
189 if (count_enabled)
190 {
191 m_count.SetCurrentValue(count);
192 m_count.SetOptionWasSet ();
193 }
194 }
195 break;
196
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000197 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000198 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000199 break;
200 }
201
202 return error;
203}
204
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000205bool
Greg Claytonc340ff82012-12-15 01:44:51 +0000206OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
Greg Clayton9c236732011-10-26 00:56:27 +0000207{
Enrico Granata45358912013-01-21 19:20:50 +0000208 m_has_gdb_format = true;
Greg Clayton9c236732011-10-26 00:56:27 +0000209 switch (format_letter)
210 {
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000211 case 'o': format = eFormatOctal; m_prev_gdb_format = format_letter; return true;
212 case 'x': format = eFormatHex; m_prev_gdb_format = format_letter; return true;
213 case 'd': format = eFormatDecimal; m_prev_gdb_format = format_letter; return true;
214 case 'u': format = eFormatUnsigned; m_prev_gdb_format = format_letter; return true;
215 case 't': format = eFormatBinary; m_prev_gdb_format = format_letter; return true;
216 case 'f': format = eFormatFloat; m_prev_gdb_format = format_letter; return true;
Greg Claytonc340ff82012-12-15 01:44:51 +0000217 case 'a': format = eFormatAddressInfo;
218 {
219 ExecutionContext exe_ctx(interpreter.GetExecutionContext());
220 Target *target = exe_ctx.GetTargetPtr();
221 if (target)
222 byte_size = target->GetArchitecture().GetAddressByteSize();
223 m_prev_gdb_format = format_letter;
224 return true;
225 }
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000226 case 'i': format = eFormatInstruction; m_prev_gdb_format = format_letter; return true;
227 case 'c': format = eFormatChar; m_prev_gdb_format = format_letter; return true;
228 case 's': format = eFormatCString; m_prev_gdb_format = format_letter; return true;
229 case 'T': format = eFormatOSType; m_prev_gdb_format = format_letter; return true;
230 case 'A': format = eFormatHexFloat; m_prev_gdb_format = format_letter; return true;
231 case 'b': byte_size = 1; m_prev_gdb_size = format_letter; return true;
232 case 'h': byte_size = 2; m_prev_gdb_size = format_letter; return true;
233 case 'w': byte_size = 4; m_prev_gdb_size = format_letter; return true;
234 case 'g': byte_size = 8; m_prev_gdb_size = format_letter; return true;
Greg Clayton9c236732011-10-26 00:56:27 +0000235 default: break;
236 }
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000237 return false;
Greg Clayton9c236732011-10-26 00:56:27 +0000238}
239
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000240void
241OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
242{
243 m_format.Clear();
Greg Claytona42880a2011-10-25 06:44:01 +0000244 m_byte_size.Clear();
245 m_count.Clear();
Enrico Granata45358912013-01-21 19:20:50 +0000246 m_has_gdb_format = false;
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000247}