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