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