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 if (! gdb_py_int_as_long (value
, &l
))
238 if (self
->type
== var_uinteger
)
240 ok
= (l
>= 0 && l
<= UINT_MAX
);
244 else if (self
->type
== var_integer
)
246 ok
= (l
>= INT_MIN
&& l
<= INT_MAX
);
251 ok
= (l
>= INT_MIN
&& l
<= INT_MAX
);
255 PyErr_SetString (PyExc_RuntimeError
,
256 _("Range exceeded."));
260 self
->value
.intval
= (int) l
;
265 PyErr_SetString (PyExc_RuntimeError
,
266 _("Unhandled type in parameter value."));
273 /* Set an attribute. Returns -1 on error, with a python exception set. */
275 set_attr (PyObject
*obj
, PyObject
*attr_name
, PyObject
*val
)
277 if (PyString_Check (attr_name
)
278 && ! strcmp (PyString_AsString (attr_name
), "value"))
282 PyErr_SetString (PyExc_RuntimeError
,
283 _("Cannot delete a parameter's value."));
286 return set_parameter_value ((parmpy_object
*) obj
, val
);
289 return PyObject_GenericSetAttr (obj
, attr_name
, val
);
294 /* A helper function that dispatches to the appropriate add_setshow
297 add_setshow_generic (int parmclass
, enum command_class cmdclass
,
298 char *cmd_name
, parmpy_object
*self
,
299 char *set_doc
, char *show_doc
, char *help_doc
,
300 struct cmd_list_element
**set_list
,
301 struct cmd_list_element
**show_list
)
306 add_setshow_boolean_cmd (cmd_name
, cmdclass
, &self
->value
.intval
,
307 set_doc
, show_doc
, help_doc
,
308 NULL
, NULL
, set_list
, show_list
);
311 case var_auto_boolean
:
312 add_setshow_auto_boolean_cmd (cmd_name
, cmdclass
,
313 &self
->value
.autoboolval
,
314 set_doc
, show_doc
, help_doc
,
315 NULL
, NULL
, set_list
, show_list
);
319 add_setshow_uinteger_cmd (cmd_name
, cmdclass
, &self
->value
.uintval
,
320 set_doc
, show_doc
, help_doc
,
321 NULL
, NULL
, set_list
, show_list
);
325 add_setshow_integer_cmd (cmd_name
, cmdclass
, &self
->value
.intval
,
326 set_doc
, show_doc
, help_doc
,
327 NULL
, NULL
, set_list
, show_list
);
331 add_setshow_string_cmd (cmd_name
, cmdclass
, &self
->value
.stringval
,
332 set_doc
, show_doc
, help_doc
,
333 NULL
, NULL
, set_list
, show_list
);
336 case var_string_noescape
:
337 add_setshow_string_noescape_cmd (cmd_name
, cmdclass
,
338 &self
->value
.stringval
,
339 set_doc
, show_doc
, help_doc
,
340 NULL
, NULL
, set_list
, show_list
);
343 case var_optional_filename
:
344 add_setshow_optional_filename_cmd (cmd_name
, cmdclass
,
345 &self
->value
.stringval
,
346 set_doc
, show_doc
, help_doc
,
347 NULL
, NULL
, set_list
, show_list
);
351 add_setshow_filename_cmd (cmd_name
, cmdclass
, &self
->value
.stringval
,
352 set_doc
, show_doc
, help_doc
,
353 NULL
, NULL
, set_list
, show_list
);
357 add_setshow_zinteger_cmd (cmd_name
, cmdclass
, &self
->value
.intval
,
358 set_doc
, show_doc
, help_doc
,
359 NULL
, NULL
, set_list
, show_list
);
363 add_setshow_enum_cmd (cmd_name
, cmdclass
, self
->enumeration
,
364 &self
->value
.cstringval
,
365 set_doc
, show_doc
, help_doc
,
366 NULL
, NULL
, set_list
, show_list
);
367 /* Initialize the value, just in case. */
368 self
->value
.cstringval
= self
->enumeration
[0];
373 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
374 error, with a python exception set. */
376 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
379 struct cleanup
*back_to
;
383 PyErr_SetString (PyExc_RuntimeError
,
384 _("An enumeration is required for PARAM_ENUM."));
388 if (! PySequence_Check (enum_values
))
390 PyErr_SetString (PyExc_RuntimeError
,
391 _("The enumeration is not a sequence."));
395 size
= PySequence_Size (enum_values
);
400 PyErr_SetString (PyExc_RuntimeError
,
401 _("The enumeration is empty."));
405 self
->enumeration
= xmalloc ((size
+ 1) * sizeof (char *));
406 back_to
= make_cleanup (free_current_contents
, &self
->enumeration
);
407 memset (self
->enumeration
, 0, (size
+ 1) * sizeof (char *));
409 for (i
= 0; i
< size
; ++i
)
411 PyObject
*item
= PySequence_GetItem (enum_values
, i
);
415 do_cleanups (back_to
);
418 if (! gdbpy_is_string (item
))
420 do_cleanups (back_to
);
421 PyErr_SetString (PyExc_RuntimeError
,
422 _("The enumeration item not a string."));
425 self
->enumeration
[i
] = python_string_to_host_string (item
);
426 if (self
->enumeration
[i
] == NULL
)
428 do_cleanups (back_to
);
431 make_cleanup (xfree
, (char *) self
->enumeration
[i
]);
434 discard_cleanups (back_to
);
438 /* A helper function which returns a documentation string for an
441 get_doc_string (PyObject
*object
, PyObject
*attr
)
445 if (PyObject_HasAttr (object
, attr
))
447 PyObject
*ds_obj
= PyObject_GetAttr (object
, attr
);
449 if (ds_obj
&& gdbpy_is_string (ds_obj
))
451 result
= python_string_to_host_string (ds_obj
);
453 gdbpy_print_stack ();
457 result
= xstrdup (_("This command is not documented."));
461 /* Object initializer; sets up gdb-side structures for command.
463 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
465 NAME is the name of the parameter. It may consist of multiple
466 words, in which case the final word is the name of the new command,
467 and earlier words must be prefix commands.
469 CMDCLASS is the kind of command. It should be one of the COMMAND_*
470 constants defined in the gdb module.
472 PARMCLASS is the type of the parameter. It should be one of the
473 PARAM_* constants defined in the gdb module.
475 If PARMCLASS is PARAM_ENUM, then the final argument should be a
476 collection of strings. These strings are the valid values for this
479 The documentation for the parameter is taken from the doc string
480 for the python class.
482 Returns -1 on error, with a python exception set. */
485 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
487 parmpy_object
*obj
= (parmpy_object
*) self
;
489 char *set_doc
, *show_doc
, *doc
;
491 int parmclass
, cmdtype
;
492 PyObject
*enum_values
= NULL
;
493 struct cmd_list_element
**set_list
, **show_list
;
494 volatile struct gdb_exception except
;
496 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
500 if (cmdtype
!= no_class
&& cmdtype
!= class_run
501 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
502 && cmdtype
!= class_files
&& cmdtype
!= class_support
503 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
504 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
505 && cmdtype
!= class_maintenance
)
507 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
511 if (parmclass
!= var_boolean
/* ARI: var_boolean */
512 && parmclass
!= var_auto_boolean
513 && parmclass
!= var_uinteger
&& parmclass
!= var_integer
514 && parmclass
!= var_string
&& parmclass
!= var_string_noescape
515 && parmclass
!= var_optional_filename
&& parmclass
!= var_filename
516 && parmclass
!= var_zinteger
&& parmclass
!= var_enum
)
518 PyErr_SetString (PyExc_RuntimeError
,
519 _("Invalid parameter class argument."));
523 if (enum_values
&& parmclass
!= var_enum
)
525 PyErr_SetString (PyExc_RuntimeError
,
526 _("Only PARAM_ENUM accepts a fourth argument."));
529 if (parmclass
== var_enum
)
531 if (! compute_enum_values (obj
, enum_values
))
535 obj
->enumeration
= NULL
;
536 obj
->type
= (enum var_types
) parmclass
;
537 memset (&obj
->value
, 0, sizeof (obj
->value
));
539 cmd_name
= gdbpy_parse_command_name (name
, &set_list
,
545 cmd_name
= gdbpy_parse_command_name (name
, &show_list
,
550 set_doc
= get_doc_string (self
, set_doc_cst
);
551 show_doc
= get_doc_string (self
, show_doc_cst
);
552 doc
= get_doc_string (self
, gdbpy_doc_cst
);
556 TRY_CATCH (except
, RETURN_MASK_ALL
)
558 add_setshow_generic (parmclass
, (enum command_class
) cmdtype
,
561 doc
, set_list
, show_list
);
563 if (except
.reason
< 0)
570 PyErr_Format (except
.reason
== RETURN_QUIT
571 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
572 "%s", except
.message
);
580 /* Initialize the 'parameters' module. */
582 gdbpy_initialize_parameters (void)
586 if (PyType_Ready (&parmpy_object_type
) < 0)
589 set_doc_cst
= PyString_FromString ("set_doc");
592 show_doc_cst
= PyString_FromString ("show_doc");
596 for (i
= 0; parm_constants
[i
].name
; ++i
)
598 if (PyModule_AddIntConstant (gdb_module
,
599 parm_constants
[i
].name
,
600 parm_constants
[i
].value
) < 0)
604 Py_INCREF (&parmpy_object_type
);
605 PyModule_AddObject (gdb_module
, "Parameter",
606 (PyObject
*) &parmpy_object_type
);
611 static PyTypeObject parmpy_object_type
=
613 PyObject_HEAD_INIT (NULL
)
615 "gdb.Parameter", /*tp_name*/
616 sizeof (parmpy_object
), /*tp_basicsize*/
625 0, /*tp_as_sequence*/
630 get_attr
, /*tp_getattro*/
631 set_attr
, /*tp_setattro*/
633 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
634 "GDB parameter object", /* tp_doc */
637 0, /* tp_richcompare */
638 0, /* tp_weaklistoffset */
646 0, /* tp_descr_get */
647 0, /* tp_descr_set */
648 0, /* tp_dictoffset */
649 parmpy_init
, /* tp_init */
651 PyType_GenericNew
/* tp_new */