]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-prettyprint.c
* gdbarch.sh (target_gdbarch): Remove macro.
[thirdparty/binutils-gdb.git] / gdb / python / py-prettyprint.c
CommitLineData
a6bac58e
TT
1/* Python pretty-printing
2
0b302171 3 Copyright (C) 2008-2012 Free Software Foundation, Inc.
a6bac58e
TT
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"
6bf0ce2b 26
a6bac58e 27#include "python.h"
a6bac58e
TT
28
29#ifdef HAVE_PYTHON
6bf0ce2b
PP
30#include "python-internal.h"
31
621c8364
TT
32/* Return type of print_string_repr. */
33
34enum 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
a6bac58e
TT
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. */
fa33c3cd 49
a6bac58e
TT
50static PyObject *
51search_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
967cf477 63 /* Skip if disabled. */
1bdb0c54
TT
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 }
967cf477 78
a6bac58e
TT
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
fa33c3cd
DE
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
a6bac58e 97static PyObject *
fa33c3cd 98find_pretty_printer_from_objfiles (PyObject *value)
a6bac58e 99{
fa33c3cd
DE
100 PyObject *pp_list;
101 PyObject *function;
a6bac58e 102 struct objfile *obj;
a6bac58e 103
a6bac58e
TT
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);
fa33c3cd 116 Py_XDECREF (pp_list);
a6bac58e 117
fa33c3cd 118 /* If there is an error in any objfile list, abort the search and exit. */
a6bac58e 119 if (! function)
fa33c3cd 120 return NULL;
a6bac58e
TT
121
122 if (function != Py_None)
fa33c3cd 123 return function;
a6bac58e
TT
124
125 Py_DECREF (function);
a6bac58e
TT
126 }
127
fa33c3cd
DE
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
137static PyObject *
138find_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
158static PyObject *
159find_pretty_printer_from_gdb (PyObject *value)
160{
161 PyObject *pp_list;
162 PyObject *function;
163
23fa7f66 164 /* Fetch the global pretty printer list. */
b9516fa1
YPK
165 if (gdb_python_module == NULL
166 || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
fa33c3cd 167 Py_RETURN_NONE;
b9516fa1 168 pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
fa33c3cd 169 if (pp_list == NULL || ! PyList_Check (pp_list))
a6bac58e 170 {
fa33c3cd
DE
171 Py_XDECREF (pp_list);
172 Py_RETURN_NONE;
a6bac58e 173 }
a6bac58e
TT
174
175 function = search_pp_list (pp_list, value);
a6bac58e 176 Py_XDECREF (pp_list);
a6bac58e
TT
177 return function;
178}
be759fcf 179
fa33c3cd
DE
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
184static PyObject *
185find_pretty_printer (PyObject *value)
186{
187 PyObject *function;
188
23fa7f66 189 /* Look at the pretty-printer list for each objfile
fa33c3cd
DE
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
23fa7f66 196 /* Look at the pretty-printer list for the current program-space. */
fa33c3cd
DE
197 function = find_pretty_printer_from_progspace (value);
198 if (function == NULL || function != Py_None)
199 return function;
200 Py_DECREF (function);
201
23fa7f66 202 /* Look at the pretty-printer list in the gdb module. */
fa33c3cd
DE
203 function = find_pretty_printer_from_gdb (value);
204 return function;
205}
be759fcf 206
fbb8f299
PM
207/* Pretty-print a single value, via the printer object PRINTER.
208 If the function returns a string, a PyObject containing the string
79f283fe
PM
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
8dc78533
JK
212 is returned. On error, *OUT_VALUE is set to NULL, NULL is
213 returned, with a python exception set. */
79f283fe 214
fbb8f299 215static PyObject *
a6bac58e
TT
216pretty_print_one_value (PyObject *printer, struct value **out_value)
217{
a6bac58e 218 volatile struct gdb_exception except;
fbb8f299 219 PyObject *result = NULL;
a6bac58e 220
fbb8f299 221 *out_value = NULL;
a6bac58e
TT
222 TRY_CATCH (except, RETURN_MASK_ALL)
223 {
a6bac58e
TT
224 result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
225 if (result)
226 {
79f283fe
PM
227 if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
228 && result != Py_None)
fbb8f299
PM
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 }
a6bac58e
TT
236 }
237 }
238
fbb8f299 239 return result;
a6bac58e
TT
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. */
246char *
247gdbpy_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);
a6bac58e 256 if (hint)
f04e4012
TT
257 {
258 if (gdbpy_is_string (hint))
8dc78533
JK
259 {
260 result = python_string_to_host_string (hint);
261 if (result == NULL)
262 gdbpy_print_stack ();
263 }
f04e4012
TT
264 Py_DECREF (hint);
265 }
a6bac58e
TT
266 else
267 gdbpy_print_stack ();
268
269 return result;
270}
271
621c8364
TT
272/* A wrapper for gdbpy_print_stack that ignores MemoryError. */
273
274static void
275print_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')
1dae3efc 292 fprintf_filtered (stream, _("<error reading variable>"));
621c8364
TT
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
a6bac58e 302/* Helper for apply_val_pretty_printer which calls to_string and
621c8364
TT
303 formats the result. */
304
305static enum string_repr_result
a6bac58e
TT
306print_string_repr (PyObject *printer, const char *hint,
307 struct ui_file *stream, int recurse,
308 const struct value_print_options *options,
50810684
UW
309 const struct language_defn *language,
310 struct gdbarch *gdbarch)
a6bac58e 311{
a6bac58e 312 struct value *replacement = NULL;
fbb8f299 313 PyObject *py_str = NULL;
621c8364 314 enum string_repr_result result = string_repr_ok;
a6bac58e 315
fbb8f299
PM
316 py_str = pretty_print_one_value (printer, &replacement);
317 if (py_str)
a6bac58e 318 {
09ca9e2e
TT
319 struct cleanup *cleanup = make_cleanup_py_decref (py_str);
320
79f283fe 321 if (py_str == Py_None)
621c8364 322 result = string_repr_none;
09ca9e2e 323 else if (gdbpy_is_lazy_string (py_str))
fbb8f299 324 {
09ca9e2e 325 CORE_ADDR addr;
79f283fe
PM
326 long length;
327 struct type *type;
328 char *encoding = NULL;
a81766d8 329 struct value_print_options local_opts = *options;
79f283fe 330
09ca9e2e
TT
331 make_cleanup (free_current_contents, &encoding);
332 gdbpy_extract_lazy_string (py_str, &addr, &type,
333 &length, &encoding);
334
a81766d8 335 local_opts.addressprint = 0;
09ca9e2e 336 val_print_string (type, encoding, addr, (int) length,
a81766d8 337 stream, &local_opts);
09ca9e2e
TT
338 }
339 else
340 {
341 PyObject *string;
342
343 string = python_string_to_target_python_string (py_str);
344 if (string)
79f283fe 345 {
09ca9e2e
TT
346 gdb_byte *output;
347 long length;
348 struct type *type;
349
350 make_cleanup_py_decref (string);
351 output = PyString_AsString (string);
352 length = PyString_Size (string);
353 type = builtin_type (gdbarch)->builtin_char;
354
355 if (hint && !strcmp (hint, "string"))
356 LA_PRINT_STRING (stream, type, output, length, NULL,
79f283fe
PM
357 0, options);
358 else
359 fputs_filtered (output, stream);
be759fcf
PM
360 }
361 else
621c8364
TT
362 {
363 result = string_repr_error;
364 print_stack_unless_memory_error (stream);
365 }
79f283fe 366 }
09ca9e2e
TT
367
368 do_cleanups (cleanup);
a6bac58e
TT
369 }
370 else if (replacement)
269f82e5
PP
371 {
372 struct value_print_options opts = *options;
373
374 opts.addressprint = 0;
375 common_val_print (replacement, stream, recurse, &opts, language);
376 }
a6bac58e 377 else
621c8364
TT
378 {
379 result = string_repr_error;
380 print_stack_unless_memory_error (stream);
381 }
79f283fe 382
621c8364 383 return result;
a6bac58e
TT
384}
385
386static void
387py_restore_tstate (void *p)
388{
389 PyFrameObject *frame = p;
390 PyThreadState *tstate = PyThreadState_GET ();
d59b6f6c 391
a6bac58e
TT
392 tstate->frame = frame;
393}
394
395/* Create a dummy PyFrameObject, needed to work around
396 a Python-2.4 bug with generators. */
397static PyObject *
eeae04df 398push_dummy_python_frame (void)
a6bac58e
TT
399{
400 PyObject *empty_string, *null_tuple, *globals;
401 PyCodeObject *code;
402 PyFrameObject *frame;
403 PyThreadState *tstate;
404
405 empty_string = PyString_FromString ("");
406 if (!empty_string)
407 return NULL;
408
409 null_tuple = PyTuple_New (0);
410 if (!null_tuple)
411 {
412 Py_DECREF (empty_string);
413 return NULL;
414 }
415
416 code = PyCode_New (0, /* argcount */
417 0, /* nlocals */
418 0, /* stacksize */
419 0, /* flags */
420 empty_string, /* code */
421 null_tuple, /* consts */
422 null_tuple, /* names */
423 null_tuple, /* varnames */
424#if PYTHON_API_VERSION >= 1010
425 null_tuple, /* freevars */
426 null_tuple, /* cellvars */
427#endif
428 empty_string, /* filename */
429 empty_string, /* name */
430 1, /* firstlineno */
431 empty_string /* lnotab */
432 );
433
434 Py_DECREF (empty_string);
435 Py_DECREF (null_tuple);
436
437 if (!code)
438 return NULL;
439
440 globals = PyDict_New ();
441 if (!globals)
442 {
443 Py_DECREF (code);
444 return NULL;
445 }
446
447 tstate = PyThreadState_GET ();
448
449 frame = PyFrame_New (tstate, code, globals, NULL);
450
451 Py_DECREF (globals);
452 Py_DECREF (code);
453
454 if (!frame)
455 return NULL;
456
457 tstate->frame = frame;
458 make_cleanup (py_restore_tstate, frame->f_back);
459 return (PyObject *) frame;
460}
461
462/* Helper for apply_val_pretty_printer that formats children of the
79f283fe
PM
463 printer, if any exist. If is_py_none is true, then nothing has
464 been printed by to_string, and format output accordingly. */
a6bac58e
TT
465static void
466print_children (PyObject *printer, const char *hint,
467 struct ui_file *stream, int recurse,
468 const struct value_print_options *options,
79f283fe
PM
469 const struct language_defn *language,
470 int is_py_none)
a6bac58e
TT
471{
472 int is_map, is_array, done_flag, pretty;
473 unsigned int i;
474 PyObject *children, *iter, *frame;
475 struct cleanup *cleanups;
476
477 if (! PyObject_HasAttr (printer, gdbpy_children_cst))
478 return;
479
480 /* If we are printing a map or an array, we want some special
481 formatting. */
482 is_map = hint && ! strcmp (hint, "map");
483 is_array = hint && ! strcmp (hint, "array");
484
485 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
486 NULL);
487 if (! children)
488 {
621c8364 489 print_stack_unless_memory_error (stream);
a6bac58e
TT
490 return;
491 }
492
493 cleanups = make_cleanup_py_decref (children);
494
495 iter = PyObject_GetIter (children);
496 if (!iter)
497 {
621c8364 498 print_stack_unless_memory_error (stream);
a6bac58e
TT
499 goto done;
500 }
501 make_cleanup_py_decref (iter);
502
503 /* Use the prettyprint_arrays option if we are printing an array,
504 and the pretty option otherwise. */
a81766d8
TT
505 if (is_array)
506 pretty = options->prettyprint_arrays;
507 else
508 {
509 if (options->pretty == Val_prettyprint)
510 pretty = 1;
511 else
512 pretty = options->prettyprint_structs;
513 }
a6bac58e
TT
514
515 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
516 where it insists on having a non-NULL tstate->frame when
517 a generator is called. */
518 frame = push_dummy_python_frame ();
519 if (!frame)
520 {
521 gdbpy_print_stack ();
522 goto done;
523 }
524 make_cleanup_py_decref (frame);
525
526 done_flag = 0;
527 for (i = 0; i < options->print_max; ++i)
528 {
529 PyObject *py_v, *item = PyIter_Next (iter);
ddd49eee 530 const char *name;
a6bac58e
TT
531 struct cleanup *inner_cleanup;
532
533 if (! item)
534 {
535 if (PyErr_Occurred ())
621c8364 536 print_stack_unless_memory_error (stream);
a6bac58e
TT
537 /* Set a flag so we can know whether we printed all the
538 available elements. */
539 else
540 done_flag = 1;
541 break;
542 }
543
544 if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
545 {
546 gdbpy_print_stack ();
547 Py_DECREF (item);
548 continue;
549 }
550 inner_cleanup = make_cleanup_py_decref (item);
551
552 /* Print initial "{". For other elements, there are three
553 cases:
554 1. Maps. Print a "," after each value element.
555 2. Arrays. Always print a ",".
556 3. Other. Always print a ",". */
557 if (i == 0)
79f283fe
PM
558 {
559 if (is_py_none)
560 fputs_filtered ("{", stream);
561 else
562 fputs_filtered (" = {", stream);
563 }
564
a6bac58e
TT
565 else if (! is_map || i % 2 == 0)
566 fputs_filtered (pretty ? "," : ", ", stream);
567
568 /* In summary mode, we just want to print "= {...}" if there is
569 a value. */
570 if (options->summary)
571 {
572 /* This increment tricks the post-loop logic to print what
573 we want. */
574 ++i;
575 /* Likewise. */
576 pretty = 0;
577 break;
578 }
579
580 if (! is_map || i % 2 == 0)
581 {
582 if (pretty)
583 {
584 fputs_filtered ("\n", stream);
585 print_spaces_filtered (2 + 2 * recurse, stream);
586 }
587 else
588 wrap_here (n_spaces (2 + 2 *recurse));
589 }
590
591 if (is_map && i % 2 == 0)
592 fputs_filtered ("[", stream);
593 else if (is_array)
594 {
595 /* We print the index, not whatever the child method
596 returned as the name. */
597 if (options->print_array_indexes)
598 fprintf_filtered (stream, "[%d] = ", i);
599 }
600 else if (! is_map)
601 {
602 fputs_filtered (name, stream);
603 fputs_filtered (" = ", stream);
604 }
605
09ca9e2e 606 if (gdbpy_is_lazy_string (py_v))
a6bac58e 607 {
09ca9e2e
TT
608 CORE_ADDR addr;
609 struct type *type;
610 long length;
611 char *encoding = NULL;
a81766d8 612 struct value_print_options local_opts = *options;
be759fcf 613
09ca9e2e
TT
614 make_cleanup (free_current_contents, &encoding);
615 gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
616
a81766d8 617 local_opts.addressprint = 0;
09ca9e2e 618 val_print_string (type, encoding, addr, (int) length, stream,
a81766d8 619 &local_opts);
09ca9e2e
TT
620
621 do_cleanups (inner_cleanup);
622 }
623 else if (gdbpy_is_string (py_v))
624 {
625 gdb_byte *output;
626
627 output = python_string_to_host_string (py_v);
628 if (!output)
629 gdbpy_print_stack ();
a6bac58e
TT
630 else
631 {
09ca9e2e 632 fputs_filtered (output, stream);
be759fcf 633 xfree (output);
a6bac58e
TT
634 }
635 }
636 else
637 {
638 struct value *value = convert_value_from_python (py_v);
639
640 if (value == NULL)
641 {
642 gdbpy_print_stack ();
643 error (_("Error while executing Python code."));
644 }
645 else
646 common_val_print (value, stream, recurse + 1, options, language);
647 }
648
649 if (is_map && i % 2 == 0)
650 fputs_filtered ("] = ", stream);
651
652 do_cleanups (inner_cleanup);
653 }
654
655 if (i)
656 {
657 if (!done_flag)
658 {
659 if (pretty)
660 {
661 fputs_filtered ("\n", stream);
662 print_spaces_filtered (2 + 2 * recurse, stream);
663 }
664 fputs_filtered ("...", stream);
665 }
666 if (pretty)
667 {
668 fputs_filtered ("\n", stream);
669 print_spaces_filtered (2 * recurse, stream);
670 }
671 fputs_filtered ("}", stream);
672 }
673
674 done:
675 do_cleanups (cleanups);
676}
677
678int
679apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
680 int embedded_offset, CORE_ADDR address,
681 struct ui_file *stream, int recurse,
0e03807e 682 const struct value *val,
a6bac58e
TT
683 const struct value_print_options *options,
684 const struct language_defn *language)
685{
50810684 686 struct gdbarch *gdbarch = get_type_arch (type);
a6bac58e
TT
687 PyObject *printer = NULL;
688 PyObject *val_obj = NULL;
689 struct value *value;
690 char *hint = NULL;
691 struct cleanup *cleanups;
692 int result = 0;
621c8364 693 enum string_repr_result print_result;
4e07d55f
PA
694
695 /* No pretty-printer support for unavailable values. */
63360adc 696 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
4e07d55f
PA
697 return 0;
698
d452c4bc 699 cleanups = ensure_python_env (gdbarch, language);
a6bac58e
TT
700
701 /* Instantiate the printer. */
702 if (valaddr)
703 valaddr += embedded_offset;
edf3d5f3
TT
704 value = value_from_contents_and_address (type, valaddr,
705 address + embedded_offset);
63360adc
MS
706
707 set_value_component_location (value, val);
708 /* set_value_component_location resets the address, so we may
709 need to set it again. */
710 if (VALUE_LVAL (value) != lval_internalvar
711 && VALUE_LVAL (value) != lval_internalvar_component
712 && VALUE_LVAL (value) != lval_computed)
713 set_value_address (value, address + embedded_offset);
a6bac58e
TT
714
715 val_obj = value_to_value_object (value);
716 if (! val_obj)
717 goto done;
718
719 /* Find the constructor. */
720 printer = find_pretty_printer (val_obj);
721 Py_DECREF (val_obj);
722 make_cleanup_py_decref (printer);
723 if (! printer || printer == Py_None)
724 goto done;
725
726 /* If we are printing a map, we want some special formatting. */
727 hint = gdbpy_get_display_hint (printer);
728 make_cleanup (free_current_contents, &hint);
729
730 /* Print the section */
621c8364
TT
731 print_result = print_string_repr (printer, hint, stream, recurse,
732 options, language, gdbarch);
733 if (print_result != string_repr_error)
734 print_children (printer, hint, stream, recurse, options, language,
735 print_result == string_repr_none);
79f283fe 736
a6bac58e
TT
737 result = 1;
738
739
740 done:
741 if (PyErr_Occurred ())
621c8364 742 print_stack_unless_memory_error (stream);
a6bac58e
TT
743 do_cleanups (cleanups);
744 return result;
745}
746
fbb8f299 747
b6313243
TT
748/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
749 print object. It must have a 'to_string' method (but this is
750 checked by varobj, not here) which takes no arguments and
fbb8f299
PM
751 returns a string. The printer will return a value and in the case
752 of a Python string being returned, this function will return a
753 PyObject containing the string. For any other type, *REPLACEMENT is
754 set to the replacement value and this function returns NULL. On
755 error, *REPLACEMENT is set to NULL and this function also returns
756 NULL. */
757PyObject *
b6313243 758apply_varobj_pretty_printer (PyObject *printer_obj,
621c8364
TT
759 struct value **replacement,
760 struct ui_file *stream)
b6313243 761{
fbb8f299 762 PyObject *py_str = NULL;
b6313243
TT
763
764 *replacement = NULL;
fbb8f299
PM
765 py_str = pretty_print_one_value (printer_obj, replacement);
766
767 if (*replacement == NULL && py_str == NULL)
621c8364 768 print_stack_unless_memory_error (stream);
b6313243 769
fbb8f299 770 return py_str;
b6313243
TT
771}
772
773/* Find a pretty-printer object for the varobj module. Returns a new
774 reference to the object if successful; returns NULL if not. VALUE
775 is the value for which a printer tests to determine if it
776 can pretty-print the value. */
777PyObject *
778gdbpy_get_varobj_pretty_printer (struct value *value)
779{
780 PyObject *val_obj;
781 PyObject *pretty_printer = NULL;
782 volatile struct gdb_exception except;
783
784 TRY_CATCH (except, RETURN_MASK_ALL)
785 {
786 value = value_copy (value);
787 }
788 GDB_PY_HANDLE_EXCEPTION (except);
789
790 val_obj = value_to_value_object (value);
791 if (! val_obj)
792 return NULL;
793
794 pretty_printer = find_pretty_printer (val_obj);
795 Py_DECREF (val_obj);
796 return pretty_printer;
797}
798
799/* A Python function which wraps find_pretty_printer and instantiates
800 the resulting class. This accepts a Value argument and returns a
801 pretty printer instance, or None. This function is useful as an
802 argument to the MI command -var-set-visualizer. */
803PyObject *
804gdbpy_default_visualizer (PyObject *self, PyObject *args)
805{
806 PyObject *val_obj;
f92adf3c 807 PyObject *cons;
b6313243
TT
808 struct value *value;
809
810 if (! PyArg_ParseTuple (args, "O", &val_obj))
811 return NULL;
812 value = value_object_to_value (val_obj);
813 if (! value)
814 {
044c0f87
PM
815 PyErr_SetString (PyExc_TypeError,
816 _("Argument must be a gdb.Value."));
b6313243
TT
817 return NULL;
818 }
819
820 cons = find_pretty_printer (val_obj);
821 return cons;
822}
823
a6bac58e
TT
824#else /* HAVE_PYTHON */
825
826int
827apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
828 int embedded_offset, CORE_ADDR address,
6bf0ce2b 829 struct ui_file *stream, int recurse,
0e03807e 830 const struct value *val,
6bf0ce2b 831 const struct value_print_options *options,
a6bac58e
TT
832 const struct language_defn *language)
833{
834 return 0;
835}
836
837#endif /* HAVE_PYTHON */