Rename gdb exception types
[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;
a70b8144 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 }
230d2906 215 catch (const gdb_exception &except)
492d29ea
PA
216 {
217 }
a6bac58e 218
a5c5eda7 219 return result;
a6bac58e
TT
220}
221
222/* Return the display hint for the object printer, PRINTER. Return
223 NULL if there is no display_hint method, or if the method did not
224 return a string. On error, print stack trace and return NULL. On
225 success, return an xmalloc()d string. */
9b972014 226gdb::unique_xmalloc_ptr<char>
a6bac58e
TT
227gdbpy_get_display_hint (PyObject *printer)
228{
9b972014 229 gdb::unique_xmalloc_ptr<char> result;
a6bac58e
TT
230
231 if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
232 return NULL;
233
7780f186
TT
234 gdbpy_ref<> hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst,
235 NULL));
0700aea5 236 if (hint != NULL)
f04e4012 237 {
0700aea5 238 if (gdbpy_is_string (hint.get ()))
8dc78533 239 {
0700aea5 240 result = python_string_to_host_string (hint.get ());
8dc78533
JK
241 if (result == NULL)
242 gdbpy_print_stack ();
243 }
f04e4012 244 }
a6bac58e
TT
245 else
246 gdbpy_print_stack ();
247
248 return result;
249}
250
621c8364
TT
251/* A wrapper for gdbpy_print_stack that ignores MemoryError. */
252
253static void
254print_stack_unless_memory_error (struct ui_file *stream)
255{
256 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
257 {
5c329e6a
TT
258 gdbpy_err_fetch fetched_error;
259 gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
621c8364
TT
260
261 if (msg == NULL || *msg == '\0')
1dae3efc 262 fprintf_filtered (stream, _("<error reading variable>"));
621c8364 263 else
9b972014
TT
264 fprintf_filtered (stream, _("<error reading variable: %s>"),
265 msg.get ());
621c8364
TT
266 }
267 else
268 gdbpy_print_stack ();
269}
270
6dddc817 271/* Helper for gdbpy_apply_val_pretty_printer which calls to_string and
621c8364
TT
272 formats the result. */
273
274static enum string_repr_result
a6bac58e
TT
275print_string_repr (PyObject *printer, const char *hint,
276 struct ui_file *stream, int recurse,
277 const struct value_print_options *options,
50810684
UW
278 const struct language_defn *language,
279 struct gdbarch *gdbarch)
a6bac58e 280{
a6bac58e 281 struct value *replacement = NULL;
621c8364 282 enum string_repr_result result = string_repr_ok;
a6bac58e 283
a5c5eda7 284 gdbpy_ref<> py_str = pretty_print_one_value (printer, &replacement);
2bd5759d 285 if (py_str != NULL)
a6bac58e 286 {
79f283fe 287 if (py_str == Py_None)
621c8364 288 result = string_repr_none;
2bd5759d 289 else if (gdbpy_is_lazy_string (py_str.get ()))
fbb8f299 290 {
09ca9e2e 291 CORE_ADDR addr;
79f283fe
PM
292 long length;
293 struct type *type;
1eba6383 294 gdb::unique_xmalloc_ptr<char> encoding;
a81766d8 295 struct value_print_options local_opts = *options;
79f283fe 296
2bd5759d 297 gdbpy_extract_lazy_string (py_str.get (), &addr, &type,
09ca9e2e
TT
298 &length, &encoding);
299
a81766d8 300 local_opts.addressprint = 0;
1eba6383 301 val_print_string (type, encoding.get (), addr, (int) length,
a81766d8 302 stream, &local_opts);
09ca9e2e
TT
303 }
304 else
305 {
7780f186 306 gdbpy_ref<> string
833d985d 307 = python_string_to_target_python_string (py_str.get ());
2bd5759d 308 if (string != NULL)
79f283fe 309 {
89f6d837 310 char *output;
09ca9e2e
TT
311 long length;
312 struct type *type;
313
2bd5759d
TT
314 output = PyBytes_AS_STRING (string.get ());
315 length = PyBytes_GET_SIZE (string.get ());
09ca9e2e
TT
316 type = builtin_type (gdbarch)->builtin_char;
317
318 if (hint && !strcmp (hint, "string"))
89f6d837
PA
319 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
320 length, NULL, 0, options);
79f283fe
PM
321 else
322 fputs_filtered (output, stream);
be759fcf
PM
323 }
324 else
621c8364
TT
325 {
326 result = string_repr_error;
327 print_stack_unless_memory_error (stream);
328 }
79f283fe 329 }
a6bac58e
TT
330 }
331 else if (replacement)
269f82e5
PP
332 {
333 struct value_print_options opts = *options;
334
335 opts.addressprint = 0;
336 common_val_print (replacement, stream, recurse, &opts, language);
337 }
a6bac58e 338 else
621c8364
TT
339 {
340 result = string_repr_error;
341 print_stack_unless_memory_error (stream);
342 }
79f283fe 343
621c8364 344 return result;
a6bac58e
TT
345}
346
6dddc817 347/* Helper for gdbpy_apply_val_pretty_printer that formats children of the
79f283fe
PM
348 printer, if any exist. If is_py_none is true, then nothing has
349 been printed by to_string, and format output accordingly. */
a6bac58e
TT
350static void
351print_children (PyObject *printer, const char *hint,
352 struct ui_file *stream, int recurse,
353 const struct value_print_options *options,
79f283fe
PM
354 const struct language_defn *language,
355 int is_py_none)
a6bac58e
TT
356{
357 int is_map, is_array, done_flag, pretty;
358 unsigned int i;
a6bac58e
TT
359
360 if (! PyObject_HasAttr (printer, gdbpy_children_cst))
361 return;
362
363 /* If we are printing a map or an array, we want some special
364 formatting. */
365 is_map = hint && ! strcmp (hint, "map");
366 is_array = hint && ! strcmp (hint, "array");
367
7780f186
TT
368 gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
369 NULL));
2bd5759d 370 if (children == NULL)
a6bac58e 371 {
621c8364 372 print_stack_unless_memory_error (stream);
a6bac58e
TT
373 return;
374 }
375
7780f186 376 gdbpy_ref<> iter (PyObject_GetIter (children.get ()));
2bd5759d 377 if (iter == NULL)
a6bac58e 378 {
621c8364 379 print_stack_unless_memory_error (stream);
2bd5759d 380 return;
a6bac58e 381 }
a6bac58e 382
2a998fc0 383 /* Use the prettyformat_arrays option if we are printing an array,
a6bac58e 384 and the pretty option otherwise. */
a81766d8 385 if (is_array)
2a998fc0 386 pretty = options->prettyformat_arrays;
a81766d8
TT
387 else
388 {
2a998fc0 389 if (options->prettyformat == Val_prettyformat)
a81766d8
TT
390 pretty = 1;
391 else
2a998fc0 392 pretty = options->prettyformat_structs;
a81766d8 393 }
a6bac58e 394
a6bac58e
TT
395 done_flag = 0;
396 for (i = 0; i < options->print_max; ++i)
397 {
2bd5759d 398 PyObject *py_v;
ddd49eee 399 const char *name;
a6bac58e 400
7780f186 401 gdbpy_ref<> item (PyIter_Next (iter.get ()));
2bd5759d 402 if (item == NULL)
a6bac58e
TT
403 {
404 if (PyErr_Occurred ())
621c8364 405 print_stack_unless_memory_error (stream);
a6bac58e
TT
406 /* Set a flag so we can know whether we printed all the
407 available elements. */
256458bc 408 else
a6bac58e
TT
409 done_flag = 1;
410 break;
411 }
412
2bd5759d 413 if (! PyTuple_Check (item.get ()) || PyTuple_Size (item.get ()) != 2)
69b4374a
DE
414 {
415 PyErr_SetString (PyExc_TypeError,
416 _("Result of children iterator not a tuple"
417 " of two elements."));
418 gdbpy_print_stack ();
69b4374a
DE
419 continue;
420 }
2bd5759d 421 if (! PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
a6bac58e 422 {
69b4374a
DE
423 /* The user won't necessarily get a stack trace here, so provide
424 more context. */
425 if (gdbpy_print_python_errors_p ())
426 fprintf_unfiltered (gdb_stderr,
427 _("Bad result from children iterator.\n"));
a6bac58e 428 gdbpy_print_stack ();
a6bac58e
TT
429 continue;
430 }
a6bac58e
TT
431
432 /* Print initial "{". For other elements, there are three
433 cases:
434 1. Maps. Print a "," after each value element.
435 2. Arrays. Always print a ",".
436 3. Other. Always print a ",". */
437 if (i == 0)
79f283fe
PM
438 {
439 if (is_py_none)
440 fputs_filtered ("{", stream);
441 else
442 fputs_filtered (" = {", stream);
443 }
444
a6bac58e
TT
445 else if (! is_map || i % 2 == 0)
446 fputs_filtered (pretty ? "," : ", ", stream);
447
448 /* In summary mode, we just want to print "= {...}" if there is
449 a value. */
450 if (options->summary)
451 {
452 /* This increment tricks the post-loop logic to print what
453 we want. */
454 ++i;
455 /* Likewise. */
456 pretty = 0;
457 break;
458 }
459
460 if (! is_map || i % 2 == 0)
461 {
462 if (pretty)
463 {
464 fputs_filtered ("\n", stream);
465 print_spaces_filtered (2 + 2 * recurse, stream);
466 }
467 else
468 wrap_here (n_spaces (2 + 2 *recurse));
469 }
470
471 if (is_map && i % 2 == 0)
472 fputs_filtered ("[", stream);
473 else if (is_array)
474 {
475 /* We print the index, not whatever the child method
476 returned as the name. */
477 if (options->print_array_indexes)
478 fprintf_filtered (stream, "[%d] = ", i);
479 }
480 else if (! is_map)
481 {
482 fputs_filtered (name, stream);
483 fputs_filtered (" = ", stream);
484 }
485
09ca9e2e 486 if (gdbpy_is_lazy_string (py_v))
a6bac58e 487 {
09ca9e2e
TT
488 CORE_ADDR addr;
489 struct type *type;
490 long length;
1eba6383 491 gdb::unique_xmalloc_ptr<char> encoding;
a81766d8 492 struct value_print_options local_opts = *options;
be759fcf 493
09ca9e2e
TT
494 gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
495
a81766d8 496 local_opts.addressprint = 0;
1eba6383 497 val_print_string (type, encoding.get (), addr, (int) length, stream,
a81766d8 498 &local_opts);
09ca9e2e
TT
499 }
500 else if (gdbpy_is_string (py_v))
501 {
9b972014 502 gdb::unique_xmalloc_ptr<char> output;
09ca9e2e
TT
503
504 output = python_string_to_host_string (py_v);
505 if (!output)
506 gdbpy_print_stack ();
a6bac58e 507 else
9b972014 508 fputs_filtered (output.get (), stream);
a6bac58e
TT
509 }
510 else
511 {
512 struct value *value = convert_value_from_python (py_v);
513
514 if (value == NULL)
515 {
516 gdbpy_print_stack ();
517 error (_("Error while executing Python code."));
518 }
519 else
520 common_val_print (value, stream, recurse + 1, options, language);
521 }
522
523 if (is_map && i % 2 == 0)
524 fputs_filtered ("] = ", stream);
a6bac58e
TT
525 }
526
527 if (i)
528 {
529 if (!done_flag)
530 {
531 if (pretty)
532 {
533 fputs_filtered ("\n", stream);
534 print_spaces_filtered (2 + 2 * recurse, stream);
535 }
536 fputs_filtered ("...", stream);
537 }
538 if (pretty)
539 {
540 fputs_filtered ("\n", stream);
541 print_spaces_filtered (2 * recurse, stream);
542 }
543 fputs_filtered ("}", stream);
544 }
a6bac58e
TT
545}
546
6dddc817
DE
547enum ext_lang_rc
548gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
668e1674 549 struct type *type,
6b850546 550 LONGEST embedded_offset, CORE_ADDR address,
6dddc817 551 struct ui_file *stream, int recurse,
668e1674 552 struct value *val,
6dddc817
DE
553 const struct value_print_options *options,
554 const struct language_defn *language)
a6bac58e 555{
50810684 556 struct gdbarch *gdbarch = get_type_arch (type);
a6bac58e 557 struct value *value;
621c8364 558 enum string_repr_result print_result;
c51f6a54
TT
559
560 if (value_lazy (val))
561 value_fetch_lazy (val);
4e07d55f
PA
562
563 /* No pretty-printer support for unavailable values. */
63360adc 564 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
6dddc817 565 return EXT_LANG_RC_NOP;
0646da15
TT
566
567 if (!gdb_python_initialized)
6dddc817 568 return EXT_LANG_RC_NOP;
4e07d55f 569
e9f0c363 570 gdbpy_enter enter_py (gdbarch, language);
a6bac58e
TT
571
572 /* Instantiate the printer. */
3fff9862 573 value = value_from_component (val, type, embedded_offset);
a6bac58e 574
7780f186 575 gdbpy_ref<> val_obj (value_to_value_object (value));
e9f0c363 576 if (val_obj == NULL)
6dddc817 577 {
e9f0c363
TT
578 print_stack_unless_memory_error (stream);
579 return EXT_LANG_RC_ERROR;
6dddc817 580 }
256458bc 581
a6bac58e 582 /* Find the constructor. */
7780f186 583 gdbpy_ref<> printer (find_pretty_printer (val_obj.get ()));
c8c735b9 584 if (printer == NULL)
6dddc817 585 {
e9f0c363
TT
586 print_stack_unless_memory_error (stream);
587 return EXT_LANG_RC_ERROR;
6dddc817 588 }
c8c735b9 589
c8c735b9 590 if (printer == Py_None)
e9f0c363 591 return EXT_LANG_RC_NOP;
a6bac58e
TT
592
593 /* If we are printing a map, we want some special formatting. */
e9f0c363 594 gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ()));
a6bac58e
TT
595
596 /* Print the section */
e9f0c363
TT
597 print_result = print_string_repr (printer.get (), hint.get (), stream,
598 recurse, options, language, gdbarch);
621c8364 599 if (print_result != string_repr_error)
e9f0c363
TT
600 print_children (printer.get (), hint.get (), stream, recurse, options,
601 language, print_result == string_repr_none);
a6bac58e 602
a6bac58e 603 if (PyErr_Occurred ())
621c8364 604 print_stack_unless_memory_error (stream);
e9f0c363 605 return EXT_LANG_RC_OK;
a6bac58e
TT
606}
607
fbb8f299 608
b6313243
TT
609/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
610 print object. It must have a 'to_string' method (but this is
611 checked by varobj, not here) which takes no arguments and
fbb8f299
PM
612 returns a string. The printer will return a value and in the case
613 of a Python string being returned, this function will return a
614 PyObject containing the string. For any other type, *REPLACEMENT is
615 set to the replacement value and this function returns NULL. On
616 error, *REPLACEMENT is set to NULL and this function also returns
617 NULL. */
a5c5eda7 618gdbpy_ref<>
b6313243 619apply_varobj_pretty_printer (PyObject *printer_obj,
621c8364
TT
620 struct value **replacement,
621 struct ui_file *stream)
b6313243 622{
b6313243 623 *replacement = NULL;
a5c5eda7 624 gdbpy_ref<> py_str = pretty_print_one_value (printer_obj, replacement);
fbb8f299
PM
625
626 if (*replacement == NULL && py_str == NULL)
621c8364 627 print_stack_unless_memory_error (stream);
b6313243 628
fbb8f299 629 return py_str;
b6313243
TT
630}
631
632/* Find a pretty-printer object for the varobj module. Returns a new
633 reference to the object if successful; returns NULL if not. VALUE
256458bc 634 is the value for which a printer tests to determine if it
b6313243 635 can pretty-print the value. */
a31abe80 636gdbpy_ref<>
b6313243
TT
637gdbpy_get_varobj_pretty_printer (struct value *value)
638{
a70b8144 639 try
b6313243
TT
640 {
641 value = value_copy (value);
642 }
230d2906 643 catch (const gdb_exception &except)
492d29ea
PA
644 {
645 GDB_PY_HANDLE_EXCEPTION (except);
646 }
256458bc 647
7780f186 648 gdbpy_ref<> val_obj (value_to_value_object (value));
0700aea5 649 if (val_obj == NULL)
b6313243
TT
650 return NULL;
651
0700aea5 652 return find_pretty_printer (val_obj.get ());
b6313243
TT
653}
654
655/* A Python function which wraps find_pretty_printer and instantiates
656 the resulting class. This accepts a Value argument and returns a
657 pretty printer instance, or None. This function is useful as an
658 argument to the MI command -var-set-visualizer. */
659PyObject *
660gdbpy_default_visualizer (PyObject *self, PyObject *args)
661{
662 PyObject *val_obj;
b6313243
TT
663 struct value *value;
664
665 if (! PyArg_ParseTuple (args, "O", &val_obj))
666 return NULL;
667 value = value_object_to_value (val_obj);
668 if (! value)
669 {
256458bc 670 PyErr_SetString (PyExc_TypeError,
044c0f87 671 _("Argument must be a gdb.Value."));
b6313243
TT
672 return NULL;
673 }
674
a31abe80 675 return find_pretty_printer (val_obj).release ();
b6313243 676}
This page took 1.471292 seconds and 4 git commands to generate.