Fix TARGET_CHAR_BIT/HOST_CHAR_BIT confusion in gmp-utils.c
[binutils-gdb.git] / gdb / python / py-param.c
blobdf45e563b4da7a730b7ceb69f644f2c9005f33b6
1 /* GDB parameters implemented in Python
3 Copyright (C) 2008-2020 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/>. */
21 #include "defs.h"
22 #include "value.h"
23 #include "python-internal.h"
24 #include "charset.h"
25 #include "gdbcmd.h"
26 #include "cli/cli-decode.h"
27 #include "completer.h"
28 #include "language.h"
29 #include "arch-utils.h"
31 /* Parameter constants and their values. */
32 struct parm_constant
34 const char *name;
35 int value;
38 struct parm_constant parm_constants[] =
40 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
41 { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
42 { "PARAM_UINTEGER", var_uinteger },
43 { "PARAM_INTEGER", var_integer },
44 { "PARAM_STRING", var_string },
45 { "PARAM_STRING_NOESCAPE", var_string_noescape },
46 { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
47 { "PARAM_FILENAME", var_filename },
48 { "PARAM_ZINTEGER", var_zinteger },
49 { "PARAM_ZUINTEGER", var_zuinteger },
50 { "PARAM_ZUINTEGER_UNLIMITED", var_zuinteger_unlimited },
51 { "PARAM_ENUM", var_enum },
52 { NULL, 0 }
55 /* A union that can hold anything described by enum var_types. */
56 union parmpy_variable
58 /* Hold a boolean value. */
59 bool boolval;
61 /* Hold an integer value. */
62 int intval;
64 /* Hold an auto_boolean. */
65 enum auto_boolean autoboolval;
67 /* Hold an unsigned integer value, for uinteger. */
68 unsigned int uintval;
70 /* Hold a string, for the various string types. */
71 char *stringval;
73 /* Hold a string, for enums. */
74 const char *cstringval;
77 /* A GDB parameter. */
78 struct parmpy_object
80 PyObject_HEAD
82 /* The type of the parameter. */
83 enum var_types type;
85 /* The value of the parameter. */
86 union parmpy_variable value;
88 /* For an enum command, the possible values. The vector is
89 allocated with xmalloc, as is each element. It is
90 NULL-terminated. */
91 const char **enumeration;
94 extern PyTypeObject parmpy_object_type
95 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
97 /* Some handy string constants. */
98 static PyObject *set_doc_cst;
99 static PyObject *show_doc_cst;
103 /* Get an attribute. */
104 static PyObject *
105 get_attr (PyObject *obj, PyObject *attr_name)
107 if (PyString_Check (attr_name)
108 #ifdef IS_PY3K
109 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
110 #else
111 && ! strcmp (PyString_AsString (attr_name), "value"))
112 #endif
114 parmpy_object *self = (parmpy_object *) obj;
116 return gdbpy_parameter_value (self->type, &self->value);
119 return PyObject_GenericGetAttr (obj, attr_name);
122 /* Set a parameter value from a Python value. Return 0 on success. Returns
123 -1 on error, with a python exception set. */
124 static int
125 set_parameter_value (parmpy_object *self, PyObject *value)
127 int cmp;
129 switch (self->type)
131 case var_string:
132 case var_string_noescape:
133 case var_optional_filename:
134 case var_filename:
135 if (! gdbpy_is_string (value)
136 && (self->type == var_filename
137 || value != Py_None))
139 PyErr_SetString (PyExc_RuntimeError,
140 _("String required for filename."));
142 return -1;
144 if (value == Py_None)
146 xfree (self->value.stringval);
147 if (self->type == var_optional_filename)
148 self->value.stringval = xstrdup ("");
149 else
150 self->value.stringval = NULL;
152 else
154 gdb::unique_xmalloc_ptr<char>
155 string (python_string_to_host_string (value));
156 if (string == NULL)
157 return -1;
159 xfree (self->value.stringval);
160 self->value.stringval = string.release ();
162 break;
164 case var_enum:
166 int i;
168 if (! gdbpy_is_string (value))
170 PyErr_SetString (PyExc_RuntimeError,
171 _("ENUM arguments must be a string."));
172 return -1;
175 gdb::unique_xmalloc_ptr<char>
176 str (python_string_to_host_string (value));
177 if (str == NULL)
178 return -1;
179 for (i = 0; self->enumeration[i]; ++i)
180 if (! strcmp (self->enumeration[i], str.get ()))
181 break;
182 if (! self->enumeration[i])
184 PyErr_SetString (PyExc_RuntimeError,
185 _("The value must be member of an enumeration."));
186 return -1;
188 self->value.cstringval = self->enumeration[i];
189 break;
192 case var_boolean:
193 if (! PyBool_Check (value))
195 PyErr_SetString (PyExc_RuntimeError,
196 _("A boolean argument is required."));
197 return -1;
199 cmp = PyObject_IsTrue (value);
200 if (cmp < 0)
201 return -1;
202 self->value.boolval = cmp;
203 break;
205 case var_auto_boolean:
206 if (! PyBool_Check (value) && value != Py_None)
208 PyErr_SetString (PyExc_RuntimeError,
209 _("A boolean or None is required"));
210 return -1;
213 if (value == Py_None)
214 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
215 else
217 cmp = PyObject_IsTrue (value);
218 if (cmp < 0 )
219 return -1;
220 if (cmp == 1)
221 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
222 else
223 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
225 break;
227 case var_integer:
228 case var_zinteger:
229 case var_uinteger:
230 case var_zuinteger:
231 case var_zuinteger_unlimited:
233 long l;
234 int ok;
236 if (! PyInt_Check (value))
238 PyErr_SetString (PyExc_RuntimeError,
239 _("The value must be integer."));
240 return -1;
243 if (! gdb_py_int_as_long (value, &l))
244 return -1;
246 switch (self->type)
248 case var_uinteger:
249 if (l == 0)
250 l = UINT_MAX;
251 /* Fall through. */
252 case var_zuinteger:
253 ok = (l >= 0 && l <= UINT_MAX);
254 break;
256 case var_zuinteger_unlimited:
257 ok = (l >= -1 && l <= INT_MAX);
258 break;
260 case var_integer:
261 ok = (l >= INT_MIN && l <= INT_MAX);
262 if (l == 0)
263 l = INT_MAX;
264 break;
266 case var_zinteger:
267 ok = (l >= INT_MIN && l <= INT_MAX);
268 break;
270 default:
271 gdb_assert_not_reached ("unknown var_ constant");
274 if (! ok)
276 PyErr_SetString (PyExc_RuntimeError,
277 _("Range exceeded."));
278 return -1;
281 if (self->type == var_uinteger || self->type == var_zuinteger)
282 self->value.uintval = (unsigned) l;
283 else
284 self->value.intval = (int) l;
285 break;
288 default:
289 PyErr_SetString (PyExc_RuntimeError,
290 _("Unhandled type in parameter value."));
291 return -1;
294 return 0;
297 /* Set an attribute. Returns -1 on error, with a python exception set. */
298 static int
299 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
301 if (PyString_Check (attr_name)
302 #ifdef IS_PY3K
303 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
304 #else
305 && ! strcmp (PyString_AsString (attr_name), "value"))
306 #endif
308 if (!val)
310 PyErr_SetString (PyExc_RuntimeError,
311 _("Cannot delete a parameter's value."));
312 return -1;
314 return set_parameter_value ((parmpy_object *) obj, val);
317 return PyObject_GenericSetAttr (obj, attr_name, val);
320 /* A helper function which returns a documentation string for an
321 object. */
323 static gdb::unique_xmalloc_ptr<char>
324 get_doc_string (PyObject *object, PyObject *attr)
326 gdb::unique_xmalloc_ptr<char> result;
328 if (PyObject_HasAttr (object, attr))
330 gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
332 if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
334 result = python_string_to_host_string (ds_obj.get ());
335 if (result == NULL)
336 gdbpy_print_stack ();
339 if (! result)
340 result.reset (xstrdup (_("This command is not documented.")));
341 return result;
344 /* Helper function which will execute a METHOD in OBJ passing the
345 argument ARG. ARG can be NULL. METHOD should return a Python
346 string. If this function returns NULL, there has been an error and
347 the appropriate exception set. */
348 static gdb::unique_xmalloc_ptr<char>
349 call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
351 gdb::unique_xmalloc_ptr<char> data;
352 gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
354 if (result == NULL)
355 return NULL;
357 if (gdbpy_is_string (result.get ()))
359 data = python_string_to_host_string (result.get ());
360 if (! data)
361 return NULL;
363 else
365 PyErr_SetString (PyExc_RuntimeError,
366 _("Parameter must return a string value."));
367 return NULL;
370 return data;
373 /* A callback function that is registered against the respective
374 add_setshow_* set_doc prototype. This function will either call
375 the Python function "get_set_string" or extract the Python
376 attribute "set_doc" and return the contents as a string. If
377 neither exist, insert a string indicating the Parameter is not
378 documented. */
379 static void
380 get_set_value (const char *args, int from_tty,
381 struct cmd_list_element *c)
383 PyObject *obj = (PyObject *) get_cmd_context (c);
384 gdb::unique_xmalloc_ptr<char> set_doc_string;
386 gdbpy_enter enter_py (get_current_arch (), current_language);
387 gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
389 if (set_doc_func == NULL)
391 gdbpy_print_stack ();
392 return;
395 if (PyObject_HasAttr (obj, set_doc_func.get ()))
397 set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
398 if (! set_doc_string)
399 gdbpy_handle_exception ();
402 const char *str = set_doc_string.get ();
403 if (str != nullptr && str[0] != '\0')
404 fprintf_filtered (gdb_stdout, "%s\n", str);
407 /* A callback function that is registered against the respective
408 add_setshow_* show_doc prototype. This function will either call
409 the Python function "get_show_string" or extract the Python
410 attribute "show_doc" and return the contents as a string. If
411 neither exist, insert a string indicating the Parameter is not
412 documented. */
413 static void
414 get_show_value (struct ui_file *file, int from_tty,
415 struct cmd_list_element *c,
416 const char *value)
418 PyObject *obj = (PyObject *) get_cmd_context (c);
419 gdb::unique_xmalloc_ptr<char> show_doc_string;
421 gdbpy_enter enter_py (get_current_arch (), current_language);
422 gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
424 if (show_doc_func == NULL)
426 gdbpy_print_stack ();
427 return;
430 if (PyObject_HasAttr (obj, show_doc_func.get ()))
432 gdbpy_ref<> val_obj (PyString_FromString (value));
434 if (val_obj == NULL)
436 gdbpy_print_stack ();
437 return;
440 show_doc_string = call_doc_function (obj, show_doc_func.get (),
441 val_obj.get ());
442 if (! show_doc_string)
444 gdbpy_print_stack ();
445 return;
448 fprintf_filtered (file, "%s\n", show_doc_string.get ());
450 else
452 /* We have to preserve the existing < GDB 7.3 API. If a
453 callback function does not exist, then attempt to read the
454 show_doc attribute. */
455 show_doc_string = get_doc_string (obj, show_doc_cst);
456 fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
461 /* A helper function that dispatches to the appropriate add_setshow
462 function. */
463 static void
464 add_setshow_generic (int parmclass, enum command_class cmdclass,
465 const char *cmd_name, parmpy_object *self,
466 const char *set_doc, const char *show_doc,
467 const char *help_doc,
468 struct cmd_list_element **set_list,
469 struct cmd_list_element **show_list)
471 struct cmd_list_element *param = NULL;
472 const char *tmp_name = NULL;
474 switch (parmclass)
476 case var_boolean:
478 add_setshow_boolean_cmd (cmd_name, cmdclass,
479 &self->value.boolval, set_doc, show_doc,
480 help_doc, get_set_value, get_show_value,
481 set_list, show_list);
483 break;
485 case var_auto_boolean:
486 add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
487 &self->value.autoboolval,
488 set_doc, show_doc, help_doc,
489 get_set_value, get_show_value,
490 set_list, show_list);
491 break;
493 case var_uinteger:
494 add_setshow_uinteger_cmd (cmd_name, cmdclass,
495 &self->value.uintval, set_doc, show_doc,
496 help_doc, get_set_value, get_show_value,
497 set_list, show_list);
498 break;
500 case var_integer:
501 add_setshow_integer_cmd (cmd_name, cmdclass,
502 &self->value.intval, set_doc, show_doc,
503 help_doc, get_set_value, get_show_value,
504 set_list, show_list); break;
506 case var_string:
507 add_setshow_string_cmd (cmd_name, cmdclass,
508 &self->value.stringval, set_doc, show_doc,
509 help_doc, get_set_value, get_show_value,
510 set_list, show_list); break;
512 case var_string_noescape:
513 add_setshow_string_noescape_cmd (cmd_name, cmdclass,
514 &self->value.stringval,
515 set_doc, show_doc, help_doc,
516 get_set_value, get_show_value,
517 set_list, show_list);
519 break;
521 case var_optional_filename:
522 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
523 &self->value.stringval, set_doc,
524 show_doc, help_doc, get_set_value,
525 get_show_value, set_list,
526 show_list);
527 break;
529 case var_filename:
530 add_setshow_filename_cmd (cmd_name, cmdclass,
531 &self->value.stringval, set_doc, show_doc,
532 help_doc, get_set_value, get_show_value,
533 set_list, show_list); break;
535 case var_zinteger:
536 add_setshow_zinteger_cmd (cmd_name, cmdclass,
537 &self->value.intval, set_doc, show_doc,
538 help_doc, get_set_value, get_show_value,
539 set_list, show_list);
540 break;
542 case var_zuinteger:
543 add_setshow_zuinteger_cmd (cmd_name, cmdclass,
544 &self->value.uintval, set_doc, show_doc,
545 help_doc, get_set_value, get_show_value,
546 set_list, show_list);
547 break;
549 case var_zuinteger_unlimited:
550 add_setshow_zuinteger_unlimited_cmd (cmd_name, cmdclass,
551 &self->value.intval, set_doc,
552 show_doc, help_doc, get_set_value,
553 get_show_value,
554 set_list, show_list);
555 break;
557 case var_enum:
558 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
559 &self->value.cstringval, set_doc, show_doc,
560 help_doc, get_set_value, get_show_value,
561 set_list, show_list);
562 /* Initialize the value, just in case. */
563 self->value.cstringval = self->enumeration[0];
564 break;
567 /* Lookup created parameter, and register Python object against the
568 parameter context. Perform this task against both lists. */
569 tmp_name = cmd_name;
570 param = lookup_cmd (&tmp_name, *show_list, "", NULL, 0, 1);
571 if (param)
572 set_cmd_context (param, self);
574 tmp_name = cmd_name;
575 param = lookup_cmd (&tmp_name, *set_list, "", NULL, 0, 1);
576 if (param)
577 set_cmd_context (param, self);
580 /* A helper which computes enum values. Returns 1 on success. Returns 0 on
581 error, with a python exception set. */
582 static int
583 compute_enum_values (parmpy_object *self, PyObject *enum_values)
585 Py_ssize_t size, i;
587 if (! enum_values)
589 PyErr_SetString (PyExc_RuntimeError,
590 _("An enumeration is required for PARAM_ENUM."));
591 return 0;
594 if (! PySequence_Check (enum_values))
596 PyErr_SetString (PyExc_RuntimeError,
597 _("The enumeration is not a sequence."));
598 return 0;
601 size = PySequence_Size (enum_values);
602 if (size < 0)
603 return 0;
604 if (size == 0)
606 PyErr_SetString (PyExc_RuntimeError,
607 _("The enumeration is empty."));
608 return 0;
611 gdb_argv holder (XCNEWVEC (char *, size + 1));
612 char **enumeration = holder.get ();
614 for (i = 0; i < size; ++i)
616 gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
618 if (item == NULL)
619 return 0;
620 if (! gdbpy_is_string (item.get ()))
622 PyErr_SetString (PyExc_RuntimeError,
623 _("The enumeration item not a string."));
624 return 0;
626 enumeration[i] = python_string_to_host_string (item.get ()).release ();
627 if (enumeration[i] == NULL)
628 return 0;
631 self->enumeration = const_cast<const char**> (holder.release ());
632 return 1;
635 /* Object initializer; sets up gdb-side structures for command.
637 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
639 NAME is the name of the parameter. It may consist of multiple
640 words, in which case the final word is the name of the new command,
641 and earlier words must be prefix commands.
643 CMDCLASS is the kind of command. It should be one of the COMMAND_*
644 constants defined in the gdb module.
646 PARMCLASS is the type of the parameter. It should be one of the
647 PARAM_* constants defined in the gdb module.
649 If PARMCLASS is PARAM_ENUM, then the final argument should be a
650 collection of strings. These strings are the valid values for this
651 parameter.
653 The documentation for the parameter is taken from the doc string
654 for the python class.
656 Returns -1 on error, with a python exception set. */
658 static int
659 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
661 parmpy_object *obj = (parmpy_object *) self;
662 const char *name;
663 gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
664 char *cmd_name;
665 int parmclass, cmdtype;
666 PyObject *enum_values = NULL;
667 struct cmd_list_element **set_list, **show_list;
669 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
670 &enum_values))
671 return -1;
673 if (cmdtype != no_class && cmdtype != class_run
674 && cmdtype != class_vars && cmdtype != class_stack
675 && cmdtype != class_files && cmdtype != class_support
676 && cmdtype != class_info && cmdtype != class_breakpoint
677 && cmdtype != class_trace && cmdtype != class_obscure
678 && cmdtype != class_maintenance)
680 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
681 return -1;
684 if (parmclass != var_boolean /* ARI: var_boolean */
685 && parmclass != var_auto_boolean
686 && parmclass != var_uinteger && parmclass != var_integer
687 && parmclass != var_string && parmclass != var_string_noescape
688 && parmclass != var_optional_filename && parmclass != var_filename
689 && parmclass != var_zinteger && parmclass != var_zuinteger
690 && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
692 PyErr_SetString (PyExc_RuntimeError,
693 _("Invalid parameter class argument."));
694 return -1;
697 if (enum_values && parmclass != var_enum)
699 PyErr_SetString (PyExc_RuntimeError,
700 _("Only PARAM_ENUM accepts a fourth argument."));
701 return -1;
703 if (parmclass == var_enum)
705 if (! compute_enum_values (obj, enum_values))
706 return -1;
708 else
709 obj->enumeration = NULL;
710 obj->type = (enum var_types) parmclass;
711 memset (&obj->value, 0, sizeof (obj->value));
713 cmd_name = gdbpy_parse_command_name (name, &set_list,
714 &setlist);
716 if (! cmd_name)
717 return -1;
718 xfree (cmd_name);
719 cmd_name = gdbpy_parse_command_name (name, &show_list,
720 &showlist);
721 if (! cmd_name)
722 return -1;
724 set_doc = get_doc_string (self, set_doc_cst);
725 show_doc = get_doc_string (self, show_doc_cst);
726 doc = get_doc_string (self, gdbpy_doc_cst);
728 Py_INCREF (self);
732 add_setshow_generic (parmclass, (enum command_class) cmdtype,
733 cmd_name, obj,
734 set_doc.get (), show_doc.get (),
735 doc.get (), set_list, show_list);
737 catch (const gdb_exception &except)
739 xfree (cmd_name);
740 Py_DECREF (self);
741 gdbpy_convert_exception (except);
742 return -1;
745 return 0;
750 /* Initialize the 'parameters' module. */
752 gdbpy_initialize_parameters (void)
754 int i;
756 parmpy_object_type.tp_new = PyType_GenericNew;
757 if (PyType_Ready (&parmpy_object_type) < 0)
758 return -1;
760 set_doc_cst = PyString_FromString ("set_doc");
761 if (! set_doc_cst)
762 return -1;
763 show_doc_cst = PyString_FromString ("show_doc");
764 if (! show_doc_cst)
765 return -1;
767 for (i = 0; parm_constants[i].name; ++i)
769 if (PyModule_AddIntConstant (gdb_module,
770 parm_constants[i].name,
771 parm_constants[i].value) < 0)
772 return -1;
775 return gdb_pymodule_addobject (gdb_module, "Parameter",
776 (PyObject *) &parmpy_object_type);
781 PyTypeObject parmpy_object_type =
783 PyVarObject_HEAD_INIT (NULL, 0)
784 "gdb.Parameter", /*tp_name*/
785 sizeof (parmpy_object), /*tp_basicsize*/
786 0, /*tp_itemsize*/
787 0, /*tp_dealloc*/
788 0, /*tp_print*/
789 0, /*tp_getattr*/
790 0, /*tp_setattr*/
791 0, /*tp_compare*/
792 0, /*tp_repr*/
793 0, /*tp_as_number*/
794 0, /*tp_as_sequence*/
795 0, /*tp_as_mapping*/
796 0, /*tp_hash */
797 0, /*tp_call*/
798 0, /*tp_str*/
799 get_attr, /*tp_getattro*/
800 set_attr, /*tp_setattro*/
801 0, /*tp_as_buffer*/
802 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
803 "GDB parameter object", /* tp_doc */
804 0, /* tp_traverse */
805 0, /* tp_clear */
806 0, /* tp_richcompare */
807 0, /* tp_weaklistoffset */
808 0, /* tp_iter */
809 0, /* tp_iternext */
810 0, /* tp_methods */
811 0, /* tp_members */
812 0, /* tp_getset */
813 0, /* tp_base */
814 0, /* tp_dict */
815 0, /* tp_descr_get */
816 0, /* tp_descr_set */
817 0, /* tp_dictoffset */
818 parmpy_init, /* tp_init */
819 0, /* tp_alloc */