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 set_show_commands commands
;
473 commands
= add_setshow_boolean_cmd (cmd_name
.get (), cmdclass
,
474 &self
->value
.boolval
, set_doc
,
475 show_doc
, help_doc
, get_set_value
,
476 get_show_value
, set_list
, show_list
);
480 case var_auto_boolean
:
481 commands
= add_setshow_auto_boolean_cmd (cmd_name
.get (), cmdclass
,
482 &self
->value
.autoboolval
,
483 set_doc
, show_doc
, help_doc
,
484 get_set_value
, get_show_value
,
485 set_list
, show_list
);
489 commands
= add_setshow_uinteger_cmd (cmd_name
.get (), cmdclass
,
490 &self
->value
.uintval
, set_doc
,
491 show_doc
, help_doc
, get_set_value
,
492 get_show_value
, set_list
, show_list
);
496 commands
= add_setshow_integer_cmd (cmd_name
.get (), cmdclass
,
497 &self
->value
.intval
, set_doc
,
498 show_doc
, help_doc
, get_set_value
,
499 get_show_value
, set_list
, show_list
);
503 commands
= add_setshow_string_cmd (cmd_name
.get (), cmdclass
,
504 &self
->value
.stringval
, set_doc
,
505 show_doc
, help_doc
, get_set_value
,
506 get_show_value
, set_list
, show_list
);
509 case var_string_noescape
:
510 commands
= 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
);
517 case var_optional_filename
:
518 commands
= add_setshow_optional_filename_cmd (cmd_name
.get (), cmdclass
,
519 &self
->value
.stringval
,
520 set_doc
, show_doc
, help_doc
,
522 get_show_value
, set_list
,
527 commands
= add_setshow_filename_cmd (cmd_name
.get (), cmdclass
,
528 &self
->value
.stringval
, set_doc
,
529 show_doc
, help_doc
, get_set_value
,
530 get_show_value
, set_list
, show_list
);
534 commands
= add_setshow_zinteger_cmd (cmd_name
.get (), cmdclass
,
535 &self
->value
.intval
, set_doc
,
536 show_doc
, help_doc
, get_set_value
,
537 get_show_value
, set_list
, show_list
);
541 commands
= add_setshow_zuinteger_cmd (cmd_name
.get (), cmdclass
,
542 &self
->value
.uintval
, set_doc
,
543 show_doc
, help_doc
, get_set_value
,
544 get_show_value
, set_list
,
548 case var_zuinteger_unlimited
:
549 commands
= add_setshow_zuinteger_unlimited_cmd (cmd_name
.get (), cmdclass
,
552 help_doc
, get_set_value
,
553 get_show_value
, set_list
,
558 commands
= add_setshow_enum_cmd (cmd_name
.get (), cmdclass
,
560 &self
->value
.cstringval
, set_doc
,
561 show_doc
, help_doc
, get_set_value
,
562 get_show_value
, set_list
, show_list
);
563 /* Initialize the value, just in case. */
564 self
->value
.cstringval
= self
->enumeration
[0];
568 gdb_assert_not_reached ("Unhandled parameter class.");
571 /* Register Python objects in both commands' context. */
572 set_cmd_context (commands
.set
, self
);
573 set_cmd_context (commands
.show
, self
);
575 /* We (unfortunately) currently leak the command name. */
579 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
580 error, with a python exception set. */
582 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
588 PyErr_SetString (PyExc_RuntimeError
,
589 _("An enumeration is required for PARAM_ENUM."));
593 if (! PySequence_Check (enum_values
))
595 PyErr_SetString (PyExc_RuntimeError
,
596 _("The enumeration is not a sequence."));
600 size
= PySequence_Size (enum_values
);
605 PyErr_SetString (PyExc_RuntimeError
,
606 _("The enumeration is empty."));
610 gdb_argv
holder (XCNEWVEC (char *, size
+ 1));
611 char **enumeration
= holder
.get ();
613 for (i
= 0; i
< size
; ++i
)
615 gdbpy_ref
<> item (PySequence_GetItem (enum_values
, i
));
619 if (! gdbpy_is_string (item
.get ()))
621 PyErr_SetString (PyExc_RuntimeError
,
622 _("The enumeration item not a string."));
625 enumeration
[i
] = python_string_to_host_string (item
.get ()).release ();
626 if (enumeration
[i
] == NULL
)
630 self
->enumeration
= const_cast<const char**> (holder
.release ());
634 /* Object initializer; sets up gdb-side structures for command.
636 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
638 NAME is the name of the parameter. It may consist of multiple
639 words, in which case the final word is the name of the new command,
640 and earlier words must be prefix commands.
642 CMDCLASS is the kind of command. It should be one of the COMMAND_*
643 constants defined in the gdb module.
645 PARMCLASS is the type of the parameter. It should be one of the
646 PARAM_* constants defined in the gdb module.
648 If PARMCLASS is PARAM_ENUM, then the final argument should be a
649 collection of strings. These strings are the valid values for this
652 The documentation for the parameter is taken from the doc string
653 for the python class.
655 Returns -1 on error, with a python exception set. */
658 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
660 parmpy_object
*obj
= (parmpy_object
*) self
;
662 gdb::unique_xmalloc_ptr
<char> set_doc
, show_doc
, doc
;
663 int parmclass
, cmdtype
;
664 PyObject
*enum_values
= NULL
;
665 struct cmd_list_element
**set_list
, **show_list
;
667 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
671 if (cmdtype
!= no_class
&& cmdtype
!= class_run
672 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
673 && cmdtype
!= class_files
&& cmdtype
!= class_support
674 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
675 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
676 && cmdtype
!= class_maintenance
)
678 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
682 if (parmclass
!= var_boolean
/* ARI: var_boolean */
683 && parmclass
!= var_auto_boolean
684 && parmclass
!= var_uinteger
&& parmclass
!= var_integer
685 && parmclass
!= var_string
&& parmclass
!= var_string_noescape
686 && parmclass
!= var_optional_filename
&& parmclass
!= var_filename
687 && parmclass
!= var_zinteger
&& parmclass
!= var_zuinteger
688 && parmclass
!= var_zuinteger_unlimited
&& parmclass
!= var_enum
)
690 PyErr_SetString (PyExc_RuntimeError
,
691 _("Invalid parameter class argument."));
695 if (enum_values
&& parmclass
!= var_enum
)
697 PyErr_SetString (PyExc_RuntimeError
,
698 _("Only PARAM_ENUM accepts a fourth argument."));
701 if (parmclass
== var_enum
)
703 if (! compute_enum_values (obj
, enum_values
))
707 obj
->enumeration
= NULL
;
708 obj
->type
= (enum var_types
) parmclass
;
709 memset (&obj
->value
, 0, sizeof (obj
->value
));
711 gdb::unique_xmalloc_ptr
<char> cmd_name
712 = gdbpy_parse_command_name (name
, &set_list
, &setlist
);
713 if (cmd_name
== nullptr)
716 cmd_name
= gdbpy_parse_command_name (name
, &show_list
, &showlist
);
717 if (cmd_name
== nullptr)
720 set_doc
= get_doc_string (self
, set_doc_cst
);
721 show_doc
= get_doc_string (self
, show_doc_cst
);
722 doc
= get_doc_string (self
, gdbpy_doc_cst
);
728 add_setshow_generic (parmclass
, (enum command_class
) cmdtype
,
729 std::move (cmd_name
), obj
,
730 set_doc
.get (), show_doc
.get (),
731 doc
.get (), set_list
, show_list
);
733 catch (const gdb_exception
&except
)
736 gdbpy_convert_exception (except
);
745 /* Initialize the 'parameters' module. */
747 gdbpy_initialize_parameters (void)
751 parmpy_object_type
.tp_new
= PyType_GenericNew
;
752 if (PyType_Ready (&parmpy_object_type
) < 0)
755 set_doc_cst
= PyString_FromString ("set_doc");
758 show_doc_cst
= PyString_FromString ("show_doc");
762 for (i
= 0; parm_constants
[i
].name
; ++i
)
764 if (PyModule_AddIntConstant (gdb_module
,
765 parm_constants
[i
].name
,
766 parm_constants
[i
].value
) < 0)
770 return gdb_pymodule_addobject (gdb_module
, "Parameter",
771 (PyObject
*) &parmpy_object_type
);
776 PyTypeObject parmpy_object_type
=
778 PyVarObject_HEAD_INIT (NULL
, 0)
779 "gdb.Parameter", /*tp_name*/
780 sizeof (parmpy_object
), /*tp_basicsize*/
789 0, /*tp_as_sequence*/
794 get_attr
, /*tp_getattro*/
795 set_attr
, /*tp_setattro*/
797 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
798 "GDB parameter object", /* tp_doc */
801 0, /* tp_richcompare */
802 0, /* tp_weaklistoffset */
810 0, /* tp_descr_get */
811 0, /* tp_descr_set */
812 0, /* tp_dictoffset */
813 parmpy_init
, /* tp_init */