1cfca06d7SDimitry Andric //===-- OptionGroupFormat.cpp ---------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Interpreter/OptionGroupFormat.h"
10f034231aSEd Maste
1174a628f7SDimitry Andric #include "lldb/Host/OptionParser.h"
12f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
13f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
14f034231aSEd Maste #include "lldb/Target/Target.h"
15f034231aSEd Maste
16f034231aSEd Maste using namespace lldb;
17f034231aSEd Maste using namespace lldb_private;
18f034231aSEd Maste
19f65dcba8SDimitry Andric static constexpr OptionDefinition g_default_option_definitions[] = {
2014f1b3e8SDimitry Andric {LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument,
2194994d37SDimitry Andric nullptr, {}, 0, eArgTypeFormat,
2214f1b3e8SDimitry Andric "Specify a format to be used for display."},
2314f1b3e8SDimitry Andric {LLDB_OPT_SET_2, false, "gdb-format", 'G', OptionParser::eRequiredArgument,
2494994d37SDimitry Andric nullptr, {}, 0, eArgTypeGDBFormat,
2514f1b3e8SDimitry Andric "Specify a format using a GDB format specifier string."},
2614f1b3e8SDimitry Andric {LLDB_OPT_SET_3, false, "size", 's', OptionParser::eRequiredArgument,
2794994d37SDimitry Andric nullptr, {}, 0, eArgTypeByteSize,
2814f1b3e8SDimitry Andric "The size in bytes to use when displaying with the selected format."},
2914f1b3e8SDimitry Andric {LLDB_OPT_SET_4, false, "count", 'c', OptionParser::eRequiredArgument,
3094994d37SDimitry Andric nullptr, {}, 0, eArgTypeCount,
3114f1b3e8SDimitry Andric "The number of total items to display."},
32f034231aSEd Maste };
33f034231aSEd Maste
OptionGroupFormat(lldb::Format default_format,uint64_t default_byte_size,uint64_t default_count,OptionGroupFormatUsageTextVector usage_text_vector)34f65dcba8SDimitry Andric OptionGroupFormat::OptionGroupFormat(
35f65dcba8SDimitry Andric lldb::Format default_format, uint64_t default_byte_size,
36f65dcba8SDimitry Andric uint64_t default_count, OptionGroupFormatUsageTextVector usage_text_vector)
37f65dcba8SDimitry Andric : m_format(default_format, default_format),
38f65dcba8SDimitry Andric m_byte_size(default_byte_size, default_byte_size),
39f65dcba8SDimitry Andric m_count(default_count, default_count), m_prev_gdb_format('x'),
40e3b55780SDimitry Andric m_prev_gdb_size('w'), m_has_gdb_format(false) {
41f65dcba8SDimitry Andric // Copy the default option definitions.
42f65dcba8SDimitry Andric std::copy(std::begin(g_default_option_definitions),
43f65dcba8SDimitry Andric std::end(g_default_option_definitions),
44f65dcba8SDimitry Andric std::begin(m_option_definitions));
45f65dcba8SDimitry Andric
46f65dcba8SDimitry Andric for (auto usage_text_tuple : usage_text_vector) {
47f65dcba8SDimitry Andric switch (std::get<0>(usage_text_tuple)) {
48f65dcba8SDimitry Andric case eArgTypeFormat:
49f65dcba8SDimitry Andric m_option_definitions[0].usage_text = std::get<1>(usage_text_tuple);
50f65dcba8SDimitry Andric break;
51f65dcba8SDimitry Andric case eArgTypeByteSize:
52f65dcba8SDimitry Andric m_option_definitions[2].usage_text = std::get<1>(usage_text_tuple);
53f65dcba8SDimitry Andric break;
54f65dcba8SDimitry Andric default:
55f65dcba8SDimitry Andric llvm_unreachable("Unimplemented option");
56f65dcba8SDimitry Andric }
57f65dcba8SDimitry Andric }
58f65dcba8SDimitry Andric }
59f65dcba8SDimitry Andric
GetDefinitions()6014f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() {
61e3b55780SDimitry Andric auto result = llvm::ArrayRef(m_option_definitions);
6214f1b3e8SDimitry Andric if (m_byte_size.GetDefaultValue() < UINT64_MAX) {
63f034231aSEd Maste if (m_count.GetDefaultValue() < UINT64_MAX)
6414f1b3e8SDimitry Andric return result;
65f034231aSEd Maste else
6614f1b3e8SDimitry Andric return result.take_front(3);
67f034231aSEd Maste }
6814f1b3e8SDimitry Andric return result.take_front(2);
69f034231aSEd Maste }
70f034231aSEd Maste
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)71b76161e4SDimitry Andric Status OptionGroupFormat::SetOptionValue(uint32_t option_idx,
7214f1b3e8SDimitry Andric llvm::StringRef option_arg,
7314f1b3e8SDimitry Andric ExecutionContext *execution_context) {
74b76161e4SDimitry Andric Status error;
75f65dcba8SDimitry Andric const int short_option = m_option_definitions[option_idx].short_option;
76f034231aSEd Maste
7714f1b3e8SDimitry Andric switch (short_option) {
78f034231aSEd Maste case 'f':
795e95aa85SEd Maste error = m_format.SetValueFromString(option_arg);
80f034231aSEd Maste break;
81f034231aSEd Maste
82f034231aSEd Maste case 'c':
8314f1b3e8SDimitry Andric if (m_count.GetDefaultValue() == 0) {
84f034231aSEd Maste error.SetErrorString("--count option is disabled");
8514f1b3e8SDimitry Andric } else {
865e95aa85SEd Maste error = m_count.SetValueFromString(option_arg);
87f034231aSEd Maste if (m_count.GetCurrentValue() == 0)
8814f1b3e8SDimitry Andric error.SetErrorStringWithFormat("invalid --count option value '%s'",
8914f1b3e8SDimitry Andric option_arg.str().c_str());
90f034231aSEd Maste }
91f034231aSEd Maste break;
92f034231aSEd Maste
93f034231aSEd Maste case 's':
9414f1b3e8SDimitry Andric if (m_byte_size.GetDefaultValue() == 0) {
95f034231aSEd Maste error.SetErrorString("--size option is disabled");
9614f1b3e8SDimitry Andric } else {
975e95aa85SEd Maste error = m_byte_size.SetValueFromString(option_arg);
98f034231aSEd Maste if (m_byte_size.GetCurrentValue() == 0)
9914f1b3e8SDimitry Andric error.SetErrorStringWithFormat("invalid --size option value '%s'",
10014f1b3e8SDimitry Andric option_arg.str().c_str());
101f034231aSEd Maste }
102f034231aSEd Maste break;
103f034231aSEd Maste
10414f1b3e8SDimitry Andric case 'G': {
105f034231aSEd Maste uint64_t count = 0;
10614f1b3e8SDimitry Andric llvm::StringRef gdb_format_str = option_arg;
10714f1b3e8SDimitry Andric gdb_format_str.consumeInteger(0, count);
108f034231aSEd Maste
109f034231aSEd Maste Format format = eFormatDefault;
110f034231aSEd Maste uint32_t byte_size = 0;
111f034231aSEd Maste
11214f1b3e8SDimitry Andric while (!gdb_format_str.empty() &&
11314f1b3e8SDimitry Andric ParserGDBFormatLetter(execution_context, gdb_format_str[0], format,
11414f1b3e8SDimitry Andric byte_size)) {
11514f1b3e8SDimitry Andric gdb_format_str = gdb_format_str.drop_front();
116f034231aSEd Maste }
117f034231aSEd Maste
11814f1b3e8SDimitry Andric // We the first character of the "gdb_format_str" is not the
119f034231aSEd Maste // NULL terminator, we didn't consume the entire string and
120f73363f1SDimitry Andric // something is wrong. Also, if none of the format, size or count was
121f73363f1SDimitry Andric // specified correctly, then abort.
12214f1b3e8SDimitry Andric if (!gdb_format_str.empty() ||
12314f1b3e8SDimitry Andric (format == eFormatInvalid && byte_size == 0 && count == 0)) {
124f034231aSEd Maste // Nothing got set correctly
12514f1b3e8SDimitry Andric error.SetErrorStringWithFormat("invalid gdb format string '%s'",
12614f1b3e8SDimitry Andric option_arg.str().c_str());
127f034231aSEd Maste return error;
128f034231aSEd Maste }
129f034231aSEd Maste
130f73363f1SDimitry Andric // At least one of the format, size or count was set correctly. Anything
131f73363f1SDimitry Andric // that wasn't set correctly should be set to the previous default
132f034231aSEd Maste if (format == eFormatInvalid)
13314f1b3e8SDimitry Andric ParserGDBFormatLetter(execution_context, m_prev_gdb_format, format,
13414f1b3e8SDimitry Andric byte_size);
135f034231aSEd Maste
136f034231aSEd Maste const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
137f034231aSEd Maste const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
13814f1b3e8SDimitry Andric if (byte_size_enabled) {
139f034231aSEd Maste // Byte size is enabled
140f034231aSEd Maste if (byte_size == 0)
14114f1b3e8SDimitry Andric ParserGDBFormatLetter(execution_context, m_prev_gdb_size, format,
14214f1b3e8SDimitry Andric byte_size);
14314f1b3e8SDimitry Andric } else {
144f73363f1SDimitry Andric // Byte size is disabled, make sure it wasn't specified but if this is an
145f73363f1SDimitry Andric // address, it's actually necessary to specify one so don't error out
14614f1b3e8SDimitry Andric if (byte_size > 0 && format != lldb::eFormatAddressInfo) {
14714f1b3e8SDimitry Andric error.SetErrorString(
14814f1b3e8SDimitry Andric "this command doesn't support specifying a byte size");
149f034231aSEd Maste return error;
150f034231aSEd Maste }
151f034231aSEd Maste }
152f034231aSEd Maste
15314f1b3e8SDimitry Andric if (count_enabled) {
15414f1b3e8SDimitry Andric // Count is enabled and was not set, set it to the default for gdb format
15514f1b3e8SDimitry Andric // statements (which is 1).
156f034231aSEd Maste if (count == 0)
157f034231aSEd Maste count = 1;
15814f1b3e8SDimitry Andric } else {
159f034231aSEd Maste // Count is disabled, make sure it wasn't specified
16014f1b3e8SDimitry Andric if (count > 0) {
161f034231aSEd Maste error.SetErrorString("this command doesn't support specifying a count");
162f034231aSEd Maste return error;
163f034231aSEd Maste }
164f034231aSEd Maste }
165f034231aSEd Maste
166f034231aSEd Maste m_format.SetCurrentValue(format);
167f034231aSEd Maste m_format.SetOptionWasSet();
16814f1b3e8SDimitry Andric if (byte_size_enabled) {
169f034231aSEd Maste m_byte_size.SetCurrentValue(byte_size);
170f034231aSEd Maste m_byte_size.SetOptionWasSet();
171f034231aSEd Maste }
17214f1b3e8SDimitry Andric if (count_enabled) {
173f034231aSEd Maste m_count.SetCurrentValue(count);
174f034231aSEd Maste m_count.SetOptionWasSet();
175f034231aSEd Maste }
17614f1b3e8SDimitry Andric } break;
177f034231aSEd Maste
178f034231aSEd Maste default:
179ead24645SDimitry Andric llvm_unreachable("Unimplemented option");
180f034231aSEd Maste }
181f034231aSEd Maste
182f034231aSEd Maste return error;
183f034231aSEd Maste }
184f034231aSEd Maste
ParserGDBFormatLetter(ExecutionContext * execution_context,char format_letter,Format & format,uint32_t & byte_size)18514f1b3e8SDimitry Andric bool OptionGroupFormat::ParserGDBFormatLetter(
18614f1b3e8SDimitry Andric ExecutionContext *execution_context, char format_letter, Format &format,
18714f1b3e8SDimitry Andric uint32_t &byte_size) {
188f034231aSEd Maste m_has_gdb_format = true;
18914f1b3e8SDimitry Andric switch (format_letter) {
19014f1b3e8SDimitry Andric case 'o':
19114f1b3e8SDimitry Andric format = eFormatOctal;
19214f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
19314f1b3e8SDimitry Andric return true;
19414f1b3e8SDimitry Andric case 'x':
19514f1b3e8SDimitry Andric format = eFormatHex;
19614f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
19714f1b3e8SDimitry Andric return true;
19814f1b3e8SDimitry Andric case 'd':
19914f1b3e8SDimitry Andric format = eFormatDecimal;
20014f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
20114f1b3e8SDimitry Andric return true;
20214f1b3e8SDimitry Andric case 'u':
20314f1b3e8SDimitry Andric format = eFormatUnsigned;
20414f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
20514f1b3e8SDimitry Andric return true;
20614f1b3e8SDimitry Andric case 't':
20714f1b3e8SDimitry Andric format = eFormatBinary;
20814f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
20914f1b3e8SDimitry Andric return true;
21014f1b3e8SDimitry Andric case 'f':
21114f1b3e8SDimitry Andric format = eFormatFloat;
21214f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
21314f1b3e8SDimitry Andric return true;
21414f1b3e8SDimitry Andric case 'a':
21514f1b3e8SDimitry Andric format = eFormatAddressInfo;
216f034231aSEd Maste {
21714f1b3e8SDimitry Andric TargetSP target_sp =
21814f1b3e8SDimitry Andric execution_context ? execution_context->GetTargetSP() : TargetSP();
21914f1b3e8SDimitry Andric if (target_sp)
22014f1b3e8SDimitry Andric byte_size = target_sp->GetArchitecture().GetAddressByteSize();
221f034231aSEd Maste m_prev_gdb_format = format_letter;
222f034231aSEd Maste return true;
223f034231aSEd Maste }
22414f1b3e8SDimitry Andric case 'i':
22514f1b3e8SDimitry Andric format = eFormatInstruction;
22614f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
22714f1b3e8SDimitry Andric return true;
22814f1b3e8SDimitry Andric case 'c':
22914f1b3e8SDimitry Andric format = eFormatChar;
23014f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
23114f1b3e8SDimitry Andric return true;
23214f1b3e8SDimitry Andric case 's':
23314f1b3e8SDimitry Andric format = eFormatCString;
23414f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
23514f1b3e8SDimitry Andric return true;
23614f1b3e8SDimitry Andric case 'T':
23714f1b3e8SDimitry Andric format = eFormatOSType;
23814f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
23914f1b3e8SDimitry Andric return true;
24014f1b3e8SDimitry Andric case 'A':
24114f1b3e8SDimitry Andric format = eFormatHexFloat;
24214f1b3e8SDimitry Andric m_prev_gdb_format = format_letter;
24314f1b3e8SDimitry Andric return true;
24414f1b3e8SDimitry Andric
24574a628f7SDimitry Andric case 'b':
24674a628f7SDimitry Andric case 'h':
24774a628f7SDimitry Andric case 'w':
24874a628f7SDimitry Andric case 'g':
24974a628f7SDimitry Andric {
250f73363f1SDimitry Andric // Size isn't used for printing instructions, so if a size is specified,
251f73363f1SDimitry Andric // and the previous format was 'i', then we should reset it to the
252f73363f1SDimitry Andric // default ('x'). Otherwise we'll continue to print as instructions,
25314f1b3e8SDimitry Andric // which isn't expected.
25474a628f7SDimitry Andric if (format_letter == 'b')
25514f1b3e8SDimitry Andric byte_size = 1;
25674a628f7SDimitry Andric else if (format_letter == 'h')
25714f1b3e8SDimitry Andric byte_size = 2;
25874a628f7SDimitry Andric else if (format_letter == 'w')
25914f1b3e8SDimitry Andric byte_size = 4;
26074a628f7SDimitry Andric else if (format_letter == 'g')
26114f1b3e8SDimitry Andric byte_size = 8;
26214f1b3e8SDimitry Andric
26314f1b3e8SDimitry Andric m_prev_gdb_size = format_letter;
26414f1b3e8SDimitry Andric if (m_prev_gdb_format == 'i')
26514f1b3e8SDimitry Andric m_prev_gdb_format = 'x';
26614f1b3e8SDimitry Andric return true;
26774a628f7SDimitry Andric }
26814f1b3e8SDimitry Andric break;
26914f1b3e8SDimitry Andric default:
27014f1b3e8SDimitry Andric break;
271f034231aSEd Maste }
27274a628f7SDimitry Andric
27374a628f7SDimitry Andric
274f034231aSEd Maste return false;
275f034231aSEd Maste }
276f034231aSEd Maste
OptionParsingStarting(ExecutionContext * execution_context)27714f1b3e8SDimitry Andric void OptionGroupFormat::OptionParsingStarting(
27814f1b3e8SDimitry Andric ExecutionContext *execution_context) {
279f034231aSEd Maste m_format.Clear();
280f034231aSEd Maste m_byte_size.Clear();
281f034231aSEd Maste m_count.Clear();
282f034231aSEd Maste m_has_gdb_format = false;
283f034231aSEd Maste }
284