blob: 790cbb668fa1e5e15be6e876c2b8065db4906072 [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
Matt Kopecfe21d4f2013-02-21 23:55:31 +000010#include "lldb/lldb-python.h"
11
Johnny Chena0f34692011-05-13 20:21:08 +000012#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000013
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Greg Claytonc340ff82012-12-15 01:44:51 +000018#include "lldb/Core/ArchSpec.h"
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Target/ExecutionContext.h"
21#include "lldb/Target/Target.h"
Johnny Chen4003f572011-09-10 00:48:33 +000022#include "lldb/Utility/Utils.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Greg Claytona42880a2011-10-25 06:44:01 +000027OptionGroupFormat::OptionGroupFormat (lldb::Format default_format,
28 uint64_t default_byte_size,
29 uint64_t default_count) :
30 m_format (default_format, default_format),
31 m_byte_size (default_byte_size, default_byte_size),
Greg Clayton9c236732011-10-26 00:56:27 +000032 m_count (default_count, default_count),
33 m_prev_gdb_format('x'),
34 m_prev_gdb_size('w')
Greg Clayton57b3c6b2011-04-27 22:04:39 +000035{
36}
37
38OptionGroupFormat::~OptionGroupFormat ()
39{
40}
41
42static OptionDefinition
43g_option_table[] =
44{
Greg Clayton9c236732011-10-26 00:56:27 +000045{ 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 +000046{ LLDB_OPT_SET_2, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
47{ LLDB_OPT_SET_3, false, "size" ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
48{ 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 +000049};
Greg Clayton57b3c6b2011-04-27 22:04:39 +000050
51uint32_t
52OptionGroupFormat::GetNumDefinitions ()
53{
Greg Claytona42880a2011-10-25 06:44:01 +000054 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
55 {
56 if (m_count.GetDefaultValue() < UINT64_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +000057 return 4;
Greg Claytona42880a2011-10-25 06:44:01 +000058 else
Greg Clayton9c236732011-10-26 00:56:27 +000059 return 3;
Greg Claytona42880a2011-10-25 06:44:01 +000060 }
Greg Clayton9c236732011-10-26 00:56:27 +000061 return 2;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000062}
63
64const OptionDefinition *
65OptionGroupFormat::GetDefinitions ()
66{
67 return g_option_table;
68}
69
70Error
71OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
Greg Clayton56bbdaf2011-04-28 20:55:26 +000072 uint32_t option_idx,
73 const char *option_arg)
Greg Clayton57b3c6b2011-04-27 22:04:39 +000074{
75 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +000076 const int short_option = g_option_table[option_idx].short_option;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000077
78 switch (short_option)
79 {
80 case 'f':
81 error = m_format.SetValueFromCString (option_arg);
82 break;
83
Greg Claytona42880a2011-10-25 06:44:01 +000084 case 'c':
85 if (m_count.GetDefaultValue() == 0)
86 {
87 error.SetErrorString ("--count option is disabled");
88 }
89 else
90 {
91 error = m_count.SetValueFromCString (option_arg);
92 if (m_count.GetCurrentValue() == 0)
93 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
94 }
95 break;
96
97 case 's':
98 if (m_byte_size.GetDefaultValue() == 0)
99 {
100 error.SetErrorString ("--size option is disabled");
101 }
102 else
103 {
104 error = m_byte_size.SetValueFromCString (option_arg);
105 if (m_byte_size.GetCurrentValue() == 0)
106 error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
107 }
108 break;
109
Greg Clayton9c236732011-10-26 00:56:27 +0000110 case 'G':
111 {
112 char *end = NULL;
113 const char *gdb_format_cstr = option_arg;
114 uint64_t count = 0;
115 if (::isdigit (gdb_format_cstr[0]))
116 {
117 count = strtoull (gdb_format_cstr, &end, 0);
118
119 if (option_arg != end)
120 gdb_format_cstr = end; // We have a valid count, advance the string position
121 else
122 count = 0;
123 }
124
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000125 Format format = eFormatDefault;
126 uint32_t byte_size = 0;
127
Greg Claytonc340ff82012-12-15 01:44:51 +0000128 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size))
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000129 {
Greg Clayton9c236732011-10-26 00:56:27 +0000130 ++gdb_format_cstr;
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000131 }
Greg Clayton9c236732011-10-26 00:56:27 +0000132
133 // We the first character of the "gdb_format_cstr" is not the
134 // NULL terminator, we didn't consume the entire string and
135 // something is wrong. Also, if none of the format, size or count
136 // was specified correctly, then abort.
137 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
138 {
139 // Nothing got set correctly
140 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
141 return error;
142 }
143
144 // At least one of the format, size or count was set correctly.
145 // Anything that wasn't set correctly should be set to the
146 // previous default
147 if (format == eFormatInvalid)
Greg Claytonc340ff82012-12-15 01:44:51 +0000148 ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size);
Greg Clayton9c236732011-10-26 00:56:27 +0000149
150 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
151 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
152 if (byte_size_enabled)
153 {
154 // Byte size is enabled
155 if (byte_size == 0)
Greg Claytonc340ff82012-12-15 01:44:51 +0000156 ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
Greg Clayton9c236732011-10-26 00:56:27 +0000157 }
158 else
159 {
160 // Byte size is disabled, make sure it wasn't specified
161 if (byte_size > 0)
162 {
163 error.SetErrorString ("this command doesn't support specifying a byte size");
164 return error;
165 }
166 }
167
168 if (count_enabled)
169 {
Jim Inghame5f12972012-08-23 21:16:25 +0000170 // 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 +0000171 if (count == 0)
Jim Inghame5f12972012-08-23 21:16:25 +0000172 count = 1;
Greg Clayton9c236732011-10-26 00:56:27 +0000173 }
174 else
175 {
176 // Count is disabled, make sure it wasn't specified
177 if (count > 0)
178 {
179 error.SetErrorString ("this command doesn't support specifying a count");
180 return error;
181 }
182 }
183
184 m_format.SetCurrentValue (format);
185 m_format.SetOptionWasSet ();
186 if (byte_size_enabled)
187 {
188 m_byte_size.SetCurrentValue (byte_size);
189 m_byte_size.SetOptionWasSet ();
190 }
191 if (count_enabled)
192 {
193 m_count.SetCurrentValue(count);
194 m_count.SetOptionWasSet ();
195 }
196 }
197 break;
198
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000199 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000200 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000201 break;
202 }
203
204 return error;
205}
206
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000207bool
Greg Claytonc340ff82012-12-15 01:44:51 +0000208OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
Greg Clayton9c236732011-10-26 00:56:27 +0000209{
Enrico Granata45358912013-01-21 19:20:50 +0000210 m_has_gdb_format = true;
Greg Clayton9c236732011-10-26 00:56:27 +0000211 switch (format_letter)
212 {
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000213 case 'o': format = eFormatOctal; m_prev_gdb_format = format_letter; return true;
214 case 'x': format = eFormatHex; m_prev_gdb_format = format_letter; return true;
215 case 'd': format = eFormatDecimal; m_prev_gdb_format = format_letter; return true;
216 case 'u': format = eFormatUnsigned; m_prev_gdb_format = format_letter; return true;
217 case 't': format = eFormatBinary; m_prev_gdb_format = format_letter; return true;
218 case 'f': format = eFormatFloat; m_prev_gdb_format = format_letter; return true;
Greg Claytonc340ff82012-12-15 01:44:51 +0000219 case 'a': format = eFormatAddressInfo;
220 {
221 ExecutionContext exe_ctx(interpreter.GetExecutionContext());
222 Target *target = exe_ctx.GetTargetPtr();
223 if (target)
224 byte_size = target->GetArchitecture().GetAddressByteSize();
225 m_prev_gdb_format = format_letter;
226 return true;
227 }
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000228 case 'i': format = eFormatInstruction; m_prev_gdb_format = format_letter; return true;
229 case 'c': format = eFormatChar; m_prev_gdb_format = format_letter; return true;
230 case 's': format = eFormatCString; m_prev_gdb_format = format_letter; return true;
231 case 'T': format = eFormatOSType; m_prev_gdb_format = format_letter; return true;
232 case 'A': format = eFormatHexFloat; m_prev_gdb_format = format_letter; return true;
233 case 'b': byte_size = 1; m_prev_gdb_size = format_letter; return true;
234 case 'h': byte_size = 2; m_prev_gdb_size = format_letter; return true;
235 case 'w': byte_size = 4; m_prev_gdb_size = format_letter; return true;
236 case 'g': byte_size = 8; m_prev_gdb_size = format_letter; return true;
Greg Clayton9c236732011-10-26 00:56:27 +0000237 default: break;
238 }
Greg Clayton3ec8bb72011-10-28 23:27:55 +0000239 return false;
Greg Clayton9c236732011-10-26 00:56:27 +0000240}
241
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000242void
243OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
244{
245 m_format.Clear();
Greg Claytona42880a2011-10-25 06:44:01 +0000246 m_byte_size.Clear();
247 m_count.Clear();
Enrico Granata45358912013-01-21 19:20:50 +0000248 m_has_gdb_format = false;
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000249}