aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
authorLancelot SIX <lsix@lancelotsix.com>2021-09-13 22:32:19 +0100
committerLancelot SIX <lsix@lancelotsix.com>2021-10-03 17:53:16 +0100
commit1d7fe7f01b93ecaeb3e481ed09d3deac7890a97f (patch)
tree18849feb1ba77bfe2be1cce3e95c75c981cec765 /gdb/python
parent39d53d04357606a15efd400147fa7369d71baf2c (diff)
downloadfsf-binutils-gdb-1d7fe7f01b93ecaeb3e481ed09d3deac7890a97f.zip
fsf-binutils-gdb-1d7fe7f01b93ecaeb3e481ed09d3deac7890a97f.tar.gz
fsf-binutils-gdb-1d7fe7f01b93ecaeb3e481ed09d3deac7890a97f.tar.bz2
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
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-param.c26
-rw-r--r--gdb/python/python-internal.h2
-rw-r--r--gdb/python/python.c29
3 files changed, 43 insertions, 14 deletions
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index f9dcb07..1dd716b 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -88,6 +88,30 @@ struct parmpy_object
const char **enumeration;
};
+/* Wraps a setting around an existing parmpy_object. This abstraction
+ is used to manipulate the value in S->VALUE in a type safe manner using
+ the setting interface. */
+
+static setting
+make_setting (parmpy_object *s)
+{
+
+ if (var_type_uses<bool> (s->type))
+ return setting (s->type, &s->value.boolval);
+ else if (var_type_uses<int> (s->type))
+ return setting (s->type, &s->value.intval);
+ else if (var_type_uses<auto_boolean> (s->type))
+ return setting (s->type, &s->value.autoboolval);
+ else if (var_type_uses<unsigned int> (s->type))
+ return setting (s->type, &s->value.uintval);
+ else if (var_type_uses<char *> (s->type))
+ return setting (s->type, &s->value.stringval);
+ else if (var_type_uses<const char *> (s->type))
+ return setting (s->type, &s->value.cstringval);
+ else
+ gdb_assert_not_reached ("unhandled var type");
+}
+
extern PyTypeObject parmpy_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
@@ -110,7 +134,7 @@ get_attr (PyObject *obj, PyObject *attr_name)
{
parmpy_object *self = (parmpy_object *) obj;
- return gdbpy_parameter_value (self->type, &self->value);
+ return gdbpy_parameter_value (make_setting (self));
}
return PyObject_GenericGetAttr (obj, attr_name);
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 368681b..022d4a6 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -438,7 +438,7 @@ PyObject *gdbpy_create_ptid_object (ptid_t ptid);
PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args);
PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args);
PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args);
-PyObject *gdbpy_parameter_value (enum var_types type, void *var);
+PyObject *gdbpy_parameter_value (const setting &var);
gdb::unique_xmalloc_ptr<char> gdbpy_parse_command_name
(const char *name, struct cmd_list_element ***base_list,
struct cmd_list_element **start_list);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 37eacef..a26c373 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -449,9 +449,9 @@ python_command (const char *arg, int from_tty)
NULL (and set a Python exception) on error. Helper function for
get_parameter. */
PyObject *
-gdbpy_parameter_value (enum var_types type, void *var)
+gdbpy_parameter_value (const setting &var)
{
- switch (type)
+ switch (var.type ())
{
case var_string:
case var_string_noescape:
@@ -459,16 +459,20 @@ gdbpy_parameter_value (enum var_types type, void *var)
case var_filename:
case var_enum:
{
- const char *str = *(char **) var;
+ const char *str;
+ if (var.type () == var_enum)
+ str = var.get<const char *> ();
+ else
+ str = var.get<char *> ();
- if (! str)
+ if (str == nullptr)
str = "";
return host_string_to_python_string (str).release ();
}
case var_boolean:
{
- if (* (bool *) var)
+ if (var.get<bool> ())
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
@@ -476,7 +480,7 @@ gdbpy_parameter_value (enum var_types type, void *var)
case var_auto_boolean:
{
- enum auto_boolean ab = * (enum auto_boolean *) var;
+ enum auto_boolean ab = var.get<enum auto_boolean> ();
if (ab == AUTO_BOOLEAN_TRUE)
Py_RETURN_TRUE;
@@ -487,16 +491,16 @@ gdbpy_parameter_value (enum var_types type, void *var)
}
case var_integer:
- if ((* (int *) var) == INT_MAX)
+ if (var.get<int> () == INT_MAX)
Py_RETURN_NONE;
/* Fall through. */
case var_zinteger:
case var_zuinteger_unlimited:
- return gdb_py_object_from_longest (* (int *) var).release ();
+ return gdb_py_object_from_longest (var.get<int> ()).release ();
case var_uinteger:
{
- unsigned int val = * (unsigned int *) var;
+ unsigned int val = var.get<unsigned int> ();
if (val == UINT_MAX)
Py_RETURN_NONE;
@@ -505,7 +509,7 @@ gdbpy_parameter_value (enum var_types type, void *var)
case var_zuinteger:
{
- unsigned int val = * (unsigned int *) var;
+ unsigned int val = var.get<unsigned int> ();
return gdb_py_object_from_ulongest (val).release ();
}
}
@@ -542,10 +546,11 @@ gdbpy_parameter (PyObject *self, PyObject *args)
return PyErr_Format (PyExc_RuntimeError,
_("Could not find parameter `%s'."), arg);
- if (! cmd->var)
+ if (!cmd->var.has_value ())
return PyErr_Format (PyExc_RuntimeError,
_("`%s' is not a parameter."), arg);
- return gdbpy_parameter_value (cmd->var_type, cmd->var);
+
+ return gdbpy_parameter_value (*cmd->var);
}
/* Wrapper for target_charset. */