blob: 601a78458730c0a7e926e9497b396fb31a111358 [file] [log] [blame]
Greg Clayton84c39662011-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 Kopec676a4872013-02-21 23:55:31 +000010#include "lldb/lldb-python.h"
11
Johnny Chen4bee32e2011-05-13 20:21:08 +000012#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton84c39662011-04-27 22:04:39 +000013
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Greg Clayton81409632012-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 Chen7c575b32011-09-10 00:48:33 +000022#include "lldb/Utility/Utils.h"
Greg Clayton84c39662011-04-27 22:04:39 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Greg Clayton1deb7962011-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 Clayton86edbf42011-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 Clayton84c39662011-04-27 22:04:39 +000035{
36}
37
38OptionGroupFormat::~OptionGroupFormat ()
39{
40}
41
42static OptionDefinition
43g_option_table[] =
44{
Zachary Turnerd37221d2014-07-09 16:31:49 +000045{ LLDB_OPT_SET_1, false, "format" ,'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFormat , "Specify a format to be used for display."},
46{ LLDB_OPT_SET_2, false, "gdb-format",'G', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
47{ LLDB_OPT_SET_3, false, "size" ,'s', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
48{ LLDB_OPT_SET_4, false, "count" ,'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCount , "The number of total items to display."},
Greg Clayton84c39662011-04-27 22:04:39 +000049};
Greg Clayton84c39662011-04-27 22:04:39 +000050
51uint32_t
52OptionGroupFormat::GetNumDefinitions ()
53{
Greg Clayton1deb7962011-10-25 06:44:01 +000054 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
55 {
56 if (m_count.GetDefaultValue() < UINT64_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +000057 return 4;
Greg Clayton1deb7962011-10-25 06:44:01 +000058 else
Greg Clayton86edbf42011-10-26 00:56:27 +000059 return 3;
Greg Clayton1deb7962011-10-25 06:44:01 +000060 }
Greg Clayton86edbf42011-10-26 00:56:27 +000061 return 2;
Greg Clayton84c39662011-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 Clayton68ebae62011-04-28 20:55:26 +000072 uint32_t option_idx,
73 const char *option_arg)
Greg Clayton84c39662011-04-27 22:04:39 +000074{
75 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +000076 const int short_option = g_option_table[option_idx].short_option;
Greg Clayton84c39662011-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 Clayton1deb7962011-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 Clayton86edbf42011-10-26 00:56:27 +0000110 case 'G':
111 {
Ed Masted78c9572014-04-20 00:31:37 +0000112 char *end = nullptr;
Greg Clayton86edbf42011-10-26 00:56:27 +0000113 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 Clayton7c533b22011-10-28 23:27:55 +0000125 Format format = eFormatDefault;
126 uint32_t byte_size = 0;
127
Greg Clayton81409632012-12-15 01:44:51 +0000128 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size))
Greg Clayton7c533b22011-10-28 23:27:55 +0000129 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000130 ++gdb_format_cstr;
Greg Clayton7c533b22011-10-28 23:27:55 +0000131 }
Greg Clayton86edbf42011-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 Clayton81409632012-12-15 01:44:51 +0000148 ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size);
Greg Clayton86edbf42011-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 Clayton81409632012-12-15 01:44:51 +0000156 ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
Greg Clayton86edbf42011-10-26 00:56:27 +0000157 }
158 else
159 {
160 // Byte size is disabled, make sure it wasn't specified
Enrico Granata98f13482013-10-11 22:09:26 +0000161 // but if this is an address, it's actually necessary to
162 // specify one so don't error out
163 if (byte_size > 0 && format != lldb::eFormatAddressInfo)
Greg Clayton86edbf42011-10-26 00:56:27 +0000164 {
165 error.SetErrorString ("this command doesn't support specifying a byte size");
166 return error;
167 }
168 }
169
170 if (count_enabled)
171 {
Jim Ingham608036a2012-08-23 21:16:25 +0000172 // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
Greg Clayton86edbf42011-10-26 00:56:27 +0000173 if (count == 0)
Jim Ingham608036a2012-08-23 21:16:25 +0000174 count = 1;
Greg Clayton86edbf42011-10-26 00:56:27 +0000175 }
176 else
177 {
178 // Count is disabled, make sure it wasn't specified
179 if (count > 0)
180 {
181 error.SetErrorString ("this command doesn't support specifying a count");
182 return error;
183 }
184 }
185
186 m_format.SetCurrentValue (format);
187 m_format.SetOptionWasSet ();
188 if (byte_size_enabled)
189 {
190 m_byte_size.SetCurrentValue (byte_size);
191 m_byte_size.SetOptionWasSet ();
192 }
193 if (count_enabled)
194 {
195 m_count.SetCurrentValue(count);
196 m_count.SetOptionWasSet ();
197 }
198 }
199 break;
200
Greg Clayton84c39662011-04-27 22:04:39 +0000201 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000202 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Greg Clayton84c39662011-04-27 22:04:39 +0000203 break;
204 }
205
206 return error;
207}
208
Greg Clayton7c533b22011-10-28 23:27:55 +0000209bool
Greg Clayton81409632012-12-15 01:44:51 +0000210OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
Greg Clayton86edbf42011-10-26 00:56:27 +0000211{
Enrico Granata6b4ddc62013-01-21 19:20:50 +0000212 m_has_gdb_format = true;
Greg Clayton86edbf42011-10-26 00:56:27 +0000213 switch (format_letter)
214 {
Greg Clayton7c533b22011-10-28 23:27:55 +0000215 case 'o': format = eFormatOctal; m_prev_gdb_format = format_letter; return true;
216 case 'x': format = eFormatHex; m_prev_gdb_format = format_letter; return true;
217 case 'd': format = eFormatDecimal; m_prev_gdb_format = format_letter; return true;
218 case 'u': format = eFormatUnsigned; m_prev_gdb_format = format_letter; return true;
219 case 't': format = eFormatBinary; m_prev_gdb_format = format_letter; return true;
220 case 'f': format = eFormatFloat; m_prev_gdb_format = format_letter; return true;
Greg Clayton81409632012-12-15 01:44:51 +0000221 case 'a': format = eFormatAddressInfo;
222 {
223 ExecutionContext exe_ctx(interpreter.GetExecutionContext());
224 Target *target = exe_ctx.GetTargetPtr();
225 if (target)
226 byte_size = target->GetArchitecture().GetAddressByteSize();
227 m_prev_gdb_format = format_letter;
228 return true;
229 }
Greg Clayton7c533b22011-10-28 23:27:55 +0000230 case 'i': format = eFormatInstruction; m_prev_gdb_format = format_letter; return true;
231 case 'c': format = eFormatChar; m_prev_gdb_format = format_letter; return true;
232 case 's': format = eFormatCString; m_prev_gdb_format = format_letter; return true;
233 case 'T': format = eFormatOSType; m_prev_gdb_format = format_letter; return true;
234 case 'A': format = eFormatHexFloat; m_prev_gdb_format = format_letter; return true;
235 case 'b': byte_size = 1; m_prev_gdb_size = format_letter; return true;
236 case 'h': byte_size = 2; m_prev_gdb_size = format_letter; return true;
237 case 'w': byte_size = 4; m_prev_gdb_size = format_letter; return true;
238 case 'g': byte_size = 8; m_prev_gdb_size = format_letter; return true;
Greg Clayton86edbf42011-10-26 00:56:27 +0000239 default: break;
240 }
Greg Clayton7c533b22011-10-28 23:27:55 +0000241 return false;
Greg Clayton86edbf42011-10-26 00:56:27 +0000242}
243
Greg Clayton84c39662011-04-27 22:04:39 +0000244void
245OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
246{
247 m_format.Clear();
Greg Clayton1deb7962011-10-25 06:44:01 +0000248 m_byte_size.Clear();
249 m_count.Clear();
Enrico Granata6b4ddc62013-01-21 19:20:50 +0000250 m_has_gdb_format = false;
Greg Clayton84c39662011-04-27 22:04:39 +0000251}