Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
618f726f | 3 | Copyright (C) 2008-2016 Free Software Foundation, Inc. |
a6bac58e TT |
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 | #include "defs.h" | |
a6bac58e TT |
21 | #include "objfiles.h" |
22 | #include "symtab.h" | |
23 | #include "language.h" | |
24 | #include "valprint.h" | |
6dddc817 | 25 | #include "extension-priv.h" |
a6bac58e | 26 | #include "python.h" |
6bf0ce2b PP |
27 | #include "python-internal.h" |
28 | ||
621c8364 TT |
29 | /* Return type of print_string_repr. */ |
30 | ||
31 | enum string_repr_result | |
32 | { | |
33 | /* The string method returned None. */ | |
34 | string_repr_none, | |
35 | /* The string method had an error. */ | |
36 | string_repr_error, | |
37 | /* Everything ok. */ | |
38 | string_repr_ok | |
39 | }; | |
40 | ||
a6bac58e TT |
41 | /* Helper function for find_pretty_printer which iterates over a list, |
42 | calls each function and inspects output. This will return a | |
43 | printer object if one recognizes VALUE. If no printer is found, it | |
44 | will return None. On error, it will set the Python error and | |
45 | return NULL. */ | |
fa33c3cd | 46 | |
a6bac58e TT |
47 | static PyObject * |
48 | search_pp_list (PyObject *list, PyObject *value) | |
49 | { | |
50 | Py_ssize_t pp_list_size, list_index; | |
51 | PyObject *function, *printer = NULL; | |
52 | ||
53 | pp_list_size = PyList_Size (list); | |
54 | for (list_index = 0; list_index < pp_list_size; list_index++) | |
55 | { | |
56 | function = PyList_GetItem (list, list_index); | |
57 | if (! function) | |
58 | return NULL; | |
59 | ||
967cf477 | 60 | /* Skip if disabled. */ |
1bdb0c54 TT |
61 | if (PyObject_HasAttr (function, gdbpy_enabled_cst)) |
62 | { | |
63 | PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst); | |
64 | int cmp; | |
65 | ||
66 | if (!attr) | |
67 | return NULL; | |
68 | cmp = PyObject_IsTrue (attr); | |
7d38e38f | 69 | Py_DECREF (attr); |
1bdb0c54 TT |
70 | if (cmp == -1) |
71 | return NULL; | |
72 | ||
73 | if (!cmp) | |
74 | continue; | |
75 | } | |
967cf477 | 76 | |
a6bac58e TT |
77 | printer = PyObject_CallFunctionObjArgs (function, value, NULL); |
78 | if (! printer) | |
79 | return NULL; | |
80 | else if (printer != Py_None) | |
81 | return printer; | |
82 | ||
83 | Py_DECREF (printer); | |
84 | } | |
85 | ||
86 | Py_RETURN_NONE; | |
87 | } | |
88 | ||
fa33c3cd DE |
89 | /* Subroutine of find_pretty_printer to simplify it. |
90 | Look for a pretty-printer to print VALUE in all objfiles. | |
91 | The result is NULL if there's an error and the search should be terminated. | |
92 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
93 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
94 | ||
a6bac58e | 95 | static PyObject * |
fa33c3cd | 96 | find_pretty_printer_from_objfiles (PyObject *value) |
a6bac58e | 97 | { |
fa33c3cd DE |
98 | PyObject *pp_list; |
99 | PyObject *function; | |
a6bac58e | 100 | struct objfile *obj; |
a6bac58e | 101 | |
a6bac58e TT |
102 | ALL_OBJFILES (obj) |
103 | { | |
104 | PyObject *objf = objfile_to_objfile_object (obj); | |
105 | if (!objf) | |
106 | { | |
107 | /* Ignore the error and continue. */ | |
108 | PyErr_Clear (); | |
109 | continue; | |
110 | } | |
111 | ||
112 | pp_list = objfpy_get_printers (objf, NULL); | |
113 | function = search_pp_list (pp_list, value); | |
fa33c3cd | 114 | Py_XDECREF (pp_list); |
a6bac58e | 115 | |
fa33c3cd | 116 | /* If there is an error in any objfile list, abort the search and exit. */ |
a6bac58e | 117 | if (! function) |
fa33c3cd | 118 | return NULL; |
a6bac58e TT |
119 | |
120 | if (function != Py_None) | |
fa33c3cd | 121 | return function; |
256458bc | 122 | |
a6bac58e | 123 | Py_DECREF (function); |
a6bac58e TT |
124 | } |
125 | ||
fa33c3cd DE |
126 | Py_RETURN_NONE; |
127 | } | |
128 | ||
129 | /* Subroutine of find_pretty_printer to simplify it. | |
130 | Look for a pretty-printer to print VALUE in the current program space. | |
131 | The result is NULL if there's an error and the search should be terminated. | |
132 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
133 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
134 | ||
135 | static PyObject * | |
136 | find_pretty_printer_from_progspace (PyObject *value) | |
137 | { | |
138 | PyObject *pp_list; | |
139 | PyObject *function; | |
140 | PyObject *obj = pspace_to_pspace_object (current_program_space); | |
141 | ||
142 | if (!obj) | |
143 | return NULL; | |
144 | pp_list = pspy_get_printers (obj, NULL); | |
145 | function = search_pp_list (pp_list, value); | |
146 | Py_XDECREF (pp_list); | |
147 | return function; | |
148 | } | |
149 | ||
150 | /* Subroutine of find_pretty_printer to simplify it. | |
151 | Look for a pretty-printer to print VALUE in the gdb module. | |
152 | The result is NULL if there's an error and the search should be terminated. | |
153 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
154 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
155 | ||
156 | static PyObject * | |
157 | find_pretty_printer_from_gdb (PyObject *value) | |
158 | { | |
159 | PyObject *pp_list; | |
160 | PyObject *function; | |
161 | ||
23fa7f66 | 162 | /* Fetch the global pretty printer list. */ |
b9516fa1 YPK |
163 | if (gdb_python_module == NULL |
164 | || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers")) | |
fa33c3cd | 165 | Py_RETURN_NONE; |
b9516fa1 | 166 | pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers"); |
fa33c3cd | 167 | if (pp_list == NULL || ! PyList_Check (pp_list)) |
a6bac58e | 168 | { |
fa33c3cd DE |
169 | Py_XDECREF (pp_list); |
170 | Py_RETURN_NONE; | |
a6bac58e | 171 | } |
a6bac58e TT |
172 | |
173 | function = search_pp_list (pp_list, value); | |
a6bac58e | 174 | Py_XDECREF (pp_list); |
a6bac58e TT |
175 | return function; |
176 | } | |
be759fcf | 177 | |
fa33c3cd DE |
178 | /* Find the pretty-printing constructor function for VALUE. If no |
179 | pretty-printer exists, return None. If one exists, return a new | |
180 | reference. On error, set the Python error and return NULL. */ | |
181 | ||
182 | static PyObject * | |
183 | find_pretty_printer (PyObject *value) | |
184 | { | |
185 | PyObject *function; | |
186 | ||
23fa7f66 | 187 | /* Look at the pretty-printer list for each objfile |
fa33c3cd DE |
188 | in the current program-space. */ |
189 | function = find_pretty_printer_from_objfiles (value); | |
190 | if (function == NULL || function != Py_None) | |
191 | return function; | |
192 | Py_DECREF (function); | |
193 | ||
23fa7f66 | 194 | /* Look at the pretty-printer list for the current program-space. */ |
fa33c3cd DE |
195 | function = find_pretty_printer_from_progspace (value); |
196 | if (function == NULL || function != Py_None) | |
197 | return function; | |
198 | Py_DECREF (function); | |
199 | ||
23fa7f66 | 200 | /* Look at the pretty-printer list in the gdb module. */ |
fa33c3cd DE |
201 | function = find_pretty_printer_from_gdb (value); |
202 | return function; | |
203 | } | |
be759fcf | 204 | |
fbb8f299 PM |
205 | /* Pretty-print a single value, via the printer object PRINTER. |
206 | If the function returns a string, a PyObject containing the string | |
79f283fe PM |
207 | is returned. If the function returns Py_NONE that means the pretty |
208 | printer returned the Python None as a value. Otherwise, if the | |
209 | function returns a value, *OUT_VALUE is set to the value, and NULL | |
8dc78533 JK |
210 | is returned. On error, *OUT_VALUE is set to NULL, NULL is |
211 | returned, with a python exception set. */ | |
79f283fe | 212 | |
fbb8f299 | 213 | static PyObject * |
a6bac58e TT |
214 | pretty_print_one_value (PyObject *printer, struct value **out_value) |
215 | { | |
fbb8f299 | 216 | PyObject *result = NULL; |
a6bac58e | 217 | |
fbb8f299 | 218 | *out_value = NULL; |
492d29ea | 219 | TRY |
a6bac58e | 220 | { |
a6bac58e TT |
221 | result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL); |
222 | if (result) | |
223 | { | |
256458bc | 224 | if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result) |
79f283fe | 225 | && result != Py_None) |
fbb8f299 PM |
226 | { |
227 | *out_value = convert_value_from_python (result); | |
228 | if (PyErr_Occurred ()) | |
229 | *out_value = NULL; | |
230 | Py_DECREF (result); | |
231 | result = NULL; | |
232 | } | |
a6bac58e TT |
233 | } |
234 | } | |
492d29ea PA |
235 | CATCH (except, RETURN_MASK_ALL) |
236 | { | |
237 | } | |
238 | END_CATCH | |
a6bac58e | 239 | |
fbb8f299 | 240 | return result; |
a6bac58e TT |
241 | } |
242 | ||
243 | /* Return the display hint for the object printer, PRINTER. Return | |
244 | NULL if there is no display_hint method, or if the method did not | |
245 | return a string. On error, print stack trace and return NULL. On | |
246 | success, return an xmalloc()d string. */ | |
9b972014 | 247 | gdb::unique_xmalloc_ptr<char> |
a6bac58e TT |
248 | gdbpy_get_display_hint (PyObject *printer) |
249 | { | |
250 | PyObject *hint; | |
9b972014 | 251 | gdb::unique_xmalloc_ptr<char> result; |
a6bac58e TT |
252 | |
253 | if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst)) | |
254 | return NULL; | |
255 | ||
256 | hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL); | |
a6bac58e | 257 | if (hint) |
f04e4012 TT |
258 | { |
259 | if (gdbpy_is_string (hint)) | |
8dc78533 JK |
260 | { |
261 | result = python_string_to_host_string (hint); | |
262 | if (result == NULL) | |
263 | gdbpy_print_stack (); | |
264 | } | |
f04e4012 TT |
265 | Py_DECREF (hint); |
266 | } | |
a6bac58e TT |
267 | else |
268 | gdbpy_print_stack (); | |
269 | ||
270 | return result; | |
271 | } | |
272 | ||
621c8364 TT |
273 | /* A wrapper for gdbpy_print_stack that ignores MemoryError. */ |
274 | ||
275 | static void | |
276 | print_stack_unless_memory_error (struct ui_file *stream) | |
277 | { | |
278 | if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) | |
279 | { | |
280 | struct cleanup *cleanup; | |
281 | PyObject *type, *value, *trace; | |
621c8364 TT |
282 | |
283 | PyErr_Fetch (&type, &value, &trace); | |
284 | cleanup = make_cleanup_py_decref (type); | |
285 | make_cleanup_py_decref (value); | |
286 | make_cleanup_py_decref (trace); | |
287 | ||
9b972014 TT |
288 | gdb::unique_xmalloc_ptr<char> |
289 | msg (gdbpy_exception_to_string (type, value)); | |
621c8364 TT |
290 | |
291 | if (msg == NULL || *msg == '\0') | |
1dae3efc | 292 | fprintf_filtered (stream, _("<error reading variable>")); |
621c8364 | 293 | else |
9b972014 TT |
294 | fprintf_filtered (stream, _("<error reading variable: %s>"), |
295 | msg.get ()); | |
621c8364 TT |
296 | |
297 | do_cleanups (cleanup); | |
298 | } | |
299 | else | |
300 | gdbpy_print_stack (); | |
301 | } | |
302 | ||
6dddc817 | 303 | /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and |
621c8364 TT |
304 | formats the result. */ |
305 | ||
306 | static enum string_repr_result | |
a6bac58e TT |
307 | print_string_repr (PyObject *printer, const char *hint, |
308 | struct ui_file *stream, int recurse, | |
309 | const struct value_print_options *options, | |
50810684 UW |
310 | const struct language_defn *language, |
311 | struct gdbarch *gdbarch) | |
a6bac58e | 312 | { |
a6bac58e | 313 | struct value *replacement = NULL; |
fbb8f299 | 314 | PyObject *py_str = NULL; |
621c8364 | 315 | enum string_repr_result result = string_repr_ok; |
a6bac58e | 316 | |
fbb8f299 PM |
317 | py_str = pretty_print_one_value (printer, &replacement); |
318 | if (py_str) | |
a6bac58e | 319 | { |
09ca9e2e TT |
320 | struct cleanup *cleanup = make_cleanup_py_decref (py_str); |
321 | ||
79f283fe | 322 | if (py_str == Py_None) |
621c8364 | 323 | result = string_repr_none; |
09ca9e2e | 324 | else if (gdbpy_is_lazy_string (py_str)) |
fbb8f299 | 325 | { |
09ca9e2e | 326 | CORE_ADDR addr; |
79f283fe PM |
327 | long length; |
328 | struct type *type; | |
329 | char *encoding = NULL; | |
a81766d8 | 330 | struct value_print_options local_opts = *options; |
79f283fe | 331 | |
09ca9e2e TT |
332 | make_cleanup (free_current_contents, &encoding); |
333 | gdbpy_extract_lazy_string (py_str, &addr, &type, | |
334 | &length, &encoding); | |
335 | ||
a81766d8 | 336 | local_opts.addressprint = 0; |
09ca9e2e | 337 | val_print_string (type, encoding, addr, (int) length, |
a81766d8 | 338 | stream, &local_opts); |
09ca9e2e TT |
339 | } |
340 | else | |
341 | { | |
342 | PyObject *string; | |
343 | ||
344 | string = python_string_to_target_python_string (py_str); | |
345 | if (string) | |
79f283fe | 346 | { |
89f6d837 | 347 | char *output; |
09ca9e2e TT |
348 | long length; |
349 | struct type *type; | |
350 | ||
351 | make_cleanup_py_decref (string); | |
9a27f2c6 | 352 | #ifdef IS_PY3K |
89f6d837 | 353 | output = PyBytes_AS_STRING (string); |
9a27f2c6 PK |
354 | length = PyBytes_GET_SIZE (string); |
355 | #else | |
09ca9e2e TT |
356 | output = PyString_AsString (string); |
357 | length = PyString_Size (string); | |
9a27f2c6 | 358 | #endif |
09ca9e2e TT |
359 | type = builtin_type (gdbarch)->builtin_char; |
360 | ||
361 | if (hint && !strcmp (hint, "string")) | |
89f6d837 PA |
362 | LA_PRINT_STRING (stream, type, (gdb_byte *) output, |
363 | length, NULL, 0, options); | |
79f283fe PM |
364 | else |
365 | fputs_filtered (output, stream); | |
be759fcf PM |
366 | } |
367 | else | |
621c8364 TT |
368 | { |
369 | result = string_repr_error; | |
370 | print_stack_unless_memory_error (stream); | |
371 | } | |
79f283fe | 372 | } |
09ca9e2e TT |
373 | |
374 | do_cleanups (cleanup); | |
a6bac58e TT |
375 | } |
376 | else if (replacement) | |
269f82e5 PP |
377 | { |
378 | struct value_print_options opts = *options; | |
379 | ||
380 | opts.addressprint = 0; | |
381 | common_val_print (replacement, stream, recurse, &opts, language); | |
382 | } | |
a6bac58e | 383 | else |
621c8364 TT |
384 | { |
385 | result = string_repr_error; | |
386 | print_stack_unless_memory_error (stream); | |
387 | } | |
79f283fe | 388 | |
621c8364 | 389 | return result; |
a6bac58e TT |
390 | } |
391 | ||
9a27f2c6 | 392 | #ifndef IS_PY3K |
a6bac58e TT |
393 | static void |
394 | py_restore_tstate (void *p) | |
395 | { | |
19ba03f4 | 396 | PyFrameObject *frame = (PyFrameObject *) p; |
a6bac58e | 397 | PyThreadState *tstate = PyThreadState_GET (); |
d59b6f6c | 398 | |
a6bac58e TT |
399 | tstate->frame = frame; |
400 | } | |
401 | ||
402 | /* Create a dummy PyFrameObject, needed to work around | |
403 | a Python-2.4 bug with generators. */ | |
404 | static PyObject * | |
eeae04df | 405 | push_dummy_python_frame (void) |
a6bac58e TT |
406 | { |
407 | PyObject *empty_string, *null_tuple, *globals; | |
408 | PyCodeObject *code; | |
409 | PyFrameObject *frame; | |
410 | PyThreadState *tstate; | |
411 | ||
412 | empty_string = PyString_FromString (""); | |
413 | if (!empty_string) | |
414 | return NULL; | |
415 | ||
416 | null_tuple = PyTuple_New (0); | |
417 | if (!null_tuple) | |
418 | { | |
419 | Py_DECREF (empty_string); | |
420 | return NULL; | |
421 | } | |
422 | ||
423 | code = PyCode_New (0, /* argcount */ | |
424 | 0, /* nlocals */ | |
425 | 0, /* stacksize */ | |
426 | 0, /* flags */ | |
427 | empty_string, /* code */ | |
428 | null_tuple, /* consts */ | |
429 | null_tuple, /* names */ | |
430 | null_tuple, /* varnames */ | |
431 | #if PYTHON_API_VERSION >= 1010 | |
432 | null_tuple, /* freevars */ | |
433 | null_tuple, /* cellvars */ | |
434 | #endif | |
435 | empty_string, /* filename */ | |
436 | empty_string, /* name */ | |
437 | 1, /* firstlineno */ | |
438 | empty_string /* lnotab */ | |
439 | ); | |
440 | ||
441 | Py_DECREF (empty_string); | |
442 | Py_DECREF (null_tuple); | |
443 | ||
444 | if (!code) | |
445 | return NULL; | |
446 | ||
447 | globals = PyDict_New (); | |
448 | if (!globals) | |
449 | { | |
450 | Py_DECREF (code); | |
451 | return NULL; | |
452 | } | |
453 | ||
454 | tstate = PyThreadState_GET (); | |
455 | ||
456 | frame = PyFrame_New (tstate, code, globals, NULL); | |
457 | ||
458 | Py_DECREF (globals); | |
459 | Py_DECREF (code); | |
460 | ||
461 | if (!frame) | |
462 | return NULL; | |
463 | ||
464 | tstate->frame = frame; | |
465 | make_cleanup (py_restore_tstate, frame->f_back); | |
466 | return (PyObject *) frame; | |
467 | } | |
9a27f2c6 | 468 | #endif |
a6bac58e | 469 | |
6dddc817 | 470 | /* Helper for gdbpy_apply_val_pretty_printer that formats children of the |
79f283fe PM |
471 | printer, if any exist. If is_py_none is true, then nothing has |
472 | been printed by to_string, and format output accordingly. */ | |
a6bac58e TT |
473 | static void |
474 | print_children (PyObject *printer, const char *hint, | |
475 | struct ui_file *stream, int recurse, | |
476 | const struct value_print_options *options, | |
79f283fe PM |
477 | const struct language_defn *language, |
478 | int is_py_none) | |
a6bac58e TT |
479 | { |
480 | int is_map, is_array, done_flag, pretty; | |
481 | unsigned int i; | |
9a27f2c6 PK |
482 | PyObject *children, *iter; |
483 | #ifndef IS_PY3K | |
484 | PyObject *frame; | |
485 | #endif | |
a6bac58e TT |
486 | struct cleanup *cleanups; |
487 | ||
488 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
489 | return; | |
490 | ||
491 | /* If we are printing a map or an array, we want some special | |
492 | formatting. */ | |
493 | is_map = hint && ! strcmp (hint, "map"); | |
494 | is_array = hint && ! strcmp (hint, "array"); | |
495 | ||
496 | children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, | |
497 | NULL); | |
498 | if (! children) | |
499 | { | |
621c8364 | 500 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
501 | return; |
502 | } | |
503 | ||
504 | cleanups = make_cleanup_py_decref (children); | |
505 | ||
506 | iter = PyObject_GetIter (children); | |
507 | if (!iter) | |
508 | { | |
621c8364 | 509 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
510 | goto done; |
511 | } | |
512 | make_cleanup_py_decref (iter); | |
513 | ||
2a998fc0 | 514 | /* Use the prettyformat_arrays option if we are printing an array, |
a6bac58e | 515 | and the pretty option otherwise. */ |
a81766d8 | 516 | if (is_array) |
2a998fc0 | 517 | pretty = options->prettyformat_arrays; |
a81766d8 TT |
518 | else |
519 | { | |
2a998fc0 | 520 | if (options->prettyformat == Val_prettyformat) |
a81766d8 TT |
521 | pretty = 1; |
522 | else | |
2a998fc0 | 523 | pretty = options->prettyformat_structs; |
a81766d8 | 524 | } |
a6bac58e TT |
525 | |
526 | /* Manufacture a dummy Python frame to work around Python 2.4 bug, | |
527 | where it insists on having a non-NULL tstate->frame when | |
528 | a generator is called. */ | |
9a27f2c6 | 529 | #ifndef IS_PY3K |
a6bac58e TT |
530 | frame = push_dummy_python_frame (); |
531 | if (!frame) | |
532 | { | |
533 | gdbpy_print_stack (); | |
534 | goto done; | |
535 | } | |
536 | make_cleanup_py_decref (frame); | |
9a27f2c6 | 537 | #endif |
a6bac58e TT |
538 | |
539 | done_flag = 0; | |
540 | for (i = 0; i < options->print_max; ++i) | |
541 | { | |
542 | PyObject *py_v, *item = PyIter_Next (iter); | |
ddd49eee | 543 | const char *name; |
a6bac58e TT |
544 | struct cleanup *inner_cleanup; |
545 | ||
546 | if (! item) | |
547 | { | |
548 | if (PyErr_Occurred ()) | |
621c8364 | 549 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
550 | /* Set a flag so we can know whether we printed all the |
551 | available elements. */ | |
256458bc | 552 | else |
a6bac58e TT |
553 | done_flag = 1; |
554 | break; | |
555 | } | |
556 | ||
69b4374a DE |
557 | if (! PyTuple_Check (item) || PyTuple_Size (item) != 2) |
558 | { | |
559 | PyErr_SetString (PyExc_TypeError, | |
560 | _("Result of children iterator not a tuple" | |
561 | " of two elements.")); | |
562 | gdbpy_print_stack (); | |
563 | Py_DECREF (item); | |
564 | continue; | |
565 | } | |
a6bac58e TT |
566 | if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) |
567 | { | |
69b4374a DE |
568 | /* The user won't necessarily get a stack trace here, so provide |
569 | more context. */ | |
570 | if (gdbpy_print_python_errors_p ()) | |
571 | fprintf_unfiltered (gdb_stderr, | |
572 | _("Bad result from children iterator.\n")); | |
a6bac58e TT |
573 | gdbpy_print_stack (); |
574 | Py_DECREF (item); | |
575 | continue; | |
576 | } | |
577 | inner_cleanup = make_cleanup_py_decref (item); | |
578 | ||
579 | /* Print initial "{". For other elements, there are three | |
580 | cases: | |
581 | 1. Maps. Print a "," after each value element. | |
582 | 2. Arrays. Always print a ",". | |
583 | 3. Other. Always print a ",". */ | |
584 | if (i == 0) | |
79f283fe PM |
585 | { |
586 | if (is_py_none) | |
587 | fputs_filtered ("{", stream); | |
588 | else | |
589 | fputs_filtered (" = {", stream); | |
590 | } | |
591 | ||
a6bac58e TT |
592 | else if (! is_map || i % 2 == 0) |
593 | fputs_filtered (pretty ? "," : ", ", stream); | |
594 | ||
595 | /* In summary mode, we just want to print "= {...}" if there is | |
596 | a value. */ | |
597 | if (options->summary) | |
598 | { | |
599 | /* This increment tricks the post-loop logic to print what | |
600 | we want. */ | |
601 | ++i; | |
602 | /* Likewise. */ | |
603 | pretty = 0; | |
604 | break; | |
605 | } | |
606 | ||
607 | if (! is_map || i % 2 == 0) | |
608 | { | |
609 | if (pretty) | |
610 | { | |
611 | fputs_filtered ("\n", stream); | |
612 | print_spaces_filtered (2 + 2 * recurse, stream); | |
613 | } | |
614 | else | |
615 | wrap_here (n_spaces (2 + 2 *recurse)); | |
616 | } | |
617 | ||
618 | if (is_map && i % 2 == 0) | |
619 | fputs_filtered ("[", stream); | |
620 | else if (is_array) | |
621 | { | |
622 | /* We print the index, not whatever the child method | |
623 | returned as the name. */ | |
624 | if (options->print_array_indexes) | |
625 | fprintf_filtered (stream, "[%d] = ", i); | |
626 | } | |
627 | else if (! is_map) | |
628 | { | |
629 | fputs_filtered (name, stream); | |
630 | fputs_filtered (" = ", stream); | |
631 | } | |
632 | ||
09ca9e2e | 633 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 634 | { |
09ca9e2e TT |
635 | CORE_ADDR addr; |
636 | struct type *type; | |
637 | long length; | |
638 | char *encoding = NULL; | |
a81766d8 | 639 | struct value_print_options local_opts = *options; |
be759fcf | 640 | |
09ca9e2e TT |
641 | make_cleanup (free_current_contents, &encoding); |
642 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); | |
643 | ||
a81766d8 | 644 | local_opts.addressprint = 0; |
09ca9e2e | 645 | val_print_string (type, encoding, addr, (int) length, stream, |
a81766d8 | 646 | &local_opts); |
09ca9e2e TT |
647 | } |
648 | else if (gdbpy_is_string (py_v)) | |
649 | { | |
9b972014 | 650 | gdb::unique_xmalloc_ptr<char> output; |
09ca9e2e TT |
651 | |
652 | output = python_string_to_host_string (py_v); | |
653 | if (!output) | |
654 | gdbpy_print_stack (); | |
a6bac58e | 655 | else |
9b972014 | 656 | fputs_filtered (output.get (), stream); |
a6bac58e TT |
657 | } |
658 | else | |
659 | { | |
660 | struct value *value = convert_value_from_python (py_v); | |
661 | ||
662 | if (value == NULL) | |
663 | { | |
664 | gdbpy_print_stack (); | |
665 | error (_("Error while executing Python code.")); | |
666 | } | |
667 | else | |
668 | common_val_print (value, stream, recurse + 1, options, language); | |
669 | } | |
670 | ||
671 | if (is_map && i % 2 == 0) | |
672 | fputs_filtered ("] = ", stream); | |
673 | ||
674 | do_cleanups (inner_cleanup); | |
675 | } | |
676 | ||
677 | if (i) | |
678 | { | |
679 | if (!done_flag) | |
680 | { | |
681 | if (pretty) | |
682 | { | |
683 | fputs_filtered ("\n", stream); | |
684 | print_spaces_filtered (2 + 2 * recurse, stream); | |
685 | } | |
686 | fputs_filtered ("...", stream); | |
687 | } | |
688 | if (pretty) | |
689 | { | |
690 | fputs_filtered ("\n", stream); | |
691 | print_spaces_filtered (2 * recurse, stream); | |
692 | } | |
693 | fputs_filtered ("}", stream); | |
694 | } | |
695 | ||
696 | done: | |
697 | do_cleanups (cleanups); | |
698 | } | |
699 | ||
6dddc817 DE |
700 | enum ext_lang_rc |
701 | gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, | |
668e1674 | 702 | struct type *type, |
6b850546 | 703 | LONGEST embedded_offset, CORE_ADDR address, |
6dddc817 | 704 | struct ui_file *stream, int recurse, |
668e1674 | 705 | struct value *val, |
6dddc817 DE |
706 | const struct value_print_options *options, |
707 | const struct language_defn *language) | |
a6bac58e | 708 | { |
50810684 | 709 | struct gdbarch *gdbarch = get_type_arch (type); |
a6bac58e TT |
710 | PyObject *printer = NULL; |
711 | PyObject *val_obj = NULL; | |
712 | struct value *value; | |
9b972014 | 713 | gdb::unique_xmalloc_ptr<char> hint; |
a6bac58e | 714 | struct cleanup *cleanups; |
6dddc817 | 715 | enum ext_lang_rc result = EXT_LANG_RC_NOP; |
621c8364 | 716 | enum string_repr_result print_result; |
668e1674 | 717 | const gdb_byte *valaddr = value_contents_for_printing (val); |
4e07d55f PA |
718 | |
719 | /* No pretty-printer support for unavailable values. */ | |
63360adc | 720 | if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
6dddc817 | 721 | return EXT_LANG_RC_NOP; |
0646da15 TT |
722 | |
723 | if (!gdb_python_initialized) | |
6dddc817 | 724 | return EXT_LANG_RC_NOP; |
4e07d55f | 725 | |
d452c4bc | 726 | cleanups = ensure_python_env (gdbarch, language); |
a6bac58e TT |
727 | |
728 | /* Instantiate the printer. */ | |
3fff9862 | 729 | value = value_from_component (val, type, embedded_offset); |
a6bac58e TT |
730 | |
731 | val_obj = value_to_value_object (value); | |
732 | if (! val_obj) | |
6dddc817 DE |
733 | { |
734 | result = EXT_LANG_RC_ERROR; | |
735 | goto done; | |
736 | } | |
256458bc | 737 | |
a6bac58e TT |
738 | /* Find the constructor. */ |
739 | printer = find_pretty_printer (val_obj); | |
740 | Py_DECREF (val_obj); | |
c8c735b9 PA |
741 | |
742 | if (printer == NULL) | |
6dddc817 DE |
743 | { |
744 | result = EXT_LANG_RC_ERROR; | |
745 | goto done; | |
746 | } | |
c8c735b9 | 747 | |
a6bac58e | 748 | make_cleanup_py_decref (printer); |
c8c735b9 | 749 | if (printer == Py_None) |
6dddc817 DE |
750 | { |
751 | result = EXT_LANG_RC_NOP; | |
752 | goto done; | |
753 | } | |
a6bac58e TT |
754 | |
755 | /* If we are printing a map, we want some special formatting. */ | |
756 | hint = gdbpy_get_display_hint (printer); | |
a6bac58e TT |
757 | |
758 | /* Print the section */ | |
9b972014 | 759 | print_result = print_string_repr (printer, hint.get (), stream, recurse, |
621c8364 TT |
760 | options, language, gdbarch); |
761 | if (print_result != string_repr_error) | |
9b972014 | 762 | print_children (printer, hint.get (), stream, recurse, options, language, |
621c8364 | 763 | print_result == string_repr_none); |
79f283fe | 764 | |
6dddc817 | 765 | result = EXT_LANG_RC_OK; |
a6bac58e TT |
766 | |
767 | done: | |
768 | if (PyErr_Occurred ()) | |
621c8364 | 769 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
770 | do_cleanups (cleanups); |
771 | return result; | |
772 | } | |
773 | ||
fbb8f299 | 774 | |
b6313243 TT |
775 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
776 | print object. It must have a 'to_string' method (but this is | |
777 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
778 | returns a string. The printer will return a value and in the case |
779 | of a Python string being returned, this function will return a | |
780 | PyObject containing the string. For any other type, *REPLACEMENT is | |
781 | set to the replacement value and this function returns NULL. On | |
782 | error, *REPLACEMENT is set to NULL and this function also returns | |
783 | NULL. */ | |
784 | PyObject * | |
b6313243 | 785 | apply_varobj_pretty_printer (PyObject *printer_obj, |
621c8364 TT |
786 | struct value **replacement, |
787 | struct ui_file *stream) | |
b6313243 | 788 | { |
fbb8f299 | 789 | PyObject *py_str = NULL; |
b6313243 TT |
790 | |
791 | *replacement = NULL; | |
fbb8f299 PM |
792 | py_str = pretty_print_one_value (printer_obj, replacement); |
793 | ||
794 | if (*replacement == NULL && py_str == NULL) | |
621c8364 | 795 | print_stack_unless_memory_error (stream); |
b6313243 | 796 | |
fbb8f299 | 797 | return py_str; |
b6313243 TT |
798 | } |
799 | ||
800 | /* Find a pretty-printer object for the varobj module. Returns a new | |
801 | reference to the object if successful; returns NULL if not. VALUE | |
256458bc | 802 | is the value for which a printer tests to determine if it |
b6313243 TT |
803 | can pretty-print the value. */ |
804 | PyObject * | |
805 | gdbpy_get_varobj_pretty_printer (struct value *value) | |
806 | { | |
807 | PyObject *val_obj; | |
808 | PyObject *pretty_printer = NULL; | |
b6313243 | 809 | |
492d29ea | 810 | TRY |
b6313243 TT |
811 | { |
812 | value = value_copy (value); | |
813 | } | |
492d29ea PA |
814 | CATCH (except, RETURN_MASK_ALL) |
815 | { | |
816 | GDB_PY_HANDLE_EXCEPTION (except); | |
817 | } | |
818 | END_CATCH | |
256458bc | 819 | |
b6313243 TT |
820 | val_obj = value_to_value_object (value); |
821 | if (! val_obj) | |
822 | return NULL; | |
823 | ||
824 | pretty_printer = find_pretty_printer (val_obj); | |
825 | Py_DECREF (val_obj); | |
826 | return pretty_printer; | |
827 | } | |
828 | ||
829 | /* A Python function which wraps find_pretty_printer and instantiates | |
830 | the resulting class. This accepts a Value argument and returns a | |
831 | pretty printer instance, or None. This function is useful as an | |
832 | argument to the MI command -var-set-visualizer. */ | |
833 | PyObject * | |
834 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
835 | { | |
836 | PyObject *val_obj; | |
f92adf3c | 837 | PyObject *cons; |
b6313243 TT |
838 | struct value *value; |
839 | ||
840 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
841 | return NULL; | |
842 | value = value_object_to_value (val_obj); | |
843 | if (! value) | |
844 | { | |
256458bc | 845 | PyErr_SetString (PyExc_TypeError, |
044c0f87 | 846 | _("Argument must be a gdb.Value.")); |
b6313243 TT |
847 | return NULL; |
848 | } | |
849 | ||
850 | cons = find_pretty_printer (val_obj); | |
851 | return cons; | |
852 | } |