Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 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" | |
6bf0ce2b | 26 | |
a6bac58e | 27 | #include "python.h" |
a6bac58e TT |
28 | |
29 | #ifdef HAVE_PYTHON | |
6bf0ce2b PP |
30 | #include "python-internal.h" |
31 | ||
621c8364 TT |
32 | /* Return type of print_string_repr. */ |
33 | ||
34 | enum string_repr_result | |
35 | { | |
36 | /* The string method returned None. */ | |
37 | string_repr_none, | |
38 | /* The string method had an error. */ | |
39 | string_repr_error, | |
40 | /* Everything ok. */ | |
41 | string_repr_ok | |
42 | }; | |
43 | ||
a6bac58e TT |
44 | /* Helper function for find_pretty_printer which iterates over a list, |
45 | calls each function and inspects output. This will return a | |
46 | printer object if one recognizes VALUE. If no printer is found, it | |
47 | will return None. On error, it will set the Python error and | |
48 | return NULL. */ | |
fa33c3cd | 49 | |
a6bac58e TT |
50 | static PyObject * |
51 | search_pp_list (PyObject *list, PyObject *value) | |
52 | { | |
53 | Py_ssize_t pp_list_size, list_index; | |
54 | PyObject *function, *printer = NULL; | |
55 | ||
56 | pp_list_size = PyList_Size (list); | |
57 | for (list_index = 0; list_index < pp_list_size; list_index++) | |
58 | { | |
59 | function = PyList_GetItem (list, list_index); | |
60 | if (! function) | |
61 | return NULL; | |
62 | ||
967cf477 | 63 | /* Skip if disabled. */ |
1bdb0c54 TT |
64 | if (PyObject_HasAttr (function, gdbpy_enabled_cst)) |
65 | { | |
66 | PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst); | |
67 | int cmp; | |
68 | ||
69 | if (!attr) | |
70 | return NULL; | |
71 | cmp = PyObject_IsTrue (attr); | |
7d38e38f | 72 | Py_DECREF (attr); |
1bdb0c54 TT |
73 | if (cmp == -1) |
74 | return NULL; | |
75 | ||
76 | if (!cmp) | |
77 | continue; | |
78 | } | |
967cf477 | 79 | |
a6bac58e TT |
80 | printer = PyObject_CallFunctionObjArgs (function, value, NULL); |
81 | if (! printer) | |
82 | return NULL; | |
83 | else if (printer != Py_None) | |
84 | return printer; | |
85 | ||
86 | Py_DECREF (printer); | |
87 | } | |
88 | ||
89 | Py_RETURN_NONE; | |
90 | } | |
91 | ||
fa33c3cd DE |
92 | /* Subroutine of find_pretty_printer to simplify it. |
93 | Look for a pretty-printer to print VALUE in all objfiles. | |
94 | The result is NULL if there's an error and the search should be terminated. | |
95 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
96 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
97 | ||
a6bac58e | 98 | static PyObject * |
fa33c3cd | 99 | find_pretty_printer_from_objfiles (PyObject *value) |
a6bac58e | 100 | { |
fa33c3cd DE |
101 | PyObject *pp_list; |
102 | PyObject *function; | |
a6bac58e | 103 | struct objfile *obj; |
a6bac58e | 104 | |
a6bac58e TT |
105 | ALL_OBJFILES (obj) |
106 | { | |
107 | PyObject *objf = objfile_to_objfile_object (obj); | |
108 | if (!objf) | |
109 | { | |
110 | /* Ignore the error and continue. */ | |
111 | PyErr_Clear (); | |
112 | continue; | |
113 | } | |
114 | ||
115 | pp_list = objfpy_get_printers (objf, NULL); | |
116 | function = search_pp_list (pp_list, value); | |
fa33c3cd | 117 | Py_XDECREF (pp_list); |
a6bac58e | 118 | |
fa33c3cd | 119 | /* If there is an error in any objfile list, abort the search and exit. */ |
a6bac58e | 120 | if (! function) |
fa33c3cd | 121 | return NULL; |
a6bac58e TT |
122 | |
123 | if (function != Py_None) | |
fa33c3cd | 124 | return function; |
256458bc | 125 | |
a6bac58e | 126 | Py_DECREF (function); |
a6bac58e TT |
127 | } |
128 | ||
fa33c3cd DE |
129 | Py_RETURN_NONE; |
130 | } | |
131 | ||
132 | /* Subroutine of find_pretty_printer to simplify it. | |
133 | Look for a pretty-printer to print VALUE in the current program space. | |
134 | The result is NULL if there's an error and the search should be terminated. | |
135 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
136 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
137 | ||
138 | static PyObject * | |
139 | find_pretty_printer_from_progspace (PyObject *value) | |
140 | { | |
141 | PyObject *pp_list; | |
142 | PyObject *function; | |
143 | PyObject *obj = pspace_to_pspace_object (current_program_space); | |
144 | ||
145 | if (!obj) | |
146 | return NULL; | |
147 | pp_list = pspy_get_printers (obj, NULL); | |
148 | function = search_pp_list (pp_list, value); | |
149 | Py_XDECREF (pp_list); | |
150 | return function; | |
151 | } | |
152 | ||
153 | /* Subroutine of find_pretty_printer to simplify it. | |
154 | Look for a pretty-printer to print VALUE in the gdb module. | |
155 | The result is NULL if there's an error and the search should be terminated. | |
156 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
157 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
158 | ||
159 | static PyObject * | |
160 | find_pretty_printer_from_gdb (PyObject *value) | |
161 | { | |
162 | PyObject *pp_list; | |
163 | PyObject *function; | |
164 | ||
23fa7f66 | 165 | /* Fetch the global pretty printer list. */ |
b9516fa1 YPK |
166 | if (gdb_python_module == NULL |
167 | || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers")) | |
fa33c3cd | 168 | Py_RETURN_NONE; |
b9516fa1 | 169 | pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers"); |
fa33c3cd | 170 | if (pp_list == NULL || ! PyList_Check (pp_list)) |
a6bac58e | 171 | { |
fa33c3cd DE |
172 | Py_XDECREF (pp_list); |
173 | Py_RETURN_NONE; | |
a6bac58e | 174 | } |
a6bac58e TT |
175 | |
176 | function = search_pp_list (pp_list, value); | |
a6bac58e | 177 | Py_XDECREF (pp_list); |
a6bac58e TT |
178 | return function; |
179 | } | |
be759fcf | 180 | |
fa33c3cd DE |
181 | /* Find the pretty-printing constructor function for VALUE. If no |
182 | pretty-printer exists, return None. If one exists, return a new | |
183 | reference. On error, set the Python error and return NULL. */ | |
184 | ||
185 | static PyObject * | |
186 | find_pretty_printer (PyObject *value) | |
187 | { | |
188 | PyObject *function; | |
189 | ||
23fa7f66 | 190 | /* Look at the pretty-printer list for each objfile |
fa33c3cd DE |
191 | in the current program-space. */ |
192 | function = find_pretty_printer_from_objfiles (value); | |
193 | if (function == NULL || function != Py_None) | |
194 | return function; | |
195 | Py_DECREF (function); | |
196 | ||
23fa7f66 | 197 | /* Look at the pretty-printer list for the current program-space. */ |
fa33c3cd DE |
198 | function = find_pretty_printer_from_progspace (value); |
199 | if (function == NULL || function != Py_None) | |
200 | return function; | |
201 | Py_DECREF (function); | |
202 | ||
23fa7f66 | 203 | /* Look at the pretty-printer list in the gdb module. */ |
fa33c3cd DE |
204 | function = find_pretty_printer_from_gdb (value); |
205 | return function; | |
206 | } | |
be759fcf | 207 | |
fbb8f299 PM |
208 | /* Pretty-print a single value, via the printer object PRINTER. |
209 | If the function returns a string, a PyObject containing the string | |
79f283fe PM |
210 | is returned. If the function returns Py_NONE that means the pretty |
211 | printer returned the Python None as a value. Otherwise, if the | |
212 | function returns a value, *OUT_VALUE is set to the value, and NULL | |
8dc78533 JK |
213 | is returned. On error, *OUT_VALUE is set to NULL, NULL is |
214 | returned, with a python exception set. */ | |
79f283fe | 215 | |
fbb8f299 | 216 | static PyObject * |
a6bac58e TT |
217 | pretty_print_one_value (PyObject *printer, struct value **out_value) |
218 | { | |
a6bac58e | 219 | volatile struct gdb_exception except; |
fbb8f299 | 220 | PyObject *result = NULL; |
a6bac58e | 221 | |
fbb8f299 | 222 | *out_value = NULL; |
a6bac58e TT |
223 | TRY_CATCH (except, RETURN_MASK_ALL) |
224 | { | |
a6bac58e TT |
225 | result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL); |
226 | if (result) | |
227 | { | |
256458bc | 228 | if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result) |
79f283fe | 229 | && result != Py_None) |
fbb8f299 PM |
230 | { |
231 | *out_value = convert_value_from_python (result); | |
232 | if (PyErr_Occurred ()) | |
233 | *out_value = NULL; | |
234 | Py_DECREF (result); | |
235 | result = NULL; | |
236 | } | |
a6bac58e TT |
237 | } |
238 | } | |
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. */ | |
247 | char * | |
248 | gdbpy_get_display_hint (PyObject *printer) | |
249 | { | |
250 | PyObject *hint; | |
251 | char *result = NULL; | |
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; | |
282 | char *msg; | |
283 | ||
284 | PyErr_Fetch (&type, &value, &trace); | |
285 | cleanup = make_cleanup_py_decref (type); | |
286 | make_cleanup_py_decref (value); | |
287 | make_cleanup_py_decref (trace); | |
288 | ||
289 | msg = gdbpy_exception_to_string (type, value); | |
290 | make_cleanup (xfree, msg); | |
291 | ||
292 | if (msg == NULL || *msg == '\0') | |
1dae3efc | 293 | fprintf_filtered (stream, _("<error reading variable>")); |
621c8364 TT |
294 | else |
295 | fprintf_filtered (stream, _("<error reading variable: %s>"), msg); | |
296 | ||
297 | do_cleanups (cleanup); | |
298 | } | |
299 | else | |
300 | gdbpy_print_stack (); | |
301 | } | |
302 | ||
a6bac58e | 303 | /* Helper for 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 | { | |
396 | PyFrameObject *frame = p; | |
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 TT |
469 | |
470 | /* Helper for 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 | ||
557 | if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) | |
558 | { | |
559 | gdbpy_print_stack (); | |
560 | Py_DECREF (item); | |
561 | continue; | |
562 | } | |
563 | inner_cleanup = make_cleanup_py_decref (item); | |
564 | ||
565 | /* Print initial "{". For other elements, there are three | |
566 | cases: | |
567 | 1. Maps. Print a "," after each value element. | |
568 | 2. Arrays. Always print a ",". | |
569 | 3. Other. Always print a ",". */ | |
570 | if (i == 0) | |
79f283fe PM |
571 | { |
572 | if (is_py_none) | |
573 | fputs_filtered ("{", stream); | |
574 | else | |
575 | fputs_filtered (" = {", stream); | |
576 | } | |
577 | ||
a6bac58e TT |
578 | else if (! is_map || i % 2 == 0) |
579 | fputs_filtered (pretty ? "," : ", ", stream); | |
580 | ||
581 | /* In summary mode, we just want to print "= {...}" if there is | |
582 | a value. */ | |
583 | if (options->summary) | |
584 | { | |
585 | /* This increment tricks the post-loop logic to print what | |
586 | we want. */ | |
587 | ++i; | |
588 | /* Likewise. */ | |
589 | pretty = 0; | |
590 | break; | |
591 | } | |
592 | ||
593 | if (! is_map || i % 2 == 0) | |
594 | { | |
595 | if (pretty) | |
596 | { | |
597 | fputs_filtered ("\n", stream); | |
598 | print_spaces_filtered (2 + 2 * recurse, stream); | |
599 | } | |
600 | else | |
601 | wrap_here (n_spaces (2 + 2 *recurse)); | |
602 | } | |
603 | ||
604 | if (is_map && i % 2 == 0) | |
605 | fputs_filtered ("[", stream); | |
606 | else if (is_array) | |
607 | { | |
608 | /* We print the index, not whatever the child method | |
609 | returned as the name. */ | |
610 | if (options->print_array_indexes) | |
611 | fprintf_filtered (stream, "[%d] = ", i); | |
612 | } | |
613 | else if (! is_map) | |
614 | { | |
615 | fputs_filtered (name, stream); | |
616 | fputs_filtered (" = ", stream); | |
617 | } | |
618 | ||
09ca9e2e | 619 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 620 | { |
09ca9e2e TT |
621 | CORE_ADDR addr; |
622 | struct type *type; | |
623 | long length; | |
624 | char *encoding = NULL; | |
a81766d8 | 625 | struct value_print_options local_opts = *options; |
be759fcf | 626 | |
09ca9e2e TT |
627 | make_cleanup (free_current_contents, &encoding); |
628 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); | |
629 | ||
a81766d8 | 630 | local_opts.addressprint = 0; |
09ca9e2e | 631 | val_print_string (type, encoding, addr, (int) length, stream, |
a81766d8 | 632 | &local_opts); |
09ca9e2e TT |
633 | } |
634 | else if (gdbpy_is_string (py_v)) | |
635 | { | |
89f6d837 | 636 | char *output; |
09ca9e2e TT |
637 | |
638 | output = python_string_to_host_string (py_v); | |
639 | if (!output) | |
640 | gdbpy_print_stack (); | |
a6bac58e TT |
641 | else |
642 | { | |
09ca9e2e | 643 | fputs_filtered (output, stream); |
be759fcf | 644 | xfree (output); |
a6bac58e TT |
645 | } |
646 | } | |
647 | else | |
648 | { | |
649 | struct value *value = convert_value_from_python (py_v); | |
650 | ||
651 | if (value == NULL) | |
652 | { | |
653 | gdbpy_print_stack (); | |
654 | error (_("Error while executing Python code.")); | |
655 | } | |
656 | else | |
657 | common_val_print (value, stream, recurse + 1, options, language); | |
658 | } | |
659 | ||
660 | if (is_map && i % 2 == 0) | |
661 | fputs_filtered ("] = ", stream); | |
662 | ||
663 | do_cleanups (inner_cleanup); | |
664 | } | |
665 | ||
666 | if (i) | |
667 | { | |
668 | if (!done_flag) | |
669 | { | |
670 | if (pretty) | |
671 | { | |
672 | fputs_filtered ("\n", stream); | |
673 | print_spaces_filtered (2 + 2 * recurse, stream); | |
674 | } | |
675 | fputs_filtered ("...", stream); | |
676 | } | |
677 | if (pretty) | |
678 | { | |
679 | fputs_filtered ("\n", stream); | |
680 | print_spaces_filtered (2 * recurse, stream); | |
681 | } | |
682 | fputs_filtered ("}", stream); | |
683 | } | |
684 | ||
685 | done: | |
686 | do_cleanups (cleanups); | |
687 | } | |
688 | ||
689 | int | |
690 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
691 | int embedded_offset, CORE_ADDR address, | |
692 | struct ui_file *stream, int recurse, | |
0e03807e | 693 | const struct value *val, |
a6bac58e TT |
694 | const struct value_print_options *options, |
695 | const struct language_defn *language) | |
696 | { | |
50810684 | 697 | struct gdbarch *gdbarch = get_type_arch (type); |
a6bac58e TT |
698 | PyObject *printer = NULL; |
699 | PyObject *val_obj = NULL; | |
700 | struct value *value; | |
701 | char *hint = NULL; | |
702 | struct cleanup *cleanups; | |
703 | int result = 0; | |
621c8364 | 704 | enum string_repr_result print_result; |
4e07d55f PA |
705 | |
706 | /* No pretty-printer support for unavailable values. */ | |
63360adc | 707 | if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
4e07d55f | 708 | return 0; |
0646da15 TT |
709 | |
710 | if (!gdb_python_initialized) | |
711 | return 0; | |
4e07d55f | 712 | |
d452c4bc | 713 | cleanups = ensure_python_env (gdbarch, language); |
a6bac58e TT |
714 | |
715 | /* Instantiate the printer. */ | |
716 | if (valaddr) | |
717 | valaddr += embedded_offset; | |
edf3d5f3 TT |
718 | value = value_from_contents_and_address (type, valaddr, |
719 | address + embedded_offset); | |
63360adc MS |
720 | |
721 | set_value_component_location (value, val); | |
722 | /* set_value_component_location resets the address, so we may | |
723 | need to set it again. */ | |
724 | if (VALUE_LVAL (value) != lval_internalvar | |
725 | && VALUE_LVAL (value) != lval_internalvar_component | |
726 | && VALUE_LVAL (value) != lval_computed) | |
727 | set_value_address (value, address + embedded_offset); | |
a6bac58e TT |
728 | |
729 | val_obj = value_to_value_object (value); | |
730 | if (! val_obj) | |
731 | goto done; | |
256458bc | 732 | |
a6bac58e TT |
733 | /* Find the constructor. */ |
734 | printer = find_pretty_printer (val_obj); | |
735 | Py_DECREF (val_obj); | |
c8c735b9 PA |
736 | |
737 | if (printer == NULL) | |
738 | goto done; | |
739 | ||
a6bac58e | 740 | make_cleanup_py_decref (printer); |
c8c735b9 | 741 | if (printer == Py_None) |
a6bac58e TT |
742 | goto done; |
743 | ||
744 | /* If we are printing a map, we want some special formatting. */ | |
745 | hint = gdbpy_get_display_hint (printer); | |
746 | make_cleanup (free_current_contents, &hint); | |
747 | ||
748 | /* Print the section */ | |
621c8364 TT |
749 | print_result = print_string_repr (printer, hint, stream, recurse, |
750 | options, language, gdbarch); | |
751 | if (print_result != string_repr_error) | |
752 | print_children (printer, hint, stream, recurse, options, language, | |
753 | print_result == string_repr_none); | |
79f283fe | 754 | |
a6bac58e TT |
755 | result = 1; |
756 | ||
757 | ||
758 | done: | |
759 | if (PyErr_Occurred ()) | |
621c8364 | 760 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
761 | do_cleanups (cleanups); |
762 | return result; | |
763 | } | |
764 | ||
fbb8f299 | 765 | |
b6313243 TT |
766 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
767 | print object. It must have a 'to_string' method (but this is | |
768 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
769 | returns a string. The printer will return a value and in the case |
770 | of a Python string being returned, this function will return a | |
771 | PyObject containing the string. For any other type, *REPLACEMENT is | |
772 | set to the replacement value and this function returns NULL. On | |
773 | error, *REPLACEMENT is set to NULL and this function also returns | |
774 | NULL. */ | |
775 | PyObject * | |
b6313243 | 776 | apply_varobj_pretty_printer (PyObject *printer_obj, |
621c8364 TT |
777 | struct value **replacement, |
778 | struct ui_file *stream) | |
b6313243 | 779 | { |
fbb8f299 | 780 | PyObject *py_str = NULL; |
b6313243 TT |
781 | |
782 | *replacement = NULL; | |
fbb8f299 PM |
783 | py_str = pretty_print_one_value (printer_obj, replacement); |
784 | ||
785 | if (*replacement == NULL && py_str == NULL) | |
621c8364 | 786 | print_stack_unless_memory_error (stream); |
b6313243 | 787 | |
fbb8f299 | 788 | return py_str; |
b6313243 TT |
789 | } |
790 | ||
791 | /* Find a pretty-printer object for the varobj module. Returns a new | |
792 | reference to the object if successful; returns NULL if not. VALUE | |
256458bc | 793 | is the value for which a printer tests to determine if it |
b6313243 TT |
794 | can pretty-print the value. */ |
795 | PyObject * | |
796 | gdbpy_get_varobj_pretty_printer (struct value *value) | |
797 | { | |
798 | PyObject *val_obj; | |
799 | PyObject *pretty_printer = NULL; | |
800 | volatile struct gdb_exception except; | |
801 | ||
802 | TRY_CATCH (except, RETURN_MASK_ALL) | |
803 | { | |
804 | value = value_copy (value); | |
805 | } | |
806 | GDB_PY_HANDLE_EXCEPTION (except); | |
256458bc | 807 | |
b6313243 TT |
808 | val_obj = value_to_value_object (value); |
809 | if (! val_obj) | |
810 | return NULL; | |
811 | ||
812 | pretty_printer = find_pretty_printer (val_obj); | |
813 | Py_DECREF (val_obj); | |
814 | return pretty_printer; | |
815 | } | |
816 | ||
817 | /* A Python function which wraps find_pretty_printer and instantiates | |
818 | the resulting class. This accepts a Value argument and returns a | |
819 | pretty printer instance, or None. This function is useful as an | |
820 | argument to the MI command -var-set-visualizer. */ | |
821 | PyObject * | |
822 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
823 | { | |
824 | PyObject *val_obj; | |
f92adf3c | 825 | PyObject *cons; |
b6313243 TT |
826 | struct value *value; |
827 | ||
828 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
829 | return NULL; | |
830 | value = value_object_to_value (val_obj); | |
831 | if (! value) | |
832 | { | |
256458bc | 833 | PyErr_SetString (PyExc_TypeError, |
044c0f87 | 834 | _("Argument must be a gdb.Value.")); |
b6313243 TT |
835 | return NULL; |
836 | } | |
837 | ||
838 | cons = find_pretty_printer (val_obj); | |
839 | return cons; | |
840 | } | |
841 | ||
a6bac58e TT |
842 | #else /* HAVE_PYTHON */ |
843 | ||
844 | int | |
845 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
846 | int embedded_offset, CORE_ADDR address, | |
6bf0ce2b | 847 | struct ui_file *stream, int recurse, |
0e03807e | 848 | const struct value *val, |
6bf0ce2b | 849 | const struct value_print_options *options, |
a6bac58e TT |
850 | const struct language_defn *language) |
851 | { | |
852 | return 0; | |
853 | } | |
854 | ||
855 | #endif /* HAVE_PYTHON */ |