]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-prettyprint.c
2010-01-13 Phil Muldoon <pmuldoon@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / python / py-prettyprint.c
CommitLineData
a6bac58e
TT
1/* Python pretty-printing
2
4c38e0a4 3 Copyright (C) 2008, 2009, 2010 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"
21#include "exceptions.h"
22#include "objfiles.h"
23#include "symtab.h"
24#include "language.h"
25#include "valprint.h"
6bf0ce2b 26
a6bac58e 27#include "python.h"
a6bac58e
TT
28
29#ifdef HAVE_PYTHON
6bf0ce2b
PP
30#include "python-internal.h"
31
a6bac58e
TT
32
33/* Helper function for find_pretty_printer which iterates over a list,
34 calls each function and inspects output. This will return a
35 printer object if one recognizes VALUE. If no printer is found, it
36 will return None. On error, it will set the Python error and
37 return NULL. */
38static PyObject *
39search_pp_list (PyObject *list, PyObject *value)
40{
41 Py_ssize_t pp_list_size, list_index;
42 PyObject *function, *printer = NULL;
43
44 pp_list_size = PyList_Size (list);
45 for (list_index = 0; list_index < pp_list_size; list_index++)
46 {
47 function = PyList_GetItem (list, list_index);
48 if (! function)
49 return NULL;
50
51 printer = PyObject_CallFunctionObjArgs (function, value, NULL);
52 if (! printer)
53 return NULL;
54 else if (printer != Py_None)
55 return printer;
56
57 Py_DECREF (printer);
58 }
59
60 Py_RETURN_NONE;
61}
62
63/* Find the pretty-printing constructor function for VALUE. If no
64 pretty-printer exists, return None. If one exists, return a new
65 reference. On error, set the Python error and return NULL. */
66static PyObject *
67find_pretty_printer (PyObject *value)
68{
69 PyObject *pp_list = NULL;
70 PyObject *function = NULL;
71 struct objfile *obj;
72 volatile struct gdb_exception except;
73
74 /* Look at the pretty-printer dictionary for each objfile. */
75 ALL_OBJFILES (obj)
76 {
77 PyObject *objf = objfile_to_objfile_object (obj);
78 if (!objf)
79 {
80 /* Ignore the error and continue. */
81 PyErr_Clear ();
82 continue;
83 }
84
85 pp_list = objfpy_get_printers (objf, NULL);
86 function = search_pp_list (pp_list, value);
87
88 /* If there is an error in any objfile list, abort the search and
89 exit. */
90 if (! function)
91 {
92 Py_XDECREF (pp_list);
93 return NULL;
94 }
95
96 if (function != Py_None)
97 goto done;
98
99 Py_DECREF (function);
100 Py_XDECREF (pp_list);
101 }
102
103 pp_list = NULL;
104 /* Fetch the global pretty printer dictionary. */
105 if (! PyObject_HasAttrString (gdb_module, "pretty_printers"))
106 {
107 function = Py_None;
108 Py_INCREF (function);
109 goto done;
110 }
111 pp_list = PyObject_GetAttrString (gdb_module, "pretty_printers");
112 if (! pp_list)
113 goto done;
114 if (! PyList_Check (pp_list))
115 goto done;
116
117 function = search_pp_list (pp_list, value);
118
119 done:
120 Py_XDECREF (pp_list);
121
122 return function;
123}
be759fcf
PM
124
125
fbb8f299
PM
126/* Pretty-print a single value, via the printer object PRINTER.
127 If the function returns a string, a PyObject containing the string
128 is returned. Otherwise, if the function returns a value,
129 *OUT_VALUE is set to the value, and NULL is returned. On error,
130 *OUT_VALUE is set to NULL, and NULL is returned. */
131static PyObject *
a6bac58e
TT
132pretty_print_one_value (PyObject *printer, struct value **out_value)
133{
a6bac58e 134 volatile struct gdb_exception except;
fbb8f299 135 PyObject *result = NULL;
a6bac58e 136
fbb8f299 137 *out_value = NULL;
a6bac58e
TT
138 TRY_CATCH (except, RETURN_MASK_ALL)
139 {
a6bac58e
TT
140 result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
141 if (result)
142 {
be759fcf 143 if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result))
fbb8f299
PM
144 {
145 *out_value = convert_value_from_python (result);
146 if (PyErr_Occurred ())
147 *out_value = NULL;
148 Py_DECREF (result);
149 result = NULL;
150 }
a6bac58e
TT
151 }
152 }
153
fbb8f299 154 return result;
a6bac58e
TT
155}
156
157/* Return the display hint for the object printer, PRINTER. Return
158 NULL if there is no display_hint method, or if the method did not
159 return a string. On error, print stack trace and return NULL. On
160 success, return an xmalloc()d string. */
161char *
162gdbpy_get_display_hint (PyObject *printer)
163{
164 PyObject *hint;
165 char *result = NULL;
166
167 if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
168 return NULL;
169
170 hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
171 if (gdbpy_is_string (hint))
172 result = python_string_to_host_string (hint);
173 if (hint)
174 Py_DECREF (hint);
175 else
176 gdbpy_print_stack ();
177
178 return result;
179}
180
181/* Helper for apply_val_pretty_printer which calls to_string and
182 formats the result. */
183static void
184print_string_repr (PyObject *printer, const char *hint,
185 struct ui_file *stream, int recurse,
186 const struct value_print_options *options,
50810684
UW
187 const struct language_defn *language,
188 struct gdbarch *gdbarch)
a6bac58e 189{
a6bac58e 190 struct value *replacement = NULL;
fbb8f299 191 PyObject *py_str = NULL;
a6bac58e 192
fbb8f299
PM
193 py_str = pretty_print_one_value (printer, &replacement);
194 if (py_str)
a6bac58e 195 {
be759fcf
PM
196 gdb_byte *output = NULL;
197 long length;
198 struct type *type;
199 char *encoding = NULL;
200 PyObject *string = NULL;
201 int is_lazy;
202
203 is_lazy = gdbpy_is_lazy_string (py_str);
204 if (is_lazy)
205 output = gdbpy_extract_lazy_string (py_str, &type, &length, &encoding);
206 else
fbb8f299 207 {
be759fcf
PM
208 string = python_string_to_target_python_string (py_str);
209 if (string)
210 {
211 output = PyString_AsString (string);
212 length = PyString_Size (string);
213 type = builtin_type (gdbarch)->builtin_char;
214 }
215 else
216 gdbpy_print_stack ();
fbb8f299 217
be759fcf
PM
218 }
219
220 if (output)
221 {
222 if (is_lazy || (hint && !strcmp (hint, "string")))
223 LA_PRINT_STRING (stream, type, output, length, encoding,
224 0, options);
fbb8f299
PM
225 else
226 fputs_filtered (output, stream);
fbb8f299 227 }
a6bac58e 228 else
fbb8f299 229 gdbpy_print_stack ();
be759fcf
PM
230
231 if (string)
232 Py_DECREF (string);
233 else
234 xfree (output);
235
236 xfree (encoding);
fbb8f299 237 Py_DECREF (py_str);
a6bac58e
TT
238 }
239 else if (replacement)
269f82e5
PP
240 {
241 struct value_print_options opts = *options;
242
243 opts.addressprint = 0;
244 common_val_print (replacement, stream, recurse, &opts, language);
245 }
a6bac58e
TT
246 else
247 gdbpy_print_stack ();
248}
249
250static void
251py_restore_tstate (void *p)
252{
253 PyFrameObject *frame = p;
254 PyThreadState *tstate = PyThreadState_GET ();
255 tstate->frame = frame;
256}
257
258/* Create a dummy PyFrameObject, needed to work around
259 a Python-2.4 bug with generators. */
260static PyObject *
261push_dummy_python_frame ()
262{
263 PyObject *empty_string, *null_tuple, *globals;
264 PyCodeObject *code;
265 PyFrameObject *frame;
266 PyThreadState *tstate;
267
268 empty_string = PyString_FromString ("");
269 if (!empty_string)
270 return NULL;
271
272 null_tuple = PyTuple_New (0);
273 if (!null_tuple)
274 {
275 Py_DECREF (empty_string);
276 return NULL;
277 }
278
279 code = PyCode_New (0, /* argcount */
280 0, /* nlocals */
281 0, /* stacksize */
282 0, /* flags */
283 empty_string, /* code */
284 null_tuple, /* consts */
285 null_tuple, /* names */
286 null_tuple, /* varnames */
287#if PYTHON_API_VERSION >= 1010
288 null_tuple, /* freevars */
289 null_tuple, /* cellvars */
290#endif
291 empty_string, /* filename */
292 empty_string, /* name */
293 1, /* firstlineno */
294 empty_string /* lnotab */
295 );
296
297 Py_DECREF (empty_string);
298 Py_DECREF (null_tuple);
299
300 if (!code)
301 return NULL;
302
303 globals = PyDict_New ();
304 if (!globals)
305 {
306 Py_DECREF (code);
307 return NULL;
308 }
309
310 tstate = PyThreadState_GET ();
311
312 frame = PyFrame_New (tstate, code, globals, NULL);
313
314 Py_DECREF (globals);
315 Py_DECREF (code);
316
317 if (!frame)
318 return NULL;
319
320 tstate->frame = frame;
321 make_cleanup (py_restore_tstate, frame->f_back);
322 return (PyObject *) frame;
323}
324
325/* Helper for apply_val_pretty_printer that formats children of the
326 printer, if any exist. */
327static void
328print_children (PyObject *printer, const char *hint,
329 struct ui_file *stream, int recurse,
330 const struct value_print_options *options,
331 const struct language_defn *language)
332{
333 int is_map, is_array, done_flag, pretty;
334 unsigned int i;
335 PyObject *children, *iter, *frame;
336 struct cleanup *cleanups;
337
338 if (! PyObject_HasAttr (printer, gdbpy_children_cst))
339 return;
340
341 /* If we are printing a map or an array, we want some special
342 formatting. */
343 is_map = hint && ! strcmp (hint, "map");
344 is_array = hint && ! strcmp (hint, "array");
345
346 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
347 NULL);
348 if (! children)
349 {
350 gdbpy_print_stack ();
351 return;
352 }
353
354 cleanups = make_cleanup_py_decref (children);
355
356 iter = PyObject_GetIter (children);
357 if (!iter)
358 {
359 gdbpy_print_stack ();
360 goto done;
361 }
362 make_cleanup_py_decref (iter);
363
364 /* Use the prettyprint_arrays option if we are printing an array,
365 and the pretty option otherwise. */
366 pretty = is_array ? options->prettyprint_arrays : options->pretty;
367
368 /* Manufacture a dummy Python frame to work around Python 2.4 bug,
369 where it insists on having a non-NULL tstate->frame when
370 a generator is called. */
371 frame = push_dummy_python_frame ();
372 if (!frame)
373 {
374 gdbpy_print_stack ();
375 goto done;
376 }
377 make_cleanup_py_decref (frame);
378
379 done_flag = 0;
380 for (i = 0; i < options->print_max; ++i)
381 {
382 PyObject *py_v, *item = PyIter_Next (iter);
383 char *name;
384 struct cleanup *inner_cleanup;
385
386 if (! item)
387 {
388 if (PyErr_Occurred ())
389 gdbpy_print_stack ();
390 /* Set a flag so we can know whether we printed all the
391 available elements. */
392 else
393 done_flag = 1;
394 break;
395 }
396
397 if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
398 {
399 gdbpy_print_stack ();
400 Py_DECREF (item);
401 continue;
402 }
403 inner_cleanup = make_cleanup_py_decref (item);
404
405 /* Print initial "{". For other elements, there are three
406 cases:
407 1. Maps. Print a "," after each value element.
408 2. Arrays. Always print a ",".
409 3. Other. Always print a ",". */
410 if (i == 0)
411 fputs_filtered (" = {", stream);
412 else if (! is_map || i % 2 == 0)
413 fputs_filtered (pretty ? "," : ", ", stream);
414
415 /* In summary mode, we just want to print "= {...}" if there is
416 a value. */
417 if (options->summary)
418 {
419 /* This increment tricks the post-loop logic to print what
420 we want. */
421 ++i;
422 /* Likewise. */
423 pretty = 0;
424 break;
425 }
426
427 if (! is_map || i % 2 == 0)
428 {
429 if (pretty)
430 {
431 fputs_filtered ("\n", stream);
432 print_spaces_filtered (2 + 2 * recurse, stream);
433 }
434 else
435 wrap_here (n_spaces (2 + 2 *recurse));
436 }
437
438 if (is_map && i % 2 == 0)
439 fputs_filtered ("[", stream);
440 else if (is_array)
441 {
442 /* We print the index, not whatever the child method
443 returned as the name. */
444 if (options->print_array_indexes)
445 fprintf_filtered (stream, "[%d] = ", i);
446 }
447 else if (! is_map)
448 {
449 fputs_filtered (name, stream);
450 fputs_filtered (" = ", stream);
451 }
452
be759fcf 453 if (gdbpy_is_lazy_string (py_v) || gdbpy_is_string (py_v))
a6bac58e 454 {
be759fcf
PM
455 gdb_byte *output = NULL;
456
457 if (gdbpy_is_lazy_string (py_v))
458 {
459 struct type *type;
460 long length;
461 char *encoding = NULL;
462
463 output = gdbpy_extract_lazy_string (py_v, &type,
464 &length, &encoding);
465 if (!output)
466 gdbpy_print_stack ();
467 LA_PRINT_STRING (stream, type, output, length, encoding,
468 0, options);
469 xfree (encoding);
470 xfree (output);
471 }
a6bac58e
TT
472 else
473 {
be759fcf
PM
474 output = python_string_to_host_string (py_v);
475 fputs_filtered (output, stream);
476 xfree (output);
a6bac58e
TT
477 }
478 }
479 else
480 {
481 struct value *value = convert_value_from_python (py_v);
482
483 if (value == NULL)
484 {
485 gdbpy_print_stack ();
486 error (_("Error while executing Python code."));
487 }
488 else
489 common_val_print (value, stream, recurse + 1, options, language);
490 }
491
492 if (is_map && i % 2 == 0)
493 fputs_filtered ("] = ", stream);
494
495 do_cleanups (inner_cleanup);
496 }
497
498 if (i)
499 {
500 if (!done_flag)
501 {
502 if (pretty)
503 {
504 fputs_filtered ("\n", stream);
505 print_spaces_filtered (2 + 2 * recurse, stream);
506 }
507 fputs_filtered ("...", stream);
508 }
509 if (pretty)
510 {
511 fputs_filtered ("\n", stream);
512 print_spaces_filtered (2 * recurse, stream);
513 }
514 fputs_filtered ("}", stream);
515 }
516
517 done:
518 do_cleanups (cleanups);
519}
520
521int
522apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
523 int embedded_offset, CORE_ADDR address,
524 struct ui_file *stream, int recurse,
525 const struct value_print_options *options,
526 const struct language_defn *language)
527{
50810684 528 struct gdbarch *gdbarch = get_type_arch (type);
a6bac58e
TT
529 PyObject *printer = NULL;
530 PyObject *val_obj = NULL;
531 struct value *value;
532 char *hint = NULL;
533 struct cleanup *cleanups;
534 int result = 0;
a6bac58e 535
d452c4bc 536 cleanups = ensure_python_env (gdbarch, language);
a6bac58e
TT
537
538 /* Instantiate the printer. */
539 if (valaddr)
540 valaddr += embedded_offset;
541 value = value_from_contents_and_address (type, valaddr, address);
542
543 val_obj = value_to_value_object (value);
544 if (! val_obj)
545 goto done;
546
547 /* Find the constructor. */
548 printer = find_pretty_printer (val_obj);
549 Py_DECREF (val_obj);
550 make_cleanup_py_decref (printer);
551 if (! printer || printer == Py_None)
552 goto done;
553
554 /* If we are printing a map, we want some special formatting. */
555 hint = gdbpy_get_display_hint (printer);
556 make_cleanup (free_current_contents, &hint);
557
558 /* Print the section */
50810684
UW
559 print_string_repr (printer, hint, stream, recurse, options, language,
560 gdbarch);
a6bac58e
TT
561 print_children (printer, hint, stream, recurse, options, language);
562 result = 1;
563
564
565 done:
566 if (PyErr_Occurred ())
567 gdbpy_print_stack ();
568 do_cleanups (cleanups);
569 return result;
570}
571
fbb8f299 572
b6313243
TT
573/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the
574 print object. It must have a 'to_string' method (but this is
575 checked by varobj, not here) which takes no arguments and
fbb8f299
PM
576 returns a string. The printer will return a value and in the case
577 of a Python string being returned, this function will return a
578 PyObject containing the string. For any other type, *REPLACEMENT is
579 set to the replacement value and this function returns NULL. On
580 error, *REPLACEMENT is set to NULL and this function also returns
581 NULL. */
582PyObject *
b6313243
TT
583apply_varobj_pretty_printer (PyObject *printer_obj,
584 struct value **replacement)
585{
fbb8f299
PM
586 int size = 0;
587 PyObject *py_str = NULL;
b6313243
TT
588
589 *replacement = NULL;
fbb8f299
PM
590 py_str = pretty_print_one_value (printer_obj, replacement);
591
592 if (*replacement == NULL && py_str == NULL)
b6313243 593 gdbpy_print_stack ();
b6313243 594
fbb8f299 595 return py_str;
b6313243
TT
596}
597
598/* Find a pretty-printer object for the varobj module. Returns a new
599 reference to the object if successful; returns NULL if not. VALUE
600 is the value for which a printer tests to determine if it
601 can pretty-print the value. */
602PyObject *
603gdbpy_get_varobj_pretty_printer (struct value *value)
604{
605 PyObject *val_obj;
606 PyObject *pretty_printer = NULL;
607 volatile struct gdb_exception except;
608
609 TRY_CATCH (except, RETURN_MASK_ALL)
610 {
611 value = value_copy (value);
612 }
613 GDB_PY_HANDLE_EXCEPTION (except);
614
615 val_obj = value_to_value_object (value);
616 if (! val_obj)
617 return NULL;
618
619 pretty_printer = find_pretty_printer (val_obj);
620 Py_DECREF (val_obj);
621 return pretty_printer;
622}
623
624/* A Python function which wraps find_pretty_printer and instantiates
625 the resulting class. This accepts a Value argument and returns a
626 pretty printer instance, or None. This function is useful as an
627 argument to the MI command -var-set-visualizer. */
628PyObject *
629gdbpy_default_visualizer (PyObject *self, PyObject *args)
630{
631 PyObject *val_obj;
632 PyObject *cons, *printer = NULL;
633 struct value *value;
634
635 if (! PyArg_ParseTuple (args, "O", &val_obj))
636 return NULL;
637 value = value_object_to_value (val_obj);
638 if (! value)
639 {
640 PyErr_SetString (PyExc_TypeError, "argument must be a gdb.Value");
641 return NULL;
642 }
643
644 cons = find_pretty_printer (val_obj);
645 return cons;
646}
647
a6bac58e
TT
648#else /* HAVE_PYTHON */
649
650int
651apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
652 int embedded_offset, CORE_ADDR address,
6bf0ce2b
PP
653 struct ui_file *stream, int recurse,
654 const struct value_print_options *options,
a6bac58e
TT
655 const struct language_defn *language)
656{
657 return 0;
658}
659
660#endif /* HAVE_PYTHON */