]>
Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | /* Python pretty-printing |
2 | ||
213516ef | 3 | Copyright (C) 2008-2023 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 | |
fb282576 TT |
30 | extern PyTypeObject printer_object_type; |
31 | ||
621c8364 TT |
32 | /* Return type of print_string_repr. */ |
33 | ||
cec000ad | 34 | enum gdbpy_string_repr_result |
621c8364 TT |
35 | { |
36 | /* The string method returned None. */ | |
37 | string_repr_none, | |
38 | /* The string method had an error. */ | |
39 | string_repr_error, | |
40 | /* Everything ok. */ | |
41 | string_repr_ok | |
42 | }; | |
43 | ||
c4a3dbaf TT |
44 | /* If non-null, points to options that are in effect while |
45 | printing. */ | |
46 | const struct value_print_options *gdbpy_current_print_options; | |
47 | ||
a6bac58e TT |
48 | /* Helper function for find_pretty_printer which iterates over a list, |
49 | calls each function and inspects output. This will return a | |
50 | printer object if one recognizes VALUE. If no printer is found, it | |
51 | will return None. On error, it will set the Python error and | |
52 | return NULL. */ | |
fa33c3cd | 53 | |
a31abe80 | 54 | static gdbpy_ref<> |
a6bac58e TT |
55 | search_pp_list (PyObject *list, PyObject *value) |
56 | { | |
57 | Py_ssize_t pp_list_size, list_index; | |
a6bac58e TT |
58 | |
59 | pp_list_size = PyList_Size (list); | |
60 | for (list_index = 0; list_index < pp_list_size; list_index++) | |
61 | { | |
0700aea5 | 62 | PyObject *function = PyList_GetItem (list, list_index); |
a6bac58e TT |
63 | if (! function) |
64 | return NULL; | |
65 | ||
967cf477 | 66 | /* Skip if disabled. */ |
1bdb0c54 TT |
67 | if (PyObject_HasAttr (function, gdbpy_enabled_cst)) |
68 | { | |
7780f186 | 69 | gdbpy_ref<> attr (PyObject_GetAttr (function, gdbpy_enabled_cst)); |
1bdb0c54 TT |
70 | int cmp; |
71 | ||
0700aea5 | 72 | if (attr == NULL) |
1bdb0c54 | 73 | return NULL; |
0700aea5 | 74 | cmp = PyObject_IsTrue (attr.get ()); |
1bdb0c54 TT |
75 | if (cmp == -1) |
76 | return NULL; | |
77 | ||
78 | if (!cmp) | |
79 | continue; | |
80 | } | |
967cf477 | 81 | |
7780f186 TT |
82 | gdbpy_ref<> printer (PyObject_CallFunctionObjArgs (function, value, |
83 | NULL)); | |
0700aea5 | 84 | if (printer == NULL) |
a6bac58e TT |
85 | return NULL; |
86 | else if (printer != Py_None) | |
a31abe80 | 87 | return printer; |
a6bac58e TT |
88 | } |
89 | ||
a31abe80 | 90 | return gdbpy_ref<>::new_reference (Py_None); |
a6bac58e TT |
91 | } |
92 | ||
fa33c3cd DE |
93 | /* Subroutine of find_pretty_printer to simplify it. |
94 | Look for a pretty-printer to print VALUE in all objfiles. | |
95 | The result is NULL if there's an error and the search should be terminated. | |
96 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
97 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
98 | ||
a6bac58e | 99 | static PyObject * |
fa33c3cd | 100 | find_pretty_printer_from_objfiles (PyObject *value) |
a6bac58e | 101 | { |
2030c079 | 102 | for (objfile *obj : current_program_space->objfiles ()) |
aed57c53 TT |
103 | { |
104 | gdbpy_ref<> objf = objfile_to_objfile_object (obj); | |
105 | if (objf == NULL) | |
106 | { | |
107 | /* Ignore the error and continue. */ | |
108 | PyErr_Clear (); | |
109 | continue; | |
110 | } | |
a6bac58e | 111 | |
aed57c53 TT |
112 | gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL)); |
113 | gdbpy_ref<> function (search_pp_list (pp_list.get (), value)); | |
114 | ||
115 | /* If there is an error in any objfile list, abort the search and exit. */ | |
116 | if (function == NULL) | |
117 | return NULL; | |
118 | ||
119 | if (function != Py_None) | |
120 | return function.release (); | |
121 | } | |
a6bac58e | 122 | |
fa33c3cd DE |
123 | Py_RETURN_NONE; |
124 | } | |
125 | ||
126 | /* Subroutine of find_pretty_printer to simplify it. | |
127 | Look for a pretty-printer to print VALUE in the current program space. | |
128 | The result is NULL if there's an error and the search should be terminated. | |
129 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
130 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
131 | ||
a31abe80 | 132 | static gdbpy_ref<> |
fa33c3cd DE |
133 | find_pretty_printer_from_progspace (PyObject *value) |
134 | { | |
3c7aa307 | 135 | gdbpy_ref<> obj = pspace_to_pspace_object (current_program_space); |
fa33c3cd | 136 | |
3c7aa307 | 137 | if (obj == NULL) |
fa33c3cd | 138 | return NULL; |
3c7aa307 | 139 | gdbpy_ref<> pp_list (pspy_get_printers (obj.get (), NULL)); |
0700aea5 | 140 | return search_pp_list (pp_list.get (), value); |
fa33c3cd DE |
141 | } |
142 | ||
143 | /* Subroutine of find_pretty_printer to simplify it. | |
144 | Look for a pretty-printer to print VALUE in the gdb module. | |
145 | The result is NULL if there's an error and the search should be terminated. | |
146 | The result is Py_None, suitably inc-ref'd, if no pretty-printer was found. | |
147 | Otherwise the result is the pretty-printer function, suitably inc-ref'd. */ | |
148 | ||
a31abe80 | 149 | static gdbpy_ref<> |
fa33c3cd DE |
150 | find_pretty_printer_from_gdb (PyObject *value) |
151 | { | |
23fa7f66 | 152 | /* Fetch the global pretty printer list. */ |
b9516fa1 YPK |
153 | if (gdb_python_module == NULL |
154 | || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers")) | |
a31abe80 | 155 | return gdbpy_ref<>::new_reference (Py_None); |
7780f186 TT |
156 | gdbpy_ref<> pp_list (PyObject_GetAttrString (gdb_python_module, |
157 | "pretty_printers")); | |
0700aea5 | 158 | if (pp_list == NULL || ! PyList_Check (pp_list.get ())) |
a31abe80 | 159 | return gdbpy_ref<>::new_reference (Py_None); |
a6bac58e | 160 | |
0700aea5 | 161 | return search_pp_list (pp_list.get (), value); |
a6bac58e | 162 | } |
be759fcf | 163 | |
fa33c3cd DE |
164 | /* Find the pretty-printing constructor function for VALUE. If no |
165 | pretty-printer exists, return None. If one exists, return a new | |
166 | reference. On error, set the Python error and return NULL. */ | |
167 | ||
a31abe80 | 168 | static gdbpy_ref<> |
fa33c3cd DE |
169 | find_pretty_printer (PyObject *value) |
170 | { | |
23fa7f66 | 171 | /* Look at the pretty-printer list for each objfile |
fa33c3cd | 172 | in the current program-space. */ |
7780f186 | 173 | gdbpy_ref<> function (find_pretty_printer_from_objfiles (value)); |
fa33c3cd | 174 | if (function == NULL || function != Py_None) |
a31abe80 | 175 | return function; |
fa33c3cd | 176 | |
23fa7f66 | 177 | /* Look at the pretty-printer list for the current program-space. */ |
a31abe80 | 178 | function = find_pretty_printer_from_progspace (value); |
fa33c3cd | 179 | if (function == NULL || function != Py_None) |
a31abe80 | 180 | return function; |
fa33c3cd | 181 | |
23fa7f66 | 182 | /* Look at the pretty-printer list in the gdb module. */ |
0700aea5 | 183 | return find_pretty_printer_from_gdb (value); |
fa33c3cd | 184 | } |
be759fcf | 185 | |
fbb8f299 PM |
186 | /* Pretty-print a single value, via the printer object PRINTER. |
187 | If the function returns a string, a PyObject containing the string | |
79f283fe PM |
188 | is returned. If the function returns Py_NONE that means the pretty |
189 | printer returned the Python None as a value. Otherwise, if the | |
190 | function returns a value, *OUT_VALUE is set to the value, and NULL | |
8dc78533 JK |
191 | is returned. On error, *OUT_VALUE is set to NULL, NULL is |
192 | returned, with a python exception set. */ | |
79f283fe | 193 | |
a5c5eda7 | 194 | static gdbpy_ref<> |
a6bac58e TT |
195 | pretty_print_one_value (PyObject *printer, struct value **out_value) |
196 | { | |
1bdfaf42 | 197 | gdbpy_ref<> result; |
a6bac58e | 198 | |
fbb8f299 | 199 | *out_value = NULL; |
a70b8144 | 200 | try |
a6bac58e | 201 | { |
332cf4c9 TT |
202 | if (!PyObject_HasAttr (printer, gdbpy_to_string_cst)) |
203 | result = gdbpy_ref<>::new_reference (Py_None); | |
204 | else | |
a6bac58e | 205 | { |
332cf4c9 TT |
206 | result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, |
207 | NULL)); | |
208 | if (result != NULL) | |
fbb8f299 | 209 | { |
332cf4c9 TT |
210 | if (! gdbpy_is_string (result.get ()) |
211 | && ! gdbpy_is_lazy_string (result.get ()) | |
212 | && result != Py_None) | |
213 | { | |
214 | *out_value = convert_value_from_python (result.get ()); | |
215 | if (PyErr_Occurred ()) | |
216 | *out_value = NULL; | |
217 | result = NULL; | |
218 | } | |
fbb8f299 | 219 | } |
a6bac58e TT |
220 | } |
221 | } | |
230d2906 | 222 | catch (const gdb_exception &except) |
492d29ea PA |
223 | { |
224 | } | |
a6bac58e | 225 | |
a5c5eda7 | 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. */ | |
9b972014 | 233 | gdb::unique_xmalloc_ptr<char> |
a6bac58e TT |
234 | gdbpy_get_display_hint (PyObject *printer) |
235 | { | |
9b972014 | 236 | gdb::unique_xmalloc_ptr<char> result; |
a6bac58e TT |
237 | |
238 | if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst)) | |
239 | return NULL; | |
240 | ||
7780f186 TT |
241 | gdbpy_ref<> hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, |
242 | NULL)); | |
0700aea5 | 243 | if (hint != NULL) |
f04e4012 | 244 | { |
0700aea5 | 245 | if (gdbpy_is_string (hint.get ())) |
8dc78533 | 246 | { |
0700aea5 | 247 | result = python_string_to_host_string (hint.get ()); |
8dc78533 JK |
248 | if (result == NULL) |
249 | gdbpy_print_stack (); | |
250 | } | |
f04e4012 | 251 | } |
a6bac58e TT |
252 | else |
253 | gdbpy_print_stack (); | |
254 | ||
255 | return result; | |
256 | } | |
257 | ||
621c8364 TT |
258 | /* A wrapper for gdbpy_print_stack that ignores MemoryError. */ |
259 | ||
260 | static void | |
261 | print_stack_unless_memory_error (struct ui_file *stream) | |
262 | { | |
263 | if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) | |
264 | { | |
5c329e6a TT |
265 | gdbpy_err_fetch fetched_error; |
266 | gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string (); | |
621c8364 TT |
267 | |
268 | if (msg == NULL || *msg == '\0') | |
7f6aba03 TT |
269 | fprintf_styled (stream, metadata_style.style (), |
270 | _("<error reading variable>")); | |
621c8364 | 271 | else |
7f6aba03 TT |
272 | fprintf_styled (stream, metadata_style.style (), |
273 | _("<error reading variable: %s>"), msg.get ()); | |
621c8364 TT |
274 | } |
275 | else | |
276 | gdbpy_print_stack (); | |
277 | } | |
278 | ||
6dddc817 | 279 | /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and |
621c8364 TT |
280 | formats the result. */ |
281 | ||
cec000ad | 282 | static enum gdbpy_string_repr_result |
a6bac58e TT |
283 | print_string_repr (PyObject *printer, const char *hint, |
284 | struct ui_file *stream, int recurse, | |
285 | const struct value_print_options *options, | |
50810684 UW |
286 | const struct language_defn *language, |
287 | struct gdbarch *gdbarch) | |
a6bac58e | 288 | { |
a6bac58e | 289 | struct value *replacement = NULL; |
cec000ad | 290 | enum gdbpy_string_repr_result result = string_repr_ok; |
a6bac58e | 291 | |
a5c5eda7 | 292 | gdbpy_ref<> py_str = pretty_print_one_value (printer, &replacement); |
2bd5759d | 293 | if (py_str != NULL) |
a6bac58e | 294 | { |
79f283fe | 295 | if (py_str == Py_None) |
621c8364 | 296 | result = string_repr_none; |
2bd5759d | 297 | else if (gdbpy_is_lazy_string (py_str.get ())) |
fbb8f299 | 298 | { |
09ca9e2e | 299 | CORE_ADDR addr; |
79f283fe PM |
300 | long length; |
301 | struct type *type; | |
1eba6383 | 302 | gdb::unique_xmalloc_ptr<char> encoding; |
a81766d8 | 303 | struct value_print_options local_opts = *options; |
79f283fe | 304 | |
2bd5759d | 305 | gdbpy_extract_lazy_string (py_str.get (), &addr, &type, |
09ca9e2e TT |
306 | &length, &encoding); |
307 | ||
dad6b350 | 308 | local_opts.addressprint = false; |
1eba6383 | 309 | val_print_string (type, encoding.get (), addr, (int) length, |
a81766d8 | 310 | stream, &local_opts); |
09ca9e2e TT |
311 | } |
312 | else | |
313 | { | |
7780f186 | 314 | gdbpy_ref<> string |
833d985d | 315 | = python_string_to_target_python_string (py_str.get ()); |
2bd5759d | 316 | if (string != NULL) |
79f283fe | 317 | { |
89f6d837 | 318 | char *output; |
09ca9e2e TT |
319 | long length; |
320 | struct type *type; | |
321 | ||
2bd5759d TT |
322 | output = PyBytes_AS_STRING (string.get ()); |
323 | length = PyBytes_GET_SIZE (string.get ()); | |
09ca9e2e TT |
324 | type = builtin_type (gdbarch)->builtin_char; |
325 | ||
326 | if (hint && !strcmp (hint, "string")) | |
5cc0917c AB |
327 | language->printstr (stream, type, (gdb_byte *) output, |
328 | length, NULL, 0, options); | |
79f283fe | 329 | else |
0426ad51 | 330 | gdb_puts (output, stream); |
be759fcf PM |
331 | } |
332 | else | |
621c8364 TT |
333 | { |
334 | result = string_repr_error; | |
335 | print_stack_unless_memory_error (stream); | |
336 | } | |
79f283fe | 337 | } |
a6bac58e TT |
338 | } |
339 | else if (replacement) | |
269f82e5 PP |
340 | { |
341 | struct value_print_options opts = *options; | |
342 | ||
dad6b350 | 343 | opts.addressprint = false; |
269f82e5 PP |
344 | common_val_print (replacement, stream, recurse, &opts, language); |
345 | } | |
a6bac58e | 346 | else |
621c8364 TT |
347 | { |
348 | result = string_repr_error; | |
349 | print_stack_unless_memory_error (stream); | |
350 | } | |
79f283fe | 351 | |
621c8364 | 352 | return result; |
a6bac58e TT |
353 | } |
354 | ||
6dddc817 | 355 | /* Helper for gdbpy_apply_val_pretty_printer that formats children of the |
79f283fe PM |
356 | printer, if any exist. If is_py_none is true, then nothing has |
357 | been printed by to_string, and format output accordingly. */ | |
a6bac58e TT |
358 | static void |
359 | print_children (PyObject *printer, const char *hint, | |
360 | struct ui_file *stream, int recurse, | |
361 | const struct value_print_options *options, | |
79f283fe PM |
362 | const struct language_defn *language, |
363 | int is_py_none) | |
a6bac58e TT |
364 | { |
365 | int is_map, is_array, done_flag, pretty; | |
366 | unsigned int i; | |
a6bac58e TT |
367 | |
368 | if (! PyObject_HasAttr (printer, gdbpy_children_cst)) | |
369 | return; | |
370 | ||
371 | /* If we are printing a map or an array, we want some special | |
372 | formatting. */ | |
373 | is_map = hint && ! strcmp (hint, "map"); | |
374 | is_array = hint && ! strcmp (hint, "array"); | |
375 | ||
7780f186 TT |
376 | gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, |
377 | NULL)); | |
2bd5759d | 378 | if (children == NULL) |
a6bac58e | 379 | { |
621c8364 | 380 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
381 | return; |
382 | } | |
383 | ||
7780f186 | 384 | gdbpy_ref<> iter (PyObject_GetIter (children.get ())); |
2bd5759d | 385 | if (iter == NULL) |
a6bac58e | 386 | { |
621c8364 | 387 | print_stack_unless_memory_error (stream); |
2bd5759d | 388 | return; |
a6bac58e | 389 | } |
a6bac58e | 390 | |
2a998fc0 | 391 | /* Use the prettyformat_arrays option if we are printing an array, |
a6bac58e | 392 | and the pretty option otherwise. */ |
a81766d8 | 393 | if (is_array) |
2a998fc0 | 394 | pretty = options->prettyformat_arrays; |
a81766d8 TT |
395 | else |
396 | { | |
2a998fc0 | 397 | if (options->prettyformat == Val_prettyformat) |
a81766d8 TT |
398 | pretty = 1; |
399 | else | |
2a998fc0 | 400 | pretty = options->prettyformat_structs; |
a81766d8 | 401 | } |
a6bac58e | 402 | |
a6bac58e TT |
403 | done_flag = 0; |
404 | for (i = 0; i < options->print_max; ++i) | |
405 | { | |
2bd5759d | 406 | PyObject *py_v; |
ddd49eee | 407 | const char *name; |
a6bac58e | 408 | |
7780f186 | 409 | gdbpy_ref<> item (PyIter_Next (iter.get ())); |
2bd5759d | 410 | if (item == NULL) |
a6bac58e TT |
411 | { |
412 | if (PyErr_Occurred ()) | |
621c8364 | 413 | print_stack_unless_memory_error (stream); |
a6bac58e TT |
414 | /* Set a flag so we can know whether we printed all the |
415 | available elements. */ | |
256458bc | 416 | else |
a6bac58e TT |
417 | done_flag = 1; |
418 | break; | |
419 | } | |
420 | ||
2bd5759d | 421 | if (! PyTuple_Check (item.get ()) || PyTuple_Size (item.get ()) != 2) |
69b4374a DE |
422 | { |
423 | PyErr_SetString (PyExc_TypeError, | |
424 | _("Result of children iterator not a tuple" | |
425 | " of two elements.")); | |
426 | gdbpy_print_stack (); | |
69b4374a DE |
427 | continue; |
428 | } | |
2bd5759d | 429 | if (! PyArg_ParseTuple (item.get (), "sO", &name, &py_v)) |
a6bac58e | 430 | { |
69b4374a DE |
431 | /* The user won't necessarily get a stack trace here, so provide |
432 | more context. */ | |
433 | if (gdbpy_print_python_errors_p ()) | |
6cb06a8c TT |
434 | gdb_printf (gdb_stderr, |
435 | _("Bad result from children iterator.\n")); | |
a6bac58e | 436 | gdbpy_print_stack (); |
a6bac58e TT |
437 | continue; |
438 | } | |
a6bac58e | 439 | |
ecf25064 KC |
440 | /* Print initial "=" to separate print_string_repr output and |
441 | children. For other elements, there are three cases: | |
a6bac58e TT |
442 | 1. Maps. Print a "," after each value element. |
443 | 2. Arrays. Always print a ",". | |
444 | 3. Other. Always print a ",". */ | |
445 | if (i == 0) | |
01add95b SM |
446 | { |
447 | if (!is_py_none) | |
0426ad51 | 448 | gdb_puts (" = ", stream); |
01add95b | 449 | } |
a6bac58e | 450 | else if (! is_map || i % 2 == 0) |
0426ad51 | 451 | gdb_puts (pretty ? "," : ", ", stream); |
a6bac58e | 452 | |
ecf25064 KC |
453 | /* Skip printing children if max_depth has been reached. This check |
454 | is performed after print_string_repr and the "=" separator so that | |
455 | these steps are not skipped if the variable is located within the | |
456 | permitted depth. */ | |
457 | if (val_print_check_max_depth (stream, recurse, options, language)) | |
458 | return; | |
459 | else if (i == 0) | |
460 | /* Print initial "{" to bookend children. */ | |
0426ad51 | 461 | gdb_puts ("{", stream); |
ecf25064 | 462 | |
a6bac58e TT |
463 | /* In summary mode, we just want to print "= {...}" if there is |
464 | a value. */ | |
465 | if (options->summary) | |
466 | { | |
467 | /* This increment tricks the post-loop logic to print what | |
468 | we want. */ | |
469 | ++i; | |
470 | /* Likewise. */ | |
471 | pretty = 0; | |
472 | break; | |
473 | } | |
474 | ||
475 | if (! is_map || i % 2 == 0) | |
476 | { | |
477 | if (pretty) | |
478 | { | |
0426ad51 | 479 | gdb_puts ("\n", stream); |
d0b1020b | 480 | print_spaces (2 + 2 * recurse, stream); |
a6bac58e TT |
481 | } |
482 | else | |
1285ce86 | 483 | stream->wrap_here (2 + 2 *recurse); |
a6bac58e TT |
484 | } |
485 | ||
486 | if (is_map && i % 2 == 0) | |
0426ad51 | 487 | gdb_puts ("[", stream); |
a6bac58e TT |
488 | else if (is_array) |
489 | { | |
490 | /* We print the index, not whatever the child method | |
491 | returned as the name. */ | |
492 | if (options->print_array_indexes) | |
6cb06a8c | 493 | gdb_printf (stream, "[%d] = ", i); |
a6bac58e TT |
494 | } |
495 | else if (! is_map) | |
496 | { | |
0426ad51 TT |
497 | gdb_puts (name, stream); |
498 | gdb_puts (" = ", stream); | |
a6bac58e TT |
499 | } |
500 | ||
09ca9e2e | 501 | if (gdbpy_is_lazy_string (py_v)) |
a6bac58e | 502 | { |
09ca9e2e TT |
503 | CORE_ADDR addr; |
504 | struct type *type; | |
505 | long length; | |
1eba6383 | 506 | gdb::unique_xmalloc_ptr<char> encoding; |
a81766d8 | 507 | struct value_print_options local_opts = *options; |
be759fcf | 508 | |
09ca9e2e TT |
509 | gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); |
510 | ||
dad6b350 | 511 | local_opts.addressprint = false; |
1eba6383 | 512 | val_print_string (type, encoding.get (), addr, (int) length, stream, |
a81766d8 | 513 | &local_opts); |
09ca9e2e TT |
514 | } |
515 | else if (gdbpy_is_string (py_v)) | |
516 | { | |
9b972014 | 517 | gdb::unique_xmalloc_ptr<char> output; |
09ca9e2e TT |
518 | |
519 | output = python_string_to_host_string (py_v); | |
520 | if (!output) | |
521 | gdbpy_print_stack (); | |
a6bac58e | 522 | else |
0426ad51 | 523 | gdb_puts (output.get (), stream); |
a6bac58e TT |
524 | } |
525 | else | |
526 | { | |
527 | struct value *value = convert_value_from_python (py_v); | |
528 | ||
529 | if (value == NULL) | |
530 | { | |
531 | gdbpy_print_stack (); | |
532 | error (_("Error while executing Python code.")); | |
533 | } | |
534 | else | |
2e62ab40 AB |
535 | { |
536 | /* When printing the key of a map we allow one additional | |
537 | level of depth. This means the key will print before the | |
538 | value does. */ | |
539 | struct value_print_options opt = *options; | |
540 | if (is_map && i % 2 == 0 | |
541 | && opt.max_depth != -1 | |
542 | && opt.max_depth < INT_MAX) | |
543 | ++opt.max_depth; | |
544 | common_val_print (value, stream, recurse + 1, &opt, language); | |
545 | } | |
a6bac58e TT |
546 | } |
547 | ||
548 | if (is_map && i % 2 == 0) | |
0426ad51 | 549 | gdb_puts ("] = ", stream); |
a6bac58e TT |
550 | } |
551 | ||
552 | if (i) | |
553 | { | |
554 | if (!done_flag) | |
555 | { | |
556 | if (pretty) | |
557 | { | |
0426ad51 | 558 | gdb_puts ("\n", stream); |
d0b1020b | 559 | print_spaces (2 + 2 * recurse, stream); |
a6bac58e | 560 | } |
0426ad51 | 561 | gdb_puts ("...", stream); |
a6bac58e TT |
562 | } |
563 | if (pretty) | |
564 | { | |
0426ad51 | 565 | gdb_puts ("\n", stream); |
d0b1020b | 566 | print_spaces (2 * recurse, stream); |
a6bac58e | 567 | } |
0426ad51 | 568 | gdb_puts ("}", stream); |
a6bac58e | 569 | } |
a6bac58e TT |
570 | } |
571 | ||
6dddc817 DE |
572 | enum ext_lang_rc |
573 | gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, | |
42331a1e | 574 | struct value *value, |
6dddc817 | 575 | struct ui_file *stream, int recurse, |
6dddc817 DE |
576 | const struct value_print_options *options, |
577 | const struct language_defn *language) | |
a6bac58e | 578 | { |
d0c97917 | 579 | struct type *type = value->type (); |
8ee511af | 580 | struct gdbarch *gdbarch = type->arch (); |
cec000ad | 581 | enum gdbpy_string_repr_result print_result; |
c51f6a54 | 582 | |
3ee3b270 | 583 | if (value->lazy ()) |
78259c36 | 584 | value->fetch_lazy (); |
4e07d55f PA |
585 | |
586 | /* No pretty-printer support for unavailable values. */ | |
d00664db | 587 | if (!value->bytes_available (0, type->length ())) |
6dddc817 | 588 | return EXT_LANG_RC_NOP; |
0646da15 TT |
589 | |
590 | if (!gdb_python_initialized) | |
6dddc817 | 591 | return EXT_LANG_RC_NOP; |
4e07d55f | 592 | |
e9f0c363 | 593 | gdbpy_enter enter_py (gdbarch, language); |
a6bac58e | 594 | |
f3d3bbbc | 595 | gdbpy_ref<> val_obj (value_to_value_object (value)); |
e9f0c363 | 596 | if (val_obj == NULL) |
6dddc817 | 597 | { |
e9f0c363 TT |
598 | print_stack_unless_memory_error (stream); |
599 | return EXT_LANG_RC_ERROR; | |
6dddc817 | 600 | } |
256458bc | 601 | |
a6bac58e | 602 | /* Find the constructor. */ |
7780f186 | 603 | gdbpy_ref<> printer (find_pretty_printer (val_obj.get ())); |
c8c735b9 | 604 | if (printer == NULL) |
6dddc817 | 605 | { |
e9f0c363 TT |
606 | print_stack_unless_memory_error (stream); |
607 | return EXT_LANG_RC_ERROR; | |
6dddc817 | 608 | } |
c8c735b9 | 609 | |
c8c735b9 | 610 | if (printer == Py_None) |
e9f0c363 | 611 | return EXT_LANG_RC_NOP; |
a6bac58e | 612 | |
c4a3dbaf TT |
613 | scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options, |
614 | options); | |
615 | ||
a6bac58e | 616 | /* If we are printing a map, we want some special formatting. */ |
e9f0c363 | 617 | gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ())); |
a6bac58e TT |
618 | |
619 | /* Print the section */ | |
e9f0c363 TT |
620 | print_result = print_string_repr (printer.get (), hint.get (), stream, |
621 | recurse, options, language, gdbarch); | |
621c8364 | 622 | if (print_result != string_repr_error) |
e9f0c363 TT |
623 | print_children (printer.get (), hint.get (), stream, recurse, options, |
624 | language, print_result == string_repr_none); | |
a6bac58e | 625 | |
a6bac58e | 626 | if (PyErr_Occurred ()) |
621c8364 | 627 | print_stack_unless_memory_error (stream); |
e9f0c363 | 628 | return EXT_LANG_RC_OK; |
a6bac58e TT |
629 | } |
630 | ||
fbb8f299 | 631 | |
b6313243 TT |
632 | /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the |
633 | print object. It must have a 'to_string' method (but this is | |
634 | checked by varobj, not here) which takes no arguments and | |
fbb8f299 PM |
635 | returns a string. The printer will return a value and in the case |
636 | of a Python string being returned, this function will return a | |
637 | PyObject containing the string. For any other type, *REPLACEMENT is | |
638 | set to the replacement value and this function returns NULL. On | |
639 | error, *REPLACEMENT is set to NULL and this function also returns | |
640 | NULL. */ | |
a5c5eda7 | 641 | gdbpy_ref<> |
b6313243 | 642 | apply_varobj_pretty_printer (PyObject *printer_obj, |
621c8364 | 643 | struct value **replacement, |
c4a3dbaf TT |
644 | struct ui_file *stream, |
645 | const value_print_options *opts) | |
b6313243 | 646 | { |
c4a3dbaf TT |
647 | scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options, |
648 | opts); | |
649 | ||
b6313243 | 650 | *replacement = NULL; |
a5c5eda7 | 651 | gdbpy_ref<> py_str = pretty_print_one_value (printer_obj, replacement); |
fbb8f299 PM |
652 | |
653 | if (*replacement == NULL && py_str == NULL) | |
621c8364 | 654 | print_stack_unless_memory_error (stream); |
b6313243 | 655 | |
fbb8f299 | 656 | return py_str; |
b6313243 TT |
657 | } |
658 | ||
659 | /* Find a pretty-printer object for the varobj module. Returns a new | |
660 | reference to the object if successful; returns NULL if not. VALUE | |
256458bc | 661 | is the value for which a printer tests to determine if it |
b6313243 | 662 | can pretty-print the value. */ |
a31abe80 | 663 | gdbpy_ref<> |
b6313243 TT |
664 | gdbpy_get_varobj_pretty_printer (struct value *value) |
665 | { | |
7780f186 | 666 | gdbpy_ref<> val_obj (value_to_value_object (value)); |
0700aea5 | 667 | if (val_obj == NULL) |
b6313243 TT |
668 | return NULL; |
669 | ||
0700aea5 | 670 | return find_pretty_printer (val_obj.get ()); |
b6313243 TT |
671 | } |
672 | ||
673 | /* A Python function which wraps find_pretty_printer and instantiates | |
674 | the resulting class. This accepts a Value argument and returns a | |
675 | pretty printer instance, or None. This function is useful as an | |
676 | argument to the MI command -var-set-visualizer. */ | |
677 | PyObject * | |
678 | gdbpy_default_visualizer (PyObject *self, PyObject *args) | |
679 | { | |
680 | PyObject *val_obj; | |
b6313243 TT |
681 | struct value *value; |
682 | ||
683 | if (! PyArg_ParseTuple (args, "O", &val_obj)) | |
684 | return NULL; | |
685 | value = value_object_to_value (val_obj); | |
686 | if (! value) | |
687 | { | |
256458bc | 688 | PyErr_SetString (PyExc_TypeError, |
044c0f87 | 689 | _("Argument must be a gdb.Value.")); |
b6313243 TT |
690 | return NULL; |
691 | } | |
692 | ||
a31abe80 | 693 | return find_pretty_printer (val_obj).release (); |
b6313243 | 694 | } |
c4a3dbaf TT |
695 | |
696 | /* Helper function to set a boolean in a dictionary. */ | |
697 | static int | |
698 | set_boolean (PyObject *dict, const char *name, bool val) | |
699 | { | |
700 | gdbpy_ref<> val_obj (PyBool_FromLong (val)); | |
701 | if (val_obj == nullptr) | |
702 | return -1; | |
703 | return PyDict_SetItemString (dict, name, val_obj.get ()); | |
704 | } | |
705 | ||
706 | /* Helper function to set an integer in a dictionary. */ | |
707 | static int | |
708 | set_unsigned (PyObject *dict, const char *name, unsigned int val) | |
709 | { | |
710 | gdbpy_ref<> val_obj = gdb_py_object_from_ulongest (val); | |
711 | if (val_obj == nullptr) | |
712 | return -1; | |
713 | return PyDict_SetItemString (dict, name, val_obj.get ()); | |
714 | } | |
715 | ||
716 | /* Implement gdb.print_options. */ | |
717 | PyObject * | |
718 | gdbpy_print_options (PyObject *unused1, PyObject *unused2) | |
719 | { | |
720 | gdbpy_ref<> result (PyDict_New ()); | |
721 | if (result == nullptr) | |
722 | return nullptr; | |
723 | ||
724 | value_print_options opts; | |
725 | gdbpy_get_print_options (&opts); | |
726 | ||
727 | if (set_boolean (result.get (), "raw", | |
728 | opts.raw) < 0 | |
729 | || set_boolean (result.get (), "pretty_arrays", | |
730 | opts.prettyformat_arrays) < 0 | |
731 | || set_boolean (result.get (), "pretty_structs", | |
732 | opts.prettyformat_structs) < 0 | |
733 | || set_boolean (result.get (), "array_indexes", | |
734 | opts.print_array_indexes) < 0 | |
735 | || set_boolean (result.get (), "symbols", | |
736 | opts.symbol_print) < 0 | |
737 | || set_boolean (result.get (), "unions", | |
738 | opts.unionprint) < 0 | |
739 | || set_boolean (result.get (), "address", | |
740 | opts.addressprint) < 0 | |
741 | || set_boolean (result.get (), "deref_refs", | |
742 | opts.deref_ref) < 0 | |
743 | || set_boolean (result.get (), "actual_objects", | |
744 | opts.objectprint) < 0 | |
745 | || set_boolean (result.get (), "static_members", | |
746 | opts.static_field_print) < 0 | |
747 | || set_boolean (result.get (), "deref_refs", | |
748 | opts.deref_ref) < 0 | |
3028a2db TT |
749 | || set_boolean (result.get (), "nibbles", |
750 | opts.nibblesprint) < 0 | |
72be9d6b TT |
751 | || set_boolean (result.get (), "summary", |
752 | opts.summary) < 0 | |
c4a3dbaf TT |
753 | || set_unsigned (result.get (), "max_elements", |
754 | opts.print_max) < 0 | |
755 | || set_unsigned (result.get (), "max_depth", | |
756 | opts.max_depth) < 0 | |
757 | || set_unsigned (result.get (), "repeat_threshold", | |
758 | opts.repeat_count_threshold) < 0) | |
759 | return nullptr; | |
760 | ||
761 | if (opts.format != 0) | |
762 | { | |
763 | char str[2] = { (char) opts.format, 0 }; | |
764 | gdbpy_ref<> fmtstr = host_string_to_python_string (str); | |
765 | if (fmtstr == nullptr) | |
766 | return nullptr; | |
767 | if (PyDict_SetItemString (result.get (), "format", fmtstr.get ()) < 0) | |
768 | return nullptr; | |
769 | } | |
770 | ||
771 | return result.release (); | |
772 | } | |
773 | ||
774 | /* Helper function that either finds the prevailing print options, or | |
775 | calls get_user_print_options. */ | |
776 | void | |
777 | gdbpy_get_print_options (value_print_options *opts) | |
778 | { | |
779 | if (gdbpy_current_print_options != nullptr) | |
780 | *opts = *gdbpy_current_print_options; | |
781 | else | |
782 | get_user_print_options (opts); | |
783 | } | |
fb282576 TT |
784 | |
785 | /* A ValuePrinter is just a "tag", so it has no state other than that | |
786 | required by Python. */ | |
787 | struct printer_object | |
788 | { | |
789 | PyObject_HEAD | |
790 | }; | |
791 | ||
792 | /* The ValuePrinter type object. */ | |
793 | PyTypeObject printer_object_type = | |
794 | { | |
795 | PyVarObject_HEAD_INIT (NULL, 0) | |
796 | "gdb.ValuePrinter", /*tp_name*/ | |
797 | sizeof (printer_object), /*tp_basicsize*/ | |
798 | 0, /*tp_itemsize*/ | |
799 | 0, /*tp_dealloc*/ | |
800 | 0, /*tp_print*/ | |
801 | 0, /*tp_getattr*/ | |
802 | 0, /*tp_setattr*/ | |
803 | 0, /*tp_compare*/ | |
804 | 0, /*tp_repr*/ | |
805 | 0, /*tp_as_number*/ | |
806 | 0, /*tp_as_sequence*/ | |
807 | 0, /*tp_as_mapping*/ | |
808 | 0, /*tp_hash*/ | |
809 | 0, /*tp_call*/ | |
810 | 0, /*tp_str*/ | |
811 | 0, /*tp_getattro*/ | |
812 | 0, /*tp_setattro*/ | |
813 | 0, /*tp_as_buffer*/ | |
814 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
815 | "GDB value printer object", /* tp_doc */ | |
816 | 0, /* tp_traverse */ | |
817 | 0, /* tp_clear */ | |
818 | 0, /* tp_richcompare */ | |
819 | 0, /* tp_weaklistoffset */ | |
820 | 0, /* tp_iter */ | |
821 | 0, /* tp_iternext */ | |
822 | 0, /* tp_methods */ | |
823 | 0, /* tp_members */ | |
824 | 0, /* tp_getset */ | |
825 | 0, /* tp_base */ | |
826 | 0, /* tp_dict */ | |
827 | 0, /* tp_descr_get */ | |
828 | 0, /* tp_descr_set */ | |
829 | 0, /* tp_dictoffset */ | |
830 | 0, /* tp_init */ | |
831 | 0, /* tp_alloc */ | |
832 | PyType_GenericNew, /* tp_new */ | |
833 | }; | |
834 | ||
835 | /* Set up the ValuePrinter type. */ | |
836 | ||
837 | static int | |
838 | gdbpy_initialize_prettyprint () | |
839 | { | |
840 | if (PyType_Ready (&printer_object_type) < 0) | |
841 | return -1; | |
842 | return gdb_pymodule_addobject (gdb_module, "ValuePrinter", | |
843 | (PyObject *) &printer_object_type); | |
844 | } | |
845 | ||
846 | GDBPY_INITIALIZE_FILE (gdbpy_initialize_prettyprint); |