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