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