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
)
187 struct value
*replacement
= NULL
;
189 output
= pretty_print_one_value (printer
, &replacement
);
192 if (hint
&& !strcmp (hint
, "string"))
193 LA_PRINT_STRING (stream
, builtin_type (current_gdbarch
)->builtin_char
,
194 (gdb_byte
*) output
, strlen (output
),
197 fputs_filtered (output
, stream
);
200 else if (replacement
)
201 common_val_print (replacement
, stream
, recurse
, options
, language
);
203 gdbpy_print_stack ();
207 py_restore_tstate (void *p
)
209 PyFrameObject
*frame
= p
;
210 PyThreadState
*tstate
= PyThreadState_GET ();
211 tstate
->frame
= frame
;
214 /* Create a dummy PyFrameObject, needed to work around
215 a Python-2.4 bug with generators. */
217 push_dummy_python_frame ()
219 PyObject
*empty_string
, *null_tuple
, *globals
;
221 PyFrameObject
*frame
;
222 PyThreadState
*tstate
;
224 empty_string
= PyString_FromString ("");
228 null_tuple
= PyTuple_New (0);
231 Py_DECREF (empty_string
);
235 code
= PyCode_New (0, /* argcount */
239 empty_string
, /* code */
240 null_tuple
, /* consts */
241 null_tuple
, /* names */
242 null_tuple
, /* varnames */
243 #if PYTHON_API_VERSION >= 1010
244 null_tuple
, /* freevars */
245 null_tuple
, /* cellvars */
247 empty_string
, /* filename */
248 empty_string
, /* name */
250 empty_string
/* lnotab */
253 Py_DECREF (empty_string
);
254 Py_DECREF (null_tuple
);
259 globals
= PyDict_New ();
266 tstate
= PyThreadState_GET ();
268 frame
= PyFrame_New (tstate
, code
, globals
, NULL
);
276 tstate
->frame
= frame
;
277 make_cleanup (py_restore_tstate
, frame
->f_back
);
278 return (PyObject
*) frame
;
281 /* Helper for apply_val_pretty_printer that formats children of the
282 printer, if any exist. */
284 print_children (PyObject
*printer
, const char *hint
,
285 struct ui_file
*stream
, int recurse
,
286 const struct value_print_options
*options
,
287 const struct language_defn
*language
)
289 int is_map
, is_array
, done_flag
, pretty
;
291 PyObject
*children
, *iter
, *frame
;
292 struct cleanup
*cleanups
;
294 if (! PyObject_HasAttr (printer
, gdbpy_children_cst
))
297 /* If we are printing a map or an array, we want some special
299 is_map
= hint
&& ! strcmp (hint
, "map");
300 is_array
= hint
&& ! strcmp (hint
, "array");
302 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
306 gdbpy_print_stack ();
310 cleanups
= make_cleanup_py_decref (children
);
312 iter
= PyObject_GetIter (children
);
315 gdbpy_print_stack ();
318 make_cleanup_py_decref (iter
);
320 /* Use the prettyprint_arrays option if we are printing an array,
321 and the pretty option otherwise. */
322 pretty
= is_array
? options
->prettyprint_arrays
: options
->pretty
;
324 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
325 where it insists on having a non-NULL tstate->frame when
326 a generator is called. */
327 frame
= push_dummy_python_frame ();
330 gdbpy_print_stack ();
333 make_cleanup_py_decref (frame
);
336 for (i
= 0; i
< options
->print_max
; ++i
)
338 PyObject
*py_v
, *item
= PyIter_Next (iter
);
340 struct cleanup
*inner_cleanup
;
344 if (PyErr_Occurred ())
345 gdbpy_print_stack ();
346 /* Set a flag so we can know whether we printed all the
347 available elements. */
353 if (! PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
355 gdbpy_print_stack ();
359 inner_cleanup
= make_cleanup_py_decref (item
);
361 /* Print initial "{". For other elements, there are three
363 1. Maps. Print a "," after each value element.
364 2. Arrays. Always print a ",".
365 3. Other. Always print a ",". */
367 fputs_filtered (" = {", stream
);
368 else if (! is_map
|| i
% 2 == 0)
369 fputs_filtered (pretty
? "," : ", ", stream
);
371 /* In summary mode, we just want to print "= {...}" if there is
373 if (options
->summary
)
375 /* This increment tricks the post-loop logic to print what
383 if (! is_map
|| i
% 2 == 0)
387 fputs_filtered ("\n", stream
);
388 print_spaces_filtered (2 + 2 * recurse
, stream
);
391 wrap_here (n_spaces (2 + 2 *recurse
));
394 if (is_map
&& i
% 2 == 0)
395 fputs_filtered ("[", stream
);
398 /* We print the index, not whatever the child method
399 returned as the name. */
400 if (options
->print_array_indexes
)
401 fprintf_filtered (stream
, "[%d] = ", i
);
405 fputs_filtered (name
, stream
);
406 fputs_filtered (" = ", stream
);
409 if (gdbpy_is_string (py_v
))
411 char *text
= python_string_to_host_string (py_v
);
413 gdbpy_print_stack ();
416 fputs_filtered (text
, stream
);
422 struct value
*value
= convert_value_from_python (py_v
);
426 gdbpy_print_stack ();
427 error (_("Error while executing Python code."));
430 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
433 if (is_map
&& i
% 2 == 0)
434 fputs_filtered ("] = ", stream
);
436 do_cleanups (inner_cleanup
);
445 fputs_filtered ("\n", stream
);
446 print_spaces_filtered (2 + 2 * recurse
, stream
);
448 fputs_filtered ("...", stream
);
452 fputs_filtered ("\n", stream
);
453 print_spaces_filtered (2 * recurse
, stream
);
455 fputs_filtered ("}", stream
);
459 do_cleanups (cleanups
);
463 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
464 int embedded_offset
, CORE_ADDR address
,
465 struct ui_file
*stream
, int recurse
,
466 const struct value_print_options
*options
,
467 const struct language_defn
*language
)
469 PyObject
*printer
= NULL
;
470 PyObject
*val_obj
= NULL
;
473 struct cleanup
*cleanups
;
475 PyGILState_STATE state
;
477 state
= PyGILState_Ensure ();
478 cleanups
= make_cleanup_py_restore_gil (&state
);
480 /* Instantiate the printer. */
482 valaddr
+= embedded_offset
;
483 value
= value_from_contents_and_address (type
, valaddr
, address
);
485 val_obj
= value_to_value_object (value
);
489 /* Find the constructor. */
490 printer
= find_pretty_printer (val_obj
);
492 make_cleanup_py_decref (printer
);
493 if (! printer
|| printer
== Py_None
)
496 /* If we are printing a map, we want some special formatting. */
497 hint
= gdbpy_get_display_hint (printer
);
498 make_cleanup (free_current_contents
, &hint
);
500 /* Print the section */
501 print_string_repr (printer
, hint
, stream
, recurse
, options
, language
);
502 print_children (printer
, hint
, stream
, recurse
, options
, language
);
507 if (PyErr_Occurred ())
508 gdbpy_print_stack ();
509 do_cleanups (cleanups
);
513 /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
514 print object. It must have a 'to_string' method (but this is
515 checked by varobj, not here) which takes no arguments and
516 returns a string. This function returns an xmalloc()d string if
517 the printer returns a string. The printer may return a replacement
518 value instead; in this case *REPLACEMENT is set to the replacement
519 value, and this function returns NULL. On error, *REPLACEMENT is
520 set to NULL and this function also returns NULL. */
522 apply_varobj_pretty_printer (PyObject
*printer_obj
,
523 struct value
**replacement
)
526 PyGILState_STATE state
= PyGILState_Ensure ();
529 result
= pretty_print_one_value (printer_obj
, replacement
);
531 gdbpy_print_stack ();
532 PyGILState_Release (state
);
537 /* Find a pretty-printer object for the varobj module. Returns a new
538 reference to the object if successful; returns NULL if not. VALUE
539 is the value for which a printer tests to determine if it
540 can pretty-print the value. */
542 gdbpy_get_varobj_pretty_printer (struct value
*value
)
545 PyObject
*pretty_printer
= NULL
;
546 volatile struct gdb_exception except
;
548 TRY_CATCH (except
, RETURN_MASK_ALL
)
550 value
= value_copy (value
);
552 GDB_PY_HANDLE_EXCEPTION (except
);
554 val_obj
= value_to_value_object (value
);
558 pretty_printer
= find_pretty_printer (val_obj
);
560 return pretty_printer
;
563 /* A Python function which wraps find_pretty_printer and instantiates
564 the resulting class. This accepts a Value argument and returns a
565 pretty printer instance, or None. This function is useful as an
566 argument to the MI command -var-set-visualizer. */
568 gdbpy_default_visualizer (PyObject
*self
, PyObject
*args
)
571 PyObject
*cons
, *printer
= NULL
;
574 if (! PyArg_ParseTuple (args
, "O", &val_obj
))
576 value
= value_object_to_value (val_obj
);
579 PyErr_SetString (PyExc_TypeError
, "argument must be a gdb.Value");
583 cons
= find_pretty_printer (val_obj
);
587 #else /* HAVE_PYTHON */
590 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
591 int embedded_offset
, CORE_ADDR address
,
592 struct ui_file
*stream
, int recurse
,
593 const struct value_print_options
*options
,
594 const struct language_defn
*language
)
599 #endif /* HAVE_PYTHON */
This page took 0.043551 seconds and 4 git commands to generate.