* psymtab.c (expand_partial_symbol_tables): Use
[deliverable/binutils-gdb.git] / gdb / python / py-param.c
CommitLineData
d7b32ed3
PM
1/* GDB parameters implemented in Python
2
7b6bb8da 3 Copyright (C) 2008, 2009, 2010, 2011 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"
23#include "exceptions.h"
24#include "python-internal.h"
25#include "charset.h"
26#include "gdbcmd.h"
27#include "cli/cli-decode.h"
28#include "completer.h"
29
30/* Parameter constants and their values. */
31struct parm_constant
32{
33 char *name;
34 int value;
35};
36
37struct parm_constant parm_constants[] =
38{
3c0ee1a4 39 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
d7b32ed3
PM
40 { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
41 { "PARAM_UINTEGER", var_uinteger },
42 { "PARAM_INTEGER", var_integer },
43 { "PARAM_STRING", var_string },
44 { "PARAM_STRING_NOESCAPE", var_string_noescape },
45 { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
46 { "PARAM_FILENAME", var_filename },
47 { "PARAM_ZINTEGER", var_zinteger },
48 { "PARAM_ENUM", var_enum },
49 { NULL, 0 }
50};
51
52/* A union that can hold anything described by enum var_types. */
53union parmpy_variable
54{
55 /* Hold an integer value, for boolean and integer types. */
56 int intval;
57
58 /* Hold an auto_boolean. */
59 enum auto_boolean autoboolval;
60
61 /* Hold an unsigned integer value, for uinteger. */
62 unsigned int uintval;
63
64 /* Hold a string, for the various string types. */
65 char *stringval;
66
67 /* Hold a string, for enums. */
68 const char *cstringval;
69};
70
71/* A GDB parameter. */
72struct parmpy_object
73{
74 PyObject_HEAD
75
76 /* The type of the parameter. */
77 enum var_types type;
78
79 /* The value of the parameter. */
80 union parmpy_variable value;
81
82 /* For an enum command, the possible values. The vector is
83 allocated with xmalloc, as is each element. It is
84 NULL-terminated. */
85 const char **enumeration;
86};
87
88typedef struct parmpy_object parmpy_object;
89
90static PyTypeObject parmpy_object_type;
91
92/* Some handy string constants. */
93static PyObject *set_doc_cst;
94static PyObject *show_doc_cst;
95
96\f
97
98/* Get an attribute. */
99static PyObject *
100get_attr (PyObject *obj, PyObject *attr_name)
101{
102 if (PyString_Check (attr_name)
103 && ! strcmp (PyString_AsString (attr_name), "value"))
104 {
105 parmpy_object *self = (parmpy_object *) obj;
d59b6f6c 106
d7b32ed3
PM
107 return gdbpy_parameter_value (self->type, &self->value);
108 }
109
110 return PyObject_GenericGetAttr (obj, attr_name);
111}
112
8dc78533
JK
113/* Set a parameter value from a Python value. Return 0 on success. Returns
114 -1 on error, with a python exception set. */
d7b32ed3
PM
115static int
116set_parameter_value (parmpy_object *self, PyObject *value)
117{
118 int cmp;
119
120 switch (self->type)
121 {
122 case var_string:
123 case var_string_noescape:
124 case var_optional_filename:
125 case var_filename:
126 if (! gdbpy_is_string (value)
127 && (self->type == var_filename
128 || value != Py_None))
129 {
130 PyErr_SetString (PyExc_RuntimeError,
131 _("String required for filename."));
132
133 return -1;
134 }
d7b32ed3
PM
135 if (value == Py_None)
136 {
8dc78533 137 xfree (self->value.stringval);
d7b32ed3
PM
138 if (self->type == var_optional_filename)
139 self->value.stringval = xstrdup ("");
140 else
141 self->value.stringval = NULL;
142 }
143 else
8dc78533
JK
144 {
145 char *string;
146
147 string = python_string_to_host_string (value);
148 if (string == NULL)
149 return -1;
150
151 xfree (self->value.stringval);
152 self->value.stringval = string;
153 }
d7b32ed3
PM
154 break;
155
156 case var_enum:
157 {
158 int i;
159 char *str;
160
161 if (! gdbpy_is_string (value))
162 {
163 PyErr_SetString (PyExc_RuntimeError,
164 _("ENUM arguments must be a string."));
165 return -1;
166 }
167
168 str = python_string_to_host_string (value);
8dc78533
JK
169 if (str == NULL)
170 return -1;
d7b32ed3
PM
171 for (i = 0; self->enumeration[i]; ++i)
172 if (! strcmp (self->enumeration[i], str))
173 break;
174 xfree (str);
175 if (! self->enumeration[i])
176 {
177 PyErr_SetString (PyExc_RuntimeError,
178 _("The value must be member of an enumeration."));
179 return -1;
180 }
181 self->value.cstringval = self->enumeration[i];
182 break;
183 }
184
185 case var_boolean:
186 if (! PyBool_Check (value))
187 {
188 PyErr_SetString (PyExc_RuntimeError,
189 _("A boolean argument is required."));
190 return -1;
191 }
192 cmp = PyObject_IsTrue (value);
193 if (cmp < 0)
194 return -1;
195 self->value.intval = cmp;
196 break;
197
198 case var_auto_boolean:
199 if (! PyBool_Check (value) && value != Py_None)
200 {
201 PyErr_SetString (PyExc_RuntimeError,
202 _("A boolean or None is required"));
203 return -1;
204 }
205
206 if (value == Py_None)
207 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
208 else
209 {
210 cmp = PyObject_IsTrue (value);
211 if (cmp < 0 )
212 return -1;
213 if (cmp == 1)
214 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
215 else
216 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
217
218 break;
219 }
220
221 case var_integer:
222 case var_zinteger:
223 case var_uinteger:
224 {
225 long l;
226 int ok;
227
228 if (! PyInt_Check (value))
229 {
230 PyErr_SetString (PyExc_RuntimeError,
231 _("The value must be integer."));
232 return -1;
233 }
234
74aedc46
TT
235 if (! gdb_py_int_as_long (value, &l))
236 return -1;
237
d7b32ed3
PM
238 if (self->type == var_uinteger)
239 {
240 ok = (l >= 0 && l <= UINT_MAX);
241 if (l == 0)
242 l = UINT_MAX;
243 }
244 else if (self->type == var_integer)
245 {
246 ok = (l >= INT_MIN && l <= INT_MAX);
247 if (l == 0)
248 l = INT_MAX;
249 }
250 else
251 ok = (l >= INT_MIN && l <= INT_MAX);
252
253 if (! ok)
254 {
255 PyErr_SetString (PyExc_RuntimeError,
256 _("Range exceeded."));
257 return -1;
258 }
259
260 self->value.intval = (int) l;
261 break;
262 }
263
264 default:
265 PyErr_SetString (PyExc_RuntimeError,
266 _("Unhandled type in parameter value."));
267 return -1;
268 }
269
270 return 0;
271}
272
8dc78533 273/* Set an attribute. Returns -1 on error, with a python exception set. */
d7b32ed3
PM
274static int
275set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
276{
277 if (PyString_Check (attr_name)
278 && ! strcmp (PyString_AsString (attr_name), "value"))
279 {
280 if (!val)
281 {
282 PyErr_SetString (PyExc_RuntimeError,
283 _("Cannot delete a parameter's value."));
284 return -1;
285 }
286 return set_parameter_value ((parmpy_object *) obj, val);
287 }
288
289 return PyObject_GenericSetAttr (obj, attr_name, val);
290}
291
292\f
293
294/* A helper function that dispatches to the appropriate add_setshow
295 function. */
296static void
297add_setshow_generic (int parmclass, enum command_class cmdclass,
298 char *cmd_name, parmpy_object *self,
299 char *set_doc, char *show_doc, char *help_doc,
300 struct cmd_list_element **set_list,
301 struct cmd_list_element **show_list)
302{
303 switch (parmclass)
304 {
305 case var_boolean:
306 add_setshow_boolean_cmd (cmd_name, cmdclass, &self->value.intval,
307 set_doc, show_doc, help_doc,
308 NULL, NULL, set_list, show_list);
309 break;
310
311 case var_auto_boolean:
312 add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
313 &self->value.autoboolval,
314 set_doc, show_doc, help_doc,
315 NULL, NULL, set_list, show_list);
316 break;
317
318 case var_uinteger:
319 add_setshow_uinteger_cmd (cmd_name, cmdclass, &self->value.uintval,
320 set_doc, show_doc, help_doc,
321 NULL, NULL, set_list, show_list);
322 break;
323
324 case var_integer:
325 add_setshow_integer_cmd (cmd_name, cmdclass, &self->value.intval,
326 set_doc, show_doc, help_doc,
327 NULL, NULL, set_list, show_list);
328 break;
329
330 case var_string:
331 add_setshow_string_cmd (cmd_name, cmdclass, &self->value.stringval,
332 set_doc, show_doc, help_doc,
333 NULL, NULL, set_list, show_list);
334 break;
335
336 case var_string_noescape:
337 add_setshow_string_noescape_cmd (cmd_name, cmdclass,
338 &self->value.stringval,
339 set_doc, show_doc, help_doc,
340 NULL, NULL, set_list, show_list);
341 break;
342
343 case var_optional_filename:
344 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
345 &self->value.stringval,
346 set_doc, show_doc, help_doc,
347 NULL, NULL, set_list, show_list);
348 break;
349
350 case var_filename:
351 add_setshow_filename_cmd (cmd_name, cmdclass, &self->value.stringval,
352 set_doc, show_doc, help_doc,
353 NULL, NULL, set_list, show_list);
354 break;
355
356 case var_zinteger:
357 add_setshow_zinteger_cmd (cmd_name, cmdclass, &self->value.intval,
358 set_doc, show_doc, help_doc,
359 NULL, NULL, set_list, show_list);
360 break;
361
362 case var_enum:
363 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
364 &self->value.cstringval,
365 set_doc, show_doc, help_doc,
366 NULL, NULL, set_list, show_list);
367 /* Initialize the value, just in case. */
368 self->value.cstringval = self->enumeration[0];
369 break;
370 }
371}
372
8dc78533
JK
373/* A helper which computes enum values. Returns 1 on success. Returns 0 on
374 error, with a python exception set. */
d7b32ed3
PM
375static int
376compute_enum_values (parmpy_object *self, PyObject *enum_values)
377{
378 Py_ssize_t size, i;
8dc78533 379 struct cleanup *back_to;
d7b32ed3
PM
380
381 if (! enum_values)
382 {
383 PyErr_SetString (PyExc_RuntimeError,
384 _("An enumeration is required for PARAM_ENUM."));
385 return 0;
386 }
387
388 if (! PySequence_Check (enum_values))
389 {
390 PyErr_SetString (PyExc_RuntimeError,
391 _("The enumeration is not a sequence."));
392 return 0;
393 }
394
395 size = PySequence_Size (enum_values);
396 if (size < 0)
397 return 0;
398 if (size == 0)
399 {
400 PyErr_SetString (PyExc_RuntimeError,
401 _("The enumeration is empty."));
402 return 0;
403 }
404
405 self->enumeration = xmalloc ((size + 1) * sizeof (char *));
8dc78533 406 back_to = make_cleanup (free_current_contents, &self->enumeration);
d7b32ed3
PM
407 memset (self->enumeration, 0, (size + 1) * sizeof (char *));
408
409 for (i = 0; i < size; ++i)
410 {
411 PyObject *item = PySequence_GetItem (enum_values, i);
d59b6f6c 412
d7b32ed3 413 if (! item)
8dc78533
JK
414 {
415 do_cleanups (back_to);
416 return 0;
417 }
d7b32ed3
PM
418 if (! gdbpy_is_string (item))
419 {
8dc78533 420 do_cleanups (back_to);
d7b32ed3
PM
421 PyErr_SetString (PyExc_RuntimeError,
422 _("The enumeration item not a string."));
423 return 0;
424 }
425 self->enumeration[i] = python_string_to_host_string (item);
8dc78533
JK
426 if (self->enumeration[i] == NULL)
427 {
428 do_cleanups (back_to);
429 return 0;
430 }
431 make_cleanup (xfree, (char *) self->enumeration[i]);
d7b32ed3
PM
432 }
433
8dc78533 434 discard_cleanups (back_to);
d7b32ed3
PM
435 return 1;
436}
437
438/* A helper function which returns a documentation string for an
439 object. */
440static char *
441get_doc_string (PyObject *object, PyObject *attr)
442{
443 char *result = NULL;
d59b6f6c 444
d7b32ed3
PM
445 if (PyObject_HasAttr (object, attr))
446 {
447 PyObject *ds_obj = PyObject_GetAttr (object, attr);
d59b6f6c 448
d7b32ed3 449 if (ds_obj && gdbpy_is_string (ds_obj))
8dc78533
JK
450 {
451 result = python_string_to_host_string (ds_obj);
452 if (result == NULL)
453 gdbpy_print_stack ();
454 }
d7b32ed3
PM
455 }
456 if (! result)
457 result = xstrdup (_("This command is not documented."));
458 return result;
459}
460
461/* Object initializer; sets up gdb-side structures for command.
462
463 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
464
465 NAME is the name of the parameter. It may consist of multiple
466 words, in which case the final word is the name of the new command,
467 and earlier words must be prefix commands.
468
469 CMDCLASS is the kind of command. It should be one of the COMMAND_*
470 constants defined in the gdb module.
471
472 PARMCLASS is the type of the parameter. It should be one of the
473 PARAM_* constants defined in the gdb module.
474
475 If PARMCLASS is PARAM_ENUM, then the final argument should be a
476 collection of strings. These strings are the valid values for this
477 parameter.
478
479 The documentation for the parameter is taken from the doc string
480 for the python class.
8dc78533
JK
481
482 Returns -1 on error, with a python exception set. */
483
d7b32ed3
PM
484static int
485parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
486{
487 parmpy_object *obj = (parmpy_object *) self;
488 char *name;
489 char *set_doc, *show_doc, *doc;
490 char *cmd_name;
491 int parmclass, cmdtype;
492 PyObject *enum_values = NULL;
d7b32ed3
PM
493 struct cmd_list_element **set_list, **show_list;
494 volatile struct gdb_exception except;
495
496 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
497 &enum_values))
498 return -1;
499
500 if (cmdtype != no_class && cmdtype != class_run
501 && cmdtype != class_vars && cmdtype != class_stack
502 && cmdtype != class_files && cmdtype != class_support
503 && cmdtype != class_info && cmdtype != class_breakpoint
504 && cmdtype != class_trace && cmdtype != class_obscure
505 && cmdtype != class_maintenance)
506 {
507 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
508 return -1;
509 }
510
3c0ee1a4
PM
511 if (parmclass != var_boolean /* ARI: var_boolean */
512 && parmclass != var_auto_boolean
d7b32ed3
PM
513 && parmclass != var_uinteger && parmclass != var_integer
514 && parmclass != var_string && parmclass != var_string_noescape
515 && parmclass != var_optional_filename && parmclass != var_filename
516 && parmclass != var_zinteger && parmclass != var_enum)
517 {
9a2b4c1b
MS
518 PyErr_SetString (PyExc_RuntimeError,
519 _("Invalid parameter class argument."));
d7b32ed3
PM
520 return -1;
521 }
522
523 if (enum_values && parmclass != var_enum)
524 {
525 PyErr_SetString (PyExc_RuntimeError,
526 _("Only PARAM_ENUM accepts a fourth argument."));
527 return -1;
528 }
529 if (parmclass == var_enum)
530 {
531 if (! compute_enum_values (obj, enum_values))
532 return -1;
533 }
534 else
535 obj->enumeration = NULL;
536 obj->type = (enum var_types) parmclass;
537 memset (&obj->value, 0, sizeof (obj->value));
538
539 cmd_name = gdbpy_parse_command_name (name, &set_list,
540 &setlist);
541
542 if (! cmd_name)
543 return -1;
544 xfree (cmd_name);
545 cmd_name = gdbpy_parse_command_name (name, &show_list,
546 &showlist);
547 if (! cmd_name)
548 return -1;
549
550 set_doc = get_doc_string (self, set_doc_cst);
551 show_doc = get_doc_string (self, show_doc_cst);
552 doc = get_doc_string (self, gdbpy_doc_cst);
553
554 Py_INCREF (self);
555
556 TRY_CATCH (except, RETURN_MASK_ALL)
557 {
558 add_setshow_generic (parmclass, (enum command_class) cmdtype,
559 cmd_name, obj,
560 set_doc, show_doc,
561 doc, set_list, show_list);
562 }
563 if (except.reason < 0)
564 {
565 xfree (cmd_name);
566 xfree (set_doc);
567 xfree (show_doc);
568 xfree (doc);
569 Py_DECREF (self);
570 PyErr_Format (except.reason == RETURN_QUIT
571 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
572 "%s", except.message);
573 return -1;
574 }
575 return 0;
576}
577
578\f
579
580/* Initialize the 'parameters' module. */
581void
582gdbpy_initialize_parameters (void)
583{
584 int i;
585
586 if (PyType_Ready (&parmpy_object_type) < 0)
587 return;
588
589 set_doc_cst = PyString_FromString ("set_doc");
590 if (! set_doc_cst)
591 return;
592 show_doc_cst = PyString_FromString ("show_doc");
593 if (! show_doc_cst)
594 return;
595
596 for (i = 0; parm_constants[i].name; ++i)
597 {
598 if (PyModule_AddIntConstant (gdb_module,
599 parm_constants[i].name,
600 parm_constants[i].value) < 0)
601 return;
602 }
603
604 Py_INCREF (&parmpy_object_type);
605 PyModule_AddObject (gdb_module, "Parameter",
606 (PyObject *) &parmpy_object_type);
607}
608
609\f
610
611static PyTypeObject parmpy_object_type =
612{
613 PyObject_HEAD_INIT (NULL)
614 0, /*ob_size*/
615 "gdb.Parameter", /*tp_name*/
616 sizeof (parmpy_object), /*tp_basicsize*/
617 0, /*tp_itemsize*/
618 0, /*tp_dealloc*/
619 0, /*tp_print*/
620 0, /*tp_getattr*/
621 0, /*tp_setattr*/
622 0, /*tp_compare*/
623 0, /*tp_repr*/
624 0, /*tp_as_number*/
625 0, /*tp_as_sequence*/
626 0, /*tp_as_mapping*/
627 0, /*tp_hash */
628 0, /*tp_call*/
629 0, /*tp_str*/
630 get_attr, /*tp_getattro*/
631 set_attr, /*tp_setattro*/
632 0, /*tp_as_buffer*/
633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
634 "GDB parameter object", /* tp_doc */
635 0, /* tp_traverse */
636 0, /* tp_clear */
637 0, /* tp_richcompare */
638 0, /* tp_weaklistoffset */
639 0, /* tp_iter */
640 0, /* tp_iternext */
641 0, /* tp_methods */
642 0, /* tp_members */
643 0, /* tp_getset */
644 0, /* tp_base */
645 0, /* tp_dict */
646 0, /* tp_descr_get */
647 0, /* tp_descr_set */
648 0, /* tp_dictoffset */
649 parmpy_init, /* tp_init */
650 0, /* tp_alloc */
651 PyType_GenericNew /* tp_new */
652};
This page took 0.125855 seconds and 4 git commands to generate.