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