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"
32 /* Helper function for find_pretty_printer which iterates over a list,
33 calls each function and inspects output. This will return a
34 printer object if one recognizes VALUE. If no printer is found, it
35 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 /* Subroutine of find_pretty_printer to simplify it.
64 Look for a pretty-printer to print VALUE in all objfiles.
65 The result is NULL if there's an error and the search should be terminated.
66 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
67 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
70 find_pretty_printer_from_objfiles (PyObject
*value
)
78 PyObject
*objf
= objfile_to_objfile_object (obj
);
81 /* Ignore the error and continue. */
86 pp_list
= objfpy_get_printers (objf
, NULL
);
87 function
= search_pp_list (pp_list
, value
);
90 /* If there is an error in any objfile list, abort the search and exit. */
94 if (function
!= Py_None
)
103 /* Subroutine of find_pretty_printer to simplify it.
104 Look for a pretty-printer to print VALUE in the current program space.
105 The result is NULL if there's an error and the search should be terminated.
106 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
107 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
110 find_pretty_printer_from_progspace (PyObject
*value
)
114 PyObject
*obj
= pspace_to_pspace_object (current_program_space
);
118 pp_list
= pspy_get_printers (obj
, NULL
);
119 function
= search_pp_list (pp_list
, value
);
120 Py_XDECREF (pp_list
);
124 /* Subroutine of find_pretty_printer to simplify it.
125 Look for a pretty-printer to print VALUE in the gdb module.
126 The result is NULL if there's an error and the search should be terminated.
127 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
128 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
131 find_pretty_printer_from_gdb (PyObject
*value
)
136 /* Fetch the global pretty printer dictionary. */
137 if (! PyObject_HasAttrString (gdb_module
, "pretty_printers"))
139 pp_list
= PyObject_GetAttrString (gdb_module
, "pretty_printers");
140 if (pp_list
== NULL
|| ! PyList_Check (pp_list
))
142 Py_XDECREF (pp_list
);
146 function
= search_pp_list (pp_list
, value
);
147 Py_XDECREF (pp_list
);
151 /* Find the pretty-printing constructor function for VALUE. If no
152 pretty-printer exists, return None. If one exists, return a new
153 reference. On error, set the Python error and return NULL. */
156 find_pretty_printer (PyObject
*value
)
160 /* Look at the pretty-printer dictionary for each objfile
161 in the current program-space. */
162 function
= find_pretty_printer_from_objfiles (value
);
163 if (function
== NULL
|| function
!= Py_None
)
165 Py_DECREF (function
);
167 /* Look at the pretty-printer dictionary for the current program-space. */
168 function
= find_pretty_printer_from_progspace (value
);
169 if (function
== NULL
|| function
!= Py_None
)
171 Py_DECREF (function
);
173 /* Look at the pretty-printer dictionary in the gdb module. */
174 function
= find_pretty_printer_from_gdb (value
);
178 /* Pretty-print a single value, via the printer object PRINTER.
179 If the function returns a string, a PyObject containing the string
180 is returned. If the function returns Py_NONE that means the pretty
181 printer returned the Python None as a value. Otherwise, if the
182 function returns a value, *OUT_VALUE is set to the value, and NULL
183 is returned. On error, *OUT_VALUE is set to NULL, and NULL is
187 pretty_print_one_value (PyObject
*printer
, struct value
**out_value
)
189 volatile struct gdb_exception except
;
190 PyObject
*result
= NULL
;
193 TRY_CATCH (except
, RETURN_MASK_ALL
)
195 result
= PyObject_CallMethodObjArgs (printer
, gdbpy_to_string_cst
, NULL
);
198 if (! gdbpy_is_string (result
) && ! gdbpy_is_lazy_string (result
)
199 && result
!= Py_None
)
201 *out_value
= convert_value_from_python (result
);
202 if (PyErr_Occurred ())
213 /* Return the display hint for the object printer, PRINTER. Return
214 NULL if there is no display_hint method, or if the method did not
215 return a string. On error, print stack trace and return NULL. On
216 success, return an xmalloc()d string. */
218 gdbpy_get_display_hint (PyObject
*printer
)
223 if (! PyObject_HasAttr (printer
, gdbpy_display_hint_cst
))
226 hint
= PyObject_CallMethodObjArgs (printer
, gdbpy_display_hint_cst
, NULL
);
227 if (gdbpy_is_string (hint
))
228 result
= python_string_to_host_string (hint
);
232 gdbpy_print_stack ();
237 /* Helper for apply_val_pretty_printer which calls to_string and
238 formats the result. If the value returnd is Py_None, nothing is
239 printed and the function returns a 1; in all other cases data is
240 printed as given by the pretty printer and the function returns 0.
243 print_string_repr (PyObject
*printer
, const char *hint
,
244 struct ui_file
*stream
, int recurse
,
245 const struct value_print_options
*options
,
246 const struct language_defn
*language
,
247 struct gdbarch
*gdbarch
)
249 struct value
*replacement
= NULL
;
250 PyObject
*py_str
= NULL
;
253 py_str
= pretty_print_one_value (printer
, &replacement
);
256 if (py_str
== Py_None
)
260 gdb_byte
*output
= NULL
;
263 char *encoding
= NULL
;
264 PyObject
*string
= NULL
;
267 is_lazy
= gdbpy_is_lazy_string (py_str
);
269 output
= gdbpy_extract_lazy_string (py_str
, &type
, &length
, &encoding
);
272 string
= python_string_to_target_python_string (py_str
);
275 output
= PyString_AsString (string
);
276 length
= PyString_Size (string
);
277 type
= builtin_type (gdbarch
)->builtin_char
;
280 gdbpy_print_stack ();
286 if (is_lazy
|| (hint
&& !strcmp (hint
, "string")))
287 LA_PRINT_STRING (stream
, type
, output
, length
, encoding
,
290 fputs_filtered (output
, stream
);
293 gdbpy_print_stack ();
304 else if (replacement
)
306 struct value_print_options opts
= *options
;
308 opts
.addressprint
= 0;
309 common_val_print (replacement
, stream
, recurse
, &opts
, language
);
312 gdbpy_print_stack ();
318 py_restore_tstate (void *p
)
320 PyFrameObject
*frame
= p
;
321 PyThreadState
*tstate
= PyThreadState_GET ();
322 tstate
->frame
= frame
;
325 /* Create a dummy PyFrameObject, needed to work around
326 a Python-2.4 bug with generators. */
328 push_dummy_python_frame ()
330 PyObject
*empty_string
, *null_tuple
, *globals
;
332 PyFrameObject
*frame
;
333 PyThreadState
*tstate
;
335 empty_string
= PyString_FromString ("");
339 null_tuple
= PyTuple_New (0);
342 Py_DECREF (empty_string
);
346 code
= PyCode_New (0, /* argcount */
350 empty_string
, /* code */
351 null_tuple
, /* consts */
352 null_tuple
, /* names */
353 null_tuple
, /* varnames */
354 #if PYTHON_API_VERSION >= 1010
355 null_tuple
, /* freevars */
356 null_tuple
, /* cellvars */
358 empty_string
, /* filename */
359 empty_string
, /* name */
361 empty_string
/* lnotab */
364 Py_DECREF (empty_string
);
365 Py_DECREF (null_tuple
);
370 globals
= PyDict_New ();
377 tstate
= PyThreadState_GET ();
379 frame
= PyFrame_New (tstate
, code
, globals
, NULL
);
387 tstate
->frame
= frame
;
388 make_cleanup (py_restore_tstate
, frame
->f_back
);
389 return (PyObject
*) frame
;
392 /* Helper for apply_val_pretty_printer that formats children of the
393 printer, if any exist. If is_py_none is true, then nothing has
394 been printed by to_string, and format output accordingly. */
396 print_children (PyObject
*printer
, const char *hint
,
397 struct ui_file
*stream
, int recurse
,
398 const struct value_print_options
*options
,
399 const struct language_defn
*language
,
402 int is_map
, is_array
, done_flag
, pretty
;
404 PyObject
*children
, *iter
, *frame
;
405 struct cleanup
*cleanups
;
407 if (! PyObject_HasAttr (printer
, gdbpy_children_cst
))
410 /* If we are printing a map or an array, we want some special
412 is_map
= hint
&& ! strcmp (hint
, "map");
413 is_array
= hint
&& ! strcmp (hint
, "array");
415 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
419 gdbpy_print_stack ();
423 cleanups
= make_cleanup_py_decref (children
);
425 iter
= PyObject_GetIter (children
);
428 gdbpy_print_stack ();
431 make_cleanup_py_decref (iter
);
433 /* Use the prettyprint_arrays option if we are printing an array,
434 and the pretty option otherwise. */
435 pretty
= is_array
? options
->prettyprint_arrays
: options
->pretty
;
437 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
438 where it insists on having a non-NULL tstate->frame when
439 a generator is called. */
440 frame
= push_dummy_python_frame ();
443 gdbpy_print_stack ();
446 make_cleanup_py_decref (frame
);
449 for (i
= 0; i
< options
->print_max
; ++i
)
451 PyObject
*py_v
, *item
= PyIter_Next (iter
);
453 struct cleanup
*inner_cleanup
;
457 if (PyErr_Occurred ())
458 gdbpy_print_stack ();
459 /* Set a flag so we can know whether we printed all the
460 available elements. */
466 if (! PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
468 gdbpy_print_stack ();
472 inner_cleanup
= make_cleanup_py_decref (item
);
474 /* Print initial "{". For other elements, there are three
476 1. Maps. Print a "," after each value element.
477 2. Arrays. Always print a ",".
478 3. Other. Always print a ",". */
482 fputs_filtered ("{", stream
);
484 fputs_filtered (" = {", stream
);
487 else if (! is_map
|| i
% 2 == 0)
488 fputs_filtered (pretty
? "," : ", ", stream
);
490 /* In summary mode, we just want to print "= {...}" if there is
492 if (options
->summary
)
494 /* This increment tricks the post-loop logic to print what
502 if (! is_map
|| i
% 2 == 0)
506 fputs_filtered ("\n", stream
);
507 print_spaces_filtered (2 + 2 * recurse
, stream
);
510 wrap_here (n_spaces (2 + 2 *recurse
));
513 if (is_map
&& i
% 2 == 0)
514 fputs_filtered ("[", stream
);
517 /* We print the index, not whatever the child method
518 returned as the name. */
519 if (options
->print_array_indexes
)
520 fprintf_filtered (stream
, "[%d] = ", i
);
524 fputs_filtered (name
, stream
);
525 fputs_filtered (" = ", stream
);
528 if (gdbpy_is_lazy_string (py_v
) || gdbpy_is_string (py_v
))
530 gdb_byte
*output
= NULL
;
532 if (gdbpy_is_lazy_string (py_v
))
536 char *encoding
= NULL
;
538 output
= gdbpy_extract_lazy_string (py_v
, &type
,
541 gdbpy_print_stack ();
542 LA_PRINT_STRING (stream
, type
, output
, length
, encoding
,
549 output
= python_string_to_host_string (py_v
);
550 fputs_filtered (output
, stream
);
556 struct value
*value
= convert_value_from_python (py_v
);
560 gdbpy_print_stack ();
561 error (_("Error while executing Python code."));
564 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
567 if (is_map
&& i
% 2 == 0)
568 fputs_filtered ("] = ", stream
);
570 do_cleanups (inner_cleanup
);
579 fputs_filtered ("\n", stream
);
580 print_spaces_filtered (2 + 2 * recurse
, stream
);
582 fputs_filtered ("...", stream
);
586 fputs_filtered ("\n", stream
);
587 print_spaces_filtered (2 * recurse
, stream
);
589 fputs_filtered ("}", stream
);
593 do_cleanups (cleanups
);
597 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
598 int embedded_offset
, CORE_ADDR address
,
599 struct ui_file
*stream
, int recurse
,
600 const struct value_print_options
*options
,
601 const struct language_defn
*language
)
603 struct gdbarch
*gdbarch
= get_type_arch (type
);
604 PyObject
*printer
= NULL
;
605 PyObject
*val_obj
= NULL
;
608 struct cleanup
*cleanups
;
611 cleanups
= ensure_python_env (gdbarch
, language
);
613 /* Instantiate the printer. */
615 valaddr
+= embedded_offset
;
616 value
= value_from_contents_and_address (type
, valaddr
,
617 address
+ embedded_offset
);
619 val_obj
= value_to_value_object (value
);
623 /* Find the constructor. */
624 printer
= find_pretty_printer (val_obj
);
626 make_cleanup_py_decref (printer
);
627 if (! printer
|| printer
== Py_None
)
630 /* If we are printing a map, we want some special formatting. */
631 hint
= gdbpy_get_display_hint (printer
);
632 make_cleanup (free_current_contents
, &hint
);
634 /* Print the section */
635 is_py_none
= print_string_repr (printer
, hint
, stream
, recurse
,
636 options
, language
, gdbarch
);
637 print_children (printer
, hint
, stream
, recurse
, options
, language
,
644 if (PyErr_Occurred ())
645 gdbpy_print_stack ();
646 do_cleanups (cleanups
);
651 /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
652 print object. It must have a 'to_string' method (but this is
653 checked by varobj, not here) which takes no arguments and
654 returns a string. The printer will return a value and in the case
655 of a Python string being returned, this function will return a
656 PyObject containing the string. For any other type, *REPLACEMENT is
657 set to the replacement value and this function returns NULL. On
658 error, *REPLACEMENT is set to NULL and this function also returns
661 apply_varobj_pretty_printer (PyObject
*printer_obj
,
662 struct value
**replacement
)
665 PyObject
*py_str
= NULL
;
668 py_str
= pretty_print_one_value (printer_obj
, replacement
);
670 if (*replacement
== NULL
&& py_str
== NULL
)
671 gdbpy_print_stack ();
676 /* Find a pretty-printer object for the varobj module. Returns a new
677 reference to the object if successful; returns NULL if not. VALUE
678 is the value for which a printer tests to determine if it
679 can pretty-print the value. */
681 gdbpy_get_varobj_pretty_printer (struct value
*value
)
684 PyObject
*pretty_printer
= NULL
;
685 volatile struct gdb_exception except
;
687 TRY_CATCH (except
, RETURN_MASK_ALL
)
689 value
= value_copy (value
);
691 GDB_PY_HANDLE_EXCEPTION (except
);
693 val_obj
= value_to_value_object (value
);
697 pretty_printer
= find_pretty_printer (val_obj
);
699 return pretty_printer
;
702 /* A Python function which wraps find_pretty_printer and instantiates
703 the resulting class. This accepts a Value argument and returns a
704 pretty printer instance, or None. This function is useful as an
705 argument to the MI command -var-set-visualizer. */
707 gdbpy_default_visualizer (PyObject
*self
, PyObject
*args
)
710 PyObject
*cons
, *printer
= NULL
;
713 if (! PyArg_ParseTuple (args
, "O", &val_obj
))
715 value
= value_object_to_value (val_obj
);
718 PyErr_SetString (PyExc_TypeError
,
719 _("Argument must be a gdb.Value."));
723 cons
= find_pretty_printer (val_obj
);
727 #else /* HAVE_PYTHON */
730 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
731 int embedded_offset
, CORE_ADDR address
,
732 struct ui_file
*stream
, int recurse
,
733 const struct value_print_options
*options
,
734 const struct language_defn
*language
)
739 #endif /* HAVE_PYTHON */
This page took 0.045907 seconds and 5 git commands to generate.