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