1 /* GDB parameters implemented in Python
3 Copyright (C) 2008-2022 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. The std::string is
69 std::string *stringval;
71 /* Hold a string, for enums. */
72 const char *cstringval;
75 /* A GDB parameter. */
80 /* The type of the parameter. */
83 /* The value of the parameter. */
84 union parmpy_variable value;
86 /* For an enum command, the possible values. The vector is
87 allocated with xmalloc, as is each element. It is
89 const char **enumeration;
92 /* Wraps a setting around an existing parmpy_object. This abstraction
93 is used to manipulate the value in S->VALUE in a type safe manner using
94 the setting interface. */
97 make_setting (parmpy_object *s)
99 if (var_type_uses<bool> (s->type))
100 return setting (s->type, &s->value.boolval);
101 else if (var_type_uses<int> (s->type))
102 return setting (s->type, &s->value.intval);
103 else if (var_type_uses<auto_boolean> (s->type))
104 return setting (s->type, &s->value.autoboolval);
105 else if (var_type_uses<unsigned int> (s->type))
106 return setting (s->type, &s->value.uintval);
107 else if (var_type_uses<std::string> (s->type))
108 return setting (s->type, s->value.stringval);
109 else if (var_type_uses<const char *> (s->type))
110 return setting (s->type, &s->value.cstringval);
112 gdb_assert_not_reached ("unhandled var type");
115 extern PyTypeObject parmpy_object_type
116 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
118 /* Some handy string constants. */
119 static PyObject *set_doc_cst;
120 static PyObject *show_doc_cst;
124 /* Get an attribute. */
126 get_attr (PyObject *obj, PyObject *attr_name)
128 if (PyString_Check (attr_name)
130 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
132 && ! strcmp (PyString_AsString (attr_name), "value"))
135 parmpy_object *self = (parmpy_object *) obj;
137 return gdbpy_parameter_value (make_setting (self));
140 return PyObject_GenericGetAttr (obj, attr_name);
143 /* Set a parameter value from a Python value. Return 0 on success. Returns
144 -1 on error, with a python exception set. */
146 set_parameter_value (parmpy_object *self, PyObject *value)
153 case var_string_noescape:
154 case var_optional_filename:
156 if (! gdbpy_is_string (value)
157 && (self->type == var_filename
158 || value != Py_None))
160 PyErr_SetString (PyExc_RuntimeError,
161 _("String required for filename."));
165 if (value == Py_None)
166 self->value.stringval->clear ();
169 gdb::unique_xmalloc_ptr<char>
170 string (python_string_to_host_string (value));
174 *self->value.stringval = string.get ();
182 if (! gdbpy_is_string (value))
184 PyErr_SetString (PyExc_RuntimeError,
185 _("ENUM arguments must be a string."));
189 gdb::unique_xmalloc_ptr<char>
190 str (python_string_to_host_string (value));
193 for (i = 0; self->enumeration[i]; ++i)
194 if (! strcmp (self->enumeration[i], str.get ()))
196 if (! self->enumeration[i])
198 PyErr_SetString (PyExc_RuntimeError,
199 _("The value must be member of an enumeration."));
202 self->value.cstringval = self->enumeration[i];
207 if (! PyBool_Check (value))
209 PyErr_SetString (PyExc_RuntimeError,
210 _("A boolean argument is required."));
213 cmp = PyObject_IsTrue (value);
216 self->value.boolval = cmp;
219 case var_auto_boolean:
220 if (! PyBool_Check (value) && value != Py_None)
222 PyErr_SetString (PyExc_RuntimeError,
223 _("A boolean or None is required"));
227 if (value == Py_None)
228 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
231 cmp = PyObject_IsTrue (value);
235 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
237 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
245 case var_zuinteger_unlimited:
250 if (! PyInt_Check (value))
252 PyErr_SetString (PyExc_RuntimeError,
253 _("The value must be integer."));
257 if (! gdb_py_int_as_long (value, &l))
267 ok = (l >= 0 && l <= UINT_MAX);
270 case var_zuinteger_unlimited:
271 ok = (l >= -1 && l <= INT_MAX);
275 ok = (l >= INT_MIN && l <= INT_MAX);
281 ok = (l >= INT_MIN && l <= INT_MAX);
285 gdb_assert_not_reached ("unknown var_ constant");
290 PyErr_SetString (PyExc_RuntimeError,
291 _("Range exceeded."));
295 if (self->type == var_uinteger || self->type == var_zuinteger)
296 self->value.uintval = (unsigned) l;
298 self->value.intval = (int) l;
303 PyErr_SetString (PyExc_RuntimeError,
304 _("Unhandled type in parameter value."));
311 /* Set an attribute. Returns -1 on error, with a python exception set. */
313 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
315 if (PyString_Check (attr_name)
317 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
319 && ! strcmp (PyString_AsString (attr_name), "value"))
324 PyErr_SetString (PyExc_RuntimeError,
325 _("Cannot delete a parameter's value."));
328 return set_parameter_value ((parmpy_object *) obj, val);
331 return PyObject_GenericSetAttr (obj, attr_name, val);
334 /* Build up the path to command C, but drop the first component of the
335 command prefix. This is only intended for use with the set/show
336 parameters this file deals with, the first prefix should always be
337 either 'set' or 'show'.
339 As an example, if this full command is 'set prefix_a prefix_b command'
340 this function will return the string 'prefix_a prefix_b command'. */
343 full_cmd_name_without_first_prefix (struct cmd_list_element *c)
345 std::vector<std::string> components
346 = c->command_components ();
347 gdb_assert (components.size () > 1);
348 std::string result = components[1];
349 for (int i = 2; i < components.size (); ++i)
350 result += " " + components[i];
354 /* The different types of documentation string. */
360 doc_string_description
363 /* A helper function which returns a documentation string for an
366 static gdb::unique_xmalloc_ptr<char>
367 get_doc_string (PyObject *object, enum doc_string_type doc_type,
368 const char *cmd_name)
370 gdb::unique_xmalloc_ptr<char> result;
372 PyObject *attr = nullptr;
378 case doc_string_show:
381 case doc_string_description:
382 attr = gdbpy_doc_cst;
385 gdb_assert (attr != nullptr);
387 if (PyObject_HasAttr (object, attr))
389 gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
391 if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
393 result = python_string_to_host_string (ds_obj.get ());
395 gdbpy_print_stack ();
399 if (result == nullptr)
401 if (doc_type == doc_string_description)
402 result.reset (xstrdup (_("This command is not documented.")));
405 if (doc_type == doc_string_show)
406 result = xstrprintf (_("Show the current value of '%s'."),
409 result = xstrprintf (_("Set the current value of '%s'."),
416 /* Helper function which will execute a METHOD in OBJ passing the
417 argument ARG. ARG can be NULL. METHOD should return a Python
418 string. If this function returns NULL, there has been an error and
419 the appropriate exception set. */
420 static gdb::unique_xmalloc_ptr<char>
421 call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
423 gdb::unique_xmalloc_ptr<char> data;
424 gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
429 if (gdbpy_is_string (result.get ()))
431 data = python_string_to_host_string (result.get ());
437 PyErr_SetString (PyExc_RuntimeError,
438 _("Parameter must return a string value."));
445 /* A callback function that is registered against the respective
446 add_setshow_* set_doc prototype. This function calls the Python function
447 "get_set_string" if it exists, which will return a string. That string
448 is then printed. If "get_set_string" does not exist, or returns an
449 empty string, then nothing is printed. */
451 get_set_value (const char *args, int from_tty,
452 struct cmd_list_element *c)
454 PyObject *obj = (PyObject *) c->context ();
455 gdb::unique_xmalloc_ptr<char> set_doc_string;
457 gdbpy_enter enter_py;
458 gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
460 if (set_doc_func == NULL)
462 gdbpy_print_stack ();
466 if (PyObject_HasAttr (obj, set_doc_func.get ()))
468 set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
469 if (! set_doc_string)
470 gdbpy_handle_exception ();
473 const char *str = set_doc_string.get ();
474 if (str != nullptr && str[0] != '\0')
475 printf_filtered ("%s\n", str);
478 /* A callback function that is registered against the respective
479 add_setshow_* show_doc prototype. This function will either call
480 the Python function "get_show_string" or extract the Python
481 attribute "show_doc" and return the contents as a string. If
482 neither exist, insert a string indicating the Parameter is not
485 get_show_value (struct ui_file *file, int from_tty,
486 struct cmd_list_element *c,
489 PyObject *obj = (PyObject *) c->context ();
490 gdb::unique_xmalloc_ptr<char> show_doc_string;
492 gdbpy_enter enter_py;
493 gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
495 if (show_doc_func == NULL)
497 gdbpy_print_stack ();
501 if (PyObject_HasAttr (obj, show_doc_func.get ()))
503 gdbpy_ref<> val_obj (PyString_FromString (value));
507 gdbpy_print_stack ();
511 show_doc_string = call_doc_function (obj, show_doc_func.get (),
513 if (! show_doc_string)
515 gdbpy_print_stack ();
519 fprintf_filtered (file, "%s\n", show_doc_string.get ());
523 /* If there is no 'get_show_string' callback then we want to show
524 something sensible here. In older versions of GDB (< 7.3) we
525 didn't support 'get_show_string', and instead we just made use of
526 GDB's builtin use of the show_doc. However, GDB's builtin
527 show_doc adjustment is not i18n friendly, so, instead, we just
528 print this generic string. */
529 std::string cmd_path = full_cmd_name_without_first_prefix (c);
530 fprintf_filtered (file, _("The current value of '%s' is \"%s\".\n"),
531 cmd_path.c_str (), value);
536 /* A helper function that dispatches to the appropriate add_setshow
539 add_setshow_generic (int parmclass, enum command_class cmdclass,
540 gdb::unique_xmalloc_ptr<char> cmd_name,
542 const char *set_doc, const char *show_doc,
543 const char *help_doc,
544 struct cmd_list_element **set_list,
545 struct cmd_list_element **show_list)
547 set_show_commands commands;
552 commands = add_setshow_boolean_cmd (cmd_name.get (), cmdclass,
553 &self->value.boolval, set_doc,
554 show_doc, help_doc, get_set_value,
555 get_show_value, set_list, show_list);
559 case var_auto_boolean:
560 commands = add_setshow_auto_boolean_cmd (cmd_name.get (), cmdclass,
561 &self->value.autoboolval,
562 set_doc, show_doc, help_doc,
563 get_set_value, get_show_value,
564 set_list, show_list);
568 commands = add_setshow_uinteger_cmd (cmd_name.get (), cmdclass,
569 &self->value.uintval, set_doc,
570 show_doc, help_doc, get_set_value,
571 get_show_value, set_list, show_list);
575 commands = add_setshow_integer_cmd (cmd_name.get (), cmdclass,
576 &self->value.intval, set_doc,
577 show_doc, help_doc, get_set_value,
578 get_show_value, set_list, show_list);
582 commands = add_setshow_string_cmd (cmd_name.get (), cmdclass,
583 self->value.stringval, set_doc,
584 show_doc, help_doc, get_set_value,
585 get_show_value, set_list, show_list);
588 case var_string_noescape:
589 commands = add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
590 self->value.stringval,
591 set_doc, show_doc, help_doc,
592 get_set_value, get_show_value,
593 set_list, show_list);
596 case var_optional_filename:
597 commands = add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
598 self->value.stringval,
599 set_doc, show_doc, help_doc,
601 get_show_value, set_list,
606 commands = add_setshow_filename_cmd (cmd_name.get (), cmdclass,
607 self->value.stringval, set_doc,
608 show_doc, help_doc, get_set_value,
609 get_show_value, set_list, show_list);
613 commands = add_setshow_zinteger_cmd (cmd_name.get (), cmdclass,
614 &self->value.intval, set_doc,
615 show_doc, help_doc, get_set_value,
616 get_show_value, set_list, show_list);
620 commands = add_setshow_zuinteger_cmd (cmd_name.get (), cmdclass,
621 &self->value.uintval, set_doc,
622 show_doc, help_doc, get_set_value,
623 get_show_value, set_list,
627 case var_zuinteger_unlimited:
628 commands = add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
631 help_doc, get_set_value,
632 get_show_value, set_list,
637 /* Initialize the value, just in case. */
638 self->value.cstringval = self->enumeration[0];
639 commands = add_setshow_enum_cmd (cmd_name.get (), cmdclass,
641 &self->value.cstringval, set_doc,
642 show_doc, help_doc, get_set_value,
643 get_show_value, set_list, show_list);
647 gdb_assert_not_reached ("Unhandled parameter class.");
650 /* Register Python objects in both commands' context. */
651 commands.set->set_context (self);
652 commands.show->set_context (self);
654 /* We (unfortunately) currently leak the command name. */
658 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
659 error, with a python exception set. */
661 compute_enum_values (parmpy_object *self, PyObject *enum_values)
667 PyErr_SetString (PyExc_RuntimeError,
668 _("An enumeration is required for PARAM_ENUM."));
672 if (! PySequence_Check (enum_values))
674 PyErr_SetString (PyExc_RuntimeError,
675 _("The enumeration is not a sequence."));
679 size = PySequence_Size (enum_values);
684 PyErr_SetString (PyExc_RuntimeError,
685 _("The enumeration is empty."));
689 gdb_argv holder (XCNEWVEC (char *, size + 1));
690 char **enumeration = holder.get ();
692 for (i = 0; i < size; ++i)
694 gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
698 if (! gdbpy_is_string (item.get ()))
700 PyErr_SetString (PyExc_RuntimeError,
701 _("The enumeration item not a string."));
704 enumeration[i] = python_string_to_host_string (item.get ()).release ();
705 if (enumeration[i] == NULL)
709 self->enumeration = const_cast<const char**> (holder.release ());
713 /* Object initializer; sets up gdb-side structures for command.
715 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
717 NAME is the name of the parameter. It may consist of multiple
718 words, in which case the final word is the name of the new command,
719 and earlier words must be prefix commands.
721 CMDCLASS is the kind of command. It should be one of the COMMAND_*
722 constants defined in the gdb module.
724 PARMCLASS is the type of the parameter. It should be one of the
725 PARAM_* constants defined in the gdb module.
727 If PARMCLASS is PARAM_ENUM, then the final argument should be a
728 collection of strings. These strings are the valid values for this
731 The documentation for the parameter is taken from the doc string
732 for the python class.
734 Returns -1 on error, with a python exception set. */
737 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
739 parmpy_object *obj = (parmpy_object *) self;
741 gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
742 int parmclass, cmdtype;
743 PyObject *enum_values = NULL;
744 struct cmd_list_element **set_list, **show_list;
746 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
750 if (cmdtype != no_class && cmdtype != class_run
751 && cmdtype != class_vars && cmdtype != class_stack
752 && cmdtype != class_files && cmdtype != class_support
753 && cmdtype != class_info && cmdtype != class_breakpoint
754 && cmdtype != class_trace && cmdtype != class_obscure
755 && cmdtype != class_maintenance)
757 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
761 if (parmclass != var_boolean /* ARI: var_boolean */
762 && parmclass != var_auto_boolean
763 && parmclass != var_uinteger && parmclass != var_integer
764 && parmclass != var_string && parmclass != var_string_noescape
765 && parmclass != var_optional_filename && parmclass != var_filename
766 && parmclass != var_zinteger && parmclass != var_zuinteger
767 && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
769 PyErr_SetString (PyExc_RuntimeError,
770 _("Invalid parameter class argument."));
774 if (enum_values && parmclass != var_enum)
776 PyErr_SetString (PyExc_RuntimeError,
777 _("Only PARAM_ENUM accepts a fourth argument."));
780 if (parmclass == var_enum)
782 if (! compute_enum_values (obj, enum_values))
786 obj->enumeration = NULL;
787 obj->type = (enum var_types) parmclass;
788 memset (&obj->value, 0, sizeof (obj->value));
790 if (var_type_uses<std::string> (obj->type))
791 obj->value.stringval = new std::string;
793 gdb::unique_xmalloc_ptr<char> cmd_name
794 = gdbpy_parse_command_name (name, &set_list, &setlist);
795 if (cmd_name == nullptr)
798 cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
799 if (cmd_name == nullptr)
802 set_doc = get_doc_string (self, doc_string_set, name);
803 show_doc = get_doc_string (self, doc_string_show, name);
804 doc = get_doc_string (self, doc_string_description, cmd_name.get ());
810 add_setshow_generic (parmclass, (enum command_class) cmdtype,
811 std::move (cmd_name), obj,
812 set_doc.get (), show_doc.get (),
813 doc.get (), set_list, show_list);
815 catch (const gdb_exception &except)
818 gdbpy_convert_exception (except);
825 /* Deallocate function for a gdb.Parameter. */
828 parmpy_dealloc (PyObject *obj)
830 parmpy_object *parm_obj = (parmpy_object *) obj;
832 if (var_type_uses<std::string> (parm_obj->type))
833 delete parm_obj->value.stringval;
836 /* Initialize the 'parameters' module. */
838 gdbpy_initialize_parameters (void)
842 parmpy_object_type.tp_new = PyType_GenericNew;
843 if (PyType_Ready (&parmpy_object_type) < 0)
846 set_doc_cst = PyString_FromString ("set_doc");
849 show_doc_cst = PyString_FromString ("show_doc");
853 for (i = 0; parm_constants[i].name; ++i)
855 if (PyModule_AddIntConstant (gdb_module,
856 parm_constants[i].name,
857 parm_constants[i].value) < 0)
861 return gdb_pymodule_addobject (gdb_module, "Parameter",
862 (PyObject *) &parmpy_object_type);
867 PyTypeObject parmpy_object_type =
869 PyVarObject_HEAD_INIT (NULL, 0)
870 "gdb.Parameter", /*tp_name*/
871 sizeof (parmpy_object), /*tp_basicsize*/
873 parmpy_dealloc, /*tp_dealloc*/
880 0, /*tp_as_sequence*/
885 get_attr, /*tp_getattro*/
886 set_attr, /*tp_setattro*/
888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
889 "GDB parameter object", /* tp_doc */
892 0, /* tp_richcompare */
893 0, /* tp_weaklistoffset */
901 0, /* tp_descr_get */
902 0, /* tp_descr_set */
903 0, /* tp_dictoffset */
904 parmpy_init, /* tp_init */