]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-param.c
GDB copyright headers update after running GDB's copyright.py script.
[thirdparty/binutils-gdb.git] / gdb / python / py-param.c
CommitLineData
d7b32ed3
PM
1/* GDB parameters implemented in Python
2
618f726f 3 Copyright (C) 2008-2016 Free Software Foundation, Inc.
d7b32ed3
PM
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"
d7b32ed3
PM
23#include "python-internal.h"
24#include "charset.h"
25#include "gdbcmd.h"
26#include "cli/cli-decode.h"
27#include "completer.h"
ecec24e6
PM
28#include "language.h"
29#include "arch-utils.h"
d7b32ed3
PM
30
31/* Parameter constants and their values. */
32struct parm_constant
33{
34 char *name;
35 int value;
36};
37
38struct parm_constant parm_constants[] =
39{
3c0ee1a4 40 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
d7b32ed3
PM
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_ENUM", var_enum },
50 { NULL, 0 }
51};
52
53/* A union that can hold anything described by enum var_types. */
54union parmpy_variable
55{
56 /* Hold an integer value, for boolean and integer types. */
57 int intval;
58
59 /* Hold an auto_boolean. */
60 enum auto_boolean autoboolval;
61
62 /* Hold an unsigned integer value, for uinteger. */
63 unsigned int uintval;
64
65 /* Hold a string, for the various string types. */
66 char *stringval;
67
68 /* Hold a string, for enums. */
69 const char *cstringval;
70};
71
72/* A GDB parameter. */
73struct parmpy_object
74{
75 PyObject_HEAD
76
77 /* The type of the parameter. */
78 enum var_types type;
79
80 /* The value of the parameter. */
81 union parmpy_variable value;
82
83 /* For an enum command, the possible values. The vector is
84 allocated with xmalloc, as is each element. It is
85 NULL-terminated. */
86 const char **enumeration;
87};
88
89typedef struct parmpy_object parmpy_object;
90
e36122e9 91extern PyTypeObject parmpy_object_type
62eec1a5 92 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
d7b32ed3
PM
93
94/* Some handy string constants. */
95static PyObject *set_doc_cst;
96static PyObject *show_doc_cst;
97
98\f
99
100/* Get an attribute. */
101static PyObject *
102get_attr (PyObject *obj, PyObject *attr_name)
103{
104 if (PyString_Check (attr_name)
9a27f2c6
PK
105#ifdef IS_PY3K
106 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
107#else
d7b32ed3 108 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 109#endif
d7b32ed3
PM
110 {
111 parmpy_object *self = (parmpy_object *) obj;
d59b6f6c 112
d7b32ed3
PM
113 return gdbpy_parameter_value (self->type, &self->value);
114 }
115
116 return PyObject_GenericGetAttr (obj, attr_name);
117}
118
8dc78533
JK
119/* Set a parameter value from a Python value. Return 0 on success. Returns
120 -1 on error, with a python exception set. */
d7b32ed3
PM
121static int
122set_parameter_value (parmpy_object *self, PyObject *value)
123{
124 int cmp;
125
126 switch (self->type)
127 {
128 case var_string:
129 case var_string_noescape:
130 case var_optional_filename:
131 case var_filename:
132 if (! gdbpy_is_string (value)
133 && (self->type == var_filename
134 || value != Py_None))
135 {
256458bc 136 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
137 _("String required for filename."));
138
139 return -1;
140 }
d7b32ed3
PM
141 if (value == Py_None)
142 {
8dc78533 143 xfree (self->value.stringval);
d7b32ed3
PM
144 if (self->type == var_optional_filename)
145 self->value.stringval = xstrdup ("");
146 else
147 self->value.stringval = NULL;
148 }
149 else
8dc78533
JK
150 {
151 char *string;
152
153 string = python_string_to_host_string (value);
154 if (string == NULL)
155 return -1;
156
157 xfree (self->value.stringval);
158 self->value.stringval = string;
159 }
d7b32ed3
PM
160 break;
161
162 case var_enum:
163 {
164 int i;
165 char *str;
166
167 if (! gdbpy_is_string (value))
168 {
256458bc 169 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
170 _("ENUM arguments must be a string."));
171 return -1;
172 }
173
174 str = python_string_to_host_string (value);
8dc78533
JK
175 if (str == NULL)
176 return -1;
d7b32ed3
PM
177 for (i = 0; self->enumeration[i]; ++i)
178 if (! strcmp (self->enumeration[i], str))
179 break;
180 xfree (str);
181 if (! self->enumeration[i])
182 {
183 PyErr_SetString (PyExc_RuntimeError,
184 _("The value must be member of an enumeration."));
185 return -1;
186 }
187 self->value.cstringval = self->enumeration[i];
188 break;
189 }
190
191 case var_boolean:
192 if (! PyBool_Check (value))
193 {
256458bc 194 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
195 _("A boolean argument is required."));
196 return -1;
197 }
198 cmp = PyObject_IsTrue (value);
256458bc 199 if (cmp < 0)
d7b32ed3
PM
200 return -1;
201 self->value.intval = cmp;
202 break;
203
204 case var_auto_boolean:
205 if (! PyBool_Check (value) && value != Py_None)
206 {
207 PyErr_SetString (PyExc_RuntimeError,
208 _("A boolean or None is required"));
209 return -1;
210 }
211
212 if (value == Py_None)
213 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
214 else
215 {
216 cmp = PyObject_IsTrue (value);
217 if (cmp < 0 )
256458bc 218 return -1;
d7b32ed3
PM
219 if (cmp == 1)
220 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
256458bc 221 else
d7b32ed3 222 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
d7b32ed3 223 }
92e96192 224 break;
d7b32ed3
PM
225
226 case var_integer:
227 case var_zinteger:
228 case var_uinteger:
229 {
230 long l;
231 int ok;
232
233 if (! PyInt_Check (value))
234 {
256458bc 235 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
236 _("The value must be integer."));
237 return -1;
238 }
239
74aedc46
TT
240 if (! gdb_py_int_as_long (value, &l))
241 return -1;
242
d7b32ed3
PM
243 if (self->type == var_uinteger)
244 {
245 ok = (l >= 0 && l <= UINT_MAX);
246 if (l == 0)
247 l = UINT_MAX;
248 }
249 else if (self->type == var_integer)
250 {
251 ok = (l >= INT_MIN && l <= INT_MAX);
252 if (l == 0)
253 l = INT_MAX;
254 }
255 else
256 ok = (l >= INT_MIN && l <= INT_MAX);
257
258 if (! ok)
259 {
256458bc 260 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
261 _("Range exceeded."));
262 return -1;
263 }
264
265 self->value.intval = (int) l;
266 break;
267 }
268
269 default:
256458bc 270 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
271 _("Unhandled type in parameter value."));
272 return -1;
273 }
274
275 return 0;
276}
277
8dc78533 278/* Set an attribute. Returns -1 on error, with a python exception set. */
d7b32ed3
PM
279static int
280set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
281{
282 if (PyString_Check (attr_name)
9a27f2c6
PK
283#ifdef IS_PY3K
284 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
285#else
d7b32ed3 286 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 287#endif
d7b32ed3
PM
288 {
289 if (!val)
290 {
291 PyErr_SetString (PyExc_RuntimeError,
292 _("Cannot delete a parameter's value."));
293 return -1;
294 }
295 return set_parameter_value ((parmpy_object *) obj, val);
296 }
297
298 return PyObject_GenericSetAttr (obj, attr_name, val);
299}
300
ecec24e6
PM
301/* A helper function which returns a documentation string for an
302 object. */
303
304static char *
305get_doc_string (PyObject *object, PyObject *attr)
306{
307 char *result = NULL;
308
309 if (PyObject_HasAttr (object, attr))
310 {
311 PyObject *ds_obj = PyObject_GetAttr (object, attr);
312
313 if (ds_obj && gdbpy_is_string (ds_obj))
314 {
315 result = python_string_to_host_string (ds_obj);
316 if (result == NULL)
317 gdbpy_print_stack ();
318 }
319 Py_XDECREF (ds_obj);
320 }
321 if (! result)
322 result = xstrdup (_("This command is not documented."));
323 return result;
324}
325
326/* Helper function which will execute a METHOD in OBJ passing the
327 argument ARG. ARG can be NULL. METHOD should return a Python
328 string. If this function returns NULL, there has been an error and
329 the appropriate exception set. */
330static char *
331call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
332{
333 char *data = NULL;
334 PyObject *result = PyObject_CallMethodObjArgs (obj, method, arg, NULL);
335
336 if (! result)
337 return NULL;
338
339 if (gdbpy_is_string (result))
340 {
341 data = python_string_to_host_string (result);
8432bc41 342 Py_DECREF (result);
ecec24e6
PM
343 if (! data)
344 return NULL;
345 }
346 else
347 {
348 PyErr_SetString (PyExc_RuntimeError,
349 _("Parameter must return a string value."));
8432bc41 350 Py_DECREF (result);
ecec24e6
PM
351 return NULL;
352 }
353
354 return data;
355}
356
357/* A callback function that is registered against the respective
358 add_setshow_* set_doc prototype. This function will either call
359 the Python function "get_set_string" or extract the Python
360 attribute "set_doc" and return the contents as a string. If
361 neither exist, insert a string indicating the Parameter is not
362 documented. */
363static void
364get_set_value (char *args, int from_tty,
365 struct cmd_list_element *c)
366{
367 PyObject *obj = (PyObject *) get_cmd_context (c);
368 char *set_doc_string;
369 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
370 current_language);
371 PyObject *set_doc_func = PyString_FromString ("get_set_string");
372
373 if (! set_doc_func)
374 goto error;
375
ecec24e6
PM
376 if (PyObject_HasAttr (obj, set_doc_func))
377 {
378 set_doc_string = call_doc_function (obj, set_doc_func, NULL);
379 if (! set_doc_string)
380 goto error;
381 }
382 else
383 {
384 /* We have to preserve the existing < GDB 7.3 API. If a
385 callback function does not exist, then attempt to read the
386 set_doc attribute. */
387 set_doc_string = get_doc_string (obj, set_doc_cst);
388 }
389
390 make_cleanup (xfree, set_doc_string);
391 fprintf_filtered (gdb_stdout, "%s\n", set_doc_string);
392
3d4a3c3e 393 Py_XDECREF (set_doc_func);
ecec24e6
PM
394 do_cleanups (cleanup);
395 return;
396
397 error:
3d4a3c3e 398 Py_XDECREF (set_doc_func);
ecec24e6
PM
399 gdbpy_print_stack ();
400 do_cleanups (cleanup);
401 return;
402}
403
404/* A callback function that is registered against the respective
405 add_setshow_* show_doc prototype. This function will either call
406 the Python function "get_show_string" or extract the Python
407 attribute "show_doc" and return the contents as a string. If
408 neither exist, insert a string indicating the Parameter is not
409 documented. */
410static void
411get_show_value (struct ui_file *file, int from_tty,
412 struct cmd_list_element *c,
413 const char *value)
414{
415 PyObject *obj = (PyObject *) get_cmd_context (c);
416 char *show_doc_string = NULL;
417 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
418 current_language);
419 PyObject *show_doc_func = PyString_FromString ("get_show_string");
420
421 if (! show_doc_func)
422 goto error;
423
ecec24e6
PM
424 if (PyObject_HasAttr (obj, show_doc_func))
425 {
426 PyObject *val_obj = PyString_FromString (value);
427
428 if (! val_obj)
429 goto error;
430
ecec24e6 431 show_doc_string = call_doc_function (obj, show_doc_func, val_obj);
3d4a3c3e 432 Py_DECREF (val_obj);
ecec24e6
PM
433 if (! show_doc_string)
434 goto error;
435
436 make_cleanup (xfree, show_doc_string);
437
438 fprintf_filtered (file, "%s\n", show_doc_string);
439 }
440 else
441 {
442 /* We have to preserve the existing < GDB 7.3 API. If a
443 callback function does not exist, then attempt to read the
444 show_doc attribute. */
445 show_doc_string = get_doc_string (obj, show_doc_cst);
446 make_cleanup (xfree, show_doc_string);
447 fprintf_filtered (file, "%s %s\n", show_doc_string, value);
448 }
449
3d4a3c3e 450 Py_XDECREF (show_doc_func);
ecec24e6
PM
451 do_cleanups (cleanup);
452 return;
453
454 error:
3d4a3c3e 455 Py_XDECREF (show_doc_func);
ecec24e6
PM
456 gdbpy_print_stack ();
457 do_cleanups (cleanup);
458 return;
459}
d7b32ed3
PM
460\f
461
462/* A helper function that dispatches to the appropriate add_setshow
463 function. */
464static void
465add_setshow_generic (int parmclass, enum command_class cmdclass,
466 char *cmd_name, parmpy_object *self,
467 char *set_doc, char *show_doc, char *help_doc,
468 struct cmd_list_element **set_list,
469 struct cmd_list_element **show_list)
470{
ecec24e6 471 struct cmd_list_element *param = NULL;
6f937416 472 const char *tmp_name = NULL;
ecec24e6 473
d7b32ed3
PM
474 switch (parmclass)
475 {
476 case var_boolean:
ecec24e6
PM
477
478 add_setshow_boolean_cmd (cmd_name, cmdclass,
479 &self->value.intval, set_doc, show_doc,
480 help_doc, get_set_value, get_show_value,
481 set_list, show_list);
482
d7b32ed3
PM
483 break;
484
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,
ecec24e6
PM
489 get_set_value, get_show_value,
490 set_list, show_list);
d7b32ed3
PM
491 break;
492
493 case var_uinteger:
ecec24e6
PM
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);
d7b32ed3
PM
498 break;
499
500 case var_integer:
ecec24e6
PM
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;
d7b32ed3
PM
505
506 case var_string:
ecec24e6
PM
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;
d7b32ed3
PM
511
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,
ecec24e6
PM
516 get_set_value, get_show_value,
517 set_list, show_list);
518
d7b32ed3
PM
519 break;
520
521 case var_optional_filename:
522 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
ecec24e6
PM
523 &self->value.stringval, set_doc,
524 show_doc, help_doc, get_set_value,
525 get_show_value, set_list,
526 show_list);
d7b32ed3
PM
527 break;
528
529 case var_filename:
ecec24e6
PM
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;
d7b32ed3
PM
534
535 case var_zinteger:
ecec24e6
PM
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);
d7b32ed3
PM
540 break;
541
542 case var_enum:
543 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
ecec24e6
PM
544 &self->value.cstringval, set_doc, show_doc,
545 help_doc, get_set_value, get_show_value,
546 set_list, show_list);
d7b32ed3
PM
547 /* Initialize the value, just in case. */
548 self->value.cstringval = self->enumeration[0];
549 break;
550 }
ecec24e6
PM
551
552 /* Lookup created parameter, and register Python object against the
553 parameter context. Perform this task against both lists. */
554 tmp_name = cmd_name;
555 param = lookup_cmd (&tmp_name, *show_list, "", 0, 1);
556 if (param)
557 set_cmd_context (param, self);
558
559 tmp_name = cmd_name;
560 param = lookup_cmd (&tmp_name, *set_list, "", 0, 1);
561 if (param)
562 set_cmd_context (param, self);
d7b32ed3
PM
563}
564
8dc78533
JK
565/* A helper which computes enum values. Returns 1 on success. Returns 0 on
566 error, with a python exception set. */
d7b32ed3
PM
567static int
568compute_enum_values (parmpy_object *self, PyObject *enum_values)
569{
570 Py_ssize_t size, i;
8dc78533 571 struct cleanup *back_to;
d7b32ed3
PM
572
573 if (! enum_values)
574 {
575 PyErr_SetString (PyExc_RuntimeError,
576 _("An enumeration is required for PARAM_ENUM."));
577 return 0;
578 }
579
580 if (! PySequence_Check (enum_values))
581 {
256458bc 582 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
583 _("The enumeration is not a sequence."));
584 return 0;
585 }
586
587 size = PySequence_Size (enum_values);
588 if (size < 0)
589 return 0;
590 if (size == 0)
591 {
256458bc 592 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
593 _("The enumeration is empty."));
594 return 0;
595 }
596
8d749320 597 self->enumeration = XCNEWVEC (const char *, size + 1);
8dc78533 598 back_to = make_cleanup (free_current_contents, &self->enumeration);
d7b32ed3
PM
599
600 for (i = 0; i < size; ++i)
601 {
602 PyObject *item = PySequence_GetItem (enum_values, i);
d59b6f6c 603
d7b32ed3 604 if (! item)
8dc78533
JK
605 {
606 do_cleanups (back_to);
607 return 0;
608 }
d7b32ed3
PM
609 if (! gdbpy_is_string (item))
610 {
fcb49fc8 611 Py_DECREF (item);
8dc78533 612 do_cleanups (back_to);
256458bc 613 PyErr_SetString (PyExc_RuntimeError,
d7b32ed3
PM
614 _("The enumeration item not a string."));
615 return 0;
616 }
617 self->enumeration[i] = python_string_to_host_string (item);
fcb49fc8 618 Py_DECREF (item);
8dc78533
JK
619 if (self->enumeration[i] == NULL)
620 {
621 do_cleanups (back_to);
622 return 0;
623 }
624 make_cleanup (xfree, (char *) self->enumeration[i]);
d7b32ed3
PM
625 }
626
8dc78533 627 discard_cleanups (back_to);
d7b32ed3
PM
628 return 1;
629}
630
d7b32ed3
PM
631/* Object initializer; sets up gdb-side structures for command.
632
633 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
634
635 NAME is the name of the parameter. It may consist of multiple
636 words, in which case the final word is the name of the new command,
637 and earlier words must be prefix commands.
638
639 CMDCLASS is the kind of command. It should be one of the COMMAND_*
640 constants defined in the gdb module.
641
642 PARMCLASS is the type of the parameter. It should be one of the
643 PARAM_* constants defined in the gdb module.
644
645 If PARMCLASS is PARAM_ENUM, then the final argument should be a
646 collection of strings. These strings are the valid values for this
647 parameter.
648
649 The documentation for the parameter is taken from the doc string
650 for the python class.
8dc78533
JK
651
652 Returns -1 on error, with a python exception set. */
653
d7b32ed3
PM
654static int
655parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
656{
657 parmpy_object *obj = (parmpy_object *) self;
ddd49eee 658 const char *name;
d7b32ed3
PM
659 char *set_doc, *show_doc, *doc;
660 char *cmd_name;
661 int parmclass, cmdtype;
662 PyObject *enum_values = NULL;
d7b32ed3 663 struct cmd_list_element **set_list, **show_list;
d7b32ed3
PM
664
665 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
666 &enum_values))
667 return -1;
668
669 if (cmdtype != no_class && cmdtype != class_run
670 && cmdtype != class_vars && cmdtype != class_stack
671 && cmdtype != class_files && cmdtype != class_support
672 && cmdtype != class_info && cmdtype != class_breakpoint
673 && cmdtype != class_trace && cmdtype != class_obscure
674 && cmdtype != class_maintenance)
675 {
676 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
677 return -1;
678 }
679
3c0ee1a4
PM
680 if (parmclass != var_boolean /* ARI: var_boolean */
681 && parmclass != var_auto_boolean
d7b32ed3
PM
682 && parmclass != var_uinteger && parmclass != var_integer
683 && parmclass != var_string && parmclass != var_string_noescape
684 && parmclass != var_optional_filename && parmclass != var_filename
685 && parmclass != var_zinteger && parmclass != var_enum)
686 {
9a2b4c1b
MS
687 PyErr_SetString (PyExc_RuntimeError,
688 _("Invalid parameter class argument."));
d7b32ed3
PM
689 return -1;
690 }
691
692 if (enum_values && parmclass != var_enum)
693 {
694 PyErr_SetString (PyExc_RuntimeError,
695 _("Only PARAM_ENUM accepts a fourth argument."));
696 return -1;
697 }
698 if (parmclass == var_enum)
699 {
700 if (! compute_enum_values (obj, enum_values))
701 return -1;
702 }
703 else
704 obj->enumeration = NULL;
705 obj->type = (enum var_types) parmclass;
706 memset (&obj->value, 0, sizeof (obj->value));
707
63d97a20 708 cmd_name = gdbpy_parse_command_name (name, &set_list,
d7b32ed3
PM
709 &setlist);
710
711 if (! cmd_name)
63d97a20 712 return -1;
d7b32ed3 713 xfree (cmd_name);
63d97a20 714 cmd_name = gdbpy_parse_command_name (name, &show_list,
d7b32ed3
PM
715 &showlist);
716 if (! cmd_name)
717 return -1;
718
719 set_doc = get_doc_string (self, set_doc_cst);
720 show_doc = get_doc_string (self, show_doc_cst);
721 doc = get_doc_string (self, gdbpy_doc_cst);
722
723 Py_INCREF (self);
724
492d29ea 725 TRY
d7b32ed3
PM
726 {
727 add_setshow_generic (parmclass, (enum command_class) cmdtype,
728 cmd_name, obj,
729 set_doc, show_doc,
730 doc, set_list, show_list);
731 }
492d29ea 732 CATCH (except, RETURN_MASK_ALL)
d7b32ed3
PM
733 {
734 xfree (cmd_name);
735 xfree (set_doc);
736 xfree (show_doc);
737 xfree (doc);
738 Py_DECREF (self);
739 PyErr_Format (except.reason == RETURN_QUIT
740 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
741 "%s", except.message);
742 return -1;
743 }
492d29ea
PA
744 END_CATCH
745
d7b32ed3
PM
746 return 0;
747}
748
749\f
750
751/* Initialize the 'parameters' module. */
999633ed 752int
d7b32ed3
PM
753gdbpy_initialize_parameters (void)
754{
755 int i;
756
6a1b1664 757 parmpy_object_type.tp_new = PyType_GenericNew;
d7b32ed3 758 if (PyType_Ready (&parmpy_object_type) < 0)
999633ed 759 return -1;
d7b32ed3
PM
760
761 set_doc_cst = PyString_FromString ("set_doc");
762 if (! set_doc_cst)
999633ed 763 return -1;
d7b32ed3
PM
764 show_doc_cst = PyString_FromString ("show_doc");
765 if (! show_doc_cst)
999633ed 766 return -1;
d7b32ed3
PM
767
768 for (i = 0; parm_constants[i].name; ++i)
769 {
770 if (PyModule_AddIntConstant (gdb_module,
771 parm_constants[i].name,
772 parm_constants[i].value) < 0)
999633ed 773 return -1;
d7b32ed3
PM
774 }
775
aa36459a
TT
776 return gdb_pymodule_addobject (gdb_module, "Parameter",
777 (PyObject *) &parmpy_object_type);
d7b32ed3
PM
778}
779
780\f
781
e36122e9 782PyTypeObject parmpy_object_type =
d7b32ed3 783{
9a27f2c6 784 PyVarObject_HEAD_INIT (NULL, 0)
d7b32ed3
PM
785 "gdb.Parameter", /*tp_name*/
786 sizeof (parmpy_object), /*tp_basicsize*/
787 0, /*tp_itemsize*/
788 0, /*tp_dealloc*/
789 0, /*tp_print*/
790 0, /*tp_getattr*/
791 0, /*tp_setattr*/
792 0, /*tp_compare*/
793 0, /*tp_repr*/
794 0, /*tp_as_number*/
795 0, /*tp_as_sequence*/
796 0, /*tp_as_mapping*/
797 0, /*tp_hash */
798 0, /*tp_call*/
799 0, /*tp_str*/
800 get_attr, /*tp_getattro*/
801 set_attr, /*tp_setattro*/
802 0, /*tp_as_buffer*/
803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
804 "GDB parameter object", /* tp_doc */
805 0, /* tp_traverse */
806 0, /* tp_clear */
807 0, /* tp_richcompare */
808 0, /* tp_weaklistoffset */
809 0, /* tp_iter */
810 0, /* tp_iternext */
811 0, /* tp_methods */
812 0, /* tp_members */
813 0, /* tp_getset */
814 0, /* tp_base */
815 0, /* tp_dict */
816 0, /* tp_descr_get */
817 0, /* tp_descr_set */
818 0, /* tp_dictoffset */
819 parmpy_init, /* tp_init */
820 0, /* tp_alloc */
d7b32ed3 821};