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