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