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