gdb: Introduce setting construct within cmd_list_element

cmd_list_element can contain a pointer to data that can be set and / or
shown.  This is achieved with the void* VAR member which points to the
data that can be accessed, while the VAR_TYPE member (of type enum
var_types) indicates how to interpret the data pointed to.

With this pattern, the user of the cmd_list_element needs to know what
is the storage type associated with a given VAR_TYPES in order to do
the proper casting.  No automatic safeguard is available to prevent
miss-use of the pointer.  Client code typically looks something like:

	switch (c->var_type)
	{
	  case var_zuinteger:
	    unsigned int v = *(unsigned int*) c->var;
	    ...
	    break;
	  case var_boolean:
	    bool v = *(bool *) c->var;
	    ...
	    break;
	  ...
	}

This patch proposes to add an abstraction around the var_types and void*
pointer pair.  The abstraction is meant to prevent the user from having
to handle the cast and verify that the data is read or written as a type
that is coherent with the setting's var_type.  This is achieved by
introducing the struct setting which exposes a set of templated get /
set member functions.  The template parameter is the type of the
variable that holds the referred variable.

Using those accessors allows runtime checks to be inserted in order to
ensure that the data pointed to has the expected type.  For example,
instantiating the member functions with bool will yield something
similar to:

	const bool &get<bool> () const
	{
	  gdb_assert (m_var_type == var_boolean);
	  gdb_assert (m_var != nullptr);
	  return *static_cast<bool *> (m_var);
	}
	void set<bool> (const bool &var)
	{
	  gdb_assert (m_var_type == var_boolean);
	  gdb_assert (m_var != nullptr);
	  *static_cast<bool *> (m_var) = var;
	}

Using the new abstraction, our initial example becomes:

	switch (c->var_type)
	{
	  case var_zuinteger:
	    unsigned int v = c->var->get<unsigned int> ();
	    ...
	    break;
	  case var_boolean:
	    bool v = c->var->get<bool> ();
	    ...
	    break;
	  ...
	}

While the call site is still similar, the introduction of runtime checks
help ensure correct usage of the data.

In order to avoid turning the bulk of add_setshow_cmd_full into a
templated function, and following a suggestion from Pedro Alves, a
setting can be constructed from a pre validated type erased reference to
a variable.  This is what setting::erased_args is used for.

Introducing an opaque abstraction to describe a setting will also make
it possible to use callbacks to retrieve or set the value of the setting
on the fly instead of pointing to a static chunk of memory.  This will
be done added in a later commit.

Given that a cmd_list_element may or may not reference a setting, the
VAR and VAR_TYPES members of the struct are replaced with a
gdb::optional<setting> named VAR.

Few internal function signatures have been modified to take into account
this new abstraction:

-The functions value_from_setting, str_value_from_setting and
 get_setshow_command_value_string used to have a 'cmd_list_element *'
 parameter but only used it for the VAR and VAR_TYPE member. They now
 take a 'const setting &' parameter instead.
- Similarly, the 'void *' and a 'enum var_types' parameters of
  pascm_param_value and gdbpy_parameter_value have been replaced with a
  'const setting &' parameter.

No user visible change is expected after this patch.

Tested on GNU/Linux x86_64, with no regression noticed.

Co-authored-by: Simon Marchi <simon.marchi@polymtl.ca>
Change-Id: Ie1d08c3ceb8b30b3d7bf1efe036eb8acffcd2f34
This commit is contained in:
Lancelot SIX 2021-09-13 22:32:19 +01:00
parent 39d53d0435
commit 1d7fe7f01b
14 changed files with 547 additions and 257 deletions

View file

