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