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