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 | { |
09ca9e2e TT |
278 | struct cleanup *cleanup = make_cleanup_py_decref (py_str); |
279 | ||
79f283fe PM |
280 | if (py_str == Py_None) |
281 | is_py_none = 1; | |
09ca9e2e | 282 | else if (gdbpy_is_lazy_string (py_str)) |
fbb8f299 | 283 | { |
09ca9e2e | 284 | CORE_ADDR addr; |
79f283fe PM |
285 | long length; |
286 | struct type *type; | |
287 | char *encoding = NULL; | |
79f283fe | 288 | |
09ca9e2e TT |
289 | make_cleanup (free_current_contents, &encoding); |
290 | gdbpy_extract_lazy_string (py_str, &addr, &type, | |
291 | &length, &encoding); | |
292 | ||
293 | val_print_string (type, encoding, addr, (int) length, | |
294 | stream, options); | |
295 | } | |
296 | else | |
297 | { | |
298 | PyObject *string; | |
299 | ||
300 | string = python_string_to_target_python_string (py_str); | |
301 | if (string) | |
79f283fe | 302 | { |
09ca9e2e TT |
303 | gdb_byte *output; |
304 | long length; | |
305 | struct type *type; | |
306 | ||
307 | make_cleanup_py_decref (string); | |
308 | output = PyString_AsString (string); | |
309 | length = PyString_Size (string); | |
310 | type = builtin_type (gdbarch)->builtin_char; | |
311 | ||
312 | if (hint && !strcmp (hint, "string")) | |
313 | LA_PRINT_STRING (stream, type, output, length, NULL, | |
79f283fe PM |
314 | 0, options); |
315 | else | |
316 | fputs_filtered (output, stream); | |
be759fcf PM |
317 | } |
318 | else | |
319 | gdbpy_print_stack (); | |
79f283fe | 320 | } |
09ca9e2e TT |
321 | |
322 | do_cleanups (cleanup); | |
a6bac58e TT |
323 | } |
324 | else if (replacement) | |
269f82e5 PP |
325 | { |
326 | struct value_print_options opts = *options; | |
327 | ||
328 | opts.addressprint = 0; | |
329 | common_val_print (replacement, stream, recurse, &opts, language); | |
330 | } | |
a6bac58e TT |
331 | else |
332 | gdbpy_print_stack (); | |
79f283fe PM |
333 | |
334 | return is_py_none; | |
a6bac58e TT |
335 | } |
336 | ||
337 | static void | |
338 | py_restore_tstate (void *p) | |
339 | { | |
340 | PyFrameObject *frame = p; | |
341 | PyThreadState *tstate = PyThreadState_GET (); | |
d59b6f6c | 342 | |
a6bac58e TT |
343 | tstate->frame = frame; |
344 | } | |
345 | ||
346 | /* Create a dummy PyFrameObject, needed to work around | |
347 | a Python-2.4 bug with generators. */ | |
348 | static PyObject * | |
349 | push_dummy_python_frame () | |
350 | { | |
351 | PyObject *empty_string, *null_tuple, *globals; | |
352 | PyCodeObject *code; | |
353 | PyFrameObject *frame; | |
354 | PyThreadState *tstate; | |
355 | ||
356 | empty_string = PyString_FromString (""); | |
357 | if (!empty_string) | |
358 | return NULL; | |
359 | ||
360 | null_tuple = PyTuple_New (0); | |
361 | if (!null_tuple) | |
362 | { | |
363 | Py_DECREF (empty_string); | |
364 | return NULL; | |
365 | } | |
366 | ||
367 | code = PyCode_New (0, /* argcount */ | |
368 | 0, /* nlocals */ | |
369 | 0, /* stacksize */ | |
370 | 0, /* flags */ | |
371 | empty_string, /* code */ | |
372 | null_tuple, /* consts */ | |
373 | null_tuple, /* names */ | |
374 | null_tuple, /* varnames */ | |
375 | #if PYTHON_API_VERSION >= 1010 | |
376 | null_tuple, /* freevars */ | |
377 | null_tuple, /* cellvars */ | |
378 | #endif | |
379 | empty_string, /* filename */ | |
380 | empty_string, /* name */ | |
381 | 1, /* firstlineno */ | |
382 | empty_string /* lnotab */ | |
383 | ); | |
384 | ||
385 | Py_DECREF (empty_string); | |
386 | Py_DECREF (null_tuple); | |
387 | ||
388 | if (!code) | |
389 | return NULL; | |
390 | ||
391 | globals = PyDict_New (); | |
392 | if (!globals) | |
393 | { | |
394 | Py_DECREF (code); | |
395 | return NULL; | |
396 | } | |
397 | ||
398 | tstate = PyThreadState_GET (); | |
399 | ||
400 | frame = PyFrame_New (tstate, code, globals, NULL); | |
401 | ||
402 | Py_DECREF (globals); | |
403 | Py_DECREF (code); | |
404 | ||
405 | if (!frame) | |
406 | return NULL; | |
407 | ||
408 | tstate->frame = frame; | |
409 | make_cleanup (py_restore_tstate, frame->f_back); | |
410 | return (PyObject *) frame; | |
411 | } | |
412 | ||
413 | /* Helper for apply_val_pretty_printer that formats children of the | |
79f283fe PM |
414 | printer, if any exist. If is_py_none is true, then nothing has |
415 | been printed by to_string, and format output accordingly. */ | |
a6bac58e TT |
416 | static void |
417 | print_children (PyObject *printer, const char *hint, | |
418 | struct ui_file *stream, int recurse, | |
419 | const struct value_print_options *options, | |
79f283fe PM |
420 | const struct language_defn *language, |
421 | int is_py_none) | |
a6bac58e TT |
422 | { |
423 | int is_map, is_array, done_flag, pretty; | |
424 | unsigned int i; | |
425 | PyObject *children, *iter, *frame; | |
426 | struct cleanup *cleanups; | |
427 | ||
428 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
429 | return; | |
430 | ||
431 | /* If we are printing a map or an array, we want some special | |
432 | formatting. */ | |
433 | is_map = hint && ! strcmp (hint, "map"); | |
434 | is_array = hint && ! strcmp (hint, "array"); | |
435 | ||
436 | children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, | |
437 | NULL); | |
438 | if (! children) | |
439 | { | |
440 | gdbpy_print_stack (); | |
441 | return; | |
442 | } | |
443 | ||
444 | cleanups = make_cleanup_py_decref (children); | |
445 | ||
446 | iter = PyObject_GetIter (children); | |
447 | if (!iter) | |
448 | { | |
449 | gdbpy_print_stack (); | |
450 | goto done; | |
451 | } | |
452 | make_cleanup_py_decref (iter); | |
453 | ||
454 | /* Use the prettyprint_arrays option if we are printing an array, | |
455 | and the pretty option otherwise. */ | |
456 | pretty = is_array ? options->prettyprint_arrays : options->pretty; | |
457 | ||
458 | /* Manufacture a dummy Python frame to work around Python 2.4 bug, | |
459 | where it insists on having a non-NULL tstate->frame when | |
460 | a generator is called. */ | |
461 | frame = push_dummy_python_frame (); | |
462 | if (!frame) | |
463 | { | |
464 | gdbpy_print_stack (); | |
465 | goto done; | |
466 | } | |
467 | make_cleanup_py_decref (frame); | |
468 | ||
469 | done_flag = 0; | |
470 | for (i = 0; i < options->print_max; ++i) | |
471 | { | |
472 | PyObject *py_v, *item = PyIter_Next (iter); | |
473 | char *name; | |
474 | struct cleanup *inner_cleanup; | |
475 | ||
476 | if (! item) | |
477 | { | |
478 | if (PyErr_Occurred ()) | |
479 | gdbpy_print_stack (); | |
480 | /* Set a flag so we can know whether we printed all the | |
481 | available elements. */ | |
482 | else | |
483 | done_flag = 1; | |
484 | break; | |
485 | } | |
486 | ||
487 | if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) | |
488 | { | |
489 | gdbpy_print_stack (); | |
490 | Py_DECREF (item); | |
491 | continue; | |
492 | } | |
493 | inner_cleanup = make_cleanup_py_decref (item); | |
494 | ||
495 | /* Print initial "{". For other elements, there are three | |
496 | cases: | |
497 | 1. Maps. Print a "," after each value element. | |
498 | 2. Arrays. Always print a ",". | |
499 | 3. Other. Always print a ",". */ | |
500 | if (i == 0) | |
79f283fe PM |
501 | { |
502 | if (is_py_none) | |
503 | fputs_filtered ("{", stream); | |
504 | else | |
505 | fputs_filtered (" = {", stream); | |
506 | } | |
507 | ||
a6bac58e TT |
508 | else if (! is_map || i % 2 == 0) |
509 | fputs_filtered (pretty ? "," : ", ", stream); | |
510 | ||
511 | /* In summary mode, we just want to print "= {...}" if there is | |
512 | a value. */ | |
513 | if (options->summary) | |
514 | { | |
515 | /* This increment tricks the post-loop logic to print what | |
516 | we want. */ | |
517 | ++i; | |
518 | /* Likewise. */ | |
519 | pretty = 0; | |
520 | break; | |
521 | } | |
522 | ||
523 | if (! is_map || i % 2 == 0) | |
524 | { | |
525 | if (pretty) | |
526 | { | |
527 | fputs_filtered ("\n", stream); | |
528 | print_spaces_filtered (2 + 2 * recurse, stream); | |
529 | } | |
530 | else | |
531 | wrap_here (n_spaces (2 + 2 *recurse)); | |
532 | } | |
533 | ||
534 | if (is_map && i % 2 == 0) | |
535 | fputs_filtered ("[", stream); | |
536 | else if (is_array) | |
537 | { | |
538 | /* We print the index, not whatever the child method | |
539 | returned as the name. */ | |
540 | if (options->print_array_indexes) | |
541 | fprintf_filtered (stream, "[%d] = ", i); | |
542 | } | |
543 | else if (! is_map) | |
544 | { | |
545 | fputs_filtered (name, stream); | |
546 | fputs_filtered (" = ", stream); | |
547 | } | |
548 | ||
09ca9e2e | 549 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 550 | { |
09ca9e2e TT |
551 | CORE_ADDR addr; |
552 | struct type *type; | |
553 | long length; | |
554 | char *encoding = NULL; | |
be759fcf | 555 | |
09ca9e2e TT |
556 | make_cleanup (free_current_contents, &encoding); |
557 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); | |
558 | ||
559 | val_print_string (type, encoding, addr, (int) length, stream, | |
560 | options); | |
561 | ||
562 | do_cleanups (inner_cleanup); | |
563 | } | |
564 | else if (gdbpy_is_string (py_v)) | |
565 | { | |
566 | gdb_byte *output; | |
567 | ||
568 | output = python_string_to_host_string (py_v); | |
569 | if (!output) | |
570 | gdbpy_print_stack (); | |
a6bac58e TT |
571 | else |
572 | { | |
09ca9e2e | 573 | fputs_filtered (output, stream); |
be759fcf | 574 | xfree (output); |
a6bac58e TT |
575 | } |
576 | } | |
577 | else | |
578 | { | |
579 | struct value *value = convert_value_from_python (py_v); | |
580 | ||
581 | if (value == NULL) | |
582 | { | |
583 | gdbpy_print_stack (); | |
584 | error (_("Error while executing Python code.")); | |
585 | } | |
586 | else | |
587 | common_val_print (value, stream, recurse + 1, options, language); | |
588 | } | |
589 | ||
590 | if (is_map && i % 2 == 0) | |
591 | fputs_filtered ("] = ", stream); | |
592 | ||
593 | do_cleanups (inner_cleanup); | |
594 | } | |
595 | ||
596 | if (i) | |
597 | { | |
598 | if (!done_flag) | |
599 | { | |
600 | if (pretty) | |
601 | { | |
602 | fputs_filtered ("\n", stream); | |
603 | print_spaces_filtered (2 + 2 * recurse, stream); | |
604 | } | |
605 | fputs_filtered ("...", stream); | |
606 | } | |
607 | if (pretty) | |
608 | { | |
609 | fputs_filtered ("\n", stream); | |
610 | print_spaces_filtered (2 * recurse, stream); | |
611 | } | |
612 | fputs_filtered ("}", stream); | |
613 | } | |
614 | ||
615 | done: | |
616 | do_cleanups (cleanups); | |
617 | } | |
618 | ||
619 | int | |
620 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
621 | int embedded_offset, CORE_ADDR address, | |
622 | struct ui_file *stream, int recurse, | |
0e03807e | 623 | const struct value *val, |
a6bac58e TT |
624 | const struct value_print_options *options, |
625 | const struct language_defn *language) | |
626 | { | |
50810684 | 627 | struct gdbarch *gdbarch = get_type_arch (type); |
a6bac58e TT |
628 | PyObject *printer = NULL; |
629 | PyObject *val_obj = NULL; | |
630 | struct value *value; | |
631 | char *hint = NULL; | |
632 | struct cleanup *cleanups; | |
633 | int result = 0; | |
79f283fe | 634 | int is_py_none = 0; |
d452c4bc | 635 | cleanups = ensure_python_env (gdbarch, language); |
a6bac58e TT |
636 | |
637 | /* Instantiate the printer. */ | |
638 | if (valaddr) | |
639 | valaddr += embedded_offset; | |
edf3d5f3 TT |
640 | value = value_from_contents_and_address (type, valaddr, |
641 | address + embedded_offset); | |
0e03807e TT |
642 | if (val != NULL) |
643 | { | |
644 | set_value_component_location (value, val); | |
645 | /* set_value_component_location resets the address, so we may | |
646 | need to set it again. */ | |
647 | if (VALUE_LVAL (value) != lval_internalvar | |
648 | && VALUE_LVAL (value) != lval_internalvar_component | |
649 | && VALUE_LVAL (value) != lval_computed) | |
650 | set_value_address (value, address + embedded_offset); | |
651 | } | |
a6bac58e TT |
652 | |
653 | val_obj = value_to_value_object (value); | |
654 | if (! val_obj) | |
655 | goto done; | |
656 | ||
657 | /* Find the constructor. */ | |
658 | printer = find_pretty_printer (val_obj); | |
659 | Py_DECREF (val_obj); | |
660 | make_cleanup_py_decref (printer); | |
661 | if (! printer || printer == Py_None) | |
662 | goto done; | |
663 | ||
664 | /* If we are printing a map, we want some special formatting. */ | |
665 | hint = gdbpy_get_display_hint (printer); | |
666 | make_cleanup (free_current_contents, &hint); | |
667 | ||
668 | /* Print the section */ | |
79f283fe PM |
669 | is_py_none = print_string_repr (printer, hint, stream, recurse, |
670 | options, language, gdbarch); | |
671 | print_children (printer, hint, stream, recurse, options, language, | |
672 | is_py_none); | |
673 | ||
a6bac58e TT |
674 | result = 1; |
675 | ||
676 | ||
677 | done: | |
678 | if (PyErr_Occurred ()) | |
679 | gdbpy_print_stack (); | |
680 | do_cleanups (cleanups); | |
681 | return result; | |
682 | } | |
683 | ||
fbb8f299 | 684 | |
b6313243 TT |
685 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
686 | print object. It must have a 'to_string' method (but this is | |
687 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
688 | returns a string. The printer will return a value and in the case |
689 | of a Python string being returned, this function will return a | |
690 | PyObject containing the string. For any other type, *REPLACEMENT is | |
691 | set to the replacement value and this function returns NULL. On | |
692 | error, *REPLACEMENT is set to NULL and this function also returns | |
693 | NULL. */ | |
694 | PyObject * | |
b6313243 TT |
695 | apply_varobj_pretty_printer (PyObject *printer_obj, |
696 | struct value **replacement) | |
697 | { | |
fbb8f299 | 698 | PyObject *py_str = NULL; |
b6313243 TT |
699 | |
700 | *replacement = NULL; | |
fbb8f299 PM |
701 | py_str = pretty_print_one_value (printer_obj, replacement); |
702 | ||
703 | if (*replacement == NULL && py_str == NULL) | |
b6313243 | 704 | gdbpy_print_stack (); |
b6313243 | 705 | |
fbb8f299 | 706 | return py_str; |
b6313243 TT |
707 | } |
708 | ||
709 | /* Find a pretty-printer object for the varobj module. Returns a new | |
710 | reference to the object if successful; returns NULL if not. VALUE | |
711 | is the value for which a printer tests to determine if it | |
712 | can pretty-print the value. */ | |
713 | PyObject * | |
714 | gdbpy_get_varobj_pretty_printer (struct value *value) | |
715 | { | |
716 | PyObject *val_obj; | |
717 | PyObject *pretty_printer = NULL; | |
718 | volatile struct gdb_exception except; | |
719 | ||
720 | TRY_CATCH (except, RETURN_MASK_ALL) | |
721 | { | |
722 | value = value_copy (value); | |
723 | } | |
724 | GDB_PY_HANDLE_EXCEPTION (except); | |
725 | ||
726 | val_obj = value_to_value_object (value); | |
727 | if (! val_obj) | |
728 | return NULL; | |
729 | ||
730 | pretty_printer = find_pretty_printer (val_obj); | |
731 | Py_DECREF (val_obj); | |
732 | return pretty_printer; | |
733 | } | |
734 | ||
735 | /* A Python function which wraps find_pretty_printer and instantiates | |
736 | the resulting class. This accepts a Value argument and returns a | |
737 | pretty printer instance, or None. This function is useful as an | |
738 | argument to the MI command -var-set-visualizer. */ | |
739 | PyObject * | |
740 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
741 | { | |
742 | PyObject *val_obj; | |
f92adf3c | 743 | PyObject *cons; |
b6313243 TT |
744 | struct value *value; |
745 | ||
746 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
747 | return NULL; | |
748 | value = value_object_to_value (val_obj); | |
749 | if (! value) | |
750 | { | |
044c0f87 PM |
751 | PyErr_SetString (PyExc_TypeError, |
752 | _("Argument must be a gdb.Value.")); | |
b6313243 TT |
753 | return NULL; |
754 | } | |
755 | ||
756 | cons = find_pretty_printer (val_obj); | |
757 | return cons; | |
758 | } | |
759 | ||
a6bac58e TT |
760 | #else /* HAVE_PYTHON */ |
761 | ||
762 | int | |
763 | apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, | |
764 | int embedded_offset, CORE_ADDR address, | |
6bf0ce2b | 765 | struct ui_file *stream, int recurse, |
0e03807e | 766 | const struct value *val, |
6bf0ce2b | 767 | const struct value_print_options *options, |
a6bac58e TT |
768 | const struct language_defn *language) |
769 | { | |
770 | return 0; | |
771 | } | |
772 | ||
773 | #endif /* HAVE_PYTHON */ |