2011-04-28 06:04:39 +08:00
|
|
|
//===-- OptionGroupFormat.cpp -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-04-28 06:04:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-05-14 04:21:08 +08:00
|
|
|
#include "lldb/Interpreter/OptionGroupFormat.h"
|
2011-04-28 06:04:39 +08:00
|
|
|
|
2017-03-23 07:33:16 +08:00
|
|
|
#include "lldb/Host/OptionParser.h"
|
2012-12-15 09:44:51 +08:00
|
|
|
#include "lldb/Interpreter/CommandInterpreter.h"
|
|
|
|
#include "lldb/Target/ExecutionContext.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
2011-04-28 06:04:39 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
OptionGroupFormat::OptionGroupFormat(lldb::Format default_format,
|
|
|
|
uint64_t default_byte_size,
|
|
|
|
uint64_t default_count)
|
|
|
|
: m_format(default_format, default_format),
|
|
|
|
m_byte_size(default_byte_size, default_byte_size),
|
|
|
|
m_count(default_count, default_count), m_prev_gdb_format('x'),
|
|
|
|
m_prev_gdb_size('w') {}
|
2011-04-28 06:04:39 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
OptionGroupFormat::~OptionGroupFormat() {}
|
2011-04-28 06:04:39 +08:00
|
|
|
|
2018-09-27 02:50:19 +08:00
|
|
|
static constexpr OptionDefinition g_option_table[] = {
|
2016-09-07 04:57:50 +08:00
|
|
|
{LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument,
|
2018-09-27 02:50:19 +08:00
|
|
|
nullptr, {}, 0, eArgTypeFormat,
|
2016-09-07 04:57:50 +08:00
|
|
|
"Specify a format to be used for display."},
|
|
|
|
{LLDB_OPT_SET_2, false, "gdb-format", 'G', OptionParser::eRequiredArgument,
|
2018-09-27 02:50:19 +08:00
|
|
|
nullptr, {}, 0, eArgTypeGDBFormat,
|
2016-09-07 04:57:50 +08:00
|
|
|
"Specify a format using a GDB format specifier string."},
|
|
|
|
{LLDB_OPT_SET_3, false, "size", 's', OptionParser::eRequiredArgument,
|
2018-09-27 02:50:19 +08:00
|
|
|
nullptr, {}, 0, eArgTypeByteSize,
|
2016-09-07 04:57:50 +08:00
|
|
|
"The size in bytes to use when displaying with the selected format."},
|
|
|
|
{LLDB_OPT_SET_4, false, "count", 'c', OptionParser::eRequiredArgument,
|
2018-09-27 02:50:19 +08:00
|
|
|
nullptr, {}, 0, eArgTypeCount,
|
2016-09-07 04:57:50 +08:00
|
|
|
"The number of total items to display."},
|
2011-04-28 06:04:39 +08:00
|
|
|
};
|
|
|
|
|
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
|
|
|
llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() {
|
2016-09-23 05:06:13 +08:00
|
|
|
auto result = llvm::makeArrayRef(g_option_table);
|
2016-09-07 04:57:50 +08:00
|
|
|
if (m_byte_size.GetDefaultValue() < UINT64_MAX) {
|
|
|
|
if (m_count.GetDefaultValue() < UINT64_MAX)
|
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
|
|
|
return result;
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
|
|
|
return result.take_front(3);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
Convert option tables to ArrayRefs.
This change is very mechanical. All it does is change the
signature of `Options::GetDefinitions()` and `OptionGroup::
GetDefinitions()` to return an `ArrayRef<OptionDefinition>`
instead of a `const OptionDefinition *`. In the case of the
former, it deletes the sentinel entry from every table, and
in the case of the latter, it removes the `GetNumDefinitions()`
method from the interface. These are no longer necessary as
`ArrayRef` carries its own length.
In the former case, iteration was done by using a sentinel
entry, so there was no knowledge of length. Because of this
the individual option tables were allowed to be defined below
the corresponding class (after all, only a pointer was needed).
Now, however, the length must be known at compile time to
construct the `ArrayRef`, and as a result it is necessary to
move every option table before its corresponding class. This
results in this CL looking very big, but in terms of substance
there is not much here.
Differential revision: https://reviews.llvm.org/D24834
llvm-svn: 282188
2016-09-23 04:22:55 +08:00
|
|
|
return result.take_front(2);
|
2011-04-28 06:04:39 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status OptionGroupFormat::SetOptionValue(uint32_t option_idx,
|
|
|
|
llvm::StringRef option_arg,
|
|
|
|
ExecutionContext *execution_context) {
|
|
|
|
Status error;
|
2016-09-07 04:57:50 +08:00
|
|
|
const int short_option = g_option_table[option_idx].short_option;
|
2011-04-28 06:04:39 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
switch (short_option) {
|
|
|
|
case 'f':
|
|
|
|
error = m_format.SetValueFromString(option_arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
if (m_count.GetDefaultValue() == 0) {
|
|
|
|
error.SetErrorString("--count option is disabled");
|
|
|
|
} else {
|
|
|
|
error = m_count.SetValueFromString(option_arg);
|
|
|
|
if (m_count.GetCurrentValue() == 0)
|
|
|
|
error.SetErrorStringWithFormat("invalid --count option value '%s'",
|
2016-09-24 01:48:13 +08:00
|
|
|
option_arg.str().c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (m_byte_size.GetDefaultValue() == 0) {
|
|
|
|
error.SetErrorString("--size option is disabled");
|
|
|
|
} else {
|
|
|
|
error = m_byte_size.SetValueFromString(option_arg);
|
|
|
|
if (m_byte_size.GetCurrentValue() == 0)
|
|
|
|
error.SetErrorStringWithFormat("invalid --size option value '%s'",
|
2016-09-24 01:48:13 +08:00
|
|
|
option_arg.str().c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G': {
|
|
|
|
uint64_t count = 0;
|
2016-09-24 01:48:13 +08:00
|
|
|
llvm::StringRef gdb_format_str = option_arg;
|
|
|
|
gdb_format_str.consumeInteger(0, count);
|
2011-10-25 14:44:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Format format = eFormatDefault;
|
|
|
|
uint32_t byte_size = 0;
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2016-09-24 01:48:13 +08:00
|
|
|
while (!gdb_format_str.empty() &&
|
|
|
|
ParserGDBFormatLetter(execution_context, gdb_format_str[0], format,
|
2016-09-07 04:57:50 +08:00
|
|
|
byte_size)) {
|
2016-09-24 01:48:13 +08:00
|
|
|
gdb_format_str = gdb_format_str.drop_front();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2016-09-24 01:48:13 +08:00
|
|
|
// We the first character of the "gdb_format_str" is not the
|
2016-09-07 04:57:50 +08:00
|
|
|
// NULL terminator, we didn't consume the entire string and
|
2018-05-01 00:49:04 +08:00
|
|
|
// something is wrong. Also, if none of the format, size or count was
|
|
|
|
// specified correctly, then abort.
|
2016-09-24 01:48:13 +08:00
|
|
|
if (!gdb_format_str.empty() ||
|
2016-09-07 04:57:50 +08:00
|
|
|
(format == eFormatInvalid && byte_size == 0 && count == 0)) {
|
|
|
|
// Nothing got set correctly
|
|
|
|
error.SetErrorStringWithFormat("invalid gdb format string '%s'",
|
2016-09-24 01:48:13 +08:00
|
|
|
option_arg.str().c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
return error;
|
|
|
|
}
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// At least one of the format, size or count was set correctly. Anything
|
|
|
|
// that wasn't set correctly should be set to the previous default
|
2016-09-07 04:57:50 +08:00
|
|
|
if (format == eFormatInvalid)
|
|
|
|
ParserGDBFormatLetter(execution_context, m_prev_gdb_format, format,
|
|
|
|
byte_size);
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
|
|
|
|
const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
|
|
|
|
if (byte_size_enabled) {
|
|
|
|
// Byte size is enabled
|
|
|
|
if (byte_size == 0)
|
|
|
|
ParserGDBFormatLetter(execution_context, m_prev_gdb_size, format,
|
|
|
|
byte_size);
|
|
|
|
} else {
|
2018-05-01 00:49:04 +08:00
|
|
|
// Byte size is disabled, make sure it wasn't specified but if this is an
|
|
|
|
// address, it's actually necessary to specify one so don't error out
|
2016-09-07 04:57:50 +08:00
|
|
|
if (byte_size > 0 && format != lldb::eFormatAddressInfo) {
|
|
|
|
error.SetErrorString(
|
|
|
|
"this command doesn't support specifying a byte size");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (count_enabled) {
|
|
|
|
// Count is enabled and was not set, set it to the default for gdb format
|
|
|
|
// statements (which is 1).
|
|
|
|
if (count == 0)
|
|
|
|
count = 1;
|
|
|
|
} else {
|
|
|
|
// Count is disabled, make sure it wasn't specified
|
|
|
|
if (count > 0) {
|
|
|
|
error.SetErrorString("this command doesn't support specifying a count");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 08:56:27 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_format.SetCurrentValue(format);
|
|
|
|
m_format.SetOptionWasSet();
|
|
|
|
if (byte_size_enabled) {
|
|
|
|
m_byte_size.SetCurrentValue(byte_size);
|
|
|
|
m_byte_size.SetOptionWasSet();
|
2011-04-28 06:04:39 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
if (count_enabled) {
|
|
|
|
m_count.SetCurrentValue(count);
|
|
|
|
m_count.SetOptionWasSet();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
|
|
|
|
break;
|
|
|
|
}
|
2011-04-28 06:04:39 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return error;
|
2011-04-28 06:04:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool OptionGroupFormat::ParserGDBFormatLetter(
|
|
|
|
ExecutionContext *execution_context, char format_letter, Format &format,
|
|
|
|
uint32_t &byte_size) {
|
|
|
|
m_has_gdb_format = true;
|
|
|
|
switch (format_letter) {
|
|
|
|
case 'o':
|
|
|
|
format = eFormatOctal;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'x':
|
|
|
|
format = eFormatHex;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'd':
|
|
|
|
format = eFormatDecimal;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'u':
|
|
|
|
format = eFormatUnsigned;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 't':
|
|
|
|
format = eFormatBinary;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'f':
|
|
|
|
format = eFormatFloat;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'a':
|
|
|
|
format = eFormatAddressInfo;
|
2011-10-26 08:56:27 +08:00
|
|
|
{
|
2016-09-07 04:57:50 +08:00
|
|
|
TargetSP target_sp =
|
|
|
|
execution_context ? execution_context->GetTargetSP() : TargetSP();
|
|
|
|
if (target_sp)
|
|
|
|
byte_size = target_sp->GetArchitecture().GetAddressByteSize();
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
2011-10-26 08:56:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
case 'i':
|
|
|
|
format = eFormatInstruction;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'c':
|
|
|
|
format = eFormatChar;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 's':
|
|
|
|
format = eFormatCString;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'T':
|
|
|
|
format = eFormatOSType;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
case 'A':
|
|
|
|
format = eFormatHexFloat;
|
|
|
|
m_prev_gdb_format = format_letter;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
case 'h':
|
|
|
|
case 'w':
|
|
|
|
case 'g':
|
2017-01-25 09:41:48 +08:00
|
|
|
{
|
2018-05-01 00:49:04 +08:00
|
|
|
// Size isn't used for printing instructions, so if a size is specified,
|
|
|
|
// and the previous format was 'i', then we should reset it to the
|
|
|
|
// default ('x'). Otherwise we'll continue to print as instructions,
|
2017-01-25 09:41:48 +08:00
|
|
|
// which isn't expected.
|
|
|
|
if (format_letter == 'b')
|
|
|
|
byte_size = 1;
|
|
|
|
else if (format_letter == 'h')
|
|
|
|
byte_size = 2;
|
|
|
|
else if (format_letter == 'w')
|
|
|
|
byte_size = 4;
|
|
|
|
else if (format_letter == 'g')
|
|
|
|
byte_size = 8;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-01-25 09:41:48 +08:00
|
|
|
m_prev_gdb_size = format_letter;
|
|
|
|
if (m_prev_gdb_format == 'i')
|
|
|
|
m_prev_gdb_format = 'x';
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-25 09:41:48 +08:00
|
|
|
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return false;
|
2011-10-26 08:56:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void OptionGroupFormat::OptionParsingStarting(
|
|
|
|
ExecutionContext *execution_context) {
|
|
|
|
m_format.Clear();
|
|
|
|
m_byte_size.Clear();
|
|
|
|
m_count.Clear();
|
|
|
|
m_has_gdb_format = false;
|
2011-04-28 06:04:39 +08:00
|
|
|
}
|