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
);
124 /* Pretty-print a single value, via the printer object PRINTER.
125 If the function returns a string, a PyObject containing the string
126 is returned. Otherwise, if the function returns a value,
127 *OUT_VALUE is set to the value, and NULL is returned. On error,
128 *OUT_VALUE is set to NULL, and NULL is returned. */
130 pretty_print_one_value (PyObject
*printer
, struct value
**out_value
)
132 volatile struct gdb_exception except
;
133 PyObject
*result
= NULL
;
136 TRY_CATCH (except
, RETURN_MASK_ALL
)
138 result
= PyObject_CallMethodObjArgs (printer
, gdbpy_to_string_cst
, NULL
);
141 if (! gdbpy_is_string (result
))
143 *out_value
= convert_value_from_python (result
);
144 if (PyErr_Occurred ())
155 /* Return the display hint for the object printer, PRINTER. Return
156 NULL if there is no display_hint method, or if the method did not
157 return a string. On error, print stack trace and return NULL. On
158 success, return an xmalloc()d string. */
160 gdbpy_get_display_hint (PyObject
*printer
)
165 if (! PyObject_HasAttr (printer
, gdbpy_display_hint_cst
))
168 hint
= PyObject_CallMethodObjArgs (printer
, gdbpy_display_hint_cst
, NULL
);
169 if (gdbpy_is_string (hint
))
170 result
= python_string_to_host_string (hint
);
174 gdbpy_print_stack ();
179 /* Helper for apply_val_pretty_printer which calls to_string and
180 formats the result. */
182 print_string_repr (PyObject
*printer
, const char *hint
,
183 struct ui_file
*stream
, int recurse
,
184 const struct value_print_options
*options
,
185 const struct language_defn
*language
,
186 struct gdbarch
*gdbarch
)
188 struct value
*replacement
= NULL
;
189 PyObject
*py_str
= NULL
;
191 py_str
= pretty_print_one_value (printer
, &replacement
);
194 PyObject
*string
= python_string_to_target_python_string (py_str
);
197 gdb_byte
*output
= PyString_AsString (string
);
198 int len
= PyString_Size (string
);
200 if (hint
&& !strcmp (hint
, "string"))
201 LA_PRINT_STRING (stream
, builtin_type (gdbarch
)->builtin_char
,
202 output
, len
, 0, options
);
204 fputs_filtered (output
, stream
);
208 gdbpy_print_stack ();
211 else if (replacement
)
212 common_val_print (replacement
, stream
, recurse
, options
, language
);
214 gdbpy_print_stack ();
218 py_restore_tstate (void *p
)
220 PyFrameObject
*frame
= p
;
221 PyThreadState
*tstate
= PyThreadState_GET ();
222 tstate
->frame
= frame
;
225 /* Create a dummy PyFrameObject, needed to work around
226 a Python-2.4 bug with generators. */
228 push_dummy_python_frame ()
230 PyObject
*empty_string
, *null_tuple
, *globals
;
232 PyFrameObject
*frame
;
233 PyThreadState
*tstate
;
235 empty_string
= PyString_FromString ("");
239 null_tuple
= PyTuple_New (0);
242 Py_DECREF (empty_string
);
246 code
= PyCode_New (0, /* argcount */
250 empty_string
, /* code */
251 null_tuple
, /* consts */
252 null_tuple
, /* names */
253 null_tuple
, /* varnames */
254 #if PYTHON_API_VERSION >= 1010
255 null_tuple
, /* freevars */
256 null_tuple
, /* cellvars */
258 empty_string
, /* filename */
259 empty_string
, /* name */
261 empty_string
/* lnotab */
264 Py_DECREF (empty_string
);
265 Py_DECREF (null_tuple
);
270 globals
= PyDict_New ();
277 tstate
= PyThreadState_GET ();
279 frame
= PyFrame_New (tstate
, code
, globals
, NULL
);
287 tstate
->frame
= frame
;
288 make_cleanup (py_restore_tstate
, frame
->f_back
);
289 return (PyObject
*) frame
;
292 /* Helper for apply_val_pretty_printer that formats children of the
293 printer, if any exist. */
295 print_children (PyObject
*printer
, const char *hint
,
296 struct ui_file
*stream
, int recurse
,
297 const struct value_print_options
*options
,
298 const struct language_defn
*language
)
300 int is_map
, is_array
, done_flag
, pretty
;
302 PyObject
*children
, *iter
, *frame
;
303 struct cleanup
*cleanups
;
305 if (! PyObject_HasAttr (printer
, gdbpy_children_cst
))
308 /* If we are printing a map or an array, we want some special
310 is_map
= hint
&& ! strcmp (hint
, "map");
311 is_array
= hint
&& ! strcmp (hint
, "array");
313 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
317 gdbpy_print_stack ();
321 cleanups
= make_cleanup_py_decref (children
);
323 iter
= PyObject_GetIter (children
);
326 gdbpy_print_stack ();
329 make_cleanup_py_decref (iter
);
331 /* Use the prettyprint_arrays option if we are printing an array,
332 and the pretty option otherwise. */
333 pretty
= is_array
? options
->prettyprint_arrays
: options
->pretty
;
335 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
336 where it insists on having a non-NULL tstate->frame when
337 a generator is called. */
338 frame
= push_dummy_python_frame ();
341 gdbpy_print_stack ();
344 make_cleanup_py_decref (frame
);
347 for (i
= 0; i
< options
->print_max
; ++i
)
349 PyObject
*py_v
, *item
= PyIter_Next (iter
);
351 struct cleanup
*inner_cleanup
;
355 if (PyErr_Occurred ())
356 gdbpy_print_stack ();
357 /* Set a flag so we can know whether we printed all the
358 available elements. */
364 if (! PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
366 gdbpy_print_stack ();
370 inner_cleanup
= make_cleanup_py_decref (item
);
372 /* Print initial "{". For other elements, there are three
374 1. Maps. Print a "," after each value element.
375 2. Arrays. Always print a ",".
376 3. Other. Always print a ",". */
378 fputs_filtered (" = {", stream
);
379 else if (! is_map
|| i
% 2 == 0)
380 fputs_filtered (pretty
? "," : ", ", stream
);
382 /* In summary mode, we just want to print "= {...}" if there is
384 if (options
->summary
)
386 /* This increment tricks the post-loop logic to print what
394 if (! is_map
|| i
% 2 == 0)
398 fputs_filtered ("\n", stream
);
399 print_spaces_filtered (2 + 2 * recurse
, stream
);
402 wrap_here (n_spaces (2 + 2 *recurse
));
405 if (is_map
&& i
% 2 == 0)
406 fputs_filtered ("[", stream
);
409 /* We print the index, not whatever the child method
410 returned as the name. */
411 if (options
->print_array_indexes
)
412 fprintf_filtered (stream
, "[%d] = ", i
);
416 fputs_filtered (name
, stream
);
417 fputs_filtered (" = ", stream
);
420 if (gdbpy_is_string (py_v
))
422 char *text
= python_string_to_host_string (py_v
);
424 gdbpy_print_stack ();
427 fputs_filtered (text
, stream
);
433 struct value
*value
= convert_value_from_python (py_v
);
437 gdbpy_print_stack ();
438 error (_("Error while executing Python code."));
441 common_val_print (value
, stream
, recurse
+ 1, options
, language
);
444 if (is_map
&& i
% 2 == 0)
445 fputs_filtered ("] = ", stream
);
447 do_cleanups (inner_cleanup
);
456 fputs_filtered ("\n", stream
);
457 print_spaces_filtered (2 + 2 * recurse
, stream
);
459 fputs_filtered ("...", stream
);
463 fputs_filtered ("\n", stream
);
464 print_spaces_filtered (2 * recurse
, stream
);
466 fputs_filtered ("}", stream
);
470 do_cleanups (cleanups
);
474 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
475 int embedded_offset
, CORE_ADDR address
,
476 struct ui_file
*stream
, int recurse
,
477 const struct value_print_options
*options
,
478 const struct language_defn
*language
)
480 struct gdbarch
*gdbarch
= get_type_arch (type
);
481 PyObject
*printer
= NULL
;
482 PyObject
*val_obj
= NULL
;
485 struct cleanup
*cleanups
;
488 cleanups
= ensure_python_env (gdbarch
, language
);
490 /* Instantiate the printer. */
492 valaddr
+= embedded_offset
;
493 value
= value_from_contents_and_address (type
, valaddr
, address
);
495 val_obj
= value_to_value_object (value
);
499 /* Find the constructor. */
500 printer
= find_pretty_printer (val_obj
);
502 make_cleanup_py_decref (printer
);
503 if (! printer
|| printer
== Py_None
)
506 /* If we are printing a map, we want some special formatting. */
507 hint
= gdbpy_get_display_hint (printer
);
508 make_cleanup (free_current_contents
, &hint
);
510 /* Print the section */
511 print_string_repr (printer
, hint
, stream
, recurse
, options
, language
,
513 print_children (printer
, hint
, stream
, recurse
, options
, language
);
518 if (PyErr_Occurred ())
519 gdbpy_print_stack ();
520 do_cleanups (cleanups
);
525 /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
526 print object. It must have a 'to_string' method (but this is
527 checked by varobj, not here) which takes no arguments and
528 returns a string. The printer will return a value and in the case
529 of a Python string being returned, this function will return a
530 PyObject containing the string. For any other type, *REPLACEMENT is
531 set to the replacement value and this function returns NULL. On
532 error, *REPLACEMENT is set to NULL and this function also returns
535 apply_varobj_pretty_printer (PyObject
*printer_obj
,
536 struct value
**replacement
)
539 PyObject
*py_str
= NULL
;
542 py_str
= pretty_print_one_value (printer_obj
, replacement
);
544 if (*replacement
== NULL
&& py_str
== NULL
)
545 gdbpy_print_stack ();
550 /* Find a pretty-printer object for the varobj module. Returns a new
551 reference to the object if successful; returns NULL if not. VALUE
552 is the value for which a printer tests to determine if it
553 can pretty-print the value. */
555 gdbpy_get_varobj_pretty_printer (struct value
*value
)
558 PyObject
*pretty_printer
= NULL
;
559 volatile struct gdb_exception except
;
561 TRY_CATCH (except
, RETURN_MASK_ALL
)
563 value
= value_copy (value
);
565 GDB_PY_HANDLE_EXCEPTION (except
);
567 val_obj
= value_to_value_object (value
);
571 pretty_printer
= find_pretty_printer (val_obj
);
573 return pretty_printer
;
576 /* A Python function which wraps find_pretty_printer and instantiates
577 the resulting class. This accepts a Value argument and returns a
578 pretty printer instance, or None. This function is useful as an
579 argument to the MI command -var-set-visualizer. */
581 gdbpy_default_visualizer (PyObject
*self
, PyObject
*args
)
584 PyObject
*cons
, *printer
= NULL
;
587 if (! PyArg_ParseTuple (args
, "O", &val_obj
))
589 value
= value_object_to_value (val_obj
);
592 PyErr_SetString (PyExc_TypeError
, "argument must be a gdb.Value");
596 cons
= find_pretty_printer (val_obj
);
600 #else /* HAVE_PYTHON */
603 apply_val_pretty_printer (struct type
*type
, const gdb_byte
*valaddr
,
604 int embedded_offset
, CORE_ADDR address
,
605 struct ui_file
*stream
, int recurse
,
606 const struct value_print_options
*options
,
607 const struct language_defn
*language
)
612 #endif /* HAVE_PYTHON */
This page took 0.051221 seconds and 4 git commands to generate.