1/* GDB parameters implemented in Python
2
3   Copyright (C) 2008-2023 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20
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"
30
31/* Parameter constants and their values.  */
32static struct {
33  const char *name;
34  int value;
35} parm_constants[] =
36{
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 },
49  { NULL, 0 }
50};
51
52/* A union that can hold anything described by enum var_types.  */
53union parmpy_variable
54{
55  /* Hold a boolean value.  */
56  bool boolval;
57
58  /* Hold an integer value.  */
59  int intval;
60
61  /* Hold an auto_boolean.  */
62  enum auto_boolean autoboolval;
63
64  /* Hold an unsigned integer value, for uinteger.  */
65  unsigned int uintval;
66
67  /* Hold a string, for the various string types.  The std::string is
68     new-ed.  */
69  std::string *stringval;
70
71  /* Hold a string, for enums.  */
72  const char *cstringval;
73};
74
75/* A GDB parameter.  */
76struct parmpy_object
77{
78  PyObject_HEAD
79
80  /* The type of the parameter.  */
81  enum var_types type;
82
83  /* The value of the parameter.  */
84  union parmpy_variable value;
85
86  /* For an enum command, the possible values.  The vector is
87     allocated with xmalloc, as is each element.  It is
88     NULL-terminated.  */
89  const char **enumeration;
90};
91
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.  */
95
96static setting
97make_setting (parmpy_object *s)
98{
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);
111  else
112    gdb_assert_not_reached ("unhandled var type");
113}
114
115extern PyTypeObject parmpy_object_type
116    CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
117
118/* Some handy string constants.  */
119static PyObject *set_doc_cst;
120static PyObject *show_doc_cst;
121
122
123
124/* Get an attribute.  */
125static PyObject *
126get_attr (PyObject *obj, PyObject *attr_name)
127{
128  if (PyUnicode_Check (attr_name)
129      && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
130    {
131      parmpy_object *self = (parmpy_object *) obj;
132
133      return gdbpy_parameter_value (make_setting (self));
134    }
135
136  return PyObject_GenericGetAttr (obj, attr_name);
137}
138
139/* Set a parameter value from a Python value.  Return 0 on success.  Returns
140   -1 on error, with a python exception set.  */
141static int
142set_parameter_value (parmpy_object *self, PyObject *value)
143{
144  int cmp;
145
146  switch (self->type)
147    {
148    case var_string:
149    case var_string_noescape:
150    case var_optional_filename:
151    case var_filename:
152      if (! gdbpy_is_string (value)
153	  && (self->type == var_filename
154	      || value != Py_None))
155	{
156	  PyErr_SetString (PyExc_RuntimeError,
157			   _("String required for filename."));
158
159	  return -1;
160	}
161      if (value == Py_None)
162	self->value.stringval->clear ();
163      else
164	{
165	  gdb::unique_xmalloc_ptr<char>
166	    string (python_string_to_host_string (value));
167	  if (string == NULL)
168	    return -1;
169
170	  *self->value.stringval = string.get ();
171	}
172      break;
173
174    case var_enum:
175      {
176	int i;
177
178	if (! gdbpy_is_string (value))
179	  {
180	    PyErr_SetString (PyExc_RuntimeError,
181			     _("ENUM arguments must be a string."));
182	    return -1;
183	  }
184
185	gdb::unique_xmalloc_ptr<char>
186	  str (python_string_to_host_string (value));
187	if (str == NULL)
188	  return -1;
189	for (i = 0; self->enumeration[i]; ++i)
190	  if (! strcmp (self->enumeration[i], str.get ()))
191	    break;
192	if (! self->enumeration[i])
193	  {
194	    PyErr_SetString (PyExc_RuntimeError,
195			     _("The value must be member of an enumeration."));
196	    return -1;
197	  }
198	self->value.cstringval = self->enumeration[i];
199	break;
200      }
201
202    case var_boolean:
203      if (! PyBool_Check (value))
204	{
205	  PyErr_SetString (PyExc_RuntimeError,
206			   _("A boolean argument is required."));
207	  return -1;
208	}
209      cmp = PyObject_IsTrue (value);
210      if (cmp < 0)
211	  return -1;
212      self->value.boolval = cmp;
213      break;
214
215    case var_auto_boolean:
216      if (! PyBool_Check (value) && value != Py_None)
217	{
218	  PyErr_SetString (PyExc_RuntimeError,
219			   _("A boolean or None is required"));
220	  return -1;
221	}
222
223      if (value == Py_None)
224	self->value.autoboolval = AUTO_BOOLEAN_AUTO;
225      else
226	{
227	  cmp = PyObject_IsTrue (value);
228	  if (cmp < 0 )
229	    return -1;
230	  if (cmp == 1)
231	    self->value.autoboolval = AUTO_BOOLEAN_TRUE;
232	  else
233	    self->value.autoboolval = AUTO_BOOLEAN_FALSE;
234	}
235      break;
236
237    case var_integer:
238    case var_zinteger:
239    case var_uinteger:
240    case var_zuinteger:
241    case var_zuinteger_unlimited:
242      {
243	long l;
244	int ok;
245
246	if (value == Py_None
247	    && (self->type == var_uinteger || self->type == var_integer))
248	  l = 0;
249	else if (value == Py_None && self->type == var_zuinteger_unlimited)
250	  l = -1;
251	else if (!PyLong_Check (value))
252	  {
253	    PyErr_SetString (PyExc_RuntimeError,
254			     _("The value must be integer."));
255	    return -1;
256	  }
257	else if (! gdb_py_int_as_long (value, &l))
258	  return -1;
259
260	switch (self->type)
261	  {
262	  case var_uinteger:
263	    if (l == 0)
264	      l = UINT_MAX;
265	    /* Fall through.  */
266	  case var_zuinteger:
267	    ok = (l >= 0 && l <= UINT_MAX);
268	    break;
269
270	  case var_zuinteger_unlimited:
271	    ok = (l >= -1 && l <= INT_MAX);
272	    break;
273
274	  case var_integer:
275	    ok = (l >= INT_MIN && l <= INT_MAX);
276	    if (l == 0)
277	      l = INT_MAX;
278	    break;
279
280	  case var_zinteger:
281	    ok = (l >= INT_MIN && l <= INT_MAX);
282	    break;
283
284	  default:
285	    gdb_assert_not_reached ("unknown var_ constant");
286	  }
287
288	if (! ok)
289	  {
290	    PyErr_SetString (PyExc_RuntimeError,
291			     _("Range exceeded."));
292	    return -1;
293	  }
294
295	if (self->type == var_uinteger || self->type == var_zuinteger)
296	  self->value.uintval = (unsigned) l;
297	else
298	  self->value.intval = (int) l;
299	break;
300      }
301
302    default:
303      PyErr_SetString (PyExc_RuntimeError,
304		       _("Unhandled type in parameter value."));
305      return -1;
306    }
307
308  return 0;
309}
310
311/* Set an attribute.  Returns -1 on error, with a python exception set.  */
312static int
313set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
314{
315  if (PyUnicode_Check (attr_name)
316      && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
317    {
318      if (!val)
319	{
320	  PyErr_SetString (PyExc_RuntimeError,
321			   _("Cannot delete a parameter's value."));
322	  return -1;
323	}
324      return set_parameter_value ((parmpy_object *) obj, val);
325    }
326
327  return PyObject_GenericSetAttr (obj, attr_name, val);
328}
329
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'.
334
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'.  */
337
338static std::string
339full_cmd_name_without_first_prefix (struct cmd_list_element *c)
340{
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];
347  return result;
348}
349
350/* The different types of documentation string.  */
351
352enum doc_string_type
353{
354  doc_string_set,
355  doc_string_show,
356  doc_string_description
357};
358
359/* A helper function which returns a documentation string for an
360   object. */
361
362static gdb::unique_xmalloc_ptr<char>
363get_doc_string (PyObject *object, enum doc_string_type doc_type,
364		const char *cmd_name)
365{
366  gdb::unique_xmalloc_ptr<char> result;
367
368  PyObject *attr = nullptr;
369  switch (doc_type)
370    {
371    case doc_string_set:
372      attr = set_doc_cst;
373      break;
374    case doc_string_show:
375      attr = show_doc_cst;
376      break;
377    case doc_string_description:
378      attr = gdbpy_doc_cst;
379      break;
380    }
381  gdb_assert (attr != nullptr);
382
383  if (PyObject_HasAttr (object, attr))
384    {
385      gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
386
387      if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
388	{
389	  result = python_string_to_host_string (ds_obj.get ());
390	  if (result == NULL)
391	    gdbpy_print_stack ();
392	  else if (doc_type == doc_string_description)
393	    result = gdbpy_fix_doc_string_indentation (std::move (result));
394	}
395    }
396
397  if (result == nullptr)
398    {
399      if (doc_type == doc_string_description)
400	result.reset (xstrdup (_("This command is not documented.")));
401      else
402	{
403	  if (doc_type == doc_string_show)
404	    result = xstrprintf (_("Show the current value of '%s'."),
405				 cmd_name);
406	  else
407	    result = xstrprintf (_("Set the current value of '%s'."),
408				 cmd_name);
409	}
410    }
411  return result;
412}
413
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.  */
418static gdb::unique_xmalloc_ptr<char>
419call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
420{
421  gdb::unique_xmalloc_ptr<char> data;
422  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
423
424  if (result == NULL)
425    return NULL;
426
427  if (gdbpy_is_string (result.get ()))
428    {
429      data = python_string_to_host_string (result.get ());
430      if (! data)
431	return NULL;
432    }
433  else
434    {
435      PyErr_SetString (PyExc_RuntimeError,
436		       _("Parameter must return a string value."));
437      return NULL;
438    }
439
440  return data;
441}
442
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.  */
448static void
449get_set_value (const char *args, int from_tty,
450	       struct cmd_list_element *c)
451{
452  PyObject *obj = (PyObject *) c->context ();
453  gdb::unique_xmalloc_ptr<char> set_doc_string;
454
455  gdbpy_enter enter_py;
456  gdbpy_ref<> set_doc_func (PyUnicode_FromString ("get_set_string"));
457
458  if (set_doc_func == NULL)
459    {
460      gdbpy_print_stack ();
461      return;
462    }
463
464  if (PyObject_HasAttr (obj, set_doc_func.get ()))
465    {
466      set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
467      if (! set_doc_string)
468	gdbpy_handle_exception ();
469    }
470
471  const char *str = set_doc_string.get ();
472  if (str != nullptr && str[0] != '\0')
473    gdb_printf ("%s\n", str);
474}
475
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
481   documented.  */
482static void
483get_show_value (struct ui_file *file, int from_tty,
484		struct cmd_list_element *c,
485		const char *value)
486{
487  PyObject *obj = (PyObject *) c->context ();
488  gdb::unique_xmalloc_ptr<char> show_doc_string;
489
490  gdbpy_enter enter_py;
491  gdbpy_ref<> show_doc_func (PyUnicode_FromString ("get_show_string"));
492
493  if (show_doc_func == NULL)
494    {
495      gdbpy_print_stack ();
496      return;
497    }
498
499  if (PyObject_HasAttr (obj, show_doc_func.get ()))
500    {
501      gdbpy_ref<> val_obj (PyUnicode_FromString (value));
502
503      if (val_obj == NULL)
504	{
505	  gdbpy_print_stack ();
506	  return;
507	}
508
509      show_doc_string = call_doc_function (obj, show_doc_func.get (),
510					   val_obj.get ());
511      if (! show_doc_string)
512	{
513	  gdbpy_print_stack ();
514	  return;
515	}
516
517      gdb_printf (file, "%s\n", show_doc_string.get ());
518    }
519  else
520    {
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);
530    }
531}
532
533
534/* A helper function that dispatches to the appropriate add_setshow
535   function.  */
536static void
537add_setshow_generic (int parmclass, enum command_class cmdclass,
538		     gdb::unique_xmalloc_ptr<char> cmd_name,
539		     parmpy_object *self,
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)
544{
545  set_show_commands commands;
546
547  switch (parmclass)
548    {
549    case var_boolean:
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);
554
555      break;
556
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);
563      break;
564
565    case var_uinteger:
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);
570      break;
571
572    case var_integer:
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);
577      break;
578
579    case var_string:
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);
584      break;
585
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);
592      break;
593
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,
598						    get_set_value,
599						    get_show_value, set_list,
600						    show_list);
601      break;
602
603    case var_filename:
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);
608      break;
609
610    case var_zinteger:
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);
615      break;
616
617    case var_zuinteger:
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,
622					    show_list);
623      break;
624
625    case var_zuinteger_unlimited:
626      commands = add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
627						      &self->value.intval,
628						      set_doc, show_doc,
629						      help_doc, get_set_value,
630						      get_show_value, set_list,
631						      show_list);
632      break;
633
634    case var_enum:
635      /* Initialize the value, just in case.  */
636      self->value.cstringval = self->enumeration[0];
637      commands = add_setshow_enum_cmd (cmd_name.get (), cmdclass,
638				       self->enumeration,
639				       &self->value.cstringval, set_doc,
640				       show_doc, help_doc, get_set_value,
641				       get_show_value, set_list, show_list);
642      break;
643
644    default:
645      gdb_assert_not_reached ("Unhandled parameter class.");
646    }
647
648  /* Register Python objects in both commands' context.  */
649  commands.set->set_context (self);
650  commands.show->set_context (self);
651
652  /* We (unfortunately) currently leak the command name.  */
653  cmd_name.release ();
654}
655
656/* A helper which computes enum values.  Returns 1 on success.  Returns 0 on
657   error, with a python exception set.  */
658static int
659compute_enum_values (parmpy_object *self, PyObject *enum_values)
660{
661  Py_ssize_t size, i;
662
663  if (! enum_values)
664    {
665      PyErr_SetString (PyExc_RuntimeError,
666		       _("An enumeration is required for PARAM_ENUM."));
667      return 0;
668    }
669
670  if (! PySequence_Check (enum_values))
671    {
672      PyErr_SetString (PyExc_RuntimeError,
673		       _("The enumeration is not a sequence."));
674      return 0;
675    }
676
677  size = PySequence_Size (enum_values);
678  if (size < 0)
679    return 0;
680  if (size == 0)
681    {
682      PyErr_SetString (PyExc_RuntimeError,
683		       _("The enumeration is empty."));
684      return 0;
685    }
686
687  gdb_argv holder (XCNEWVEC (char *, size + 1));
688  char **enumeration = holder.get ();
689
690  for (i = 0; i < size; ++i)
691    {
692      gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
693
694      if (item == NULL)
695	return 0;
696      if (! gdbpy_is_string (item.get ()))
697	{
698	  PyErr_SetString (PyExc_RuntimeError,
699			   _("The enumeration item not a string."));
700	  return 0;
701	}
702      enumeration[i] = python_string_to_host_string (item.get ()).release ();
703      if (enumeration[i] == NULL)
704	return 0;
705    }
706
707  self->enumeration = const_cast<const char**> (holder.release ());
708  return 1;
709}
710
711/* Object initializer; sets up gdb-side structures for command.
712
713   Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
714
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.
718
719   CMDCLASS is the kind of command.  It should be one of the COMMAND_*
720   constants defined in the gdb module.
721
722   PARMCLASS is the type of the parameter.  It should be one of the
723   PARAM_* constants defined in the gdb module.
724
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
727   parameter.
728
729   The documentation for the parameter is taken from the doc string
730   for the python class.
731
732   Returns -1 on error, with a python exception set.  */
733
734static int
735parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
736{
737  parmpy_object *obj = (parmpy_object *) self;
738  const char *name;
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;
743
744  if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
745			  &enum_values))
746    return -1;
747
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)
754    {
755      PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
756      return -1;
757    }
758
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)
766    {
767      PyErr_SetString (PyExc_RuntimeError,
768		       _("Invalid parameter class argument."));
769      return -1;
770    }
771
772  if (enum_values && parmclass != var_enum)
773    {
774      PyErr_SetString (PyExc_RuntimeError,
775		       _("Only PARAM_ENUM accepts a fourth argument."));
776      return -1;
777    }
778  if (parmclass == var_enum)
779    {
780      if (! compute_enum_values (obj, enum_values))
781	return -1;
782    }
783  else
784    obj->enumeration = NULL;
785  obj->type = (enum var_types) parmclass;
786  memset (&obj->value, 0, sizeof (obj->value));
787
788  if (var_type_uses<std::string> (obj->type))
789    obj->value.stringval = new std::string;
790
791  gdb::unique_xmalloc_ptr<char> cmd_name
792    = gdbpy_parse_command_name (name, &set_list, &setlist);
793  if (cmd_name == nullptr)
794    return -1;
795
796  cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
797  if (cmd_name == nullptr)
798    return -1;
799
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 ());
803
804  Py_INCREF (self);
805
806  try
807    {
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);
812    }
813  catch (const gdb_exception &except)
814    {
815      Py_DECREF (self);
816      gdbpy_convert_exception (except);
817      return -1;
818    }
819
820  return 0;
821}
822
823/* Deallocate function for a gdb.Parameter.  */
824
825static void
826parmpy_dealloc (PyObject *obj)
827{
828  parmpy_object *parm_obj = (parmpy_object *) obj;
829
830  if (var_type_uses<std::string> (parm_obj->type))
831    delete parm_obj->value.stringval;
832}
833
834/* Initialize the 'parameters' module.  */
835int
836gdbpy_initialize_parameters (void)
837{
838  int i;
839
840  parmpy_object_type.tp_new = PyType_GenericNew;
841  if (PyType_Ready (&parmpy_object_type) < 0)
842    return -1;
843
844  set_doc_cst = PyUnicode_FromString ("set_doc");
845  if (! set_doc_cst)
846    return -1;
847  show_doc_cst = PyUnicode_FromString ("show_doc");
848  if (! show_doc_cst)
849    return -1;
850
851  for (i = 0; parm_constants[i].name; ++i)
852    {
853      if (PyModule_AddIntConstant (gdb_module,
854				   parm_constants[i].name,
855				   parm_constants[i].value) < 0)
856	return -1;
857    }
858
859  return gdb_pymodule_addobject (gdb_module, "Parameter",
860				 (PyObject *) &parmpy_object_type);
861}
862
863
864
865PyTypeObject parmpy_object_type =
866{
867  PyVarObject_HEAD_INIT (NULL, 0)
868  "gdb.Parameter",		  /*tp_name*/
869  sizeof (parmpy_object),	  /*tp_basicsize*/
870  0,				  /*tp_itemsize*/
871  parmpy_dealloc,		  /*tp_dealloc*/
872  0,				  /*tp_print*/
873  0,				  /*tp_getattr*/
874  0,				  /*tp_setattr*/
875  0,				  /*tp_compare*/
876  0,				  /*tp_repr*/
877  0,				  /*tp_as_number*/
878  0,				  /*tp_as_sequence*/
879  0,				  /*tp_as_mapping*/
880  0,				  /*tp_hash */
881  0,				  /*tp_call*/
882  0,				  /*tp_str*/
883  get_attr,			  /*tp_getattro*/
884  set_attr,			  /*tp_setattro*/
885  0,				  /*tp_as_buffer*/
886  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
887  "GDB parameter object",	  /* tp_doc */
888  0,				  /* tp_traverse */
889  0,				  /* tp_clear */
890  0,				  /* tp_richcompare */
891  0,				  /* tp_weaklistoffset */
892  0,				  /* tp_iter */
893  0,				  /* tp_iternext */
894  0,				  /* tp_methods */
895  0,				  /* tp_members */
896  0,				  /* tp_getset */
897  0,				  /* tp_base */
898  0,				  /* tp_dict */
899  0,				  /* tp_descr_get */
900  0,				  /* tp_descr_set */
901  0,				  /* tp_dictoffset */
902  parmpy_init,			  /* tp_init */
903  0,				  /* tp_alloc */
904};
905