1 /* GDB parameters implemented in Python
3 Copyright (C) 2008-2021 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 "python-internal.h"
26 #include "cli/cli-decode.h"
27 #include "completer.h"
29 #include "arch-utils.h"
31 /* Parameter constants and their values. */
37 { "PARAM_BOOLEAN", var_boolean
}, /* ARI: var_boolean */
38 { "PARAM_AUTO_BOOLEAN", var_auto_boolean
},
39 { "PARAM_UINTEGER", var_uinteger
},
40 { "PARAM_INTEGER", var_integer
},
41 { "PARAM_STRING", var_string
},
42 { "PARAM_STRING_NOESCAPE", var_string_noescape
},
43 { "PARAM_OPTIONAL_FILENAME", var_optional_filename
},
44 { "PARAM_FILENAME", var_filename
},
45 { "PARAM_ZINTEGER", var_zinteger
},
46 { "PARAM_ZUINTEGER", var_zuinteger
},
47 { "PARAM_ZUINTEGER_UNLIMITED", var_zuinteger_unlimited
},
48 { "PARAM_ENUM", var_enum
},
52 /* A union that can hold anything described by enum var_types. */
55 /* Hold a boolean value. */
58 /* Hold an integer value. */
61 /* Hold an auto_boolean. */
62 enum auto_boolean autoboolval
;
64 /* Hold an unsigned integer value, for uinteger. */
67 /* Hold a string, for the various string types. */
70 /* Hold a string, for enums. */
71 const char *cstringval
;
74 /* A GDB parameter. */
79 /* The type of the parameter. */
82 /* The value of the parameter. */
83 union parmpy_variable value
;
85 /* For an enum command, the possible values. The vector is
86 allocated with xmalloc, as is each element. It is
88 const char **enumeration
;
91 extern PyTypeObject parmpy_object_type
92 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
94 /* Some handy string constants. */
95 static PyObject
*set_doc_cst
;
96 static PyObject
*show_doc_cst
;
100 /* Get an attribute. */
102 get_attr (PyObject
*obj
, PyObject
*attr_name
)
104 if (PyString_Check (attr_name
)
106 && ! PyUnicode_CompareWithASCIIString (attr_name
, "value"))
108 && ! strcmp (PyString_AsString (attr_name
), "value"))
111 parmpy_object
*self
= (parmpy_object
*) obj
;
113 return gdbpy_parameter_value (self
->type
, &self
->value
);
116 return PyObject_GenericGetAttr (obj
, attr_name
);
119 /* Set a parameter value from a Python value. Return 0 on success. Returns
120 -1 on error, with a python exception set. */
122 set_parameter_value (parmpy_object
*self
, PyObject
*value
)
129 case var_string_noescape
:
130 case var_optional_filename
:
132 if (! gdbpy_is_string (value
)
133 && (self
->type
== var_filename
134 || value
!= Py_None
))
136 PyErr_SetString (PyExc_RuntimeError
,
137 _("String required for filename."));
141 if (value
== Py_None
)
143 xfree (self
->value
.stringval
);
144 if (self
->type
== var_optional_filename
)
145 self
->value
.stringval
= xstrdup ("");
147 self
->value
.stringval
= NULL
;
151 gdb::unique_xmalloc_ptr
<char>
152 string (python_string_to_host_string (value
));
156 xfree (self
->value
.stringval
);
157 self
->value
.stringval
= string
.release ();
165 if (! gdbpy_is_string (value
))
167 PyErr_SetString (PyExc_RuntimeError
,
168 _("ENUM arguments must be a string."));
172 gdb::unique_xmalloc_ptr
<char>
173 str (python_string_to_host_string (value
));
176 for (i
= 0; self
->enumeration
[i
]; ++i
)
177 if (! strcmp (self
->enumeration
[i
], str
.get ()))
179 if (! self
->enumeration
[i
])
181 PyErr_SetString (PyExc_RuntimeError
,
182 _("The value must be member of an enumeration."));
185 self
->value
.cstringval
= self
->enumeration
[i
];
190 if (! PyBool_Check (value
))
192 PyErr_SetString (PyExc_RuntimeError
,
193 _("A boolean argument is required."));
196 cmp
= PyObject_IsTrue (value
);
199 self
->value
.boolval
= cmp
;
202 case var_auto_boolean
:
203 if (! PyBool_Check (value
) && value
!= Py_None
)
205 PyErr_SetString (PyExc_RuntimeError
,
206 _("A boolean or None is required"));
210 if (value
== Py_None
)
211 self
->value
.autoboolval
= AUTO_BOOLEAN_AUTO
;
214 cmp
= PyObject_IsTrue (value
);
218 self
->value
.autoboolval
= AUTO_BOOLEAN_TRUE
;
220 self
->value
.autoboolval
= AUTO_BOOLEAN_FALSE
;
228 case var_zuinteger_unlimited
:
233 if (! PyInt_Check (value
))
235 PyErr_SetString (PyExc_RuntimeError
,
236 _("The value must be integer."));
240 if (! gdb_py_int_as_long (value
, &l
))
250 ok
= (l
>= 0 && l
<= UINT_MAX
);
253 case var_zuinteger_unlimited
:
254 ok
= (l
>= -1 && l
<= INT_MAX
);
258 ok
= (l
>= INT_MIN
&& l
<= INT_MAX
);
264 ok
= (l
>= INT_MIN
&& l
<= INT_MAX
);
268 gdb_assert_not_reached ("unknown var_ constant");
273 PyErr_SetString (PyExc_RuntimeError
,
274 _("Range exceeded."));
278 if (self
->type
== var_uinteger
|| self
->type
== var_zuinteger
)
279 self
->value
.uintval
= (unsigned) l
;
281 self
->value
.intval
= (int) l
;
286 PyErr_SetString (PyExc_RuntimeError
,
287 _("Unhandled type in parameter value."));
294 /* Set an attribute. Returns -1 on error, with a python exception set. */
296 set_attr (PyObject
*obj
, PyObject
*attr_name
, PyObject
*val
)
298 if (PyString_Check (attr_name
)
300 && ! PyUnicode_CompareWithASCIIString (attr_name
, "value"))
302 && ! strcmp (PyString_AsString (attr_name
), "value"))
307 PyErr_SetString (PyExc_RuntimeError
,
308 _("Cannot delete a parameter's value."));
311 return set_parameter_value ((parmpy_object
*) obj
, val
);
314 return PyObject_GenericSetAttr (obj
, attr_name
, val
);
317 /* A helper function which returns a documentation string for an
320 static gdb::unique_xmalloc_ptr
<char>
321 get_doc_string (PyObject
*object
, PyObject
*attr
)
323 gdb::unique_xmalloc_ptr
<char> result
;
325 if (PyObject_HasAttr (object
, attr
))
327 gdbpy_ref
<> ds_obj (PyObject_GetAttr (object
, attr
));
329 if (ds_obj
!= NULL
&& gdbpy_is_string (ds_obj
.get ()))
331 result
= python_string_to_host_string (ds_obj
.get ());
333 gdbpy_print_stack ();
337 result
.reset (xstrdup (_("This command is not documented.")));
341 /* Helper function which will execute a METHOD in OBJ passing the
342 argument ARG. ARG can be NULL. METHOD should return a Python
343 string. If this function returns NULL, there has been an error and
344 the appropriate exception set. */
345 static gdb::unique_xmalloc_ptr
<char>
346 call_doc_function (PyObject
*obj
, PyObject
*method
, PyObject
*arg
)
348 gdb::unique_xmalloc_ptr
<char> data
;
349 gdbpy_ref
<> result (PyObject_CallMethodObjArgs (obj
, method
, arg
, NULL
));
354 if (gdbpy_is_string (result
.get ()))
356 data
= python_string_to_host_string (result
.get ());
362 PyErr_SetString (PyExc_RuntimeError
,
363 _("Parameter must return a string value."));
370 /* A callback function that is registered against the respective
371 add_setshow_* set_doc prototype. This function calls the Python function
372 "get_set_string" if it exists, which will return a string. That string
373 is then printed. If "get_set_string" does not exist, or returns an
374 empty string, then nothing is printed. */
376 get_set_value (const char *args
, int from_tty
,
377 struct cmd_list_element
*c
)
379 PyObject
*obj
= (PyObject
*) get_cmd_context (c
);
380 gdb::unique_xmalloc_ptr
<char> set_doc_string
;
382 gdbpy_enter
enter_py (get_current_arch (), current_language
);
383 gdbpy_ref
<> set_doc_func (PyString_FromString ("get_set_string"));
385 if (set_doc_func
== NULL
)
387 gdbpy_print_stack ();
391 if (PyObject_HasAttr (obj
, set_doc_func
.get ()))
393 set_doc_string
= call_doc_function (obj
, set_doc_func
.get (), NULL
);
394 if (! set_doc_string
)
395 gdbpy_handle_exception ();
398 const char *str
= set_doc_string
.get ();
399 if (str
!= nullptr && str
[0] != '\0')
400 fprintf_filtered (gdb_stdout
, "%s\n", str
);
403 /* A callback function that is registered against the respective
404 add_setshow_* show_doc prototype. This function will either call
405 the Python function "get_show_string" or extract the Python
406 attribute "show_doc" and return the contents as a string. If
407 neither exist, insert a string indicating the Parameter is not
410 get_show_value (struct ui_file
*file
, int from_tty
,
411 struct cmd_list_element
*c
,
414 PyObject
*obj
= (PyObject
*) get_cmd_context (c
);
415 gdb::unique_xmalloc_ptr
<char> show_doc_string
;
417 gdbpy_enter
enter_py (get_current_arch (), current_language
);
418 gdbpy_ref
<> show_doc_func (PyString_FromString ("get_show_string"));
420 if (show_doc_func
== NULL
)
422 gdbpy_print_stack ();
426 if (PyObject_HasAttr (obj
, show_doc_func
.get ()))
428 gdbpy_ref
<> val_obj (PyString_FromString (value
));
432 gdbpy_print_stack ();
436 show_doc_string
= call_doc_function (obj
, show_doc_func
.get (),
438 if (! show_doc_string
)
440 gdbpy_print_stack ();
444 fprintf_filtered (file
, "%s\n", show_doc_string
.get ());
448 /* We have to preserve the existing < GDB 7.3 API. If a
449 callback function does not exist, then attempt to read the
450 show_doc attribute. */
451 show_doc_string
= get_doc_string (obj
, show_doc_cst
);
452 fprintf_filtered (file
, "%s %s\n", show_doc_string
.get (), value
);
457 /* A helper function that dispatches to the appropriate add_setshow
460 add_setshow_generic (int parmclass
, enum command_class cmdclass
,
461 gdb::unique_xmalloc_ptr
<char> cmd_name
,
463 const char *set_doc
, const char *show_doc
,
464 const char *help_doc
,
465 struct cmd_list_element
**set_list
,
466 struct cmd_list_element
**show_list
)
468 struct cmd_list_element
*param
= NULL
;
469 const char *tmp_name
= NULL
;
475 add_setshow_boolean_cmd (cmd_name
.get (), cmdclass
,
476 &self
->value
.boolval
, set_doc
, show_doc
,
477 help_doc
, get_set_value
, get_show_value
,
478 set_list
, show_list
);
482 case var_auto_boolean
:
483 add_setshow_auto_boolean_cmd (cmd_name
.get (), cmdclass
,
484 &self
->value
.autoboolval
,
485 set_doc
, show_doc
, help_doc
,
486 get_set_value
, get_show_value
,
487 set_list
, show_list
);
491 add_setshow_uinteger_cmd (cmd_name
.get (), cmdclass
,
492 &self
->value
.uintval
, set_doc
, show_doc
,
493 help_doc
, get_set_value
, get_show_value
,
494 set_list
, show_list
);
498 add_setshow_integer_cmd (cmd_name
.get (), cmdclass
,
499 &self
->value
.intval
, set_doc
, show_doc
,
500 help_doc
, get_set_value
, get_show_value
,
501 set_list
, show_list
); break;
504 add_setshow_string_cmd (cmd_name
.get (), cmdclass
,
505 &self
->value
.stringval
, set_doc
, show_doc
,
506 help_doc
, get_set_value
, get_show_value
,
507 set_list
, show_list
); break;
509 case var_string_noescape
:
510 add_setshow_string_noescape_cmd (cmd_name
.get (), cmdclass
,
511 &self
->value
.stringval
,
512 set_doc
, show_doc
, help_doc
,
513 get_set_value
, get_show_value
,
514 set_list
, show_list
);
518 case var_optional_filename
:
519 add_setshow_optional_filename_cmd (cmd_name
.get (), cmdclass
,
520 &self
->value
.stringval
, set_doc
,
521 show_doc
, help_doc
, get_set_value
,
522 get_show_value
, set_list
,
527 add_setshow_filename_cmd (cmd_name
.get (), cmdclass
,
528 &self
->value
.stringval
, set_doc
, show_doc
,
529 help_doc
, get_set_value
, get_show_value
,
530 set_list
, show_list
); break;
533 add_setshow_zinteger_cmd (cmd_name
.get (), cmdclass
,
534 &self
->value
.intval
, set_doc
, show_doc
,
535 help_doc
, get_set_value
, get_show_value
,
536 set_list
, show_list
);
540 add_setshow_zuinteger_cmd (cmd_name
.get (), cmdclass
,
541 &self
->value
.uintval
, set_doc
, show_doc
,
542 help_doc
, get_set_value
, get_show_value
,
543 set_list
, show_list
);
546 case var_zuinteger_unlimited
:
547 add_setshow_zuinteger_unlimited_cmd (cmd_name
.get (), cmdclass
,
548 &self
->value
.intval
, set_doc
,
549 show_doc
, help_doc
, get_set_value
,
551 set_list
, show_list
);
555 add_setshow_enum_cmd (cmd_name
.get (), cmdclass
, self
->enumeration
,
556 &self
->value
.cstringval
, set_doc
, show_doc
,
557 help_doc
, get_set_value
, get_show_value
,
558 set_list
, show_list
);
559 /* Initialize the value, just in case. */
560 self
->value
.cstringval
= self
->enumeration
[0];
564 /* Lookup created parameter, and register Python object against the
565 parameter context. Perform this task against both lists. */
566 tmp_name
= cmd_name
.get ();
567 param
= lookup_cmd (&tmp_name
, *show_list
, "", NULL
, 0, 1);
569 set_cmd_context (param
, self
);
571 tmp_name
= cmd_name
.get ();
572 param
= lookup_cmd (&tmp_name
, *set_list
, "", NULL
, 0, 1);
574 set_cmd_context (param
, self
);
576 /* We (unfortunately) currently leak the command name. */
580 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
581 error, with a python exception set. */
583 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
589 PyErr_SetString (PyExc_RuntimeError
,
590 _("An enumeration is required for PARAM_ENUM."));
594 if (! PySequence_Check (enum_values
))
596 PyErr_SetString (PyExc_RuntimeError
,
597 _("The enumeration is not a sequence."));
601 size
= PySequence_Size (enum_values
);
606 PyErr_SetString (PyExc_RuntimeError
,
607 _("The enumeration is empty."));
611 gdb_argv
holder (XCNEWVEC (char *, size
+ 1));
612 char **enumeration
= holder
.get ();
614 for (i
= 0; i
< size
; ++i
)
616 gdbpy_ref
<> item (PySequence_GetItem (enum_values
, i
));
620 if (! gdbpy_is_string (item
.get ()))
622 PyErr_SetString (PyExc_RuntimeError
,
623 _("The enumeration item not a string."));
626 enumeration
[i
] = python_string_to_host_string (item
.get ()).release ();
627 if (enumeration
[i
] == NULL
)
631 self
->enumeration
= const_cast<const char**> (holder
.release ());
635 /* Object initializer; sets up gdb-side structures for command.
637 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
639 NAME is the name of the parameter. It may consist of multiple
640 words, in which case the final word is the name of the new command,
641 and earlier words must be prefix commands.
643 CMDCLASS is the kind of command. It should be one of the COMMAND_*
644 constants defined in the gdb module.
646 PARMCLASS is the type of the parameter. It should be one of the
647 PARAM_* constants defined in the gdb module.
649 If PARMCLASS is PARAM_ENUM, then the final argument should be a
650 collection of strings. These strings are the valid values for this
653 The documentation for the parameter is taken from the doc string
654 for the python class.
656 Returns -1 on error, with a python exception set. */
659 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
661 parmpy_object
*obj
= (parmpy_object
*) self
;
663 gdb::unique_xmalloc_ptr
<char> set_doc
, show_doc
, doc
;
664 int parmclass
, cmdtype
;
665 PyObject
*enum_values
= NULL
;
666 struct cmd_list_element
**set_list
, **show_list
;
668 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
672 if (cmdtype
!= no_class
&& cmdtype
!= class_run
673 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
674 && cmdtype
!= class_files
&& cmdtype
!= class_support
675 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
676 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
677 && cmdtype
!= class_maintenance
)
679 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
683 if (parmclass
!= var_boolean
/* ARI: var_boolean */
684 && parmclass
!= var_auto_boolean
685 && parmclass
!= var_uinteger
&& parmclass
!= var_integer
686 && parmclass
!= var_string
&& parmclass
!= var_string_noescape
687 && parmclass
!= var_optional_filename
&& parmclass
!= var_filename
688 && parmclass
!= var_zinteger
&& parmclass
!= var_zuinteger
689 && parmclass
!= var_zuinteger_unlimited
&& parmclass
!= var_enum
)
691 PyErr_SetString (PyExc_RuntimeError
,
692 _("Invalid parameter class argument."));
696 if (enum_values
&& parmclass
!= var_enum
)
698 PyErr_SetString (PyExc_RuntimeError
,
699 _("Only PARAM_ENUM accepts a fourth argument."));
702 if (parmclass
== var_enum
)
704 if (! compute_enum_values (obj
, enum_values
))
708 obj
->enumeration
= NULL
;
709 obj
->type
= (enum var_types
) parmclass
;
710 memset (&obj
->value
, 0, sizeof (obj
->value
));
712 gdb::unique_xmalloc_ptr
<char> cmd_name
713 = gdbpy_parse_command_name (name
, &set_list
, &setlist
);
714 if (cmd_name
== nullptr)
717 cmd_name
= gdbpy_parse_command_name (name
, &show_list
, &showlist
);
718 if (cmd_name
== nullptr)
721 set_doc
= get_doc_string (self
, set_doc_cst
);
722 show_doc
= get_doc_string (self
, show_doc_cst
);
723 doc
= get_doc_string (self
, gdbpy_doc_cst
);
729 add_setshow_generic (parmclass
, (enum command_class
) cmdtype
,
730 std::move (cmd_name
), obj
,
731 set_doc
.get (), show_doc
.get (),
732 doc
.get (), set_list
, show_list
);
734 catch (const gdb_exception
&except
)
737 gdbpy_convert_exception (except
);
746 /* Initialize the 'parameters' module. */
748 gdbpy_initialize_parameters (void)
752 parmpy_object_type
.tp_new
= PyType_GenericNew
;
753 if (PyType_Ready (&parmpy_object_type
) < 0)
756 set_doc_cst
= PyString_FromString ("set_doc");
759 show_doc_cst
= PyString_FromString ("show_doc");
763 for (i
= 0; parm_constants
[i
].name
; ++i
)
765 if (PyModule_AddIntConstant (gdb_module
,
766 parm_constants
[i
].name
,
767 parm_constants
[i
].value
) < 0)
771 return gdb_pymodule_addobject (gdb_module
, "Parameter",
772 (PyObject
*) &parmpy_object_type
);
777 PyTypeObject parmpy_object_type
=
779 PyVarObject_HEAD_INIT (NULL
, 0)
780 "gdb.Parameter", /*tp_name*/
781 sizeof (parmpy_object
), /*tp_basicsize*/
790 0, /*tp_as_sequence*/
795 get_attr
, /*tp_getattro*/
796 set_attr
, /*tp_setattro*/
798 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
799 "GDB parameter object", /* tp_doc */
802 0, /* tp_richcompare */
803 0, /* tp_weaklistoffset */
811 0, /* tp_descr_get */
812 0, /* tp_descr_set */
813 0, /* tp_dictoffset */
814 parmpy_init
, /* tp_init */