1 /* Python pretty-printing
3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "exceptions.h"
30 #include "python-internal.h"
33 /* Helper function for find_pretty_printer which iterates over a list,
34 calls each function and inspects output. This will return a
35 printer object if one recognizes VALUE. If no printer is found, it
36 will return None. On error, it will set the Python error and
39 search_pp_list (PyObject
*list
, PyObject
*value
)
41 Py_ssize_t pp_list_size
, list_index
;
42 PyObject
*function
, *printer
= NULL
;
44 pp_list_size
= PyList_Size (list
);
45 for (list_index
= 0; list_index
< pp_list_size
; list_index
++)
47 function
= PyList_GetItem (list
, list_index
);
51 printer
= PyObject_CallFunctionObjArgs (function
, value
, NULL
);
54 else if (printer
!= Py_None
)
63 /* Find the pretty-printing constructor function for VALUE. If no
64 pretty-printer exists, return None. If one exists, return a new
65 reference. On error, set the Python error and return NULL. */
67 find_pretty_printer (PyObject
*value
)
69 PyObject
*pp_list
= NULL
;
70 PyObject
*function
= NULL
;
72 volatile struct gdb_exception except
;
74 /* Look at the pretty-printer dictionary for each objfile. */
77 PyObject
*objf
= objfile_to_objfile_object (obj
);
80 /* Ignore the error and continue. */
85 pp_list
= objfpy_get_printers (objf
, NULL
);
86 function
= search_pp_list (pp_list
, value
);
88 /* If there is an error in any objfile list, abort the search and
96 if (function
!= Py_None
)
100 Py_XDECREF (pp_list
);
104 /* Fetch the global pretty printer dictionary. */
105 if (! PyObject_HasAttrString (gdb_module
, "pretty_printers"))
108 Py_INCREF (function
);
111 pp_list
= PyObject_GetAttrString (gdb_module
, "pretty_printers");
114 if (! PyList_Check (pp_list
))
117 function
= search_pp_list (pp_list
, value
);
120 Py_XDECREF (pp_list
);
126 /* Pretty-print a single value, via the printer object PRINTER.
127 If the function returns a string, a PyObject containing the string
128 is returned. Otherwise, if the function returns a value,
129 *OUT_VALUE is set to the value, and NULL is returned. On error,
130 *OUT_VALUE is set to NULL, and NULL is returned. */
132 pretty_print_one_value (PyObject
*printer
, struct value
**out_value
)
134 volatile struct gdb_exception except
;
135 PyObject
*result
= NULL
;
138 TRY_CATCH (except
, RETURN_MASK_ALL
)
140 result
= PyObject_CallMethodObjArgs (printer
, gdbpy_to_string_cst
, NULL
);
143 if (! gdbpy_is_string (result
) && ! gdbpy_is_lazy_string (result
))
145 *out_value
= convert_value_from_python (result
);
146 if (PyErr_Occurred ())
157 /* Return the display hint for the object printer, PRINTER. Return
158 NULL if there is no display_hint method, or if the method did not
159 return a string. On error, print stack trace and return NULL. On
160 success, return an xmalloc()d string. */
162 gdbpy_get_display_hint (PyObject
*printer
)
167 if (! PyObject_HasAttr (printer
, gdbpy_display_hint_cst
))
170 hint
= PyObject_CallMethodObjArgs (printer
, gdbpy_display_hint_cst
, NULL
);
171 if (gdbpy_is_string (hint
))
172 result
= python_string_to_host_string (hint
);
176 gdbpy_print_stack ();
181 /* Helper for apply_val_pretty_printer which calls to_string and
182 formats the result. */
184 print_string_repr (PyObject
*printer
, const char *hint
,
185 struct ui_file
*stream
, int recurse
,
186 const struct value_print_options
*options
,
187 const struct language_defn
*language
,
188 struct gdbarch
*gdbarch
)
190 struct value
*replacement
= NULL
;
191 PyObject
*py_str
= NULL
;
193 py_str
= pretty_print_one_value (printer
, &replacement
);
196 gdb_byte
*output
= NULL
;
199 char *encoding
= NULL
;
200 PyObject
*string
= NULL
;
203 is_lazy
= gdbpy_is_lazy_string (py_str
);
205 output
= gdbpy_extract_lazy_string (py_str
, &type
, &length
, &encoding
);
208 string
= python_string_to_target_python_string (py_str
);
211 output
= PyString_AsString (string
);
212 length
= PyString_Size (string
);
213 type
= builtin_type (gdbarch
)->builtin_char
;
216 gdbpy_print_stack ();
222 if (is_lazy
|| (hint
&& !strcmp (hint
, "string")))
223 LA_PRINT_STRING (stream
, type
, output
, length
, encoding
,
226 fputs_filtered (output
, stream
);
229 gdbpy_print_stack ();
239 else if (replacement
)
241 struct value_print_options opts
= *options
;
243 opts
.addressprint
= 0;
244 common_val_print (replacement
, stream
, recurse
, &opts
, language
);
247 gdbpy_print_stack ();
251 py_restore_tstate (void *p
)
253 PyFrameObject
*frame
= p
;
254 PyThreadState
*tstate
= PyThreadState_GET ();
255 tstate
->frame
= frame
;
258 /* Create a dummy PyFrameObject, needed to work around
259 a Python-2.4 bug with generators. */
261 push_dummy_python_frame ()
263 PyObject
*empty_string
, *null_tuple
, *globals
;
265 PyFrameObject
*frame
;
266 PyThreadState
*tstate
;
268 empty_string
= PyString_FromString ("");
272 null_tuple
= PyTuple_New (0);
275 Py_DECREF (empty_string
);
279 code
= PyCode_New (0, /* argcount */
283 empty_string
, /* code */
284 null_tuple
, /* consts */
285 null_tuple
, /* names */
286 null_tuple
, /* varnames */
287 #if PYTHON_API_VERSION >= 1010
288 null_tuple
, /* freevars */
289 null_tuple
, /* cellvars */
291 empty_string
, /* filename */
292 empty_string
, /* name */
294 empty_string
/* lnotab */
297 Py_DECREF (empty_string
);
298 Py_DECREF (null_tuple
);
303 globals
= PyDict_New ();
310 tstate
= PyThreadState_GET ();
312 frame
= PyFrame_New (tstate
, code
, globals
, NULL
);
320 tstate
->frame
= frame
;
321 make_cleanup (py_restore_tstate
, frame
->f_back
);
322 return (PyObject
*) frame
;
325 /* Helper for apply_val_pretty_printer that formats children of the
326 printer, if any exist. */
328 print_children (PyObject
*printer
, const char *hint
,
329 struct ui_file
*stream
, int recurse
,
330 const struct value_print_options
*options
,
331 const struct language_defn
*language
)
333 int is_map
, is_array
, done_flag
, pretty
;
335 PyObject
*children
, *iter
, *frame
;
336 struct cleanup
*cleanups
;
338 if (! PyObject_HasAttr (printer
, gdbpy_children_cst
))
341 /* If we are printing a map or an array, we want some special
343 is_map
= hint
&& ! strcmp (hint
, "map");
344 is_array
= hint
&& ! strcmp (hint
, "array");
346 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
350 gdbpy_print_stack ();
354 cleanups
= make_cleanup_py_decref (children
);
356 iter
= PyObject_GetIter (children
);
359 gdbpy_print_stack ();
362 make_cleanup_py_decref (iter
);
364 /* Use the prettyprint_arrays option if we are printing an array,
365 and the pretty option otherwise. */
366 pretty
= is_array
? options
->prettyprint_arrays
: options
->pretty
;
368 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
369 where it insists on having a non-NULL tstate->frame when
370 a generator is called. */
371 frame
= push_dummy_python_frame ();
374 gdbpy_print_stack ();
377 make_cleanup_py_decref (frame
);
380 for (i
= 0; i
< options
->print_max
; ++i
)
382 PyObject
*py_v
, *item
= PyIter_Next (iter
);
384 struct cleanup
*inner_cleanup
;
388 if (PyErr_Occurred ())
389 gdbpy_print_stack ();
390 /* Set a flag so we can know whether we printed all the
391 available elements. */
397 if (! PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
399 gdbpy_print_stack ();
403 inner_cleanup
= make_cleanup_py_decref (item
);
405 /* Print initial "{". For other elements, there are three
407 1. Maps. Print a "," after each value element.
408 2. Arrays. Always print a ",".
409 3. Other. Always print a ",". */
411 fputs_filtered (" = {", stream
);
412 else if (! is_map
|| i
% 2 == 0)
413 fputs_filtered (pretty
? "," : ", ", stream
);
415 /* In summary mode, we just want to print "= {...}" if there is
417 if (options
->summary
)
419 /* This increment tricks the post-loop logic to print what
427 if (! is_map
|| i
% 2 == 0)
431 fputs_filtered ("\n", stream
);
432 print_spaces_filtered (2 + 2 * recurse
, stream
);
435 wrap_here (n_spaces (2 + 2 *recurse
));
438 if (is_map
&& i
% 2 == 0)
439 fputs_filtered ("[", stream
);
442 /* We print the index, not whatever the child method
443 returned as the name. */
444 if (options
->print_array_indexes
)
445 fprintf_filtered (stream
, "[%d] = ", i
);
449 fputs_filtered (name
, stream
);
450 fputs_filtered (" = ", stream
);
453 if (gdbpy_is_lazy_string (py_v
) || gdbpy_is_string (py_v
))
455 gdb_byte
*output
= NULL
;
457 if (gdbpy_is_lazy_string (py_v
))
461 char *encoding
= NULL
;
463 output
= gdbpy_extract_lazy_string (py_v
, &type
,
466 gdbpy_print_stack ();
467 LA_PRINT_STRING (stream
, type
, output
, length
, encoding
,
474 output
= python_string_to_host_string (py_v
);
475 fputs_filtered (output
, stream
);
481 struct value
*value
= convert_value_from_python (py_v
);
485 gdbpy_print_stack ();
486 error (_("Error while executing Python code."));
489 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
492 if (is_map
&& i
% 2 == 0)
493 fputs_filtered ("] = ", stream
);
495 do_cleanups (inner_cleanup
);
504 fputs_filtered ("\n", stream
);
505 print_spaces_filtered (2 + 2 * recurse
, stream
);
507 fputs_filtered ("...", stream
);
511 fputs_filtered ("\n", stream
);
512 print_spaces_filtered (2 * recurse
, stream
);
514 fputs_filtered ("}", stream
);
518 do_cleanups (cleanups
);
522 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
523 int embedded_offset
, CORE_ADDR address
,
524 struct ui_file
*stream
, int recurse
,
525 const struct value_print_options
*options
,
526 const struct language_defn
*language
)
528 struct gdbarch
*gdbarch
= get_type_arch (type
);
529 PyObject
*printer
= NULL
;
530 PyObject
*val_obj
= NULL
;
533 struct cleanup
*cleanups
;
536 cleanups
= ensure_python_env (gdbarch
, language
);
538 /* Instantiate the printer. */
540 valaddr
+= embedded_offset
;
541 value
= value_from_contents_and_address (type
, valaddr
, address
);
543 val_obj
= value_to_value_object (value
);
547 /* Find the constructor. */
548 printer
= find_pretty_printer (val_obj
);
550 make_cleanup_py_decref (printer
);
551 if (! printer
|| printer
== Py_None
)
554 /* If we are printing a map, we want some special formatting. */
555 hint
= gdbpy_get_display_hint (printer
);
556 make_cleanup (free_current_contents
, &hint
);
558 /* Print the section */
559 print_string_repr (printer
, hint
, stream
, recurse
, options
, language
,
561 print_children (printer
, hint
, stream
, recurse
, options
, language
);
566 if (PyErr_Occurred ())
567 gdbpy_print_stack ();
568 do_cleanups (cleanups
);
573 /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
574 print object. It must have a 'to_string' method (but this is
575 checked by varobj, not here) which takes no arguments and
576 returns a string. The printer will return a value and in the case
577 of a Python string being returned, this function will return a
578 PyObject containing the string. For any other type, *REPLACEMENT is
579 set to the replacement value and this function returns NULL. On
580 error, *REPLACEMENT is set to NULL and this function also returns
583 apply_varobj_pretty_printer (PyObject
*printer_obj
,
584 struct value
**replacement
)
587 PyObject
*py_str
= NULL
;
590 py_str
= pretty_print_one_value (printer_obj
, replacement
);
592 if (*replacement
== NULL
&& py_str
== NULL
)
593 gdbpy_print_stack ();
598 /* Find a pretty-printer object for the varobj module. Returns a new
599 reference to the object if successful; returns NULL if not. VALUE
600 is the value for which a printer tests to determine if it
601 can pretty-print the value. */
603 gdbpy_get_varobj_pretty_printer (struct value
*value
)
606 PyObject
*pretty_printer
= NULL
;
607 volatile struct gdb_exception except
;
609 TRY_CATCH (except
, RETURN_MASK_ALL
)
611 value
= value_copy (value
);
613 GDB_PY_HANDLE_EXCEPTION (except
);
615 val_obj
= value_to_value_object (value
);
619 pretty_printer
= find_pretty_printer (val_obj
);
621 return pretty_printer
;
624 /* A Python function which wraps find_pretty_printer and instantiates
625 the resulting class. This accepts a Value argument and returns a
626 pretty printer instance, or None. This function is useful as an
627 argument to the MI command -var-set-visualizer. */
629 gdbpy_default_visualizer (PyObject
*self
, PyObject
*args
)
632 PyObject
*cons
, *printer
= NULL
;
635 if (! PyArg_ParseTuple (args
, "O", &val_obj
))
637 value
= value_object_to_value (val_obj
);
640 PyErr_SetString (PyExc_TypeError
, "argument must be a gdb.Value");
644 cons
= find_pretty_printer (val_obj
);
648 #else /* HAVE_PYTHON */
651 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
652 int embedded_offset
, CORE_ADDR address
,
653 struct ui_file
*stream
, int recurse
,
654 const struct value_print_options
*options
,
655 const struct language_defn
*language
)
660 #endif /* HAVE_PYTHON */