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