Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
0b302171 | 3 | Copyright (C) 2008-2012 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); | |
72 | if (cmp == -1) | |
73 | return NULL; | |
74 | ||
75 | if (!cmp) | |
76 | continue; | |
77 | } | |
967cf477 | 78 | |
a6bac58e TT |
79 | printer = PyObject_CallFunctionObjArgs (function, value, NULL); |
80 | if (! printer) | |
81 | return NULL; | |
82 | else if (printer != Py_None) | |
83 | return printer; | |
84 | ||
85 | Py_DECREF (printer); | |
86 | } | |
87 | ||
88 | Py_RETURN_NONE; | |
89 | } | |
90 | ||
fa33c3cd DE |
91 | /* Subroutine of find_pretty_printer to simplify it. |
92 | Look for a pretty-printer to print VALUE in all objfiles. | |
93 | The result is NULL if there's an error and the search should be terminated. | |
94 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
95 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
96 | ||
a6bac58e | 97 | static PyObject * |
fa33c3cd | 98 | find_pretty_printer_from_objfiles (PyObject *value) |
a6bac58e | 99 | { |
fa33c3cd DE |
100 | PyObject *pp_list; |
101 | PyObject *function; | |
a6bac58e | 102 | struct objfile *obj; |
a6bac58e | 103 | |
a6bac58e TT |
104 | ALL_OBJFILES (obj) |
105 | { | |
106 | PyObject *objf = objfile_to_objfile_object (obj); | |
107 | if (!objf) | |
108 | { | |
109 | /* Ignore the error and continue. */ | |
110 | PyErr_Clear (); | |
111 | continue; | |
112 | } | |
113 | ||
114 | pp_list = objfpy_get_printers (objf, NULL); | |
115 | function = search_pp_list (pp_list, value); | |
fa33c3cd | 116 | Py_XDECREF (pp_list); |
a6bac58e | 117 | |
fa33c3cd | 118 | /* If there is an error in any objfile list, abort the search and exit. */ |
a6bac58e | 119 | if (! function) |
fa33c3cd | 120 | return NULL; |
a6bac58e TT |
121 | |
122 | if (function != Py_None) | |
fa33c3cd | 123 | return function; |
a6bac58e TT |
124 | |
125 | Py_DECREF (function); | |
a6bac58e TT |
126 | } |
127 | ||
fa33c3cd DE |
128 | Py_RETURN_NONE; |
129 | } | |
130 | ||
131 | /* Subroutine of find_pretty_printer to simplify it. | |
132 | Look for a pretty-printer to print VALUE in the current program space. | |
133 | The result is NULL if there's an error and the search should be terminated. | |
134 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
135 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
136 | ||
137 | static PyObject * | |
138 | find_pretty_printer_from_progspace (PyObject *value) | |
139 | { | |
140 | PyObject *pp_list; | |
141 | PyObject *function; | |
142 | PyObject *obj = pspace_to_pspace_object (current_program_space); | |
143 | ||
144 | if (!obj) | |
145 | return NULL; | |
146 | pp_list = pspy_get_printers (obj, NULL); | |
147 | function = search_pp_list (pp_list, value); | |
148 | Py_XDECREF (pp_list); | |
149 | return function; | |
150 | } | |
151 | ||
152 | /* Subroutine of find_pretty_printer to simplify it. | |
153 | Look for a pretty-printer to print VALUE in the gdb module. | |
154 | The result is NULL if there's an error and the search should be terminated. | |
155 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
156 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
157 | ||
158 | static PyObject * | |
159 | find_pretty_printer_from_gdb (PyObject *value) | |
160 | { | |
161 | PyObject *pp_list; | |
162 | PyObject *function; | |
163 | ||
23fa7f66 | 164 | /* Fetch the global pretty printer list. */ |
a6bac58e | 165 | if (! PyObject_HasAttrString (gdb_module, "pretty_printers")) |
fa33c3cd DE |
166 | Py_RETURN_NONE; |
167 | pp_list = PyObject_GetAttrString (gdb_module, "pretty_printers"); | |
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 | { | |
79f283fe PM |
226 | if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result) |
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 | ||
a6bac58e | 301 | /* Helper for 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 | { |
09ca9e2e TT |
345 | gdb_byte *output; |
346 | long length; | |
347 | struct type *type; | |
348 | ||
349 | make_cleanup_py_decref (string); | |
350 | output = PyString_AsString (string); | |
351 | length = PyString_Size (string); | |
352 | type = builtin_type (gdbarch)->builtin_char; | |
353 | ||
354 | if (hint && !strcmp (hint, "string")) | |
355 | LA_PRINT_STRING (stream, type, output, length, NULL, | |
79f283fe PM |
356 | 0, options); |
357 | else | |
358 | fputs_filtered (output, stream); | |
be759fcf PM |
359 | } |
360 | else | |
621c8364 TT |
361 | { |
362 | result = string_repr_error; | |
363 | print_stack_unless_memory_error (stream); | |
364 | } | |
79f283fe | 365 | } |
09ca9e2e TT |
366 | |
367 | do_cleanups (cleanup); | |
a6bac58e TT |
368 | } |
369 | else if (replacement) | |
269f82e5 PP |
370 | { |
371 | struct value_print_options opts = *options; | |
372 | ||
373 | opts.addressprint = 0; | |
374 | common_val_print (replacement, stream, recurse, &opts, language); | |
375 | } | |
a6bac58e | 376 | else |
621c8364 TT |
377 | { |
378 | result = string_repr_error; | |
379 | print_stack_unless_memory_error (stream); | |
380 | } | |
79f283fe | 381 | |
621c8364 | 382 | return result; |
a6bac58e TT |
383 | } |
384 | ||
385 | static void | |
386 | py_restore_tstate (void *p) | |
387 | { | |
388 | PyFrameObject *frame = p; | |
389 | PyThreadState *tstate = PyThreadState_GET (); | |
d59b6f6c | 390 | |
a6bac58e TT |
391 | tstate->frame = frame; |
392 | } | |
393 | ||
394 | /* Create a dummy PyFrameObject, needed to work around | |
395 | a Python-2.4 bug with generators. */ | |
396 | static PyObject * | |
eeae04df | 397 | push_dummy_python_frame (void) |
a6bac58e TT |
398 | { |
399 | PyObject *empty_string, *null_tuple, *globals; | |
400 | PyCodeObject *code; | |
401 | PyFrameObject *frame; | |
402 | PyThreadState *tstate; | |
403 | ||
404 | empty_string = PyString_FromString (""); | |
405 | if (!empty_string) | |
406 | return NULL; | |
407 | ||
408 | null_tuple = PyTuple_New (0); | |
409 | if (!null_tuple) | |
410 | { | |
411 | Py_DECREF (empty_string); | |
412 | return NULL; | |
413 | } | |
414 | ||
415 | code = PyCode_New (0, /* argcount */ | |
416 | 0, /* nlocals */ | |
417 | 0, /* stacksize */ | |
418 | 0, /* flags */ | |
419 | empty_string, /* code */ | |
420 | null_tuple, /* consts */ | |
421 | null_tuple, /* names */ | |
422 | null_tuple, /* varnames */ | |
423 | #if PYTHON_API_VERSION >= 1010 | |
424 | null_tuple, /* freevars */ | |
425 | null_tuple, /* cellvars */ | |
426 | #endif | |
427 | empty_string, /* filename */ | |
428 | empty_string, /* name */ | |
429 | 1, /* firstlineno */ | |
430 | empty_string /* lnotab */ | |
431 | ); | |
432 | ||
433 | Py_DECREF (empty_string); | |
434 | Py_DECREF (null_tuple); | |
435 | ||
436 | if (!code) | |
437 | return NULL; | |
438 | ||
439 | globals = PyDict_New (); | |
440 | if (!globals) | |
441 | { | |
442 | Py_DECREF (code); | |
443 | return NULL; | |
444 | } | |
445 | ||
446 | tstate = PyThreadState_GET (); | |
447 | ||
448 | frame = PyFrame_New (tstate, code, globals, NULL); | |
449 | ||
450 | Py_DECREF (globals); | |
451 | Py_DECREF (code); | |
452 | ||
453 | if (!frame) | |
454 | return NULL; | |
455 | ||
456 | tstate->frame = frame; | |
457 | make_cleanup (py_restore_tstate, frame->f_back); | |
458 | return (PyObject *) frame; | |
459 | } | |
460 | ||
461 | /* Helper for apply_val_pretty_printer that formats children of the | |
79f283fe PM |
462 | printer, if any exist. If is_py_none is true, then nothing has |
463 | been printed by to_string, and format output accordingly. */ | |
a6bac58e TT |
464 | static void |
465 | print_children (PyObject *printer, const char *hint, | |
466 | struct ui_file *stream, int recurse, | |
467 | const struct value_print_options *options, | |
79f283fe PM |
468 | const struct language_defn *language, |
469 | int is_py_none) | |
a6bac58e TT |
470 | { |
471 | int is_map, is_array, done_flag, pretty; | |
472 | unsigned int i; | |
473 | PyObject *children, *iter, *frame; | |
474 | struct cleanup *cleanups; | |
475 | ||
476 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
477 | return; | |
478 | ||
479 | /* If we are printing a map or an array, we want some special | |
480 | formatting. */ | |
481 | is_map = hint && ! strcmp (hint, "map"); | |
482 | is_array = hint && ! strcmp (hint, "array"); | |
483 | ||
484 | children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, | |
485 | NULL); | |
486 | if (! children) | |
487 | { | |
621c8364 | 488 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
489 | return; |
490 | } | |
491 | ||
492 | cleanups = make_cleanup_py_decref (children); | |
493 | ||
494 | iter = PyObject_GetIter (children); | |
495 | if (!iter) | |
496 | { | |
621c8364 | 497 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
498 | goto done; |
499 | } | |
500 | make_cleanup_py_decref (iter); | |
501 | ||
502 | /* Use the prettyprint_arrays option if we are printing an array, | |
503 | and the pretty option otherwise. */ | |
a81766d8 TT |
504 | if (is_array) |
505 | pretty = options->prettyprint_arrays; | |
506 | else | |
507 | { | |
508 | if (options->pretty == Val_prettyprint) | |
509 | pretty = 1; | |
510 | else | |
511 | pretty = options->prettyprint_structs; | |
512 | } | |
a6bac58e TT |
513 | |
514 | /* Manufacture a dummy Python frame to work around Python 2.4 bug, | |
515 | where it insists on having a non-NULL tstate->frame when | |
516 | a generator is called. */ | |
517 | frame = push_dummy_python_frame (); | |
518 | if (!frame) | |
519 | { | |
520 | gdbpy_print_stack (); | |
521 | goto done; | |
522 | } | |
523 | make_cleanup_py_decref (frame); | |
524 | ||
525 | done_flag = 0; | |
526 | for (i = 0; i < options->print_max; ++i) | |
527 | { | |
528 | PyObject *py_v, *item = PyIter_Next (iter); | |
ddd49eee | 529 | const char *name; |
a6bac58e TT |
530 | struct cleanup *inner_cleanup; |
531 | ||
532 | if (! item) | |
533 | { | |
534 | if (PyErr_Occurred ()) | |
621c8364 | 535 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
536 | /* Set a flag so we can know whether we printed all the |
537 | available elements. */ | |
538 | else | |
539 | done_flag = 1; | |
540 | break; | |
541 | } | |
542 | ||
543 | if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) | |
544 | { | |
545 | gdbpy_print_stack (); | |
546 | Py_DECREF (item); | |
547 | continue; | |
548 | } | |
549 | inner_cleanup = make_cleanup_py_decref (item); | |
550 | ||
551 | /* Print initial "{". For other elements, there are three | |
552 | cases: | |
553 | 1. Maps. Print a "," after each value element. | |
554 | 2. Arrays. Always print a ",". | |
555 | 3. Other. Always print a ",". */ | |
556 | if (i == 0) | |
79f283fe PM |
557 | { |
558 | if (is_py_none) | |
559 | fputs_filtered ("{", stream); | |
560 | else | |
561 | fputs_filtered (" = {", stream); | |
562 | } | |
563 | ||
a6bac58e TT |
564 | else if (! is_map || i % 2 == 0) |
565 | fputs_filtered (pretty ? "," : ", ", stream); | |
566 | ||
567 | /* In summary mode, we just want to print "= {...}" if there is | |
568 | a value. */ | |
569 | if (options->summary) | |
570 | { | |
571 | /* This increment tricks the post-loop logic to print what | |
572 | we want. */ | |
573 | ++i; | |
574 | /* Likewise. */ | |
575 | pretty = 0; | |
576 | break; | |
577 | } | |
578 | ||
579 | if (! is_map || i % 2 == 0) | |
580 | { | |
581 | if (pretty) | |
582 | { | |
583 | fputs_filtered ("\n", stream); | |
584 | print_spaces_filtered (2 + 2 * recurse, stream); | |
585 | } | |
586 | else | |
587 | wrap_here (n_spaces (2 + 2 *recurse)); | |
588 | } | |
589 | ||
590 | if (is_map && i % 2 == 0) | |
591 | fputs_filtered ("[", stream); | |
592 | else if (is_array) | |
593 | { | |
594 | /* We print the index, not whatever the child method | |
595 | returned as the name. */ | |
596 | if (options->print_array_indexes) | |
597 | fprintf_filtered (stream, "[%d] = ", i); | |
598 | } | |
599 | else if (! is_map) | |
600 | { | |
601 | fputs_filtered (name, stream); | |
602 | fputs_filtered (" = ", stream); | |
603 | } | |
604 | ||
09ca9e2e | 605 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 606 | { |
09ca9e2e TT |
607 | CORE_ADDR addr; |
608 | struct type *type; | |
609 | long length; | |
610 | char *encoding = NULL; | |
a81766d8 | 611 | struct value_print_options local_opts = *options; |
be759fcf | 612 | |
09ca9e2e TT |
613 | make_cleanup (free_current_contents, &encoding); |
614 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); | |
615 | ||
a81766d8 | 616 | local_opts.addressprint = 0; |
09ca9e2e | 617 | val_print_string (type, encoding, addr, (int) length, stream, |
a81766d8 | 618 | &local_opts); |
09ca9e2e TT |
619 | |
620 | do_cleanups (inner_cleanup); | |
621 | } | |
622 | else if (gdbpy_is_string (py_v)) | |
623 | { | |
624 | gdb_byte *output; | |
625 | ||
626 | output = python_string_to_host_string (py_v); | |
627 | if (!output) | |
628 | gdbpy_print_stack (); | |
a6bac58e TT |
629 | else |
630 | { | |
09ca9e2e | 631 | fputs_filtered (output, stream); |
be759fcf | 632 | xfree (output); |
a6bac58e TT |
633 | } |
634 | } | |
635 | else | |
636 | { | |
637 | struct value *value = convert_value_from_python (py_v); | |
638 | ||
639 | if (value == NULL) | |
640 | { | |
641 | gdbpy_print_stack (); | |
642 | error (_("Error while executing Python code.")); | |
643 | } | |
644 | else | |
645 | common_val_print (value, stream, recurse + 1, options, language); | |
646 | } | |
647 | ||
648 | if (is_map && i % 2 == 0) | |
649 | fputs_filtered ("] = ", stream); | |
650 | ||
651 | do_cleanups (inner_cleanup); | |
652 | } | |
653 | ||
654 | if (i) | |
655 | { | |
656 | if (!done_flag) | |
657 | { | |
658 | if (pretty) | |
659 | { | |
660 | fputs_filtered ("\n", stream); | |
661 | print_spaces_filtered (2 + 2 * recurse, stream); | |
662 | } | |
663 | fputs_filtered ("...", stream); | |
664 | } | |
665 | if (pretty) | |
666 | { | |
667 | fputs_filtered ("\n", stream); | |
668 | print_spaces_filtered (2 * recurse, stream); | |
669 | } | |
670 | fputs_filtered ("}", stream); | |
671 | } | |
672 | ||
673 | done: | |
674 | do_cleanups (cleanups); | |
675 | } | |
676 | ||
677 | int | |
678 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
679 | int embedded_offset, CORE_ADDR address, | |
680 | struct ui_file *stream, int recurse, | |
0e03807e | 681 | const struct value *val, |
a6bac58e TT |
682 | const struct value_print_options *options, |
683 | const struct language_defn *language) | |
684 | { | |
50810684 | 685 | struct gdbarch *gdbarch = get_type_arch (type); |
a6bac58e TT |
686 | PyObject *printer = NULL; |
687 | PyObject *val_obj = NULL; | |
688 | struct value *value; | |
689 | char *hint = NULL; | |
690 | struct cleanup *cleanups; | |
691 | int result = 0; | |
621c8364 | 692 | enum string_repr_result print_result; |
4e07d55f PA |
693 | |
694 | /* No pretty-printer support for unavailable values. */ | |
63360adc | 695 | if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
4e07d55f PA |
696 | return 0; |
697 | ||
d452c4bc | 698 | cleanups = ensure_python_env (gdbarch, language); |
a6bac58e TT |
699 | |
700 | /* Instantiate the printer. */ | |
701 | if (valaddr) | |
702 | valaddr += embedded_offset; | |
edf3d5f3 TT |
703 | value = value_from_contents_and_address (type, valaddr, |
704 | address + embedded_offset); | |
63360adc MS |
705 | |
706 | set_value_component_location (value, val); | |
707 | /* set_value_component_location resets the address, so we may | |
708 | need to set it again. */ | |
709 | if (VALUE_LVAL (value) != lval_internalvar | |
710 | && VALUE_LVAL (value) != lval_internalvar_component | |
711 | && VALUE_LVAL (value) != lval_computed) | |
712 | set_value_address (value, address + embedded_offset); | |
a6bac58e TT |
713 | |
714 | val_obj = value_to_value_object (value); | |
715 | if (! val_obj) | |
716 | goto done; | |
717 | ||
718 | /* Find the constructor. */ | |
719 | printer = find_pretty_printer (val_obj); | |
720 | Py_DECREF (val_obj); | |
721 | make_cleanup_py_decref (printer); | |
722 | if (! printer || printer == Py_None) | |
723 | goto done; | |
724 | ||
725 | /* If we are printing a map, we want some special formatting. */ | |
726 | hint = gdbpy_get_display_hint (printer); | |
727 | make_cleanup (free_current_contents, &hint); | |
728 | ||
729 | /* Print the section */ | |
621c8364 TT |
730 | print_result = print_string_repr (printer, hint, stream, recurse, |
731 | options, language, gdbarch); | |
732 | if (print_result != string_repr_error) | |
733 | print_children (printer, hint, stream, recurse, options, language, | |
734 | print_result == string_repr_none); | |
79f283fe | 735 | |
a6bac58e TT |
736 | result = 1; |
737 | ||
738 | ||
739 | done: | |
740 | if (PyErr_Occurred ()) | |
621c8364 | 741 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
742 | do_cleanups (cleanups); |
743 | return result; | |
744 | } | |
745 | ||
fbb8f299 | 746 | |
b6313243 TT |
747 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
748 | print object. It must have a 'to_string' method (but this is | |
749 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
750 | returns a string. The printer will return a value and in the case |
751 | of a Python string being returned, this function will return a | |
752 | PyObject containing the string. For any other type, *REPLACEMENT is | |
753 | set to the replacement value and this function returns NULL. On | |
754 | error, *REPLACEMENT is set to NULL and this function also returns | |
755 | NULL. */ | |
756 | PyObject * | |
b6313243 | 757 | apply_varobj_pretty_printer (PyObject *printer_obj, |
621c8364 TT |
758 | struct value **replacement, |
759 | struct ui_file *stream) | |
b6313243 | 760 | { |
fbb8f299 | 761 | PyObject *py_str = NULL; |
b6313243 TT |
762 | |
763 | *replacement = NULL; | |
fbb8f299 PM |
764 | py_str = pretty_print_one_value (printer_obj, replacement); |
765 | ||
766 | if (*replacement == NULL && py_str == NULL) | |
621c8364 | 767 | print_stack_unless_memory_error (stream); |
b6313243 | 768 | |
fbb8f299 | 769 | return py_str; |
b6313243 TT |
770 | } |
771 | ||
772 | /* Find a pretty-printer object for the varobj module. Returns a new | |
773 | reference to the object if successful; returns NULL if not. VALUE | |
774 | is the value for which a printer tests to determine if it | |
775 | can pretty-print the value. */ | |
776 | PyObject * | |
777 | gdbpy_get_varobj_pretty_printer (struct value *value) | |
778 | { | |
779 | PyObject *val_obj; | |
780 | PyObject *pretty_printer = NULL; | |
781 | volatile struct gdb_exception except; | |
782 | ||
783 | TRY_CATCH (except, RETURN_MASK_ALL) | |
784 | { | |
785 | value = value_copy (value); | |
786 | } | |
787 | GDB_PY_HANDLE_EXCEPTION (except); | |
788 | ||
789 | val_obj = value_to_value_object (value); | |
790 | if (! val_obj) | |
791 | return NULL; | |
792 | ||
793 | pretty_printer = find_pretty_printer (val_obj); | |
794 | Py_DECREF (val_obj); | |
795 | return pretty_printer; | |
796 | } | |
797 | ||
798 | /* A Python function which wraps find_pretty_printer and instantiates | |
799 | the resulting class. This accepts a Value argument and returns a | |
800 | pretty printer instance, or None. This function is useful as an | |
801 | argument to the MI command -var-set-visualizer. */ | |
802 | PyObject * | |
803 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
804 | { | |
805 | PyObject *val_obj; | |
f92adf3c | 806 | PyObject *cons; |
b6313243 TT |
807 | struct value *value; |
808 | ||
809 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
810 | return NULL; | |
811 | value = value_object_to_value (val_obj); | |
812 | if (! value) | |
813 | { | |
044c0f87 PM |
814 | PyErr_SetString (PyExc_TypeError, |
815 | _("Argument must be a gdb.Value.")); | |
b6313243 TT |
816 | return NULL; |
817 | } | |
818 | ||
819 | cons = find_pretty_printer (val_obj); | |
820 | return cons; | |
821 | } | |
822 | ||
a6bac58e TT |
823 | #else /* HAVE_PYTHON */ |
824 | ||
825 | int | |
826 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
827 | int embedded_offset, CORE_ADDR address, | |
6bf0ce2b | 828 | struct ui_file *stream, int recurse, |
0e03807e | 829 | const struct value *val, |
6bf0ce2b | 830 | const struct value_print_options *options, |
a6bac58e TT |
831 | const struct language_defn *language) |
832 | { | |
833 | return 0; | |
834 | } | |
835 | ||
836 | #endif /* HAVE_PYTHON */ |