1 //===-- OptionGroupFormat.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Interpreter/OptionGroupFormat.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Utils.h"
25 using namespace lldb_private;
27 OptionGroupFormat::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),
32 m_count (default_count, default_count),
33 m_prev_gdb_format('x'),
38 OptionGroupFormat::~OptionGroupFormat ()
42 static OptionDefinition
45 { LLDB_OPT_SET_1, false, "format" ,'f', required_argument, NULL, 0, eArgTypeFormat , "Specify a format to be used for display."},
46 { 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."},
52 OptionGroupFormat::GetNumDefinitions ()
54 if (m_byte_size.GetDefaultValue() < UINT64_MAX)
56 if (m_count.GetDefaultValue() < UINT64_MAX)
64 const OptionDefinition *
65 OptionGroupFormat::GetDefinitions ()
67 return g_option_table;
71 OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
73 const char *option_arg)
76 const int short_option = g_option_table[option_idx].short_option;
81 error = m_format.SetValueFromCString (option_arg);
85 if (m_count.GetDefaultValue() == 0)
87 error.SetErrorString ("--count option is disabled");
91 error = m_count.SetValueFromCString (option_arg);
92 if (m_count.GetCurrentValue() == 0)
93 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
98 if (m_byte_size.GetDefaultValue() == 0)
100 error.SetErrorString ("--size option is disabled");
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);
113 const char *gdb_format_cstr = option_arg;
115 if (::isdigit (gdb_format_cstr[0]))
117 count = strtoull (gdb_format_cstr, &end, 0);
119 if (option_arg != end)
120 gdb_format_cstr = end; // We have a valid count, advance the string position
125 Format format = eFormatDefault;
126 uint32_t byte_size = 0;
128 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size))
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))
139 // Nothing got set correctly
140 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
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
147 if (format == eFormatInvalid)
148 ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size);
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)
154 // Byte size is enabled
156 ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
160 // Byte size is disabled, make sure it wasn't specified
163 error.SetErrorString ("this command doesn't support specifying a byte size");
170 // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
176 // Count is disabled, make sure it wasn't specified
179 error.SetErrorString ("this command doesn't support specifying a count");
184 m_format.SetCurrentValue (format);
185 m_format.SetOptionWasSet ();
186 if (byte_size_enabled)
188 m_byte_size.SetCurrentValue (byte_size);
189 m_byte_size.SetOptionWasSet ();
193 m_count.SetCurrentValue(count);
194 m_count.SetOptionWasSet ();
200 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
208 OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
210 m_has_gdb_format = true;
211 switch (format_letter)
213 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;
219 case 'a': format = eFormatAddressInfo;
221 ExecutionContext exe_ctx(interpreter.GetExecutionContext());
222 Target *target = exe_ctx.GetTargetPtr();
224 byte_size = target->GetArchitecture().GetAddressByteSize();
225 m_prev_gdb_format = format_letter;
228 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;
243 OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
248 m_has_gdb_format = false;