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/Interpreter/OptionGroupFormat.h"
12 #include "lldb/Host/OptionParser.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Target.h"
18 using namespace lldb_private;
20 OptionGroupFormat::OptionGroupFormat(lldb::Format default_format,
21 uint64_t default_byte_size,
22 uint64_t default_count)
23 : m_format(default_format, default_format),
24 m_byte_size(default_byte_size, default_byte_size),
25 m_count(default_count, default_count), m_prev_gdb_format('x'),
26 m_prev_gdb_size('w') {}
28 OptionGroupFormat::~OptionGroupFormat() {}
30 static constexpr OptionDefinition g_option_table[] = {
31 {LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument,
32 nullptr, {}, 0, eArgTypeFormat,
33 "Specify a format to be used for display."},
34 {LLDB_OPT_SET_2, false, "gdb-format", 'G', OptionParser::eRequiredArgument,
35 nullptr, {}, 0, eArgTypeGDBFormat,
36 "Specify a format using a GDB format specifier string."},
37 {LLDB_OPT_SET_3, false, "size", 's', OptionParser::eRequiredArgument,
38 nullptr, {}, 0, eArgTypeByteSize,
39 "The size in bytes to use when displaying with the selected format."},
40 {LLDB_OPT_SET_4, false, "count", 'c', OptionParser::eRequiredArgument,
41 nullptr, {}, 0, eArgTypeCount,
42 "The number of total items to display."},
45 llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() {
46 auto result = llvm::makeArrayRef(g_option_table);
47 if (m_byte_size.GetDefaultValue() < UINT64_MAX) {
48 if (m_count.GetDefaultValue() < UINT64_MAX)
51 return result.take_front(3);
53 return result.take_front(2);
56 Status OptionGroupFormat::SetOptionValue(uint32_t option_idx,
57 llvm::StringRef option_arg,
58 ExecutionContext *execution_context) {
60 const int short_option = g_option_table[option_idx].short_option;
62 switch (short_option) {
64 error = m_format.SetValueFromString(option_arg);
68 if (m_count.GetDefaultValue() == 0) {
69 error.SetErrorString("--count option is disabled");
71 error = m_count.SetValueFromString(option_arg);
72 if (m_count.GetCurrentValue() == 0)
73 error.SetErrorStringWithFormat("invalid --count option value '%s'",
74 option_arg.str().c_str());
79 if (m_byte_size.GetDefaultValue() == 0) {
80 error.SetErrorString("--size option is disabled");
82 error = m_byte_size.SetValueFromString(option_arg);
83 if (m_byte_size.GetCurrentValue() == 0)
84 error.SetErrorStringWithFormat("invalid --size option value '%s'",
85 option_arg.str().c_str());
91 llvm::StringRef gdb_format_str = option_arg;
92 gdb_format_str.consumeInteger(0, count);
94 Format format = eFormatDefault;
95 uint32_t byte_size = 0;
97 while (!gdb_format_str.empty() &&
98 ParserGDBFormatLetter(execution_context, gdb_format_str[0], format,
100 gdb_format_str = gdb_format_str.drop_front();
103 // We the first character of the "gdb_format_str" is not the
104 // NULL terminator, we didn't consume the entire string and
105 // something is wrong. Also, if none of the format, size or count was
106 // specified correctly, then abort.
107 if (!gdb_format_str.empty() ||
108 (format == eFormatInvalid && byte_size == 0 && count == 0)) {
109 // Nothing got set correctly
110 error.SetErrorStringWithFormat("invalid gdb format string '%s'",
111 option_arg.str().c_str());
115 // At least one of the format, size or count was set correctly. Anything
116 // that wasn't set correctly should be set to the previous default
117 if (format == eFormatInvalid)
118 ParserGDBFormatLetter(execution_context, m_prev_gdb_format, format,
121 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
122 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
123 if (byte_size_enabled) {
124 // Byte size is enabled
126 ParserGDBFormatLetter(execution_context, m_prev_gdb_size, format,
129 // Byte size is disabled, make sure it wasn't specified but if this is an
130 // address, it's actually necessary to specify one so don't error out
131 if (byte_size > 0 && format != lldb::eFormatAddressInfo) {
132 error.SetErrorString(
133 "this command doesn't support specifying a byte size");
139 // Count is enabled and was not set, set it to the default for gdb format
140 // statements (which is 1).
144 // Count is disabled, make sure it wasn't specified
146 error.SetErrorString("this command doesn't support specifying a count");
151 m_format.SetCurrentValue(format);
152 m_format.SetOptionWasSet();
153 if (byte_size_enabled) {
154 m_byte_size.SetCurrentValue(byte_size);
155 m_byte_size.SetOptionWasSet();
158 m_count.SetCurrentValue(count);
159 m_count.SetOptionWasSet();
164 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
171 bool OptionGroupFormat::ParserGDBFormatLetter(
172 ExecutionContext *execution_context, char format_letter, Format &format,
173 uint32_t &byte_size) {
174 m_has_gdb_format = true;
175 switch (format_letter) {
177 format = eFormatOctal;
178 m_prev_gdb_format = format_letter;
182 m_prev_gdb_format = format_letter;
185 format = eFormatDecimal;
186 m_prev_gdb_format = format_letter;
189 format = eFormatUnsigned;
190 m_prev_gdb_format = format_letter;
193 format = eFormatBinary;
194 m_prev_gdb_format = format_letter;
197 format = eFormatFloat;
198 m_prev_gdb_format = format_letter;
201 format = eFormatAddressInfo;
204 execution_context ? execution_context->GetTargetSP() : TargetSP();
206 byte_size = target_sp->GetArchitecture().GetAddressByteSize();
207 m_prev_gdb_format = format_letter;
211 format = eFormatInstruction;
212 m_prev_gdb_format = format_letter;
215 format = eFormatChar;
216 m_prev_gdb_format = format_letter;
219 format = eFormatCString;
220 m_prev_gdb_format = format_letter;
223 format = eFormatOSType;
224 m_prev_gdb_format = format_letter;
227 format = eFormatHexFloat;
228 m_prev_gdb_format = format_letter;
236 // Size isn't used for printing instructions, so if a size is specified,
237 // and the previous format was 'i', then we should reset it to the
238 // default ('x'). Otherwise we'll continue to print as instructions,
239 // which isn't expected.
240 if (format_letter == 'b')
242 else if (format_letter == 'h')
244 else if (format_letter == 'w')
246 else if (format_letter == 'g')
249 m_prev_gdb_size = format_letter;
250 if (m_prev_gdb_format == 'i')
251 m_prev_gdb_format = 'x';
263 void OptionGroupFormat::OptionParsingStarting(
264 ExecutionContext *execution_context) {
268 m_has_gdb_format = false;