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"
14 // Other libraries and framework includes
16 #include "lldb/Core/ArchSpec.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Utils.h"
23 using namespace lldb_private;
25 OptionGroupFormat::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),
30 m_count(default_count, default_count), m_prev_gdb_format('x'),
31 m_prev_gdb_size('w') {}
33 OptionGroupFormat::~OptionGroupFormat() {}
35 static OptionDefinition g_option_table[] = {
36 {LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument,
37 nullptr, nullptr, 0, eArgTypeFormat,
38 "Specify a format to be used for display."},
39 {LLDB_OPT_SET_2, false, "gdb-format", 'G', OptionParser::eRequiredArgument,
40 nullptr, nullptr, 0, eArgTypeGDBFormat,
41 "Specify a format using a GDB format specifier string."},
42 {LLDB_OPT_SET_3, false, "size", 's', OptionParser::eRequiredArgument,
43 nullptr, nullptr, 0, eArgTypeByteSize,
44 "The size in bytes to use when displaying with the selected format."},
45 {LLDB_OPT_SET_4, false, "count", 'c', OptionParser::eRequiredArgument,
46 nullptr, nullptr, 0, eArgTypeCount,
47 "The number of total items to display."},
50 llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() {
51 auto result = llvm::makeArrayRef(g_option_table);
52 if (m_byte_size.GetDefaultValue() < UINT64_MAX) {
53 if (m_count.GetDefaultValue() < UINT64_MAX)
56 return result.take_front(3);
58 return result.take_front(2);
61 Error OptionGroupFormat::SetOptionValue(uint32_t option_idx,
62 llvm::StringRef option_arg,
63 ExecutionContext *execution_context) {
65 const int short_option = g_option_table[option_idx].short_option;
67 switch (short_option) {
69 error = m_format.SetValueFromString(option_arg);
73 if (m_count.GetDefaultValue() == 0) {
74 error.SetErrorString("--count option is disabled");
76 error = m_count.SetValueFromString(option_arg);
77 if (m_count.GetCurrentValue() == 0)
78 error.SetErrorStringWithFormat("invalid --count option value '%s'",
79 option_arg.str().c_str());
84 if (m_byte_size.GetDefaultValue() == 0) {
85 error.SetErrorString("--size option is disabled");
87 error = m_byte_size.SetValueFromString(option_arg);
88 if (m_byte_size.GetCurrentValue() == 0)
89 error.SetErrorStringWithFormat("invalid --size option value '%s'",
90 option_arg.str().c_str());
96 llvm::StringRef gdb_format_str = option_arg;
97 gdb_format_str.consumeInteger(0, count);
99 Format format = eFormatDefault;
100 uint32_t byte_size = 0;
102 while (!gdb_format_str.empty() &&
103 ParserGDBFormatLetter(execution_context, gdb_format_str[0], format,
105 gdb_format_str = gdb_format_str.drop_front();
108 // We the first character of the "gdb_format_str" is not the
109 // NULL terminator, we didn't consume the entire string and
110 // something is wrong. Also, if none of the format, size or count
111 // was specified correctly, then abort.
112 if (!gdb_format_str.empty() ||
113 (format == eFormatInvalid && byte_size == 0 && count == 0)) {
114 // Nothing got set correctly
115 error.SetErrorStringWithFormat("invalid gdb format string '%s'",
116 option_arg.str().c_str());
120 // At least one of the format, size or count was set correctly.
121 // Anything that wasn't set correctly should be set to the
123 if (format == eFormatInvalid)
124 ParserGDBFormatLetter(execution_context, m_prev_gdb_format, format,
127 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
128 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
129 if (byte_size_enabled) {
130 // Byte size is enabled
132 ParserGDBFormatLetter(execution_context, m_prev_gdb_size, format,
135 // Byte size is disabled, make sure it wasn't specified
136 // but if this is an address, it's actually necessary to
137 // specify one so don't error out
138 if (byte_size > 0 && format != lldb::eFormatAddressInfo) {
139 error.SetErrorString(
140 "this command doesn't support specifying a byte size");
146 // Count is enabled and was not set, set it to the default for gdb format
147 // statements (which is 1).
151 // Count is disabled, make sure it wasn't specified
153 error.SetErrorString("this command doesn't support specifying a count");
158 m_format.SetCurrentValue(format);
159 m_format.SetOptionWasSet();
160 if (byte_size_enabled) {
161 m_byte_size.SetCurrentValue(byte_size);
162 m_byte_size.SetOptionWasSet();
165 m_count.SetCurrentValue(count);
166 m_count.SetOptionWasSet();
171 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
178 bool OptionGroupFormat::ParserGDBFormatLetter(
179 ExecutionContext *execution_context, char format_letter, Format &format,
180 uint32_t &byte_size) {
181 m_has_gdb_format = true;
182 switch (format_letter) {
184 format = eFormatOctal;
185 m_prev_gdb_format = format_letter;
189 m_prev_gdb_format = format_letter;
192 format = eFormatDecimal;
193 m_prev_gdb_format = format_letter;
196 format = eFormatUnsigned;
197 m_prev_gdb_format = format_letter;
200 format = eFormatBinary;
201 m_prev_gdb_format = format_letter;
204 format = eFormatFloat;
205 m_prev_gdb_format = format_letter;
208 format = eFormatAddressInfo;
211 execution_context ? execution_context->GetTargetSP() : TargetSP();
213 byte_size = target_sp->GetArchitecture().GetAddressByteSize();
214 m_prev_gdb_format = format_letter;
218 format = eFormatInstruction;
219 m_prev_gdb_format = format_letter;
222 format = eFormatChar;
223 m_prev_gdb_format = format_letter;
226 format = eFormatCString;
227 m_prev_gdb_format = format_letter;
230 format = eFormatOSType;
231 m_prev_gdb_format = format_letter;
234 format = eFormatHexFloat;
235 m_prev_gdb_format = format_letter;
238 // Size isn't used for printing instructions, so if a size is specified, and
239 // the previous format was
240 // 'i', then we should reset it to the default ('x'). Otherwise we'll
241 // continue to print as instructions,
242 // which isn't expected.
255 m_prev_gdb_size = format_letter;
256 if (m_prev_gdb_format == 'i')
257 m_prev_gdb_format = 'x';
267 void OptionGroupFormat::OptionParsingStarting(
268 ExecutionContext *execution_context) {
272 m_has_gdb_format = false;