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