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