]> Git Repo - binutils.git/blob - gdb/python/py-param.c
gdb: remove COMPUNIT_PRODUCER macro, add getter/setter
[binutils.git] / gdb / python / py-param.c
1 /* GDB parameters implemented in Python
2
3    Copyright (C) 2008-2022 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.  */
32 static 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.  */
53 union 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.  */
76 struct 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
96 static setting
97 make_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
115 extern PyTypeObject parmpy_object_type
116     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
117
118 /* Some handy string constants.  */
119 static PyObject *set_doc_cst;
120 static PyObject *show_doc_cst;
121
122 \f
123
124 /* Get an attribute.  */
125 static PyObject *
126 get_attr (PyObject *obj, PyObject *attr_name)
127 {
128   if (PyString_Check (attr_name)
129 #ifdef IS_PY3K
130       && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
131 #else
132       && ! strcmp (PyString_AsString (attr_name), "value"))
133 #endif
134     {
135       parmpy_object *self = (parmpy_object *) obj;
136
137       return gdbpy_parameter_value (make_setting (self));
138     }
139
140   return PyObject_GenericGetAttr (obj, attr_name);
141 }
142
143 /* Set a parameter value from a Python value.  Return 0 on success.  Returns
144    -1 on error, with a python exception set.  */
145 static int
146 set_parameter_value (parmpy_object *self, PyObject *value)
147 {
148   int cmp;
149
150   switch (self->type)
151     {
152     case var_string:
153     case var_string_noescape:
154     case var_optional_filename:
155     case var_filename:
156       if (! gdbpy_is_string (value)
157           && (self->type == var_filename
158               || value != Py_None))
159         {
160           PyErr_SetString (PyExc_RuntimeError,
161                            _("String required for filename."));
162
163           return -1;
164         }
165       if (value == Py_None)
166         self->value.stringval->clear ();
167       else
168         {
169           gdb::unique_xmalloc_ptr<char>
170             string (python_string_to_host_string (value));
171           if (string == NULL)
172             return -1;
173
174           *self->value.stringval = string.get ();
175         }
176       break;
177
178     case var_enum:
179       {
180         int i;
181
182         if (! gdbpy_is_string (value))
183           {
184             PyErr_SetString (PyExc_RuntimeError,
185                              _("ENUM arguments must be a string."));
186             return -1;
187           }
188
189         gdb::unique_xmalloc_ptr<char>
190           str (python_string_to_host_string (value));
191         if (str == NULL)
192           return -1;
193         for (i = 0; self->enumeration[i]; ++i)
194           if (! strcmp (self->enumeration[i], str.get ()))
195             break;
196         if (! self->enumeration[i])
197           {
198             PyErr_SetString (PyExc_RuntimeError,
199                              _("The value must be member of an enumeration."));
200             return -1;
201           }
202         self->value.cstringval = self->enumeration[i];
203         break;
204       }
205
206     case var_boolean:
207       if (! PyBool_Check (value))
208         {
209           PyErr_SetString (PyExc_RuntimeError,
210                            _("A boolean argument is required."));
211           return -1;
212         }
213       cmp = PyObject_IsTrue (value);
214       if (cmp < 0)
215           return -1;
216       self->value.boolval = cmp;
217       break;
218
219     case var_auto_boolean:
220       if (! PyBool_Check (value) && value != Py_None)
221         {
222           PyErr_SetString (PyExc_RuntimeError,
223                            _("A boolean or None is required"));
224           return -1;
225         }
226
227       if (value == Py_None)
228         self->value.autoboolval = AUTO_BOOLEAN_AUTO;
229       else
230         {
231           cmp = PyObject_IsTrue (value);
232           if (cmp < 0 )
233             return -1;  
234           if (cmp == 1)
235             self->value.autoboolval = AUTO_BOOLEAN_TRUE;
236           else
237             self->value.autoboolval = AUTO_BOOLEAN_FALSE;
238         }
239       break;
240
241     case var_integer:
242     case var_zinteger:
243     case var_uinteger:
244     case var_zuinteger:
245     case var_zuinteger_unlimited:
246       {
247         long l;
248         int ok;
249
250         if (! PyInt_Check (value))
251           {
252             PyErr_SetString (PyExc_RuntimeError,
253                              _("The value must be integer."));
254             return -1;
255           }
256
257         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.  */
312 static int
313 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
314 {
315   if (PyString_Check (attr_name)
316 #ifdef IS_PY3K
317       && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
318 #else
319       && ! strcmp (PyString_AsString (attr_name), "value"))
320 #endif
321     {
322       if (!val)
323         {
324           PyErr_SetString (PyExc_RuntimeError,
325                            _("Cannot delete a parameter's value."));
326           return -1;
327         }
328       return set_parameter_value ((parmpy_object *) obj, val);
329     }
330
331   return PyObject_GenericSetAttr (obj, attr_name, val);
332 }
333
334 /* Build up the path to command C, but drop the first component of the
335    command prefix.  This is only intended for use with the set/show
336    parameters this file deals with, the first prefix should always be
337    either 'set' or 'show'.
338
339    As an example, if this full command is 'set prefix_a prefix_b command'
340    this function will return the string 'prefix_a prefix_b command'.  */
341
342 static std::string
343 full_cmd_name_without_first_prefix (struct cmd_list_element *c)
344 {
345   std::vector<std::string> components
346     = c->command_components ();
347   gdb_assert (components.size () > 1);
348   std::string result = components[1];
349   for (int i = 2; i < components.size (); ++i)
350     result += " " + components[i];
351   return result;
352 }
353
354 /* The different types of documentation string.  */
355
356 enum doc_string_type
357 {
358   doc_string_set,
359   doc_string_show,
360   doc_string_description
361 };
362
363 /* A helper function which returns a documentation string for an
364    object. */
365
366 static gdb::unique_xmalloc_ptr<char>
367 get_doc_string (PyObject *object, enum doc_string_type doc_type,
368                 const char *cmd_name)
369 {
370   gdb::unique_xmalloc_ptr<char> result;
371
372   PyObject *attr = nullptr;
373   switch (doc_type)
374     {
375     case doc_string_set:
376       attr = set_doc_cst;
377       break;
378     case doc_string_show:
379       attr = show_doc_cst;
380       break;
381     case doc_string_description:
382       attr = gdbpy_doc_cst;
383       break;
384     }
385   gdb_assert (attr != nullptr);
386
387   if (PyObject_HasAttr (object, attr))
388     {
389       gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
390
391       if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
392         {
393           result = python_string_to_host_string (ds_obj.get ());
394           if (result == NULL)
395             gdbpy_print_stack ();
396         }
397     }
398
399   if (result == nullptr)
400     {
401       if (doc_type == doc_string_description)
402         result.reset (xstrdup (_("This command is not documented.")));
403       else
404         {
405           if (doc_type == doc_string_show)
406             result = xstrprintf (_("Show the current value of '%s'."),
407                                  cmd_name);
408           else
409             result = xstrprintf (_("Set the current value of '%s'."),
410                                  cmd_name);
411         }
412     }
413   return result;
414 }
415
416 /* Helper function which will execute a METHOD in OBJ passing the
417    argument ARG.  ARG can be NULL.  METHOD should return a Python
418    string.  If this function returns NULL, there has been an error and
419    the appropriate exception set.  */
420 static gdb::unique_xmalloc_ptr<char>
421 call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
422 {
423   gdb::unique_xmalloc_ptr<char> data;
424   gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
425
426   if (result == NULL)
427     return NULL;
428
429   if (gdbpy_is_string (result.get ()))
430     {
431       data = python_string_to_host_string (result.get ());
432       if (! data)
433         return NULL;
434     }
435   else
436     {
437       PyErr_SetString (PyExc_RuntimeError,
438                        _("Parameter must return a string value."));
439       return NULL;
440     }
441
442   return data;
443 }
444
445 /* A callback function that is registered against the respective
446    add_setshow_* set_doc prototype.  This function calls the Python function
447    "get_set_string" if it exists, which will return a string.  That string
448    is then printed.  If "get_set_string" does not exist, or returns an
449    empty string, then nothing is printed.  */
450 static void
451 get_set_value (const char *args, int from_tty,
452                struct cmd_list_element *c)
453 {
454   PyObject *obj = (PyObject *) c->context ();
455   gdb::unique_xmalloc_ptr<char> set_doc_string;
456
457   gdbpy_enter enter_py;
458   gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
459
460   if (set_doc_func == NULL)
461     {
462       gdbpy_print_stack ();
463       return;
464     }
465
466   if (PyObject_HasAttr (obj, set_doc_func.get ()))
467     {
468       set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
469       if (! set_doc_string)
470         gdbpy_handle_exception ();
471     }
472
473   const char *str = set_doc_string.get ();
474   if (str != nullptr && str[0] != '\0')
475     printf_filtered ("%s\n", str);
476 }
477
478 /* A callback function that is registered against the respective
479    add_setshow_* show_doc prototype.  This function will either call
480    the Python function "get_show_string" or extract the Python
481    attribute "show_doc" and return the contents as a string.  If
482    neither exist, insert a string indicating the Parameter is not
483    documented.  */
484 static void
485 get_show_value (struct ui_file *file, int from_tty,
486                 struct cmd_list_element *c,
487                 const char *value)
488 {
489   PyObject *obj = (PyObject *) c->context ();
490   gdb::unique_xmalloc_ptr<char> show_doc_string;
491
492   gdbpy_enter enter_py;
493   gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
494
495   if (show_doc_func == NULL)
496     {
497       gdbpy_print_stack ();
498       return;
499     }
500
501   if (PyObject_HasAttr (obj, show_doc_func.get ()))
502     {
503       gdbpy_ref<> val_obj (PyString_FromString (value));
504
505       if (val_obj == NULL)
506         {
507           gdbpy_print_stack ();
508           return;
509         }
510
511       show_doc_string = call_doc_function (obj, show_doc_func.get (),
512                                            val_obj.get ());
513       if (! show_doc_string)
514         {
515           gdbpy_print_stack ();
516           return;
517         }
518
519       fprintf_filtered (file, "%s\n", show_doc_string.get ());
520     }
521   else
522     {
523       /* If there is no 'get_show_string' callback then we want to show
524          something sensible here.  In older versions of GDB (< 7.3) we
525          didn't support 'get_show_string', and instead we just made use of
526          GDB's builtin use of the show_doc.  However, GDB's builtin
527          show_doc adjustment is not i18n friendly, so, instead, we just
528          print this generic string.  */
529       std::string cmd_path = full_cmd_name_without_first_prefix (c);
530       fprintf_filtered (file, _("The current value of '%s' is \"%s\".\n"),
531                                 cmd_path.c_str (), value);
532     }
533 }
534 \f
535
536 /* A helper function that dispatches to the appropriate add_setshow
537    function.  */
538 static void
539 add_setshow_generic (int parmclass, enum command_class cmdclass,
540                      gdb::unique_xmalloc_ptr<char> cmd_name,
541                      parmpy_object *self,
542                      const char *set_doc, const char *show_doc,
543                      const char *help_doc,
544                      struct cmd_list_element **set_list,
545                      struct cmd_list_element **show_list)
546 {
547   set_show_commands commands;
548
549   switch (parmclass)
550     {
551     case var_boolean:
552       commands = add_setshow_boolean_cmd (cmd_name.get (), cmdclass,
553                                           &self->value.boolval, set_doc,
554                                           show_doc, help_doc, get_set_value,
555                                           get_show_value, set_list, show_list);
556
557       break;
558
559     case var_auto_boolean:
560       commands = add_setshow_auto_boolean_cmd (cmd_name.get (), cmdclass,
561                                                &self->value.autoboolval,
562                                                set_doc, show_doc, help_doc,
563                                                get_set_value, get_show_value,
564                                                set_list, show_list);
565       break;
566
567     case var_uinteger:
568       commands = add_setshow_uinteger_cmd (cmd_name.get (), cmdclass,
569                                            &self->value.uintval, set_doc,
570                                            show_doc, help_doc, get_set_value,
571                                            get_show_value, set_list, show_list);
572       break;
573
574     case var_integer:
575       commands = add_setshow_integer_cmd (cmd_name.get (), cmdclass,
576                                           &self->value.intval, set_doc,
577                                           show_doc, help_doc, get_set_value,
578                                           get_show_value, set_list, show_list);
579       break;
580
581     case var_string:
582       commands = add_setshow_string_cmd (cmd_name.get (), cmdclass,
583                                          self->value.stringval, set_doc,
584                                          show_doc, help_doc, get_set_value,
585                                          get_show_value, set_list, show_list);
586       break;
587
588     case var_string_noescape:
589       commands = add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
590                                                   self->value.stringval,
591                                                   set_doc, show_doc, help_doc,
592                                                   get_set_value, get_show_value,
593                                                   set_list, show_list);
594       break;
595
596     case var_optional_filename:
597       commands = add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
598                                                     self->value.stringval,
599                                                     set_doc, show_doc, help_doc,
600                                                     get_set_value,
601                                                     get_show_value, set_list,
602                                                     show_list);
603       break;
604
605     case var_filename:
606       commands = add_setshow_filename_cmd (cmd_name.get (), cmdclass,
607                                            self->value.stringval, set_doc,
608                                            show_doc, help_doc, get_set_value,
609                                            get_show_value, set_list, show_list);
610       break;
611
612     case var_zinteger:
613       commands = add_setshow_zinteger_cmd (cmd_name.get (), cmdclass,
614                                            &self->value.intval, set_doc,
615                                            show_doc, help_doc, get_set_value,
616                                            get_show_value, set_list, show_list);
617       break;
618
619     case var_zuinteger:
620       commands = add_setshow_zuinteger_cmd (cmd_name.get (), cmdclass,
621                                             &self->value.uintval, set_doc,
622                                             show_doc, help_doc, get_set_value,
623                                             get_show_value, set_list,
624                                             show_list);
625       break;
626
627     case var_zuinteger_unlimited:
628       commands = add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
629                                                       &self->value.intval,
630                                                       set_doc, show_doc,
631                                                       help_doc, get_set_value,
632                                                       get_show_value, set_list,
633                                                       show_list);
634       break;
635
636     case var_enum:
637       /* Initialize the value, just in case.  */
638       self->value.cstringval = self->enumeration[0];
639       commands = add_setshow_enum_cmd (cmd_name.get (), cmdclass,
640                                        self->enumeration,
641                                        &self->value.cstringval, set_doc,
642                                        show_doc, help_doc, get_set_value,
643                                        get_show_value, set_list, show_list);
644       break;
645
646     default:
647       gdb_assert_not_reached ("Unhandled parameter class.");
648     }
649
650   /* Register Python objects in both commands' context.  */
651   commands.set->set_context (self);
652   commands.show->set_context (self);
653
654   /* We (unfortunately) currently leak the command name.  */
655   cmd_name.release ();
656 }
657
658 /* A helper which computes enum values.  Returns 1 on success.  Returns 0 on
659    error, with a python exception set.  */
660 static int
661 compute_enum_values (parmpy_object *self, PyObject *enum_values)
662 {
663   Py_ssize_t size, i;
664
665   if (! enum_values)
666     {
667       PyErr_SetString (PyExc_RuntimeError,
668                        _("An enumeration is required for PARAM_ENUM."));
669       return 0;
670     }
671
672   if (! PySequence_Check (enum_values))
673     {
674       PyErr_SetString (PyExc_RuntimeError,
675                        _("The enumeration is not a sequence."));
676       return 0;
677     }
678
679   size = PySequence_Size (enum_values);
680   if (size < 0)
681     return 0;
682   if (size == 0)
683     {
684       PyErr_SetString (PyExc_RuntimeError,
685                        _("The enumeration is empty."));
686       return 0;
687     }
688
689   gdb_argv holder (XCNEWVEC (char *, size + 1));
690   char **enumeration = holder.get ();
691
692   for (i = 0; i < size; ++i)
693     {
694       gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
695
696       if (item == NULL)
697         return 0;
698       if (! gdbpy_is_string (item.get ()))
699         {
700           PyErr_SetString (PyExc_RuntimeError,
701                            _("The enumeration item not a string."));
702           return 0;
703         }
704       enumeration[i] = python_string_to_host_string (item.get ()).release ();
705       if (enumeration[i] == NULL)
706         return 0;
707     }
708
709   self->enumeration = const_cast<const char**> (holder.release ());
710   return 1;
711 }
712
713 /* Object initializer; sets up gdb-side structures for command.
714
715    Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
716
717    NAME is the name of the parameter.  It may consist of multiple
718    words, in which case the final word is the name of the new command,
719    and earlier words must be prefix commands.
720
721    CMDCLASS is the kind of command.  It should be one of the COMMAND_*
722    constants defined in the gdb module.
723
724    PARMCLASS is the type of the parameter.  It should be one of the
725    PARAM_* constants defined in the gdb module.
726
727    If PARMCLASS is PARAM_ENUM, then the final argument should be a
728    collection of strings.  These strings are the valid values for this
729    parameter.
730
731    The documentation for the parameter is taken from the doc string
732    for the python class.
733
734    Returns -1 on error, with a python exception set.  */
735
736 static int
737 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
738 {
739   parmpy_object *obj = (parmpy_object *) self;
740   const char *name;
741   gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
742   int parmclass, cmdtype;
743   PyObject *enum_values = NULL;
744   struct cmd_list_element **set_list, **show_list;
745
746   if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
747                           &enum_values))
748     return -1;
749
750   if (cmdtype != no_class && cmdtype != class_run
751       && cmdtype != class_vars && cmdtype != class_stack
752       && cmdtype != class_files && cmdtype != class_support
753       && cmdtype != class_info && cmdtype != class_breakpoint
754       && cmdtype != class_trace && cmdtype != class_obscure
755       && cmdtype != class_maintenance)
756     {
757       PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
758       return -1;
759     }
760
761   if (parmclass != var_boolean /* ARI: var_boolean */
762       && parmclass != var_auto_boolean
763       && parmclass != var_uinteger && parmclass != var_integer
764       && parmclass != var_string && parmclass != var_string_noescape
765       && parmclass != var_optional_filename && parmclass != var_filename
766       && parmclass != var_zinteger && parmclass != var_zuinteger
767       && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
768     {
769       PyErr_SetString (PyExc_RuntimeError,
770                        _("Invalid parameter class argument."));
771       return -1;
772     }
773
774   if (enum_values && parmclass != var_enum)
775     {
776       PyErr_SetString (PyExc_RuntimeError,
777                        _("Only PARAM_ENUM accepts a fourth argument."));
778       return -1;
779     }
780   if (parmclass == var_enum)
781     {
782       if (! compute_enum_values (obj, enum_values))
783         return -1;
784     }
785   else
786     obj->enumeration = NULL;
787   obj->type = (enum var_types) parmclass;
788   memset (&obj->value, 0, sizeof (obj->value));
789
790   if (var_type_uses<std::string> (obj->type))
791     obj->value.stringval = new std::string;
792
793   gdb::unique_xmalloc_ptr<char> cmd_name
794     = gdbpy_parse_command_name (name, &set_list, &setlist);
795   if (cmd_name == nullptr)
796     return -1;
797
798   cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
799   if (cmd_name == nullptr)
800     return -1;
801
802   set_doc = get_doc_string (self, doc_string_set, name);
803   show_doc = get_doc_string (self, doc_string_show, name);
804   doc = get_doc_string (self, doc_string_description, cmd_name.get ());
805
806   Py_INCREF (self);
807
808   try
809     {
810       add_setshow_generic (parmclass, (enum command_class) cmdtype,
811                            std::move (cmd_name), obj,
812                            set_doc.get (), show_doc.get (),
813                            doc.get (), set_list, show_list);
814     }
815   catch (const gdb_exception &except)
816     {
817       Py_DECREF (self);
818       gdbpy_convert_exception (except);
819       return -1;
820     }
821
822   return 0;
823 }
824
825 /* Deallocate function for a gdb.Parameter.  */
826
827 static void
828 parmpy_dealloc (PyObject *obj)
829 {
830   parmpy_object *parm_obj = (parmpy_object *) obj;
831
832   if (var_type_uses<std::string> (parm_obj->type))
833     delete parm_obj->value.stringval;
834 }
835
836 /* Initialize the 'parameters' module.  */
837 int
838 gdbpy_initialize_parameters (void)
839 {
840   int i;
841
842   parmpy_object_type.tp_new = PyType_GenericNew;
843   if (PyType_Ready (&parmpy_object_type) < 0)
844     return -1;
845
846   set_doc_cst = PyString_FromString ("set_doc");
847   if (! set_doc_cst)
848     return -1;
849   show_doc_cst = PyString_FromString ("show_doc");
850   if (! show_doc_cst)
851     return -1;
852
853   for (i = 0; parm_constants[i].name; ++i)
854     {
855       if (PyModule_AddIntConstant (gdb_module,
856                                    parm_constants[i].name,
857                                    parm_constants[i].value) < 0)
858         return -1;
859     }
860
861   return gdb_pymodule_addobject (gdb_module, "Parameter",
862                                  (PyObject *) &parmpy_object_type);
863 }
864
865 \f
866
867 PyTypeObject parmpy_object_type =
868 {
869   PyVarObject_HEAD_INIT (NULL, 0)
870   "gdb.Parameter",                /*tp_name*/
871   sizeof (parmpy_object),         /*tp_basicsize*/
872   0,                              /*tp_itemsize*/
873   parmpy_dealloc,                 /*tp_dealloc*/
874   0,                              /*tp_print*/
875   0,                              /*tp_getattr*/
876   0,                              /*tp_setattr*/
877   0,                              /*tp_compare*/
878   0,                              /*tp_repr*/
879   0,                              /*tp_as_number*/
880   0,                              /*tp_as_sequence*/
881   0,                              /*tp_as_mapping*/
882   0,                              /*tp_hash */
883   0,                              /*tp_call*/
884   0,                              /*tp_str*/
885   get_attr,                       /*tp_getattro*/
886   set_attr,                       /*tp_setattro*/
887   0,                              /*tp_as_buffer*/
888   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
889   "GDB parameter object",         /* tp_doc */
890   0,                              /* tp_traverse */
891   0,                              /* tp_clear */
892   0,                              /* tp_richcompare */
893   0,                              /* tp_weaklistoffset */
894   0,                              /* tp_iter */
895   0,                              /* tp_iternext */
896   0,                              /* tp_methods */
897   0,                              /* tp_members */
898   0,                              /* tp_getset */
899   0,                              /* tp_base */
900   0,                              /* tp_dict */
901   0,                              /* tp_descr_get */
902   0,                              /* tp_descr_set */
903   0,                              /* tp_dictoffset */
904   parmpy_init,                    /* tp_init */
905   0,                              /* tp_alloc */
906 };
This page took 0.082284 seconds and 4 git commands to generate.