1 /* GDB parameters implemented in Python
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "exceptions.h"
24 #include "python-internal.h"
27 #include "cli/cli-decode.h"
28 #include "completer.h"
30 /* Parameter constants and their values. */
37 struct parm_constant parm_constants[] =
39 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
40 { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
41 { "PARAM_UINTEGER", var_uinteger },
42 { "PARAM_INTEGER", var_integer },
43 { "PARAM_STRING", var_string },
44 { "PARAM_STRING_NOESCAPE", var_string_noescape },
45 { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
46 { "PARAM_FILENAME", var_filename },
47 { "PARAM_ZINTEGER", var_zinteger },
48 { "PARAM_ENUM", var_enum },
52 /* A union that can hold anything described by enum var_types. */
55 /* Hold an integer value, for boolean and integer types. */
58 /* Hold an auto_boolean. */
59 enum auto_boolean autoboolval;
61 /* Hold an unsigned integer value, for uinteger. */
64 /* Hold a string, for the various string types. */
67 /* Hold a string, for enums. */
68 const char *cstringval;
71 /* A GDB parameter. */
76 /* The type of the parameter. */
79 /* The value of the parameter. */
80 union parmpy_variable value;
82 /* For an enum command, the possible values. The vector is
83 allocated with xmalloc, as is each element. It is
85 const char **enumeration;
88 typedef struct parmpy_object parmpy_object;
90 static PyTypeObject parmpy_object_type;
92 /* Some handy string constants. */
93 static PyObject *set_doc_cst;
94 static PyObject *show_doc_cst;
98 /* Get an attribute. */
100 get_attr (PyObject *obj, PyObject *attr_name)
102 if (PyString_Check (attr_name)
103 && ! strcmp (PyString_AsString (attr_name), "value"))
105 parmpy_object *self = (parmpy_object *) obj;
107 return gdbpy_parameter_value (self->type, &self->value);
110 return PyObject_GenericGetAttr (obj, attr_name);
113 /* Set a parameter value from a Python value. Return 0 on success. Returns
114 -1 on error, with a python exception set. */
116 set_parameter_value (parmpy_object *self, PyObject *value)
123 case var_string_noescape:
124 case var_optional_filename:
126 if (! gdbpy_is_string (value)
127 && (self->type == var_filename
128 || value != Py_None))
130 PyErr_SetString (PyExc_RuntimeError,
131 _("String required for filename."));
135 if (value == Py_None)
137 xfree (self->value.stringval);
138 if (self->type == var_optional_filename)
139 self->value.stringval = xstrdup ("");
141 self->value.stringval = NULL;
147 string = python_string_to_host_string (value);
151 xfree (self->value.stringval);
152 self->value.stringval = string;
161 if (! gdbpy_is_string (value))
163 PyErr_SetString (PyExc_RuntimeError,
164 _("ENUM arguments must be a string."));
168 str = python_string_to_host_string (value);
171 for (i = 0; self->enumeration[i]; ++i)
172 if (! strcmp (self->enumeration[i], str))
175 if (! self->enumeration[i])
177 PyErr_SetString (PyExc_RuntimeError,
178 _("The value must be member of an enumeration."));
181 self->value.cstringval = self->enumeration[i];
186 if (! PyBool_Check (value))
188 PyErr_SetString (PyExc_RuntimeError,
189 _("A boolean argument is required."));
192 cmp = PyObject_IsTrue (value);
195 self->value.intval = cmp;
198 case var_auto_boolean:
199 if (! PyBool_Check (value) && value != Py_None)
201 PyErr_SetString (PyExc_RuntimeError,
202 _("A boolean or None is required"));
206 if (value == Py_None)
207 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
210 cmp = PyObject_IsTrue (value);
214 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
216 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
228 if (! PyInt_Check (value))
230 PyErr_SetString (PyExc_RuntimeError,
231 _("The value must be integer."));
235 l = PyInt_AsLong (value);
236 if (self->type == var_uinteger)
238 ok = (l >= 0 && l <= UINT_MAX);
242 else if (self->type == var_integer)
244 ok = (l >= INT_MIN && l <= INT_MAX);
249 ok = (l >= INT_MIN && l <= INT_MAX);
253 PyErr_SetString (PyExc_RuntimeError,
254 _("Range exceeded."));
258 self->value.intval = (int) l;
263 PyErr_SetString (PyExc_RuntimeError,
264 _("Unhandled type in parameter value."));
271 /* Set an attribute. Returns -1 on error, with a python exception set. */
273 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
275 if (PyString_Check (attr_name)
276 && ! strcmp (PyString_AsString (attr_name), "value"))
280 PyErr_SetString (PyExc_RuntimeError,
281 _("Cannot delete a parameter's value."));
284 return set_parameter_value ((parmpy_object *) obj, val);
287 return PyObject_GenericSetAttr (obj, attr_name, val);
292 /* A helper function that dispatches to the appropriate add_setshow
295 add_setshow_generic (int parmclass, enum command_class cmdclass,
296 char *cmd_name, parmpy_object *self,
297 char *set_doc, char *show_doc, char *help_doc,
298 struct cmd_list_element **set_list,
299 struct cmd_list_element **show_list)
304 add_setshow_boolean_cmd (cmd_name, cmdclass, &self->value.intval,
305 set_doc, show_doc, help_doc,
306 NULL, NULL, set_list, show_list);
309 case var_auto_boolean:
310 add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
311 &self->value.autoboolval,
312 set_doc, show_doc, help_doc,
313 NULL, NULL, set_list, show_list);
317 add_setshow_uinteger_cmd (cmd_name, cmdclass, &self->value.uintval,
318 set_doc, show_doc, help_doc,
319 NULL, NULL, set_list, show_list);
323 add_setshow_integer_cmd (cmd_name, cmdclass, &self->value.intval,
324 set_doc, show_doc, help_doc,
325 NULL, NULL, set_list, show_list);
329 add_setshow_string_cmd (cmd_name, cmdclass, &self->value.stringval,
330 set_doc, show_doc, help_doc,
331 NULL, NULL, set_list, show_list);
334 case var_string_noescape:
335 add_setshow_string_noescape_cmd (cmd_name, cmdclass,
336 &self->value.stringval,
337 set_doc, show_doc, help_doc,
338 NULL, NULL, set_list, show_list);
341 case var_optional_filename:
342 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
343 &self->value.stringval,
344 set_doc, show_doc, help_doc,
345 NULL, NULL, set_list, show_list);
349 add_setshow_filename_cmd (cmd_name, cmdclass, &self->value.stringval,
350 set_doc, show_doc, help_doc,
351 NULL, NULL, set_list, show_list);
355 add_setshow_zinteger_cmd (cmd_name, cmdclass, &self->value.intval,
356 set_doc, show_doc, help_doc,
357 NULL, NULL, set_list, show_list);
361 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
362 &self->value.cstringval,
363 set_doc, show_doc, help_doc,
364 NULL, NULL, set_list, show_list);
365 /* Initialize the value, just in case. */
366 self->value.cstringval = self->enumeration[0];
371 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
372 error, with a python exception set. */
374 compute_enum_values (parmpy_object *self, PyObject *enum_values)
377 struct cleanup *back_to;
381 PyErr_SetString (PyExc_RuntimeError,
382 _("An enumeration is required for PARAM_ENUM."));
386 if (! PySequence_Check (enum_values))
388 PyErr_SetString (PyExc_RuntimeError,
389 _("The enumeration is not a sequence."));
393 size = PySequence_Size (enum_values);
398 PyErr_SetString (PyExc_RuntimeError,
399 _("The enumeration is empty."));
403 self->enumeration = xmalloc ((size + 1) * sizeof (char *));
404 back_to = make_cleanup (free_current_contents, &self->enumeration);
405 memset (self->enumeration, 0, (size + 1) * sizeof (char *));
407 for (i = 0; i < size; ++i)
409 PyObject *item = PySequence_GetItem (enum_values, i);
413 do_cleanups (back_to);
416 if (! gdbpy_is_string (item))
418 do_cleanups (back_to);
419 PyErr_SetString (PyExc_RuntimeError,
420 _("The enumeration item not a string."));
423 self->enumeration[i] = python_string_to_host_string (item);
424 if (self->enumeration[i] == NULL)
426 do_cleanups (back_to);
429 make_cleanup (xfree, (char *) self->enumeration[i]);
432 discard_cleanups (back_to);
436 /* A helper function which returns a documentation string for an
439 get_doc_string (PyObject *object, PyObject *attr)
443 if (PyObject_HasAttr (object, attr))
445 PyObject *ds_obj = PyObject_GetAttr (object, attr);
447 if (ds_obj && gdbpy_is_string (ds_obj))
449 result = python_string_to_host_string (ds_obj);
451 gdbpy_print_stack ();
455 result = xstrdup (_("This command is not documented."));
459 /* Object initializer; sets up gdb-side structures for command.
461 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
463 NAME is the name of the parameter. It may consist of multiple
464 words, in which case the final word is the name of the new command,
465 and earlier words must be prefix commands.
467 CMDCLASS is the kind of command. It should be one of the COMMAND_*
468 constants defined in the gdb module.
470 PARMCLASS is the type of the parameter. It should be one of the
471 PARAM_* constants defined in the gdb module.
473 If PARMCLASS is PARAM_ENUM, then the final argument should be a
474 collection of strings. These strings are the valid values for this
477 The documentation for the parameter is taken from the doc string
478 for the python class.
480 Returns -1 on error, with a python exception set. */
483 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
485 parmpy_object *obj = (parmpy_object *) self;
487 char *set_doc, *show_doc, *doc;
489 int parmclass, cmdtype;
490 PyObject *enum_values = NULL;
491 struct cmd_list_element **set_list, **show_list;
492 volatile struct gdb_exception except;
494 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
498 if (cmdtype != no_class && cmdtype != class_run
499 && cmdtype != class_vars && cmdtype != class_stack
500 && cmdtype != class_files && cmdtype != class_support
501 && cmdtype != class_info && cmdtype != class_breakpoint
502 && cmdtype != class_trace && cmdtype != class_obscure
503 && cmdtype != class_maintenance)
505 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
509 if (parmclass != var_boolean /* ARI: var_boolean */
510 && parmclass != var_auto_boolean
511 && parmclass != var_uinteger && parmclass != var_integer
512 && parmclass != var_string && parmclass != var_string_noescape
513 && parmclass != var_optional_filename && parmclass != var_filename
514 && parmclass != var_zinteger && parmclass != var_enum)
516 PyErr_SetString (PyExc_RuntimeError, _("Invalid parameter class argument."));
520 if (enum_values && parmclass != var_enum)
522 PyErr_SetString (PyExc_RuntimeError,
523 _("Only PARAM_ENUM accepts a fourth argument."));
526 if (parmclass == var_enum)
528 if (! compute_enum_values (obj, enum_values))
532 obj->enumeration = NULL;
533 obj->type = (enum var_types) parmclass;
534 memset (&obj->value, 0, sizeof (obj->value));
536 cmd_name = gdbpy_parse_command_name (name, &set_list,
542 cmd_name = gdbpy_parse_command_name (name, &show_list,
547 set_doc = get_doc_string (self, set_doc_cst);
548 show_doc = get_doc_string (self, show_doc_cst);
549 doc = get_doc_string (self, gdbpy_doc_cst);
553 TRY_CATCH (except, RETURN_MASK_ALL)
555 add_setshow_generic (parmclass, (enum command_class) cmdtype,
558 doc, set_list, show_list);
560 if (except.reason < 0)
567 PyErr_Format (except.reason == RETURN_QUIT
568 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
569 "%s", except.message);
577 /* Initialize the 'parameters' module. */
579 gdbpy_initialize_parameters (void)
583 if (PyType_Ready (&parmpy_object_type) < 0)
586 set_doc_cst = PyString_FromString ("set_doc");
589 show_doc_cst = PyString_FromString ("show_doc");
593 for (i = 0; parm_constants[i].name; ++i)
595 if (PyModule_AddIntConstant (gdb_module,
596 parm_constants[i].name,
597 parm_constants[i].value) < 0)
601 Py_INCREF (&parmpy_object_type);
602 PyModule_AddObject (gdb_module, "Parameter",
603 (PyObject *) &parmpy_object_type);
608 static PyTypeObject parmpy_object_type =
610 PyObject_HEAD_INIT (NULL)
612 "gdb.Parameter", /*tp_name*/
613 sizeof (parmpy_object), /*tp_basicsize*/
622 0, /*tp_as_sequence*/
627 get_attr, /*tp_getattro*/
628 set_attr, /*tp_setattro*/
630 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
631 "GDB parameter object", /* tp_doc */
634 0, /* tp_richcompare */
635 0, /* tp_weaklistoffset */
643 0, /* tp_descr_get */
644 0, /* tp_descr_set */
645 0, /* tp_dictoffset */
646 parmpy_init, /* tp_init */
648 PyType_GenericNew /* tp_new */