1 /* GDB parameters implemented in Python
3 Copyright (C) 2008-2025 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/>. */
22 #include "python-internal.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "completer.h"
28 #include "arch-utils.h"
31 /* Python parameter types as in PARM_CONSTANTS below. */
40 param_string_noescape
,
41 param_optional_filename
,
45 param_zuinteger_unlimited
,
50 /* Translation from Python parameters to GDB variable types. Keep in the
51 same order as PARAM_TYPES due to C++'s lack of designated initializers. */
55 /* The type of the parameter. */
58 /* Extra literals, such as `unlimited', accepted in lieu of a number. */
59 const literal_def
*extra_literals
;
65 { var_uinteger
, uinteger_unlimited_literals
},
66 { var_integer
, integer_unlimited_literals
},
68 { var_string_noescape
},
69 { var_optional_filename
},
73 { var_pinteger
, pinteger_unlimited_literals
},
78 /* Parameter constants and their values. */
84 { "PARAM_BOOLEAN", param_boolean
}, /* ARI: param_boolean */
85 { "PARAM_AUTO_BOOLEAN", param_auto_boolean
},
86 { "PARAM_UINTEGER", param_uinteger
},
87 { "PARAM_INTEGER", param_integer
},
88 { "PARAM_STRING", param_string
},
89 { "PARAM_STRING_NOESCAPE", param_string_noescape
},
90 { "PARAM_OPTIONAL_FILENAME", param_optional_filename
},
91 { "PARAM_FILENAME", param_filename
},
92 { "PARAM_ZINTEGER", param_zinteger
},
93 { "PARAM_ZUINTEGER", param_zuinteger
},
94 { "PARAM_ZUINTEGER_UNLIMITED", param_zuinteger_unlimited
},
95 { "PARAM_ENUM", param_enum
},
96 { "PARAM_COLOR", param_color
},
100 /* A union that can hold anything described by enum var_types. */
101 union parmpy_variable
103 /* Hold a boolean value. */
106 /* Hold an integer value. */
109 /* Hold an auto_boolean. */
110 enum auto_boolean autoboolval
;
112 /* Hold an unsigned integer value, for uinteger. */
113 unsigned int uintval
;
115 /* Hold a string, for the various string types. The std::string is
117 std::string
*stringval
;
119 /* Hold a string, for enums. */
120 const char *cstringval
;
123 ui_file_style::color color
;
126 /* A GDB parameter. */
131 /* The type of the parameter. */
134 /* Extra literals, such as `unlimited', accepted in lieu of a number. */
135 const literal_def
*extra_literals
;
137 /* The value of the parameter. */
138 union parmpy_variable value
;
140 /* For an enum command, the possible values. The vector is
141 allocated with xmalloc, as is each element. It is
143 const char **enumeration
;
146 /* Wraps a setting around an existing parmpy_object. This abstraction
147 is used to manipulate the value in S->VALUE in a type safe manner using
148 the setting interface. */
151 make_setting (parmpy_object
*s
)
153 enum var_types type
= s
->type
;
155 if (var_type_uses
<bool> (type
))
156 return setting (type
, &s
->value
.boolval
);
157 else if (var_type_uses
<int> (type
))
158 return setting (type
, &s
->value
.intval
, s
->extra_literals
);
159 else if (var_type_uses
<auto_boolean
> (type
))
160 return setting (type
, &s
->value
.autoboolval
);
161 else if (var_type_uses
<unsigned int> (type
))
162 return setting (type
, &s
->value
.uintval
, s
->extra_literals
);
163 else if (var_type_uses
<std::string
> (type
))
164 return setting (type
, s
->value
.stringval
);
165 else if (var_type_uses
<const char *> (type
))
166 return setting (type
, &s
->value
.cstringval
);
167 else if (var_type_uses
<ui_file_style::color
> (s
->type
))
168 return setting (s
->type
, &s
->value
.color
);
170 gdb_assert_not_reached ("unhandled var type");
173 extern PyTypeObject parmpy_object_type
174 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
176 /* Some handy string constants. */
177 static PyObject
*set_doc_cst
;
178 static PyObject
*show_doc_cst
;
182 /* Get an attribute. */
184 get_attr (PyObject
*obj
, PyObject
*attr_name
)
186 if (PyUnicode_Check (attr_name
)
187 && ! PyUnicode_CompareWithASCIIString (attr_name
, "value"))
189 parmpy_object
*self
= (parmpy_object
*) obj
;
191 return gdbpy_parameter_value (make_setting (self
));
194 return PyObject_GenericGetAttr (obj
, attr_name
);
197 /* Set a parameter value from a Python value. Return 0 on success. Returns
198 -1 on error, with a python exception set. */
200 set_parameter_value (parmpy_object
*self
, PyObject
*value
)
207 case var_string_noescape
:
208 case var_optional_filename
:
210 if (! gdbpy_is_string (value
)
211 && (self
->type
== var_filename
212 || value
!= Py_None
))
214 PyErr_SetString (PyExc_RuntimeError
,
215 _("String required for filename."));
219 if (value
== Py_None
)
220 self
->value
.stringval
->clear ();
223 gdb::unique_xmalloc_ptr
<char>
224 string (python_string_to_host_string (value
));
228 *self
->value
.stringval
= string
.get ();
236 if (! gdbpy_is_string (value
))
238 PyErr_SetString (PyExc_RuntimeError
,
239 _("ENUM arguments must be a string."));
243 gdb::unique_xmalloc_ptr
<char>
244 str (python_string_to_host_string (value
));
247 for (i
= 0; self
->enumeration
[i
]; ++i
)
248 if (! strcmp (self
->enumeration
[i
], str
.get ()))
250 if (! self
->enumeration
[i
])
252 PyErr_SetString (PyExc_RuntimeError
,
253 _("The value must be member of an enumeration."));
256 self
->value
.cstringval
= self
->enumeration
[i
];
262 if (gdbpy_is_color (value
))
263 self
->value
.color
= gdbpy_get_color (value
);
266 PyErr_SetString (PyExc_RuntimeError
,
267 _("color argument must be a gdb.Color object."));
274 if (! PyBool_Check (value
))
276 PyErr_SetString (PyExc_RuntimeError
,
277 _("A boolean argument is required."));
280 cmp
= PyObject_IsTrue (value
);
283 self
->value
.boolval
= cmp
;
286 case var_auto_boolean
:
287 if (! PyBool_Check (value
) && value
!= Py_None
)
289 PyErr_SetString (PyExc_RuntimeError
,
290 _("A boolean or None is required"));
294 if (value
== Py_None
)
295 self
->value
.autoboolval
= AUTO_BOOLEAN_AUTO
;
298 cmp
= PyObject_IsTrue (value
);
302 self
->value
.autoboolval
= AUTO_BOOLEAN_TRUE
;
304 self
->value
.autoboolval
= AUTO_BOOLEAN_FALSE
;
312 const literal_def
*extra_literals
= self
->extra_literals
;
313 enum tribool allowed
= TRIBOOL_UNKNOWN
;
314 enum var_types var_type
= self
->type
;
315 std::string buffer
= "";
319 if (extra_literals
!= nullptr)
321 gdb::unique_xmalloc_ptr
<char>
322 str (python_string_to_host_string (value
));
323 const char *s
= str
!= nullptr ? str
.get () : nullptr;
326 for (const literal_def
*l
= extra_literals
;
327 l
->literal
!= nullptr;
332 buffer
= buffer
+ "'" + l
->literal
+ "'";
333 if (allowed
== TRIBOOL_UNKNOWN
334 && ((value
== Py_None
&& !strcmp ("unlimited", l
->literal
))
335 || (s
!= nullptr && !strcmp (s
, l
->literal
))))
338 allowed
= TRIBOOL_TRUE
;
343 if (allowed
== TRIBOOL_UNKNOWN
)
345 val
= PyLong_AsLongLong (value
);
347 if (PyErr_Occurred ())
349 if (extra_literals
== nullptr)
350 PyErr_SetString (PyExc_RuntimeError
,
351 _("The value must be integer."));
353 PyErr_SetString (PyExc_RuntimeError
,
354 string_printf (_("integer or one of: %s"),
355 buffer
.c_str ()).c_str ());
357 PyErr_SetString (PyExc_RuntimeError
,
358 string_printf (_("integer or %s"),
359 buffer
.c_str ()).c_str ());
364 if (extra_literals
!= nullptr)
365 for (const literal_def
*l
= extra_literals
;
366 l
->literal
!= nullptr;
369 if (l
->val
.has_value () && val
== *l
->val
)
371 allowed
= TRIBOOL_TRUE
;
375 else if (val
== l
->use
)
376 allowed
= TRIBOOL_FALSE
;
380 if (allowed
== TRIBOOL_UNKNOWN
)
382 if (val
> UINT_MAX
|| val
< INT_MIN
383 || (var_type
== var_uinteger
&& val
< 0)
384 || (var_type
== var_integer
&& val
> INT_MAX
)
385 || (var_type
== var_pinteger
&& val
< 0)
386 || (var_type
== var_pinteger
&& val
> INT_MAX
))
387 allowed
= TRIBOOL_FALSE
;
389 if (allowed
== TRIBOOL_FALSE
)
391 PyErr_SetString (PyExc_RuntimeError
,
392 _("Range exceeded."));
396 if (self
->type
== var_uinteger
)
397 self
->value
.uintval
= (unsigned) val
;
399 self
->value
.intval
= (int) val
;
404 PyErr_SetString (PyExc_RuntimeError
,
405 _("Unhandled type in parameter value."));
412 /* Set an attribute. Returns -1 on error, with a python exception set. */
414 set_attr (PyObject
*obj
, PyObject
*attr_name
, PyObject
*val
)
416 if (PyUnicode_Check (attr_name
)
417 && ! PyUnicode_CompareWithASCIIString (attr_name
, "value"))
421 PyErr_SetString (PyExc_RuntimeError
,
422 _("Cannot delete a parameter's value."));
425 return set_parameter_value ((parmpy_object
*) obj
, val
);
428 return PyObject_GenericSetAttr (obj
, attr_name
, val
);
431 /* Build up the path to command C, but drop the first component of the
432 command prefix. This is only intended for use with the set/show
433 parameters this file deals with, the first prefix should always be
434 either 'set' or 'show'.
436 As an example, if this full command is 'set prefix_a prefix_b command'
437 this function will return the string 'prefix_a prefix_b command'. */
440 full_cmd_name_without_first_prefix (struct cmd_list_element
*c
)
442 std::vector
<std::string
> components
443 = c
->command_components ();
444 gdb_assert (components
.size () > 1);
445 std::string result
= components
[1];
446 for (int i
= 2; i
< components
.size (); ++i
)
447 result
+= " " + components
[i
];
451 /* The different types of documentation string. */
457 doc_string_description
460 /* A helper function which returns a documentation string for an
463 static gdb::unique_xmalloc_ptr
<char>
464 get_doc_string (PyObject
*object
, enum doc_string_type doc_type
,
465 const char *cmd_name
)
467 gdb::unique_xmalloc_ptr
<char> result
;
469 PyObject
*attr
= nullptr;
475 case doc_string_show
:
478 case doc_string_description
:
479 attr
= gdbpy_doc_cst
;
482 gdb_assert (attr
!= nullptr);
484 if (PyObject_HasAttr (object
, attr
))
486 gdbpy_ref
<> ds_obj (PyObject_GetAttr (object
, attr
));
488 if (ds_obj
!= NULL
&& gdbpy_is_string (ds_obj
.get ()))
490 result
= python_string_to_host_string (ds_obj
.get ());
492 gdbpy_print_stack ();
493 else if (doc_type
== doc_string_description
)
494 result
= gdbpy_fix_doc_string_indentation (std::move (result
));
498 /* For the set/show docs, if these strings are empty then we set then to
499 a non-empty string. This ensures that the command has some sane
500 documentation for its 'help' text. */
501 if (result
== nullptr
502 || (doc_type
!= doc_string_description
&& *result
== '\0'))
504 if (doc_type
== doc_string_description
)
505 result
.reset (xstrdup (_("This command is not documented.")));
508 if (doc_type
== doc_string_show
)
509 result
= xstrprintf (_("Show the current value of '%s'."),
512 result
= xstrprintf (_("Set the current value of '%s'."),
519 /* Helper function which will execute a METHOD in OBJ passing the
520 argument ARG. ARG can be NULL. METHOD should return a Python
521 string. If this function returns NULL, there has been an error and
522 the appropriate exception set. */
523 static gdb::unique_xmalloc_ptr
<char>
524 call_doc_function (PyObject
*obj
, PyObject
*method
, PyObject
*arg
)
526 gdb::unique_xmalloc_ptr
<char> data
;
527 gdbpy_ref
<> result (PyObject_CallMethodObjArgs (obj
, method
, arg
, NULL
));
532 if (gdbpy_is_string (result
.get ()))
534 data
= python_string_to_host_string (result
.get ());
540 PyErr_SetString (PyExc_RuntimeError
,
541 _("Parameter must return a string value."));
548 /* A callback function that is registered against the respective
549 add_setshow_* set_doc prototype. This function calls the Python function
550 "get_set_string" if it exists, which will return a string. That string
551 is then printed. If "get_set_string" does not exist, or returns an
552 empty string, then nothing is printed. */
554 get_set_value (const char *args
, int from_tty
,
555 struct cmd_list_element
*c
)
557 PyObject
*obj
= (PyObject
*) c
->context ();
558 gdb::unique_xmalloc_ptr
<char> set_doc_string
;
560 gdbpy_enter enter_py
;
561 gdbpy_ref
<> set_doc_func (PyUnicode_FromString ("get_set_string"));
563 if (set_doc_func
== NULL
)
565 gdbpy_print_stack ();
569 if (PyObject_HasAttr (obj
, set_doc_func
.get ()))
571 set_doc_string
= call_doc_function (obj
, set_doc_func
.get (), NULL
);
572 if (! set_doc_string
)
573 gdbpy_handle_exception ();
576 const char *str
= set_doc_string
.get ();
577 if (str
!= nullptr && str
[0] != '\0')
578 gdb_printf ("%s\n", str
);
581 /* A callback function that is registered against the respective
582 add_setshow_* show_doc prototype. This function will either call
583 the Python function "get_show_string" or extract the Python
584 attribute "show_doc" and return the contents as a string. If
585 neither exist, insert a string indicating the Parameter is not
588 get_show_value (struct ui_file
*file
, int from_tty
,
589 struct cmd_list_element
*c
,
592 PyObject
*obj
= (PyObject
*) c
->context ();
593 gdb::unique_xmalloc_ptr
<char> show_doc_string
;
595 gdbpy_enter enter_py
;
596 gdbpy_ref
<> show_doc_func (PyUnicode_FromString ("get_show_string"));
598 if (show_doc_func
== NULL
)
600 gdbpy_print_stack ();
604 if (PyObject_HasAttr (obj
, show_doc_func
.get ()))
606 gdbpy_ref
<> val_obj (PyUnicode_FromString (value
));
610 gdbpy_print_stack ();
614 show_doc_string
= call_doc_function (obj
, show_doc_func
.get (),
616 if (! show_doc_string
)
618 gdbpy_print_stack ();
622 gdb_printf (file
, "%s\n", show_doc_string
.get ());
626 /* If there is no 'get_show_string' callback then we want to show
627 something sensible here. In older versions of GDB (< 7.3) we
628 didn't support 'get_show_string', and instead we just made use of
629 GDB's builtin use of the show_doc. However, GDB's builtin
630 show_doc adjustment is not i18n friendly, so, instead, we just
631 print this generic string. */
632 std::string cmd_path
= full_cmd_name_without_first_prefix (c
);
633 gdb_printf (file
, _("The current value of '%s' is \"%s\".\n"),
634 cmd_path
.c_str (), value
);
639 /* A helper function that dispatches to the appropriate add_setshow
642 add_setshow_generic (enum var_types type
, const literal_def
*extra_literals
,
643 enum command_class cmdclass
,
644 gdb::unique_xmalloc_ptr
<char> cmd_name
,
646 const char *set_doc
, const char *show_doc
,
647 const char *help_doc
,
648 struct cmd_list_element
**set_list
,
649 struct cmd_list_element
**show_list
)
651 set_show_commands commands
;
656 commands
= add_setshow_boolean_cmd (cmd_name
.get (), cmdclass
,
657 &self
->value
.boolval
, set_doc
,
658 show_doc
, help_doc
, get_set_value
,
659 get_show_value
, set_list
, show_list
);
663 case var_auto_boolean
:
664 commands
= add_setshow_auto_boolean_cmd (cmd_name
.get (), cmdclass
,
665 &self
->value
.autoboolval
,
666 set_doc
, show_doc
, help_doc
,
667 get_set_value
, get_show_value
,
668 set_list
, show_list
);
672 commands
= add_setshow_uinteger_cmd (cmd_name
.get (), cmdclass
,
673 &self
->value
.uintval
,
674 extra_literals
, set_doc
,
675 show_doc
, help_doc
, get_set_value
,
676 get_show_value
, set_list
, show_list
);
680 commands
= add_setshow_integer_cmd (cmd_name
.get (), cmdclass
,
682 extra_literals
, set_doc
,
683 show_doc
, help_doc
, get_set_value
,
684 get_show_value
, set_list
, show_list
);
688 commands
= add_setshow_pinteger_cmd (cmd_name
.get (), cmdclass
,
690 extra_literals
, set_doc
,
691 show_doc
, help_doc
, get_set_value
,
692 get_show_value
, set_list
, show_list
);
696 commands
= add_setshow_string_cmd (cmd_name
.get (), cmdclass
,
697 self
->value
.stringval
, set_doc
,
698 show_doc
, help_doc
, get_set_value
,
699 get_show_value
, set_list
, show_list
);
702 case var_string_noescape
:
703 commands
= add_setshow_string_noescape_cmd (cmd_name
.get (), cmdclass
,
704 self
->value
.stringval
,
705 set_doc
, show_doc
, help_doc
,
706 get_set_value
, get_show_value
,
707 set_list
, show_list
);
710 case var_optional_filename
:
711 commands
= add_setshow_optional_filename_cmd (cmd_name
.get (), cmdclass
,
712 self
->value
.stringval
,
713 set_doc
, show_doc
, help_doc
,
715 get_show_value
, set_list
,
720 commands
= add_setshow_filename_cmd (cmd_name
.get (), cmdclass
,
721 self
->value
.stringval
, set_doc
,
722 show_doc
, help_doc
, get_set_value
,
723 get_show_value
, set_list
, show_list
);
727 /* Initialize the value, just in case. */
728 self
->value
.cstringval
= self
->enumeration
[0];
729 commands
= add_setshow_enum_cmd (cmd_name
.get (), cmdclass
,
731 &self
->value
.cstringval
, set_doc
,
732 show_doc
, help_doc
, get_set_value
,
733 get_show_value
, set_list
, show_list
);
737 /* Initialize the value, just in case. */
738 self
->value
.color
= ui_file_style::NONE
;
739 commands
= add_setshow_color_cmd (cmd_name
.get (), cmdclass
,
740 &self
->value
.color
, set_doc
,
741 show_doc
, help_doc
, get_set_value
,
742 get_show_value
, set_list
, show_list
);
746 gdb_assert_not_reached ("Unhandled parameter class.");
749 /* Register Python objects in both commands' context. */
750 commands
.set
->set_context (self
);
751 commands
.show
->set_context (self
);
753 /* We (unfortunately) currently leak the command name. */
757 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
758 error, with a python exception set. */
760 compute_enum_values (parmpy_object
*self
, PyObject
*enum_values
)
766 PyErr_SetString (PyExc_RuntimeError
,
767 _("An enumeration is required for PARAM_ENUM."));
771 if (! PySequence_Check (enum_values
))
773 PyErr_SetString (PyExc_RuntimeError
,
774 _("The enumeration is not a sequence."));
778 size
= PySequence_Size (enum_values
);
783 PyErr_SetString (PyExc_RuntimeError
,
784 _("The enumeration is empty."));
788 gdb_argv
holder (XCNEWVEC (char *, size
+ 1));
789 char **enumeration
= holder
.get ();
791 for (i
= 0; i
< size
; ++i
)
793 gdbpy_ref
<> item (PySequence_GetItem (enum_values
, i
));
797 if (! gdbpy_is_string (item
.get ()))
799 PyErr_SetString (PyExc_RuntimeError
,
800 _("The enumeration item not a string."));
803 enumeration
[i
] = python_string_to_host_string (item
.get ()).release ();
804 if (enumeration
[i
] == NULL
)
808 self
->enumeration
= const_cast<const char**> (holder
.release ());
812 /* Object initializer; sets up gdb-side structures for command.
814 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
816 NAME is the name of the parameter. It may consist of multiple
817 words, in which case the final word is the name of the new command,
818 and earlier words must be prefix commands.
820 CMDCLASS is the kind of command. It should be one of the COMMAND_*
821 constants defined in the gdb module.
823 PARMCLASS is the type of the parameter. It should be one of the
824 PARAM_* constants defined in the gdb module.
826 If PARMCLASS is PARAM_ENUM, then the final argument should be a
827 collection of strings. These strings are the valid values for this
830 The documentation for the parameter is taken from the doc string
831 for the python class.
833 Returns -1 on error, with a python exception set. */
836 parmpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
838 parmpy_object
*obj
= (parmpy_object
*) self
;
840 gdb::unique_xmalloc_ptr
<char> set_doc
, show_doc
, doc
;
841 int parmclass
, cmdtype
;
842 PyObject
*enum_values
= NULL
;
843 struct cmd_list_element
**set_list
, **show_list
;
844 const literal_def
*extra_literals
;
847 if (! PyArg_ParseTuple (args
, "sii|O", &name
, &cmdtype
, &parmclass
,
851 if (cmdtype
!= no_class
&& cmdtype
!= class_run
852 && cmdtype
!= class_vars
&& cmdtype
!= class_stack
853 && cmdtype
!= class_files
&& cmdtype
!= class_support
854 && cmdtype
!= class_info
&& cmdtype
!= class_breakpoint
855 && cmdtype
!= class_trace
&& cmdtype
!= class_obscure
856 && cmdtype
!= class_maintenance
)
858 PyErr_Format (PyExc_RuntimeError
, _("Invalid command class argument."));
862 if (parmclass
!= param_boolean
/* ARI: param_boolean */
863 && parmclass
!= param_auto_boolean
864 && parmclass
!= param_uinteger
&& parmclass
!= param_integer
865 && parmclass
!= param_string
&& parmclass
!= param_string_noescape
866 && parmclass
!= param_optional_filename
&& parmclass
!= param_filename
867 && parmclass
!= param_zinteger
&& parmclass
!= param_zuinteger
868 && parmclass
!= param_zuinteger_unlimited
&& parmclass
!= param_enum
869 && parmclass
!= param_color
)
871 PyErr_SetString (PyExc_RuntimeError
,
872 _("Invalid parameter class argument."));
876 if (enum_values
&& parmclass
!= param_enum
)
878 PyErr_SetString (PyExc_RuntimeError
,
879 _("Only PARAM_ENUM accepts a fourth argument."));
882 if (parmclass
== param_enum
)
884 if (! compute_enum_values (obj
, enum_values
))
888 obj
->enumeration
= NULL
;
889 type
= param_to_var
[parmclass
].type
;
890 extra_literals
= param_to_var
[parmclass
].extra_literals
;
892 obj
->extra_literals
= extra_literals
;
893 obj
->value
= {}; /* zeros initialization */
895 if (var_type_uses
<std::string
> (obj
->type
))
896 obj
->value
.stringval
= new std::string
;
898 gdb::unique_xmalloc_ptr
<char> cmd_name
899 = gdbpy_parse_command_name (name
, &set_list
, &setlist
);
900 if (cmd_name
== nullptr)
903 cmd_name
= gdbpy_parse_command_name (name
, &show_list
, &showlist
);
904 if (cmd_name
== nullptr)
907 set_doc
= get_doc_string (self
, doc_string_set
, name
);
908 show_doc
= get_doc_string (self
, doc_string_show
, name
);
909 doc
= get_doc_string (self
, doc_string_description
, cmd_name
.get ());
911 /* The set/show docs should always be a non-empty string. */
912 gdb_assert (set_doc
!= nullptr && *set_doc
!= '\0');
913 gdb_assert (show_doc
!= nullptr && *show_doc
!= '\0');
915 /* For the DOC string only, if it is the empty string, then we convert it
916 to NULL. This means GDB will not even display a blank line for this
917 part of the help text, instead the set/show line is all the user will
919 gdb_assert (doc
!= nullptr);
927 add_setshow_generic (type
, extra_literals
,
928 (enum command_class
) cmdtype
,
929 std::move (cmd_name
), obj
,
930 set_doc
.get (), show_doc
.get (),
931 doc
.get (), set_list
, show_list
);
933 catch (const gdb_exception
&except
)
936 return gdbpy_handle_gdb_exception (-1, except
);
942 /* Deallocate function for a gdb.Parameter. */
945 parmpy_dealloc (PyObject
*obj
)
947 parmpy_object
*parm_obj
= (parmpy_object
*) obj
;
949 if (var_type_uses
<std::string
> (parm_obj
->type
))
950 delete parm_obj
->value
.stringval
;
951 else if (var_type_uses
<ui_file_style::color
> (parm_obj
->type
))
952 parm_obj
->value
.color
.~color();
955 /* Initialize the 'parameters' module. */
956 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
957 gdbpy_initialize_parameters (void)
961 parmpy_object_type
.tp_new
= PyType_GenericNew
;
962 if (gdbpy_type_ready (&parmpy_object_type
) < 0)
965 set_doc_cst
= PyUnicode_FromString ("set_doc");
968 show_doc_cst
= PyUnicode_FromString ("show_doc");
972 for (i
= 0; parm_constants
[i
].name
; ++i
)
974 if (PyModule_AddIntConstant (gdb_module
,
975 parm_constants
[i
].name
,
976 parm_constants
[i
].value
) < 0)
983 GDBPY_INITIALIZE_FILE (gdbpy_initialize_parameters
);
987 PyTypeObject parmpy_object_type
=
989 PyVarObject_HEAD_INIT (NULL
, 0)
990 "gdb.Parameter", /*tp_name*/
991 sizeof (parmpy_object
), /*tp_basicsize*/
993 parmpy_dealloc
, /*tp_dealloc*/
1000 0, /*tp_as_sequence*/
1001 0, /*tp_as_mapping*/
1005 get_attr
, /*tp_getattro*/
1006 set_attr
, /*tp_setattro*/
1008 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1009 "GDB parameter object", /* tp_doc */
1010 0, /* tp_traverse */
1012 0, /* tp_richcompare */
1013 0, /* tp_weaklistoffset */
1015 0, /* tp_iternext */
1021 0, /* tp_descr_get */
1022 0, /* tp_descr_set */
1023 0, /* tp_dictoffset */
1024 parmpy_init
, /* tp_init */