]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-prettyprint.c
ld: Clear map_head_is_link_order for .gnu_object_only
[thirdparty/binutils-gdb.git] / gdb / python / py-prettyprint.c
CommitLineData
a6bac58e
TT
1/* Python pretty-printing
2
d01e8234 3 Copyright (C) 2008-2025 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
a6bac58e
TT
20#include "objfiles.h"
21#include "symtab.h"
22#include "language.h"
23#include "valprint.h"
6dddc817 24#include "extension-priv.h"
a6bac58e 25#include "python.h"
6bf0ce2b 26#include "python-internal.h"
7f6aba03 27#include "cli/cli-style.h"
6bf0ce2b 28
fb282576
TT
29extern PyTypeObject printer_object_type;
30
621c8364
TT
31/* Return type of print_string_repr. */
32
cec000ad 33enum gdbpy_string_repr_result
621c8364
TT
34 {
35 /* The string method returned None. */
36 string_repr_none,
37 /* The string method had an error. */
38 string_repr_error,
39 /* Everything ok. */
40 string_repr_ok
41 };
42
c4a3dbaf
TT
43/* If non-null, points to options that are in effect while
44 printing. */
45const struct value_print_options *gdbpy_current_print_options;
46
a6bac58e
TT
47/* Helper function for find_pretty_printer which iterates over a list,
48 calls each function and inspects output. This will return a
49 printer object if one recognizes VALUE. If no printer is found, it
50 will return None. On error, it will set the Python error and
51 return NULL. */
fa33c3cd 52
a31abe80 53static gdbpy_ref<>
a6bac58e
TT
54search_pp_list (PyObject *list, PyObject *value)
55{
56 Py_ssize_t pp_list_size, list_index;
a6bac58e
TT
57
58 pp_list_size = PyList_Size (list);
59 for (list_index = 0; list_index < pp_list_size; list_index++)
60 {
0700aea5 61 PyObject *function = PyList_GetItem (list, list_index);
a6bac58e
TT
62 if (! function)
63 return NULL;
64
967cf477 65 /* Skip if disabled. */
1bdb0c54
TT
66 if (PyObject_HasAttr (function, gdbpy_enabled_cst))
67 {
7780f186 68 gdbpy_ref<> attr (PyObject_GetAttr (function, gdbpy_enabled_cst));
1bdb0c54
TT
69 int cmp;
70
0700aea5 71 if (attr == NULL)
1bdb0c54 72 return NULL;
0700aea5 73 cmp = PyObject_IsTrue (attr.get ());
1bdb0c54
TT
74 if (cmp == -1)
75 return NULL;
76
77 if (!cmp)
78 continue;
79 }
967cf477 80
7780f186
TT
81 gdbpy_ref<> printer (PyObject_CallFunctionObjArgs (function, value,
82 NULL));
0700aea5 83 if (printer == NULL)
a6bac58e
TT
84 return NULL;
85 else if (printer != Py_None)
a31abe80 86 return printer;
a6bac58e
TT
87 }
88
a31abe80 89 return gdbpy_ref<>::new_reference (Py_None);
a6bac58e
TT
90}
91
fa33c3cd
DE
92/* Subroutine of find_pretty_printer to simplify it.
93 Look for a pretty-printer to print VALUE in all objfiles.
94 The result is NULL if there's an error and the search should be terminated.
95 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
96 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
97
a6bac58e 98static PyObject *
fa33c3cd 99find_pretty_printer_from_objfiles (PyObject *value)
a6bac58e 100{
2030c079 101 for (objfile *obj : current_program_space->objfiles ())
aed57c53
TT
102 {
103 gdbpy_ref<> objf = objfile_to_objfile_object (obj);
104 if (objf == NULL)
105 {
106 /* Ignore the error and continue. */
107 PyErr_Clear ();
108 continue;
109 }
a6bac58e 110
aed57c53
TT
111 gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
112 gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
113
114 /* If there is an error in any objfile list, abort the search and exit. */
115 if (function == NULL)
116 return NULL;
117
118 if (function != Py_None)
119 return function.release ();
120 }
a6bac58e 121
fa33c3cd
DE
122 Py_RETURN_NONE;
123}
124
125/* Subroutine of find_pretty_printer to simplify it.
126 Look for a pretty-printer to print VALUE in the current program space.
127 The result is NULL if there's an error and the search should be terminated.
128 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
129 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
130
a31abe80 131static gdbpy_ref<>
fa33c3cd
DE
132find_pretty_printer_from_progspace (PyObject *value)
133{
3c7aa307 134 gdbpy_ref<> obj = pspace_to_pspace_object (current_program_space);
fa33c3cd 135
3c7aa307 136 if (obj == NULL)
fa33c3cd 137 return NULL;
3c7aa307 138 gdbpy_ref<> pp_list (pspy_get_printers (obj.get (), NULL));
0700aea5 139 return search_pp_list (pp_list.get (), value);
fa33c3cd
DE
140}
141
142/* Subroutine of find_pretty_printer to simplify it.
143 Look for a pretty-printer to print VALUE in the gdb module.
144 The result is NULL if there's an error and the search should be terminated.
145 The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
146 Otherwise the result is the pretty-printer function, suitably inc-ref'd. */
147
a31abe80 148static gdbpy_ref<>
fa33c3cd
DE
149find_pretty_printer_from_gdb (PyObject *value)
150{
23fa7f66 151 /* Fetch the global pretty printer list. */
b9516fa1
YPK
152 if (gdb_python_module == NULL
153 || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
a31abe80 154 return gdbpy_ref<>::new_reference (Py_None);
7780f186
TT
155 gdbpy_ref<> pp_list (PyObject_GetAttrString (gdb_python_module,
156 "pretty_printers"));
0700aea5 157 if (pp_list == NULL || ! PyList_Check (pp_list.get ()))
a31abe80 158 return gdbpy_ref<>::new_reference (Py_None);
a6bac58e 159
0700aea5 160 return search_pp_list (pp_list.get (), value);
a6bac58e 161}
be759fcf 162
fa33c3cd
DE
163/* Find the pretty-printing constructor function for VALUE. If no
164 pretty-printer exists, return None. If one exists, return a new
165 reference. On error, set the Python error and return NULL. */
166
a31abe80 167static gdbpy_ref<>
fa33c3cd
DE
168find_pretty_printer (PyObject *value)
169{
23fa7f66 170 /* Look at the pretty-printer list for each objfile
fa33c3cd 171 in the current program-space. */
7780f186 172 gdbpy_ref<> function (find_pretty_printer_from_objfiles (value));
fa33c3cd 173 if (function == NULL || function != Py_None)
a31abe80 174 return function;
fa33c3cd 175
23fa7f66 176 /* Look at the pretty-printer list for the current program-space. */
a31abe80 177 function = find_pretty_printer_from_progspace (value);
fa33c3cd 178 if (function == NULL || function != Py_None)
a31abe80 179 return function;
fa33c3cd 180
23fa7f66 181 /* Look at the pretty-printer list in the gdb module. */
0700aea5 182 return find_pretty_printer_from_gdb (value);
fa33c3cd 183}
be759fcf 184
fbb8f299
PM
185/* Pretty-print a single value, via the printer object PRINTER.
186 If the function returns a string, a PyObject containing the string
79f283fe
PM
187 is returned. If the function returns Py_NONE that means the pretty
188 printer returned the Python None as a value. Otherwise, if the
189 function returns a value, *OUT_VALUE is set to the value, and NULL
8dc78533
JK
190 is returned. On error, *OUT_VALUE is set to NULL, NULL is
191 returned, with a python exception set. */
79f283fe 192
a5c5eda7 193static gdbpy_ref<>
a6bac58e
TT
194pretty_print_one_value (PyObject *printer, struct value **out_value)
195{
1bdfaf42 196 gdbpy_ref<> result;
a6bac58e 197
fbb8f299 198 *out_value = NULL;
a70b8144 199 try
a6bac58e 200 {
332cf4c9
TT
201 if (!PyObject_HasAttr (printer, gdbpy_to_string_cst))
202 result = gdbpy_ref<>::new_reference (Py_None);
203 else
a6bac58e 204 {
332cf4c9
TT
205 result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst,
206 NULL));
207 if (result != NULL)
fbb8f299 208 {
332cf4c9
TT
209 if (! gdbpy_is_string (result.get ())
210 && ! gdbpy_is_lazy_string (result.get ())
211 && result != Py_None)
212 {
213 *out_value = convert_value_from_python (result.get ());
214 if (PyErr_Occurred ())
215 *out_value = NULL;
216 result = NULL;
217 }
fbb8f299 218 }
a6bac58e
TT
219 }
220 }
230d2906 221 catch (const gdb_exception &except)
492d29ea
PA
222 {
223 }
a6bac58e 224
a5c5eda7 225 return result;
a6bac58e
TT
226}
227
228/* Return the display hint for the object printer, PRINTER. Return
229 NULL if there is no display_hint method, or if the method did not
230 return a string. On error, print stack trace and return NULL. On
231 success, return an xmalloc()d string. */
9b972014 232gdb::unique_xmalloc_ptr<char>
a6bac58e
TT
233gdbpy_get_display_hint (PyObject *printer)
234{
9b972014 235 gdb::unique_xmalloc_ptr<char> result;
a6bac58e
TT
236
237 if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
238 return NULL;
239
7780f186
TT
240 gdbpy_ref<> hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst,
241 NULL));
0700aea5 242 if (hint != NULL)
f04e4012 243 {
0700aea5 244 if (gdbpy_is_string (hint.get ()))
8dc78533 245 {
0700aea5 246 result = python_string_to_host_string (hint.get ());
8dc78533
JK
247 if (result == NULL)
248 gdbpy_print_stack ();
249 }
f04e4012 250 }
a6bac58e
TT
251 else
252 gdbpy_print_stack ();
253
254 return result;
255}
256
621c8364
TT
257/* A wrapper for gdbpy_print_stack that ignores MemoryError. */
258
259static void
260print_stack_unless_memory_error (struct ui_file *stream)
261{
262 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
263 {
5c329e6a
TT
264 gdbpy_err_fetch fetched_error;
265 gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
621c8364
TT
266
267 if (msg == NULL || *msg == '\0')
7f6aba03
TT
268 fprintf_styled (stream, metadata_style.style (),
269 _("<error reading variable>"));
621c8364 270 else
7f6aba03
TT
271 fprintf_styled (stream, metadata_style.style (),
272 _("<error reading variable: %s>"), msg.get ());
621c8364
TT
273 }
274 else
275 gdbpy_print_stack ();
276}
277
6dddc817 278/* Helper for gdbpy_apply_val_pretty_printer which calls to_string and
621c8364
TT
279 formats the result. */
280
cec000ad 281static enum gdbpy_string_repr_result
a6bac58e
TT
282print_string_repr (PyObject *printer, const char *hint,
283 struct ui_file *stream, int recurse,
284 const struct value_print_options *options,
50810684
UW
285 const struct language_defn *language,
286 struct gdbarch *gdbarch)
a6bac58e 287{
a6bac58e 288 struct value *replacement = NULL;
cec000ad 289 enum gdbpy_string_repr_result result = string_repr_ok;
a6bac58e 290
a5c5eda7 291 gdbpy_ref<> py_str = pretty_print_one_value (printer, &replacement);
2bd5759d 292 if (py_str != NULL)
a6bac58e 293 {
79f283fe 294 if (py_str == Py_None)
621c8364 295 result = string_repr_none;
2bd5759d 296 else if (gdbpy_is_lazy_string (py_str.get ()))
fbb8f299 297 {
09ca9e2e 298 CORE_ADDR addr;
79f283fe
PM
299 long length;
300 struct type *type;
1eba6383 301 gdb::unique_xmalloc_ptr<char> encoding;
a81766d8 302 struct value_print_options local_opts = *options;
79f283fe 303
2bd5759d 304 gdbpy_extract_lazy_string (py_str.get (), &addr, &type,
09ca9e2e
TT
305 &length, &encoding);
306
dad6b350 307 local_opts.addressprint = false;
1eba6383 308 val_print_string (type, encoding.get (), addr, (int) length,
a81766d8 309 stream, &local_opts);
09ca9e2e
TT
310 }
311 else
312 {
7780f186 313 gdbpy_ref<> string
833d985d 314 = python_string_to_target_python_string (py_str.get ());
2bd5759d 315 if (string != NULL)
79f283fe 316 {
89f6d837 317 char *output;
09ca9e2e
TT
318 long length;
319 struct type *type;
320
2bd5759d
TT
321 output = PyBytes_AS_STRING (string.get ());
322 length = PyBytes_GET_SIZE (string.get ());
09ca9e2e
TT
323 type = builtin_type (gdbarch)->builtin_char;
324
325 if (hint && !strcmp (hint, "string"))
5cc0917c
AB
326 language->printstr (stream, type, (gdb_byte *) output,
327 length, NULL, 0, options);
79f283fe 328 else
0426ad51 329 gdb_puts (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
dad6b350 342 opts.addressprint = false;
269f82e5
PP
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
6dddc817 354/* Helper for gdbpy_apply_val_pretty_printer that formats children of the
79f283fe
PM
355 printer, if any exist. If is_py_none is true, then nothing has
356 been printed by to_string, and format output accordingly. */
a6bac58e
TT
357static void
358print_children (PyObject *printer, const char *hint,
359 struct ui_file *stream, int recurse,
360 const struct value_print_options *options,
79f283fe
PM
361 const struct language_defn *language,
362 int is_py_none)
a6bac58e
TT
363{
364 int is_map, is_array, done_flag, pretty;
365 unsigned int i;
a6bac58e
TT
366
367 if (! PyObject_HasAttr (printer, gdbpy_children_cst))
368 return;
369
370 /* If we are printing a map or an array, we want some special
371 formatting. */
372 is_map = hint && ! strcmp (hint, "map");
373 is_array = hint && ! strcmp (hint, "array");
374
7780f186
TT
375 gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
376 NULL));
2bd5759d 377 if (children == NULL)
a6bac58e 378 {
621c8364 379 print_stack_unless_memory_error (stream);
a6bac58e
TT
380 return;
381 }
382
7780f186 383 gdbpy_ref<> iter (PyObject_GetIter (children.get ()));
2bd5759d 384 if (iter == NULL)
a6bac58e 385 {
621c8364 386 print_stack_unless_memory_error (stream);
2bd5759d 387 return;
a6bac58e 388 }
a6bac58e 389
2a998fc0 390 /* Use the prettyformat_arrays option if we are printing an array,
a6bac58e 391 and the pretty option otherwise. */
a81766d8 392 if (is_array)
2a998fc0 393 pretty = options->prettyformat_arrays;
a81766d8
TT
394 else
395 {
2a998fc0 396 if (options->prettyformat == Val_prettyformat)
a81766d8
TT
397 pretty = 1;
398 else
2a998fc0 399 pretty = options->prettyformat_structs;
a81766d8 400 }
a6bac58e 401
a6bac58e
TT
402 done_flag = 0;
403 for (i = 0; i < options->print_max; ++i)
404 {
2bd5759d 405 PyObject *py_v;
ddd49eee 406 const char *name;
a6bac58e 407
7780f186 408 gdbpy_ref<> item (PyIter_Next (iter.get ()));
2bd5759d 409 if (item == NULL)
a6bac58e
TT
410 {
411 if (PyErr_Occurred ())
621c8364 412 print_stack_unless_memory_error (stream);
a6bac58e
TT
413 /* Set a flag so we can know whether we printed all the
414 available elements. */
256458bc 415 else
a6bac58e
TT
416 done_flag = 1;
417 break;
418 }
419
2bd5759d 420 if (! PyTuple_Check (item.get ()) || PyTuple_Size (item.get ()) != 2)
69b4374a
DE
421 {
422 PyErr_SetString (PyExc_TypeError,
423 _("Result of children iterator not a tuple"
424 " of two elements."));
425 gdbpy_print_stack ();
69b4374a
DE
426 continue;
427 }
2bd5759d 428 if (! PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
a6bac58e 429 {
69b4374a
DE
430 /* The user won't necessarily get a stack trace here, so provide
431 more context. */
432 if (gdbpy_print_python_errors_p ())
6cb06a8c
TT
433 gdb_printf (gdb_stderr,
434 _("Bad result from children iterator.\n"));
a6bac58e 435 gdbpy_print_stack ();
a6bac58e
TT
436 continue;
437 }
a6bac58e 438
ecf25064
KC
439 /* Print initial "=" to separate print_string_repr output and
440 children. For other elements, there are three cases:
a6bac58e
TT
441 1. Maps. Print a "," after each value element.
442 2. Arrays. Always print a ",".
443 3. Other. Always print a ",". */
444 if (i == 0)
01add95b
SM
445 {
446 if (!is_py_none)
0426ad51 447 gdb_puts (" = ", stream);
01add95b 448 }
a6bac58e 449 else if (! is_map || i % 2 == 0)
0426ad51 450 gdb_puts (pretty ? "," : ", ", stream);
a6bac58e 451
ecf25064
KC
452 /* Skip printing children if max_depth has been reached. This check
453 is performed after print_string_repr and the "=" separator so that
454 these steps are not skipped if the variable is located within the
455 permitted depth. */
456 if (val_print_check_max_depth (stream, recurse, options, language))
457 return;
458 else if (i == 0)
459 /* Print initial "{" to bookend children. */
0426ad51 460 gdb_puts ("{", stream);
ecf25064 461
a6bac58e
TT
462 /* In summary mode, we just want to print "= {...}" if there is
463 a value. */
464 if (options->summary)
465 {
466 /* This increment tricks the post-loop logic to print what
467 we want. */
468 ++i;
469 /* Likewise. */
470 pretty = 0;
471 break;
472 }
473
474 if (! is_map || i % 2 == 0)
475 {
476 if (pretty)
477 {
0426ad51 478 gdb_puts ("\n", stream);
d0b1020b 479 print_spaces (2 + 2 * recurse, stream);
a6bac58e
TT
480 }
481 else
1285ce86 482 stream->wrap_here (2 + 2 *recurse);
a6bac58e
TT
483 }
484
485 if (is_map && i % 2 == 0)
0426ad51 486 gdb_puts ("[", stream);
a6bac58e
TT
487 else if (is_array)
488 {
489 /* We print the index, not whatever the child method
490 returned as the name. */
491 if (options->print_array_indexes)
6cb06a8c 492 gdb_printf (stream, "[%d] = ", i);
a6bac58e
TT
493 }
494 else if (! is_map)
495 {
0426ad51
TT
496 gdb_puts (name, stream);
497 gdb_puts (" = ", stream);
a6bac58e
TT
498 }
499
09ca9e2e 500 if (gdbpy_is_lazy_string (py_v))
a6bac58e 501 {
09ca9e2e
TT
502 CORE_ADDR addr;
503 struct type *type;
504 long length;
1eba6383 505 gdb::unique_xmalloc_ptr<char> encoding;
a81766d8 506 struct value_print_options local_opts = *options;
be759fcf 507
09ca9e2e
TT
508 gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
509
dad6b350 510 local_opts.addressprint = false;
1eba6383 511 val_print_string (type, encoding.get (), addr, (int) length, stream,
a81766d8 512 &local_opts);
09ca9e2e
TT
513 }
514 else if (gdbpy_is_string (py_v))
515 {
9b972014 516 gdb::unique_xmalloc_ptr<char> output;
09ca9e2e
TT
517
518 output = python_string_to_host_string (py_v);
519 if (!output)
520 gdbpy_print_stack ();
a6bac58e 521 else
0426ad51 522 gdb_puts (output.get (), stream);
a6bac58e
TT
523 }
524 else
525 {
526 struct value *value = convert_value_from_python (py_v);
527
528 if (value == NULL)
529 {
530 gdbpy_print_stack ();
531 error (_("Error while executing Python code."));
532 }
533 else
2e62ab40
AB
534 {
535 /* When printing the key of a map we allow one additional
536 level of depth. This means the key will print before the
537 value does. */
538 struct value_print_options opt = *options;
539 if (is_map && i % 2 == 0
540 && opt.max_depth != -1
541 && opt.max_depth < INT_MAX)
542 ++opt.max_depth;
543 common_val_print (value, stream, recurse + 1, &opt, language);
544 }
a6bac58e
TT
545 }
546
547 if (is_map && i % 2 == 0)
0426ad51 548 gdb_puts ("] = ", stream);
a6bac58e
TT
549 }
550
551 if (i)
552 {
553 if (!done_flag)
554 {
555 if (pretty)
556 {
0426ad51 557 gdb_puts ("\n", stream);
d0b1020b 558 print_spaces (2 + 2 * recurse, stream);
a6bac58e 559 }
0426ad51 560 gdb_puts ("...", stream);
a6bac58e
TT
561 }
562 if (pretty)
563 {
0426ad51 564 gdb_puts ("\n", stream);
d0b1020b 565 print_spaces (2 * recurse, stream);
a6bac58e 566 }
0426ad51 567 gdb_puts ("}", stream);
a6bac58e 568 }
a6bac58e
TT
569}
570
6dddc817
DE
571enum ext_lang_rc
572gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
42331a1e 573 struct value *value,
6dddc817 574 struct ui_file *stream, int recurse,
6dddc817
DE
575 const struct value_print_options *options,
576 const struct language_defn *language)
a6bac58e 577{
d0c97917 578 struct type *type = value->type ();
8ee511af 579 struct gdbarch *gdbarch = type->arch ();
cec000ad 580 enum gdbpy_string_repr_result print_result;
c51f6a54 581
3ee3b270 582 if (value->lazy ())
78259c36 583 value->fetch_lazy ();
4e07d55f
PA
584
585 /* No pretty-printer support for unavailable values. */
d00664db 586 if (!value->bytes_available (0, type->length ()))
6dddc817 587 return EXT_LANG_RC_NOP;
0646da15
TT
588
589 if (!gdb_python_initialized)
6dddc817 590 return EXT_LANG_RC_NOP;
4e07d55f 591
e9f0c363 592 gdbpy_enter enter_py (gdbarch, language);
a6bac58e 593
f3d3bbbc 594 gdbpy_ref<> val_obj (value_to_value_object (value));
e9f0c363 595 if (val_obj == NULL)
6dddc817 596 {
e9f0c363
TT
597 print_stack_unless_memory_error (stream);
598 return EXT_LANG_RC_ERROR;
6dddc817 599 }
256458bc 600
a6bac58e 601 /* Find the constructor. */
7780f186 602 gdbpy_ref<> printer (find_pretty_printer (val_obj.get ()));
c8c735b9 603 if (printer == NULL)
6dddc817 604 {
e9f0c363
TT
605 print_stack_unless_memory_error (stream);
606 return EXT_LANG_RC_ERROR;
6dddc817 607 }
c8c735b9 608
c8c735b9 609 if (printer == Py_None)
e9f0c363 610 return EXT_LANG_RC_NOP;
a6bac58e 611
c4a3dbaf
TT
612 scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
613 options);
614
a6bac58e 615 /* If we are printing a map, we want some special formatting. */
e9f0c363 616 gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ()));
a6bac58e
TT
617
618 /* Print the section */
e9f0c363
TT
619 print_result = print_string_repr (printer.get (), hint.get (), stream,
620 recurse, options, language, gdbarch);
621c8364 621 if (print_result != string_repr_error)
e9f0c363
TT
622 print_children (printer.get (), hint.get (), stream, recurse, options,
623 language, print_result == string_repr_none);
a6bac58e 624
a6bac58e 625 if (PyErr_Occurred ())
621c8364 626 print_stack_unless_memory_error (stream);
e9f0c363 627 return EXT_LANG_RC_OK;
a6bac58e
TT
628}
629
fbb8f299 630
b6313243
TT
631/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
632 print object. It must have a 'to_string' method (but this is
633 checked by varobj, not here) which takes no arguments and
fbb8f299
PM
634 returns a string. The printer will return a value and in the case
635 of a Python string being returned, this function will return a
636 PyObject containing the string. For any other type, *REPLACEMENT is
637 set to the replacement value and this function returns NULL. On
638 error, *REPLACEMENT is set to NULL and this function also returns
639 NULL. */
a5c5eda7 640gdbpy_ref<>
b6313243 641apply_varobj_pretty_printer (PyObject *printer_obj,
621c8364 642 struct value **replacement,
c4a3dbaf
TT
643 struct ui_file *stream,
644 const value_print_options *opts)
b6313243 645{
c4a3dbaf
TT
646 scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
647 opts);
648
b6313243 649 *replacement = NULL;
a5c5eda7 650 gdbpy_ref<> py_str = pretty_print_one_value (printer_obj, replacement);
fbb8f299
PM
651
652 if (*replacement == NULL && py_str == NULL)
621c8364 653 print_stack_unless_memory_error (stream);
b6313243 654
fbb8f299 655 return py_str;
b6313243
TT
656}
657
658/* Find a pretty-printer object for the varobj module. Returns a new
659 reference to the object if successful; returns NULL if not. VALUE
256458bc 660 is the value for which a printer tests to determine if it
b6313243 661 can pretty-print the value. */
a31abe80 662gdbpy_ref<>
b6313243
TT
663gdbpy_get_varobj_pretty_printer (struct value *value)
664{
7780f186 665 gdbpy_ref<> val_obj (value_to_value_object (value));
0700aea5 666 if (val_obj == NULL)
b6313243
TT
667 return NULL;
668
0700aea5 669 return find_pretty_printer (val_obj.get ());
b6313243
TT
670}
671
672/* A Python function which wraps find_pretty_printer and instantiates
673 the resulting class. This accepts a Value argument and returns a
674 pretty printer instance, or None. This function is useful as an
675 argument to the MI command -var-set-visualizer. */
676PyObject *
677gdbpy_default_visualizer (PyObject *self, PyObject *args)
678{
679 PyObject *val_obj;
b6313243
TT
680 struct value *value;
681
682 if (! PyArg_ParseTuple (args, "O", &val_obj))
683 return NULL;
684 value = value_object_to_value (val_obj);
685 if (! value)
686 {
256458bc 687 PyErr_SetString (PyExc_TypeError,
044c0f87 688 _("Argument must be a gdb.Value."));
b6313243
TT
689 return NULL;
690 }
691
a31abe80 692 return find_pretty_printer (val_obj).release ();
b6313243 693}
c4a3dbaf
TT
694
695/* Helper function to set a boolean in a dictionary. */
696static int
697set_boolean (PyObject *dict, const char *name, bool val)
698{
699 gdbpy_ref<> val_obj (PyBool_FromLong (val));
700 if (val_obj == nullptr)
701 return -1;
702 return PyDict_SetItemString (dict, name, val_obj.get ());
703}
704
705/* Helper function to set an integer in a dictionary. */
706static int
707set_unsigned (PyObject *dict, const char *name, unsigned int val)
708{
709 gdbpy_ref<> val_obj = gdb_py_object_from_ulongest (val);
710 if (val_obj == nullptr)
711 return -1;
712 return PyDict_SetItemString (dict, name, val_obj.get ());
713}
714
715/* Implement gdb.print_options. */
716PyObject *
717gdbpy_print_options (PyObject *unused1, PyObject *unused2)
718{
719 gdbpy_ref<> result (PyDict_New ());
720 if (result == nullptr)
721 return nullptr;
722
723 value_print_options opts;
724 gdbpy_get_print_options (&opts);
725
726 if (set_boolean (result.get (), "raw",
727 opts.raw) < 0
728 || set_boolean (result.get (), "pretty_arrays",
729 opts.prettyformat_arrays) < 0
730 || set_boolean (result.get (), "pretty_structs",
731 opts.prettyformat_structs) < 0
732 || set_boolean (result.get (), "array_indexes",
733 opts.print_array_indexes) < 0
734 || set_boolean (result.get (), "symbols",
735 opts.symbol_print) < 0
736 || set_boolean (result.get (), "unions",
737 opts.unionprint) < 0
738 || set_boolean (result.get (), "address",
739 opts.addressprint) < 0
740 || set_boolean (result.get (), "deref_refs",
741 opts.deref_ref) < 0
742 || set_boolean (result.get (), "actual_objects",
743 opts.objectprint) < 0
744 || set_boolean (result.get (), "static_members",
745 opts.static_field_print) < 0
746 || set_boolean (result.get (), "deref_refs",
747 opts.deref_ref) < 0
3028a2db
TT
748 || set_boolean (result.get (), "nibbles",
749 opts.nibblesprint) < 0
72be9d6b
TT
750 || set_boolean (result.get (), "summary",
751 opts.summary) < 0
c4a3dbaf
TT
752 || set_unsigned (result.get (), "max_elements",
753 opts.print_max) < 0
754 || set_unsigned (result.get (), "max_depth",
755 opts.max_depth) < 0
756 || set_unsigned (result.get (), "repeat_threshold",
757 opts.repeat_count_threshold) < 0)
758 return nullptr;
759
760 if (opts.format != 0)
761 {
762 char str[2] = { (char) opts.format, 0 };
763 gdbpy_ref<> fmtstr = host_string_to_python_string (str);
764 if (fmtstr == nullptr)
765 return nullptr;
766 if (PyDict_SetItemString (result.get (), "format", fmtstr.get ()) < 0)
767 return nullptr;
768 }
769
770 return result.release ();
771}
772
773/* Helper function that either finds the prevailing print options, or
774 calls get_user_print_options. */
775void
776gdbpy_get_print_options (value_print_options *opts)
777{
778 if (gdbpy_current_print_options != nullptr)
779 *opts = *gdbpy_current_print_options;
780 else
781 get_user_print_options (opts);
782}
fb282576
TT
783
784/* A ValuePrinter is just a "tag", so it has no state other than that
785 required by Python. */
786struct printer_object
787{
788 PyObject_HEAD
789};
790
791/* The ValuePrinter type object. */
792PyTypeObject printer_object_type =
793{
794 PyVarObject_HEAD_INIT (NULL, 0)
795 "gdb.ValuePrinter", /*tp_name*/
796 sizeof (printer_object), /*tp_basicsize*/
797 0, /*tp_itemsize*/
798 0, /*tp_dealloc*/
799 0, /*tp_print*/
800 0, /*tp_getattr*/
801 0, /*tp_setattr*/
802 0, /*tp_compare*/
803 0, /*tp_repr*/
804 0, /*tp_as_number*/
805 0, /*tp_as_sequence*/
806 0, /*tp_as_mapping*/
807 0, /*tp_hash*/
808 0, /*tp_call*/
809 0, /*tp_str*/
810 0, /*tp_getattro*/
811 0, /*tp_setattro*/
812 0, /*tp_as_buffer*/
813 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
814 "GDB value printer object", /* tp_doc */
815 0, /* tp_traverse */
816 0, /* tp_clear */
817 0, /* tp_richcompare */
818 0, /* tp_weaklistoffset */
819 0, /* tp_iter */
820 0, /* tp_iternext */
821 0, /* tp_methods */
822 0, /* tp_members */
823 0, /* tp_getset */
824 0, /* tp_base */
825 0, /* tp_dict */
826 0, /* tp_descr_get */
827 0, /* tp_descr_set */
828 0, /* tp_dictoffset */
829 0, /* tp_init */
830 0, /* tp_alloc */
831 PyType_GenericNew, /* tp_new */
832};
833
834/* Set up the ValuePrinter type. */
835
836static int
837gdbpy_initialize_prettyprint ()
838{
336bb2a1 839 return gdbpy_type_ready (&printer_object_type);
fb282576
TT
840}
841
842GDBPY_INITIALIZE_FILE (gdbpy_initialize_prettyprint);