1 /* Python pretty-printing
3 Copyright (C) 2008, 2009 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
);
125 /* Pretty-print a single value, via the printer object PRINTER. If
126 the function returns a string, an xmalloc()d copy is returned.
127 Otherwise, if the function returns a value, a *OUT_VALUE is set to
128 the value, and NULL is returned. On error, *OUT_VALUE is set to
129 NULL and NULL is returned. */
131 pretty_print_one_value (PyObject
*printer
, struct value
**out_value
)
134 volatile struct gdb_exception except
;
136 TRY_CATCH (except
, RETURN_MASK_ALL
)
140 result
= PyObject_CallMethodObjArgs (printer
, gdbpy_to_string_cst
, NULL
);
143 if (gdbpy_is_string (result
))
144 output
= python_string_to_host_string (result
);
146 *out_value
= convert_value_from_python (result
);
154 /* Return the display hint for the object printer, PRINTER. Return
155 NULL if there is no display_hint method, or if the method did not
156 return a string. On error, print stack trace and return NULL. On
157 success, return an xmalloc()d string. */
159 gdbpy_get_display_hint (PyObject
*printer
)
164 if (! PyObject_HasAttr (printer
, gdbpy_display_hint_cst
))
167 hint
= PyObject_CallMethodObjArgs (printer
, gdbpy_display_hint_cst
, NULL
);
168 if (gdbpy_is_string (hint
))
169 result
= python_string_to_host_string (hint
);
173 gdbpy_print_stack ();
178 /* Helper for apply_val_pretty_printer which calls to_string and
179 formats the result. */
181 print_string_repr (PyObject
*printer
, const char *hint
,
182 struct ui_file
*stream
, int recurse
,
183 const struct value_print_options
*options
,
184 const struct language_defn
*language
,
185 struct gdbarch
*gdbarch
)
188 struct value
*replacement
= NULL
;
190 output
= pretty_print_one_value (printer
, &replacement
);
193 if (hint
&& !strcmp (hint
, "string"))
194 LA_PRINT_STRING (stream
, builtin_type (gdbarch
)->builtin_char
,
195 (gdb_byte
*) output
, strlen (output
),
198 fputs_filtered (output
, stream
);
201 else if (replacement
)
202 common_val_print (replacement
, stream
, recurse
, options
, language
);
204 gdbpy_print_stack ();
208 py_restore_tstate (void *p
)
210 PyFrameObject
*frame
= p
;
211 PyThreadState
*tstate
= PyThreadState_GET ();
212 tstate
->frame
= frame
;
215 /* Create a dummy PyFrameObject, needed to work around
216 a Python-2.4 bug with generators. */
218 push_dummy_python_frame ()
220 PyObject
*empty_string
, *null_tuple
, *globals
;
222 PyFrameObject
*frame
;
223 PyThreadState
*tstate
;
225 empty_string
= PyString_FromString ("");
229 null_tuple
= PyTuple_New (0);
232 Py_DECREF (empty_string
);
236 code
= PyCode_New (0, /* argcount */
240 empty_string
, /* code */
241 null_tuple
, /* consts */
242 null_tuple
, /* names */
243 null_tuple
, /* varnames */
244 #if PYTHON_API_VERSION >= 1010
245 null_tuple
, /* freevars */
246 null_tuple
, /* cellvars */
248 empty_string
, /* filename */
249 empty_string
, /* name */
251 empty_string
/* lnotab */
254 Py_DECREF (empty_string
);
255 Py_DECREF (null_tuple
);
260 globals
= PyDict_New ();
267 tstate
= PyThreadState_GET ();
269 frame
= PyFrame_New (tstate
, code
, globals
, NULL
);
277 tstate
->frame
= frame
;
278 make_cleanup (py_restore_tstate
, frame
->f_back
);
279 return (PyObject
*) frame
;
282 /* Helper for apply_val_pretty_printer that formats children of the
283 printer, if any exist. */
285 print_children (PyObject
*printer
, const char *hint
,
286 struct ui_file
*stream
, int recurse
,
287 const struct value_print_options
*options
,
288 const struct language_defn
*language
)
290 int is_map
, is_array
, done_flag
, pretty
;
292 PyObject
*children
, *iter
, *frame
;
293 struct cleanup
*cleanups
;
295 if (! PyObject_HasAttr (printer
, gdbpy_children_cst
))
298 /* If we are printing a map or an array, we want some special
300 is_map
= hint
&& ! strcmp (hint
, "map");
301 is_array
= hint
&& ! strcmp (hint
, "array");
303 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
307 gdbpy_print_stack ();
311 cleanups
= make_cleanup_py_decref (children
);
313 iter
= PyObject_GetIter (children
);
316 gdbpy_print_stack ();
319 make_cleanup_py_decref (iter
);
321 /* Use the prettyprint_arrays option if we are printing an array,
322 and the pretty option otherwise. */
323 pretty
= is_array
? options
->prettyprint_arrays
: options
->pretty
;
325 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
326 where it insists on having a non-NULL tstate->frame when
327 a generator is called. */
328 frame
= push_dummy_python_frame ();
331 gdbpy_print_stack ();
334 make_cleanup_py_decref (frame
);
337 for (i
= 0; i
< options
->print_max
; ++i
)
339 PyObject
*py_v
, *item
= PyIter_Next (iter
);
341 struct cleanup
*inner_cleanup
;
345 if (PyErr_Occurred ())
346 gdbpy_print_stack ();
347 /* Set a flag so we can know whether we printed all the
348 available elements. */
354 if (! PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
356 gdbpy_print_stack ();
360 inner_cleanup
= make_cleanup_py_decref (item
);
362 /* Print initial "{". For other elements, there are three
364 1. Maps. Print a "," after each value element.
365 2. Arrays. Always print a ",".
366 3. Other. Always print a ",". */
368 fputs_filtered (" = {", stream
);
369 else if (! is_map
|| i
% 2 == 0)
370 fputs_filtered (pretty
? "," : ", ", stream
);
372 /* In summary mode, we just want to print "= {...}" if there is
374 if (options
->summary
)
376 /* This increment tricks the post-loop logic to print what
384 if (! is_map
|| i
% 2 == 0)
388 fputs_filtered ("\n", stream
);
389 print_spaces_filtered (2 + 2 * recurse
, stream
);
392 wrap_here (n_spaces (2 + 2 *recurse
));
395 if (is_map
&& i
% 2 == 0)
396 fputs_filtered ("[", stream
);
399 /* We print the index, not whatever the child method
400 returned as the name. */
401 if (options
->print_array_indexes
)
402 fprintf_filtered (stream
, "[%d] = ", i
);
406 fputs_filtered (name
, stream
);
407 fputs_filtered (" = ", stream
);
410 if (gdbpy_is_string (py_v
))
412 char *text
= python_string_to_host_string (py_v
);
414 gdbpy_print_stack ();
417 fputs_filtered (text
, stream
);
423 struct value
*value
= convert_value_from_python (py_v
);
427 gdbpy_print_stack ();
428 error (_("Error while executing Python code."));
431 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
434 if (is_map
&& i
% 2 == 0)
435 fputs_filtered ("] = ", stream
);
437 do_cleanups (inner_cleanup
);
446 fputs_filtered ("\n", stream
);
447 print_spaces_filtered (2 + 2 * recurse
, stream
);
449 fputs_filtered ("...", stream
);
453 fputs_filtered ("\n", stream
);
454 print_spaces_filtered (2 * recurse
, stream
);
456 fputs_filtered ("}", stream
);
460 do_cleanups (cleanups
);
464 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
465 int embedded_offset
, CORE_ADDR address
,
466 struct ui_file
*stream
, int recurse
,
467 const struct value_print_options
*options
,
468 const struct language_defn
*language
)
470 struct gdbarch
*gdbarch
= get_type_arch (type
);
471 PyObject
*printer
= NULL
;
472 PyObject
*val_obj
= NULL
;
475 struct cleanup
*cleanups
;
477 PyGILState_STATE state
;
479 state
= PyGILState_Ensure ();
480 cleanups
= make_cleanup_py_restore_gil (&state
);
482 /* Instantiate the printer. */
484 valaddr
+= embedded_offset
;
485 value
= value_from_contents_and_address (type
, valaddr
, address
);
487 val_obj
= value_to_value_object (value
);
491 /* Find the constructor. */
492 printer
= find_pretty_printer (val_obj
);
494 make_cleanup_py_decref (printer
);
495 if (! printer
|| printer
== Py_None
)
498 /* If we are printing a map, we want some special formatting. */
499 hint
= gdbpy_get_display_hint (printer
);
500 make_cleanup (free_current_contents
, &hint
);
502 /* Print the section */
503 print_string_repr (printer
, hint
, stream
, recurse
, options
, language
,
505 print_children (printer
, hint
, stream
, recurse
, options
, language
);
510 if (PyErr_Occurred ())
511 gdbpy_print_stack ();
512 do_cleanups (cleanups
);
516 /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
517 print object. It must have a 'to_string' method (but this is
518 checked by varobj, not here) which takes no arguments and
519 returns a string. This function returns an xmalloc()d string if
520 the printer returns a string. The printer may return a replacement
521 value instead; in this case *REPLACEMENT is set to the replacement
522 value, and this function returns NULL. On error, *REPLACEMENT is
523 set to NULL and this function also returns NULL. */
525 apply_varobj_pretty_printer (PyObject
*printer_obj
,
526 struct value
**replacement
)
529 PyGILState_STATE state
= PyGILState_Ensure ();
532 result
= pretty_print_one_value (printer_obj
, replacement
);
534 gdbpy_print_stack ();
535 PyGILState_Release (state
);
540 /* Find a pretty-printer object for the varobj module. Returns a new
541 reference to the object if successful; returns NULL if not. VALUE
542 is the value for which a printer tests to determine if it
543 can pretty-print the value. */
545 gdbpy_get_varobj_pretty_printer (struct value
*value
)
548 PyObject
*pretty_printer
= NULL
;
549 volatile struct gdb_exception except
;
551 TRY_CATCH (except
, RETURN_MASK_ALL
)
553 value
= value_copy (value
);
555 GDB_PY_HANDLE_EXCEPTION (except
);
557 val_obj
= value_to_value_object (value
);
561 pretty_printer
= find_pretty_printer (val_obj
);
563 return pretty_printer
;
566 /* A Python function which wraps find_pretty_printer and instantiates
567 the resulting class. This accepts a Value argument and returns a
568 pretty printer instance, or None. This function is useful as an
569 argument to the MI command -var-set-visualizer. */
571 gdbpy_default_visualizer (PyObject
*self
, PyObject
*args
)
574 PyObject
*cons
, *printer
= NULL
;
577 if (! PyArg_ParseTuple (args
, "O", &val_obj
))
579 value
= value_object_to_value (val_obj
);
582 PyErr_SetString (PyExc_TypeError
, "argument must be a gdb.Value");
586 cons
= find_pretty_printer (val_obj
);
590 #else /* HAVE_PYTHON */
593 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
594 int embedded_offset
, CORE_ADDR address
,
595 struct ui_file
*stream
, int recurse
,
596 const struct value_print_options
*options
,
597 const struct language_defn
*language
)
602 #endif /* HAVE_PYTHON */
This page took 0.065334 seconds and 4 git commands to generate.