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 /* A helper function which returns a documentation string for an
337 static gdb::unique_xmalloc_ptr
<char>
338 get_doc_string (PyObject
*object
, PyObject
*attr
)
340 gdb::unique_xmalloc_ptr
<char> result
;
342 if (PyObject_HasAttr (object
, attr
))
344 gdbpy_ref
<> ds_obj (PyObject_GetAttr (object
, attr
));
346 if (ds_obj
!= NULL
&& gdbpy_is_string (ds_obj
.get ()))
348 result
= python_string_to_host_string (ds_obj
.get ());
350 gdbpy_print_stack ();
354 result
.reset (xstrdup (_("This command is not documented.")));
358 /* Helper function which will execute a METHOD in OBJ passing the
359 argument ARG. ARG can be NULL. METHOD should return a Python
360 string. If this function returns NULL, there has been an error and
361 the appropriate exception set. */
362 static gdb::unique_xmalloc_ptr
<char>
363 call_doc_function (PyObject
*obj
, PyObject
*method
, PyObject
*arg
)
365 gdb::unique_xmalloc_ptr
<char> data
;
366 gdbpy_ref
<> result (PyObject_CallMethodObjArgs (obj
, method
, arg
, NULL
));
371 if (gdbpy_is_string (result
.get ()))
373 data
= python_string_to_host_string (result
.get ());
379 PyErr_SetString (PyExc_RuntimeError
,
380 _("Parameter must return a string value."));
387 /* A callback function that is registered against the respective
388 add_setshow_* set_doc prototype. This function calls the Python function
389 "get_set_string" if it exists, which will return a string. That string
390 is then printed. If "get_set_string" does not exist, or returns an
391 empty string, then nothing is printed. */
393 get_set_value (const char *args
, int from_tty
,
394 struct cmd_list_element
*c
)
396 PyObject
*obj
= (PyObject
*) c
->context ();
397 gdb::unique_xmalloc_ptr
<char> set_doc_string
;
399 gdbpy_enter
enter_py (get_current_arch (), current_language
);
400 gdbpy_ref
<> set_doc_func (PyString_FromString ("get_set_string"));
402 if (set_doc_func
== NULL
)
404 gdbpy_print_stack ();
408 if (PyObject_HasAttr (obj
, set_doc_func
.get ()))
410 set_doc_string
= call_doc_function (obj
, set_doc_func
.get (), NULL
);
411 if (! set_doc_string
)
412 gdbpy_handle_exception ();
415 const char *str
= set_doc_string
.get ();
416 if (str
!= nullptr && str
[0] != '\0')
417 fprintf_filtered (gdb_stdout
, "%s\n", str
);
420 /* A callback function that is registered against the respective
421 add_setshow_* show_doc prototype. This function will either call
422 the Python function "get_show_string" or extract the Python
423 attribute "show_doc" and return the contents as a string. If
424 neither exist, insert a string indicating the Parameter is not
427 get_show_value (struct ui_file
*file
, int from_tty
,
428 struct cmd_list_element
*c
,
431 PyObject
*obj
= (PyObject
*) c
->context ();
432 gdb::unique_xmalloc_ptr
<char> show_doc_string
;
434 gdbpy_enter
enter_py (get_current_arch (), current_language
);
435 gdbpy_ref
<> show_doc_func (PyString_FromString ("get_show_string"));
437 if (show_doc_func
== NULL
)
439 gdbpy_print_stack ();
443 if (PyObject_HasAttr (obj
, show_doc_func
.get ()))
445 gdbpy_ref
<> val_obj (PyString_FromString (value
));
449 gdbpy_print_stack ();
453 show_doc_string
= call_doc_function (obj
, show_doc_func
.get (),
455 if (! show_doc_string
)
457 gdbpy_print_stack ();
461 fprintf_filtered (file
, "%s\n", show_doc_string
.get ());
465 /* We have to preserve the existing < GDB 7.3 API. If a
466 callback function does not exist, then attempt to read the
467 show_doc attribute. */
468 show_doc_string
= get_doc_string (obj
, show_doc_cst
);
469 fprintf_filtered (file
, "%s %s\n", show_doc_string
.get (), value
);
474 /* A helper function that dispatches to the appropriate add_setshow
477 add_setshow_generic (int parmclass
, enum command_class cmdclass
,
478 gdb::unique_xmalloc_ptr
<char> cmd_name
,
480 const char *set_doc
, const char *show_doc
,
481 const char *help_doc
,
482 struct cmd_list_element
**set_list
,
483 struct cmd_list_element
**show_list
)
485 set_show_commands commands
;
490 commands
= add_setshow_boolean_cmd (cmd_name
.get (), cmdclass
,
491 &self
->value
.boolval
, set_doc
,
492 show_doc
, help_doc
, get_set_value
,
493 get_show_value
, set_list
, show_list
);
497 case var_auto_boolean
:
498 commands
= add_setshow_auto_boolean_cmd (cmd_name
.get (), cmdclass
,
499 &self
->value
.autoboolval
,
500 set_doc
, show_doc
, help_doc
,
501 get_set_value
, get_show_value
,
502 set_list
, show_list
);
506 commands
= add_setshow_uinteger_cmd (cmd_name
.get (), cmdclass
,
507 &self
->value
.uintval
, set_doc
,
508 show_doc
, help_doc
, get_set_value
,
509 get_show_value
, set_list
, show_list
);
513 commands
= add_setshow_integer_cmd (cmd_name
.get (), cmdclass
,
514 &self
->value
.intval
, set_doc
,
515 show_doc
, help_doc
, get_set_value
,
516 get_show_value
, set_list
, show_list
);
520 commands
= add_setshow_string_cmd (cmd_name
.get (), cmdclass
,
521 self
->value
.stringval
, set_doc
,
522 show_doc
, help_doc
, get_set_value
,
523 get_show_value
, set_list
, show_list
);
526 case var_string_noescape
:
527 commands
= add_setshow_string_noescape_cmd (cmd_name
.get (), cmdclass
,
528 self
->value
.stringval
,
529 set_doc
, show_doc
, help_doc
,
530 get_set_value
, get_show_value
,
531 set_list
, show_list
);
534 case var_optional_filename
:
535 commands
= add_setshow_optional_filename_cmd (cmd_name
.get (), cmdclass
,
536 self
->value
.stringval
,
537 set_doc
, show_doc
, help_doc
,
539 get_show_value
, set_list
,
544 commands
= add_setshow_filename_cmd (cmd_name
.get (), cmdclass
,
545 self
->value
.stringval
, set_doc
,
546 show_doc
, help_doc
, get_set_value
,
547 get_show_value
, set_list
, show_list
);
551 commands
= add_setshow_zinteger_cmd (cmd_name
.get (), cmdclass
,
552 &self
->value
.intval
, set_doc
,
553 show_doc
, help_doc
, get_set_value
,
554 get_show_value
, set_list
, show_list
);
558 commands
= add_setshow_zuinteger_cmd (cmd_name
.get (), cmdclass
,
559 &self
->value
.uintval
, set_doc
,
560 show_doc
, help_doc
, get_set_value
,
561 get_show_value
, set_list
,
565 case var_zuinteger_unlimited
:
566 commands
= add_setshow_zuinteger_unlimited_cmd (cmd_name
.get (), cmdclass
,
569 help_doc
, get_set_value
,
570 get_show_value
, set_list
,
575 commands
= add_setshow_enum_cmd (cmd_name
.get (), cmdclass
,
577 &self
->value
.cstringval
, set_doc
,
578 show_doc
, help_doc
, get_set_value
,
579 get_show_value
, set_list
, show_list
);
580 /* Initialize the value, just in case. */
581 self
->value
.cstringval
= self
->enumeration
[0];
585 gdb_assert_not_reached ("Unhandled parameter class.");
588 /* Register Python objects in both commands' context. */
589 commands
.set
->set_context (self
);
590 commands
.show
->set_context (self
);
592 /* We (unfortunately) currently leak the command name. */
596 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
597 error, with a python exception set. */
599 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
605 PyErr_SetString (PyExc_RuntimeError
,
606 _("An enumeration is required for PARAM_ENUM."));
610 if (! PySequence_Check (enum_values
))
612 PyErr_SetString (PyExc_RuntimeError
,
613 _("The enumeration is not a sequence."));
617 size
= PySequence_Size (enum_values
);
622 PyErr_SetString (PyExc_RuntimeError
,
623 _("The enumeration is empty."));
627 gdb_argv
holder (XCNEWVEC (char *, size
+ 1));
628 char **enumeration
= holder
.get ();
630 for (i
= 0; i
< size
; ++i
)
632 gdbpy_ref
<> item (PySequence_GetItem (enum_values
, i
));
636 if (! gdbpy_is_string (item
.get ()))
638 PyErr_SetString (PyExc_RuntimeError
,
639 _("The enumeration item not a string."));
642 enumeration
[i
] = python_string_to_host_string (item
.get ()).release ();
643 if (enumeration
[i
] == NULL
)
647 self
->enumeration
= const_cast<const char**> (holder
.release ());
651 /* Object initializer; sets up gdb-side structures for command.
653 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
655 NAME is the name of the parameter. It may consist of multiple
656 words, in which case the final word is the name of the new command,
657 and earlier words must be prefix commands.
659 CMDCLASS is the kind of command. It should be one of the COMMAND_*
660 constants defined in the gdb module.
662 PARMCLASS is the type of the parameter. It should be one of the
663 PARAM_* constants defined in the gdb module.
665 If PARMCLASS is PARAM_ENUM, then the final argument should be a
666 collection of strings. These strings are the valid values for this
669 The documentation for the parameter is taken from the doc string
670 for the python class.
672 Returns -1 on error, with a python exception set. */
675 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
677 parmpy_object
*obj
= (parmpy_object
*) self
;
679 gdb::unique_xmalloc_ptr
<char> set_doc
, show_doc
, doc
;
680 int parmclass
, cmdtype
;
681 PyObject
*enum_values
= NULL
;
682 struct cmd_list_element
**set_list
, **show_list
;
684 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
688 if (cmdtype
!= no_class
&& cmdtype
!= class_run
689 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
690 && cmdtype
!= class_files
&& cmdtype
!= class_support
691 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
692 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
693 && cmdtype
!= class_maintenance
)
695 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
699 if (parmclass
!= var_boolean
/* ARI: var_boolean */
700 && parmclass
!= var_auto_boolean
701 && parmclass
!= var_uinteger
&& parmclass
!= var_integer
702 && parmclass
!= var_string
&& parmclass
!= var_string_noescape
703 && parmclass
!= var_optional_filename
&& parmclass
!= var_filename
704 && parmclass
!= var_zinteger
&& parmclass
!= var_zuinteger
705 && parmclass
!= var_zuinteger_unlimited
&& parmclass
!= var_enum
)
707 PyErr_SetString (PyExc_RuntimeError
,
708 _("Invalid parameter class argument."));
712 if (enum_values
&& parmclass
!= var_enum
)
714 PyErr_SetString (PyExc_RuntimeError
,
715 _("Only PARAM_ENUM accepts a fourth argument."));
718 if (parmclass
== var_enum
)
720 if (! compute_enum_values (obj
, enum_values
))
724 obj
->enumeration
= NULL
;
725 obj
->type
= (enum var_types
) parmclass
;
726 memset (&obj
->value
, 0, sizeof (obj
->value
));
728 if (var_type_uses
<std::string
> (obj
->type
))
729 obj
->value
.stringval
= new std::string
;
731 gdb::unique_xmalloc_ptr
<char> cmd_name
732 = gdbpy_parse_command_name (name
, &set_list
, &setlist
);
733 if (cmd_name
== nullptr)
736 cmd_name
= gdbpy_parse_command_name (name
, &show_list
, &showlist
);
737 if (cmd_name
== nullptr)
740 set_doc
= get_doc_string (self
, set_doc_cst
);
741 show_doc
= get_doc_string (self
, show_doc_cst
);
742 doc
= get_doc_string (self
, gdbpy_doc_cst
);
748 add_setshow_generic (parmclass
, (enum command_class
) cmdtype
,
749 std::move (cmd_name
), obj
,
750 set_doc
.get (), show_doc
.get (),
751 doc
.get (), set_list
, show_list
);
753 catch (const gdb_exception
&except
)
756 gdbpy_convert_exception (except
);
763 /* Deallocate function for a gdb.Parameter. */
766 parmpy_dealloc (PyObject
*obj
)
768 parmpy_object
*parm_obj
= (parmpy_object
*) obj
;
770 if (var_type_uses
<std::string
> (parm_obj
->type
))
771 delete parm_obj
->value
.stringval
;
774 /* Initialize the 'parameters' module. */
776 gdbpy_initialize_parameters (void)
780 parmpy_object_type
.tp_new
= PyType_GenericNew
;
781 if (PyType_Ready (&parmpy_object_type
) < 0)
784 set_doc_cst
= PyString_FromString ("set_doc");
787 show_doc_cst
= PyString_FromString ("show_doc");
791 for (i
= 0; parm_constants
[i
].name
; ++i
)
793 if (PyModule_AddIntConstant (gdb_module
,
794 parm_constants
[i
].name
,
795 parm_constants
[i
].value
) < 0)
799 return gdb_pymodule_addobject (gdb_module
, "Parameter",
800 (PyObject
*) &parmpy_object_type
);
805 PyTypeObject parmpy_object_type
=
807 PyVarObject_HEAD_INIT (NULL
, 0)
808 "gdb.Parameter", /*tp_name*/
809 sizeof (parmpy_object
), /*tp_basicsize*/
811 parmpy_dealloc
, /*tp_dealloc*/
818 0, /*tp_as_sequence*/
823 get_attr
, /*tp_getattro*/
824 set_attr
, /*tp_setattro*/
826 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
827 "GDB parameter object", /* tp_doc */
830 0, /* tp_richcompare */
831 0, /* tp_weaklistoffset */
839 0, /* tp_descr_get */
840 0, /* tp_descr_set */
841 0, /* tp_dictoffset */
842 parmpy_init
, /* tp_init */