Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
3 | Copyright (C) 2008, 2009 Free Software Foundation, Inc. | |
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. */ | |
38 | static PyObject * | |
39 | search_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. */ | |
66 | static PyObject * | |
67 | find_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 | } | |
fbb8f299 PM |
124 | /* Pretty-print a single value, via the printer object PRINTER. |
125 | If the function returns a string, a PyObject containing the string | |
126 | is returned. Otherwise, if the function returns a value, | |
127 | *OUT_VALUE is set to the value, and NULL is returned. On error, | |
128 | *OUT_VALUE is set to NULL, and NULL is returned. */ | |
129 | static PyObject * | |
a6bac58e TT |
130 | pretty_print_one_value (PyObject *printer, struct value **out_value) |
131 | { | |
a6bac58e | 132 | volatile struct gdb_exception except; |
fbb8f299 | 133 | PyObject *result = NULL; |
a6bac58e | 134 | |
fbb8f299 | 135 | *out_value = NULL; |
a6bac58e TT |
136 | TRY_CATCH (except, RETURN_MASK_ALL) |
137 | { | |
a6bac58e TT |
138 | result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL); |
139 | if (result) | |
140 | { | |
fbb8f299 PM |
141 | if (! gdbpy_is_string (result)) |
142 | { | |
143 | *out_value = convert_value_from_python (result); | |
144 | if (PyErr_Occurred ()) | |
145 | *out_value = NULL; | |
146 | Py_DECREF (result); | |
147 | result = NULL; | |
148 | } | |
a6bac58e TT |
149 | } |
150 | } | |
151 | ||
fbb8f299 | 152 | return result; |
a6bac58e TT |
153 | } |
154 | ||
155 | /* Return the display hint for the object printer, PRINTER. Return | |
156 | NULL if there is no display_hint method, or if the method did not | |
157 | return a string. On error, print stack trace and return NULL. On | |
158 | success, return an xmalloc()d string. */ | |
159 | char * | |
160 | gdbpy_get_display_hint (PyObject *printer) | |
161 | { | |
162 | PyObject *hint; | |
163 | char *result = NULL; | |
164 | ||
165 | if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst)) | |
166 | return NULL; | |
167 | ||
168 | hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL); | |
169 | if (gdbpy_is_string (hint)) | |
170 | result = python_string_to_host_string (hint); | |
171 | if (hint) | |
172 | Py_DECREF (hint); | |
173 | else | |
174 | gdbpy_print_stack (); | |
175 | ||
176 | return result; | |
177 | } | |
178 | ||
179 | /* Helper for apply_val_pretty_printer which calls to_string and | |
180 | formats the result. */ | |
181 | static void | |
182 | print_string_repr (PyObject *printer, const char *hint, | |
183 | struct ui_file *stream, int recurse, | |
184 | const struct value_print_options *options, | |
50810684 UW |
185 | const struct language_defn *language, |
186 | struct gdbarch *gdbarch) | |
a6bac58e | 187 | { |
a6bac58e | 188 | struct value *replacement = NULL; |
fbb8f299 | 189 | PyObject *py_str = NULL; |
a6bac58e | 190 | |
fbb8f299 PM |
191 | py_str = pretty_print_one_value (printer, &replacement); |
192 | if (py_str) | |
a6bac58e | 193 | { |
fbb8f299 PM |
194 | PyObject *string = python_string_to_target_python_string (py_str); |
195 | if (string) | |
196 | { | |
197 | gdb_byte *output = PyString_AsString (string); | |
198 | int len = PyString_Size (string); | |
199 | ||
200 | if (hint && !strcmp (hint, "string")) | |
201 | LA_PRINT_STRING (stream, builtin_type (gdbarch)->builtin_char, | |
202 | output, len, 0, options); | |
203 | else | |
204 | fputs_filtered (output, stream); | |
205 | Py_DECREF (string); | |
206 | } | |
a6bac58e | 207 | else |
fbb8f299 PM |
208 | gdbpy_print_stack (); |
209 | Py_DECREF (py_str); | |
a6bac58e TT |
210 | } |
211 | else if (replacement) | |
269f82e5 PP |
212 | { |
213 | struct value_print_options opts = *options; | |
214 | ||
215 | opts.addressprint = 0; | |
216 | common_val_print (replacement, stream, recurse, &opts, language); | |
217 | } | |
a6bac58e TT |
218 | else |
219 | gdbpy_print_stack (); | |
220 | } | |
221 | ||
222 | static void | |
223 | py_restore_tstate (void *p) | |
224 | { | |
225 | PyFrameObject *frame = p; | |
226 | PyThreadState *tstate = PyThreadState_GET (); | |
227 | tstate->frame = frame; | |
228 | } | |
229 | ||
230 | /* Create a dummy PyFrameObject, needed to work around | |
231 | a Python-2.4 bug with generators. */ | |
232 | static PyObject * | |
233 | push_dummy_python_frame () | |
234 | { | |
235 | PyObject *empty_string, *null_tuple, *globals; | |
236 | PyCodeObject *code; | |
237 | PyFrameObject *frame; | |
238 | PyThreadState *tstate; | |
239 | ||
240 | empty_string = PyString_FromString (""); | |
241 | if (!empty_string) | |
242 | return NULL; | |
243 | ||
244 | null_tuple = PyTuple_New (0); | |
245 | if (!null_tuple) | |
246 | { | |
247 | Py_DECREF (empty_string); | |
248 | return NULL; | |
249 | } | |
250 | ||
251 | code = PyCode_New (0, /* argcount */ | |
252 | 0, /* nlocals */ | |
253 | 0, /* stacksize */ | |
254 | 0, /* flags */ | |
255 | empty_string, /* code */ | |
256 | null_tuple, /* consts */ | |
257 | null_tuple, /* names */ | |
258 | null_tuple, /* varnames */ | |
259 | #if PYTHON_API_VERSION >= 1010 | |
260 | null_tuple, /* freevars */ | |
261 | null_tuple, /* cellvars */ | |
262 | #endif | |
263 | empty_string, /* filename */ | |
264 | empty_string, /* name */ | |
265 | 1, /* firstlineno */ | |
266 | empty_string /* lnotab */ | |
267 | ); | |
268 | ||
269 | Py_DECREF (empty_string); | |
270 | Py_DECREF (null_tuple); | |
271 | ||
272 | if (!code) | |
273 | return NULL; | |
274 | ||
275 | globals = PyDict_New (); | |
276 | if (!globals) | |
277 | { | |
278 | Py_DECREF (code); | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | tstate = PyThreadState_GET (); | |
283 | ||
284 | frame = PyFrame_New (tstate, code, globals, NULL); | |
285 | ||
286 | Py_DECREF (globals); | |
287 | Py_DECREF (code); | |
288 | ||
289 | if (!frame) | |
290 | return NULL; | |
291 | ||
292 | tstate->frame = frame; | |
293 | make_cleanup (py_restore_tstate, frame->f_back); | |
294 | return (PyObject *) frame; | |
295 | } | |
296 | ||
297 | /* Helper for apply_val_pretty_printer that formats children of the | |
298 | printer, if any exist. */ | |
299 | static void | |
300 | print_children (PyObject *printer, const char *hint, | |
301 | struct ui_file *stream, int recurse, | |
302 | const struct value_print_options *options, | |
303 | const struct language_defn *language) | |
304 | { | |
305 | int is_map, is_array, done_flag, pretty; | |
306 | unsigned int i; | |
307 | PyObject *children, *iter, *frame; | |
308 | struct cleanup *cleanups; | |
309 | ||
310 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
311 | return; | |
312 | ||
313 | /* If we are printing a map or an array, we want some special | |
314 | formatting. */ | |
315 | is_map = hint && ! strcmp (hint, "map"); | |
316 | is_array = hint && ! strcmp (hint, "array"); | |
317 | ||
318 | children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, | |
319 | NULL); | |
320 | if (! children) | |
321 | { | |
322 | gdbpy_print_stack (); | |
323 | return; | |
324 | } | |
325 | ||
326 | cleanups = make_cleanup_py_decref (children); | |
327 | ||
328 | iter = PyObject_GetIter (children); | |
329 | if (!iter) | |
330 | { | |
331 | gdbpy_print_stack (); | |
332 | goto done; | |
333 | } | |
334 | make_cleanup_py_decref (iter); | |
335 | ||
336 | /* Use the prettyprint_arrays option if we are printing an array, | |
337 | and the pretty option otherwise. */ | |
338 | pretty = is_array ? options->prettyprint_arrays : options->pretty; | |
339 | ||
340 | /* Manufacture a dummy Python frame to work around Python 2.4 bug, | |
341 | where it insists on having a non-NULL tstate->frame when | |
342 | a generator is called. */ | |
343 | frame = push_dummy_python_frame (); | |
344 | if (!frame) | |
345 | { | |
346 | gdbpy_print_stack (); | |
347 | goto done; | |
348 | } | |
349 | make_cleanup_py_decref (frame); | |
350 | ||
351 | done_flag = 0; | |
352 | for (i = 0; i < options->print_max; ++i) | |
353 | { | |
354 | PyObject *py_v, *item = PyIter_Next (iter); | |
355 | char *name; | |
356 | struct cleanup *inner_cleanup; | |
357 | ||
358 | if (! item) | |
359 | { | |
360 | if (PyErr_Occurred ()) | |
361 | gdbpy_print_stack (); | |
362 | /* Set a flag so we can know whether we printed all the | |
363 | available elements. */ | |
364 | else | |
365 | done_flag = 1; | |
366 | break; | |
367 | } | |
368 | ||
369 | if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) | |
370 | { | |
371 | gdbpy_print_stack (); | |
372 | Py_DECREF (item); | |
373 | continue; | |
374 | } | |
375 | inner_cleanup = make_cleanup_py_decref (item); | |
376 | ||
377 | /* Print initial "{". For other elements, there are three | |
378 | cases: | |
379 | 1. Maps. Print a "," after each value element. | |
380 | 2. Arrays. Always print a ",". | |
381 | 3. Other. Always print a ",". */ | |
382 | if (i == 0) | |
383 | fputs_filtered (" = {", stream); | |
384 | else if (! is_map || i % 2 == 0) | |
385 | fputs_filtered (pretty ? "," : ", ", stream); | |
386 | ||
387 | /* In summary mode, we just want to print "= {...}" if there is | |
388 | a value. */ | |
389 | if (options->summary) | |
390 | { | |
391 | /* This increment tricks the post-loop logic to print what | |
392 | we want. */ | |
393 | ++i; | |
394 | /* Likewise. */ | |
395 | pretty = 0; | |
396 | break; | |
397 | } | |
398 | ||
399 | if (! is_map || i % 2 == 0) | |
400 | { | |
401 | if (pretty) | |
402 | { | |
403 | fputs_filtered ("\n", stream); | |
404 | print_spaces_filtered (2 + 2 * recurse, stream); | |
405 | } | |
406 | else | |
407 | wrap_here (n_spaces (2 + 2 *recurse)); | |
408 | } | |
409 | ||
410 | if (is_map && i % 2 == 0) | |
411 | fputs_filtered ("[", stream); | |
412 | else if (is_array) | |
413 | { | |
414 | /* We print the index, not whatever the child method | |
415 | returned as the name. */ | |
416 | if (options->print_array_indexes) | |
417 | fprintf_filtered (stream, "[%d] = ", i); | |
418 | } | |
419 | else if (! is_map) | |
420 | { | |
421 | fputs_filtered (name, stream); | |
422 | fputs_filtered (" = ", stream); | |
423 | } | |
424 | ||
425 | if (gdbpy_is_string (py_v)) | |
426 | { | |
427 | char *text = python_string_to_host_string (py_v); | |
428 | if (! text) | |
429 | gdbpy_print_stack (); | |
430 | else | |
431 | { | |
432 | fputs_filtered (text, stream); | |
433 | xfree (text); | |
434 | } | |
435 | } | |
436 | else | |
437 | { | |
438 | struct value *value = convert_value_from_python (py_v); | |
439 | ||
440 | if (value == NULL) | |
441 | { | |
442 | gdbpy_print_stack (); | |
443 | error (_("Error while executing Python code.")); | |
444 | } | |
445 | else | |
446 | common_val_print (value, stream, recurse + 1, options, language); | |
447 | } | |
448 | ||
449 | if (is_map && i % 2 == 0) | |
450 | fputs_filtered ("] = ", stream); | |
451 | ||
452 | do_cleanups (inner_cleanup); | |
453 | } | |
454 | ||
455 | if (i) | |
456 | { | |
457 | if (!done_flag) | |
458 | { | |
459 | if (pretty) | |
460 | { | |
461 | fputs_filtered ("\n", stream); | |
462 | print_spaces_filtered (2 + 2 * recurse, stream); | |
463 | } | |
464 | fputs_filtered ("...", stream); | |
465 | } | |
466 | if (pretty) | |
467 | { | |
468 | fputs_filtered ("\n", stream); | |
469 | print_spaces_filtered (2 * recurse, stream); | |
470 | } | |
471 | fputs_filtered ("}", stream); | |
472 | } | |
473 | ||
474 | done: | |
475 | do_cleanups (cleanups); | |
476 | } | |
477 | ||
478 | int | |
479 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
480 | int embedded_offset, CORE_ADDR address, | |
481 | struct ui_file *stream, int recurse, | |
482 | const struct value_print_options *options, | |
483 | const struct language_defn *language) | |
484 | { | |
50810684 | 485 | struct gdbarch *gdbarch = get_type_arch (type); |
a6bac58e TT |
486 | PyObject *printer = NULL; |
487 | PyObject *val_obj = NULL; | |
488 | struct value *value; | |
489 | char *hint = NULL; | |
490 | struct cleanup *cleanups; | |
491 | int result = 0; | |
a6bac58e | 492 | |
d452c4bc | 493 | cleanups = ensure_python_env (gdbarch, language); |
a6bac58e TT |
494 | |
495 | /* Instantiate the printer. */ | |
496 | if (valaddr) | |
497 | valaddr += embedded_offset; | |
498 | value = value_from_contents_and_address (type, valaddr, address); | |
499 | ||
500 | val_obj = value_to_value_object (value); | |
501 | if (! val_obj) | |
502 | goto done; | |
503 | ||
504 | /* Find the constructor. */ | |
505 | printer = find_pretty_printer (val_obj); | |
506 | Py_DECREF (val_obj); | |
507 | make_cleanup_py_decref (printer); | |
508 | if (! printer || printer == Py_None) | |
509 | goto done; | |
510 | ||
511 | /* If we are printing a map, we want some special formatting. */ | |
512 | hint = gdbpy_get_display_hint (printer); | |
513 | make_cleanup (free_current_contents, &hint); | |
514 | ||
515 | /* Print the section */ | |
50810684 UW |
516 | print_string_repr (printer, hint, stream, recurse, options, language, |
517 | gdbarch); | |
a6bac58e TT |
518 | print_children (printer, hint, stream, recurse, options, language); |
519 | result = 1; | |
520 | ||
521 | ||
522 | done: | |
523 | if (PyErr_Occurred ()) | |
524 | gdbpy_print_stack (); | |
525 | do_cleanups (cleanups); | |
526 | return result; | |
527 | } | |
528 | ||
fbb8f299 | 529 | |
b6313243 TT |
530 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
531 | print object. It must have a 'to_string' method (but this is | |
532 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
533 | returns a string. The printer will return a value and in the case |
534 | of a Python string being returned, this function will return a | |
535 | PyObject containing the string. For any other type, *REPLACEMENT is | |
536 | set to the replacement value and this function returns NULL. On | |
537 | error, *REPLACEMENT is set to NULL and this function also returns | |
538 | NULL. */ | |
539 | PyObject * | |
b6313243 TT |
540 | apply_varobj_pretty_printer (PyObject *printer_obj, |
541 | struct value **replacement) | |
542 | { | |
fbb8f299 PM |
543 | int size = 0; |
544 | PyObject *py_str = NULL; | |
b6313243 TT |
545 | |
546 | *replacement = NULL; | |
fbb8f299 PM |
547 | py_str = pretty_print_one_value (printer_obj, replacement); |
548 | ||
549 | if (*replacement == NULL && py_str == NULL) | |
b6313243 | 550 | gdbpy_print_stack (); |
b6313243 | 551 | |
fbb8f299 | 552 | return py_str; |
b6313243 TT |
553 | } |
554 | ||
555 | /* Find a pretty-printer object for the varobj module. Returns a new | |
556 | reference to the object if successful; returns NULL if not. VALUE | |
557 | is the value for which a printer tests to determine if it | |
558 | can pretty-print the value. */ | |
559 | PyObject * | |
560 | gdbpy_get_varobj_pretty_printer (struct value *value) | |
561 | { | |
562 | PyObject *val_obj; | |
563 | PyObject *pretty_printer = NULL; | |
564 | volatile struct gdb_exception except; | |
565 | ||
566 | TRY_CATCH (except, RETURN_MASK_ALL) | |
567 | { | |
568 | value = value_copy (value); | |
569 | } | |
570 | GDB_PY_HANDLE_EXCEPTION (except); | |
571 | ||
572 | val_obj = value_to_value_object (value); | |
573 | if (! val_obj) | |
574 | return NULL; | |
575 | ||
576 | pretty_printer = find_pretty_printer (val_obj); | |
577 | Py_DECREF (val_obj); | |
578 | return pretty_printer; | |
579 | } | |
580 | ||
581 | /* A Python function which wraps find_pretty_printer and instantiates | |
582 | the resulting class. This accepts a Value argument and returns a | |
583 | pretty printer instance, or None. This function is useful as an | |
584 | argument to the MI command -var-set-visualizer. */ | |
585 | PyObject * | |
586 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
587 | { | |
588 | PyObject *val_obj; | |
589 | PyObject *cons, *printer = NULL; | |
590 | struct value *value; | |
591 | ||
592 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
593 | return NULL; | |
594 | value = value_object_to_value (val_obj); | |
595 | if (! value) | |
596 | { | |
597 | PyErr_SetString (PyExc_TypeError, "argument must be a gdb.Value"); | |
598 | return NULL; | |
599 | } | |
600 | ||
601 | cons = find_pretty_printer (val_obj); | |
602 | return cons; | |
603 | } | |
604 | ||
a6bac58e TT |
605 | #else /* HAVE_PYTHON */ |
606 | ||
607 | int | |
608 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
609 | int embedded_offset, CORE_ADDR address, | |
6bf0ce2b PP |
610 | struct ui_file *stream, int recurse, |
611 | const struct value_print_options *options, | |
a6bac58e TT |
612 | const struct language_defn *language) |
613 | { | |
614 | return 0; | |
615 | } | |
616 | ||
617 | #endif /* HAVE_PYTHON */ |