xref: /dragonfly/contrib/gdb-7/gdb/python/py-prettyprint.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
1 /* Python pretty-printing
2 
3    Copyright (C) 2008-2013 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #include "defs.h"
21 #include "exceptions.h"
22 #include "objfiles.h"
23 #include "symtab.h"
24 #include "language.h"
25 #include "valprint.h"
26 
27 #include "python.h"
28 
29 #ifdef HAVE_PYTHON
30 #include "python-internal.h"
31 
32 /* Return type of print_string_repr.  */
33 
34 enum string_repr_result
35   {
36     /* The string method returned None.  */
37     string_repr_none,
38     /* The string method had an error.  */
39     string_repr_error,
40     /* Everything ok.  */
41     string_repr_ok
42   };
43 
44 /* Helper function for find_pretty_printer which iterates over a list,
45    calls each function and inspects output.  This will return a
46    printer object if one recognizes VALUE.  If no printer is found, it
47    will return None.  On error, it will set the Python error and
48    return NULL.  */
49 
50 static PyObject *
search_pp_list(PyObject * list,PyObject * value)51 search_pp_list (PyObject *list, PyObject *value)
52 {
53   Py_ssize_t pp_list_size, list_index;
54   PyObject *function, *printer = NULL;
55 
56   pp_list_size = PyList_Size (list);
57   for (list_index = 0; list_index < pp_list_size; list_index++)
58     {
59       function = PyList_GetItem (list, list_index);
60       if (! function)
61           return NULL;
62 
63       /* Skip if disabled.  */
64       if (PyObject_HasAttr (function, gdbpy_enabled_cst))
65           {
66             PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst);
67             int cmp;
68 
69             if (!attr)
70               return NULL;
71             cmp = PyObject_IsTrue (attr);
72             if (cmp == -1)
73               return NULL;
74 
75             if (!cmp)
76               continue;
77           }
78 
79       printer = PyObject_CallFunctionObjArgs (function, value, NULL);
80       if (! printer)
81           return NULL;
82       else if (printer != Py_None)
83           return printer;
84 
85       Py_DECREF (printer);
86     }
87 
88   Py_RETURN_NONE;
89 }
90 
91 /* Subroutine of find_pretty_printer to simplify it.
92    Look for a pretty-printer to print VALUE in all objfiles.
93    The result is NULL if there's an error and the search should be terminated.
94    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
95    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
96 
97 static PyObject *
find_pretty_printer_from_objfiles(PyObject * value)98 find_pretty_printer_from_objfiles (PyObject *value)
99 {
100   PyObject *pp_list;
101   PyObject *function;
102   struct objfile *obj;
103 
104   ALL_OBJFILES (obj)
105   {
106     PyObject *objf = objfile_to_objfile_object (obj);
107     if (!objf)
108       {
109           /* Ignore the error and continue.  */
110           PyErr_Clear ();
111           continue;
112       }
113 
114     pp_list = objfpy_get_printers (objf, NULL);
115     function = search_pp_list (pp_list, value);
116     Py_XDECREF (pp_list);
117 
118     /* If there is an error in any objfile list, abort the search and exit.  */
119     if (! function)
120       return NULL;
121 
122     if (function != Py_None)
123       return function;
124 
125     Py_DECREF (function);
126   }
127 
128   Py_RETURN_NONE;
129 }
130 
131 /* Subroutine of find_pretty_printer to simplify it.
132    Look for a pretty-printer to print VALUE in the current program space.
133    The result is NULL if there's an error and the search should be terminated.
134    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
135    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
136 
137 static PyObject *
find_pretty_printer_from_progspace(PyObject * value)138 find_pretty_printer_from_progspace (PyObject *value)
139 {
140   PyObject *pp_list;
141   PyObject *function;
142   PyObject *obj = pspace_to_pspace_object (current_program_space);
143 
144   if (!obj)
145     return NULL;
146   pp_list = pspy_get_printers (obj, NULL);
147   function = search_pp_list (pp_list, value);
148   Py_XDECREF (pp_list);
149   return function;
150 }
151 
152 /* Subroutine of find_pretty_printer to simplify it.
153    Look for a pretty-printer to print VALUE in the gdb module.
154    The result is NULL if there's an error and the search should be terminated.
155    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
156    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
157 
158 static PyObject *
find_pretty_printer_from_gdb(PyObject * value)159 find_pretty_printer_from_gdb (PyObject *value)
160 {
161   PyObject *pp_list;
162   PyObject *function;
163 
164   /* Fetch the global pretty printer list.  */
165   if (gdb_python_module == NULL
166       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
167     Py_RETURN_NONE;
168   pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
169   if (pp_list == NULL || ! PyList_Check (pp_list))
170     {
171       Py_XDECREF (pp_list);
172       Py_RETURN_NONE;
173     }
174 
175   function = search_pp_list (pp_list, value);
176   Py_XDECREF (pp_list);
177   return function;
178 }
179 
180 /* Find the pretty-printing constructor function for VALUE.  If no
181    pretty-printer exists, return None.  If one exists, return a new
182    reference.  On error, set the Python error and return NULL.  */
183 
184 static PyObject *
find_pretty_printer(PyObject * value)185 find_pretty_printer (PyObject *value)
186 {
187   PyObject *function;
188 
189   /* Look at the pretty-printer list for each objfile
190      in the current program-space.  */
191   function = find_pretty_printer_from_objfiles (value);
192   if (function == NULL || function != Py_None)
193     return function;
194   Py_DECREF (function);
195 
196   /* Look at the pretty-printer list for the current program-space.  */
197   function = find_pretty_printer_from_progspace (value);
198   if (function == NULL || function != Py_None)
199     return function;
200   Py_DECREF (function);
201 
202   /* Look at the pretty-printer list in the gdb module.  */
203   function = find_pretty_printer_from_gdb (value);
204   return function;
205 }
206 
207 /* Pretty-print a single value, via the printer object PRINTER.
208    If the function returns a string, a PyObject containing the string
209    is returned.  If the function returns Py_NONE that means the pretty
210    printer returned the Python None as a value.  Otherwise, if the
211    function returns a value,  *OUT_VALUE is set to the value, and NULL
212    is returned.  On error, *OUT_VALUE is set to NULL, NULL is
213    returned, with a python exception set.  */
214 
215 static PyObject *
pretty_print_one_value(PyObject * printer,struct value ** out_value)216 pretty_print_one_value (PyObject *printer, struct value **out_value)
217 {
218   volatile struct gdb_exception except;
219   PyObject *result = NULL;
220 
221   *out_value = NULL;
222   TRY_CATCH (except, RETURN_MASK_ALL)
223     {
224       result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
225       if (result)
226           {
227             if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
228                 && result != Py_None)
229               {
230                 *out_value = convert_value_from_python (result);
231                 if (PyErr_Occurred ())
232                     *out_value = NULL;
233                 Py_DECREF (result);
234                 result = NULL;
235               }
236           }
237     }
238 
239   return result;
240 }
241 
242 /* Return the display hint for the object printer, PRINTER.  Return
243    NULL if there is no display_hint method, or if the method did not
244    return a string.  On error, print stack trace and return NULL.  On
245    success, return an xmalloc()d string.  */
246 char *
gdbpy_get_display_hint(PyObject * printer)247 gdbpy_get_display_hint (PyObject *printer)
248 {
249   PyObject *hint;
250   char *result = NULL;
251 
252   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
253     return NULL;
254 
255   hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
256   if (hint)
257     {
258       if (gdbpy_is_string (hint))
259           {
260             result = python_string_to_host_string (hint);
261             if (result == NULL)
262               gdbpy_print_stack ();
263           }
264       Py_DECREF (hint);
265     }
266   else
267     gdbpy_print_stack ();
268 
269   return result;
270 }
271 
272 /* A wrapper for gdbpy_print_stack that ignores MemoryError.  */
273 
274 static void
print_stack_unless_memory_error(struct ui_file * stream)275 print_stack_unless_memory_error (struct ui_file *stream)
276 {
277   if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
278     {
279       struct cleanup *cleanup;
280       PyObject *type, *value, *trace;
281       char *msg;
282 
283       PyErr_Fetch (&type, &value, &trace);
284       cleanup = make_cleanup_py_decref (type);
285       make_cleanup_py_decref (value);
286       make_cleanup_py_decref (trace);
287 
288       msg = gdbpy_exception_to_string (type, value);
289       make_cleanup (xfree, msg);
290 
291       if (msg == NULL || *msg == '\0')
292           fprintf_filtered (stream, _("<error reading variable>"));
293       else
294           fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
295 
296       do_cleanups (cleanup);
297     }
298   else
299     gdbpy_print_stack ();
300 }
301 
302 /* Helper for apply_val_pretty_printer which calls to_string and
303    formats the result.  */
304 
305 static enum string_repr_result
print_string_repr(PyObject * printer,const char * hint,struct ui_file * stream,int recurse,const struct value_print_options * options,const struct language_defn * language,struct gdbarch * gdbarch)306 print_string_repr (PyObject *printer, const char *hint,
307                        struct ui_file *stream, int recurse,
308                        const struct value_print_options *options,
309                        const struct language_defn *language,
310                        struct gdbarch *gdbarch)
311 {
312   struct value *replacement = NULL;
313   PyObject *py_str = NULL;
314   enum string_repr_result result = string_repr_ok;
315 
316   py_str = pretty_print_one_value (printer, &replacement);
317   if (py_str)
318     {
319       struct cleanup *cleanup = make_cleanup_py_decref (py_str);
320 
321       if (py_str == Py_None)
322           result = string_repr_none;
323       else if (gdbpy_is_lazy_string (py_str))
324           {
325             CORE_ADDR addr;
326             long length;
327             struct type *type;
328             char *encoding = NULL;
329             struct value_print_options local_opts = *options;
330 
331             make_cleanup (free_current_contents, &encoding);
332             gdbpy_extract_lazy_string (py_str, &addr, &type,
333                                              &length, &encoding);
334 
335             local_opts.addressprint = 0;
336             val_print_string (type, encoding, addr, (int) length,
337                                   stream, &local_opts);
338           }
339       else
340           {
341             PyObject *string;
342 
343             string = python_string_to_target_python_string (py_str);
344             if (string)
345               {
346                 gdb_byte *output;
347                 long length;
348                 struct type *type;
349 
350                 make_cleanup_py_decref (string);
351 #ifdef IS_PY3K
352                 output = (gdb_byte *) PyBytes_AS_STRING (string);
353                 length = PyBytes_GET_SIZE (string);
354 #else
355                 output = PyString_AsString (string);
356                 length = PyString_Size (string);
357 #endif
358                 type = builtin_type (gdbarch)->builtin_char;
359 
360                 if (hint && !strcmp (hint, "string"))
361                     LA_PRINT_STRING (stream, type, output, length, NULL,
362                                          0, options);
363                 else
364                     fputs_filtered (output, stream);
365               }
366             else
367               {
368                 result = string_repr_error;
369                 print_stack_unless_memory_error (stream);
370               }
371           }
372 
373       do_cleanups (cleanup);
374     }
375   else if (replacement)
376     {
377       struct value_print_options opts = *options;
378 
379       opts.addressprint = 0;
380       common_val_print (replacement, stream, recurse, &opts, language);
381     }
382   else
383     {
384       result = string_repr_error;
385       print_stack_unless_memory_error (stream);
386     }
387 
388   return result;
389 }
390 
391 #ifndef IS_PY3K
392 static void
py_restore_tstate(void * p)393 py_restore_tstate (void *p)
394 {
395   PyFrameObject *frame = p;
396   PyThreadState *tstate = PyThreadState_GET ();
397 
398   tstate->frame = frame;
399 }
400 
401 /* Create a dummy PyFrameObject, needed to work around
402    a Python-2.4 bug with generators.  */
403 static PyObject *
push_dummy_python_frame(void)404 push_dummy_python_frame (void)
405 {
406   PyObject *empty_string, *null_tuple, *globals;
407   PyCodeObject *code;
408   PyFrameObject *frame;
409   PyThreadState *tstate;
410 
411   empty_string = PyString_FromString ("");
412   if (!empty_string)
413     return NULL;
414 
415   null_tuple = PyTuple_New (0);
416   if (!null_tuple)
417     {
418       Py_DECREF (empty_string);
419       return NULL;
420     }
421 
422   code = PyCode_New (0,                           /* argcount */
423                          0,                       /* nlocals */
424                          0,                       /* stacksize */
425                          0,                       /* flags */
426                          empty_string,  /* code */
427                          null_tuple,    /* consts */
428                          null_tuple,    /* names */
429                          null_tuple,    /* varnames */
430 #if PYTHON_API_VERSION >= 1010
431                          null_tuple,    /* freevars */
432                          null_tuple,    /* cellvars */
433 #endif
434                          empty_string,  /* filename */
435                          empty_string,  /* name */
436                          1,                       /* firstlineno */
437                          empty_string   /* lnotab */
438                         );
439 
440   Py_DECREF (empty_string);
441   Py_DECREF (null_tuple);
442 
443   if (!code)
444     return NULL;
445 
446   globals = PyDict_New ();
447   if (!globals)
448     {
449       Py_DECREF (code);
450       return NULL;
451     }
452 
453   tstate = PyThreadState_GET ();
454 
455   frame = PyFrame_New (tstate, code, globals, NULL);
456 
457   Py_DECREF (globals);
458   Py_DECREF (code);
459 
460   if (!frame)
461     return NULL;
462 
463   tstate->frame = frame;
464   make_cleanup (py_restore_tstate, frame->f_back);
465   return (PyObject *) frame;
466 }
467 #endif
468 
469 /* Helper for apply_val_pretty_printer that formats children of the
470    printer, if any exist.  If is_py_none is true, then nothing has
471    been printed by to_string, and format output accordingly. */
472 static void
print_children(PyObject * printer,const char * hint,struct ui_file * stream,int recurse,const struct value_print_options * options,const struct language_defn * language,int is_py_none)473 print_children (PyObject *printer, const char *hint,
474                     struct ui_file *stream, int recurse,
475                     const struct value_print_options *options,
476                     const struct language_defn *language,
477                     int is_py_none)
478 {
479   int is_map, is_array, done_flag, pretty;
480   unsigned int i;
481   PyObject *children, *iter;
482 #ifndef IS_PY3K
483   PyObject *frame;
484 #endif
485   struct cleanup *cleanups;
486 
487   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
488     return;
489 
490   /* If we are printing a map or an array, we want some special
491      formatting.  */
492   is_map = hint && ! strcmp (hint, "map");
493   is_array = hint && ! strcmp (hint, "array");
494 
495   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
496                                                    NULL);
497   if (! children)
498     {
499       print_stack_unless_memory_error (stream);
500       return;
501     }
502 
503   cleanups = make_cleanup_py_decref (children);
504 
505   iter = PyObject_GetIter (children);
506   if (!iter)
507     {
508       print_stack_unless_memory_error (stream);
509       goto done;
510     }
511   make_cleanup_py_decref (iter);
512 
513   /* Use the prettyprint_arrays option if we are printing an array,
514      and the pretty option otherwise.  */
515   if (is_array)
516     pretty = options->prettyprint_arrays;
517   else
518     {
519       if (options->pretty == Val_prettyprint)
520           pretty = 1;
521       else
522           pretty = options->prettyprint_structs;
523     }
524 
525   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
526      where it insists on having a non-NULL tstate->frame when
527      a generator is called.  */
528 #ifndef IS_PY3K
529   frame = push_dummy_python_frame ();
530   if (!frame)
531     {
532       gdbpy_print_stack ();
533       goto done;
534     }
535   make_cleanup_py_decref (frame);
536 #endif
537 
538   done_flag = 0;
539   for (i = 0; i < options->print_max; ++i)
540     {
541       PyObject *py_v, *item = PyIter_Next (iter);
542       const char *name;
543       struct cleanup *inner_cleanup;
544 
545       if (! item)
546           {
547             if (PyErr_Occurred ())
548               print_stack_unless_memory_error (stream);
549             /* Set a flag so we can know whether we printed all the
550                available elements.  */
551             else
552               done_flag = 1;
553             break;
554           }
555 
556       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
557           {
558             gdbpy_print_stack ();
559             Py_DECREF (item);
560             continue;
561           }
562       inner_cleanup = make_cleanup_py_decref (item);
563 
564       /* Print initial "{".  For other elements, there are three
565            cases:
566            1. Maps.  Print a "," after each value element.
567            2. Arrays.  Always print a ",".
568            3. Other.  Always print a ",".  */
569       if (i == 0)
570           {
571          if (is_py_none)
572            fputs_filtered ("{", stream);
573          else
574            fputs_filtered (" = {", stream);
575        }
576 
577       else if (! is_map || i % 2 == 0)
578           fputs_filtered (pretty ? "," : ", ", stream);
579 
580       /* In summary mode, we just want to print "= {...}" if there is
581            a value.  */
582       if (options->summary)
583           {
584             /* This increment tricks the post-loop logic to print what
585                we want.  */
586             ++i;
587             /* Likewise.  */
588             pretty = 0;
589             break;
590           }
591 
592       if (! is_map || i % 2 == 0)
593           {
594             if (pretty)
595               {
596                 fputs_filtered ("\n", stream);
597                 print_spaces_filtered (2 + 2 * recurse, stream);
598               }
599             else
600               wrap_here (n_spaces (2 + 2 *recurse));
601           }
602 
603       if (is_map && i % 2 == 0)
604           fputs_filtered ("[", stream);
605       else if (is_array)
606           {
607             /* We print the index, not whatever the child method
608                returned as the name.  */
609             if (options->print_array_indexes)
610               fprintf_filtered (stream, "[%d] = ", i);
611           }
612       else if (! is_map)
613           {
614             fputs_filtered (name, stream);
615             fputs_filtered (" = ", stream);
616           }
617 
618       if (gdbpy_is_lazy_string (py_v))
619           {
620             CORE_ADDR addr;
621             struct type *type;
622             long length;
623             char *encoding = NULL;
624             struct value_print_options local_opts = *options;
625 
626             make_cleanup (free_current_contents, &encoding);
627             gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
628 
629             local_opts.addressprint = 0;
630             val_print_string (type, encoding, addr, (int) length, stream,
631                                   &local_opts);
632 
633             do_cleanups (inner_cleanup);
634           }
635       else if (gdbpy_is_string (py_v))
636           {
637             gdb_byte *output;
638 
639             output = python_string_to_host_string (py_v);
640             if (!output)
641               gdbpy_print_stack ();
642             else
643               {
644                 fputs_filtered (output, stream);
645                 xfree (output);
646               }
647           }
648       else
649           {
650             struct value *value = convert_value_from_python (py_v);
651 
652             if (value == NULL)
653               {
654                 gdbpy_print_stack ();
655                 error (_("Error while executing Python code."));
656               }
657             else
658               common_val_print (value, stream, recurse + 1, options, language);
659           }
660 
661       if (is_map && i % 2 == 0)
662           fputs_filtered ("] = ", stream);
663 
664       do_cleanups (inner_cleanup);
665     }
666 
667   if (i)
668     {
669       if (!done_flag)
670           {
671             if (pretty)
672               {
673                 fputs_filtered ("\n", stream);
674                 print_spaces_filtered (2 + 2 * recurse, stream);
675               }
676             fputs_filtered ("...", stream);
677           }
678       if (pretty)
679           {
680             fputs_filtered ("\n", stream);
681             print_spaces_filtered (2 * recurse, stream);
682           }
683       fputs_filtered ("}", stream);
684     }
685 
686  done:
687   do_cleanups (cleanups);
688 }
689 
690 int
apply_val_pretty_printer(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options,const struct language_defn * language)691 apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
692                                 int embedded_offset, CORE_ADDR address,
693                                 struct ui_file *stream, int recurse,
694                                 const struct value *val,
695                                 const struct value_print_options *options,
696                                 const struct language_defn *language)
697 {
698   struct gdbarch *gdbarch = get_type_arch (type);
699   PyObject *printer = NULL;
700   PyObject *val_obj = NULL;
701   struct value *value;
702   char *hint = NULL;
703   struct cleanup *cleanups;
704   int result = 0;
705   enum string_repr_result print_result;
706 
707   /* No pretty-printer support for unavailable values.  */
708   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
709     return 0;
710 
711   cleanups = ensure_python_env (gdbarch, language);
712 
713   /* Instantiate the printer.  */
714   if (valaddr)
715     valaddr += embedded_offset;
716   value = value_from_contents_and_address (type, valaddr,
717                                                      address + embedded_offset);
718 
719   set_value_component_location (value, val);
720   /* set_value_component_location resets the address, so we may
721      need to set it again.  */
722   if (VALUE_LVAL (value) != lval_internalvar
723       && VALUE_LVAL (value) != lval_internalvar_component
724       && VALUE_LVAL (value) != lval_computed)
725     set_value_address (value, address + embedded_offset);
726 
727   val_obj = value_to_value_object (value);
728   if (! val_obj)
729     goto done;
730 
731   /* Find the constructor.  */
732   printer = find_pretty_printer (val_obj);
733   Py_DECREF (val_obj);
734   make_cleanup_py_decref (printer);
735   if (! printer || printer == Py_None)
736     goto done;
737 
738   /* If we are printing a map, we want some special formatting.  */
739   hint = gdbpy_get_display_hint (printer);
740   make_cleanup (free_current_contents, &hint);
741 
742   /* Print the section */
743   print_result = print_string_repr (printer, hint, stream, recurse,
744                                             options, language, gdbarch);
745   if (print_result != string_repr_error)
746     print_children (printer, hint, stream, recurse, options, language,
747                         print_result == string_repr_none);
748 
749   result = 1;
750 
751 
752  done:
753   if (PyErr_Occurred ())
754     print_stack_unless_memory_error (stream);
755   do_cleanups (cleanups);
756   return result;
757 }
758 
759 
760 /* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
761    print object.  It must have a 'to_string' method (but this is
762    checked by varobj, not here) which takes no arguments and
763    returns a string.  The printer will return a value and in the case
764    of a Python string being returned, this function will return a
765    PyObject containing the string.  For any other type, *REPLACEMENT is
766    set to the replacement value and this function returns NULL.  On
767    error, *REPLACEMENT is set to NULL and this function also returns
768    NULL.  */
769 PyObject *
apply_varobj_pretty_printer(PyObject * printer_obj,struct value ** replacement,struct ui_file * stream)770 apply_varobj_pretty_printer (PyObject *printer_obj,
771                                    struct value **replacement,
772                                    struct ui_file *stream)
773 {
774   PyObject *py_str = NULL;
775 
776   *replacement = NULL;
777   py_str = pretty_print_one_value (printer_obj, replacement);
778 
779   if (*replacement == NULL && py_str == NULL)
780     print_stack_unless_memory_error (stream);
781 
782   return py_str;
783 }
784 
785 /* Find a pretty-printer object for the varobj module.  Returns a new
786    reference to the object if successful; returns NULL if not.  VALUE
787    is the value for which a printer tests to determine if it
788    can pretty-print the value.  */
789 PyObject *
gdbpy_get_varobj_pretty_printer(struct value * value)790 gdbpy_get_varobj_pretty_printer (struct value *value)
791 {
792   PyObject *val_obj;
793   PyObject *pretty_printer = NULL;
794   volatile struct gdb_exception except;
795 
796   TRY_CATCH (except, RETURN_MASK_ALL)
797     {
798       value = value_copy (value);
799     }
800   GDB_PY_HANDLE_EXCEPTION (except);
801 
802   val_obj = value_to_value_object (value);
803   if (! val_obj)
804     return NULL;
805 
806   pretty_printer = find_pretty_printer (val_obj);
807   Py_DECREF (val_obj);
808   return pretty_printer;
809 }
810 
811 /* A Python function which wraps find_pretty_printer and instantiates
812    the resulting class.  This accepts a Value argument and returns a
813    pretty printer instance, or None.  This function is useful as an
814    argument to the MI command -var-set-visualizer.  */
815 PyObject *
gdbpy_default_visualizer(PyObject * self,PyObject * args)816 gdbpy_default_visualizer (PyObject *self, PyObject *args)
817 {
818   PyObject *val_obj;
819   PyObject *cons;
820   struct value *value;
821 
822   if (! PyArg_ParseTuple (args, "O", &val_obj))
823     return NULL;
824   value = value_object_to_value (val_obj);
825   if (! value)
826     {
827       PyErr_SetString (PyExc_TypeError,
828                            _("Argument must be a gdb.Value."));
829       return NULL;
830     }
831 
832   cons = find_pretty_printer (val_obj);
833   return cons;
834 }
835 
836 #else /* HAVE_PYTHON */
837 
838 int
apply_val_pretty_printer(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options,const struct language_defn * language)839 apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
840                                 int embedded_offset, CORE_ADDR address,
841                                 struct ui_file *stream, int recurse,
842                                 const struct value *val,
843                                 const struct value_print_options *options,
844                                 const struct language_defn *language)
845 {
846   return 0;
847 }
848 
849 #endif /* HAVE_PYTHON */
850