* python/py-arch.c (gdbpy_initialize_arch): Return 'int'.
[deliverable/binutils-gdb.git] / gdb / python / py-prettyprint.c
CommitLineData
a6bac58e
TT
1/* Python pretty-printing
2
28e7fd62 3 Copyright (C) 2008-2013 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 {
89f6d837 346 char *output;
09ca9e2e
TT
347 long length;
348 struct type *type;
349
350 make_cleanup_py_decref (string);
9a27f2c6 351#ifdef IS_PY3K
89f6d837 352 output = PyBytes_AS_STRING (string);
9a27f2c6
PK
353 length = PyBytes_GET_SIZE (string);
354#else
09ca9e2e
TT
355 output = PyString_AsString (string);
356 length = PyString_Size (string);
9a27f2c6 357#endif
09ca9e2e
TT
358 type = builtin_type (gdbarch)->builtin_char;
359
360 if (hint && !strcmp (hint, "string"))
89f6d837
PA
361 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
362 length, NULL, 0, options);
79f283fe
PM
363 else
364 fputs_filtered (output, stream);
be759fcf
PM
365 }
366 else
621c8364
TT
367 {
368 result = string_repr_error;
369 print_stack_unless_memory_error (stream);
370 }
79f283fe 371 }
09ca9e2e
TT
372
373 do_cleanups (cleanup);
a6bac58e
TT
374 }
375 else if (replacement)
269f82e5
PP
376 {
377 struct value_print_options opts = *options;
378
379 opts.addressprint = 0;
380 common_val_print (replacement, stream, recurse, &opts, language);
381 }
a6bac58e 382 else
621c8364
TT
383 {
384 result = string_repr_error;
385 print_stack_unless_memory_error (stream);
386 }
79f283fe 387
621c8364 388 return result;
a6bac58e
TT
389}
390
9a27f2c6 391#ifndef IS_PY3K
a6bac58e
TT
392static void
393py_restore_tstate (void *p)
394{
395 PyFrameObject *frame = p;
396 PyThreadState *tstate = PyThreadState_GET ();
d59b6f6c 397
a6bac58e
TT
398 tstate->frame = frame;
399}
400
401/* Create a dummy PyFrameObject, needed to work around
402 a Python-2.4 bug with generators. */
403static PyObject *
eeae04df 404push_dummy_python_frame (void)
a6bac58e
TT
405{
406 PyObject *empty_string, *null_tuple, *globals;
407 PyCodeObject *code;
408 PyFrameObject *frame;
409 PyThreadState *tstate;
410
411 empty_string = PyString_FromString ("");
412 if (!empty_string)
413 return NULL;
414
415 null_tuple = PyTuple_New (0);
416 if (!null_tuple)
417 {
418 Py_DECREF (empty_string);
419 return NULL;
420 }
421
422 code = PyCode_New (0, /* argcount */
423 0, /* nlocals */
424 0, /* stacksize */
425 0, /* flags */
426 empty_string, /* code */
427 null_tuple, /* consts */
428 null_tuple, /* names */
429 null_tuple, /* varnames */
430#if PYTHON_API_VERSION >= 1010
431 null_tuple, /* freevars */
432 null_tuple, /* cellvars */
433#endif
434 empty_string, /* filename */
435 empty_string, /* name */
436 1, /* firstlineno */
437 empty_string /* lnotab */
438 );
439
440 Py_DECREF (empty_string);
441 Py_DECREF (null_tuple);
442
443 if (!code)
444 return NULL;
445
446 globals = PyDict_New ();
447 if (!globals)
448 {
449 Py_DECREF (code);
450 return NULL;
451 }
452
453 tstate = PyThreadState_GET ();
454
455 frame = PyFrame_New (tstate, code, globals, NULL);
456
457 Py_DECREF (globals);
458 Py_DECREF (code);
459
460 if (!frame)
461 return NULL;
462
463 tstate->frame = frame;
464 make_cleanup (py_restore_tstate, frame->f_back);
465 return (PyObject *) frame;
466}
9a27f2c6 467#endif
a6bac58e
TT
468
469/* Helper for apply_val_pretty_printer that formats children of the
79f283fe
PM
470 printer, if any exist. If is_py_none is true, then nothing has
471 been printed by to_string, and format output accordingly. */
a6bac58e
TT
472static void
473print_children (PyObject *printer, const char *hint,
474 struct ui_file *stream, int recurse,
475 const struct value_print_options *options,
79f283fe
PM
476 const struct language_defn *language,
477 int is_py_none)
a6bac58e
TT
478{
479 int is_map, is_array, done_flag, pretty;
480 unsigned int i;
9a27f2c6
PK
481 PyObject *children, *iter;
482#ifndef IS_PY3K
483 PyObject *frame;
484#endif
a6bac58e
TT
485 struct cleanup *cleanups;
486
487 if (! PyObject_HasAttr (printer, gdbpy_children_cst))
488 return;
489
490 /* If we are printing a map or an array, we want some special
491 formatting. */
492 is_map = hint && ! strcmp (hint, "map");
493 is_array = hint && ! strcmp (hint, "array");
494
495 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
496 NULL);
497 if (! children)
498 {
621c8364 499 print_stack_unless_memory_error (stream);
a6bac58e
TT
500 return;
501 }
502
503 cleanups = make_cleanup_py_decref (children);
504
505 iter = PyObject_GetIter (children);
506 if (!iter)
507 {
621c8364 508 print_stack_unless_memory_error (stream);
a6bac58e
TT
509 goto done;
510 }
511 make_cleanup_py_decref (iter);
512
513 /* Use the prettyprint_arrays option if we are printing an array,
514 and the pretty option otherwise. */
a81766d8
TT
515 if (is_array)
516 pretty = options->prettyprint_arrays;
517 else
518 {
519 if (options->pretty == Val_prettyprint)
520 pretty = 1;
521 else
522 pretty = options->prettyprint_structs;
523 }
a6bac58e
TT
524
525 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
526 where it insists on having a non-NULL tstate->frame when
527 a generator is called. */
9a27f2c6 528#ifndef IS_PY3K
a6bac58e
TT
529 frame = push_dummy_python_frame ();
530 if (!frame)
531 {
532 gdbpy_print_stack ();
533 goto done;
534 }
535 make_cleanup_py_decref (frame);
9a27f2c6 536#endif
a6bac58e
TT
537
538 done_flag = 0;
539 for (i = 0; i < options->print_max; ++i)
540 {
541 PyObject *py_v, *item = PyIter_Next (iter);
ddd49eee 542 const char *name;
a6bac58e
TT
543 struct cleanup *inner_cleanup;
544
545 if (! item)
546 {
547 if (PyErr_Occurred ())
621c8364 548 print_stack_unless_memory_error (stream);
a6bac58e
TT
549 /* Set a flag so we can know whether we printed all the
550 available elements. */
551 else
552 done_flag = 1;
553 break;
554 }
555
556 if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
557 {
558 gdbpy_print_stack ();
559 Py_DECREF (item);
560 continue;
561 }
562 inner_cleanup = make_cleanup_py_decref (item);
563
564 /* Print initial "{". For other elements, there are three
565 cases:
566 1. Maps. Print a "," after each value element.
567 2. Arrays. Always print a ",".
568 3. Other. Always print a ",". */
569 if (i == 0)
79f283fe
PM
570 {
571 if (is_py_none)
572 fputs_filtered ("{", stream);
573 else
574 fputs_filtered (" = {", stream);
575 }
576
a6bac58e
TT
577 else if (! is_map || i % 2 == 0)
578 fputs_filtered (pretty ? "," : ", ", stream);
579
580 /* In summary mode, we just want to print "= {...}" if there is
581 a value. */
582 if (options->summary)
583 {
584 /* This increment tricks the post-loop logic to print what
585 we want. */
586 ++i;
587 /* Likewise. */
588 pretty = 0;
589 break;
590 }
591
592 if (! is_map || i % 2 == 0)
593 {
594 if (pretty)
595 {
596 fputs_filtered ("\n", stream);
597 print_spaces_filtered (2 + 2 * recurse, stream);
598 }
599 else
600 wrap_here (n_spaces (2 + 2 *recurse));
601 }
602
603 if (is_map && i % 2 == 0)
604 fputs_filtered ("[", stream);
605 else if (is_array)
606 {
607 /* We print the index, not whatever the child method
608 returned as the name. */
609 if (options->print_array_indexes)
610 fprintf_filtered (stream, "[%d] = ", i);
611 }
612 else if (! is_map)
613 {
614 fputs_filtered (name, stream);
615 fputs_filtered (" = ", stream);
616 }
617
09ca9e2e 618 if (gdbpy_is_lazy_string (py_v))
a6bac58e 619 {
09ca9e2e
TT
620 CORE_ADDR addr;
621 struct type *type;
622 long length;
623 char *encoding = NULL;
a81766d8 624 struct value_print_options local_opts = *options;
be759fcf 625
09ca9e2e
TT
626 make_cleanup (free_current_contents, &encoding);
627 gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
628
a81766d8 629 local_opts.addressprint = 0;
09ca9e2e 630 val_print_string (type, encoding, addr, (int) length, stream,
a81766d8 631 &local_opts);
09ca9e2e
TT
632
633 do_cleanups (inner_cleanup);
634 }
635 else if (gdbpy_is_string (py_v))
636 {
89f6d837 637 char *output;
09ca9e2e
TT
638
639 output = python_string_to_host_string (py_v);
640 if (!output)
641 gdbpy_print_stack ();
a6bac58e
TT
642 else
643 {
09ca9e2e 644 fputs_filtered (output, stream);
be759fcf 645 xfree (output);
a6bac58e
TT
646 }
647 }
648 else
649 {
650 struct value *value = convert_value_from_python (py_v);
651
652 if (value == NULL)
653 {
654 gdbpy_print_stack ();
655 error (_("Error while executing Python code."));
656 }
657 else
658 common_val_print (value, stream, recurse + 1, options, language);
659 }
660
661 if (is_map && i % 2 == 0)
662 fputs_filtered ("] = ", stream);
663
664 do_cleanups (inner_cleanup);
665 }
666
667 if (i)
668 {
669 if (!done_flag)
670 {
671 if (pretty)
672 {
673 fputs_filtered ("\n", stream);
674 print_spaces_filtered (2 + 2 * recurse, stream);
675 }
676 fputs_filtered ("...", stream);
677 }
678 if (pretty)
679 {
680 fputs_filtered ("\n", stream);
681 print_spaces_filtered (2 * recurse, stream);
682 }
683 fputs_filtered ("}", stream);
684 }
685
686 done:
687 do_cleanups (cleanups);
688}
689
690int
691apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
692 int embedded_offset, CORE_ADDR address,
693 struct ui_file *stream, int recurse,
0e03807e 694 const struct value *val,
a6bac58e
TT
695 const struct value_print_options *options,
696 const struct language_defn *language)
697{
50810684 698 struct gdbarch *gdbarch = get_type_arch (type);
a6bac58e
TT
699 PyObject *printer = NULL;
700 PyObject *val_obj = NULL;
701 struct value *value;
702 char *hint = NULL;
703 struct cleanup *cleanups;
704 int result = 0;
621c8364 705 enum string_repr_result print_result;
4e07d55f
PA
706
707 /* No pretty-printer support for unavailable values. */
63360adc 708 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
4e07d55f
PA
709 return 0;
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)
729 goto done;
730
731 /* Find the constructor. */
732 printer = find_pretty_printer (val_obj);
733 Py_DECREF (val_obj);
734 make_cleanup_py_decref (printer);
735 if (! printer || printer == Py_None)
736 goto done;
737
738 /* If we are printing a map, we want some special formatting. */
739 hint = gdbpy_get_display_hint (printer);
740 make_cleanup (free_current_contents, &hint);
741
742 /* Print the section */
621c8364
TT
743 print_result = print_string_repr (printer, hint, stream, recurse,
744 options, language, gdbarch);
745 if (print_result != string_repr_error)
746 print_children (printer, hint, stream, recurse, options, language,
747 print_result == string_repr_none);
79f283fe 748
a6bac58e
TT
749 result = 1;
750
751
752 done:
753 if (PyErr_Occurred ())
621c8364 754 print_stack_unless_memory_error (stream);
a6bac58e
TT
755 do_cleanups (cleanups);
756 return result;
757}
758
fbb8f299 759
b6313243
TT
760/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
761 print object. It must have a 'to_string' method (but this is
762 checked by varobj, not here) which takes no arguments and
fbb8f299
PM
763 returns a string. The printer will return a value and in the case
764 of a Python string being returned, this function will return a
765 PyObject containing the string. For any other type, *REPLACEMENT is
766 set to the replacement value and this function returns NULL. On
767 error, *REPLACEMENT is set to NULL and this function also returns
768 NULL. */
769PyObject *
b6313243 770apply_varobj_pretty_printer (PyObject *printer_obj,
621c8364
TT
771 struct value **replacement,
772 struct ui_file *stream)
b6313243 773{
fbb8f299 774 PyObject *py_str = NULL;
b6313243
TT
775
776 *replacement = NULL;
fbb8f299
PM
777 py_str = pretty_print_one_value (printer_obj, replacement);
778
779 if (*replacement == NULL && py_str == NULL)
621c8364 780 print_stack_unless_memory_error (stream);
b6313243 781
fbb8f299 782 return py_str;
b6313243
TT
783}
784
785/* Find a pretty-printer object for the varobj module. Returns a new
786 reference to the object if successful; returns NULL if not. VALUE
787 is the value for which a printer tests to determine if it
788 can pretty-print the value. */
789PyObject *
790gdbpy_get_varobj_pretty_printer (struct value *value)
791{
792 PyObject *val_obj;
793 PyObject *pretty_printer = NULL;
794 volatile struct gdb_exception except;
795
796 TRY_CATCH (except, RETURN_MASK_ALL)
797 {
798 value = value_copy (value);
799 }
800 GDB_PY_HANDLE_EXCEPTION (except);
801
802 val_obj = value_to_value_object (value);
803 if (! val_obj)
804 return NULL;
805
806 pretty_printer = find_pretty_printer (val_obj);
807 Py_DECREF (val_obj);
808 return pretty_printer;
809}
810
811/* A Python function which wraps find_pretty_printer and instantiates
812 the resulting class. This accepts a Value argument and returns a
813 pretty printer instance, or None. This function is useful as an
814 argument to the MI command -var-set-visualizer. */
815PyObject *
816gdbpy_default_visualizer (PyObject *self, PyObject *args)
817{
818 PyObject *val_obj;
f92adf3c 819 PyObject *cons;
b6313243
TT
820 struct value *value;
821
822 if (! PyArg_ParseTuple (args, "O", &val_obj))
823 return NULL;
824 value = value_object_to_value (val_obj);
825 if (! value)
826 {
044c0f87
PM
827 PyErr_SetString (PyExc_TypeError,
828 _("Argument must be a gdb.Value."));
b6313243
TT
829 return NULL;
830 }
831
832 cons = find_pretty_printer (val_obj);
833 return cons;
834}
835
a6bac58e
TT
836#else /* HAVE_PYTHON */
837
838int
839apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
840 int embedded_offset, CORE_ADDR address,
6bf0ce2b 841 struct ui_file *stream, int recurse,
0e03807e 842 const struct value *val,
6bf0ce2b 843 const struct value_print_options *options,
a6bac58e
TT
844 const struct language_defn *language)
845{
846 return 0;
847}
848
849#endif /* HAVE_PYTHON */
This page took 0.448864 seconds and 4 git commands to generate.