| 1 | /* Python frame filters |
| 2 | |
| 3 | Copyright (C) 2013-2015 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 "objfiles.h" |
| 22 | #include "symtab.h" |
| 23 | #include "language.h" |
| 24 | #include "arch-utils.h" |
| 25 | #include "python.h" |
| 26 | #include "ui-out.h" |
| 27 | #include "valprint.h" |
| 28 | #include "annotate.h" |
| 29 | #include "hashtab.h" |
| 30 | #include "demangle.h" |
| 31 | #include "mi/mi-cmds.h" |
| 32 | #include "python-internal.h" |
| 33 | |
| 34 | enum mi_print_types |
| 35 | { |
| 36 | MI_PRINT_ARGS, |
| 37 | MI_PRINT_LOCALS |
| 38 | }; |
| 39 | |
| 40 | /* Helper function to extract a symbol, a name and a language |
| 41 | definition from a Python object that conforms to the "Symbol Value" |
| 42 | interface. OBJ is the Python object to extract the values from. |
| 43 | NAME is a pass-through argument where the name of the symbol will |
| 44 | be written. NAME is allocated in this function, but the caller is |
| 45 | responsible for clean up. SYM is a pass-through argument where the |
| 46 | symbol will be written. In the case of the API returning a string, |
| 47 | this will be set to NULL. LANGUAGE is also a pass-through argument |
| 48 | denoting the language attributed to the Symbol. In the case of SYM |
| 49 | being NULL, this will be set to the current language. Returns |
| 50 | EXT_LANG_BT_ERROR on error with the appropriate Python exception set, and |
| 51 | EXT_LANG_BT_OK on success. */ |
| 52 | |
| 53 | static enum ext_lang_bt_status |
| 54 | extract_sym (PyObject *obj, char **name, struct symbol **sym, |
| 55 | const struct language_defn **language) |
| 56 | { |
| 57 | PyObject *result = PyObject_CallMethod (obj, "symbol", NULL); |
| 58 | |
| 59 | if (result == NULL) |
| 60 | return EXT_LANG_BT_ERROR; |
| 61 | |
| 62 | /* For 'symbol' callback, the function can return a symbol or a |
| 63 | string. */ |
| 64 | if (gdbpy_is_string (result)) |
| 65 | { |
| 66 | *name = python_string_to_host_string (result); |
| 67 | Py_DECREF (result); |
| 68 | |
| 69 | if (*name == NULL) |
| 70 | return EXT_LANG_BT_ERROR; |
| 71 | /* If the API returns a string (and not a symbol), then there is |
| 72 | no symbol derived language available and the frame filter has |
| 73 | either overridden the symbol with a string, or supplied a |
| 74 | entirely synthetic symbol/value pairing. In that case, use |
| 75 | python_language. */ |
| 76 | *language = python_language; |
| 77 | *sym = NULL; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | /* This type checks 'result' during the conversion so we |
| 82 | just call it unconditionally and check the return. */ |
| 83 | *sym = symbol_object_to_symbol (result); |
| 84 | |
| 85 | Py_DECREF (result); |
| 86 | |
| 87 | if (*sym == NULL) |
| 88 | { |
| 89 | PyErr_SetString (PyExc_RuntimeError, |
| 90 | _("Unexpected value. Expecting a " |
| 91 | "gdb.Symbol or a Python string.")); |
| 92 | return EXT_LANG_BT_ERROR; |
| 93 | } |
| 94 | |
| 95 | /* Duplicate the symbol name, so the caller has consistency |
| 96 | in garbage collection. */ |
| 97 | *name = xstrdup (SYMBOL_PRINT_NAME (*sym)); |
| 98 | |
| 99 | /* If a symbol is specified attempt to determine the language |
| 100 | from the symbol. If mode is not "auto", then the language |
| 101 | has been explicitly set, use that. */ |
| 102 | if (language_mode == language_mode_auto) |
| 103 | *language = language_def (SYMBOL_LANGUAGE (*sym)); |
| 104 | else |
| 105 | *language = current_language; |
| 106 | } |
| 107 | |
| 108 | return EXT_LANG_BT_OK; |
| 109 | } |
| 110 | |
| 111 | /* Helper function to extract a value from an object that conforms to |
| 112 | the "Symbol Value" interface. OBJ is the Python object to extract |
| 113 | the value from. VALUE is a pass-through argument where the value |
| 114 | will be written. If the object does not have the value attribute, |
| 115 | or provides the Python None for a value, VALUE will be set to NULL |
| 116 | and this function will return as successful. Returns EXT_LANG_BT_ERROR |
| 117 | on error with the appropriate Python exception set, and EXT_LANG_BT_OK on |
| 118 | success. */ |
| 119 | |
| 120 | static enum ext_lang_bt_status |
| 121 | extract_value (PyObject *obj, struct value **value) |
| 122 | { |
| 123 | if (PyObject_HasAttrString (obj, "value")) |
| 124 | { |
| 125 | PyObject *vresult = PyObject_CallMethod (obj, "value", NULL); |
| 126 | |
| 127 | if (vresult == NULL) |
| 128 | return EXT_LANG_BT_ERROR; |
| 129 | |
| 130 | /* The Python code has returned 'None' for a value, so we set |
| 131 | value to NULL. This flags that GDB should read the |
| 132 | value. */ |
| 133 | if (vresult == Py_None) |
| 134 | { |
| 135 | Py_DECREF (vresult); |
| 136 | *value = NULL; |
| 137 | return EXT_LANG_BT_OK; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | *value = convert_value_from_python (vresult); |
| 142 | Py_DECREF (vresult); |
| 143 | |
| 144 | if (*value == NULL) |
| 145 | return EXT_LANG_BT_ERROR; |
| 146 | |
| 147 | return EXT_LANG_BT_OK; |
| 148 | } |
| 149 | } |
| 150 | else |
| 151 | *value = NULL; |
| 152 | |
| 153 | return EXT_LANG_BT_OK; |
| 154 | } |
| 155 | |
| 156 | /* MI prints only certain values according to the type of symbol and |
| 157 | also what the user has specified. SYM is the symbol to check, and |
| 158 | MI_PRINT_TYPES is an enum specifying what the user wants emitted |
| 159 | for the MI command in question. */ |
| 160 | static int |
| 161 | mi_should_print (struct symbol *sym, enum mi_print_types type) |
| 162 | { |
| 163 | int print_me = 0; |
| 164 | |
| 165 | switch (SYMBOL_CLASS (sym)) |
| 166 | { |
| 167 | default: |
| 168 | case LOC_UNDEF: /* catches errors */ |
| 169 | case LOC_CONST: /* constant */ |
| 170 | case LOC_TYPEDEF: /* local typedef */ |
| 171 | case LOC_LABEL: /* local label */ |
| 172 | case LOC_BLOCK: /* local function */ |
| 173 | case LOC_CONST_BYTES: /* loc. byte seq. */ |
| 174 | case LOC_UNRESOLVED: /* unresolved static */ |
| 175 | case LOC_OPTIMIZED_OUT: /* optimized out */ |
| 176 | print_me = 0; |
| 177 | break; |
| 178 | |
| 179 | case LOC_ARG: /* argument */ |
| 180 | case LOC_REF_ARG: /* reference arg */ |
| 181 | case LOC_REGPARM_ADDR: /* indirect register arg */ |
| 182 | case LOC_LOCAL: /* stack local */ |
| 183 | case LOC_STATIC: /* static */ |
| 184 | case LOC_REGISTER: /* register */ |
| 185 | case LOC_COMPUTED: /* computed location */ |
| 186 | if (type == MI_PRINT_LOCALS) |
| 187 | print_me = ! SYMBOL_IS_ARGUMENT (sym); |
| 188 | else |
| 189 | print_me = SYMBOL_IS_ARGUMENT (sym); |
| 190 | } |
| 191 | return print_me; |
| 192 | } |
| 193 | |
| 194 | /* Helper function which outputs a type name extracted from VAL to a |
| 195 | "type" field in the output stream OUT. OUT is the ui-out structure |
| 196 | the type name will be output too, and VAL is the value that the |
| 197 | type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with |
| 198 | any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on |
| 199 | success. */ |
| 200 | |
| 201 | static enum ext_lang_bt_status |
| 202 | py_print_type (struct ui_out *out, struct value *val) |
| 203 | { |
| 204 | volatile struct gdb_exception except; |
| 205 | |
| 206 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 207 | { |
| 208 | struct type *type; |
| 209 | struct ui_file *stb; |
| 210 | struct cleanup *cleanup; |
| 211 | |
| 212 | stb = mem_fileopen (); |
| 213 | cleanup = make_cleanup_ui_file_delete (stb); |
| 214 | type = check_typedef (value_type (val)); |
| 215 | type_print (value_type (val), "", stb, -1); |
| 216 | ui_out_field_stream (out, "type", stb); |
| 217 | do_cleanups (cleanup); |
| 218 | } |
| 219 | if (except.reason < 0) |
| 220 | { |
| 221 | gdbpy_convert_exception (except); |
| 222 | return EXT_LANG_BT_ERROR; |
| 223 | } |
| 224 | |
| 225 | return EXT_LANG_BT_OK; |
| 226 | } |
| 227 | |
| 228 | /* Helper function which outputs a value to an output field in a |
| 229 | stream. OUT is the ui-out structure the value will be output to, |
| 230 | VAL is the value that will be printed, OPTS contains the value |
| 231 | printing options, ARGS_TYPE is an enumerator describing the |
| 232 | argument format, and LANGUAGE is the language_defn that the value |
| 233 | will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB |
| 234 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on |
| 235 | success. */ |
| 236 | |
| 237 | static enum ext_lang_bt_status |
| 238 | py_print_value (struct ui_out *out, struct value *val, |
| 239 | const struct value_print_options *opts, |
| 240 | int indent, |
| 241 | enum ext_lang_frame_args args_type, |
| 242 | const struct language_defn *language) |
| 243 | { |
| 244 | int should_print = 0; |
| 245 | volatile struct gdb_exception except; |
| 246 | int local_indent = (4 * indent); |
| 247 | |
| 248 | /* Never set an indent level for common_val_print if MI. */ |
| 249 | if (ui_out_is_mi_like_p (out)) |
| 250 | local_indent = 0; |
| 251 | |
| 252 | /* MI does not print certain values, differentiated by type, |
| 253 | depending on what ARGS_TYPE indicates. Test type against option. |
| 254 | For CLI print all values. */ |
| 255 | if (args_type == MI_PRINT_SIMPLE_VALUES |
| 256 | || args_type == MI_PRINT_ALL_VALUES) |
| 257 | { |
| 258 | struct type *type = NULL; |
| 259 | |
| 260 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 261 | { |
| 262 | type = check_typedef (value_type (val)); |
| 263 | } |
| 264 | if (except.reason < 0) |
| 265 | { |
| 266 | gdbpy_convert_exception (except); |
| 267 | return EXT_LANG_BT_ERROR; |
| 268 | } |
| 269 | |
| 270 | if (args_type == MI_PRINT_ALL_VALUES) |
| 271 | should_print = 1; |
| 272 | else if (args_type == MI_PRINT_SIMPLE_VALUES |
| 273 | && TYPE_CODE (type) != TYPE_CODE_ARRAY |
| 274 | && TYPE_CODE (type) != TYPE_CODE_STRUCT |
| 275 | && TYPE_CODE (type) != TYPE_CODE_UNION) |
| 276 | should_print = 1; |
| 277 | } |
| 278 | else if (args_type != NO_VALUES) |
| 279 | should_print = 1; |
| 280 | |
| 281 | if (should_print) |
| 282 | { |
| 283 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 284 | { |
| 285 | struct ui_file *stb; |
| 286 | struct cleanup *cleanup; |
| 287 | |
| 288 | stb = mem_fileopen (); |
| 289 | cleanup = make_cleanup_ui_file_delete (stb); |
| 290 | common_val_print (val, stb, indent, opts, language); |
| 291 | ui_out_field_stream (out, "value", stb); |
| 292 | do_cleanups (cleanup); |
| 293 | } |
| 294 | if (except.reason < 0) |
| 295 | { |
| 296 | gdbpy_convert_exception (except); |
| 297 | return EXT_LANG_BT_ERROR; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return EXT_LANG_BT_OK; |
| 302 | } |
| 303 | |
| 304 | /* Helper function to call a Python method and extract an iterator |
| 305 | from the result. If the function returns anything but an iterator |
| 306 | the exception is preserved and NULL is returned. FILTER is the |
| 307 | Python object to call, and FUNC is the name of the method. Returns |
| 308 | a PyObject, or NULL on error with the appropriate exception set. |
| 309 | This function can return an iterator, or NULL. */ |
| 310 | |
| 311 | static PyObject * |
| 312 | get_py_iter_from_func (PyObject *filter, char *func) |
| 313 | { |
| 314 | if (PyObject_HasAttrString (filter, func)) |
| 315 | { |
| 316 | PyObject *result = PyObject_CallMethod (filter, func, NULL); |
| 317 | |
| 318 | if (result != NULL) |
| 319 | { |
| 320 | if (result == Py_None) |
| 321 | { |
| 322 | return result; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | PyObject *iterator = PyObject_GetIter (result); |
| 327 | |
| 328 | Py_DECREF (result); |
| 329 | return iterator; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | Py_RETURN_NONE; |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | /* Helper function to output a single frame argument and value to an |
| 340 | output stream. This function will account for entry values if the |
| 341 | FV parameter is populated, the frame argument has entry values |
| 342 | associated with them, and the appropriate "set entry-value" |
| 343 | options are set. Will output in CLI or MI like format depending |
| 344 | on the type of output stream detected. OUT is the output stream, |
| 345 | SYM_NAME is the name of the symbol. If SYM_NAME is populated then |
| 346 | it must have an accompanying value in the parameter FV. FA is a |
| 347 | frame argument structure. If FA is populated, both SYM_NAME and |
| 348 | FV are ignored. OPTS contains the value printing options, |
| 349 | ARGS_TYPE is an enumerator describing the argument format, |
| 350 | PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1" |
| 351 | in MI output in commands where both arguments and locals are |
| 352 | printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions |
| 353 | converted to a Python exception, or EXT_LANG_BT_OK on success. */ |
| 354 | |
| 355 | static enum ext_lang_bt_status |
| 356 | py_print_single_arg (struct ui_out *out, |
| 357 | const char *sym_name, |
| 358 | struct frame_arg *fa, |
| 359 | struct value *fv, |
| 360 | const struct value_print_options *opts, |
| 361 | enum ext_lang_frame_args args_type, |
| 362 | int print_args_field, |
| 363 | const struct language_defn *language) |
| 364 | { |
| 365 | struct value *val; |
| 366 | volatile struct gdb_exception except; |
| 367 | enum ext_lang_bt_status retval = EXT_LANG_BT_OK; |
| 368 | |
| 369 | if (fa != NULL) |
| 370 | { |
| 371 | if (fa->val == NULL && fa->error == NULL) |
| 372 | return EXT_LANG_BT_OK; |
| 373 | language = language_def (SYMBOL_LANGUAGE (fa->sym)); |
| 374 | val = fa->val; |
| 375 | } |
| 376 | else |
| 377 | val = fv; |
| 378 | |
| 379 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 380 | { |
| 381 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); |
| 382 | |
| 383 | /* MI has varying rules for tuples, but generally if there is only |
| 384 | one element in each item in the list, do not start a tuple. The |
| 385 | exception is -stack-list-variables which emits an ARGS="1" field |
| 386 | if the value is a frame argument. This is denoted in this |
| 387 | function with PRINT_ARGS_FIELD which is flag from the caller to |
| 388 | emit the ARGS field. */ |
| 389 | if (ui_out_is_mi_like_p (out)) |
| 390 | { |
| 391 | if (print_args_field || args_type != NO_VALUES) |
| 392 | make_cleanup_ui_out_tuple_begin_end (out, NULL); |
| 393 | } |
| 394 | |
| 395 | annotate_arg_begin (); |
| 396 | |
| 397 | /* If frame argument is populated, check for entry-values and the |
| 398 | entry value options. */ |
| 399 | if (fa != NULL) |
| 400 | { |
| 401 | struct ui_file *stb; |
| 402 | |
| 403 | stb = mem_fileopen (); |
| 404 | make_cleanup_ui_file_delete (stb); |
| 405 | fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym), |
| 406 | SYMBOL_LANGUAGE (fa->sym), |
| 407 | DMGL_PARAMS | DMGL_ANSI); |
| 408 | if (fa->entry_kind == print_entry_values_compact) |
| 409 | { |
| 410 | fputs_filtered ("=", stb); |
| 411 | |
| 412 | fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym), |
| 413 | SYMBOL_LANGUAGE (fa->sym), |
| 414 | DMGL_PARAMS | DMGL_ANSI); |
| 415 | } |
| 416 | if (fa->entry_kind == print_entry_values_only |
| 417 | || fa->entry_kind == print_entry_values_compact) |
| 418 | { |
| 419 | fputs_filtered ("@entry", stb); |
| 420 | } |
| 421 | ui_out_field_stream (out, "name", stb); |
| 422 | } |
| 423 | else |
| 424 | /* Otherwise, just output the name. */ |
| 425 | ui_out_field_string (out, "name", sym_name); |
| 426 | |
| 427 | annotate_arg_name_end (); |
| 428 | |
| 429 | if (! ui_out_is_mi_like_p (out)) |
| 430 | ui_out_text (out, "="); |
| 431 | |
| 432 | if (print_args_field) |
| 433 | ui_out_field_int (out, "arg", 1); |
| 434 | |
| 435 | /* For MI print the type, but only for simple values. This seems |
| 436 | weird, but this is how MI choose to format the various output |
| 437 | types. */ |
| 438 | if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL) |
| 439 | { |
| 440 | if (py_print_type (out, val) == EXT_LANG_BT_ERROR) |
| 441 | { |
| 442 | retval = EXT_LANG_BT_ERROR; |
| 443 | do_cleanups (cleanups); |
| 444 | continue; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (val != NULL) |
| 449 | annotate_arg_value (value_type (val)); |
| 450 | |
| 451 | /* If the output is to the CLI, and the user option "set print |
| 452 | frame-arguments" is set to none, just output "...". */ |
| 453 | if (! ui_out_is_mi_like_p (out) && args_type == NO_VALUES) |
| 454 | ui_out_field_string (out, "value", "..."); |
| 455 | else |
| 456 | { |
| 457 | /* Otherwise, print the value for both MI and the CLI, except |
| 458 | for the case of MI_PRINT_NO_VALUES. */ |
| 459 | if (args_type != NO_VALUES) |
| 460 | { |
| 461 | if (val == NULL) |
| 462 | { |
| 463 | gdb_assert (fa != NULL && fa->error != NULL); |
| 464 | ui_out_field_fmt (out, "value", |
| 465 | _("<error reading variable: %s>"), |
| 466 | fa->error); |
| 467 | } |
| 468 | else if (py_print_value (out, val, opts, 0, args_type, language) |
| 469 | == EXT_LANG_BT_ERROR) |
| 470 | retval = EXT_LANG_BT_ERROR; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | do_cleanups (cleanups); |
| 475 | } |
| 476 | if (except.reason < 0) |
| 477 | gdbpy_convert_exception (except); |
| 478 | |
| 479 | return retval; |
| 480 | } |
| 481 | |
| 482 | /* Helper function to loop over frame arguments provided by the |
| 483 | "frame_arguments" Python API. Elements in the iterator must |
| 484 | conform to the "Symbol Value" interface. ITER is the Python |
| 485 | iterable object, OUT is the output stream, ARGS_TYPE is an |
| 486 | enumerator describing the argument format, PRINT_ARGS_FIELD is a |
| 487 | flag which indicates if we output "ARGS=1" in MI output in commands |
| 488 | where both arguments and locals are printed, and FRAME is the |
| 489 | backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB |
| 490 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on |
| 491 | success. */ |
| 492 | |
| 493 | static enum ext_lang_bt_status |
| 494 | enumerate_args (PyObject *iter, |
| 495 | struct ui_out *out, |
| 496 | enum ext_lang_frame_args args_type, |
| 497 | int print_args_field, |
| 498 | struct frame_info *frame) |
| 499 | { |
| 500 | PyObject *item; |
| 501 | struct value_print_options opts; |
| 502 | volatile struct gdb_exception except; |
| 503 | |
| 504 | get_user_print_options (&opts); |
| 505 | |
| 506 | if (args_type == CLI_SCALAR_VALUES) |
| 507 | { |
| 508 | /* True in "summary" mode, false otherwise. */ |
| 509 | opts.summary = 1; |
| 510 | } |
| 511 | |
| 512 | opts.deref_ref = 1; |
| 513 | |
| 514 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 515 | { |
| 516 | annotate_frame_args (); |
| 517 | } |
| 518 | if (except.reason < 0) |
| 519 | { |
| 520 | gdbpy_convert_exception (except); |
| 521 | goto error; |
| 522 | } |
| 523 | |
| 524 | /* Collect the first argument outside of the loop, so output of |
| 525 | commas in the argument output is correct. At the end of the |
| 526 | loop block collect another item from the iterator, and, if it is |
| 527 | not null emit a comma. */ |
| 528 | item = PyIter_Next (iter); |
| 529 | if (item == NULL && PyErr_Occurred ()) |
| 530 | goto error; |
| 531 | |
| 532 | while (item) |
| 533 | { |
| 534 | const struct language_defn *language; |
| 535 | char *sym_name; |
| 536 | struct symbol *sym; |
| 537 | struct value *val; |
| 538 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 539 | |
| 540 | success = extract_sym (item, &sym_name, &sym, &language); |
| 541 | if (success == EXT_LANG_BT_ERROR) |
| 542 | { |
| 543 | Py_DECREF (item); |
| 544 | goto error; |
| 545 | } |
| 546 | |
| 547 | success = extract_value (item, &val); |
| 548 | if (success == EXT_LANG_BT_ERROR) |
| 549 | { |
| 550 | xfree (sym_name); |
| 551 | Py_DECREF (item); |
| 552 | goto error; |
| 553 | } |
| 554 | |
| 555 | Py_DECREF (item); |
| 556 | item = NULL; |
| 557 | |
| 558 | if (sym && ui_out_is_mi_like_p (out) |
| 559 | && ! mi_should_print (sym, MI_PRINT_ARGS)) |
| 560 | { |
| 561 | xfree (sym_name); |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | /* If the object did not provide a value, read it using |
| 566 | read_frame_args and account for entry values, if any. */ |
| 567 | if (val == NULL) |
| 568 | { |
| 569 | struct frame_arg arg, entryarg; |
| 570 | |
| 571 | /* If there is no value, and also no symbol, set error and |
| 572 | exit. */ |
| 573 | if (sym == NULL) |
| 574 | { |
| 575 | PyErr_SetString (PyExc_RuntimeError, |
| 576 | _("No symbol or value provided.")); |
| 577 | xfree (sym_name); |
| 578 | goto error; |
| 579 | } |
| 580 | |
| 581 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 582 | { |
| 583 | read_frame_arg (sym, frame, &arg, &entryarg); |
| 584 | } |
| 585 | if (except.reason < 0) |
| 586 | { |
| 587 | xfree (sym_name); |
| 588 | gdbpy_convert_exception (except); |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | /* The object has not provided a value, so this is a frame |
| 593 | argument to be read by GDB. In this case we have to |
| 594 | account for entry-values. */ |
| 595 | |
| 596 | if (arg.entry_kind != print_entry_values_only) |
| 597 | { |
| 598 | if (py_print_single_arg (out, NULL, &arg, |
| 599 | NULL, &opts, |
| 600 | args_type, |
| 601 | print_args_field, |
| 602 | NULL) == EXT_LANG_BT_ERROR) |
| 603 | { |
| 604 | xfree (arg.error); |
| 605 | xfree (entryarg.error); |
| 606 | xfree (sym_name); |
| 607 | goto error; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (entryarg.entry_kind != print_entry_values_no) |
| 612 | { |
| 613 | if (arg.entry_kind != print_entry_values_only) |
| 614 | { |
| 615 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 616 | { |
| 617 | ui_out_text (out, ", "); |
| 618 | ui_out_wrap_hint (out, " "); |
| 619 | } |
| 620 | if (except.reason < 0) |
| 621 | { |
| 622 | xfree (arg.error); |
| 623 | xfree (entryarg.error); |
| 624 | xfree (sym_name); |
| 625 | gdbpy_convert_exception (except); |
| 626 | goto error; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts, |
| 631 | args_type, print_args_field, NULL) |
| 632 | == EXT_LANG_BT_ERROR) |
| 633 | { |
| 634 | xfree (arg.error); |
| 635 | xfree (entryarg.error); |
| 636 | xfree (sym_name); |
| 637 | goto error; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | xfree (arg.error); |
| 642 | xfree (entryarg.error); |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | /* If the object has provided a value, we just print that. */ |
| 647 | if (val != NULL) |
| 648 | { |
| 649 | if (py_print_single_arg (out, sym_name, NULL, val, &opts, |
| 650 | args_type, print_args_field, |
| 651 | language) == EXT_LANG_BT_ERROR) |
| 652 | { |
| 653 | xfree (sym_name); |
| 654 | goto error; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | xfree (sym_name); |
| 660 | |
| 661 | /* Collect the next item from the iterator. If |
| 662 | this is the last item, do not print the |
| 663 | comma. */ |
| 664 | item = PyIter_Next (iter); |
| 665 | if (item != NULL) |
| 666 | { |
| 667 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 668 | { |
| 669 | ui_out_text (out, ", "); |
| 670 | } |
| 671 | if (except.reason < 0) |
| 672 | { |
| 673 | Py_DECREF (item); |
| 674 | gdbpy_convert_exception (except); |
| 675 | goto error; |
| 676 | } |
| 677 | } |
| 678 | else if (PyErr_Occurred ()) |
| 679 | goto error; |
| 680 | |
| 681 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 682 | { |
| 683 | annotate_arg_end (); |
| 684 | } |
| 685 | if (except.reason < 0) |
| 686 | { |
| 687 | Py_DECREF (item); |
| 688 | gdbpy_convert_exception (except); |
| 689 | goto error; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return EXT_LANG_BT_OK; |
| 694 | |
| 695 | error: |
| 696 | return EXT_LANG_BT_ERROR; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | /* Helper function to loop over variables provided by the |
| 701 | "frame_locals" Python API. Elements in the iterable must conform |
| 702 | to the "Symbol Value" interface. ITER is the Python iterable |
| 703 | object, OUT is the output stream, INDENT is whether we should |
| 704 | indent the output (for CLI), ARGS_TYPE is an enumerator describing |
| 705 | the argument format, PRINT_ARGS_FIELD is flag which indicates |
| 706 | whether to output the ARGS field in the case of |
| 707 | -stack-list-variables and FRAME is the backing frame. Returns |
| 708 | EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python |
| 709 | exception, or EXT_LANG_BT_OK on success. */ |
| 710 | |
| 711 | static enum ext_lang_bt_status |
| 712 | enumerate_locals (PyObject *iter, |
| 713 | struct ui_out *out, |
| 714 | int indent, |
| 715 | enum ext_lang_frame_args args_type, |
| 716 | int print_args_field, |
| 717 | struct frame_info *frame) |
| 718 | { |
| 719 | PyObject *item; |
| 720 | struct value_print_options opts; |
| 721 | |
| 722 | get_user_print_options (&opts); |
| 723 | opts.deref_ref = 1; |
| 724 | |
| 725 | while ((item = PyIter_Next (iter))) |
| 726 | { |
| 727 | const struct language_defn *language; |
| 728 | char *sym_name; |
| 729 | struct value *val; |
| 730 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 731 | struct symbol *sym; |
| 732 | volatile struct gdb_exception except; |
| 733 | int local_indent = 8 + (8 * indent); |
| 734 | struct cleanup *locals_cleanups; |
| 735 | |
| 736 | locals_cleanups = make_cleanup_py_decref (item); |
| 737 | |
| 738 | success = extract_sym (item, &sym_name, &sym, &language); |
| 739 | if (success == EXT_LANG_BT_ERROR) |
| 740 | { |
| 741 | do_cleanups (locals_cleanups); |
| 742 | goto error; |
| 743 | } |
| 744 | |
| 745 | make_cleanup (xfree, sym_name); |
| 746 | |
| 747 | success = extract_value (item, &val); |
| 748 | if (success == EXT_LANG_BT_ERROR) |
| 749 | { |
| 750 | do_cleanups (locals_cleanups); |
| 751 | goto error; |
| 752 | } |
| 753 | |
| 754 | if (sym != NULL && ui_out_is_mi_like_p (out) |
| 755 | && ! mi_should_print (sym, MI_PRINT_LOCALS)) |
| 756 | { |
| 757 | do_cleanups (locals_cleanups); |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | /* If the object did not provide a value, read it. */ |
| 762 | if (val == NULL) |
| 763 | { |
| 764 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 765 | { |
| 766 | val = read_var_value (sym, frame); |
| 767 | } |
| 768 | if (except.reason < 0) |
| 769 | { |
| 770 | gdbpy_convert_exception (except); |
| 771 | do_cleanups (locals_cleanups); |
| 772 | goto error; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /* With PRINT_NO_VALUES, MI does not emit a tuple normally as |
| 777 | each output contains only one field. The exception is |
| 778 | -stack-list-variables, which always provides a tuple. */ |
| 779 | if (ui_out_is_mi_like_p (out)) |
| 780 | { |
| 781 | if (print_args_field || args_type != NO_VALUES) |
| 782 | make_cleanup_ui_out_tuple_begin_end (out, NULL); |
| 783 | } |
| 784 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 785 | { |
| 786 | if (! ui_out_is_mi_like_p (out)) |
| 787 | { |
| 788 | /* If the output is not MI we indent locals. */ |
| 789 | ui_out_spaces (out, local_indent); |
| 790 | } |
| 791 | |
| 792 | ui_out_field_string (out, "name", sym_name); |
| 793 | |
| 794 | if (! ui_out_is_mi_like_p (out)) |
| 795 | ui_out_text (out, " = "); |
| 796 | } |
| 797 | if (except.reason < 0) |
| 798 | { |
| 799 | gdbpy_convert_exception (except); |
| 800 | do_cleanups (locals_cleanups); |
| 801 | goto error; |
| 802 | } |
| 803 | |
| 804 | if (args_type == MI_PRINT_SIMPLE_VALUES) |
| 805 | { |
| 806 | if (py_print_type (out, val) == EXT_LANG_BT_ERROR) |
| 807 | { |
| 808 | do_cleanups (locals_cleanups); |
| 809 | goto error; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* CLI always prints values for locals. MI uses the |
| 814 | simple/no/all system. */ |
| 815 | if (! ui_out_is_mi_like_p (out)) |
| 816 | { |
| 817 | int val_indent = (indent + 1) * 4; |
| 818 | |
| 819 | if (py_print_value (out, val, &opts, val_indent, args_type, |
| 820 | language) == EXT_LANG_BT_ERROR) |
| 821 | { |
| 822 | do_cleanups (locals_cleanups); |
| 823 | goto error; |
| 824 | } |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | if (args_type != NO_VALUES) |
| 829 | { |
| 830 | if (py_print_value (out, val, &opts, 0, args_type, |
| 831 | language) == EXT_LANG_BT_ERROR) |
| 832 | { |
| 833 | do_cleanups (locals_cleanups); |
| 834 | goto error; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | do_cleanups (locals_cleanups); |
| 840 | |
| 841 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 842 | { |
| 843 | ui_out_text (out, "\n"); |
| 844 | } |
| 845 | if (except.reason < 0) |
| 846 | { |
| 847 | gdbpy_convert_exception (except); |
| 848 | goto error; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (item == NULL && PyErr_Occurred ()) |
| 853 | goto error; |
| 854 | |
| 855 | return EXT_LANG_BT_OK; |
| 856 | |
| 857 | error: |
| 858 | return EXT_LANG_BT_ERROR; |
| 859 | } |
| 860 | |
| 861 | /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on |
| 862 | error, or EXT_LANG_BT_OK on success. */ |
| 863 | |
| 864 | static enum ext_lang_bt_status |
| 865 | py_mi_print_variables (PyObject *filter, struct ui_out *out, |
| 866 | struct value_print_options *opts, |
| 867 | enum ext_lang_frame_args args_type, |
| 868 | struct frame_info *frame) |
| 869 | { |
| 870 | struct cleanup *old_chain; |
| 871 | PyObject *args_iter; |
| 872 | PyObject *locals_iter; |
| 873 | |
| 874 | args_iter = get_py_iter_from_func (filter, "frame_args"); |
| 875 | old_chain = make_cleanup_py_xdecref (args_iter); |
| 876 | if (args_iter == NULL) |
| 877 | goto error; |
| 878 | |
| 879 | locals_iter = get_py_iter_from_func (filter, "frame_locals"); |
| 880 | if (locals_iter == NULL) |
| 881 | goto error; |
| 882 | |
| 883 | make_cleanup_py_decref (locals_iter); |
| 884 | make_cleanup_ui_out_list_begin_end (out, "variables"); |
| 885 | |
| 886 | if (args_iter != Py_None) |
| 887 | if (enumerate_args (args_iter, out, args_type, 1, frame) |
| 888 | == EXT_LANG_BT_ERROR) |
| 889 | goto error; |
| 890 | |
| 891 | if (locals_iter != Py_None) |
| 892 | if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame) |
| 893 | == EXT_LANG_BT_ERROR) |
| 894 | goto error; |
| 895 | |
| 896 | do_cleanups (old_chain); |
| 897 | return EXT_LANG_BT_OK; |
| 898 | |
| 899 | error: |
| 900 | do_cleanups (old_chain); |
| 901 | return EXT_LANG_BT_ERROR; |
| 902 | } |
| 903 | |
| 904 | /* Helper function for printing locals. This function largely just |
| 905 | creates the wrapping tuple, and calls enumerate_locals. Returns |
| 906 | EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */ |
| 907 | |
| 908 | static enum ext_lang_bt_status |
| 909 | py_print_locals (PyObject *filter, |
| 910 | struct ui_out *out, |
| 911 | enum ext_lang_frame_args args_type, |
| 912 | int indent, |
| 913 | struct frame_info *frame) |
| 914 | { |
| 915 | PyObject *locals_iter = get_py_iter_from_func (filter, |
| 916 | "frame_locals"); |
| 917 | struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter); |
| 918 | |
| 919 | if (locals_iter == NULL) |
| 920 | goto locals_error; |
| 921 | |
| 922 | make_cleanup_ui_out_list_begin_end (out, "locals"); |
| 923 | |
| 924 | if (locals_iter != Py_None) |
| 925 | if (enumerate_locals (locals_iter, out, indent, args_type, |
| 926 | 0, frame) == EXT_LANG_BT_ERROR) |
| 927 | goto locals_error; |
| 928 | |
| 929 | do_cleanups (old_chain); |
| 930 | return EXT_LANG_BT_OK; |
| 931 | |
| 932 | locals_error: |
| 933 | do_cleanups (old_chain); |
| 934 | return EXT_LANG_BT_ERROR; |
| 935 | } |
| 936 | |
| 937 | /* Helper function for printing frame arguments. This function |
| 938 | largely just creates the wrapping tuple, and calls enumerate_args. |
| 939 | Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to |
| 940 | a Python exception, or EXT_LANG_BT_OK on success. */ |
| 941 | |
| 942 | static enum ext_lang_bt_status |
| 943 | py_print_args (PyObject *filter, |
| 944 | struct ui_out *out, |
| 945 | enum ext_lang_frame_args args_type, |
| 946 | struct frame_info *frame) |
| 947 | { |
| 948 | PyObject *args_iter = get_py_iter_from_func (filter, "frame_args"); |
| 949 | struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter); |
| 950 | volatile struct gdb_exception except; |
| 951 | |
| 952 | if (args_iter == NULL) |
| 953 | goto args_error; |
| 954 | |
| 955 | make_cleanup_ui_out_list_begin_end (out, "args"); |
| 956 | |
| 957 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 958 | { |
| 959 | annotate_frame_args (); |
| 960 | if (! ui_out_is_mi_like_p (out)) |
| 961 | ui_out_text (out, " ("); |
| 962 | } |
| 963 | if (except.reason < 0) |
| 964 | { |
| 965 | gdbpy_convert_exception (except); |
| 966 | goto args_error; |
| 967 | } |
| 968 | |
| 969 | if (args_iter != Py_None) |
| 970 | if (enumerate_args (args_iter, out, args_type, 0, frame) |
| 971 | == EXT_LANG_BT_ERROR) |
| 972 | goto args_error; |
| 973 | |
| 974 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 975 | { |
| 976 | if (! ui_out_is_mi_like_p (out)) |
| 977 | ui_out_text (out, ")"); |
| 978 | } |
| 979 | if (except.reason < 0) |
| 980 | { |
| 981 | gdbpy_convert_exception (except); |
| 982 | goto args_error; |
| 983 | } |
| 984 | |
| 985 | do_cleanups (old_chain); |
| 986 | return EXT_LANG_BT_OK; |
| 987 | |
| 988 | args_error: |
| 989 | do_cleanups (old_chain); |
| 990 | return EXT_LANG_BT_ERROR; |
| 991 | } |
| 992 | |
| 993 | /* Print a single frame to the designated output stream, detecting |
| 994 | whether the output is MI or console, and formatting the output |
| 995 | according to the conventions of that protocol. FILTER is the |
| 996 | frame-filter associated with this frame. FLAGS is an integer |
| 997 | describing the various print options. The FLAGS variables is |
| 998 | described in "apply_frame_filter" function. ARGS_TYPE is an |
| 999 | enumerator describing the argument format. OUT is the output |
| 1000 | stream to print, INDENT is the level of indention for this frame |
| 1001 | (in the case of elided frames), and LEVELS_PRINTED is a hash-table |
| 1002 | containing all the frames level that have already been printed. |
| 1003 | If a frame level has been printed, do not print it again (in the |
| 1004 | case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any |
| 1005 | GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED |
| 1006 | on success. */ |
| 1007 | |
| 1008 | static enum ext_lang_bt_status |
| 1009 | py_print_frame (PyObject *filter, int flags, |
| 1010 | enum ext_lang_frame_args args_type, |
| 1011 | struct ui_out *out, int indent, htab_t levels_printed) |
| 1012 | { |
| 1013 | int has_addr = 0; |
| 1014 | CORE_ADDR address = 0; |
| 1015 | struct gdbarch *gdbarch = NULL; |
| 1016 | struct frame_info *frame = NULL; |
| 1017 | struct cleanup *cleanup_stack = make_cleanup (null_cleanup, NULL); |
| 1018 | struct value_print_options opts; |
| 1019 | PyObject *py_inf_frame; |
| 1020 | int print_level, print_frame_info, print_args, print_locals; |
| 1021 | volatile struct gdb_exception except; |
| 1022 | |
| 1023 | /* Extract print settings from FLAGS. */ |
| 1024 | print_level = (flags & PRINT_LEVEL) ? 1 : 0; |
| 1025 | print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0; |
| 1026 | print_args = (flags & PRINT_ARGS) ? 1 : 0; |
| 1027 | print_locals = (flags & PRINT_LOCALS) ? 1 : 0; |
| 1028 | |
| 1029 | get_user_print_options (&opts); |
| 1030 | |
| 1031 | /* Get the underlying frame. This is needed to determine GDB |
| 1032 | architecture, and also, in the cases of frame variables/arguments to |
| 1033 | read them if they returned filter object requires us to do so. */ |
| 1034 | py_inf_frame = PyObject_CallMethod (filter, "inferior_frame", NULL); |
| 1035 | if (py_inf_frame == NULL) |
| 1036 | goto error; |
| 1037 | |
| 1038 | frame = frame_object_to_frame_info (py_inf_frame);; |
| 1039 | |
| 1040 | Py_DECREF (py_inf_frame); |
| 1041 | |
| 1042 | if (frame == NULL) |
| 1043 | goto error; |
| 1044 | |
| 1045 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1046 | { |
| 1047 | gdbarch = get_frame_arch (frame); |
| 1048 | } |
| 1049 | if (except.reason < 0) |
| 1050 | { |
| 1051 | gdbpy_convert_exception (except); |
| 1052 | goto error; |
| 1053 | } |
| 1054 | |
| 1055 | /* stack-list-variables. */ |
| 1056 | if (print_locals && print_args && ! print_frame_info) |
| 1057 | { |
| 1058 | if (py_mi_print_variables (filter, out, &opts, |
| 1059 | args_type, frame) == EXT_LANG_BT_ERROR) |
| 1060 | goto error; |
| 1061 | do_cleanups (cleanup_stack); |
| 1062 | return EXT_LANG_BT_COMPLETED; |
| 1063 | } |
| 1064 | |
| 1065 | /* -stack-list-locals does not require a |
| 1066 | wrapping frame attribute. */ |
| 1067 | if (print_frame_info || (print_args && ! print_locals)) |
| 1068 | make_cleanup_ui_out_tuple_begin_end (out, "frame"); |
| 1069 | |
| 1070 | if (print_frame_info) |
| 1071 | { |
| 1072 | /* Elided frames are also printed with this function (recursively) |
| 1073 | and are printed with indention. */ |
| 1074 | if (indent > 0) |
| 1075 | { |
| 1076 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1077 | { |
| 1078 | ui_out_spaces (out, indent*4); |
| 1079 | } |
| 1080 | if (except.reason < 0) |
| 1081 | { |
| 1082 | gdbpy_convert_exception (except); |
| 1083 | goto error; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* The address is required for frame annotations, and also for |
| 1088 | address printing. */ |
| 1089 | if (PyObject_HasAttrString (filter, "address")) |
| 1090 | { |
| 1091 | PyObject *paddr = PyObject_CallMethod (filter, "address", NULL); |
| 1092 | |
| 1093 | if (paddr == NULL) |
| 1094 | goto error; |
| 1095 | |
| 1096 | if (paddr != Py_None) |
| 1097 | { |
| 1098 | address = PyLong_AsLong (paddr); |
| 1099 | has_addr = 1; |
| 1100 | } |
| 1101 | Py_DECREF (paddr); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* Print frame level. MI does not require the level if |
| 1106 | locals/variables only are being printed. */ |
| 1107 | if ((print_frame_info || print_args) && print_level) |
| 1108 | { |
| 1109 | struct frame_info **slot; |
| 1110 | int level; |
| 1111 | volatile struct gdb_exception except; |
| 1112 | |
| 1113 | slot = (struct frame_info **) htab_find_slot (levels_printed, |
| 1114 | frame, INSERT); |
| 1115 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1116 | { |
| 1117 | level = frame_relative_level (frame); |
| 1118 | |
| 1119 | /* Check if this frame has already been printed (there are cases |
| 1120 | where elided synthetic dummy-frames have to 'borrow' the frame |
| 1121 | architecture from the eliding frame. If that is the case, do |
| 1122 | not print 'level', but print spaces. */ |
| 1123 | if (*slot == frame) |
| 1124 | ui_out_field_skip (out, "level"); |
| 1125 | else |
| 1126 | { |
| 1127 | *slot = frame; |
| 1128 | annotate_frame_begin (print_level ? level : 0, |
| 1129 | gdbarch, address); |
| 1130 | ui_out_text (out, "#"); |
| 1131 | ui_out_field_fmt_int (out, 2, ui_left, "level", |
| 1132 | level); |
| 1133 | } |
| 1134 | } |
| 1135 | if (except.reason < 0) |
| 1136 | { |
| 1137 | gdbpy_convert_exception (except); |
| 1138 | goto error; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | if (print_frame_info) |
| 1143 | { |
| 1144 | /* Print address to the address field. If an address is not provided, |
| 1145 | print nothing. */ |
| 1146 | if (opts.addressprint && has_addr) |
| 1147 | { |
| 1148 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1149 | { |
| 1150 | annotate_frame_address (); |
| 1151 | ui_out_field_core_addr (out, "addr", gdbarch, address); |
| 1152 | annotate_frame_address_end (); |
| 1153 | ui_out_text (out, " in "); |
| 1154 | } |
| 1155 | if (except.reason < 0) |
| 1156 | { |
| 1157 | gdbpy_convert_exception (except); |
| 1158 | goto error; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /* Print frame function name. */ |
| 1163 | if (PyObject_HasAttrString (filter, "function")) |
| 1164 | { |
| 1165 | PyObject *py_func = PyObject_CallMethod (filter, "function", NULL); |
| 1166 | const char *function = NULL; |
| 1167 | |
| 1168 | if (py_func == NULL) |
| 1169 | goto error; |
| 1170 | |
| 1171 | if (gdbpy_is_string (py_func)) |
| 1172 | { |
| 1173 | char *function_to_free; |
| 1174 | |
| 1175 | function = function_to_free = |
| 1176 | python_string_to_host_string (py_func); |
| 1177 | |
| 1178 | if (function == NULL) |
| 1179 | { |
| 1180 | Py_DECREF (py_func); |
| 1181 | goto error; |
| 1182 | } |
| 1183 | make_cleanup (xfree, function_to_free); |
| 1184 | } |
| 1185 | else if (PyLong_Check (py_func)) |
| 1186 | { |
| 1187 | CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func); |
| 1188 | struct bound_minimal_symbol msymbol; |
| 1189 | |
| 1190 | if (PyErr_Occurred ()) |
| 1191 | goto error; |
| 1192 | |
| 1193 | msymbol = lookup_minimal_symbol_by_pc (addr); |
| 1194 | if (msymbol.minsym != NULL) |
| 1195 | function = MSYMBOL_PRINT_NAME (msymbol.minsym); |
| 1196 | } |
| 1197 | else if (py_func != Py_None) |
| 1198 | { |
| 1199 | PyErr_SetString (PyExc_RuntimeError, |
| 1200 | _("FrameDecorator.function: expecting a " \ |
| 1201 | "String, integer or None.")); |
| 1202 | Py_DECREF (py_func); |
| 1203 | goto error; |
| 1204 | } |
| 1205 | |
| 1206 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1207 | { |
| 1208 | annotate_frame_function_name (); |
| 1209 | if (function == NULL) |
| 1210 | ui_out_field_skip (out, "func"); |
| 1211 | else |
| 1212 | ui_out_field_string (out, "func", function); |
| 1213 | } |
| 1214 | if (except.reason < 0) |
| 1215 | { |
| 1216 | Py_DECREF (py_func); |
| 1217 | gdbpy_convert_exception (except); |
| 1218 | goto error; |
| 1219 | } |
| 1220 | Py_DECREF (py_func); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | /* Frame arguments. Check the result, and error if something went |
| 1226 | wrong. */ |
| 1227 | if (print_args) |
| 1228 | { |
| 1229 | if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR) |
| 1230 | goto error; |
| 1231 | } |
| 1232 | |
| 1233 | /* File name/source/line number information. */ |
| 1234 | if (print_frame_info) |
| 1235 | { |
| 1236 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1237 | { |
| 1238 | annotate_frame_source_begin (); |
| 1239 | } |
| 1240 | if (except.reason < 0) |
| 1241 | { |
| 1242 | gdbpy_convert_exception (except); |
| 1243 | goto error; |
| 1244 | } |
| 1245 | |
| 1246 | if (PyObject_HasAttrString (filter, "filename")) |
| 1247 | { |
| 1248 | PyObject *py_fn = PyObject_CallMethod (filter, "filename", NULL); |
| 1249 | |
| 1250 | if (py_fn == NULL) |
| 1251 | goto error; |
| 1252 | |
| 1253 | if (py_fn != Py_None) |
| 1254 | { |
| 1255 | char *filename = python_string_to_host_string (py_fn); |
| 1256 | |
| 1257 | if (filename == NULL) |
| 1258 | { |
| 1259 | Py_DECREF (py_fn); |
| 1260 | goto error; |
| 1261 | } |
| 1262 | |
| 1263 | make_cleanup (xfree, filename); |
| 1264 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1265 | { |
| 1266 | ui_out_wrap_hint (out, " "); |
| 1267 | ui_out_text (out, " at "); |
| 1268 | annotate_frame_source_file (); |
| 1269 | ui_out_field_string (out, "file", filename); |
| 1270 | annotate_frame_source_file_end (); |
| 1271 | } |
| 1272 | if (except.reason < 0) |
| 1273 | { |
| 1274 | Py_DECREF (py_fn); |
| 1275 | gdbpy_convert_exception (except); |
| 1276 | goto error; |
| 1277 | } |
| 1278 | } |
| 1279 | Py_DECREF (py_fn); |
| 1280 | } |
| 1281 | |
| 1282 | if (PyObject_HasAttrString (filter, "line")) |
| 1283 | { |
| 1284 | PyObject *py_line = PyObject_CallMethod (filter, "line", NULL); |
| 1285 | int line; |
| 1286 | |
| 1287 | if (py_line == NULL) |
| 1288 | goto error; |
| 1289 | |
| 1290 | if (py_line != Py_None) |
| 1291 | { |
| 1292 | line = PyLong_AsLong (py_line); |
| 1293 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1294 | { |
| 1295 | ui_out_text (out, ":"); |
| 1296 | annotate_frame_source_line (); |
| 1297 | ui_out_field_int (out, "line", line); |
| 1298 | } |
| 1299 | if (except.reason < 0) |
| 1300 | { |
| 1301 | Py_DECREF (py_line); |
| 1302 | gdbpy_convert_exception (except); |
| 1303 | goto error; |
| 1304 | } |
| 1305 | } |
| 1306 | Py_DECREF (py_line); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* For MI we need to deal with the "children" list population of |
| 1311 | elided frames, so if MI output detected do not send newline. */ |
| 1312 | if (! ui_out_is_mi_like_p (out)) |
| 1313 | { |
| 1314 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1315 | { |
| 1316 | annotate_frame_end (); |
| 1317 | ui_out_text (out, "\n"); |
| 1318 | } |
| 1319 | if (except.reason < 0) |
| 1320 | { |
| 1321 | gdbpy_convert_exception (except); |
| 1322 | goto error; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | if (print_locals) |
| 1327 | { |
| 1328 | if (py_print_locals (filter, out, args_type, indent, |
| 1329 | frame) == EXT_LANG_BT_ERROR) |
| 1330 | goto error; |
| 1331 | } |
| 1332 | |
| 1333 | { |
| 1334 | PyObject *elided; |
| 1335 | |
| 1336 | /* Finally recursively print elided frames, if any. */ |
| 1337 | elided = get_py_iter_from_func (filter, "elided"); |
| 1338 | if (elided == NULL) |
| 1339 | goto error; |
| 1340 | |
| 1341 | make_cleanup_py_decref (elided); |
| 1342 | if (elided != Py_None) |
| 1343 | { |
| 1344 | PyObject *item; |
| 1345 | |
| 1346 | make_cleanup_ui_out_list_begin_end (out, "children"); |
| 1347 | |
| 1348 | if (! ui_out_is_mi_like_p (out)) |
| 1349 | indent++; |
| 1350 | |
| 1351 | while ((item = PyIter_Next (elided))) |
| 1352 | { |
| 1353 | enum ext_lang_bt_status success = py_print_frame (item, flags, |
| 1354 | args_type, out, |
| 1355 | indent, |
| 1356 | levels_printed); |
| 1357 | |
| 1358 | if (success == EXT_LANG_BT_ERROR) |
| 1359 | { |
| 1360 | Py_DECREF (item); |
| 1361 | goto error; |
| 1362 | } |
| 1363 | |
| 1364 | Py_DECREF (item); |
| 1365 | } |
| 1366 | if (item == NULL && PyErr_Occurred ()) |
| 1367 | goto error; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | do_cleanups (cleanup_stack); |
| 1373 | return EXT_LANG_BT_COMPLETED; |
| 1374 | |
| 1375 | error: |
| 1376 | do_cleanups (cleanup_stack); |
| 1377 | return EXT_LANG_BT_ERROR; |
| 1378 | } |
| 1379 | |
| 1380 | /* Helper function to initiate frame filter invocation at starting |
| 1381 | frame FRAME. */ |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | bootstrap_python_frame_filters (struct frame_info *frame, |
| 1385 | int frame_low, int frame_high) |
| 1386 | { |
| 1387 | struct cleanup *cleanups = |
| 1388 | make_cleanup (null_cleanup, NULL); |
| 1389 | PyObject *module, *sort_func, *iterable, *frame_obj, *iterator; |
| 1390 | PyObject *py_frame_low, *py_frame_high; |
| 1391 | |
| 1392 | frame_obj = frame_info_to_frame_object (frame); |
| 1393 | if (frame_obj == NULL) |
| 1394 | goto error; |
| 1395 | make_cleanup_py_decref (frame_obj); |
| 1396 | |
| 1397 | module = PyImport_ImportModule ("gdb.frames"); |
| 1398 | if (module == NULL) |
| 1399 | goto error; |
| 1400 | make_cleanup_py_decref (module); |
| 1401 | |
| 1402 | sort_func = PyObject_GetAttrString (module, "execute_frame_filters"); |
| 1403 | if (sort_func == NULL) |
| 1404 | goto error; |
| 1405 | make_cleanup_py_decref (sort_func); |
| 1406 | |
| 1407 | py_frame_low = PyInt_FromLong (frame_low); |
| 1408 | if (py_frame_low == NULL) |
| 1409 | goto error; |
| 1410 | make_cleanup_py_decref (py_frame_low); |
| 1411 | |
| 1412 | py_frame_high = PyInt_FromLong (frame_high); |
| 1413 | if (py_frame_high == NULL) |
| 1414 | goto error; |
| 1415 | make_cleanup_py_decref (py_frame_high); |
| 1416 | |
| 1417 | iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj, |
| 1418 | py_frame_low, |
| 1419 | py_frame_high, |
| 1420 | NULL); |
| 1421 | if (iterable == NULL) |
| 1422 | goto error; |
| 1423 | |
| 1424 | do_cleanups (cleanups); |
| 1425 | |
| 1426 | if (iterable != Py_None) |
| 1427 | { |
| 1428 | iterator = PyObject_GetIter (iterable); |
| 1429 | Py_DECREF (iterable); |
| 1430 | } |
| 1431 | else |
| 1432 | { |
| 1433 | return iterable; |
| 1434 | } |
| 1435 | |
| 1436 | return iterator; |
| 1437 | |
| 1438 | error: |
| 1439 | do_cleanups (cleanups); |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
| 1443 | /* This is the only publicly exported function in this file. FRAME |
| 1444 | is the source frame to start frame-filter invocation. FLAGS is an |
| 1445 | integer holding the flags for printing. The following elements of |
| 1446 | the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS: |
| 1447 | PRINT_LEVEL is a flag indicating whether to print the frame's |
| 1448 | relative level in the output. PRINT_FRAME_INFO is a flag that |
| 1449 | indicates whether this function should print the frame |
| 1450 | information, PRINT_ARGS is a flag that indicates whether to print |
| 1451 | frame arguments, and PRINT_LOCALS, likewise, with frame local |
| 1452 | variables. ARGS_TYPE is an enumerator describing the argument |
| 1453 | format, OUT is the output stream to print. FRAME_LOW is the |
| 1454 | beginning of the slice of frames to print, and FRAME_HIGH is the |
| 1455 | upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error, |
| 1456 | or EXT_LANG_BT_COMPLETED on success. */ |
| 1457 | |
| 1458 | enum ext_lang_bt_status |
| 1459 | gdbpy_apply_frame_filter (const struct extension_language_defn *extlang, |
| 1460 | struct frame_info *frame, int flags, |
| 1461 | enum ext_lang_frame_args args_type, |
| 1462 | struct ui_out *out, int frame_low, int frame_high) |
| 1463 | { |
| 1464 | struct gdbarch *gdbarch = NULL; |
| 1465 | struct cleanup *cleanups; |
| 1466 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 1467 | PyObject *iterable; |
| 1468 | volatile struct gdb_exception except; |
| 1469 | PyObject *item; |
| 1470 | htab_t levels_printed; |
| 1471 | |
| 1472 | if (!gdb_python_initialized) |
| 1473 | return EXT_LANG_BT_NO_FILTERS; |
| 1474 | |
| 1475 | TRY_CATCH (except, RETURN_MASK_ALL) |
| 1476 | { |
| 1477 | gdbarch = get_frame_arch (frame); |
| 1478 | } |
| 1479 | if (except.reason < 0) |
| 1480 | { |
| 1481 | /* Let gdb try to print the stack trace. */ |
| 1482 | return EXT_LANG_BT_NO_FILTERS; |
| 1483 | } |
| 1484 | |
| 1485 | cleanups = ensure_python_env (gdbarch, current_language); |
| 1486 | |
| 1487 | iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high); |
| 1488 | |
| 1489 | if (iterable == NULL) |
| 1490 | { |
| 1491 | /* Normally if there is an error GDB prints the exception, |
| 1492 | abandons the backtrace and exits. The user can then call "bt |
| 1493 | no-filters", and get a default backtrace (it would be |
| 1494 | confusing to automatically start a standard backtrace halfway |
| 1495 | through a Python filtered backtrace). However in the case |
| 1496 | where GDB cannot initialize the frame filters (most likely |
| 1497 | due to incorrect auto-load paths), GDB has printed nothing. |
| 1498 | In this case it is OK to print the default backtrace after |
| 1499 | printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS |
| 1500 | here to signify there are no filters after printing the |
| 1501 | initialization error. This return code will trigger a |
| 1502 | default backtrace. */ |
| 1503 | |
| 1504 | gdbpy_print_stack (); |
| 1505 | do_cleanups (cleanups); |
| 1506 | return EXT_LANG_BT_NO_FILTERS; |
| 1507 | } |
| 1508 | |
| 1509 | /* If iterable is None, then there are no frame filters registered. |
| 1510 | If this is the case, defer to default GDB printing routines in MI |
| 1511 | and CLI. */ |
| 1512 | make_cleanup_py_decref (iterable); |
| 1513 | if (iterable == Py_None) |
| 1514 | { |
| 1515 | success = EXT_LANG_BT_NO_FILTERS; |
| 1516 | goto done; |
| 1517 | } |
| 1518 | |
| 1519 | levels_printed = htab_create (20, |
| 1520 | htab_hash_pointer, |
| 1521 | htab_eq_pointer, |
| 1522 | NULL); |
| 1523 | make_cleanup_htab_delete (levels_printed); |
| 1524 | |
| 1525 | while ((item = PyIter_Next (iterable))) |
| 1526 | { |
| 1527 | success = py_print_frame (item, flags, args_type, out, 0, |
| 1528 | levels_printed); |
| 1529 | |
| 1530 | /* Do not exit on error printing a single frame. Print the |
| 1531 | error and continue with other frames. */ |
| 1532 | if (success == EXT_LANG_BT_ERROR) |
| 1533 | gdbpy_print_stack (); |
| 1534 | |
| 1535 | Py_DECREF (item); |
| 1536 | } |
| 1537 | |
| 1538 | if (item == NULL && PyErr_Occurred ()) |
| 1539 | goto error; |
| 1540 | |
| 1541 | done: |
| 1542 | do_cleanups (cleanups); |
| 1543 | return success; |
| 1544 | |
| 1545 | /* Exit and abandon backtrace on error, printing the exception that |
| 1546 | is set. */ |
| 1547 | error: |
| 1548 | gdbpy_print_stack (); |
| 1549 | do_cleanups (cleanups); |
| 1550 | return EXT_LANG_BT_ERROR; |
| 1551 | } |