gdb/python/py-prettyprint.c - gdb

Data types defined

Functions defined

Source code

  1. /* Python pretty-printing

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "objfiles.h"
  16. #include "symtab.h"
  17. #include "language.h"
  18. #include "valprint.h"
  19. #include "extension-priv.h"
  20. #include "python.h"
  21. #include "python-internal.h"

  22. /* Return type of print_string_repr.  */

  23. enum string_repr_result
  24.   {
  25.     /* The string method returned None.  */
  26.     string_repr_none,
  27.     /* The string method had an error.  */
  28.     string_repr_error,
  29.     /* Everything ok.  */
  30.     string_repr_ok
  31.   };

  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
  36.    return NULL.  */

  37. static PyObject *
  38. search_pp_list (PyObject *list, PyObject *value)
  39. {
  40.   Py_ssize_t pp_list_size, list_index;
  41.   PyObject *function, *printer = NULL;

  42.   pp_list_size = PyList_Size (list);
  43.   for (list_index = 0; list_index < pp_list_size; list_index++)
  44.     {
  45.       function = PyList_GetItem (list, list_index);
  46.       if (! function)
  47.         return NULL;

  48.       /* Skip if disabled.  */
  49.       if (PyObject_HasAttr (function, gdbpy_enabled_cst))
  50.         {
  51.           PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst);
  52.           int cmp;

  53.           if (!attr)
  54.             return NULL;
  55.           cmp = PyObject_IsTrue (attr);
  56.           Py_DECREF (attr);
  57.           if (cmp == -1)
  58.             return NULL;

  59.           if (!cmp)
  60.             continue;
  61.         }

  62.       printer = PyObject_CallFunctionObjArgs (function, value, NULL);
  63.       if (! printer)
  64.         return NULL;
  65.       else if (printer != Py_None)
  66.         return printer;

  67.       Py_DECREF (printer);
  68.     }

  69.   Py_RETURN_NONE;
  70. }

  71. /* Subroutine of find_pretty_printer to simplify it.
  72.    Look for a pretty-printer to print VALUE in all objfiles.
  73.    The result is NULL if there's an error and the search should be terminated.
  74.    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
  75.    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */

  76. static PyObject *
  77. find_pretty_printer_from_objfiles (PyObject *value)
  78. {
  79.   PyObject *pp_list;
  80.   PyObject *function;
  81.   struct objfile *obj;

  82.   ALL_OBJFILES (obj)
  83.   {
  84.     PyObject *objf = objfile_to_objfile_object (obj);
  85.     if (!objf)
  86.       {
  87.         /* Ignore the error and continue.  */
  88.         PyErr_Clear ();
  89.         continue;
  90.       }

  91.     pp_list = objfpy_get_printers (objf, NULL);
  92.     function = search_pp_list (pp_list, value);
  93.     Py_XDECREF (pp_list);

  94.     /* If there is an error in any objfile list, abort the search and exit.  */
  95.     if (! function)
  96.       return NULL;

  97.     if (function != Py_None)
  98.       return function;

  99.     Py_DECREF (function);
  100.   }

  101.   Py_RETURN_NONE;
  102. }

  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.  */

  108. static PyObject *
  109. find_pretty_printer_from_progspace (PyObject *value)
  110. {
  111.   PyObject *pp_list;
  112.   PyObject *function;
  113.   PyObject *obj = pspace_to_pspace_object (current_program_space);

  114.   if (!obj)
  115.     return NULL;
  116.   pp_list = pspy_get_printers (obj, NULL);
  117.   function = search_pp_list (pp_list, value);
  118.   Py_XDECREF (pp_list);
  119.   return function;
  120. }

  121. /* Subroutine of find_pretty_printer to simplify it.
  122.    Look for a pretty-printer to print VALUE in the gdb module.
  123.    The result is NULL if there's an error and the search should be terminated.
  124.    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
  125.    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */

  126. static PyObject *
  127. find_pretty_printer_from_gdb (PyObject *value)
  128. {
  129.   PyObject *pp_list;
  130.   PyObject *function;

  131.   /* Fetch the global pretty printer list.  */
  132.   if (gdb_python_module == NULL
  133.       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
  134.     Py_RETURN_NONE;
  135.   pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
  136.   if (pp_list == NULL || ! PyList_Check (pp_list))
  137.     {
  138.       Py_XDECREF (pp_list);
  139.       Py_RETURN_NONE;
  140.     }

  141.   function = search_pp_list (pp_list, value);
  142.   Py_XDECREF (pp_list);
  143.   return function;
  144. }

  145. /* Find the pretty-printing constructor function for VALUE.  If no
  146.    pretty-printer exists, return None.  If one exists, return a new
  147.    reference.  On error, set the Python error and return NULL.  */

  148. static PyObject *
  149. find_pretty_printer (PyObject *value)
  150. {
  151.   PyObject *function;

  152.   /* Look at the pretty-printer list for each objfile
  153.      in the current program-space.  */
  154.   function = find_pretty_printer_from_objfiles (value);
  155.   if (function == NULL || function != Py_None)
  156.     return function;
  157.   Py_DECREF (function);

  158.   /* Look at the pretty-printer list for the current program-space.  */
  159.   function = find_pretty_printer_from_progspace (value);
  160.   if (function == NULL || function != Py_None)
  161.     return function;
  162.   Py_DECREF (function);

  163.   /* Look at the pretty-printer list in the gdb module.  */
  164.   function = find_pretty_printer_from_gdb (value);
  165.   return function;
  166. }

  167. /* Pretty-print a single value, via the printer object PRINTER.
  168.    If the function returns a string, a PyObject containing the string
  169.    is returned.  If the function returns Py_NONE that means the pretty
  170.    printer returned the Python None as a value.  Otherwise, if the
  171.    function returns a value,  *OUT_VALUE is set to the value, and NULL
  172.    is returned.  On error, *OUT_VALUE is set to NULL, NULL is
  173.    returned, with a python exception set.  */

  174. static PyObject *
  175. pretty_print_one_value (PyObject *printer, struct value **out_value)
  176. {
  177.   volatile struct gdb_exception except;
  178.   PyObject *result = NULL;

  179.   *out_value = NULL;
  180.   TRY_CATCH (except, RETURN_MASK_ALL)
  181.     {
  182.       result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
  183.       if (result)
  184.         {
  185.           if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
  186.               && result != Py_None)
  187.             {
  188.               *out_value = convert_value_from_python (result);
  189.               if (PyErr_Occurred ())
  190.                 *out_value = NULL;
  191.               Py_DECREF (result);
  192.               result = NULL;
  193.             }
  194.         }
  195.     }

  196.   return result;
  197. }

  198. /* Return the display hint for the object printer, PRINTER.  Return
  199.    NULL if there is no display_hint method, or if the method did not
  200.    return a string.  On error, print stack trace and return NULL.  On
  201.    success, return an xmalloc()d string.  */
  202. char *
  203. gdbpy_get_display_hint (PyObject *printer)
  204. {
  205.   PyObject *hint;
  206.   char *result = NULL;

  207.   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
  208.     return NULL;

  209.   hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
  210.   if (hint)
  211.     {
  212.       if (gdbpy_is_string (hint))
  213.         {
  214.           result = python_string_to_host_string (hint);
  215.           if (result == NULL)
  216.             gdbpy_print_stack ();
  217.         }
  218.       Py_DECREF (hint);
  219.     }
  220.   else
  221.     gdbpy_print_stack ();

  222.   return result;
  223. }

  224. /* A wrapper for gdbpy_print_stack that ignores MemoryError.  */

  225. static void
  226. print_stack_unless_memory_error (struct ui_file *stream)
  227. {
  228.   if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
  229.     {
  230.       struct cleanup *cleanup;
  231.       PyObject *type, *value, *trace;
  232.       char *msg;

  233.       PyErr_Fetch (&type, &value, &trace);
  234.       cleanup = make_cleanup_py_decref (type);
  235.       make_cleanup_py_decref (value);
  236.       make_cleanup_py_decref (trace);

  237.       msg = gdbpy_exception_to_string (type, value);
  238.       make_cleanup (xfree, msg);

  239.       if (msg == NULL || *msg == '\0')
  240.         fprintf_filtered (stream, _("<error reading variable>"));
  241.       else
  242.         fprintf_filtered (stream, _("<error reading variable: %s>"), msg);

  243.       do_cleanups (cleanup);
  244.     }
  245.   else
  246.     gdbpy_print_stack ();
  247. }

  248. /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and
  249.    formats the result.  */

  250. static enum string_repr_result
  251. print_string_repr (PyObject *printer, const char *hint,
  252.                    struct ui_file *stream, int recurse,
  253.                    const struct value_print_options *options,
  254.                    const struct language_defn *language,
  255.                    struct gdbarch *gdbarch)
  256. {
  257.   struct value *replacement = NULL;
  258.   PyObject *py_str = NULL;
  259.   enum string_repr_result result = string_repr_ok;

  260.   py_str = pretty_print_one_value (printer, &replacement);
  261.   if (py_str)
  262.     {
  263.       struct cleanup *cleanup = make_cleanup_py_decref (py_str);

  264.       if (py_str == Py_None)
  265.         result = string_repr_none;
  266.       else if (gdbpy_is_lazy_string (py_str))
  267.         {
  268.           CORE_ADDR addr;
  269.           long length;
  270.           struct type *type;
  271.           char *encoding = NULL;
  272.           struct value_print_options local_opts = *options;

  273.           make_cleanup (free_current_contents, &encoding);
  274.           gdbpy_extract_lazy_string (py_str, &addr, &type,
  275.                                      &length, &encoding);

  276.           local_opts.addressprint = 0;
  277.           val_print_string (type, encoding, addr, (int) length,
  278.                             stream, &local_opts);
  279.         }
  280.       else
  281.         {
  282.           PyObject *string;

  283.           string = python_string_to_target_python_string (py_str);
  284.           if (string)
  285.             {
  286.               char *output;
  287.               long length;
  288.               struct type *type;

  289.               make_cleanup_py_decref (string);
  290. #ifdef IS_PY3K
  291.               output = PyBytes_AS_STRING (string);
  292.               length = PyBytes_GET_SIZE (string);
  293. #else
  294.               output = PyString_AsString (string);
  295.               length = PyString_Size (string);
  296. #endif
  297.               type = builtin_type (gdbarch)->builtin_char;

  298.               if (hint && !strcmp (hint, "string"))
  299.                 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
  300.                                  length, NULL, 0, options);
  301.               else
  302.                 fputs_filtered (output, stream);
  303.             }
  304.           else
  305.             {
  306.               result = string_repr_error;
  307.               print_stack_unless_memory_error (stream);
  308.             }
  309.         }

  310.       do_cleanups (cleanup);
  311.     }
  312.   else if (replacement)
  313.     {
  314.       struct value_print_options opts = *options;

  315.       opts.addressprint = 0;
  316.       common_val_print (replacement, stream, recurse, &opts, language);
  317.     }
  318.   else
  319.     {
  320.       result = string_repr_error;
  321.       print_stack_unless_memory_error (stream);
  322.     }

  323.   return result;
  324. }

  325. #ifndef IS_PY3K
  326. static void
  327. py_restore_tstate (void *p)
  328. {
  329.   PyFrameObject *frame = p;
  330.   PyThreadState *tstate = PyThreadState_GET ();

  331.   tstate->frame = frame;
  332. }

  333. /* Create a dummy PyFrameObject, needed to work around
  334.    a Python-2.4 bug with generators.  */
  335. static PyObject *
  336. push_dummy_python_frame (void)
  337. {
  338.   PyObject *empty_string, *null_tuple, *globals;
  339.   PyCodeObject *code;
  340.   PyFrameObject *frame;
  341.   PyThreadState *tstate;

  342.   empty_string = PyString_FromString ("");
  343.   if (!empty_string)
  344.     return NULL;

  345.   null_tuple = PyTuple_New (0);
  346.   if (!null_tuple)
  347.     {
  348.       Py_DECREF (empty_string);
  349.       return NULL;
  350.     }

  351.   code = PyCode_New (0,                        /* argcount */
  352.                      0,                        /* nlocals */
  353.                      0,                        /* stacksize */
  354.                      0,                        /* flags */
  355.                      empty_string,        /* code */
  356.                      null_tuple,        /* consts */
  357.                      null_tuple,        /* names */
  358.                      null_tuple,        /* varnames */
  359. #if PYTHON_API_VERSION >= 1010
  360.                      null_tuple,        /* freevars */
  361.                      null_tuple,        /* cellvars */
  362. #endif
  363.                      empty_string,        /* filename */
  364.                      empty_string,        /* name */
  365.                      1,                        /* firstlineno */
  366.                      empty_string        /* lnotab */
  367.                     );

  368.   Py_DECREF (empty_string);
  369.   Py_DECREF (null_tuple);

  370.   if (!code)
  371.     return NULL;

  372.   globals = PyDict_New ();
  373.   if (!globals)
  374.     {
  375.       Py_DECREF (code);
  376.       return NULL;
  377.     }

  378.   tstate = PyThreadState_GET ();

  379.   frame = PyFrame_New (tstate, code, globals, NULL);

  380.   Py_DECREF (globals);
  381.   Py_DECREF (code);

  382.   if (!frame)
  383.     return NULL;

  384.   tstate->frame = frame;
  385.   make_cleanup (py_restore_tstate, frame->f_back);
  386.   return (PyObject *) frame;
  387. }
  388. #endif

  389. /* Helper for gdbpy_apply_val_pretty_printer that formats children of the
  390.    printer, if any exist.  If is_py_none is true, then nothing has
  391.    been printed by to_string, and format output accordingly. */
  392. static void
  393. print_children (PyObject *printer, const char *hint,
  394.                 struct ui_file *stream, int recurse,
  395.                 const struct value_print_options *options,
  396.                 const struct language_defn *language,
  397.                 int is_py_none)
  398. {
  399.   int is_map, is_array, done_flag, pretty;
  400.   unsigned int i;
  401.   PyObject *children, *iter;
  402. #ifndef IS_PY3K
  403.   PyObject *frame;
  404. #endif
  405.   struct cleanup *cleanups;

  406.   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
  407.     return;

  408.   /* If we are printing a map or an array, we want some special
  409.      formatting.  */
  410.   is_map = hint && ! strcmp (hint, "map");
  411.   is_array = hint && ! strcmp (hint, "array");

  412.   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
  413.                                          NULL);
  414.   if (! children)
  415.     {
  416.       print_stack_unless_memory_error (stream);
  417.       return;
  418.     }

  419.   cleanups = make_cleanup_py_decref (children);

  420.   iter = PyObject_GetIter (children);
  421.   if (!iter)
  422.     {
  423.       print_stack_unless_memory_error (stream);
  424.       goto done;
  425.     }
  426.   make_cleanup_py_decref (iter);

  427.   /* Use the prettyformat_arrays option if we are printing an array,
  428.      and the pretty option otherwise.  */
  429.   if (is_array)
  430.     pretty = options->prettyformat_arrays;
  431.   else
  432.     {
  433.       if (options->prettyformat == Val_prettyformat)
  434.         pretty = 1;
  435.       else
  436.         pretty = options->prettyformat_structs;
  437.     }

  438.   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
  439.      where it insists on having a non-NULL tstate->frame when
  440.      a generator is called.  */
  441. #ifndef IS_PY3K
  442.   frame = push_dummy_python_frame ();
  443.   if (!frame)
  444.     {
  445.       gdbpy_print_stack ();
  446.       goto done;
  447.     }
  448.   make_cleanup_py_decref (frame);
  449. #endif

  450.   done_flag = 0;
  451.   for (i = 0; i < options->print_max; ++i)
  452.     {
  453.       PyObject *py_v, *item = PyIter_Next (iter);
  454.       const char *name;
  455.       struct cleanup *inner_cleanup;

  456.       if (! item)
  457.         {
  458.           if (PyErr_Occurred ())
  459.             print_stack_unless_memory_error (stream);
  460.           /* Set a flag so we can know whether we printed all the
  461.              available elements.  */
  462.           else
  463.             done_flag = 1;
  464.           break;
  465.         }

  466.       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
  467.         {
  468.           gdbpy_print_stack ();
  469.           Py_DECREF (item);
  470.           continue;
  471.         }
  472.       inner_cleanup = make_cleanup_py_decref (item);

  473.       /* Print initial "{".  For other elements, there are three
  474.          cases:
  475.          1. Maps.  Print a "," after each value element.
  476.          2. Arrays.  Always print a ",".
  477.          3. Other.  Always print a ",".  */
  478.       if (i == 0)
  479.         {
  480.          if (is_py_none)
  481.            fputs_filtered ("{", stream);
  482.          else
  483.            fputs_filtered (" = {", stream);
  484.        }

  485.       else if (! is_map || i % 2 == 0)
  486.         fputs_filtered (pretty ? "," : ", ", stream);

  487.       /* In summary mode, we just want to print "= {...}" if there is
  488.          a value.  */
  489.       if (options->summary)
  490.         {
  491.           /* This increment tricks the post-loop logic to print what
  492.              we want.  */
  493.           ++i;
  494.           /* Likewise.  */
  495.           pretty = 0;
  496.           break;
  497.         }

  498.       if (! is_map || i % 2 == 0)
  499.         {
  500.           if (pretty)
  501.             {
  502.               fputs_filtered ("\n", stream);
  503.               print_spaces_filtered (2 + 2 * recurse, stream);
  504.             }
  505.           else
  506.             wrap_here (n_spaces (2 + 2 *recurse));
  507.         }

  508.       if (is_map && i % 2 == 0)
  509.         fputs_filtered ("[", stream);
  510.       else if (is_array)
  511.         {
  512.           /* We print the index, not whatever the child method
  513.              returned as the name.  */
  514.           if (options->print_array_indexes)
  515.             fprintf_filtered (stream, "[%d] = ", i);
  516.         }
  517.       else if (! is_map)
  518.         {
  519.           fputs_filtered (name, stream);
  520.           fputs_filtered (" = ", stream);
  521.         }

  522.       if (gdbpy_is_lazy_string (py_v))
  523.         {
  524.           CORE_ADDR addr;
  525.           struct type *type;
  526.           long length;
  527.           char *encoding = NULL;
  528.           struct value_print_options local_opts = *options;

  529.           make_cleanup (free_current_contents, &encoding);
  530.           gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);

  531.           local_opts.addressprint = 0;
  532.           val_print_string (type, encoding, addr, (int) length, stream,
  533.                             &local_opts);
  534.         }
  535.       else if (gdbpy_is_string (py_v))
  536.         {
  537.           char *output;

  538.           output = python_string_to_host_string (py_v);
  539.           if (!output)
  540.             gdbpy_print_stack ();
  541.           else
  542.             {
  543.               fputs_filtered (output, stream);
  544.               xfree (output);
  545.             }
  546.         }
  547.       else
  548.         {
  549.           struct value *value = convert_value_from_python (py_v);

  550.           if (value == NULL)
  551.             {
  552.               gdbpy_print_stack ();
  553.               error (_("Error while executing Python code."));
  554.             }
  555.           else
  556.             common_val_print (value, stream, recurse + 1, options, language);
  557.         }

  558.       if (is_map && i % 2 == 0)
  559.         fputs_filtered ("] = ", stream);

  560.       do_cleanups (inner_cleanup);
  561.     }

  562.   if (i)
  563.     {
  564.       if (!done_flag)
  565.         {
  566.           if (pretty)
  567.             {
  568.               fputs_filtered ("\n", stream);
  569.               print_spaces_filtered (2 + 2 * recurse, stream);
  570.             }
  571.           fputs_filtered ("...", stream);
  572.         }
  573.       if (pretty)
  574.         {
  575.           fputs_filtered ("\n", stream);
  576.           print_spaces_filtered (2 * recurse, stream);
  577.         }
  578.       fputs_filtered ("}", stream);
  579.     }

  580. done:
  581.   do_cleanups (cleanups);
  582. }

  583. enum ext_lang_rc
  584. gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
  585.                                 struct type *type, const gdb_byte *valaddr,
  586.                                 int embedded_offset, CORE_ADDR address,
  587.                                 struct ui_file *stream, int recurse,
  588.                                 const struct value *val,
  589.                                 const struct value_print_options *options,
  590.                                 const struct language_defn *language)
  591. {
  592.   struct gdbarch *gdbarch = get_type_arch (type);
  593.   PyObject *printer = NULL;
  594.   PyObject *val_obj = NULL;
  595.   struct value *value;
  596.   char *hint = NULL;
  597.   struct cleanup *cleanups;
  598.   enum ext_lang_rc result = EXT_LANG_RC_NOP;
  599.   enum string_repr_result print_result;

  600.   /* No pretty-printer support for unavailable values.  */
  601.   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
  602.     return EXT_LANG_RC_NOP;

  603.   if (!gdb_python_initialized)
  604.     return EXT_LANG_RC_NOP;

  605.   cleanups = ensure_python_env (gdbarch, language);

  606.   /* Instantiate the printer.  */
  607.   if (valaddr)
  608.     valaddr += embedded_offset;
  609.   value = value_from_contents_and_address (type, valaddr,
  610.                                            address + embedded_offset);

  611.   set_value_component_location (value, val);
  612.   /* set_value_component_location resets the address, so we may
  613.      need to set it again.  */
  614.   if (VALUE_LVAL (value) != lval_internalvar
  615.       && VALUE_LVAL (value) != lval_internalvar_component
  616.       && VALUE_LVAL (value) != lval_computed)
  617.     set_value_address (value, address + embedded_offset);

  618.   val_obj = value_to_value_object (value);
  619.   if (! val_obj)
  620.     {
  621.       result = EXT_LANG_RC_ERROR;
  622.       goto done;
  623.     }

  624.   /* Find the constructor.  */
  625.   printer = find_pretty_printer (val_obj);
  626.   Py_DECREF (val_obj);

  627.   if (printer == NULL)
  628.     {
  629.       result = EXT_LANG_RC_ERROR;
  630.       goto done;
  631.     }

  632.   make_cleanup_py_decref (printer);
  633.   if (printer == Py_None)
  634.     {
  635.       result = EXT_LANG_RC_NOP;
  636.       goto done;
  637.     }

  638.   /* If we are printing a map, we want some special formatting.  */
  639.   hint = gdbpy_get_display_hint (printer);
  640.   make_cleanup (free_current_contents, &hint);

  641.   /* Print the section */
  642.   print_result = print_string_repr (printer, hint, stream, recurse,
  643.                                     options, language, gdbarch);
  644.   if (print_result != string_repr_error)
  645.     print_children (printer, hint, stream, recurse, options, language,
  646.                     print_result == string_repr_none);

  647.   result = EXT_LANG_RC_OK;

  648. done:
  649.   if (PyErr_Occurred ())
  650.     print_stack_unless_memory_error (stream);
  651.   do_cleanups (cleanups);
  652.   return result;
  653. }


  654. /* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
  655.    print object.  It must have a 'to_string' method (but this is
  656.    checked by varobj, not here) which takes no arguments and
  657.    returns a string.  The printer will return a value and in the case
  658.    of a Python string being returned, this function will return a
  659.    PyObject containing the string.  For any other type, *REPLACEMENT is
  660.    set to the replacement value and this function returns NULL.  On
  661.    error, *REPLACEMENT is set to NULL and this function also returns
  662.    NULL.  */
  663. PyObject *
  664. apply_varobj_pretty_printer (PyObject *printer_obj,
  665.                              struct value **replacement,
  666.                              struct ui_file *stream)
  667. {
  668.   PyObject *py_str = NULL;

  669.   *replacement = NULL;
  670.   py_str = pretty_print_one_value (printer_obj, replacement);

  671.   if (*replacement == NULL && py_str == NULL)
  672.     print_stack_unless_memory_error (stream);

  673.   return py_str;
  674. }

  675. /* Find a pretty-printer object for the varobj module.  Returns a new
  676.    reference to the object if successful; returns NULL if not.  VALUE
  677.    is the value for which a printer tests to determine if it
  678.    can pretty-print the value.  */
  679. PyObject *
  680. gdbpy_get_varobj_pretty_printer (struct value *value)
  681. {
  682.   PyObject *val_obj;
  683.   PyObject *pretty_printer = NULL;
  684.   volatile struct gdb_exception except;

  685.   TRY_CATCH (except, RETURN_MASK_ALL)
  686.     {
  687.       value = value_copy (value);
  688.     }
  689.   GDB_PY_HANDLE_EXCEPTION (except);

  690.   val_obj = value_to_value_object (value);
  691.   if (! val_obj)
  692.     return NULL;

  693.   pretty_printer = find_pretty_printer (val_obj);
  694.   Py_DECREF (val_obj);
  695.   return pretty_printer;
  696. }

  697. /* A Python function which wraps find_pretty_printer and instantiates
  698.    the resulting class.  This accepts a Value argument and returns a
  699.    pretty printer instance, or None.  This function is useful as an
  700.    argument to the MI command -var-set-visualizer.  */
  701. PyObject *
  702. gdbpy_default_visualizer (PyObject *self, PyObject *args)
  703. {
  704.   PyObject *val_obj;
  705.   PyObject *cons;
  706.   struct value *value;

  707.   if (! PyArg_ParseTuple (args, "O", &val_obj))
  708.     return NULL;
  709.   value = value_object_to_value (val_obj);
  710.   if (! value)
  711.     {
  712.       PyErr_SetString (PyExc_TypeError,
  713.                        _("Argument must be a gdb.Value."));
  714.       return NULL;
  715.     }

  716.   cons = find_pretty_printer (val_obj);
  717.   return cons;
  718. }