@ -236,7 +236,7 @@ with_command_1 (const char *set_cmd_prefix,
/*ignore_help_classes=*/ 1);
gdb_assert (set_cmd != nullptr);
if (set_cmd->var == nullptr)
if (!set_cmd->var.has_value ())
error (_("Cannot use this setting with the \"with\" command"));
std::string temp_value
@ -245,7 +245,8 @@ with_command_1 (const char *set_cmd_prefix,
if (nested_cmd == nullptr)
nested_cmd = skip_spaces (delim + 2);
std::string org_value = get_setshow_command_value_string (set_cmd);
gdb_assert (set_cmd->var.has_value ());
std::string org_value = get_setshow_command_value_string (*set_cmd->var);
/* Tweak the setting to the new temporary value. */
do_set_command (temp_value.c_str (), from_tty, set_cmd);
@ -2092,31 +2093,31 @@ setting_cmd (const char *fnname, struct cmd_list_element *showlist,
/* Builds a value from the show CMD. */
static struct value *
value_from_setting (const cmd_list_element *cmd, struct gdbarch *gdbarch)
value_from_setting (const setting &var, struct gdbarch *gdbarch)
{
switch (cmd->var_type)
switch (var.type ())
{
case var_integer:
if (*(int *) cmd->var == INT_MAX)
if (var.get<int> () == INT_MAX)
return value_from_longest (builtin_type (gdbarch)->builtin_int,
0);
else
return value_from_longest (builtin_type (gdbarch)->builtin_int,
*(int *) cmd->var);
var.get<int> ());
case var_zinteger:
return value_from_longest (builtin_type (gdbarch)->builtin_int,
*(int *) cmd->var);
var.get<int> ());
case var_boolean:
return value_from_longest (builtin_type (gdbarch)->builtin_int,
*(bool *) cmd->var ? 1 : 0);
var.get<bool> () ? 1 : 0);
case var_zuinteger_unlimited:
return value_from_longest (builtin_type (gdbarch)->builtin_int,
*(int *) cmd->var);
var.get<int> ());
case var_auto_boolean:
{
int val;
switch (*(enum auto_boolean*) cmd->var)
switch (var.get<enum auto_boolean> ())
{
case AUTO_BOOLEAN_TRUE:
val = 1;
@ -2134,27 +2135,35 @@ value_from_setting (const cmd_list_element *cmd, struct gdbarch *gdbarch)
val);
}
case var_uinteger:
if (*(unsigned int *) cmd->var == UINT_MAX)
if (var.get<unsigned int> () == UINT_MAX)
return value_from_ulongest
(builtin_type (gdbarch)->builtin_unsigned_int, 0);
else
return value_from_ulongest
(builtin_type (gdbarch)->builtin_unsigned_int,
*(unsigned int *) cmd->var);
var.get<unsigned int> ());
case var_zuinteger:
return value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
*(unsigned int *) cmd->var);
var.get<unsigned int> ());
case var_string:
case var_string_noescape:
case var_optional_filename:
case var_filename:
case var_enum:
if (*(char **) cmd->var)
return value_cstring (*(char **) cmd->var, strlen (*(char **) cmd->var),
builtin_type (gdbarch)->builtin_char);
else
return value_cstring ("", 1,
builtin_type (gdbarch)->builtin_char);
{
const char *value;
if (var.type () == var_enum)
value = var.get<const char *> ();
else
value = var.get<char *> ();
if (value != nullptr)
return value_cstring (value, strlen (value),
builtin_type (gdbarch)->builtin_char);
else
return value_cstring ("", 1,
builtin_type (gdbarch)->builtin_char);
}
default:
gdb_assert_not_reached ("bad var_type");
}
@ -2167,9 +2176,12 @@ gdb_setting_internal_fn (struct gdbarch *gdbarch,
const struct language_defn *language,
void *cookie, int argc, struct value **argv)
{
return value_from_setting (setting_cmd ("$_gdb_setting", showlist,
argc, argv),
gdbarch);
cmd_list_element *show_cmd
= setting_cmd ("$_gdb_setting", showlist, argc, argv);
gdb_assert (show_cmd->var.has_value ());
return value_from_setting (*show_cmd->var, gdbarch);
}
/* Implementation of the convenience function $_gdb_maint_setting. */
@ -2179,18 +2191,20 @@ gdb_maint_setting_internal_fn (struct gdbarch *gdbarch,
const struct language_defn *language,
void *cookie, int argc, struct value **argv)
{
return value_from_setting (setting_cmd ("$_gdb_maint_setting",
maintenance_show_cmdlist,
argc, argv),
gdbarch);
cmd_list_element *show_cmd
= setting_cmd ("$_gdb_maint_setting", maintenance_show_cmdlist, argc, argv);
gdb_assert (show_cmd->var.has_value ());
return value_from_setting (*show_cmd->var, gdbarch);
}
/* Builds a string value from the show CMD. */
static struct value *
str_value_from_setting (const cmd_list_element *cmd, struct gdbarch *gdbarch)
str_value_from_setting (const setting &var, struct gdbarch *gdbarch)
{
switch (cmd->var_type)
switch (var.type ())
{
case var_integer:
case var_zinteger:
@ -2200,7 +2214,7 @@ str_value_from_setting (const cmd_list_element *cmd, struct gdbarch *gdbarch)
case var_uinteger:
case var_zuinteger:
{
std::string cmd_val = get_setshow_command_value_string (cmd);
std::string cmd_val = get_setshow_command_value_string (var);
return value_cstring (cmd_val.c_str (), cmd_val.size (),
builtin_type (gdbarch)->builtin_char);
@ -2213,15 +2227,22 @@ str_value_from_setting (const cmd_list_element *cmd, struct gdbarch *gdbarch)
case var_enum:
/* For these cases, we do not use get_setshow_command_value_string,
as this function handle some characters specially, e.g. by
escaping quotes. So, we directly use the cmd->var string value,
similarly to the value_from_setting code for these cases. */
if (*(char **) cmd->var)
return value_cstring (*(char **) cmd->var, strlen (*(char **) cmd->var),
builtin_type (gdbarch)->builtin_char);
else
return value_cstring ("", 1,
builtin_type (gdbarch)->builtin_char);
escaping quotevar. So, we directly use the var string value,
similarly to the value_from_setting code for these casevar. */
{
const char *value;
if (var.type () == var_enum)
value = var.get<const char *> ();
else
value = var.get<char *> ();
if (value != nullptr)
return value_cstring (value, strlen (value),
builtin_type (gdbarch)->builtin_char);
else
return value_cstring ("", 1,
builtin_type (gdbarch)->builtin_char);
}
default:
gdb_assert_not_reached ("bad var_type");
}
@ -2234,9 +2255,12 @@ gdb_setting_str_internal_fn (struct gdbarch *gdbarch,
const struct language_defn *language,
void *cookie, int argc, struct value **argv)
{
return str_value_from_setting (setting_cmd ("$_gdb_setting_str",
showlist, argc, argv),
gdbarch);
cmd_list_element *show_cmd
= setting_cmd ("$_gdb_setting_str", showlist, argc, argv);
gdb_assert (show_cmd->var.has_value ());
return str_value_from_setting (*show_cmd->var, gdbarch);
}
@ -2247,10 +2271,13 @@ gdb_maint_setting_str_internal_fn (struct gdbarch *gdbarch,
const struct language_defn *language,
void *cookie, int argc, struct value **argv)
{
return str_value_from_setting (setting_cmd ("$_gdb_maint_setting_str",
maintenance_show_cmdlist,
argc, argv),
gdbarch);
cmd_list_element *show_cmd
= setting_cmd ("$_gdb_maint_setting_str", maintenance_show_cmdlist, argc,
argv);
gdb_assert (show_cmd->var.has_value ());
return str_value_from_setting (*show_cmd->var, gdbarch);
}
void _initialize_cli_cmds ();