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