15796c8dcSSimon Schubert /* Python pretty-printing
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2008-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "exceptions.h"
225796c8dcSSimon Schubert #include "objfiles.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "language.h"
255796c8dcSSimon Schubert #include "valprint.h"
265796c8dcSSimon Schubert
275796c8dcSSimon Schubert #include "python.h"
285796c8dcSSimon Schubert
295796c8dcSSimon Schubert #ifdef HAVE_PYTHON
305796c8dcSSimon Schubert #include "python-internal.h"
315796c8dcSSimon Schubert
32c50c785cSJohn Marino /* Return type of print_string_repr. */
33c50c785cSJohn Marino
34c50c785cSJohn Marino enum string_repr_result
35c50c785cSJohn Marino {
36c50c785cSJohn Marino /* The string method returned None. */
37c50c785cSJohn Marino string_repr_none,
38c50c785cSJohn Marino /* The string method had an error. */
39c50c785cSJohn Marino string_repr_error,
40c50c785cSJohn Marino /* Everything ok. */
41c50c785cSJohn Marino string_repr_ok
42c50c785cSJohn Marino };
43c50c785cSJohn Marino
445796c8dcSSimon Schubert /* Helper function for find_pretty_printer which iterates over a list,
455796c8dcSSimon Schubert calls each function and inspects output. This will return a
465796c8dcSSimon Schubert printer object if one recognizes VALUE. If no printer is found, it
475796c8dcSSimon Schubert will return None. On error, it will set the Python error and
485796c8dcSSimon Schubert return NULL. */
49cf7f2e2dSJohn Marino
505796c8dcSSimon Schubert static PyObject *
search_pp_list(PyObject * list,PyObject * value)515796c8dcSSimon Schubert search_pp_list (PyObject *list, PyObject *value)
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert Py_ssize_t pp_list_size, list_index;
545796c8dcSSimon Schubert PyObject *function, *printer = NULL;
555796c8dcSSimon Schubert
565796c8dcSSimon Schubert pp_list_size = PyList_Size (list);
575796c8dcSSimon Schubert for (list_index = 0; list_index < pp_list_size; list_index++)
585796c8dcSSimon Schubert {
595796c8dcSSimon Schubert function = PyList_GetItem (list, list_index);
605796c8dcSSimon Schubert if (! function)
615796c8dcSSimon Schubert return NULL;
625796c8dcSSimon Schubert
63cf7f2e2dSJohn Marino /* Skip if disabled. */
64c50c785cSJohn Marino if (PyObject_HasAttr (function, gdbpy_enabled_cst))
65c50c785cSJohn Marino {
66c50c785cSJohn Marino PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst);
67c50c785cSJohn Marino int cmp;
68c50c785cSJohn Marino
69c50c785cSJohn Marino if (!attr)
70c50c785cSJohn Marino return NULL;
71c50c785cSJohn Marino cmp = PyObject_IsTrue (attr);
72c50c785cSJohn Marino if (cmp == -1)
73c50c785cSJohn Marino return NULL;
74c50c785cSJohn Marino
75c50c785cSJohn Marino if (!cmp)
76cf7f2e2dSJohn Marino continue;
77c50c785cSJohn Marino }
78cf7f2e2dSJohn Marino
795796c8dcSSimon Schubert printer = PyObject_CallFunctionObjArgs (function, value, NULL);
805796c8dcSSimon Schubert if (! printer)
815796c8dcSSimon Schubert return NULL;
825796c8dcSSimon Schubert else if (printer != Py_None)
835796c8dcSSimon Schubert return printer;
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert Py_DECREF (printer);
865796c8dcSSimon Schubert }
875796c8dcSSimon Schubert
885796c8dcSSimon Schubert Py_RETURN_NONE;
895796c8dcSSimon Schubert }
905796c8dcSSimon Schubert
91cf7f2e2dSJohn Marino /* Subroutine of find_pretty_printer to simplify it.
92cf7f2e2dSJohn Marino Look for a pretty-printer to print VALUE in all objfiles.
93cf7f2e2dSJohn Marino The result is NULL if there's an error and the search should be terminated.
94cf7f2e2dSJohn Marino The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
95cf7f2e2dSJohn Marino Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
965796c8dcSSimon Schubert
97cf7f2e2dSJohn Marino static PyObject *
find_pretty_printer_from_objfiles(PyObject * value)98cf7f2e2dSJohn Marino find_pretty_printer_from_objfiles (PyObject *value)
99cf7f2e2dSJohn Marino {
100cf7f2e2dSJohn Marino PyObject *pp_list;
101cf7f2e2dSJohn Marino PyObject *function;
102cf7f2e2dSJohn Marino struct objfile *obj;
103cf7f2e2dSJohn Marino
1045796c8dcSSimon Schubert ALL_OBJFILES (obj)
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert PyObject *objf = objfile_to_objfile_object (obj);
1075796c8dcSSimon Schubert if (!objf)
1085796c8dcSSimon Schubert {
1095796c8dcSSimon Schubert /* Ignore the error and continue. */
1105796c8dcSSimon Schubert PyErr_Clear ();
1115796c8dcSSimon Schubert continue;
1125796c8dcSSimon Schubert }
1135796c8dcSSimon Schubert
1145796c8dcSSimon Schubert pp_list = objfpy_get_printers (objf, NULL);
1155796c8dcSSimon Schubert function = search_pp_list (pp_list, value);
1165796c8dcSSimon Schubert Py_XDECREF (pp_list);
117cf7f2e2dSJohn Marino
118cf7f2e2dSJohn Marino /* If there is an error in any objfile list, abort the search and exit. */
119cf7f2e2dSJohn Marino if (! function)
1205796c8dcSSimon Schubert return NULL;
1215796c8dcSSimon Schubert
1225796c8dcSSimon Schubert if (function != Py_None)
123cf7f2e2dSJohn Marino return function;
1245796c8dcSSimon Schubert
1255796c8dcSSimon Schubert Py_DECREF (function);
1265796c8dcSSimon Schubert }
1275796c8dcSSimon Schubert
128cf7f2e2dSJohn Marino Py_RETURN_NONE;
129cf7f2e2dSJohn Marino }
130cf7f2e2dSJohn Marino
131cf7f2e2dSJohn Marino /* Subroutine of find_pretty_printer to simplify it.
132cf7f2e2dSJohn Marino Look for a pretty-printer to print VALUE in the current program space.
133cf7f2e2dSJohn Marino The result is NULL if there's an error and the search should be terminated.
134cf7f2e2dSJohn Marino The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
135cf7f2e2dSJohn Marino Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
136cf7f2e2dSJohn Marino
137cf7f2e2dSJohn Marino static PyObject *
find_pretty_printer_from_progspace(PyObject * value)138cf7f2e2dSJohn Marino find_pretty_printer_from_progspace (PyObject *value)
1395796c8dcSSimon Schubert {
140cf7f2e2dSJohn Marino PyObject *pp_list;
141cf7f2e2dSJohn Marino PyObject *function;
142cf7f2e2dSJohn Marino PyObject *obj = pspace_to_pspace_object (current_program_space);
1435796c8dcSSimon Schubert
144cf7f2e2dSJohn Marino if (!obj)
145cf7f2e2dSJohn Marino return NULL;
146cf7f2e2dSJohn Marino pp_list = pspy_get_printers (obj, NULL);
1475796c8dcSSimon Schubert function = search_pp_list (pp_list, value);
1485796c8dcSSimon Schubert Py_XDECREF (pp_list);
1495796c8dcSSimon Schubert return function;
1505796c8dcSSimon Schubert }
151cf7f2e2dSJohn Marino
152cf7f2e2dSJohn Marino /* Subroutine of find_pretty_printer to simplify it.
153cf7f2e2dSJohn Marino Look for a pretty-printer to print VALUE in the gdb module.
154cf7f2e2dSJohn Marino The result is NULL if there's an error and the search should be terminated.
155cf7f2e2dSJohn Marino The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
156cf7f2e2dSJohn Marino Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
157cf7f2e2dSJohn Marino
158cf7f2e2dSJohn Marino static PyObject *
find_pretty_printer_from_gdb(PyObject * value)159cf7f2e2dSJohn Marino find_pretty_printer_from_gdb (PyObject *value)
160cf7f2e2dSJohn Marino {
161cf7f2e2dSJohn Marino PyObject *pp_list;
162cf7f2e2dSJohn Marino PyObject *function;
163cf7f2e2dSJohn Marino
164c50c785cSJohn Marino /* Fetch the global pretty printer list. */
165*ef5ccd6cSJohn Marino if (gdb_python_module == NULL
166*ef5ccd6cSJohn Marino || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
167cf7f2e2dSJohn Marino Py_RETURN_NONE;
168*ef5ccd6cSJohn Marino pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
169cf7f2e2dSJohn Marino if (pp_list == NULL || ! PyList_Check (pp_list))
170cf7f2e2dSJohn Marino {
171cf7f2e2dSJohn Marino Py_XDECREF (pp_list);
172cf7f2e2dSJohn Marino Py_RETURN_NONE;
173cf7f2e2dSJohn Marino }
174cf7f2e2dSJohn Marino
175cf7f2e2dSJohn Marino function = search_pp_list (pp_list, value);
176cf7f2e2dSJohn Marino Py_XDECREF (pp_list);
177cf7f2e2dSJohn Marino return function;
178cf7f2e2dSJohn Marino }
179cf7f2e2dSJohn Marino
180cf7f2e2dSJohn Marino /* Find the pretty-printing constructor function for VALUE. If no
181cf7f2e2dSJohn Marino pretty-printer exists, return None. If one exists, return a new
182cf7f2e2dSJohn Marino reference. On error, set the Python error and return NULL. */
183cf7f2e2dSJohn Marino
184cf7f2e2dSJohn Marino static PyObject *
find_pretty_printer(PyObject * value)185cf7f2e2dSJohn Marino find_pretty_printer (PyObject *value)
186cf7f2e2dSJohn Marino {
187cf7f2e2dSJohn Marino PyObject *function;
188cf7f2e2dSJohn Marino
189c50c785cSJohn Marino /* Look at the pretty-printer list for each objfile
190cf7f2e2dSJohn Marino in the current program-space. */
191cf7f2e2dSJohn Marino function = find_pretty_printer_from_objfiles (value);
192cf7f2e2dSJohn Marino if (function == NULL || function != Py_None)
193cf7f2e2dSJohn Marino return function;
194cf7f2e2dSJohn Marino Py_DECREF (function);
195cf7f2e2dSJohn Marino
196c50c785cSJohn Marino /* Look at the pretty-printer list for the current program-space. */
197cf7f2e2dSJohn Marino function = find_pretty_printer_from_progspace (value);
198cf7f2e2dSJohn Marino if (function == NULL || function != Py_None)
199cf7f2e2dSJohn Marino return function;
200cf7f2e2dSJohn Marino Py_DECREF (function);
201cf7f2e2dSJohn Marino
202c50c785cSJohn Marino /* Look at the pretty-printer list in the gdb module. */
203cf7f2e2dSJohn Marino function = find_pretty_printer_from_gdb (value);
204cf7f2e2dSJohn Marino return function;
205cf7f2e2dSJohn Marino }
206cf7f2e2dSJohn Marino
2075796c8dcSSimon Schubert /* Pretty-print a single value, via the printer object PRINTER.
2085796c8dcSSimon Schubert If the function returns a string, a PyObject containing the string
209cf7f2e2dSJohn Marino is returned. If the function returns Py_NONE that means the pretty
210cf7f2e2dSJohn Marino printer returned the Python None as a value. Otherwise, if the
211cf7f2e2dSJohn Marino function returns a value, *OUT_VALUE is set to the value, and NULL
212c50c785cSJohn Marino is returned. On error, *OUT_VALUE is set to NULL, NULL is
213c50c785cSJohn Marino returned, with a python exception set. */
214cf7f2e2dSJohn Marino
2155796c8dcSSimon Schubert static PyObject *
pretty_print_one_value(PyObject * printer,struct value ** out_value)2165796c8dcSSimon Schubert pretty_print_one_value (PyObject *printer, struct value **out_value)
2175796c8dcSSimon Schubert {
2185796c8dcSSimon Schubert volatile struct gdb_exception except;
2195796c8dcSSimon Schubert PyObject *result = NULL;
2205796c8dcSSimon Schubert
2215796c8dcSSimon Schubert *out_value = NULL;
2225796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ALL)
2235796c8dcSSimon Schubert {
2245796c8dcSSimon Schubert result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
2255796c8dcSSimon Schubert if (result)
2265796c8dcSSimon Schubert {
227cf7f2e2dSJohn Marino if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
228cf7f2e2dSJohn Marino && result != Py_None)
2295796c8dcSSimon Schubert {
2305796c8dcSSimon Schubert *out_value = convert_value_from_python (result);
2315796c8dcSSimon Schubert if (PyErr_Occurred ())
2325796c8dcSSimon Schubert *out_value = NULL;
2335796c8dcSSimon Schubert Py_DECREF (result);
2345796c8dcSSimon Schubert result = NULL;
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert }
2375796c8dcSSimon Schubert }
2385796c8dcSSimon Schubert
2395796c8dcSSimon Schubert return result;
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert
2425796c8dcSSimon Schubert /* Return the display hint for the object printer, PRINTER. Return
2435796c8dcSSimon Schubert NULL if there is no display_hint method, or if the method did not
2445796c8dcSSimon Schubert return a string. On error, print stack trace and return NULL. On
2455796c8dcSSimon Schubert success, return an xmalloc()d string. */
2465796c8dcSSimon Schubert char *
gdbpy_get_display_hint(PyObject * printer)2475796c8dcSSimon Schubert gdbpy_get_display_hint (PyObject *printer)
2485796c8dcSSimon Schubert {
2495796c8dcSSimon Schubert PyObject *hint;
2505796c8dcSSimon Schubert char *result = NULL;
2515796c8dcSSimon Schubert
2525796c8dcSSimon Schubert if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
2535796c8dcSSimon Schubert return NULL;
2545796c8dcSSimon Schubert
2555796c8dcSSimon Schubert hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
256cf7f2e2dSJohn Marino if (hint)
257cf7f2e2dSJohn Marino {
2585796c8dcSSimon Schubert if (gdbpy_is_string (hint))
259c50c785cSJohn Marino {
2605796c8dcSSimon Schubert result = python_string_to_host_string (hint);
261c50c785cSJohn Marino if (result == NULL)
262c50c785cSJohn Marino gdbpy_print_stack ();
263c50c785cSJohn Marino }
2645796c8dcSSimon Schubert Py_DECREF (hint);
265cf7f2e2dSJohn Marino }
2665796c8dcSSimon Schubert else
2675796c8dcSSimon Schubert gdbpy_print_stack ();
2685796c8dcSSimon Schubert
2695796c8dcSSimon Schubert return result;
2705796c8dcSSimon Schubert }
2715796c8dcSSimon Schubert
272c50c785cSJohn Marino /* A wrapper for gdbpy_print_stack that ignores MemoryError. */
273c50c785cSJohn Marino
274c50c785cSJohn Marino static void
print_stack_unless_memory_error(struct ui_file * stream)275c50c785cSJohn Marino print_stack_unless_memory_error (struct ui_file *stream)
276c50c785cSJohn Marino {
277c50c785cSJohn Marino if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
278c50c785cSJohn Marino {
279c50c785cSJohn Marino struct cleanup *cleanup;
280c50c785cSJohn Marino PyObject *type, *value, *trace;
281c50c785cSJohn Marino char *msg;
282c50c785cSJohn Marino
283c50c785cSJohn Marino PyErr_Fetch (&type, &value, &trace);
284c50c785cSJohn Marino cleanup = make_cleanup_py_decref (type);
285c50c785cSJohn Marino make_cleanup_py_decref (value);
286c50c785cSJohn Marino make_cleanup_py_decref (trace);
287c50c785cSJohn Marino
288c50c785cSJohn Marino msg = gdbpy_exception_to_string (type, value);
289c50c785cSJohn Marino make_cleanup (xfree, msg);
290c50c785cSJohn Marino
291c50c785cSJohn Marino if (msg == NULL || *msg == '\0')
292c50c785cSJohn Marino fprintf_filtered (stream, _("<error reading variable>"));
293c50c785cSJohn Marino else
294c50c785cSJohn Marino fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
295c50c785cSJohn Marino
296c50c785cSJohn Marino do_cleanups (cleanup);
297c50c785cSJohn Marino }
298c50c785cSJohn Marino else
299c50c785cSJohn Marino gdbpy_print_stack ();
300c50c785cSJohn Marino }
301c50c785cSJohn Marino
3025796c8dcSSimon Schubert /* Helper for apply_val_pretty_printer which calls to_string and
303c50c785cSJohn Marino formats the result. */
304c50c785cSJohn Marino
305c50c785cSJohn Marino 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)3065796c8dcSSimon Schubert print_string_repr (PyObject *printer, const char *hint,
3075796c8dcSSimon Schubert struct ui_file *stream, int recurse,
3085796c8dcSSimon Schubert const struct value_print_options *options,
3095796c8dcSSimon Schubert const struct language_defn *language,
3105796c8dcSSimon Schubert struct gdbarch *gdbarch)
3115796c8dcSSimon Schubert {
3125796c8dcSSimon Schubert struct value *replacement = NULL;
3135796c8dcSSimon Schubert PyObject *py_str = NULL;
314c50c785cSJohn Marino enum string_repr_result result = string_repr_ok;
3155796c8dcSSimon Schubert
3165796c8dcSSimon Schubert py_str = pretty_print_one_value (printer, &replacement);
3175796c8dcSSimon Schubert if (py_str)
3185796c8dcSSimon Schubert {
319c50c785cSJohn Marino struct cleanup *cleanup = make_cleanup_py_decref (py_str);
320c50c785cSJohn Marino
321cf7f2e2dSJohn Marino if (py_str == Py_None)
322c50c785cSJohn Marino result = string_repr_none;
323c50c785cSJohn Marino else if (gdbpy_is_lazy_string (py_str))
324cf7f2e2dSJohn Marino {
325c50c785cSJohn Marino CORE_ADDR addr;
326cf7f2e2dSJohn Marino long length;
327cf7f2e2dSJohn Marino struct type *type;
328cf7f2e2dSJohn Marino char *encoding = NULL;
329c50c785cSJohn Marino struct value_print_options local_opts = *options;
330cf7f2e2dSJohn Marino
331c50c785cSJohn Marino make_cleanup (free_current_contents, &encoding);
332c50c785cSJohn Marino gdbpy_extract_lazy_string (py_str, &addr, &type,
333c50c785cSJohn Marino &length, &encoding);
334c50c785cSJohn Marino
335c50c785cSJohn Marino local_opts.addressprint = 0;
336c50c785cSJohn Marino val_print_string (type, encoding, addr, (int) length,
337c50c785cSJohn Marino stream, &local_opts);
338c50c785cSJohn Marino }
339cf7f2e2dSJohn Marino else
340cf7f2e2dSJohn Marino {
341c50c785cSJohn Marino PyObject *string;
342c50c785cSJohn Marino
343cf7f2e2dSJohn Marino string = python_string_to_target_python_string (py_str);
3445796c8dcSSimon Schubert if (string)
3455796c8dcSSimon Schubert {
346c50c785cSJohn Marino gdb_byte *output;
347c50c785cSJohn Marino long length;
348c50c785cSJohn Marino struct type *type;
349c50c785cSJohn Marino
350c50c785cSJohn Marino make_cleanup_py_decref (string);
351*ef5ccd6cSJohn Marino #ifdef IS_PY3K
352*ef5ccd6cSJohn Marino output = (gdb_byte *) PyBytes_AS_STRING (string);
353*ef5ccd6cSJohn Marino length = PyBytes_GET_SIZE (string);
354*ef5ccd6cSJohn Marino #else
355cf7f2e2dSJohn Marino output = PyString_AsString (string);
356cf7f2e2dSJohn Marino length = PyString_Size (string);
357*ef5ccd6cSJohn Marino #endif
358cf7f2e2dSJohn Marino type = builtin_type (gdbarch)->builtin_char;
3595796c8dcSSimon Schubert
360c50c785cSJohn Marino if (hint && !strcmp (hint, "string"))
361c50c785cSJohn Marino LA_PRINT_STRING (stream, type, output, length, NULL,
362cf7f2e2dSJohn Marino 0, options);
3635796c8dcSSimon Schubert else
3645796c8dcSSimon Schubert fputs_filtered (output, stream);
3655796c8dcSSimon Schubert }
3665796c8dcSSimon Schubert else
367c50c785cSJohn Marino {
368c50c785cSJohn Marino result = string_repr_error;
369c50c785cSJohn Marino print_stack_unless_memory_error (stream);
3705796c8dcSSimon Schubert }
371cf7f2e2dSJohn Marino }
372c50c785cSJohn Marino
373c50c785cSJohn Marino do_cleanups (cleanup);
374c50c785cSJohn Marino }
3755796c8dcSSimon Schubert else if (replacement)
376cf7f2e2dSJohn Marino {
377cf7f2e2dSJohn Marino struct value_print_options opts = *options;
378cf7f2e2dSJohn Marino
379cf7f2e2dSJohn Marino opts.addressprint = 0;
380cf7f2e2dSJohn Marino common_val_print (replacement, stream, recurse, &opts, language);
381cf7f2e2dSJohn Marino }
3825796c8dcSSimon Schubert else
383c50c785cSJohn Marino {
384c50c785cSJohn Marino result = string_repr_error;
385c50c785cSJohn Marino print_stack_unless_memory_error (stream);
386c50c785cSJohn Marino }
387cf7f2e2dSJohn Marino
388c50c785cSJohn Marino return result;
3895796c8dcSSimon Schubert }
3905796c8dcSSimon Schubert
391*ef5ccd6cSJohn Marino #ifndef IS_PY3K
3925796c8dcSSimon Schubert static void
py_restore_tstate(void * p)3935796c8dcSSimon Schubert py_restore_tstate (void *p)
3945796c8dcSSimon Schubert {
3955796c8dcSSimon Schubert PyFrameObject *frame = p;
3965796c8dcSSimon Schubert PyThreadState *tstate = PyThreadState_GET ();
397cf7f2e2dSJohn Marino
3985796c8dcSSimon Schubert tstate->frame = frame;
3995796c8dcSSimon Schubert }
4005796c8dcSSimon Schubert
4015796c8dcSSimon Schubert /* Create a dummy PyFrameObject, needed to work around
4025796c8dcSSimon Schubert a Python-2.4 bug with generators. */
4035796c8dcSSimon Schubert static PyObject *
push_dummy_python_frame(void)404c50c785cSJohn Marino push_dummy_python_frame (void)
4055796c8dcSSimon Schubert {
4065796c8dcSSimon Schubert PyObject *empty_string, *null_tuple, *globals;
4075796c8dcSSimon Schubert PyCodeObject *code;
4085796c8dcSSimon Schubert PyFrameObject *frame;
4095796c8dcSSimon Schubert PyThreadState *tstate;
4105796c8dcSSimon Schubert
4115796c8dcSSimon Schubert empty_string = PyString_FromString ("");
4125796c8dcSSimon Schubert if (!empty_string)
4135796c8dcSSimon Schubert return NULL;
4145796c8dcSSimon Schubert
4155796c8dcSSimon Schubert null_tuple = PyTuple_New (0);
4165796c8dcSSimon Schubert if (!null_tuple)
4175796c8dcSSimon Schubert {
4185796c8dcSSimon Schubert Py_DECREF (empty_string);
4195796c8dcSSimon Schubert return NULL;
4205796c8dcSSimon Schubert }
4215796c8dcSSimon Schubert
4225796c8dcSSimon Schubert code = PyCode_New (0, /* argcount */
4235796c8dcSSimon Schubert 0, /* nlocals */
4245796c8dcSSimon Schubert 0, /* stacksize */
4255796c8dcSSimon Schubert 0, /* flags */
4265796c8dcSSimon Schubert empty_string, /* code */
4275796c8dcSSimon Schubert null_tuple, /* consts */
4285796c8dcSSimon Schubert null_tuple, /* names */
4295796c8dcSSimon Schubert null_tuple, /* varnames */
4305796c8dcSSimon Schubert #if PYTHON_API_VERSION >= 1010
4315796c8dcSSimon Schubert null_tuple, /* freevars */
4325796c8dcSSimon Schubert null_tuple, /* cellvars */
4335796c8dcSSimon Schubert #endif
4345796c8dcSSimon Schubert empty_string, /* filename */
4355796c8dcSSimon Schubert empty_string, /* name */
4365796c8dcSSimon Schubert 1, /* firstlineno */
4375796c8dcSSimon Schubert empty_string /* lnotab */
4385796c8dcSSimon Schubert );
4395796c8dcSSimon Schubert
4405796c8dcSSimon Schubert Py_DECREF (empty_string);
4415796c8dcSSimon Schubert Py_DECREF (null_tuple);
4425796c8dcSSimon Schubert
4435796c8dcSSimon Schubert if (!code)
4445796c8dcSSimon Schubert return NULL;
4455796c8dcSSimon Schubert
4465796c8dcSSimon Schubert globals = PyDict_New ();
4475796c8dcSSimon Schubert if (!globals)
4485796c8dcSSimon Schubert {
4495796c8dcSSimon Schubert Py_DECREF (code);
4505796c8dcSSimon Schubert return NULL;
4515796c8dcSSimon Schubert }
4525796c8dcSSimon Schubert
4535796c8dcSSimon Schubert tstate = PyThreadState_GET ();
4545796c8dcSSimon Schubert
4555796c8dcSSimon Schubert frame = PyFrame_New (tstate, code, globals, NULL);
4565796c8dcSSimon Schubert
4575796c8dcSSimon Schubert Py_DECREF (globals);
4585796c8dcSSimon Schubert Py_DECREF (code);
4595796c8dcSSimon Schubert
4605796c8dcSSimon Schubert if (!frame)
4615796c8dcSSimon Schubert return NULL;
4625796c8dcSSimon Schubert
4635796c8dcSSimon Schubert tstate->frame = frame;
4645796c8dcSSimon Schubert make_cleanup (py_restore_tstate, frame->f_back);
4655796c8dcSSimon Schubert return (PyObject *) frame;
4665796c8dcSSimon Schubert }
467*ef5ccd6cSJohn Marino #endif
4685796c8dcSSimon Schubert
4695796c8dcSSimon Schubert /* Helper for apply_val_pretty_printer that formats children of the
470cf7f2e2dSJohn Marino printer, if any exist. If is_py_none is true, then nothing has
471cf7f2e2dSJohn Marino been printed by to_string, and format output accordingly. */
4725796c8dcSSimon Schubert 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)4735796c8dcSSimon Schubert print_children (PyObject *printer, const char *hint,
4745796c8dcSSimon Schubert struct ui_file *stream, int recurse,
4755796c8dcSSimon Schubert const struct value_print_options *options,
476cf7f2e2dSJohn Marino const struct language_defn *language,
477cf7f2e2dSJohn Marino int is_py_none)
4785796c8dcSSimon Schubert {
4795796c8dcSSimon Schubert int is_map, is_array, done_flag, pretty;
4805796c8dcSSimon Schubert unsigned int i;
481*ef5ccd6cSJohn Marino PyObject *children, *iter;
482*ef5ccd6cSJohn Marino #ifndef IS_PY3K
483*ef5ccd6cSJohn Marino PyObject *frame;
484*ef5ccd6cSJohn Marino #endif
4855796c8dcSSimon Schubert struct cleanup *cleanups;
4865796c8dcSSimon Schubert
4875796c8dcSSimon Schubert if (! PyObject_HasAttr (printer, gdbpy_children_cst))
4885796c8dcSSimon Schubert return;
4895796c8dcSSimon Schubert
4905796c8dcSSimon Schubert /* If we are printing a map or an array, we want some special
4915796c8dcSSimon Schubert formatting. */
4925796c8dcSSimon Schubert is_map = hint && ! strcmp (hint, "map");
4935796c8dcSSimon Schubert is_array = hint && ! strcmp (hint, "array");
4945796c8dcSSimon Schubert
4955796c8dcSSimon Schubert children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
4965796c8dcSSimon Schubert NULL);
4975796c8dcSSimon Schubert if (! children)
4985796c8dcSSimon Schubert {
499c50c785cSJohn Marino print_stack_unless_memory_error (stream);
5005796c8dcSSimon Schubert return;
5015796c8dcSSimon Schubert }
5025796c8dcSSimon Schubert
5035796c8dcSSimon Schubert cleanups = make_cleanup_py_decref (children);
5045796c8dcSSimon Schubert
5055796c8dcSSimon Schubert iter = PyObject_GetIter (children);
5065796c8dcSSimon Schubert if (!iter)
5075796c8dcSSimon Schubert {
508c50c785cSJohn Marino print_stack_unless_memory_error (stream);
5095796c8dcSSimon Schubert goto done;
5105796c8dcSSimon Schubert }
5115796c8dcSSimon Schubert make_cleanup_py_decref (iter);
5125796c8dcSSimon Schubert
5135796c8dcSSimon Schubert /* Use the prettyprint_arrays option if we are printing an array,
5145796c8dcSSimon Schubert and the pretty option otherwise. */
515c50c785cSJohn Marino if (is_array)
516c50c785cSJohn Marino pretty = options->prettyprint_arrays;
517c50c785cSJohn Marino else
518c50c785cSJohn Marino {
519c50c785cSJohn Marino if (options->pretty == Val_prettyprint)
520c50c785cSJohn Marino pretty = 1;
521c50c785cSJohn Marino else
522c50c785cSJohn Marino pretty = options->prettyprint_structs;
523c50c785cSJohn Marino }
5245796c8dcSSimon Schubert
5255796c8dcSSimon Schubert /* Manufacture a dummy Python frame to work around Python 2.4 bug,
5265796c8dcSSimon Schubert where it insists on having a non-NULL tstate->frame when
5275796c8dcSSimon Schubert a generator is called. */
528*ef5ccd6cSJohn Marino #ifndef IS_PY3K
5295796c8dcSSimon Schubert frame = push_dummy_python_frame ();
5305796c8dcSSimon Schubert if (!frame)
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert gdbpy_print_stack ();
5335796c8dcSSimon Schubert goto done;
5345796c8dcSSimon Schubert }
5355796c8dcSSimon Schubert make_cleanup_py_decref (frame);
536*ef5ccd6cSJohn Marino #endif
5375796c8dcSSimon Schubert
5385796c8dcSSimon Schubert done_flag = 0;
5395796c8dcSSimon Schubert for (i = 0; i < options->print_max; ++i)
5405796c8dcSSimon Schubert {
5415796c8dcSSimon Schubert PyObject *py_v, *item = PyIter_Next (iter);
542a45ae5f8SJohn Marino const char *name;
5435796c8dcSSimon Schubert struct cleanup *inner_cleanup;
5445796c8dcSSimon Schubert
5455796c8dcSSimon Schubert if (! item)
5465796c8dcSSimon Schubert {
5475796c8dcSSimon Schubert if (PyErr_Occurred ())
548c50c785cSJohn Marino print_stack_unless_memory_error (stream);
5495796c8dcSSimon Schubert /* Set a flag so we can know whether we printed all the
5505796c8dcSSimon Schubert available elements. */
5515796c8dcSSimon Schubert else
5525796c8dcSSimon Schubert done_flag = 1;
5535796c8dcSSimon Schubert break;
5545796c8dcSSimon Schubert }
5555796c8dcSSimon Schubert
5565796c8dcSSimon Schubert if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
5575796c8dcSSimon Schubert {
5585796c8dcSSimon Schubert gdbpy_print_stack ();
5595796c8dcSSimon Schubert Py_DECREF (item);
5605796c8dcSSimon Schubert continue;
5615796c8dcSSimon Schubert }
5625796c8dcSSimon Schubert inner_cleanup = make_cleanup_py_decref (item);
5635796c8dcSSimon Schubert
5645796c8dcSSimon Schubert /* Print initial "{". For other elements, there are three
5655796c8dcSSimon Schubert cases:
5665796c8dcSSimon Schubert 1. Maps. Print a "," after each value element.
5675796c8dcSSimon Schubert 2. Arrays. Always print a ",".
5685796c8dcSSimon Schubert 3. Other. Always print a ",". */
5695796c8dcSSimon Schubert if (i == 0)
570cf7f2e2dSJohn Marino {
571cf7f2e2dSJohn Marino if (is_py_none)
572cf7f2e2dSJohn Marino fputs_filtered ("{", stream);
573cf7f2e2dSJohn Marino else
5745796c8dcSSimon Schubert fputs_filtered (" = {", stream);
575cf7f2e2dSJohn Marino }
576cf7f2e2dSJohn Marino
5775796c8dcSSimon Schubert else if (! is_map || i % 2 == 0)
5785796c8dcSSimon Schubert fputs_filtered (pretty ? "," : ", ", stream);
5795796c8dcSSimon Schubert
5805796c8dcSSimon Schubert /* In summary mode, we just want to print "= {...}" if there is
5815796c8dcSSimon Schubert a value. */
5825796c8dcSSimon Schubert if (options->summary)
5835796c8dcSSimon Schubert {
5845796c8dcSSimon Schubert /* This increment tricks the post-loop logic to print what
5855796c8dcSSimon Schubert we want. */
5865796c8dcSSimon Schubert ++i;
5875796c8dcSSimon Schubert /* Likewise. */
5885796c8dcSSimon Schubert pretty = 0;
5895796c8dcSSimon Schubert break;
5905796c8dcSSimon Schubert }
5915796c8dcSSimon Schubert
5925796c8dcSSimon Schubert if (! is_map || i % 2 == 0)
5935796c8dcSSimon Schubert {
5945796c8dcSSimon Schubert if (pretty)
5955796c8dcSSimon Schubert {
5965796c8dcSSimon Schubert fputs_filtered ("\n", stream);
5975796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream);
5985796c8dcSSimon Schubert }
5995796c8dcSSimon Schubert else
6005796c8dcSSimon Schubert wrap_here (n_spaces (2 + 2 *recurse));
6015796c8dcSSimon Schubert }
6025796c8dcSSimon Schubert
6035796c8dcSSimon Schubert if (is_map && i % 2 == 0)
6045796c8dcSSimon Schubert fputs_filtered ("[", stream);
6055796c8dcSSimon Schubert else if (is_array)
6065796c8dcSSimon Schubert {
6075796c8dcSSimon Schubert /* We print the index, not whatever the child method
6085796c8dcSSimon Schubert returned as the name. */
6095796c8dcSSimon Schubert if (options->print_array_indexes)
6105796c8dcSSimon Schubert fprintf_filtered (stream, "[%d] = ", i);
6115796c8dcSSimon Schubert }
6125796c8dcSSimon Schubert else if (! is_map)
6135796c8dcSSimon Schubert {
6145796c8dcSSimon Schubert fputs_filtered (name, stream);
6155796c8dcSSimon Schubert fputs_filtered (" = ", stream);
6165796c8dcSSimon Schubert }
6175796c8dcSSimon Schubert
618cf7f2e2dSJohn Marino if (gdbpy_is_lazy_string (py_v))
619cf7f2e2dSJohn Marino {
620c50c785cSJohn Marino CORE_ADDR addr;
621cf7f2e2dSJohn Marino struct type *type;
622cf7f2e2dSJohn Marino long length;
623cf7f2e2dSJohn Marino char *encoding = NULL;
624c50c785cSJohn Marino struct value_print_options local_opts = *options;
625cf7f2e2dSJohn Marino
626c50c785cSJohn Marino make_cleanup (free_current_contents, &encoding);
627c50c785cSJohn Marino gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
628c50c785cSJohn Marino
629c50c785cSJohn Marino local_opts.addressprint = 0;
630c50c785cSJohn Marino val_print_string (type, encoding, addr, (int) length, stream,
631c50c785cSJohn Marino &local_opts);
632c50c785cSJohn Marino
633c50c785cSJohn Marino do_cleanups (inner_cleanup);
634c50c785cSJohn Marino }
635c50c785cSJohn Marino else if (gdbpy_is_string (py_v))
636c50c785cSJohn Marino {
637c50c785cSJohn Marino gdb_byte *output;
638c50c785cSJohn Marino
639c50c785cSJohn Marino output = python_string_to_host_string (py_v);
640cf7f2e2dSJohn Marino if (!output)
6415796c8dcSSimon Schubert gdbpy_print_stack ();
6425796c8dcSSimon Schubert else
6435796c8dcSSimon Schubert {
644cf7f2e2dSJohn Marino fputs_filtered (output, stream);
645cf7f2e2dSJohn Marino xfree (output);
6465796c8dcSSimon Schubert }
6475796c8dcSSimon Schubert }
6485796c8dcSSimon Schubert else
6495796c8dcSSimon Schubert {
6505796c8dcSSimon Schubert struct value *value = convert_value_from_python (py_v);
6515796c8dcSSimon Schubert
6525796c8dcSSimon Schubert if (value == NULL)
6535796c8dcSSimon Schubert {
6545796c8dcSSimon Schubert gdbpy_print_stack ();
6555796c8dcSSimon Schubert error (_("Error while executing Python code."));
6565796c8dcSSimon Schubert }
6575796c8dcSSimon Schubert else
6585796c8dcSSimon Schubert common_val_print (value, stream, recurse + 1, options, language);
6595796c8dcSSimon Schubert }
6605796c8dcSSimon Schubert
6615796c8dcSSimon Schubert if (is_map && i % 2 == 0)
6625796c8dcSSimon Schubert fputs_filtered ("] = ", stream);
6635796c8dcSSimon Schubert
6645796c8dcSSimon Schubert do_cleanups (inner_cleanup);
6655796c8dcSSimon Schubert }
6665796c8dcSSimon Schubert
6675796c8dcSSimon Schubert if (i)
6685796c8dcSSimon Schubert {
6695796c8dcSSimon Schubert if (!done_flag)
6705796c8dcSSimon Schubert {
6715796c8dcSSimon Schubert if (pretty)
6725796c8dcSSimon Schubert {
6735796c8dcSSimon Schubert fputs_filtered ("\n", stream);
6745796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream);
6755796c8dcSSimon Schubert }
6765796c8dcSSimon Schubert fputs_filtered ("...", stream);
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert if (pretty)
6795796c8dcSSimon Schubert {
6805796c8dcSSimon Schubert fputs_filtered ("\n", stream);
6815796c8dcSSimon Schubert print_spaces_filtered (2 * recurse, stream);
6825796c8dcSSimon Schubert }
6835796c8dcSSimon Schubert fputs_filtered ("}", stream);
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert
6865796c8dcSSimon Schubert done:
6875796c8dcSSimon Schubert do_cleanups (cleanups);
6885796c8dcSSimon Schubert }
6895796c8dcSSimon Schubert
6905796c8dcSSimon Schubert 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)6915796c8dcSSimon Schubert apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
6925796c8dcSSimon Schubert int embedded_offset, CORE_ADDR address,
6935796c8dcSSimon Schubert struct ui_file *stream, int recurse,
694cf7f2e2dSJohn Marino const struct value *val,
6955796c8dcSSimon Schubert const struct value_print_options *options,
6965796c8dcSSimon Schubert const struct language_defn *language)
6975796c8dcSSimon Schubert {
6985796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (type);
6995796c8dcSSimon Schubert PyObject *printer = NULL;
7005796c8dcSSimon Schubert PyObject *val_obj = NULL;
7015796c8dcSSimon Schubert struct value *value;
7025796c8dcSSimon Schubert char *hint = NULL;
7035796c8dcSSimon Schubert struct cleanup *cleanups;
7045796c8dcSSimon Schubert int result = 0;
705c50c785cSJohn Marino enum string_repr_result print_result;
706c50c785cSJohn Marino
707c50c785cSJohn Marino /* No pretty-printer support for unavailable values. */
708c50c785cSJohn Marino if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
709c50c785cSJohn Marino return 0;
710c50c785cSJohn Marino
7115796c8dcSSimon Schubert cleanups = ensure_python_env (gdbarch, language);
7125796c8dcSSimon Schubert
7135796c8dcSSimon Schubert /* Instantiate the printer. */
7145796c8dcSSimon Schubert if (valaddr)
7155796c8dcSSimon Schubert valaddr += embedded_offset;
716cf7f2e2dSJohn Marino value = value_from_contents_and_address (type, valaddr,
717cf7f2e2dSJohn Marino address + embedded_offset);
718c50c785cSJohn Marino
719cf7f2e2dSJohn Marino set_value_component_location (value, val);
720cf7f2e2dSJohn Marino /* set_value_component_location resets the address, so we may
721cf7f2e2dSJohn Marino need to set it again. */
722cf7f2e2dSJohn Marino if (VALUE_LVAL (value) != lval_internalvar
723cf7f2e2dSJohn Marino && VALUE_LVAL (value) != lval_internalvar_component
724cf7f2e2dSJohn Marino && VALUE_LVAL (value) != lval_computed)
725cf7f2e2dSJohn Marino set_value_address (value, address + embedded_offset);
7265796c8dcSSimon Schubert
7275796c8dcSSimon Schubert val_obj = value_to_value_object (value);
7285796c8dcSSimon Schubert if (! val_obj)
7295796c8dcSSimon Schubert goto done;
7305796c8dcSSimon Schubert
7315796c8dcSSimon Schubert /* Find the constructor. */
7325796c8dcSSimon Schubert printer = find_pretty_printer (val_obj);
7335796c8dcSSimon Schubert Py_DECREF (val_obj);
7345796c8dcSSimon Schubert make_cleanup_py_decref (printer);
7355796c8dcSSimon Schubert if (! printer || printer == Py_None)
7365796c8dcSSimon Schubert goto done;
7375796c8dcSSimon Schubert
7385796c8dcSSimon Schubert /* If we are printing a map, we want some special formatting. */
7395796c8dcSSimon Schubert hint = gdbpy_get_display_hint (printer);
7405796c8dcSSimon Schubert make_cleanup (free_current_contents, &hint);
7415796c8dcSSimon Schubert
7425796c8dcSSimon Schubert /* Print the section */
743c50c785cSJohn Marino print_result = print_string_repr (printer, hint, stream, recurse,
744cf7f2e2dSJohn Marino options, language, gdbarch);
745c50c785cSJohn Marino if (print_result != string_repr_error)
746cf7f2e2dSJohn Marino print_children (printer, hint, stream, recurse, options, language,
747c50c785cSJohn Marino print_result == string_repr_none);
748cf7f2e2dSJohn Marino
7495796c8dcSSimon Schubert result = 1;
7505796c8dcSSimon Schubert
7515796c8dcSSimon Schubert
7525796c8dcSSimon Schubert done:
7535796c8dcSSimon Schubert if (PyErr_Occurred ())
754c50c785cSJohn Marino print_stack_unless_memory_error (stream);
7555796c8dcSSimon Schubert do_cleanups (cleanups);
7565796c8dcSSimon Schubert return result;
7575796c8dcSSimon Schubert }
7585796c8dcSSimon Schubert
7595796c8dcSSimon Schubert
7605796c8dcSSimon Schubert /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
7615796c8dcSSimon Schubert print object. It must have a 'to_string' method (but this is
7625796c8dcSSimon Schubert checked by varobj, not here) which takes no arguments and
7635796c8dcSSimon Schubert returns a string. The printer will return a value and in the case
7645796c8dcSSimon Schubert of a Python string being returned, this function will return a
7655796c8dcSSimon Schubert PyObject containing the string. For any other type, *REPLACEMENT is
7665796c8dcSSimon Schubert set to the replacement value and this function returns NULL. On
7675796c8dcSSimon Schubert error, *REPLACEMENT is set to NULL and this function also returns
7685796c8dcSSimon Schubert NULL. */
7695796c8dcSSimon Schubert PyObject *
apply_varobj_pretty_printer(PyObject * printer_obj,struct value ** replacement,struct ui_file * stream)7705796c8dcSSimon Schubert apply_varobj_pretty_printer (PyObject *printer_obj,
771c50c785cSJohn Marino struct value **replacement,
772c50c785cSJohn Marino struct ui_file *stream)
7735796c8dcSSimon Schubert {
7745796c8dcSSimon Schubert PyObject *py_str = NULL;
7755796c8dcSSimon Schubert
7765796c8dcSSimon Schubert *replacement = NULL;
7775796c8dcSSimon Schubert py_str = pretty_print_one_value (printer_obj, replacement);
7785796c8dcSSimon Schubert
7795796c8dcSSimon Schubert if (*replacement == NULL && py_str == NULL)
780c50c785cSJohn Marino print_stack_unless_memory_error (stream);
7815796c8dcSSimon Schubert
7825796c8dcSSimon Schubert return py_str;
7835796c8dcSSimon Schubert }
7845796c8dcSSimon Schubert
7855796c8dcSSimon Schubert /* Find a pretty-printer object for the varobj module. Returns a new
7865796c8dcSSimon Schubert reference to the object if successful; returns NULL if not. VALUE
7875796c8dcSSimon Schubert is the value for which a printer tests to determine if it
7885796c8dcSSimon Schubert can pretty-print the value. */
7895796c8dcSSimon Schubert PyObject *
gdbpy_get_varobj_pretty_printer(struct value * value)7905796c8dcSSimon Schubert gdbpy_get_varobj_pretty_printer (struct value *value)
7915796c8dcSSimon Schubert {
7925796c8dcSSimon Schubert PyObject *val_obj;
7935796c8dcSSimon Schubert PyObject *pretty_printer = NULL;
7945796c8dcSSimon Schubert volatile struct gdb_exception except;
7955796c8dcSSimon Schubert
7965796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ALL)
7975796c8dcSSimon Schubert {
7985796c8dcSSimon Schubert value = value_copy (value);
7995796c8dcSSimon Schubert }
8005796c8dcSSimon Schubert GDB_PY_HANDLE_EXCEPTION (except);
8015796c8dcSSimon Schubert
8025796c8dcSSimon Schubert val_obj = value_to_value_object (value);
8035796c8dcSSimon Schubert if (! val_obj)
8045796c8dcSSimon Schubert return NULL;
8055796c8dcSSimon Schubert
8065796c8dcSSimon Schubert pretty_printer = find_pretty_printer (val_obj);
8075796c8dcSSimon Schubert Py_DECREF (val_obj);
8085796c8dcSSimon Schubert return pretty_printer;
8095796c8dcSSimon Schubert }
8105796c8dcSSimon Schubert
8115796c8dcSSimon Schubert /* A Python function which wraps find_pretty_printer and instantiates
8125796c8dcSSimon Schubert the resulting class. This accepts a Value argument and returns a
8135796c8dcSSimon Schubert pretty printer instance, or None. This function is useful as an
8145796c8dcSSimon Schubert argument to the MI command -var-set-visualizer. */
8155796c8dcSSimon Schubert PyObject *
gdbpy_default_visualizer(PyObject * self,PyObject * args)8165796c8dcSSimon Schubert gdbpy_default_visualizer (PyObject *self, PyObject *args)
8175796c8dcSSimon Schubert {
8185796c8dcSSimon Schubert PyObject *val_obj;
819cf7f2e2dSJohn Marino PyObject *cons;
8205796c8dcSSimon Schubert struct value *value;
8215796c8dcSSimon Schubert
8225796c8dcSSimon Schubert if (! PyArg_ParseTuple (args, "O", &val_obj))
8235796c8dcSSimon Schubert return NULL;
8245796c8dcSSimon Schubert value = value_object_to_value (val_obj);
8255796c8dcSSimon Schubert if (! value)
8265796c8dcSSimon Schubert {
827cf7f2e2dSJohn Marino PyErr_SetString (PyExc_TypeError,
828cf7f2e2dSJohn Marino _("Argument must be a gdb.Value."));
8295796c8dcSSimon Schubert return NULL;
8305796c8dcSSimon Schubert }
8315796c8dcSSimon Schubert
8325796c8dcSSimon Schubert cons = find_pretty_printer (val_obj);
8335796c8dcSSimon Schubert return cons;
8345796c8dcSSimon Schubert }
8355796c8dcSSimon Schubert
8365796c8dcSSimon Schubert #else /* HAVE_PYTHON */
8375796c8dcSSimon Schubert
8385796c8dcSSimon Schubert 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)8395796c8dcSSimon Schubert apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
8405796c8dcSSimon Schubert int embedded_offset, CORE_ADDR address,
8415796c8dcSSimon Schubert struct ui_file *stream, int recurse,
842cf7f2e2dSJohn Marino const struct value *val,
8435796c8dcSSimon Schubert const struct value_print_options *options,
8445796c8dcSSimon Schubert const struct language_defn *language)
8455796c8dcSSimon Schubert {
8465796c8dcSSimon Schubert return 0;
8475796c8dcSSimon Schubert }
8485796c8dcSSimon Schubert
8495796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
850