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 (PyUnicode_Check (attr_name)
129 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
131 parmpy_object *self = (parmpy_object *) obj;
133 return gdbpy_parameter_value (make_setting (self));
136 return PyObject_GenericGetAttr (obj, attr_name);
139 /* Set a parameter value from a Python value. Return 0 on success. Returns
140 -1 on error, with a python exception set. */
142 set_parameter_value (parmpy_object *self, PyObject *value)
149 case var_string_noescape:
150 case var_optional_filename:
152 if (! gdbpy_is_string (value)
153 && (self->type == var_filename
154 || value != Py_None))
156 PyErr_SetString (PyExc_RuntimeError,
157 _("String required for filename."));
161 if (value == Py_None)
162 self->value.stringval->clear ();
165 gdb::unique_xmalloc_ptr<char>
166 string (python_string_to_host_string (value));
170 *self->value.stringval = string.get ();
178 if (! gdbpy_is_string (value))
180 PyErr_SetString (PyExc_RuntimeError,
181 _("ENUM arguments must be a string."));
185 gdb::unique_xmalloc_ptr<char>
186 str (python_string_to_host_string (value));
189 for (i = 0; self->enumeration[i]; ++i)
190 if (! strcmp (self->enumeration[i], str.get ()))
192 if (! self->enumeration[i])
194 PyErr_SetString (PyExc_RuntimeError,
195 _("The value must be member of an enumeration."));
198 self->value.cstringval = self->enumeration[i];
203 if (! PyBool_Check (value))
205 PyErr_SetString (PyExc_RuntimeError,
206 _("A boolean argument is required."));
209 cmp = PyObject_IsTrue (value);
212 self->value.boolval = cmp;
215 case var_auto_boolean:
216 if (! PyBool_Check (value) && value != Py_None)
218 PyErr_SetString (PyExc_RuntimeError,
219 _("A boolean or None is required"));
223 if (value == Py_None)
224 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
227 cmp = PyObject_IsTrue (value);
231 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
233 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
241 case var_zuinteger_unlimited:
246 if (!PyLong_Check (value))
248 PyErr_SetString (PyExc_RuntimeError,
249 _("The value must be integer."));
253 if (! gdb_py_int_as_long (value, &l))
263 ok = (l >= 0 && l <= UINT_MAX);
266 case var_zuinteger_unlimited:
267 ok = (l >= -1 && l <= INT_MAX);
271 ok = (l >= INT_MIN && l <= INT_MAX);
277 ok = (l >= INT_MIN && l <= INT_MAX);
281 gdb_assert_not_reached ("unknown var_ constant");
286 PyErr_SetString (PyExc_RuntimeError,
287 _("Range exceeded."));
291 if (self->type == var_uinteger || self->type == var_zuinteger)
292 self->value.uintval = (unsigned) l;
294 self->value.intval = (int) l;
299 PyErr_SetString (PyExc_RuntimeError,
300 _("Unhandled type in parameter value."));
307 /* Set an attribute. Returns -1 on error, with a python exception set. */
309 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
311 if (PyUnicode_Check (attr_name)
312 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
316 PyErr_SetString (PyExc_RuntimeError,
317 _("Cannot delete a parameter's value."));
320 return set_parameter_value ((parmpy_object *) obj, val);
323 return PyObject_GenericSetAttr (obj, attr_name, val);
326 /* Build up the path to command C, but drop the first component of the
327 command prefix. This is only intended for use with the set/show
328 parameters this file deals with, the first prefix should always be
329 either 'set' or 'show'.
331 As an example, if this full command is 'set prefix_a prefix_b command'
332 this function will return the string 'prefix_a prefix_b command'. */
335 full_cmd_name_without_first_prefix (struct cmd_list_element *c)
337 std::vector<std::string> components
338 = c->command_components ();
339 gdb_assert (components.size () > 1);
340 std::string result = components[1];
341 for (int i = 2; i < components.size (); ++i)
342 result += " " + components[i];
346 /* The different types of documentation string. */
352 doc_string_description
355 /* A helper function which returns a documentation string for an
358 static gdb::unique_xmalloc_ptr<char>
359 get_doc_string (PyObject *object, enum doc_string_type doc_type,
360 const char *cmd_name)
362 gdb::unique_xmalloc_ptr<char> result;
364 PyObject *attr = nullptr;
370 case doc_string_show:
373 case doc_string_description:
374 attr = gdbpy_doc_cst;
377 gdb_assert (attr != nullptr);
379 if (PyObject_HasAttr (object, attr))
381 gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
383 if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
385 result = python_string_to_host_string (ds_obj.get ());
387 gdbpy_print_stack ();
391 if (result == nullptr)
393 if (doc_type == doc_string_description)
394 result.reset (xstrdup (_("This command is not documented.")));
397 if (doc_type == doc_string_show)
398 result = xstrprintf (_("Show the current value of '%s'."),
401 result = xstrprintf (_("Set the current value of '%s'."),
408 /* Helper function which will execute a METHOD in OBJ passing the
409 argument ARG. ARG can be NULL. METHOD should return a Python
410 string. If this function returns NULL, there has been an error and
411 the appropriate exception set. */
412 static gdb::unique_xmalloc_ptr<char>
413 call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
415 gdb::unique_xmalloc_ptr<char> data;
416 gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
421 if (gdbpy_is_string (result.get ()))
423 data = python_string_to_host_string (result.get ());
429 PyErr_SetString (PyExc_RuntimeError,
430 _("Parameter must return a string value."));
437 /* A callback function that is registered against the respective
438 add_setshow_* set_doc prototype. This function calls the Python function
439 "get_set_string" if it exists, which will return a string. That string
440 is then printed. If "get_set_string" does not exist, or returns an
441 empty string, then nothing is printed. */
443 get_set_value (const char *args, int from_tty,
444 struct cmd_list_element *c)
446 PyObject *obj = (PyObject *) c->context ();
447 gdb::unique_xmalloc_ptr<char> set_doc_string;
449 gdbpy_enter enter_py;
450 gdbpy_ref<> set_doc_func (PyUnicode_FromString ("get_set_string"));
452 if (set_doc_func == NULL)
454 gdbpy_print_stack ();
458 if (PyObject_HasAttr (obj, set_doc_func.get ()))
460 set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
461 if (! set_doc_string)
462 gdbpy_handle_exception ();
465 const char *str = set_doc_string.get ();
466 if (str != nullptr && str[0] != '\0')
467 printf_filtered ("%s\n", str);
470 /* A callback function that is registered against the respective
471 add_setshow_* show_doc prototype. This function will either call
472 the Python function "get_show_string" or extract the Python
473 attribute "show_doc" and return the contents as a string. If
474 neither exist, insert a string indicating the Parameter is not
477 get_show_value (struct ui_file *file, int from_tty,
478 struct cmd_list_element *c,
481 PyObject *obj = (PyObject *) c->context ();
482 gdb::unique_xmalloc_ptr<char> show_doc_string;
484 gdbpy_enter enter_py;
485 gdbpy_ref<> show_doc_func (PyUnicode_FromString ("get_show_string"));
487 if (show_doc_func == NULL)
489 gdbpy_print_stack ();
493 if (PyObject_HasAttr (obj, show_doc_func.get ()))
495 gdbpy_ref<> val_obj (PyUnicode_FromString (value));
499 gdbpy_print_stack ();
503 show_doc_string = call_doc_function (obj, show_doc_func.get (),
505 if (! show_doc_string)
507 gdbpy_print_stack ();
511 fprintf_filtered (file, "%s\n", show_doc_string.get ());
515 /* If there is no 'get_show_string' callback then we want to show
516 something sensible here. In older versions of GDB (< 7.3) we
517 didn't support 'get_show_string', and instead we just made use of
518 GDB's builtin use of the show_doc. However, GDB's builtin
519 show_doc adjustment is not i18n friendly, so, instead, we just
520 print this generic string. */
521 std::string cmd_path = full_cmd_name_without_first_prefix (c);
522 fprintf_filtered (file, _("The current value of '%s' is \"%s\".\n"),
523 cmd_path.c_str (), value);
528 /* A helper function that dispatches to the appropriate add_setshow
531 add_setshow_generic (int parmclass, enum command_class cmdclass,
532 gdb::unique_xmalloc_ptr<char> cmd_name,
534 const char *set_doc, const char *show_doc,
535 const char *help_doc,
536 struct cmd_list_element **set_list,
537 struct cmd_list_element **show_list)
539 set_show_commands commands;
544 commands = add_setshow_boolean_cmd (cmd_name.get (), cmdclass,
545 &self->value.boolval, set_doc,
546 show_doc, help_doc, get_set_value,
547 get_show_value, set_list, show_list);
551 case var_auto_boolean:
552 commands = add_setshow_auto_boolean_cmd (cmd_name.get (), cmdclass,
553 &self->value.autoboolval,
554 set_doc, show_doc, help_doc,
555 get_set_value, get_show_value,
556 set_list, show_list);
560 commands = add_setshow_uinteger_cmd (cmd_name.get (), cmdclass,
561 &self->value.uintval, set_doc,
562 show_doc, help_doc, get_set_value,
563 get_show_value, set_list, show_list);
567 commands = add_setshow_integer_cmd (cmd_name.get (), cmdclass,
568 &self->value.intval, set_doc,
569 show_doc, help_doc, get_set_value,
570 get_show_value, set_list, show_list);
574 commands = add_setshow_string_cmd (cmd_name.get (), cmdclass,
575 self->value.stringval, set_doc,
576 show_doc, help_doc, get_set_value,
577 get_show_value, set_list, show_list);
580 case var_string_noescape:
581 commands = add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
582 self->value.stringval,
583 set_doc, show_doc, help_doc,
584 get_set_value, get_show_value,
585 set_list, show_list);
588 case var_optional_filename:
589 commands = add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
590 self->value.stringval,
591 set_doc, show_doc, help_doc,
593 get_show_value, set_list,
598 commands = add_setshow_filename_cmd (cmd_name.get (), cmdclass,
599 self->value.stringval, set_doc,
600 show_doc, help_doc, get_set_value,
601 get_show_value, set_list, show_list);
605 commands = add_setshow_zinteger_cmd (cmd_name.get (), cmdclass,
606 &self->value.intval, set_doc,
607 show_doc, help_doc, get_set_value,
608 get_show_value, set_list, show_list);
612 commands = add_setshow_zuinteger_cmd (cmd_name.get (), cmdclass,
613 &self->value.uintval, set_doc,
614 show_doc, help_doc, get_set_value,
615 get_show_value, set_list,
619 case var_zuinteger_unlimited:
620 commands = add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
623 help_doc, get_set_value,
624 get_show_value, set_list,
629 /* Initialize the value, just in case. */
630 self->value.cstringval = self->enumeration[0];
631 commands = add_setshow_enum_cmd (cmd_name.get (), cmdclass,
633 &self->value.cstringval, set_doc,
634 show_doc, help_doc, get_set_value,
635 get_show_value, set_list, show_list);
639 gdb_assert_not_reached ("Unhandled parameter class.");
642 /* Register Python objects in both commands' context. */
643 commands.set->set_context (self);
644 commands.show->set_context (self);
646 /* We (unfortunately) currently leak the command name. */
650 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
651 error, with a python exception set. */
653 compute_enum_values (parmpy_object *self, PyObject *enum_values)
659 PyErr_SetString (PyExc_RuntimeError,
660 _("An enumeration is required for PARAM_ENUM."));
664 if (! PySequence_Check (enum_values))
666 PyErr_SetString (PyExc_RuntimeError,
667 _("The enumeration is not a sequence."));
671 size = PySequence_Size (enum_values);
676 PyErr_SetString (PyExc_RuntimeError,
677 _("The enumeration is empty."));
681 gdb_argv holder (XCNEWVEC (char *, size + 1));
682 char **enumeration = holder.get ();
684 for (i = 0; i < size; ++i)
686 gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
690 if (! gdbpy_is_string (item.get ()))
692 PyErr_SetString (PyExc_RuntimeError,
693 _("The enumeration item not a string."));
696 enumeration[i] = python_string_to_host_string (item.get ()).release ();
697 if (enumeration[i] == NULL)
701 self->enumeration = const_cast<const char**> (holder.release ());
705 /* Object initializer; sets up gdb-side structures for command.
707 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
709 NAME is the name of the parameter. It may consist of multiple
710 words, in which case the final word is the name of the new command,
711 and earlier words must be prefix commands.
713 CMDCLASS is the kind of command. It should be one of the COMMAND_*
714 constants defined in the gdb module.
716 PARMCLASS is the type of the parameter. It should be one of the
717 PARAM_* constants defined in the gdb module.
719 If PARMCLASS is PARAM_ENUM, then the final argument should be a
720 collection of strings. These strings are the valid values for this
723 The documentation for the parameter is taken from the doc string
724 for the python class.
726 Returns -1 on error, with a python exception set. */
729 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
731 parmpy_object *obj = (parmpy_object *) self;
733 gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
734 int parmclass, cmdtype;
735 PyObject *enum_values = NULL;
736 struct cmd_list_element **set_list, **show_list;
738 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
742 if (cmdtype != no_class && cmdtype != class_run
743 && cmdtype != class_vars && cmdtype != class_stack
744 && cmdtype != class_files && cmdtype != class_support
745 && cmdtype != class_info && cmdtype != class_breakpoint
746 && cmdtype != class_trace && cmdtype != class_obscure
747 && cmdtype != class_maintenance)
749 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
753 if (parmclass != var_boolean /* ARI: var_boolean */
754 && parmclass != var_auto_boolean
755 && parmclass != var_uinteger && parmclass != var_integer
756 && parmclass != var_string && parmclass != var_string_noescape
757 && parmclass != var_optional_filename && parmclass != var_filename
758 && parmclass != var_zinteger && parmclass != var_zuinteger
759 && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
761 PyErr_SetString (PyExc_RuntimeError,
762 _("Invalid parameter class argument."));
766 if (enum_values && parmclass != var_enum)
768 PyErr_SetString (PyExc_RuntimeError,
769 _("Only PARAM_ENUM accepts a fourth argument."));
772 if (parmclass == var_enum)
774 if (! compute_enum_values (obj, enum_values))
778 obj->enumeration = NULL;
779 obj->type = (enum var_types) parmclass;
780 memset (&obj->value, 0, sizeof (obj->value));
782 if (var_type_uses<std::string> (obj->type))
783 obj->value.stringval = new std::string;
785 gdb::unique_xmalloc_ptr<char> cmd_name
786 = gdbpy_parse_command_name (name, &set_list, &setlist);
787 if (cmd_name == nullptr)
790 cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
791 if (cmd_name == nullptr)
794 set_doc = get_doc_string (self, doc_string_set, name);
795 show_doc = get_doc_string (self, doc_string_show, name);
796 doc = get_doc_string (self, doc_string_description, cmd_name.get ());
802 add_setshow_generic (parmclass, (enum command_class) cmdtype,
803 std::move (cmd_name), obj,
804 set_doc.get (), show_doc.get (),
805 doc.get (), set_list, show_list);
807 catch (const gdb_exception &except)
810 gdbpy_convert_exception (except);
817 /* Deallocate function for a gdb.Parameter. */
820 parmpy_dealloc (PyObject *obj)
822 parmpy_object *parm_obj = (parmpy_object *) obj;
824 if (var_type_uses<std::string> (parm_obj->type))
825 delete parm_obj->value.stringval;
828 /* Initialize the 'parameters' module. */
830 gdbpy_initialize_parameters (void)
834 parmpy_object_type.tp_new = PyType_GenericNew;
835 if (PyType_Ready (&parmpy_object_type) < 0)
838 set_doc_cst = PyUnicode_FromString ("set_doc");
841 show_doc_cst = PyUnicode_FromString ("show_doc");
845 for (i = 0; parm_constants[i].name; ++i)
847 if (PyModule_AddIntConstant (gdb_module,
848 parm_constants[i].name,
849 parm_constants[i].value) < 0)
853 return gdb_pymodule_addobject (gdb_module, "Parameter",
854 (PyObject *) &parmpy_object_type);
859 PyTypeObject parmpy_object_type =
861 PyVarObject_HEAD_INIT (NULL, 0)
862 "gdb.Parameter", /*tp_name*/
863 sizeof (parmpy_object), /*tp_basicsize*/
865 parmpy_dealloc, /*tp_dealloc*/
872 0, /*tp_as_sequence*/
877 get_attr, /*tp_getattro*/
878 set_attr, /*tp_setattro*/
880 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
881 "GDB parameter object", /* tp_doc */
884 0, /* tp_richcompare */
885 0, /* tp_weaklistoffset */
893 0, /* tp_descr_get */
894 0, /* tp_descr_set */
895 0, /* tp_dictoffset */
896 parmpy_init, /* tp_init */