1 /* GDB parameters implemented in Python
3 Copyright (C) 2008-2023 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
:
247 && (self
->type
== var_uinteger
|| self
->type
== var_integer
))
249 else if (value
== Py_None
&& self
->type
== var_zuinteger_unlimited
)
251 else if (!PyLong_Check (value
))
253 PyErr_SetString (PyExc_RuntimeError
,
254 _("The value must be integer."));
257 else 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 (PyUnicode_Check (attr_name
)
316 && ! PyUnicode_CompareWithASCIIString (attr_name
, "value"))
320 PyErr_SetString (PyExc_RuntimeError
,
321 _("Cannot delete a parameter's value."));
324 return set_parameter_value ((parmpy_object
*) obj
, val
);
327 return PyObject_GenericSetAttr (obj
, attr_name
, val
);
330 /* Build up the path to command C, but drop the first component of the
331 command prefix. This is only intended for use with the set/show
332 parameters this file deals with, the first prefix should always be
333 either 'set' or 'show'.
335 As an example, if this full command is 'set prefix_a prefix_b command'
336 this function will return the string 'prefix_a prefix_b command'. */
339 full_cmd_name_without_first_prefix (struct cmd_list_element
*c
)
341 std::vector
<std::string
> components
342 = c
->command_components ();
343 gdb_assert (components
.size () > 1);
344 std::string result
= components
[1];
345 for (int i
= 2; i
< components
.size (); ++i
)
346 result
+= " " + components
[i
];
350 /* The different types of documentation string. */
356 doc_string_description
359 /* A helper function which returns a documentation string for an
362 static gdb::unique_xmalloc_ptr
<char>
363 get_doc_string (PyObject
*object
, enum doc_string_type doc_type
,
364 const char *cmd_name
)
366 gdb::unique_xmalloc_ptr
<char> result
;
368 PyObject
*attr
= nullptr;
374 case doc_string_show
:
377 case doc_string_description
:
378 attr
= gdbpy_doc_cst
;
381 gdb_assert (attr
!= nullptr);
383 if (PyObject_HasAttr (object
, attr
))
385 gdbpy_ref
<> ds_obj (PyObject_GetAttr (object
, attr
));
387 if (ds_obj
!= NULL
&& gdbpy_is_string (ds_obj
.get ()))
389 result
= python_string_to_host_string (ds_obj
.get ());
391 gdbpy_print_stack ();
392 else if (doc_type
== doc_string_description
)
393 result
= gdbpy_fix_doc_string_indentation (std::move (result
));
397 if (result
== nullptr)
399 if (doc_type
== doc_string_description
)
400 result
.reset (xstrdup (_("This command is not documented.")));
403 if (doc_type
== doc_string_show
)
404 result
= xstrprintf (_("Show the current value of '%s'."),
407 result
= xstrprintf (_("Set the current value of '%s'."),
414 /* Helper function which will execute a METHOD in OBJ passing the
415 argument ARG. ARG can be NULL. METHOD should return a Python
416 string. If this function returns NULL, there has been an error and
417 the appropriate exception set. */
418 static gdb::unique_xmalloc_ptr
<char>
419 call_doc_function (PyObject
*obj
, PyObject
*method
, PyObject
*arg
)
421 gdb::unique_xmalloc_ptr
<char> data
;
422 gdbpy_ref
<> result (PyObject_CallMethodObjArgs (obj
, method
, arg
, NULL
));
427 if (gdbpy_is_string (result
.get ()))
429 data
= python_string_to_host_string (result
.get ());
435 PyErr_SetString (PyExc_RuntimeError
,
436 _("Parameter must return a string value."));
443 /* A callback function that is registered against the respective
444 add_setshow_* set_doc prototype. This function calls the Python function
445 "get_set_string" if it exists, which will return a string. That string
446 is then printed. If "get_set_string" does not exist, or returns an
447 empty string, then nothing is printed. */
449 get_set_value (const char *args
, int from_tty
,
450 struct cmd_list_element
*c
)
452 PyObject
*obj
= (PyObject
*) c
->context ();
453 gdb::unique_xmalloc_ptr
<char> set_doc_string
;
455 gdbpy_enter enter_py
;
456 gdbpy_ref
<> set_doc_func (PyUnicode_FromString ("get_set_string"));
458 if (set_doc_func
== NULL
)
460 gdbpy_print_stack ();
464 if (PyObject_HasAttr (obj
, set_doc_func
.get ()))
466 set_doc_string
= call_doc_function (obj
, set_doc_func
.get (), NULL
);
467 if (! set_doc_string
)
468 gdbpy_handle_exception ();
471 const char *str
= set_doc_string
.get ();
472 if (str
!= nullptr && str
[0] != '\0')
473 gdb_printf ("%s\n", str
);
476 /* A callback function that is registered against the respective
477 add_setshow_* show_doc prototype. This function will either call
478 the Python function "get_show_string" or extract the Python
479 attribute "show_doc" and return the contents as a string. If
480 neither exist, insert a string indicating the Parameter is not
483 get_show_value (struct ui_file
*file
, int from_tty
,
484 struct cmd_list_element
*c
,
487 PyObject
*obj
= (PyObject
*) c
->context ();
488 gdb::unique_xmalloc_ptr
<char> show_doc_string
;
490 gdbpy_enter enter_py
;
491 gdbpy_ref
<> show_doc_func (PyUnicode_FromString ("get_show_string"));
493 if (show_doc_func
== NULL
)
495 gdbpy_print_stack ();
499 if (PyObject_HasAttr (obj
, show_doc_func
.get ()))
501 gdbpy_ref
<> val_obj (PyUnicode_FromString (value
));
505 gdbpy_print_stack ();
509 show_doc_string
= call_doc_function (obj
, show_doc_func
.get (),
511 if (! show_doc_string
)
513 gdbpy_print_stack ();
517 gdb_printf (file
, "%s\n", show_doc_string
.get ());
521 /* If there is no 'get_show_string' callback then we want to show
522 something sensible here. In older versions of GDB (< 7.3) we
523 didn't support 'get_show_string', and instead we just made use of
524 GDB's builtin use of the show_doc. However, GDB's builtin
525 show_doc adjustment is not i18n friendly, so, instead, we just
526 print this generic string. */
527 std::string cmd_path
= full_cmd_name_without_first_prefix (c
);
528 gdb_printf (file
, _("The current value of '%s' is \"%s\".\n"),
529 cmd_path
.c_str (), value
);
534 /* A helper function that dispatches to the appropriate add_setshow
537 add_setshow_generic (int parmclass
, enum command_class cmdclass
,
538 gdb::unique_xmalloc_ptr
<char> cmd_name
,
540 const char *set_doc
, const char *show_doc
,
541 const char *help_doc
,
542 struct cmd_list_element
**set_list
,
543 struct cmd_list_element
**show_list
)
545 set_show_commands commands
;
550 commands
= add_setshow_boolean_cmd (cmd_name
.get (), cmdclass
,
551 &self
->value
.boolval
, set_doc
,
552 show_doc
, help_doc
, get_set_value
,
553 get_show_value
, set_list
, show_list
);
557 case var_auto_boolean
:
558 commands
= add_setshow_auto_boolean_cmd (cmd_name
.get (), cmdclass
,
559 &self
->value
.autoboolval
,
560 set_doc
, show_doc
, help_doc
,
561 get_set_value
, get_show_value
,
562 set_list
, show_list
);
566 commands
= add_setshow_uinteger_cmd (cmd_name
.get (), cmdclass
,
567 &self
->value
.uintval
, set_doc
,
568 show_doc
, help_doc
, get_set_value
,
569 get_show_value
, set_list
, show_list
);
573 commands
= add_setshow_integer_cmd (cmd_name
.get (), cmdclass
,
574 &self
->value
.intval
, set_doc
,
575 show_doc
, help_doc
, get_set_value
,
576 get_show_value
, set_list
, show_list
);
580 commands
= add_setshow_string_cmd (cmd_name
.get (), cmdclass
,
581 self
->value
.stringval
, set_doc
,
582 show_doc
, help_doc
, get_set_value
,
583 get_show_value
, set_list
, show_list
);
586 case var_string_noescape
:
587 commands
= add_setshow_string_noescape_cmd (cmd_name
.get (), cmdclass
,
588 self
->value
.stringval
,
589 set_doc
, show_doc
, help_doc
,
590 get_set_value
, get_show_value
,
591 set_list
, show_list
);
594 case var_optional_filename
:
595 commands
= add_setshow_optional_filename_cmd (cmd_name
.get (), cmdclass
,
596 self
->value
.stringval
,
597 set_doc
, show_doc
, help_doc
,
599 get_show_value
, set_list
,
604 commands
= add_setshow_filename_cmd (cmd_name
.get (), cmdclass
,
605 self
->value
.stringval
, set_doc
,
606 show_doc
, help_doc
, get_set_value
,
607 get_show_value
, set_list
, show_list
);
611 commands
= add_setshow_zinteger_cmd (cmd_name
.get (), cmdclass
,
612 &self
->value
.intval
, set_doc
,
613 show_doc
, help_doc
, get_set_value
,
614 get_show_value
, set_list
, show_list
);
618 commands
= add_setshow_zuinteger_cmd (cmd_name
.get (), cmdclass
,
619 &self
->value
.uintval
, set_doc
,
620 show_doc
, help_doc
, get_set_value
,
621 get_show_value
, set_list
,
625 case var_zuinteger_unlimited
:
626 commands
= add_setshow_zuinteger_unlimited_cmd (cmd_name
.get (), cmdclass
,
629 help_doc
, get_set_value
,
630 get_show_value
, set_list
,
635 /* Initialize the value, just in case. */
636 self
->value
.cstringval
= self
->enumeration
[0];
637 commands
= add_setshow_enum_cmd (cmd_name
.get (), cmdclass
,
639 &self
->value
.cstringval
, set_doc
,
640 show_doc
, help_doc
, get_set_value
,
641 get_show_value
, set_list
, show_list
);
645 gdb_assert_not_reached ("Unhandled parameter class.");
648 /* Register Python objects in both commands' context. */
649 commands
.set
->set_context (self
);
650 commands
.show
->set_context (self
);
652 /* We (unfortunately) currently leak the command name. */
656 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
657 error, with a python exception set. */
659 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
665 PyErr_SetString (PyExc_RuntimeError
,
666 _("An enumeration is required for PARAM_ENUM."));
670 if (! PySequence_Check (enum_values
))
672 PyErr_SetString (PyExc_RuntimeError
,
673 _("The enumeration is not a sequence."));
677 size
= PySequence_Size (enum_values
);
682 PyErr_SetString (PyExc_RuntimeError
,
683 _("The enumeration is empty."));
687 gdb_argv
holder (XCNEWVEC (char *, size
+ 1));
688 char **enumeration
= holder
.get ();
690 for (i
= 0; i
< size
; ++i
)
692 gdbpy_ref
<> item (PySequence_GetItem (enum_values
, i
));
696 if (! gdbpy_is_string (item
.get ()))
698 PyErr_SetString (PyExc_RuntimeError
,
699 _("The enumeration item not a string."));
702 enumeration
[i
] = python_string_to_host_string (item
.get ()).release ();
703 if (enumeration
[i
] == NULL
)
707 self
->enumeration
= const_cast<const char**> (holder
.release ());
711 /* Object initializer; sets up gdb-side structures for command.
713 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
715 NAME is the name of the parameter. It may consist of multiple
716 words, in which case the final word is the name of the new command,
717 and earlier words must be prefix commands.
719 CMDCLASS is the kind of command. It should be one of the COMMAND_*
720 constants defined in the gdb module.
722 PARMCLASS is the type of the parameter. It should be one of the
723 PARAM_* constants defined in the gdb module.
725 If PARMCLASS is PARAM_ENUM, then the final argument should be a
726 collection of strings. These strings are the valid values for this
729 The documentation for the parameter is taken from the doc string
730 for the python class.
732 Returns -1 on error, with a python exception set. */
735 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
737 parmpy_object
*obj
= (parmpy_object
*) self
;
739 gdb::unique_xmalloc_ptr
<char> set_doc
, show_doc
, doc
;
740 int parmclass
, cmdtype
;
741 PyObject
*enum_values
= NULL
;
742 struct cmd_list_element
**set_list
, **show_list
;
744 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
748 if (cmdtype
!= no_class
&& cmdtype
!= class_run
749 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
750 && cmdtype
!= class_files
&& cmdtype
!= class_support
751 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
752 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
753 && cmdtype
!= class_maintenance
)
755 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
759 if (parmclass
!= var_boolean
/* ARI: var_boolean */
760 && parmclass
!= var_auto_boolean
761 && parmclass
!= var_uinteger
&& parmclass
!= var_integer
762 && parmclass
!= var_string
&& parmclass
!= var_string_noescape
763 && parmclass
!= var_optional_filename
&& parmclass
!= var_filename
764 && parmclass
!= var_zinteger
&& parmclass
!= var_zuinteger
765 && parmclass
!= var_zuinteger_unlimited
&& parmclass
!= var_enum
)
767 PyErr_SetString (PyExc_RuntimeError
,
768 _("Invalid parameter class argument."));
772 if (enum_values
&& parmclass
!= var_enum
)
774 PyErr_SetString (PyExc_RuntimeError
,
775 _("Only PARAM_ENUM accepts a fourth argument."));
778 if (parmclass
== var_enum
)
780 if (! compute_enum_values (obj
, enum_values
))
784 obj
->enumeration
= NULL
;
785 obj
->type
= (enum var_types
) parmclass
;
786 memset (&obj
->value
, 0, sizeof (obj
->value
));
788 if (var_type_uses
<std::string
> (obj
->type
))
789 obj
->value
.stringval
= new std::string
;
791 gdb::unique_xmalloc_ptr
<char> cmd_name
792 = gdbpy_parse_command_name (name
, &set_list
, &setlist
);
793 if (cmd_name
== nullptr)
796 cmd_name
= gdbpy_parse_command_name (name
, &show_list
, &showlist
);
797 if (cmd_name
== nullptr)
800 set_doc
= get_doc_string (self
, doc_string_set
, name
);
801 show_doc
= get_doc_string (self
, doc_string_show
, name
);
802 doc
= get_doc_string (self
, doc_string_description
, cmd_name
.get ());
808 add_setshow_generic (parmclass
, (enum command_class
) cmdtype
,
809 std::move (cmd_name
), obj
,
810 set_doc
.get (), show_doc
.get (),
811 doc
.get (), set_list
, show_list
);
813 catch (const gdb_exception
&except
)
816 gdbpy_convert_exception (except
);
823 /* Deallocate function for a gdb.Parameter. */
826 parmpy_dealloc (PyObject
*obj
)
828 parmpy_object
*parm_obj
= (parmpy_object
*) obj
;
830 if (var_type_uses
<std::string
> (parm_obj
->type
))
831 delete parm_obj
->value
.stringval
;
834 /* Initialize the 'parameters' module. */
836 gdbpy_initialize_parameters (void)
840 parmpy_object_type
.tp_new
= PyType_GenericNew
;
841 if (PyType_Ready (&parmpy_object_type
) < 0)
844 set_doc_cst
= PyUnicode_FromString ("set_doc");
847 show_doc_cst
= PyUnicode_FromString ("show_doc");
851 for (i
= 0; parm_constants
[i
].name
; ++i
)
853 if (PyModule_AddIntConstant (gdb_module
,
854 parm_constants
[i
].name
,
855 parm_constants
[i
].value
) < 0)
859 return gdb_pymodule_addobject (gdb_module
, "Parameter",
860 (PyObject
*) &parmpy_object_type
);
865 PyTypeObject parmpy_object_type
=
867 PyVarObject_HEAD_INIT (NULL
, 0)
868 "gdb.Parameter", /*tp_name*/
869 sizeof (parmpy_object
), /*tp_basicsize*/
871 parmpy_dealloc
, /*tp_dealloc*/
878 0, /*tp_as_sequence*/
883 get_attr
, /*tp_getattro*/
884 set_attr
, /*tp_setattro*/
886 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
887 "GDB parameter object", /* tp_doc */
890 0, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
899 0, /* tp_descr_get */
900 0, /* tp_descr_set */
901 0, /* tp_dictoffset */
902 parmpy_init
, /* tp_init */