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