/* GDB parameters implemented in Python
- Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
+ Copyright (C) 2008-2016 Free Software Foundation, Inc.
This file is part of GDB.
#include "defs.h"
#include "value.h"
-#include "exceptions.h"
#include "python-internal.h"
#include "charset.h"
#include "gdbcmd.h"
#include "cli/cli-decode.h"
#include "completer.h"
+#include "language.h"
+#include "arch-utils.h"
/* Parameter constants and their values. */
struct parm_constant
typedef struct parmpy_object parmpy_object;
-static PyTypeObject parmpy_object_type;
+extern PyTypeObject parmpy_object_type
+ CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
/* Some handy string constants. */
static PyObject *set_doc_cst;
get_attr (PyObject *obj, PyObject *attr_name)
{
if (PyString_Check (attr_name)
+#ifdef IS_PY3K
+ && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
+#else
&& ! strcmp (PyString_AsString (attr_name), "value"))
+#endif
{
parmpy_object *self = (parmpy_object *) obj;
+
return gdbpy_parameter_value (self->type, &self->value);
}
return PyObject_GenericGetAttr (obj, attr_name);
}
-/* Set a parameter value from a Python value. Return 0 on success, -1
- on failure. */
+/* Set a parameter value from a Python value. Return 0 on success. Returns
+ -1 on error, with a python exception set. */
static int
set_parameter_value (parmpy_object *self, PyObject *value)
{
&& (self->type == var_filename
|| value != Py_None))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("String required for filename."));
return -1;
}
- if (self->value.stringval)
- xfree (self->value.stringval);
if (value == Py_None)
{
+ xfree (self->value.stringval);
if (self->type == var_optional_filename)
self->value.stringval = xstrdup ("");
else
self->value.stringval = NULL;
}
else
- self->value.stringval = python_string_to_host_string (value);
+ {
+ gdb::unique_xmalloc_ptr<char>
+ string (python_string_to_host_string (value));
+ if (string == NULL)
+ return -1;
+
+ xfree (self->value.stringval);
+ self->value.stringval = string.release ();
+ }
break;
case var_enum:
{
int i;
- char *str;
if (! gdbpy_is_string (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("ENUM arguments must be a string."));
return -1;
}
- str = python_string_to_host_string (value);
+ gdb::unique_xmalloc_ptr<char>
+ str (python_string_to_host_string (value));
+ if (str == NULL)
+ return -1;
for (i = 0; self->enumeration[i]; ++i)
- if (! strcmp (self->enumeration[i], str))
+ if (! strcmp (self->enumeration[i], str.get ()))
break;
- xfree (str);
if (! self->enumeration[i])
{
PyErr_SetString (PyExc_RuntimeError,
case var_boolean:
if (! PyBool_Check (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("A boolean argument is required."));
return -1;
}
cmp = PyObject_IsTrue (value);
- if (cmp < 0)
+ if (cmp < 0)
return -1;
self->value.intval = cmp;
break;
{
cmp = PyObject_IsTrue (value);
if (cmp < 0 )
- return -1;
+ return -1;
if (cmp == 1)
self->value.autoboolval = AUTO_BOOLEAN_TRUE;
- else
+ else
self->value.autoboolval = AUTO_BOOLEAN_FALSE;
-
- break;
}
+ break;
case var_integer:
case var_zinteger:
if (! PyInt_Check (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The value must be integer."));
return -1;
}
- l = PyInt_AsLong (value);
+ if (! gdb_py_int_as_long (value, &l))
+ return -1;
+
if (self->type == var_uinteger)
{
ok = (l >= 0 && l <= UINT_MAX);
if (! ok)
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("Range exceeded."));
return -1;
}
}
default:
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("Unhandled type in parameter value."));
return -1;
}
return 0;
}
-/* Set an attribute. */
+/* Set an attribute. Returns -1 on error, with a python exception set. */
static int
set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
{
if (PyString_Check (attr_name)
+#ifdef IS_PY3K
+ && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
+#else
&& ! strcmp (PyString_AsString (attr_name), "value"))
+#endif
{
if (!val)
{
return PyObject_GenericSetAttr (obj, attr_name, val);
}
+/* A helper function which returns a documentation string for an
+ object. */
+
+static gdb::unique_xmalloc_ptr<char>
+get_doc_string (PyObject *object, PyObject *attr)
+{
+ gdb::unique_xmalloc_ptr<char> result;
+
+ if (PyObject_HasAttr (object, attr))
+ {
+ PyObject *ds_obj = PyObject_GetAttr (object, attr);
+
+ if (ds_obj && gdbpy_is_string (ds_obj))
+ {
+ result = python_string_to_host_string (ds_obj);
+ if (result == NULL)
+ gdbpy_print_stack ();
+ }
+ Py_XDECREF (ds_obj);
+ }
+ if (! result)
+ result.reset (xstrdup (_("This command is not documented.")));
+ return result;
+}
+
+/* Helper function which will execute a METHOD in OBJ passing the
+ argument ARG. ARG can be NULL. METHOD should return a Python
+ string. If this function returns NULL, there has been an error and
+ the appropriate exception set. */
+static gdb::unique_xmalloc_ptr<char>
+call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
+{
+ gdb::unique_xmalloc_ptr<char> data;
+ PyObject *result = PyObject_CallMethodObjArgs (obj, method, arg, NULL);
+
+ if (! result)
+ return NULL;
+
+ if (gdbpy_is_string (result))
+ {
+ data = python_string_to_host_string (result);
+ Py_DECREF (result);
+ if (! data)
+ return NULL;
+ }
+ else
+ {
+ PyErr_SetString (PyExc_RuntimeError,
+ _("Parameter must return a string value."));
+ Py_DECREF (result);
+ return NULL;
+ }
+
+ return data;
+}
+
+/* A callback function that is registered against the respective
+ add_setshow_* set_doc prototype. This function will either call
+ the Python function "get_set_string" or extract the Python
+ attribute "set_doc" and return the contents as a string. If
+ neither exist, insert a string indicating the Parameter is not
+ documented. */
+static void
+get_set_value (char *args, int from_tty,
+ struct cmd_list_element *c)
+{
+ PyObject *obj = (PyObject *) get_cmd_context (c);
+ gdb::unique_xmalloc_ptr<char> set_doc_string;
+ struct cleanup *cleanup = ensure_python_env (get_current_arch (),
+ current_language);
+ PyObject *set_doc_func = PyString_FromString ("get_set_string");
+
+ if (! set_doc_func)
+ goto error;
+
+ if (PyObject_HasAttr (obj, set_doc_func))
+ {
+ set_doc_string = call_doc_function (obj, set_doc_func, NULL);
+ if (! set_doc_string)
+ goto error;
+ }
+ else
+ {
+ /* We have to preserve the existing < GDB 7.3 API. If a
+ callback function does not exist, then attempt to read the
+ set_doc attribute. */
+ set_doc_string = get_doc_string (obj, set_doc_cst);
+ }
+
+ fprintf_filtered (gdb_stdout, "%s\n", set_doc_string.get ());
+
+ Py_XDECREF (set_doc_func);
+ do_cleanups (cleanup);
+ return;
+
+ error:
+ Py_XDECREF (set_doc_func);
+ gdbpy_print_stack ();
+ do_cleanups (cleanup);
+ return;
+}
+
+/* A callback function that is registered against the respective
+ add_setshow_* show_doc prototype. This function will either call
+ the Python function "get_show_string" or extract the Python
+ attribute "show_doc" and return the contents as a string. If
+ neither exist, insert a string indicating the Parameter is not
+ documented. */
+static void
+get_show_value (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ PyObject *obj = (PyObject *) get_cmd_context (c);
+ gdb::unique_xmalloc_ptr<char> show_doc_string;
+ struct cleanup *cleanup = ensure_python_env (get_current_arch (),
+ current_language);
+ PyObject *show_doc_func = PyString_FromString ("get_show_string");
+
+ if (! show_doc_func)
+ goto error;
+
+ if (PyObject_HasAttr (obj, show_doc_func))
+ {
+ PyObject *val_obj = PyString_FromString (value);
+
+ if (! val_obj)
+ goto error;
+
+ show_doc_string = call_doc_function (obj, show_doc_func, val_obj);
+ Py_DECREF (val_obj);
+ if (! show_doc_string)
+ goto error;
+
+ fprintf_filtered (file, "%s\n", show_doc_string.get ());
+ }
+ else
+ {
+ /* We have to preserve the existing < GDB 7.3 API. If a
+ callback function does not exist, then attempt to read the
+ show_doc attribute. */
+ show_doc_string = get_doc_string (obj, show_doc_cst);
+ fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
+ }
+
+ Py_XDECREF (show_doc_func);
+ do_cleanups (cleanup);
+ return;
+
+ error:
+ Py_XDECREF (show_doc_func);
+ gdbpy_print_stack ();
+ do_cleanups (cleanup);
+ return;
+}
\f
/* A helper function that dispatches to the appropriate add_setshow
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
+ struct cmd_list_element *param = NULL;
+ const char *tmp_name = NULL;
+
switch (parmclass)
{
case var_boolean:
- add_setshow_boolean_cmd (cmd_name, cmdclass, &self->value.intval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+
+ add_setshow_boolean_cmd (cmd_name, cmdclass,
+ &self->value.intval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list);
+
break;
case var_auto_boolean:
add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
&self->value.autoboolval,
set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ get_set_value, get_show_value,
+ set_list, show_list);
break;
case var_uinteger:
- add_setshow_uinteger_cmd (cmd_name, cmdclass, &self->value.uintval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ add_setshow_uinteger_cmd (cmd_name, cmdclass,
+ &self->value.uintval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list);
break;
case var_integer:
- add_setshow_integer_cmd (cmd_name, cmdclass, &self->value.intval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
- break;
+ add_setshow_integer_cmd (cmd_name, cmdclass,
+ &self->value.intval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list); break;
case var_string:
- add_setshow_string_cmd (cmd_name, cmdclass, &self->value.stringval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
- break;
+ add_setshow_string_cmd (cmd_name, cmdclass,
+ &self->value.stringval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list); break;
case var_string_noescape:
add_setshow_string_noescape_cmd (cmd_name, cmdclass,
&self->value.stringval,
set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ get_set_value, get_show_value,
+ set_list, show_list);
+
break;
case var_optional_filename:
add_setshow_optional_filename_cmd (cmd_name, cmdclass,
- &self->value.stringval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ &self->value.stringval, set_doc,
+ show_doc, help_doc, get_set_value,
+ get_show_value, set_list,
+ show_list);
break;
case var_filename:
- add_setshow_filename_cmd (cmd_name, cmdclass, &self->value.stringval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
- break;
+ add_setshow_filename_cmd (cmd_name, cmdclass,
+ &self->value.stringval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list); break;
case var_zinteger:
- add_setshow_zinteger_cmd (cmd_name, cmdclass, &self->value.intval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ add_setshow_zinteger_cmd (cmd_name, cmdclass,
+ &self->value.intval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list);
break;
case var_enum:
add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
- &self->value.cstringval,
- set_doc, show_doc, help_doc,
- NULL, NULL, set_list, show_list);
+ &self->value.cstringval, set_doc, show_doc,
+ help_doc, get_set_value, get_show_value,
+ set_list, show_list);
/* Initialize the value, just in case. */
self->value.cstringval = self->enumeration[0];
break;
}
+
+ /* Lookup created parameter, and register Python object against the
+ parameter context. Perform this task against both lists. */
+ tmp_name = cmd_name;
+ param = lookup_cmd (&tmp_name, *show_list, "", 0, 1);
+ if (param)
+ set_cmd_context (param, self);
+
+ tmp_name = cmd_name;
+ param = lookup_cmd (&tmp_name, *set_list, "", 0, 1);
+ if (param)
+ set_cmd_context (param, self);
}
-/* A helper which computes enum values. Returns 1 on success, 0 on
- error. */
+/* A helper which computes enum values. Returns 1 on success. Returns 0 on
+ error, with a python exception set. */
static int
compute_enum_values (parmpy_object *self, PyObject *enum_values)
{
Py_ssize_t size, i;
+ struct cleanup *back_to;
if (! enum_values)
{
if (! PySequence_Check (enum_values))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration is not a sequence."));
return 0;
}
return 0;
if (size == 0)
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration is empty."));
return 0;
}
- self->enumeration = xmalloc ((size + 1) * sizeof (char *));
- memset (self->enumeration, 0, (size + 1) * sizeof (char *));
+ self->enumeration = XCNEWVEC (const char *, size + 1);
+ back_to = make_cleanup (free_current_contents, &self->enumeration);
for (i = 0; i < size; ++i)
{
PyObject *item = PySequence_GetItem (enum_values, i);
+
if (! item)
- return 0;
+ {
+ do_cleanups (back_to);
+ return 0;
+ }
if (! gdbpy_is_string (item))
{
- PyErr_SetString (PyExc_RuntimeError,
+ Py_DECREF (item);
+ do_cleanups (back_to);
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration item not a string."));
return 0;
}
- self->enumeration[i] = python_string_to_host_string (item);
+ self->enumeration[i] = python_string_to_host_string (item).release ();
+ Py_DECREF (item);
+ if (self->enumeration[i] == NULL)
+ {
+ do_cleanups (back_to);
+ return 0;
+ }
+ make_cleanup (xfree, (char *) self->enumeration[i]);
}
+ discard_cleanups (back_to);
return 1;
}
-/* A helper function which returns a documentation string for an
- object. */
-static char *
-get_doc_string (PyObject *object, PyObject *attr)
-{
- char *result = NULL;
- if (PyObject_HasAttr (object, attr))
- {
- PyObject *ds_obj = PyObject_GetAttr (object, attr);
- if (ds_obj && gdbpy_is_string (ds_obj))
- result = python_string_to_host_string (ds_obj);
- }
- if (! result)
- result = xstrdup (_("This command is not documented."));
- return result;
-}
-
/* Object initializer; sets up gdb-side structures for command.
Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
The documentation for the parameter is taken from the doc string
for the python class.
-
-*/
+
+ Returns -1 on error, with a python exception set. */
+
static int
parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
{
parmpy_object *obj = (parmpy_object *) self;
- char *name;
+ const char *name;
char *set_doc, *show_doc, *doc;
char *cmd_name;
int parmclass, cmdtype;
PyObject *enum_values = NULL;
- struct cmd_list_element *cmd_list;
struct cmd_list_element **set_list, **show_list;
- volatile struct gdb_exception except;
if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
&enum_values))
&& parmclass != var_optional_filename && parmclass != var_filename
&& parmclass != var_zinteger && parmclass != var_enum)
{
- PyErr_SetString (PyExc_RuntimeError, _("Invalid parameter class argument."));
+ PyErr_SetString (PyExc_RuntimeError,
+ _("Invalid parameter class argument."));
return -1;
}
if (! cmd_name)
return -1;
- set_doc = get_doc_string (self, set_doc_cst);
- show_doc = get_doc_string (self, show_doc_cst);
- doc = get_doc_string (self, gdbpy_doc_cst);
+ set_doc = get_doc_string (self, set_doc_cst).release ();
+ show_doc = get_doc_string (self, show_doc_cst).release ();
+ doc = get_doc_string (self, gdbpy_doc_cst).release ();
Py_INCREF (self);
- TRY_CATCH (except, RETURN_MASK_ALL)
+ TRY
{
add_setshow_generic (parmclass, (enum command_class) cmdtype,
cmd_name, obj,
set_doc, show_doc,
doc, set_list, show_list);
}
- if (except.reason < 0)
+ CATCH (except, RETURN_MASK_ALL)
{
xfree (cmd_name);
xfree (set_doc);
"%s", except.message);
return -1;
}
+ END_CATCH
+
return 0;
}
\f
/* Initialize the 'parameters' module. */
-void
+int
gdbpy_initialize_parameters (void)
{
int i;
+ parmpy_object_type.tp_new = PyType_GenericNew;
if (PyType_Ready (&parmpy_object_type) < 0)
- return;
+ return -1;
set_doc_cst = PyString_FromString ("set_doc");
if (! set_doc_cst)
- return;
+ return -1;
show_doc_cst = PyString_FromString ("show_doc");
if (! show_doc_cst)
- return;
+ return -1;
for (i = 0; parm_constants[i].name; ++i)
{
if (PyModule_AddIntConstant (gdb_module,
parm_constants[i].name,
parm_constants[i].value) < 0)
- return;
+ return -1;
}
- Py_INCREF (&parmpy_object_type);
- PyModule_AddObject (gdb_module, "Parameter",
- (PyObject *) &parmpy_object_type);
+ return gdb_pymodule_addobject (gdb_module, "Parameter",
+ (PyObject *) &parmpy_object_type);
}
\f
-static PyTypeObject parmpy_object_type =
+PyTypeObject parmpy_object_type =
{
- PyObject_HEAD_INIT (NULL)
- 0, /*ob_size*/
+ PyVarObject_HEAD_INIT (NULL, 0)
"gdb.Parameter", /*tp_name*/
sizeof (parmpy_object), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /* tp_dictoffset */
parmpy_init, /* tp_init */
0, /* tp_alloc */
- PyType_GenericNew /* tp_new */
};