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