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