GDB (API)
/home/stan/gdb/src/gdb/python/py-prettyprint.c
Go to the documentation of this file.
00001 /* Python pretty-printing
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "exceptions.h"
00022 #include "objfiles.h"
00023 #include "symtab.h"
00024 #include "language.h"
00025 #include "valprint.h"
00026 
00027 #include "python.h"
00028 
00029 #ifdef HAVE_PYTHON
00030 #include "python-internal.h"
00031 
00032 /* Return type of print_string_repr.  */
00033 
00034 enum string_repr_result
00035   {
00036     /* The string method returned None.  */
00037     string_repr_none,
00038     /* The string method had an error.  */
00039     string_repr_error,
00040     /* Everything ok.  */
00041     string_repr_ok
00042   };
00043 
00044 /* Helper function for find_pretty_printer which iterates over a list,
00045    calls each function and inspects output.  This will return a
00046    printer object if one recognizes VALUE.  If no printer is found, it
00047    will return None.  On error, it will set the Python error and
00048    return NULL.  */
00049 
00050 static PyObject *
00051 search_pp_list (PyObject *list, PyObject *value)
00052 {
00053   Py_ssize_t pp_list_size, list_index;
00054   PyObject *function, *printer = NULL;
00055 
00056   pp_list_size = PyList_Size (list);
00057   for (list_index = 0; list_index < pp_list_size; list_index++)
00058     {
00059       function = PyList_GetItem (list, list_index);
00060       if (! function)
00061         return NULL;
00062 
00063       /* Skip if disabled.  */
00064       if (PyObject_HasAttr (function, gdbpy_enabled_cst))
00065         {
00066           PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst);
00067           int cmp;
00068 
00069           if (!attr)
00070             return NULL;
00071           cmp = PyObject_IsTrue (attr);
00072           Py_DECREF (attr);
00073           if (cmp == -1)
00074             return NULL;
00075 
00076           if (!cmp)
00077             continue;
00078         }
00079 
00080       printer = PyObject_CallFunctionObjArgs (function, value, NULL);
00081       if (! printer)
00082         return NULL;
00083       else if (printer != Py_None)
00084         return printer;
00085 
00086       Py_DECREF (printer);
00087     }
00088 
00089   Py_RETURN_NONE;
00090 }
00091 
00092 /* Subroutine of find_pretty_printer to simplify it.
00093    Look for a pretty-printer to print VALUE in all objfiles.
00094    The result is NULL if there's an error and the search should be terminated.
00095    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
00096    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
00097 
00098 static PyObject *
00099 find_pretty_printer_from_objfiles (PyObject *value)
00100 {
00101   PyObject *pp_list;
00102   PyObject *function;
00103   struct objfile *obj;
00104 
00105   ALL_OBJFILES (obj)
00106   {
00107     PyObject *objf = objfile_to_objfile_object (obj);
00108     if (!objf)
00109       {
00110         /* Ignore the error and continue.  */
00111         PyErr_Clear ();
00112         continue;
00113       }
00114 
00115     pp_list = objfpy_get_printers (objf, NULL);
00116     function = search_pp_list (pp_list, value);
00117     Py_XDECREF (pp_list);
00118 
00119     /* If there is an error in any objfile list, abort the search and exit.  */
00120     if (! function)
00121       return NULL;
00122 
00123     if (function != Py_None)
00124       return function;
00125     
00126     Py_DECREF (function);
00127   }
00128 
00129   Py_RETURN_NONE;
00130 }
00131 
00132 /* Subroutine of find_pretty_printer to simplify it.
00133    Look for a pretty-printer to print VALUE in the current program space.
00134    The result is NULL if there's an error and the search should be terminated.
00135    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
00136    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
00137 
00138 static PyObject *
00139 find_pretty_printer_from_progspace (PyObject *value)
00140 {
00141   PyObject *pp_list;
00142   PyObject *function;
00143   PyObject *obj = pspace_to_pspace_object (current_program_space);
00144 
00145   if (!obj)
00146     return NULL;
00147   pp_list = pspy_get_printers (obj, NULL);
00148   function = search_pp_list (pp_list, value);
00149   Py_XDECREF (pp_list);
00150   return function;
00151 }
00152 
00153 /* Subroutine of find_pretty_printer to simplify it.
00154    Look for a pretty-printer to print VALUE in the gdb module.
00155    The result is NULL if there's an error and the search should be terminated.
00156    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
00157    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
00158 
00159 static PyObject *
00160 find_pretty_printer_from_gdb (PyObject *value)
00161 {
00162   PyObject *pp_list;
00163   PyObject *function;
00164 
00165   /* Fetch the global pretty printer list.  */
00166   if (gdb_python_module == NULL
00167       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
00168     Py_RETURN_NONE;
00169   pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
00170   if (pp_list == NULL || ! PyList_Check (pp_list))
00171     {
00172       Py_XDECREF (pp_list);
00173       Py_RETURN_NONE;
00174     }
00175 
00176   function = search_pp_list (pp_list, value);
00177   Py_XDECREF (pp_list);
00178   return function;
00179 }
00180 
00181 /* Find the pretty-printing constructor function for VALUE.  If no
00182    pretty-printer exists, return None.  If one exists, return a new
00183    reference.  On error, set the Python error and return NULL.  */
00184 
00185 static PyObject *
00186 find_pretty_printer (PyObject *value)
00187 {
00188   PyObject *function;
00189 
00190   /* Look at the pretty-printer list for each objfile
00191      in the current program-space.  */
00192   function = find_pretty_printer_from_objfiles (value);
00193   if (function == NULL || function != Py_None)
00194     return function;
00195   Py_DECREF (function);
00196 
00197   /* Look at the pretty-printer list for the current program-space.  */
00198   function = find_pretty_printer_from_progspace (value);
00199   if (function == NULL || function != Py_None)
00200     return function;
00201   Py_DECREF (function);
00202 
00203   /* Look at the pretty-printer list in the gdb module.  */
00204   function = find_pretty_printer_from_gdb (value);
00205   return function;
00206 }
00207 
00208 /* Pretty-print a single value, via the printer object PRINTER.
00209    If the function returns a string, a PyObject containing the string
00210    is returned.  If the function returns Py_NONE that means the pretty
00211    printer returned the Python None as a value.  Otherwise, if the
00212    function returns a value,  *OUT_VALUE is set to the value, and NULL
00213    is returned.  On error, *OUT_VALUE is set to NULL, NULL is
00214    returned, with a python exception set.  */
00215 
00216 static PyObject *
00217 pretty_print_one_value (PyObject *printer, struct value **out_value)
00218 {
00219   volatile struct gdb_exception except;
00220   PyObject *result = NULL;
00221 
00222   *out_value = NULL;
00223   TRY_CATCH (except, RETURN_MASK_ALL)
00224     {
00225       result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
00226       if (result)
00227         {
00228           if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)  
00229               && result != Py_None)
00230             {
00231               *out_value = convert_value_from_python (result);
00232               if (PyErr_Occurred ())
00233                 *out_value = NULL;
00234               Py_DECREF (result);
00235               result = NULL;
00236             }
00237         }
00238     }
00239 
00240   return result;
00241 }
00242 
00243 /* Return the display hint for the object printer, PRINTER.  Return
00244    NULL if there is no display_hint method, or if the method did not
00245    return a string.  On error, print stack trace and return NULL.  On
00246    success, return an xmalloc()d string.  */
00247 char *
00248 gdbpy_get_display_hint (PyObject *printer)
00249 {
00250   PyObject *hint;
00251   char *result = NULL;
00252 
00253   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
00254     return NULL;
00255 
00256   hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
00257   if (hint)
00258     {
00259       if (gdbpy_is_string (hint))
00260         {
00261           result = python_string_to_host_string (hint);
00262           if (result == NULL)
00263             gdbpy_print_stack ();
00264         }
00265       Py_DECREF (hint);
00266     }
00267   else
00268     gdbpy_print_stack ();
00269 
00270   return result;
00271 }
00272 
00273 /* A wrapper for gdbpy_print_stack that ignores MemoryError.  */
00274 
00275 static void
00276 print_stack_unless_memory_error (struct ui_file *stream)
00277 {
00278   if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
00279     {
00280       struct cleanup *cleanup;
00281       PyObject *type, *value, *trace;
00282       char *msg;
00283 
00284       PyErr_Fetch (&type, &value, &trace);
00285       cleanup = make_cleanup_py_decref (type);
00286       make_cleanup_py_decref (value);
00287       make_cleanup_py_decref (trace);
00288 
00289       msg = gdbpy_exception_to_string (type, value);
00290       make_cleanup (xfree, msg);
00291 
00292       if (msg == NULL || *msg == '\0')
00293         fprintf_filtered (stream, _("<error reading variable>"));
00294       else
00295         fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
00296 
00297       do_cleanups (cleanup);
00298     }
00299   else
00300     gdbpy_print_stack ();
00301 }
00302 
00303 /* Helper for apply_val_pretty_printer which calls to_string and
00304    formats the result.  */
00305 
00306 static enum string_repr_result
00307 print_string_repr (PyObject *printer, const char *hint,
00308                    struct ui_file *stream, int recurse,
00309                    const struct value_print_options *options,
00310                    const struct language_defn *language,
00311                    struct gdbarch *gdbarch)
00312 {
00313   struct value *replacement = NULL;
00314   PyObject *py_str = NULL;
00315   enum string_repr_result result = string_repr_ok;
00316 
00317   py_str = pretty_print_one_value (printer, &replacement);
00318   if (py_str)
00319     {
00320       struct cleanup *cleanup = make_cleanup_py_decref (py_str);
00321 
00322       if (py_str == Py_None)
00323         result = string_repr_none;
00324       else if (gdbpy_is_lazy_string (py_str))
00325         {
00326           CORE_ADDR addr;
00327           long length;
00328           struct type *type;
00329           char *encoding = NULL;
00330           struct value_print_options local_opts = *options;
00331 
00332           make_cleanup (free_current_contents, &encoding);
00333           gdbpy_extract_lazy_string (py_str, &addr, &type,
00334                                      &length, &encoding);
00335 
00336           local_opts.addressprint = 0;
00337           val_print_string (type, encoding, addr, (int) length,
00338                             stream, &local_opts);
00339         }
00340       else
00341         {
00342           PyObject *string;
00343 
00344           string = python_string_to_target_python_string (py_str);
00345           if (string)
00346             {
00347               char *output;
00348               long length;
00349               struct type *type;
00350 
00351               make_cleanup_py_decref (string);
00352 #ifdef IS_PY3K
00353               output = PyBytes_AS_STRING (string);
00354               length = PyBytes_GET_SIZE (string);
00355 #else
00356               output = PyString_AsString (string);
00357               length = PyString_Size (string);
00358 #endif
00359               type = builtin_type (gdbarch)->builtin_char;
00360 
00361               if (hint && !strcmp (hint, "string"))
00362                 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
00363                                  length, NULL, 0, options);
00364               else
00365                 fputs_filtered (output, stream);
00366             }
00367           else
00368             {
00369               result = string_repr_error;
00370               print_stack_unless_memory_error (stream);
00371             }
00372         }
00373 
00374       do_cleanups (cleanup);
00375     }
00376   else if (replacement)
00377     {
00378       struct value_print_options opts = *options;
00379 
00380       opts.addressprint = 0;
00381       common_val_print (replacement, stream, recurse, &opts, language);
00382     }
00383   else
00384     {
00385       result = string_repr_error;
00386       print_stack_unless_memory_error (stream);
00387     }
00388 
00389   return result;
00390 }
00391 
00392 #ifndef IS_PY3K
00393 static void
00394 py_restore_tstate (void *p)
00395 {
00396   PyFrameObject *frame = p;
00397   PyThreadState *tstate = PyThreadState_GET ();
00398 
00399   tstate->frame = frame;
00400 }
00401 
00402 /* Create a dummy PyFrameObject, needed to work around
00403    a Python-2.4 bug with generators.  */
00404 static PyObject *
00405 push_dummy_python_frame (void)
00406 {
00407   PyObject *empty_string, *null_tuple, *globals;
00408   PyCodeObject *code;
00409   PyFrameObject *frame;
00410   PyThreadState *tstate;
00411 
00412   empty_string = PyString_FromString ("");
00413   if (!empty_string)
00414     return NULL;
00415 
00416   null_tuple = PyTuple_New (0);
00417   if (!null_tuple)
00418     {
00419       Py_DECREF (empty_string);
00420       return NULL;
00421     }
00422 
00423   code = PyCode_New (0,                 /* argcount */
00424                      0,                 /* nlocals */
00425                      0,                 /* stacksize */
00426                      0,                 /* flags */
00427                      empty_string,      /* code */
00428                      null_tuple,        /* consts */
00429                      null_tuple,        /* names */
00430                      null_tuple,        /* varnames */
00431 #if PYTHON_API_VERSION >= 1010
00432                      null_tuple,        /* freevars */
00433                      null_tuple,        /* cellvars */
00434 #endif
00435                      empty_string,      /* filename */
00436                      empty_string,      /* name */
00437                      1,                 /* firstlineno */
00438                      empty_string       /* lnotab */
00439                     );
00440 
00441   Py_DECREF (empty_string);
00442   Py_DECREF (null_tuple);
00443 
00444   if (!code)
00445     return NULL;
00446 
00447   globals = PyDict_New ();
00448   if (!globals)
00449     {
00450       Py_DECREF (code);
00451       return NULL;
00452     }
00453 
00454   tstate = PyThreadState_GET ();
00455 
00456   frame = PyFrame_New (tstate, code, globals, NULL);
00457 
00458   Py_DECREF (globals);
00459   Py_DECREF (code);
00460 
00461   if (!frame)
00462     return NULL;
00463 
00464   tstate->frame = frame;
00465   make_cleanup (py_restore_tstate, frame->f_back);
00466   return (PyObject *) frame;
00467 }
00468 #endif
00469 
00470 /* Helper for apply_val_pretty_printer that formats children of the
00471    printer, if any exist.  If is_py_none is true, then nothing has
00472    been printed by to_string, and format output accordingly. */
00473 static void
00474 print_children (PyObject *printer, const char *hint,
00475                 struct ui_file *stream, int recurse,
00476                 const struct value_print_options *options,
00477                 const struct language_defn *language,
00478                 int is_py_none)
00479 {
00480   int is_map, is_array, done_flag, pretty;
00481   unsigned int i;
00482   PyObject *children, *iter;
00483 #ifndef IS_PY3K
00484   PyObject *frame;
00485 #endif
00486   struct cleanup *cleanups;
00487 
00488   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
00489     return;
00490 
00491   /* If we are printing a map or an array, we want some special
00492      formatting.  */
00493   is_map = hint && ! strcmp (hint, "map");
00494   is_array = hint && ! strcmp (hint, "array");
00495 
00496   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
00497                                          NULL);
00498   if (! children)
00499     {
00500       print_stack_unless_memory_error (stream);
00501       return;
00502     }
00503 
00504   cleanups = make_cleanup_py_decref (children);
00505 
00506   iter = PyObject_GetIter (children);
00507   if (!iter)
00508     {
00509       print_stack_unless_memory_error (stream);
00510       goto done;
00511     }
00512   make_cleanup_py_decref (iter);
00513 
00514   /* Use the prettyformat_arrays option if we are printing an array,
00515      and the pretty option otherwise.  */
00516   if (is_array)
00517     pretty = options->prettyformat_arrays;
00518   else
00519     {
00520       if (options->prettyformat == Val_prettyformat)
00521         pretty = 1;
00522       else
00523         pretty = options->prettyformat_structs;
00524     }
00525 
00526   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
00527      where it insists on having a non-NULL tstate->frame when
00528      a generator is called.  */
00529 #ifndef IS_PY3K
00530   frame = push_dummy_python_frame ();
00531   if (!frame)
00532     {
00533       gdbpy_print_stack ();
00534       goto done;
00535     }
00536   make_cleanup_py_decref (frame);
00537 #endif
00538 
00539   done_flag = 0;
00540   for (i = 0; i < options->print_max; ++i)
00541     {
00542       PyObject *py_v, *item = PyIter_Next (iter);
00543       const char *name;
00544       struct cleanup *inner_cleanup;
00545 
00546       if (! item)
00547         {
00548           if (PyErr_Occurred ())
00549             print_stack_unless_memory_error (stream);
00550           /* Set a flag so we can know whether we printed all the
00551              available elements.  */
00552           else    
00553             done_flag = 1;
00554           break;
00555         }
00556 
00557       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
00558         {
00559           gdbpy_print_stack ();
00560           Py_DECREF (item);
00561           continue;
00562         }
00563       inner_cleanup = make_cleanup_py_decref (item);
00564 
00565       /* Print initial "{".  For other elements, there are three
00566          cases:
00567          1. Maps.  Print a "," after each value element.
00568          2. Arrays.  Always print a ",".
00569          3. Other.  Always print a ",".  */
00570       if (i == 0)
00571         {
00572          if (is_py_none)
00573            fputs_filtered ("{", stream);
00574          else
00575            fputs_filtered (" = {", stream);
00576        }
00577 
00578       else if (! is_map || i % 2 == 0)
00579         fputs_filtered (pretty ? "," : ", ", stream);
00580 
00581       /* In summary mode, we just want to print "= {...}" if there is
00582          a value.  */
00583       if (options->summary)
00584         {
00585           /* This increment tricks the post-loop logic to print what
00586              we want.  */
00587           ++i;
00588           /* Likewise.  */
00589           pretty = 0;
00590           break;
00591         }
00592 
00593       if (! is_map || i % 2 == 0)
00594         {
00595           if (pretty)
00596             {
00597               fputs_filtered ("\n", stream);
00598               print_spaces_filtered (2 + 2 * recurse, stream);
00599             }
00600           else
00601             wrap_here (n_spaces (2 + 2 *recurse));
00602         }
00603 
00604       if (is_map && i % 2 == 0)
00605         fputs_filtered ("[", stream);
00606       else if (is_array)
00607         {
00608           /* We print the index, not whatever the child method
00609              returned as the name.  */
00610           if (options->print_array_indexes)
00611             fprintf_filtered (stream, "[%d] = ", i);
00612         }
00613       else if (! is_map)
00614         {
00615           fputs_filtered (name, stream);
00616           fputs_filtered (" = ", stream);
00617         }
00618 
00619       if (gdbpy_is_lazy_string (py_v))
00620         {
00621           CORE_ADDR addr;
00622           struct type *type;
00623           long length;
00624           char *encoding = NULL;
00625           struct value_print_options local_opts = *options;
00626 
00627           make_cleanup (free_current_contents, &encoding);
00628           gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
00629 
00630           local_opts.addressprint = 0;
00631           val_print_string (type, encoding, addr, (int) length, stream,
00632                             &local_opts);
00633         }
00634       else if (gdbpy_is_string (py_v))
00635         {
00636           char *output;
00637 
00638           output = python_string_to_host_string (py_v);
00639           if (!output)
00640             gdbpy_print_stack ();
00641           else
00642             {
00643               fputs_filtered (output, stream);
00644               xfree (output);
00645             }
00646         }
00647       else
00648         {
00649           struct value *value = convert_value_from_python (py_v);
00650 
00651           if (value == NULL)
00652             {
00653               gdbpy_print_stack ();
00654               error (_("Error while executing Python code."));
00655             }
00656           else
00657             common_val_print (value, stream, recurse + 1, options, language);
00658         }
00659 
00660       if (is_map && i % 2 == 0)
00661         fputs_filtered ("] = ", stream);
00662 
00663       do_cleanups (inner_cleanup);
00664     }
00665 
00666   if (i)
00667     {
00668       if (!done_flag)
00669         {
00670           if (pretty)
00671             {
00672               fputs_filtered ("\n", stream);
00673               print_spaces_filtered (2 + 2 * recurse, stream);
00674             }
00675           fputs_filtered ("...", stream);
00676         }
00677       if (pretty)
00678         {
00679           fputs_filtered ("\n", stream);
00680           print_spaces_filtered (2 * recurse, stream);
00681         }
00682       fputs_filtered ("}", stream);
00683     }
00684 
00685  done:
00686   do_cleanups (cleanups);
00687 }
00688 
00689 int
00690 apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
00691                           int embedded_offset, CORE_ADDR address,
00692                           struct ui_file *stream, int recurse,
00693                           const struct value *val,
00694                           const struct value_print_options *options,
00695                           const struct language_defn *language)
00696 {
00697   struct gdbarch *gdbarch = get_type_arch (type);
00698   PyObject *printer = NULL;
00699   PyObject *val_obj = NULL;
00700   struct value *value;
00701   char *hint = NULL;
00702   struct cleanup *cleanups;
00703   int result = 0;
00704   enum string_repr_result print_result;
00705 
00706   /* No pretty-printer support for unavailable values.  */
00707   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
00708     return 0;
00709 
00710   if (!gdb_python_initialized)
00711     return 0;
00712 
00713   cleanups = ensure_python_env (gdbarch, language);
00714 
00715   /* Instantiate the printer.  */
00716   if (valaddr)
00717     valaddr += embedded_offset;
00718   value = value_from_contents_and_address (type, valaddr,
00719                                            address + embedded_offset);
00720 
00721   set_value_component_location (value, val);
00722   /* set_value_component_location resets the address, so we may
00723      need to set it again.  */
00724   if (VALUE_LVAL (value) != lval_internalvar
00725       && VALUE_LVAL (value) != lval_internalvar_component
00726       && VALUE_LVAL (value) != lval_computed)
00727     set_value_address (value, address + embedded_offset);
00728 
00729   val_obj = value_to_value_object (value);
00730   if (! val_obj)
00731     goto done;
00732   
00733   /* Find the constructor.  */
00734   printer = find_pretty_printer (val_obj);
00735   Py_DECREF (val_obj);
00736 
00737   if (printer == NULL)
00738     goto done;
00739 
00740   make_cleanup_py_decref (printer);
00741   if (printer == Py_None)
00742     goto done;
00743 
00744   /* If we are printing a map, we want some special formatting.  */
00745   hint = gdbpy_get_display_hint (printer);
00746   make_cleanup (free_current_contents, &hint);
00747 
00748   /* Print the section */
00749   print_result = print_string_repr (printer, hint, stream, recurse,
00750                                     options, language, gdbarch);
00751   if (print_result != string_repr_error)
00752     print_children (printer, hint, stream, recurse, options, language,
00753                     print_result == string_repr_none);
00754 
00755   result = 1;
00756 
00757 
00758  done:
00759   if (PyErr_Occurred ())
00760     print_stack_unless_memory_error (stream);
00761   do_cleanups (cleanups);
00762   return result;
00763 }
00764 
00765 
00766 /* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
00767    print object.  It must have a 'to_string' method (but this is
00768    checked by varobj, not here) which takes no arguments and
00769    returns a string.  The printer will return a value and in the case
00770    of a Python string being returned, this function will return a
00771    PyObject containing the string.  For any other type, *REPLACEMENT is
00772    set to the replacement value and this function returns NULL.  On
00773    error, *REPLACEMENT is set to NULL and this function also returns
00774    NULL.  */
00775 PyObject *
00776 apply_varobj_pretty_printer (PyObject *printer_obj,
00777                              struct value **replacement,
00778                              struct ui_file *stream)
00779 {
00780   PyObject *py_str = NULL;
00781 
00782   *replacement = NULL;
00783   py_str = pretty_print_one_value (printer_obj, replacement);
00784 
00785   if (*replacement == NULL && py_str == NULL)
00786     print_stack_unless_memory_error (stream);
00787 
00788   return py_str;
00789 }
00790 
00791 /* Find a pretty-printer object for the varobj module.  Returns a new
00792    reference to the object if successful; returns NULL if not.  VALUE
00793    is the value for which a printer tests to determine if it 
00794    can pretty-print the value.  */
00795 PyObject *
00796 gdbpy_get_varobj_pretty_printer (struct value *value)
00797 {
00798   PyObject *val_obj;
00799   PyObject *pretty_printer = NULL;
00800   volatile struct gdb_exception except;
00801 
00802   TRY_CATCH (except, RETURN_MASK_ALL)
00803     {
00804       value = value_copy (value);
00805     }
00806   GDB_PY_HANDLE_EXCEPTION (except);
00807   
00808   val_obj = value_to_value_object (value);
00809   if (! val_obj)
00810     return NULL;
00811 
00812   pretty_printer = find_pretty_printer (val_obj);
00813   Py_DECREF (val_obj);
00814   return pretty_printer;
00815 }
00816 
00817 /* A Python function which wraps find_pretty_printer and instantiates
00818    the resulting class.  This accepts a Value argument and returns a
00819    pretty printer instance, or None.  This function is useful as an
00820    argument to the MI command -var-set-visualizer.  */
00821 PyObject *
00822 gdbpy_default_visualizer (PyObject *self, PyObject *args)
00823 {
00824   PyObject *val_obj;
00825   PyObject *cons;
00826   struct value *value;
00827 
00828   if (! PyArg_ParseTuple (args, "O", &val_obj))
00829     return NULL;
00830   value = value_object_to_value (val_obj);
00831   if (! value)
00832     {
00833       PyErr_SetString (PyExc_TypeError, 
00834                        _("Argument must be a gdb.Value."));
00835       return NULL;
00836     }
00837 
00838   cons = find_pretty_printer (val_obj);
00839   return cons;
00840 }
00841 
00842 #else /* HAVE_PYTHON */
00843 
00844 int
00845 apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
00846                           int embedded_offset, CORE_ADDR address,
00847                           struct ui_file *stream, int recurse,
00848                           const struct value *val,
00849                           const struct value_print_options *options,
00850                           const struct language_defn *language)
00851 {
00852   return 0;
00853 }
00854 
00855 #endif /* HAVE_PYTHON */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines