Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
b811d2c2 | 3 | Copyright (C) 2008-2020 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" | |
a6bac58e TT |
21 | #include "objfiles.h" |
22 | #include "symtab.h" | |
23 | #include "language.h" | |
24 | #include "valprint.h" | |
6dddc817 | 25 | #include "extension-priv.h" |
a6bac58e | 26 | #include "python.h" |
6bf0ce2b | 27 | #include "python-internal.h" |
7f6aba03 | 28 | #include "cli/cli-style.h" |
6bf0ce2b | 29 | |
621c8364 TT |
30 | /* Return type of print_string_repr. */ |
31 | ||
32 | enum string_repr_result | |
33 | { | |
34 | /* The string method returned None. */ | |
35 | string_repr_none, | |
36 | /* The string method had an error. */ | |
37 | string_repr_error, | |
38 | /* Everything ok. */ | |
39 | string_repr_ok | |
40 | }; | |
41 | ||
a6bac58e TT |
42 | /* Helper function for find_pretty_printer which iterates over a list, |
43 | calls each function and inspects output. This will return a | |
44 | printer object if one recognizes VALUE. If no printer is found, it | |
45 | will return None. On error, it will set the Python error and | |
46 | return NULL. */ | |
fa33c3cd | 47 | |
a31abe80 | 48 | static gdbpy_ref<> |
a6bac58e TT |
49 | search_pp_list (PyObject *list, PyObject *value) |
50 | { | |
51 | Py_ssize_t pp_list_size, list_index; | |
a6bac58e TT |
52 | |
53 | pp_list_size = PyList_Size (list); | |
54 | for (list_index = 0; list_index < pp_list_size; list_index++) | |
55 | { | |
0700aea5 | 56 | PyObject *function = PyList_GetItem (list, list_index); |
a6bac58e TT |
57 | if (! function) |
58 | return NULL; | |
59 | ||
967cf477 | 60 | /* Skip if disabled. */ |
1bdb0c54 TT |
61 | if (PyObject_HasAttr (function, gdbpy_enabled_cst)) |
62 | { | |
7780f186 | 63 | gdbpy_ref<> attr (PyObject_GetAttr (function, gdbpy_enabled_cst)); |
1bdb0c54 TT |
64 | int cmp; |
65 | ||
0700aea5 | 66 | if (attr == NULL) |
1bdb0c54 | 67 | return NULL; |
0700aea5 | 68 | cmp = PyObject_IsTrue (attr.get ()); |
1bdb0c54 TT |
69 | if (cmp == -1) |
70 | return NULL; | |
71 | ||
72 | if (!cmp) | |
73 | continue; | |
74 | } | |
967cf477 | 75 | |
7780f186 TT |
76 | gdbpy_ref<> printer (PyObject_CallFunctionObjArgs (function, value, |
77 | NULL)); | |
0700aea5 | 78 | if (printer == NULL) |
a6bac58e TT |
79 | return NULL; |
80 | else if (printer != Py_None) | |
a31abe80 | 81 | return printer; |
a6bac58e TT |
82 | } |
83 | ||
a31abe80 | 84 | return gdbpy_ref<>::new_reference (Py_None); |
a6bac58e TT |
85 | } |
86 | ||
fa33c3cd DE |
87 | /* Subroutine of find_pretty_printer to simplify it. |
88 | Look for a pretty-printer to print VALUE in all objfiles. | |
89 | The result is NULL if there's an error and the search should be terminated. | |
90 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
91 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
92 | ||
a6bac58e | 93 | static PyObject * |
fa33c3cd | 94 | find_pretty_printer_from_objfiles (PyObject *value) |
a6bac58e | 95 | { |
2030c079 | 96 | for (objfile *obj : current_program_space->objfiles ()) |
aed57c53 TT |
97 | { |
98 | gdbpy_ref<> objf = objfile_to_objfile_object (obj); | |
99 | if (objf == NULL) | |
100 | { | |
101 | /* Ignore the error and continue. */ | |
102 | PyErr_Clear (); | |
103 | continue; | |
104 | } | |
a6bac58e | 105 | |
aed57c53 TT |
106 | gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL)); |
107 | gdbpy_ref<> function (search_pp_list (pp_list.get (), value)); | |
108 | ||
109 | /* If there is an error in any objfile list, abort the search and exit. */ | |
110 | if (function == NULL) | |
111 | return NULL; | |
112 | ||
113 | if (function != Py_None) | |
114 | return function.release (); | |
115 | } | |
a6bac58e | 116 | |
fa33c3cd DE |
117 | Py_RETURN_NONE; |
118 | } | |
119 | ||
120 | /* Subroutine of find_pretty_printer to simplify it. | |
121 | Look for a pretty-printer to print VALUE in the current program space. | |
122 | The result is NULL if there's an error and the search should be terminated. | |
123 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
124 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
125 | ||
a31abe80 | 126 | static gdbpy_ref<> |
fa33c3cd DE |
127 | find_pretty_printer_from_progspace (PyObject *value) |
128 | { | |
3c7aa307 | 129 | gdbpy_ref<> obj = pspace_to_pspace_object (current_program_space); |
fa33c3cd | 130 | |
3c7aa307 | 131 | if (obj == NULL) |
fa33c3cd | 132 | return NULL; |
3c7aa307 | 133 | gdbpy_ref<> pp_list (pspy_get_printers (obj.get (), NULL)); |
0700aea5 | 134 | return search_pp_list (pp_list.get (), value); |
fa33c3cd DE |
135 | } |
136 | ||
137 | /* Subroutine of find_pretty_printer to simplify it. | |
138 | Look for a pretty-printer to print VALUE in the gdb module. | |
139 | The result is NULL if there's an error and the search should be terminated. | |
140 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
141 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
142 | ||
a31abe80 | 143 | static gdbpy_ref<> |
fa33c3cd DE |
144 | find_pretty_printer_from_gdb (PyObject *value) |
145 | { | |
23fa7f66 | 146 | /* Fetch the global pretty printer list. */ |
b9516fa1 YPK |
147 | if (gdb_python_module == NULL |
148 | || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers")) | |
a31abe80 | 149 | return gdbpy_ref<>::new_reference (Py_None); |
7780f186 TT |
150 | gdbpy_ref<> pp_list (PyObject_GetAttrString (gdb_python_module, |
151 | "pretty_printers")); | |
0700aea5 | 152 | if (pp_list == NULL || ! PyList_Check (pp_list.get ())) |
a31abe80 | 153 | return gdbpy_ref<>::new_reference (Py_None); |
a6bac58e | 154 | |
0700aea5 | 155 | return search_pp_list (pp_list.get (), value); |
a6bac58e | 156 | } |
be759fcf | 157 | |
fa33c3cd DE |
158 | /* Find the pretty-printing constructor function for VALUE. If no |
159 | pretty-printer exists, return None. If one exists, return a new | |
160 | reference. On error, set the Python error and return NULL. */ | |
161 | ||
a31abe80 | 162 | static gdbpy_ref<> |
fa33c3cd DE |
163 | find_pretty_printer (PyObject *value) |
164 | { | |
23fa7f66 | 165 | /* Look at the pretty-printer list for each objfile |
fa33c3cd | 166 | in the current program-space. */ |
7780f186 | 167 | gdbpy_ref<> function (find_pretty_printer_from_objfiles (value)); |
fa33c3cd | 168 | if (function == NULL || function != Py_None) |
a31abe80 | 169 | return function; |
fa33c3cd | 170 | |
23fa7f66 | 171 | /* Look at the pretty-printer list for the current program-space. */ |
a31abe80 | 172 | function = find_pretty_printer_from_progspace (value); |
fa33c3cd | 173 | if (function == NULL || function != Py_None) |
a31abe80 | 174 | return function; |
fa33c3cd | 175 | |
23fa7f66 | 176 | /* Look at the pretty-printer list in the gdb module. */ |
0700aea5 | 177 | return find_pretty_printer_from_gdb (value); |
fa33c3cd | 178 | } |
be759fcf | 179 | |
fbb8f299 PM |
180 | /* Pretty-print a single value, via the printer object PRINTER. |
181 | If the function returns a string, a PyObject containing the string | |
79f283fe PM |
182 | is returned. If the function returns Py_NONE that means the pretty |
183 | printer returned the Python None as a value. Otherwise, if the | |
184 | function returns a value, *OUT_VALUE is set to the value, and NULL | |
8dc78533 JK |
185 | is returned. On error, *OUT_VALUE is set to NULL, NULL is |
186 | returned, with a python exception set. */ | |
79f283fe | 187 | |
a5c5eda7 | 188 | static gdbpy_ref<> |
a6bac58e TT |
189 | pretty_print_one_value (PyObject *printer, struct value **out_value) |
190 | { | |
1bdfaf42 | 191 | gdbpy_ref<> result; |
a6bac58e | 192 | |
fbb8f299 | 193 | *out_value = NULL; |
a70b8144 | 194 | try |
a6bac58e | 195 | { |
332cf4c9 TT |
196 | if (!PyObject_HasAttr (printer, gdbpy_to_string_cst)) |
197 | result = gdbpy_ref<>::new_reference (Py_None); | |
198 | else | |
a6bac58e | 199 | { |
332cf4c9 TT |
200 | result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, |
201 | NULL)); | |
202 | if (result != NULL) | |
fbb8f299 | 203 | { |
332cf4c9 TT |
204 | if (! gdbpy_is_string (result.get ()) |
205 | && ! gdbpy_is_lazy_string (result.get ()) | |
206 | && result != Py_None) | |
207 | { | |
208 | *out_value = convert_value_from_python (result.get ()); | |
209 | if (PyErr_Occurred ()) | |
210 | *out_value = NULL; | |
211 | result = NULL; | |
212 | } | |
fbb8f299 | 213 | } |
a6bac58e TT |
214 | } |
215 | } | |
230d2906 | 216 | catch (const gdb_exception &except) |
492d29ea PA |
217 | { |
218 | } | |
a6bac58e | 219 | |
a5c5eda7 | 220 | return result; |
a6bac58e TT |
221 | } |
222 | ||
223 | /* Return the display hint for the object printer, PRINTER. Return | |
224 | NULL if there is no display_hint method, or if the method did not | |
225 | return a string. On error, print stack trace and return NULL. On | |
226 | success, return an xmalloc()d string. */ | |
9b972014 | 227 | gdb::unique_xmalloc_ptr<char> |
a6bac58e TT |
228 | gdbpy_get_display_hint (PyObject *printer) |
229 | { | |
9b972014 | 230 | gdb::unique_xmalloc_ptr<char> result; |
a6bac58e TT |
231 | |
232 | if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst)) | |
233 | return NULL; | |
234 | ||
7780f186 TT |
235 | gdbpy_ref<> hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, |
236 | NULL)); | |
0700aea5 | 237 | if (hint != NULL) |
f04e4012 | 238 | { |
0700aea5 | 239 | if (gdbpy_is_string (hint.get ())) |
8dc78533 | 240 | { |
0700aea5 | 241 | result = python_string_to_host_string (hint.get ()); |
8dc78533 JK |
242 | if (result == NULL) |
243 | gdbpy_print_stack (); | |
244 | } | |
f04e4012 | 245 | } |
a6bac58e TT |
246 | else |
247 | gdbpy_print_stack (); | |
248 | ||
249 | return result; | |
250 | } | |
251 | ||
621c8364 TT |
252 | /* A wrapper for gdbpy_print_stack that ignores MemoryError. */ |
253 | ||
254 | static void | |
255 | print_stack_unless_memory_error (struct ui_file *stream) | |
256 | { | |
257 | if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) | |
258 | { | |
5c329e6a TT |
259 | gdbpy_err_fetch fetched_error; |
260 | gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string (); | |
621c8364 TT |
261 | |
262 | if (msg == NULL || *msg == '\0') | |
7f6aba03 TT |
263 | fprintf_styled (stream, metadata_style.style (), |
264 | _("<error reading variable>")); | |
621c8364 | 265 | else |
7f6aba03 TT |
266 | fprintf_styled (stream, metadata_style.style (), |
267 | _("<error reading variable: %s>"), msg.get ()); | |
621c8364 TT |
268 | } |
269 | else | |
270 | gdbpy_print_stack (); | |
271 | } | |
272 | ||
6dddc817 | 273 | /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and |
621c8364 TT |
274 | formats the result. */ |
275 | ||
276 | static enum string_repr_result | |
a6bac58e TT |
277 | print_string_repr (PyObject *printer, const char *hint, |
278 | struct ui_file *stream, int recurse, | |
279 | const struct value_print_options *options, | |
50810684 UW |
280 | const struct language_defn *language, |
281 | struct gdbarch *gdbarch) | |
a6bac58e | 282 | { |
a6bac58e | 283 | struct value *replacement = NULL; |
621c8364 | 284 | enum string_repr_result result = string_repr_ok; |
a6bac58e | 285 | |
a5c5eda7 | 286 | gdbpy_ref<> py_str = pretty_print_one_value (printer, &replacement); |
2bd5759d | 287 | if (py_str != NULL) |
a6bac58e | 288 | { |
79f283fe | 289 | if (py_str == Py_None) |
621c8364 | 290 | result = string_repr_none; |
2bd5759d | 291 | else if (gdbpy_is_lazy_string (py_str.get ())) |
fbb8f299 | 292 | { |
09ca9e2e | 293 | CORE_ADDR addr; |
79f283fe PM |
294 | long length; |
295 | struct type *type; | |
1eba6383 | 296 | gdb::unique_xmalloc_ptr<char> encoding; |
a81766d8 | 297 | struct value_print_options local_opts = *options; |
79f283fe | 298 | |
2bd5759d | 299 | gdbpy_extract_lazy_string (py_str.get (), &addr, &type, |
09ca9e2e TT |
300 | &length, &encoding); |
301 | ||
a81766d8 | 302 | local_opts.addressprint = 0; |
1eba6383 | 303 | val_print_string (type, encoding.get (), addr, (int) length, |
a81766d8 | 304 | stream, &local_opts); |
09ca9e2e TT |
305 | } |
306 | else | |
307 | { | |
7780f186 | 308 | gdbpy_ref<> string |
833d985d | 309 | = python_string_to_target_python_string (py_str.get ()); |
2bd5759d | 310 | if (string != NULL) |
79f283fe | 311 | { |
89f6d837 | 312 | char *output; |
09ca9e2e TT |
313 | long length; |
314 | struct type *type; | |
315 | ||
2bd5759d TT |
316 | output = PyBytes_AS_STRING (string.get ()); |
317 | length = PyBytes_GET_SIZE (string.get ()); | |
09ca9e2e TT |
318 | type = builtin_type (gdbarch)->builtin_char; |
319 | ||
320 | if (hint && !strcmp (hint, "string")) | |
89f6d837 PA |
321 | LA_PRINT_STRING (stream, type, (gdb_byte *) output, |
322 | length, NULL, 0, options); | |
79f283fe PM |
323 | else |
324 | fputs_filtered (output, stream); | |
be759fcf PM |
325 | } |
326 | else | |
621c8364 TT |
327 | { |
328 | result = string_repr_error; | |
329 | print_stack_unless_memory_error (stream); | |
330 | } | |
79f283fe | 331 | } |
a6bac58e TT |
332 | } |
333 | else if (replacement) | |
269f82e5 PP |
334 | { |
335 | struct value_print_options opts = *options; | |
336 | ||
337 | opts.addressprint = 0; | |
338 | common_val_print (replacement, stream, recurse, &opts, language); | |
339 | } | |
a6bac58e | 340 | else |
621c8364 TT |
341 | { |
342 | result = string_repr_error; | |
343 | print_stack_unless_memory_error (stream); | |
344 | } | |
79f283fe | 345 | |
621c8364 | 346 | return result; |
a6bac58e TT |
347 | } |
348 | ||
6dddc817 | 349 | /* Helper for gdbpy_apply_val_pretty_printer that formats children of the |
79f283fe PM |
350 | printer, if any exist. If is_py_none is true, then nothing has |
351 | been printed by to_string, and format output accordingly. */ | |
a6bac58e TT |
352 | static void |
353 | print_children (PyObject *printer, const char *hint, | |
354 | struct ui_file *stream, int recurse, | |
355 | const struct value_print_options *options, | |
79f283fe PM |
356 | const struct language_defn *language, |
357 | int is_py_none) | |
a6bac58e TT |
358 | { |
359 | int is_map, is_array, done_flag, pretty; | |
360 | unsigned int i; | |
a6bac58e TT |
361 | |
362 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
363 | return; | |
364 | ||
365 | /* If we are printing a map or an array, we want some special | |
366 | formatting. */ | |
367 | is_map = hint && ! strcmp (hint, "map"); | |
368 | is_array = hint && ! strcmp (hint, "array"); | |
369 | ||
7780f186 TT |
370 | gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, |
371 | NULL)); | |
2bd5759d | 372 | if (children == NULL) |
a6bac58e | 373 | { |
621c8364 | 374 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
375 | return; |
376 | } | |
377 | ||
7780f186 | 378 | gdbpy_ref<> iter (PyObject_GetIter (children.get ())); |
2bd5759d | 379 | if (iter == NULL) |
a6bac58e | 380 | { |
621c8364 | 381 | print_stack_unless_memory_error (stream); |
2bd5759d | 382 | return; |
a6bac58e | 383 | } |
a6bac58e | 384 | |
2a998fc0 | 385 | /* Use the prettyformat_arrays option if we are printing an array, |
a6bac58e | 386 | and the pretty option otherwise. */ |
a81766d8 | 387 | if (is_array) |
2a998fc0 | 388 | pretty = options->prettyformat_arrays; |
a81766d8 TT |
389 | else |
390 | { | |
2a998fc0 | 391 | if (options->prettyformat == Val_prettyformat) |
a81766d8 TT |
392 | pretty = 1; |
393 | else | |
2a998fc0 | 394 | pretty = options->prettyformat_structs; |
a81766d8 | 395 | } |
a6bac58e | 396 | |
a6bac58e TT |
397 | done_flag = 0; |
398 | for (i = 0; i < options->print_max; ++i) | |
399 | { | |
2bd5759d | 400 | PyObject *py_v; |
ddd49eee | 401 | const char *name; |
a6bac58e | 402 | |
7780f186 | 403 | gdbpy_ref<> item (PyIter_Next (iter.get ())); |
2bd5759d | 404 | if (item == NULL) |
a6bac58e TT |
405 | { |
406 | if (PyErr_Occurred ()) | |
621c8364 | 407 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
408 | /* Set a flag so we can know whether we printed all the |
409 | available elements. */ | |
256458bc | 410 | else |
a6bac58e TT |
411 | done_flag = 1; |
412 | break; | |
413 | } | |
414 | ||
2bd5759d | 415 | if (! PyTuple_Check (item.get ()) || PyTuple_Size (item.get ()) != 2) |
69b4374a DE |
416 | { |
417 | PyErr_SetString (PyExc_TypeError, | |
418 | _("Result of children iterator not a tuple" | |
419 | " of two elements.")); | |
420 | gdbpy_print_stack (); | |
69b4374a DE |
421 | continue; |
422 | } | |
2bd5759d | 423 | if (! PyArg_ParseTuple (item.get (), "sO", &name, &py_v)) |
a6bac58e | 424 | { |
69b4374a DE |
425 | /* The user won't necessarily get a stack trace here, so provide |
426 | more context. */ | |
427 | if (gdbpy_print_python_errors_p ()) | |
428 | fprintf_unfiltered (gdb_stderr, | |
429 | _("Bad result from children iterator.\n")); | |
a6bac58e | 430 | gdbpy_print_stack (); |
a6bac58e TT |
431 | continue; |
432 | } | |
a6bac58e TT |
433 | |
434 | /* Print initial "{". For other elements, there are three | |
435 | cases: | |
436 | 1. Maps. Print a "," after each value element. | |
437 | 2. Arrays. Always print a ",". | |
438 | 3. Other. Always print a ",". */ | |
439 | if (i == 0) | |
79f283fe PM |
440 | { |
441 | if (is_py_none) | |
442 | fputs_filtered ("{", stream); | |
443 | else | |
444 | fputs_filtered (" = {", stream); | |
445 | } | |
446 | ||
a6bac58e TT |
447 | else if (! is_map || i % 2 == 0) |
448 | fputs_filtered (pretty ? "," : ", ", stream); | |
449 | ||
450 | /* In summary mode, we just want to print "= {...}" if there is | |
451 | a value. */ | |
452 | if (options->summary) | |
453 | { | |
454 | /* This increment tricks the post-loop logic to print what | |
455 | we want. */ | |
456 | ++i; | |
457 | /* Likewise. */ | |
458 | pretty = 0; | |
459 | break; | |
460 | } | |
461 | ||
462 | if (! is_map || i % 2 == 0) | |
463 | { | |
464 | if (pretty) | |
465 | { | |
466 | fputs_filtered ("\n", stream); | |
467 | print_spaces_filtered (2 + 2 * recurse, stream); | |
468 | } | |
469 | else | |
470 | wrap_here (n_spaces (2 + 2 *recurse)); | |
471 | } | |
472 | ||
473 | if (is_map && i % 2 == 0) | |
474 | fputs_filtered ("[", stream); | |
475 | else if (is_array) | |
476 | { | |
477 | /* We print the index, not whatever the child method | |
478 | returned as the name. */ | |
479 | if (options->print_array_indexes) | |
480 | fprintf_filtered (stream, "[%d] = ", i); | |
481 | } | |
482 | else if (! is_map) | |
483 | { | |
484 | fputs_filtered (name, stream); | |
485 | fputs_filtered (" = ", stream); | |
486 | } | |
487 | ||
09ca9e2e | 488 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 489 | { |
09ca9e2e TT |
490 | CORE_ADDR addr; |
491 | struct type *type; | |
492 | long length; | |
1eba6383 | 493 | gdb::unique_xmalloc_ptr<char> encoding; |
a81766d8 | 494 | struct value_print_options local_opts = *options; |
be759fcf | 495 | |
09ca9e2e TT |
496 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); |
497 | ||
a81766d8 | 498 | local_opts.addressprint = 0; |
1eba6383 | 499 | val_print_string (type, encoding.get (), addr, (int) length, stream, |
a81766d8 | 500 | &local_opts); |
09ca9e2e TT |
501 | } |
502 | else if (gdbpy_is_string (py_v)) | |
503 | { | |
9b972014 | 504 | gdb::unique_xmalloc_ptr<char> output; |
09ca9e2e TT |
505 | |
506 | output = python_string_to_host_string (py_v); | |
507 | if (!output) | |
508 | gdbpy_print_stack (); | |
a6bac58e | 509 | else |
9b972014 | 510 | fputs_filtered (output.get (), stream); |
a6bac58e TT |
511 | } |
512 | else | |
513 | { | |
514 | struct value *value = convert_value_from_python (py_v); | |
515 | ||
516 | if (value == NULL) | |
517 | { | |
518 | gdbpy_print_stack (); | |
519 | error (_("Error while executing Python code.")); | |
520 | } | |
521 | else | |
2e62ab40 AB |
522 | { |
523 | /* When printing the key of a map we allow one additional | |
524 | level of depth. This means the key will print before the | |
525 | value does. */ | |
526 | struct value_print_options opt = *options; | |
527 | if (is_map && i % 2 == 0 | |
528 | && opt.max_depth != -1 | |
529 | && opt.max_depth < INT_MAX) | |
530 | ++opt.max_depth; | |
531 | common_val_print (value, stream, recurse + 1, &opt, language); | |
532 | } | |
a6bac58e TT |
533 | } |
534 | ||
535 | if (is_map && i % 2 == 0) | |
536 | fputs_filtered ("] = ", stream); | |
a6bac58e TT |
537 | } |
538 | ||
539 | if (i) | |
540 | { | |
541 | if (!done_flag) | |
542 | { | |
543 | if (pretty) | |
544 | { | |
545 | fputs_filtered ("\n", stream); | |
546 | print_spaces_filtered (2 + 2 * recurse, stream); | |
547 | } | |
548 | fputs_filtered ("...", stream); | |
549 | } | |
550 | if (pretty) | |
551 | { | |
552 | fputs_filtered ("\n", stream); | |
553 | print_spaces_filtered (2 * recurse, stream); | |
554 | } | |
555 | fputs_filtered ("}", stream); | |
556 | } | |
a6bac58e TT |
557 | } |
558 | ||
6dddc817 DE |
559 | enum ext_lang_rc |
560 | gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, | |
42331a1e | 561 | struct value *value, |
6dddc817 | 562 | struct ui_file *stream, int recurse, |
6dddc817 DE |
563 | const struct value_print_options *options, |
564 | const struct language_defn *language) | |
a6bac58e | 565 | { |
42331a1e | 566 | struct type *type = value_type (value); |
50810684 | 567 | struct gdbarch *gdbarch = get_type_arch (type); |
621c8364 | 568 | enum string_repr_result print_result; |
c51f6a54 | 569 | |
42331a1e TT |
570 | if (value_lazy (value)) |
571 | value_fetch_lazy (value); | |
4e07d55f PA |
572 | |
573 | /* No pretty-printer support for unavailable values. */ | |
42331a1e | 574 | if (!value_bytes_available (value, 0, TYPE_LENGTH (type))) |
6dddc817 | 575 | return EXT_LANG_RC_NOP; |
0646da15 TT |
576 | |
577 | if (!gdb_python_initialized) | |
6dddc817 | 578 | return EXT_LANG_RC_NOP; |
4e07d55f | 579 | |
e9f0c363 | 580 | gdbpy_enter enter_py (gdbarch, language); |
a6bac58e | 581 | |
42331a1e | 582 | gdbpy_ref<> val_obj (value_to_value_object_no_release (value)); |
e9f0c363 | 583 | if (val_obj == NULL) |
6dddc817 | 584 | { |
e9f0c363 TT |
585 | print_stack_unless_memory_error (stream); |
586 | return EXT_LANG_RC_ERROR; | |
6dddc817 | 587 | } |
256458bc | 588 | |
a6bac58e | 589 | /* Find the constructor. */ |
7780f186 | 590 | gdbpy_ref<> printer (find_pretty_printer (val_obj.get ())); |
c8c735b9 | 591 | if (printer == NULL) |
6dddc817 | 592 | { |
e9f0c363 TT |
593 | print_stack_unless_memory_error (stream); |
594 | return EXT_LANG_RC_ERROR; | |
6dddc817 | 595 | } |
c8c735b9 | 596 | |
c8c735b9 | 597 | if (printer == Py_None) |
e9f0c363 | 598 | return EXT_LANG_RC_NOP; |
a6bac58e | 599 | |
2e62ab40 AB |
600 | if (val_print_check_max_depth (stream, recurse, options, language)) |
601 | return EXT_LANG_RC_OK; | |
602 | ||
a6bac58e | 603 | /* If we are printing a map, we want some special formatting. */ |
e9f0c363 | 604 | gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ())); |
a6bac58e TT |
605 | |
606 | /* Print the section */ | |
e9f0c363 TT |
607 | print_result = print_string_repr (printer.get (), hint.get (), stream, |
608 | recurse, options, language, gdbarch); | |
621c8364 | 609 | if (print_result != string_repr_error) |
e9f0c363 TT |
610 | print_children (printer.get (), hint.get (), stream, recurse, options, |
611 | language, print_result == string_repr_none); | |
a6bac58e | 612 | |
a6bac58e | 613 | if (PyErr_Occurred ()) |
621c8364 | 614 | print_stack_unless_memory_error (stream); |
e9f0c363 | 615 | return EXT_LANG_RC_OK; |
a6bac58e TT |
616 | } |
617 | ||
fbb8f299 | 618 | |
b6313243 TT |
619 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
620 | print object. It must have a 'to_string' method (but this is | |
621 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
622 | returns a string. The printer will return a value and in the case |
623 | of a Python string being returned, this function will return a | |
624 | PyObject containing the string. For any other type, *REPLACEMENT is | |
625 | set to the replacement value and this function returns NULL. On | |
626 | error, *REPLACEMENT is set to NULL and this function also returns | |
627 | NULL. */ | |
a5c5eda7 | 628 | gdbpy_ref<> |
b6313243 | 629 | apply_varobj_pretty_printer (PyObject *printer_obj, |
621c8364 TT |
630 | struct value **replacement, |
631 | struct ui_file *stream) | |
b6313243 | 632 | { |
b6313243 | 633 | *replacement = NULL; |
a5c5eda7 | 634 | gdbpy_ref<> py_str = pretty_print_one_value (printer_obj, replacement); |
fbb8f299 PM |
635 | |
636 | if (*replacement == NULL && py_str == NULL) | |
621c8364 | 637 | print_stack_unless_memory_error (stream); |
b6313243 | 638 | |
fbb8f299 | 639 | return py_str; |
b6313243 TT |
640 | } |
641 | ||
642 | /* Find a pretty-printer object for the varobj module. Returns a new | |
643 | reference to the object if successful; returns NULL if not. VALUE | |
256458bc | 644 | is the value for which a printer tests to determine if it |
b6313243 | 645 | can pretty-print the value. */ |
a31abe80 | 646 | gdbpy_ref<> |
b6313243 TT |
647 | gdbpy_get_varobj_pretty_printer (struct value *value) |
648 | { | |
a70b8144 | 649 | try |
b6313243 TT |
650 | { |
651 | value = value_copy (value); | |
652 | } | |
230d2906 | 653 | catch (const gdb_exception &except) |
492d29ea PA |
654 | { |
655 | GDB_PY_HANDLE_EXCEPTION (except); | |
656 | } | |
256458bc | 657 | |
7780f186 | 658 | gdbpy_ref<> val_obj (value_to_value_object (value)); |
0700aea5 | 659 | if (val_obj == NULL) |
b6313243 TT |
660 | return NULL; |
661 | ||
0700aea5 | 662 | return find_pretty_printer (val_obj.get ()); |
b6313243 TT |
663 | } |
664 | ||
665 | /* A Python function which wraps find_pretty_printer and instantiates | |
666 | the resulting class. This accepts a Value argument and returns a | |
667 | pretty printer instance, or None. This function is useful as an | |
668 | argument to the MI command -var-set-visualizer. */ | |
669 | PyObject * | |
670 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
671 | { | |
672 | PyObject *val_obj; | |
b6313243 TT |
673 | struct value *value; |
674 | ||
675 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
676 | return NULL; | |
677 | value = value_object_to_value (val_obj); | |
678 | if (! value) | |
679 | { | |
256458bc | 680 | PyErr_SetString (PyExc_TypeError, |
044c0f87 | 681 | _("Argument must be a gdb.Value.")); |
b6313243 TT |
682 | return NULL; |
683 | } | |
684 | ||
a31abe80 | 685 | return find_pretty_printer (val_obj).release (); |
b6313243 | 686 | } |