| 1 | /* Python frame filters |
| 2 | |
| 3 | Copyright (C) 2013-2018 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 | #include "py-ref.h" |
| 34 | #include "common/gdb_optional.h" |
| 35 | |
| 36 | enum mi_print_types |
| 37 | { |
| 38 | MI_PRINT_ARGS, |
| 39 | MI_PRINT_LOCALS |
| 40 | }; |
| 41 | |
| 42 | /* Helper function to extract a symbol, a name and a language |
| 43 | definition from a Python object that conforms to the "Symbol Value" |
| 44 | interface. OBJ is the Python object to extract the values from. |
| 45 | NAME is a pass-through argument where the name of the symbol will |
| 46 | be written. NAME is allocated in this function, but the caller is |
| 47 | responsible for clean up. SYM is a pass-through argument where the |
| 48 | symbol will be written and SYM_BLOCK is a pass-through argument to |
| 49 | write the block where the symbol lies in. In the case of the API |
| 50 | returning a string, this will be set to NULL. LANGUAGE is also a |
| 51 | pass-through argument denoting the language attributed to the |
| 52 | Symbol. In the case of SYM being NULL, this will be set to the |
| 53 | current language. Returns EXT_LANG_BT_ERROR on error with the |
| 54 | appropriate Python exception set, and EXT_LANG_BT_OK on success. */ |
| 55 | |
| 56 | static enum ext_lang_bt_status |
| 57 | extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name, |
| 58 | struct symbol **sym, struct block **sym_block, |
| 59 | const struct language_defn **language) |
| 60 | { |
| 61 | gdbpy_ref<> result (PyObject_CallMethod (obj, "symbol", NULL)); |
| 62 | |
| 63 | if (result == NULL) |
| 64 | return EXT_LANG_BT_ERROR; |
| 65 | |
| 66 | /* For 'symbol' callback, the function can return a symbol or a |
| 67 | string. */ |
| 68 | if (gdbpy_is_string (result.get ())) |
| 69 | { |
| 70 | *name = python_string_to_host_string (result.get ()); |
| 71 | |
| 72 | if (*name == NULL) |
| 73 | return EXT_LANG_BT_ERROR; |
| 74 | /* If the API returns a string (and not a symbol), then there is |
| 75 | no symbol derived language available and the frame filter has |
| 76 | either overridden the symbol with a string, or supplied a |
| 77 | entirely synthetic symbol/value pairing. In that case, use |
| 78 | python_language. */ |
| 79 | *language = python_language; |
| 80 | *sym = NULL; |
| 81 | *sym_block = NULL; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | /* This type checks 'result' during the conversion so we |
| 86 | just call it unconditionally and check the return. */ |
| 87 | *sym = symbol_object_to_symbol (result.get ()); |
| 88 | /* TODO: currently, we have no way to recover the block in which SYMBOL |
| 89 | was found, so we have no block to return. Trying to evaluate SYMBOL |
| 90 | will yield an incorrect value when it's located in a FRAME and |
| 91 | evaluated from another frame (as permitted in nested functions). */ |
| 92 | *sym_block = NULL; |
| 93 | |
| 94 | if (*sym == NULL) |
| 95 | { |
| 96 | PyErr_SetString (PyExc_RuntimeError, |
| 97 | _("Unexpected value. Expecting a " |
| 98 | "gdb.Symbol or a Python string.")); |
| 99 | return EXT_LANG_BT_ERROR; |
| 100 | } |
| 101 | |
| 102 | /* Duplicate the symbol name, so the caller has consistency |
| 103 | in garbage collection. */ |
| 104 | name->reset (xstrdup (SYMBOL_PRINT_NAME (*sym))); |
| 105 | |
| 106 | /* If a symbol is specified attempt to determine the language |
| 107 | from the symbol. If mode is not "auto", then the language |
| 108 | has been explicitly set, use that. */ |
| 109 | if (language_mode == language_mode_auto) |
| 110 | *language = language_def (SYMBOL_LANGUAGE (*sym)); |
| 111 | else |
| 112 | *language = current_language; |
| 113 | } |
| 114 | |
| 115 | return EXT_LANG_BT_OK; |
| 116 | } |
| 117 | |
| 118 | /* Helper function to extract a value from an object that conforms to |
| 119 | the "Symbol Value" interface. OBJ is the Python object to extract |
| 120 | the value from. VALUE is a pass-through argument where the value |
| 121 | will be written. If the object does not have the value attribute, |
| 122 | or provides the Python None for a value, VALUE will be set to NULL |
| 123 | and this function will return as successful. Returns EXT_LANG_BT_ERROR |
| 124 | on error with the appropriate Python exception set, and EXT_LANG_BT_OK on |
| 125 | success. */ |
| 126 | |
| 127 | static enum ext_lang_bt_status |
| 128 | extract_value (PyObject *obj, struct value **value) |
| 129 | { |
| 130 | if (PyObject_HasAttrString (obj, "value")) |
| 131 | { |
| 132 | gdbpy_ref<> vresult (PyObject_CallMethod (obj, "value", NULL)); |
| 133 | |
| 134 | if (vresult == NULL) |
| 135 | return EXT_LANG_BT_ERROR; |
| 136 | |
| 137 | /* The Python code has returned 'None' for a value, so we set |
| 138 | value to NULL. This flags that GDB should read the |
| 139 | value. */ |
| 140 | if (vresult == Py_None) |
| 141 | { |
| 142 | *value = NULL; |
| 143 | return EXT_LANG_BT_OK; |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | *value = convert_value_from_python (vresult.get ()); |
| 148 | |
| 149 | if (*value == NULL) |
| 150 | return EXT_LANG_BT_ERROR; |
| 151 | |
| 152 | return EXT_LANG_BT_OK; |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | *value = NULL; |
| 157 | |
| 158 | return EXT_LANG_BT_OK; |
| 159 | } |
| 160 | |
| 161 | /* MI prints only certain values according to the type of symbol and |
| 162 | also what the user has specified. SYM is the symbol to check, and |
| 163 | MI_PRINT_TYPES is an enum specifying what the user wants emitted |
| 164 | for the MI command in question. */ |
| 165 | static int |
| 166 | mi_should_print (struct symbol *sym, enum mi_print_types type) |
| 167 | { |
| 168 | int print_me = 0; |
| 169 | |
| 170 | switch (SYMBOL_CLASS (sym)) |
| 171 | { |
| 172 | default: |
| 173 | case LOC_UNDEF: /* catches errors */ |
| 174 | case LOC_CONST: /* constant */ |
| 175 | case LOC_TYPEDEF: /* local typedef */ |
| 176 | case LOC_LABEL: /* local label */ |
| 177 | case LOC_BLOCK: /* local function */ |
| 178 | case LOC_CONST_BYTES: /* loc. byte seq. */ |
| 179 | case LOC_UNRESOLVED: /* unresolved static */ |
| 180 | case LOC_OPTIMIZED_OUT: /* optimized out */ |
| 181 | print_me = 0; |
| 182 | break; |
| 183 | |
| 184 | case LOC_ARG: /* argument */ |
| 185 | case LOC_REF_ARG: /* reference arg */ |
| 186 | case LOC_REGPARM_ADDR: /* indirect register arg */ |
| 187 | case LOC_LOCAL: /* stack local */ |
| 188 | case LOC_STATIC: /* static */ |
| 189 | case LOC_REGISTER: /* register */ |
| 190 | case LOC_COMPUTED: /* computed location */ |
| 191 | if (type == MI_PRINT_LOCALS) |
| 192 | print_me = ! SYMBOL_IS_ARGUMENT (sym); |
| 193 | else |
| 194 | print_me = SYMBOL_IS_ARGUMENT (sym); |
| 195 | } |
| 196 | return print_me; |
| 197 | } |
| 198 | |
| 199 | /* Helper function which outputs a type name extracted from VAL to a |
| 200 | "type" field in the output stream OUT. OUT is the ui-out structure |
| 201 | the type name will be output too, and VAL is the value that the |
| 202 | type will be extracted from. */ |
| 203 | |
| 204 | static void |
| 205 | py_print_type (struct ui_out *out, struct value *val) |
| 206 | { |
| 207 | check_typedef (value_type (val)); |
| 208 | |
| 209 | string_file stb; |
| 210 | type_print (value_type (val), "", &stb, -1); |
| 211 | out->field_stream ("type", stb); |
| 212 | } |
| 213 | |
| 214 | /* Helper function which outputs a value to an output field in a |
| 215 | stream. OUT is the ui-out structure the value will be output to, |
| 216 | VAL is the value that will be printed, OPTS contains the value |
| 217 | printing options, ARGS_TYPE is an enumerator describing the |
| 218 | argument format, and LANGUAGE is the language_defn that the value |
| 219 | will be printed with. */ |
| 220 | |
| 221 | static void |
| 222 | py_print_value (struct ui_out *out, struct value *val, |
| 223 | const struct value_print_options *opts, |
| 224 | int indent, |
| 225 | enum ext_lang_frame_args args_type, |
| 226 | const struct language_defn *language) |
| 227 | { |
| 228 | int should_print = 0; |
| 229 | |
| 230 | /* MI does not print certain values, differentiated by type, |
| 231 | depending on what ARGS_TYPE indicates. Test type against option. |
| 232 | For CLI print all values. */ |
| 233 | if (args_type == MI_PRINT_SIMPLE_VALUES |
| 234 | || args_type == MI_PRINT_ALL_VALUES) |
| 235 | { |
| 236 | struct type *type = check_typedef (value_type (val)); |
| 237 | |
| 238 | if (args_type == MI_PRINT_ALL_VALUES) |
| 239 | should_print = 1; |
| 240 | else if (args_type == MI_PRINT_SIMPLE_VALUES |
| 241 | && TYPE_CODE (type) != TYPE_CODE_ARRAY |
| 242 | && TYPE_CODE (type) != TYPE_CODE_STRUCT |
| 243 | && TYPE_CODE (type) != TYPE_CODE_UNION) |
| 244 | should_print = 1; |
| 245 | } |
| 246 | else if (args_type != NO_VALUES) |
| 247 | should_print = 1; |
| 248 | |
| 249 | if (should_print) |
| 250 | { |
| 251 | string_file stb; |
| 252 | |
| 253 | common_val_print (val, &stb, indent, opts, language); |
| 254 | out->field_stream ("value", stb); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* Helper function to call a Python method and extract an iterator |
| 259 | from the result. If the function returns anything but an iterator |
| 260 | the exception is preserved and NULL is returned. FILTER is the |
| 261 | Python object to call, and FUNC is the name of the method. Returns |
| 262 | a PyObject, or NULL on error with the appropriate exception set. |
| 263 | This function can return an iterator, or NULL. */ |
| 264 | |
| 265 | static PyObject * |
| 266 | get_py_iter_from_func (PyObject *filter, const char *func) |
| 267 | { |
| 268 | if (PyObject_HasAttrString (filter, func)) |
| 269 | { |
| 270 | gdbpy_ref<> result (PyObject_CallMethod (filter, func, NULL)); |
| 271 | |
| 272 | if (result != NULL) |
| 273 | { |
| 274 | if (result == Py_None) |
| 275 | { |
| 276 | return result.release (); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | return PyObject_GetIter (result.get ()); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | else |
| 285 | Py_RETURN_NONE; |
| 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /* Helper function to output a single frame argument and value to an |
| 291 | output stream. This function will account for entry values if the |
| 292 | FV parameter is populated, the frame argument has entry values |
| 293 | associated with them, and the appropriate "set entry-value" |
| 294 | options are set. Will output in CLI or MI like format depending |
| 295 | on the type of output stream detected. OUT is the output stream, |
| 296 | SYM_NAME is the name of the symbol. If SYM_NAME is populated then |
| 297 | it must have an accompanying value in the parameter FV. FA is a |
| 298 | frame argument structure. If FA is populated, both SYM_NAME and |
| 299 | FV are ignored. OPTS contains the value printing options, |
| 300 | ARGS_TYPE is an enumerator describing the argument format, |
| 301 | PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1" |
| 302 | in MI output in commands where both arguments and locals are |
| 303 | printed. */ |
| 304 | |
| 305 | static void |
| 306 | py_print_single_arg (struct ui_out *out, |
| 307 | const char *sym_name, |
| 308 | struct frame_arg *fa, |
| 309 | struct value *fv, |
| 310 | const struct value_print_options *opts, |
| 311 | enum ext_lang_frame_args args_type, |
| 312 | int print_args_field, |
| 313 | const struct language_defn *language) |
| 314 | { |
| 315 | struct value *val; |
| 316 | |
| 317 | if (fa != NULL) |
| 318 | { |
| 319 | if (fa->val == NULL && fa->error == NULL) |
| 320 | return; |
| 321 | language = language_def (SYMBOL_LANGUAGE (fa->sym)); |
| 322 | val = fa->val; |
| 323 | } |
| 324 | else |
| 325 | val = fv; |
| 326 | |
| 327 | gdb::optional<ui_out_emit_tuple> maybe_tuple; |
| 328 | |
| 329 | /* MI has varying rules for tuples, but generally if there is only |
| 330 | one element in each item in the list, do not start a tuple. The |
| 331 | exception is -stack-list-variables which emits an ARGS="1" field |
| 332 | if the value is a frame argument. This is denoted in this |
| 333 | function with PRINT_ARGS_FIELD which is flag from the caller to |
| 334 | emit the ARGS field. */ |
| 335 | if (out->is_mi_like_p ()) |
| 336 | { |
| 337 | if (print_args_field || args_type != NO_VALUES) |
| 338 | maybe_tuple.emplace (out, nullptr); |
| 339 | } |
| 340 | |
| 341 | annotate_arg_begin (); |
| 342 | |
| 343 | /* If frame argument is populated, check for entry-values and the |
| 344 | entry value options. */ |
| 345 | if (fa != NULL) |
| 346 | { |
| 347 | string_file stb; |
| 348 | |
| 349 | fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (fa->sym), |
| 350 | SYMBOL_LANGUAGE (fa->sym), |
| 351 | DMGL_PARAMS | DMGL_ANSI); |
| 352 | if (fa->entry_kind == print_entry_values_compact) |
| 353 | { |
| 354 | stb.puts ("="); |
| 355 | |
| 356 | fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (fa->sym), |
| 357 | SYMBOL_LANGUAGE (fa->sym), |
| 358 | DMGL_PARAMS | DMGL_ANSI); |
| 359 | } |
| 360 | if (fa->entry_kind == print_entry_values_only |
| 361 | || fa->entry_kind == print_entry_values_compact) |
| 362 | stb.puts ("@entry"); |
| 363 | out->field_stream ("name", stb); |
| 364 | } |
| 365 | else |
| 366 | /* Otherwise, just output the name. */ |
| 367 | out->field_string ("name", sym_name); |
| 368 | |
| 369 | annotate_arg_name_end (); |
| 370 | |
| 371 | out->text ("="); |
| 372 | |
| 373 | if (print_args_field) |
| 374 | out->field_int ("arg", 1); |
| 375 | |
| 376 | /* For MI print the type, but only for simple values. This seems |
| 377 | weird, but this is how MI choose to format the various output |
| 378 | types. */ |
| 379 | if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL) |
| 380 | py_print_type (out, val); |
| 381 | |
| 382 | if (val != NULL) |
| 383 | annotate_arg_value (value_type (val)); |
| 384 | |
| 385 | /* If the output is to the CLI, and the user option "set print |
| 386 | frame-arguments" is set to none, just output "...". */ |
| 387 | if (! out->is_mi_like_p () && args_type == NO_VALUES) |
| 388 | out->field_string ("value", "..."); |
| 389 | else |
| 390 | { |
| 391 | /* Otherwise, print the value for both MI and the CLI, except |
| 392 | for the case of MI_PRINT_NO_VALUES. */ |
| 393 | if (args_type != NO_VALUES) |
| 394 | { |
| 395 | if (val == NULL) |
| 396 | { |
| 397 | gdb_assert (fa != NULL && fa->error != NULL); |
| 398 | out->field_fmt ("value", |
| 399 | _("<error reading variable: %s>"), |
| 400 | fa->error); |
| 401 | } |
| 402 | else |
| 403 | py_print_value (out, val, opts, 0, args_type, language); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* Helper function to loop over frame arguments provided by the |
| 409 | "frame_arguments" Python API. Elements in the iterator must |
| 410 | conform to the "Symbol Value" interface. ITER is the Python |
| 411 | iterable object, OUT is the output stream, ARGS_TYPE is an |
| 412 | enumerator describing the argument format, PRINT_ARGS_FIELD is a |
| 413 | flag which indicates if we output "ARGS=1" in MI output in commands |
| 414 | where both arguments and locals are printed, and FRAME is the |
| 415 | backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB |
| 416 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on |
| 417 | success. */ |
| 418 | |
| 419 | static enum ext_lang_bt_status |
| 420 | enumerate_args (PyObject *iter, |
| 421 | struct ui_out *out, |
| 422 | enum ext_lang_frame_args args_type, |
| 423 | int print_args_field, |
| 424 | struct frame_info *frame) |
| 425 | { |
| 426 | struct value_print_options opts; |
| 427 | |
| 428 | get_user_print_options (&opts); |
| 429 | |
| 430 | if (args_type == CLI_SCALAR_VALUES) |
| 431 | { |
| 432 | /* True in "summary" mode, false otherwise. */ |
| 433 | opts.summary = 1; |
| 434 | } |
| 435 | |
| 436 | opts.deref_ref = 1; |
| 437 | |
| 438 | annotate_frame_args (); |
| 439 | |
| 440 | /* Collect the first argument outside of the loop, so output of |
| 441 | commas in the argument output is correct. At the end of the |
| 442 | loop block collect another item from the iterator, and, if it is |
| 443 | not null emit a comma. */ |
| 444 | gdbpy_ref<> item (PyIter_Next (iter)); |
| 445 | if (item == NULL && PyErr_Occurred ()) |
| 446 | return EXT_LANG_BT_ERROR; |
| 447 | |
| 448 | while (item != NULL) |
| 449 | { |
| 450 | const struct language_defn *language; |
| 451 | gdb::unique_xmalloc_ptr<char> sym_name; |
| 452 | struct symbol *sym; |
| 453 | struct block *sym_block; |
| 454 | struct value *val; |
| 455 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 456 | |
| 457 | success = extract_sym (item.get (), &sym_name, &sym, &sym_block, |
| 458 | &language); |
| 459 | if (success == EXT_LANG_BT_ERROR) |
| 460 | return EXT_LANG_BT_ERROR; |
| 461 | |
| 462 | success = extract_value (item.get (), &val); |
| 463 | if (success == EXT_LANG_BT_ERROR) |
| 464 | return EXT_LANG_BT_ERROR; |
| 465 | |
| 466 | if (sym && out->is_mi_like_p () |
| 467 | && ! mi_should_print (sym, MI_PRINT_ARGS)) |
| 468 | continue; |
| 469 | |
| 470 | /* If the object did not provide a value, read it using |
| 471 | read_frame_args and account for entry values, if any. */ |
| 472 | if (val == NULL) |
| 473 | { |
| 474 | struct frame_arg arg, entryarg; |
| 475 | |
| 476 | /* If there is no value, and also no symbol, set error and |
| 477 | exit. */ |
| 478 | if (sym == NULL) |
| 479 | { |
| 480 | PyErr_SetString (PyExc_RuntimeError, |
| 481 | _("No symbol or value provided.")); |
| 482 | return EXT_LANG_BT_ERROR; |
| 483 | } |
| 484 | |
| 485 | read_frame_arg (sym, frame, &arg, &entryarg); |
| 486 | |
| 487 | gdb::unique_xmalloc_ptr<char> arg_holder (arg.error); |
| 488 | gdb::unique_xmalloc_ptr<char> entry_holder (entryarg.error); |
| 489 | |
| 490 | /* The object has not provided a value, so this is a frame |
| 491 | argument to be read by GDB. In this case we have to |
| 492 | account for entry-values. */ |
| 493 | |
| 494 | if (arg.entry_kind != print_entry_values_only) |
| 495 | { |
| 496 | py_print_single_arg (out, NULL, &arg, |
| 497 | NULL, &opts, |
| 498 | args_type, |
| 499 | print_args_field, |
| 500 | NULL); |
| 501 | } |
| 502 | |
| 503 | if (entryarg.entry_kind != print_entry_values_no) |
| 504 | { |
| 505 | if (arg.entry_kind != print_entry_values_only) |
| 506 | { |
| 507 | out->text (", "); |
| 508 | out->wrap_hint (" "); |
| 509 | } |
| 510 | |
| 511 | py_print_single_arg (out, NULL, &entryarg, NULL, &opts, |
| 512 | args_type, print_args_field, NULL); |
| 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | /* If the object has provided a value, we just print that. */ |
| 518 | if (val != NULL) |
| 519 | py_print_single_arg (out, sym_name.get (), NULL, val, &opts, |
| 520 | args_type, print_args_field, |
| 521 | language); |
| 522 | } |
| 523 | |
| 524 | /* Collect the next item from the iterator. If |
| 525 | this is the last item, do not print the |
| 526 | comma. */ |
| 527 | item.reset (PyIter_Next (iter)); |
| 528 | if (item != NULL) |
| 529 | out->text (", "); |
| 530 | else if (PyErr_Occurred ()) |
| 531 | return EXT_LANG_BT_ERROR; |
| 532 | |
| 533 | annotate_arg_end (); |
| 534 | } |
| 535 | |
| 536 | return EXT_LANG_BT_OK; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* Helper function to loop over variables provided by the |
| 541 | "frame_locals" Python API. Elements in the iterable must conform |
| 542 | to the "Symbol Value" interface. ITER is the Python iterable |
| 543 | object, OUT is the output stream, INDENT is whether we should |
| 544 | indent the output (for CLI), ARGS_TYPE is an enumerator describing |
| 545 | the argument format, PRINT_ARGS_FIELD is flag which indicates |
| 546 | whether to output the ARGS field in the case of |
| 547 | -stack-list-variables and FRAME is the backing frame. Returns |
| 548 | EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python |
| 549 | exception, or EXT_LANG_BT_OK on success. */ |
| 550 | |
| 551 | static enum ext_lang_bt_status |
| 552 | enumerate_locals (PyObject *iter, |
| 553 | struct ui_out *out, |
| 554 | int indent, |
| 555 | enum ext_lang_frame_args args_type, |
| 556 | int print_args_field, |
| 557 | struct frame_info *frame) |
| 558 | { |
| 559 | struct value_print_options opts; |
| 560 | |
| 561 | get_user_print_options (&opts); |
| 562 | opts.deref_ref = 1; |
| 563 | |
| 564 | while (true) |
| 565 | { |
| 566 | const struct language_defn *language; |
| 567 | gdb::unique_xmalloc_ptr<char> sym_name; |
| 568 | struct value *val; |
| 569 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 570 | struct symbol *sym; |
| 571 | struct block *sym_block; |
| 572 | int local_indent = 8 + (8 * indent); |
| 573 | gdb::optional<ui_out_emit_tuple> tuple; |
| 574 | |
| 575 | gdbpy_ref<> item (PyIter_Next (iter)); |
| 576 | if (item == NULL) |
| 577 | break; |
| 578 | |
| 579 | success = extract_sym (item.get (), &sym_name, &sym, &sym_block, |
| 580 | &language); |
| 581 | if (success == EXT_LANG_BT_ERROR) |
| 582 | return EXT_LANG_BT_ERROR; |
| 583 | |
| 584 | success = extract_value (item.get (), &val); |
| 585 | if (success == EXT_LANG_BT_ERROR) |
| 586 | return EXT_LANG_BT_ERROR; |
| 587 | |
| 588 | if (sym != NULL && out->is_mi_like_p () |
| 589 | && ! mi_should_print (sym, MI_PRINT_LOCALS)) |
| 590 | continue; |
| 591 | |
| 592 | /* If the object did not provide a value, read it. */ |
| 593 | if (val == NULL) |
| 594 | val = read_var_value (sym, sym_block, frame); |
| 595 | |
| 596 | /* With PRINT_NO_VALUES, MI does not emit a tuple normally as |
| 597 | each output contains only one field. The exception is |
| 598 | -stack-list-variables, which always provides a tuple. */ |
| 599 | if (out->is_mi_like_p ()) |
| 600 | { |
| 601 | if (print_args_field || args_type != NO_VALUES) |
| 602 | tuple.emplace (out, nullptr); |
| 603 | } |
| 604 | |
| 605 | /* If the output is not MI we indent locals. */ |
| 606 | out->spaces (local_indent); |
| 607 | out->field_string ("name", sym_name.get ()); |
| 608 | out->text (" = "); |
| 609 | |
| 610 | if (args_type == MI_PRINT_SIMPLE_VALUES) |
| 611 | py_print_type (out, val); |
| 612 | |
| 613 | /* CLI always prints values for locals. MI uses the |
| 614 | simple/no/all system. */ |
| 615 | if (! out->is_mi_like_p ()) |
| 616 | { |
| 617 | int val_indent = (indent + 1) * 4; |
| 618 | |
| 619 | py_print_value (out, val, &opts, val_indent, args_type, |
| 620 | language); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | if (args_type != NO_VALUES) |
| 625 | py_print_value (out, val, &opts, 0, args_type, |
| 626 | language); |
| 627 | } |
| 628 | |
| 629 | out->text ("\n"); |
| 630 | } |
| 631 | |
| 632 | if (!PyErr_Occurred ()) |
| 633 | return EXT_LANG_BT_OK; |
| 634 | |
| 635 | return EXT_LANG_BT_ERROR; |
| 636 | } |
| 637 | |
| 638 | /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on |
| 639 | error, or EXT_LANG_BT_OK on success. */ |
| 640 | |
| 641 | static enum ext_lang_bt_status |
| 642 | py_mi_print_variables (PyObject *filter, struct ui_out *out, |
| 643 | struct value_print_options *opts, |
| 644 | enum ext_lang_frame_args args_type, |
| 645 | struct frame_info *frame) |
| 646 | { |
| 647 | gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args")); |
| 648 | if (args_iter == NULL) |
| 649 | return EXT_LANG_BT_ERROR; |
| 650 | |
| 651 | gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals")); |
| 652 | if (locals_iter == NULL) |
| 653 | return EXT_LANG_BT_ERROR; |
| 654 | |
| 655 | ui_out_emit_list list_emitter (out, "variables"); |
| 656 | |
| 657 | if (args_iter != Py_None |
| 658 | && (enumerate_args (args_iter.get (), out, args_type, 1, frame) |
| 659 | == EXT_LANG_BT_ERROR)) |
| 660 | return EXT_LANG_BT_ERROR; |
| 661 | |
| 662 | if (locals_iter != Py_None |
| 663 | && (enumerate_locals (locals_iter.get (), out, 1, args_type, 1, frame) |
| 664 | == EXT_LANG_BT_ERROR)) |
| 665 | return EXT_LANG_BT_ERROR; |
| 666 | |
| 667 | return EXT_LANG_BT_OK; |
| 668 | } |
| 669 | |
| 670 | /* Helper function for printing locals. This function largely just |
| 671 | creates the wrapping tuple, and calls enumerate_locals. Returns |
| 672 | EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */ |
| 673 | |
| 674 | static enum ext_lang_bt_status |
| 675 | py_print_locals (PyObject *filter, |
| 676 | struct ui_out *out, |
| 677 | enum ext_lang_frame_args args_type, |
| 678 | int indent, |
| 679 | struct frame_info *frame) |
| 680 | { |
| 681 | gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals")); |
| 682 | if (locals_iter == NULL) |
| 683 | return EXT_LANG_BT_ERROR; |
| 684 | |
| 685 | ui_out_emit_list list_emitter (out, "locals"); |
| 686 | |
| 687 | if (locals_iter != Py_None |
| 688 | && (enumerate_locals (locals_iter.get (), out, indent, args_type, |
| 689 | 0, frame) == EXT_LANG_BT_ERROR)) |
| 690 | return EXT_LANG_BT_ERROR; |
| 691 | |
| 692 | return EXT_LANG_BT_OK; |
| 693 | } |
| 694 | |
| 695 | /* Helper function for printing frame arguments. This function |
| 696 | largely just creates the wrapping tuple, and calls enumerate_args. |
| 697 | Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to |
| 698 | a Python exception, or EXT_LANG_BT_OK on success. */ |
| 699 | |
| 700 | static enum ext_lang_bt_status |
| 701 | py_print_args (PyObject *filter, |
| 702 | struct ui_out *out, |
| 703 | enum ext_lang_frame_args args_type, |
| 704 | struct frame_info *frame) |
| 705 | { |
| 706 | gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args")); |
| 707 | if (args_iter == NULL) |
| 708 | return EXT_LANG_BT_ERROR; |
| 709 | |
| 710 | ui_out_emit_list list_emitter (out, "args"); |
| 711 | |
| 712 | out->wrap_hint (" "); |
| 713 | annotate_frame_args (); |
| 714 | out->text (" ("); |
| 715 | |
| 716 | if (args_iter != Py_None |
| 717 | && (enumerate_args (args_iter.get (), out, args_type, 0, frame) |
| 718 | == EXT_LANG_BT_ERROR)) |
| 719 | return EXT_LANG_BT_ERROR; |
| 720 | |
| 721 | out->text (")"); |
| 722 | |
| 723 | return EXT_LANG_BT_OK; |
| 724 | } |
| 725 | |
| 726 | /* Print a single frame to the designated output stream, detecting |
| 727 | whether the output is MI or console, and formatting the output |
| 728 | according to the conventions of that protocol. FILTER is the |
| 729 | frame-filter associated with this frame. FLAGS is an integer |
| 730 | describing the various print options. The FLAGS variables is |
| 731 | described in "apply_frame_filter" function. ARGS_TYPE is an |
| 732 | enumerator describing the argument format. OUT is the output |
| 733 | stream to print, INDENT is the level of indention for this frame |
| 734 | (in the case of elided frames), and LEVELS_PRINTED is a hash-table |
| 735 | containing all the frames level that have already been printed. |
| 736 | If a frame level has been printed, do not print it again (in the |
| 737 | case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any |
| 738 | GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK |
| 739 | on success. It can also throw an exception RETURN_QUIT. */ |
| 740 | |
| 741 | static enum ext_lang_bt_status |
| 742 | py_print_frame (PyObject *filter, frame_filter_flags flags, |
| 743 | enum ext_lang_frame_args args_type, |
| 744 | struct ui_out *out, int indent, htab_t levels_printed) |
| 745 | { |
| 746 | int has_addr = 0; |
| 747 | CORE_ADDR address = 0; |
| 748 | struct gdbarch *gdbarch = NULL; |
| 749 | struct frame_info *frame = NULL; |
| 750 | struct value_print_options opts; |
| 751 | int print_level, print_frame_info, print_args, print_locals; |
| 752 | gdb::unique_xmalloc_ptr<char> function_to_free; |
| 753 | |
| 754 | /* Extract print settings from FLAGS. */ |
| 755 | print_level = (flags & PRINT_LEVEL) ? 1 : 0; |
| 756 | print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0; |
| 757 | print_args = (flags & PRINT_ARGS) ? 1 : 0; |
| 758 | print_locals = (flags & PRINT_LOCALS) ? 1 : 0; |
| 759 | |
| 760 | get_user_print_options (&opts); |
| 761 | |
| 762 | /* Get the underlying frame. This is needed to determine GDB |
| 763 | architecture, and also, in the cases of frame variables/arguments to |
| 764 | read them if they returned filter object requires us to do so. */ |
| 765 | gdbpy_ref<> py_inf_frame (PyObject_CallMethod (filter, "inferior_frame", |
| 766 | NULL)); |
| 767 | if (py_inf_frame == NULL) |
| 768 | return EXT_LANG_BT_ERROR; |
| 769 | |
| 770 | frame = frame_object_to_frame_info (py_inf_frame.get ()); |
| 771 | if (frame == NULL) |
| 772 | return EXT_LANG_BT_ERROR; |
| 773 | |
| 774 | gdbarch = get_frame_arch (frame); |
| 775 | |
| 776 | /* stack-list-variables. */ |
| 777 | if (print_locals && print_args && ! print_frame_info) |
| 778 | { |
| 779 | if (py_mi_print_variables (filter, out, &opts, |
| 780 | args_type, frame) == EXT_LANG_BT_ERROR) |
| 781 | return EXT_LANG_BT_ERROR; |
| 782 | return EXT_LANG_BT_OK; |
| 783 | } |
| 784 | |
| 785 | gdb::optional<ui_out_emit_tuple> tuple; |
| 786 | |
| 787 | /* -stack-list-locals does not require a |
| 788 | wrapping frame attribute. */ |
| 789 | if (print_frame_info || (print_args && ! print_locals)) |
| 790 | tuple.emplace (out, "frame"); |
| 791 | |
| 792 | if (print_frame_info) |
| 793 | { |
| 794 | /* Elided frames are also printed with this function (recursively) |
| 795 | and are printed with indention. */ |
| 796 | if (indent > 0) |
| 797 | out->spaces (indent * 4); |
| 798 | |
| 799 | /* The address is required for frame annotations, and also for |
| 800 | address printing. */ |
| 801 | if (PyObject_HasAttrString (filter, "address")) |
| 802 | { |
| 803 | gdbpy_ref<> paddr (PyObject_CallMethod (filter, "address", NULL)); |
| 804 | |
| 805 | if (paddr == NULL) |
| 806 | return EXT_LANG_BT_ERROR; |
| 807 | |
| 808 | if (paddr != Py_None) |
| 809 | { |
| 810 | if (get_addr_from_python (paddr.get (), &address) < 0) |
| 811 | return EXT_LANG_BT_ERROR; |
| 812 | |
| 813 | has_addr = 1; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* Print frame level. MI does not require the level if |
| 819 | locals/variables only are being printed. */ |
| 820 | if ((print_frame_info || print_args) && print_level) |
| 821 | { |
| 822 | struct frame_info **slot; |
| 823 | int level; |
| 824 | |
| 825 | slot = (struct frame_info **) htab_find_slot (levels_printed, |
| 826 | frame, INSERT); |
| 827 | |
| 828 | level = frame_relative_level (frame); |
| 829 | |
| 830 | /* Check if this frame has already been printed (there are cases |
| 831 | where elided synthetic dummy-frames have to 'borrow' the frame |
| 832 | architecture from the eliding frame. If that is the case, do |
| 833 | not print 'level', but print spaces. */ |
| 834 | if (*slot == frame) |
| 835 | out->field_skip ("level"); |
| 836 | else |
| 837 | { |
| 838 | *slot = frame; |
| 839 | annotate_frame_begin (print_level ? level : 0, |
| 840 | gdbarch, address); |
| 841 | out->text ("#"); |
| 842 | out->field_fmt_int (2, ui_left, "level", |
| 843 | level); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (print_frame_info) |
| 848 | { |
| 849 | /* Print address to the address field. If an address is not provided, |
| 850 | print nothing. */ |
| 851 | if (opts.addressprint && has_addr) |
| 852 | { |
| 853 | annotate_frame_address (); |
| 854 | out->field_core_addr ("addr", gdbarch, address); |
| 855 | annotate_frame_address_end (); |
| 856 | out->text (" in "); |
| 857 | } |
| 858 | |
| 859 | /* Print frame function name. */ |
| 860 | if (PyObject_HasAttrString (filter, "function")) |
| 861 | { |
| 862 | gdbpy_ref<> py_func (PyObject_CallMethod (filter, "function", NULL)); |
| 863 | const char *function = NULL; |
| 864 | |
| 865 | if (py_func == NULL) |
| 866 | return EXT_LANG_BT_ERROR; |
| 867 | |
| 868 | if (gdbpy_is_string (py_func.get ())) |
| 869 | { |
| 870 | function_to_free = python_string_to_host_string (py_func.get ()); |
| 871 | |
| 872 | if (function_to_free == NULL) |
| 873 | return EXT_LANG_BT_ERROR; |
| 874 | |
| 875 | function = function_to_free.get (); |
| 876 | } |
| 877 | else if (PyLong_Check (py_func.get ())) |
| 878 | { |
| 879 | CORE_ADDR addr; |
| 880 | struct bound_minimal_symbol msymbol; |
| 881 | |
| 882 | if (get_addr_from_python (py_func.get (), &addr) < 0) |
| 883 | return EXT_LANG_BT_ERROR; |
| 884 | |
| 885 | msymbol = lookup_minimal_symbol_by_pc (addr); |
| 886 | if (msymbol.minsym != NULL) |
| 887 | function = MSYMBOL_PRINT_NAME (msymbol.minsym); |
| 888 | } |
| 889 | else if (py_func != Py_None) |
| 890 | { |
| 891 | PyErr_SetString (PyExc_RuntimeError, |
| 892 | _("FrameDecorator.function: expecting a " \ |
| 893 | "String, integer or None.")); |
| 894 | return EXT_LANG_BT_ERROR; |
| 895 | } |
| 896 | |
| 897 | annotate_frame_function_name (); |
| 898 | if (function == NULL) |
| 899 | out->field_skip ("func"); |
| 900 | else |
| 901 | out->field_string ("func", function); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /* Frame arguments. Check the result, and error if something went |
| 907 | wrong. */ |
| 908 | if (print_args) |
| 909 | { |
| 910 | if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR) |
| 911 | return EXT_LANG_BT_ERROR; |
| 912 | } |
| 913 | |
| 914 | /* File name/source/line number information. */ |
| 915 | if (print_frame_info) |
| 916 | { |
| 917 | annotate_frame_source_begin (); |
| 918 | |
| 919 | if (PyObject_HasAttrString (filter, "filename")) |
| 920 | { |
| 921 | gdbpy_ref<> py_fn (PyObject_CallMethod (filter, "filename", NULL)); |
| 922 | |
| 923 | if (py_fn == NULL) |
| 924 | return EXT_LANG_BT_ERROR; |
| 925 | |
| 926 | if (py_fn != Py_None) |
| 927 | { |
| 928 | gdb::unique_xmalloc_ptr<char> |
| 929 | filename (python_string_to_host_string (py_fn.get ())); |
| 930 | |
| 931 | if (filename == NULL) |
| 932 | return EXT_LANG_BT_ERROR; |
| 933 | |
| 934 | out->wrap_hint (" "); |
| 935 | out->text (" at "); |
| 936 | annotate_frame_source_file (); |
| 937 | out->field_string ("file", filename.get ()); |
| 938 | annotate_frame_source_file_end (); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (PyObject_HasAttrString (filter, "line")) |
| 943 | { |
| 944 | gdbpy_ref<> py_line (PyObject_CallMethod (filter, "line", NULL)); |
| 945 | int line; |
| 946 | |
| 947 | if (py_line == NULL) |
| 948 | return EXT_LANG_BT_ERROR; |
| 949 | |
| 950 | if (py_line != Py_None) |
| 951 | { |
| 952 | line = PyLong_AsLong (py_line.get ()); |
| 953 | if (PyErr_Occurred ()) |
| 954 | return EXT_LANG_BT_ERROR; |
| 955 | |
| 956 | out->text (":"); |
| 957 | annotate_frame_source_line (); |
| 958 | out->field_int ("line", line); |
| 959 | } |
| 960 | } |
| 961 | if (out->is_mi_like_p ()) |
| 962 | out->field_string ("arch", |
| 963 | (gdbarch_bfd_arch_info (gdbarch))->printable_name); |
| 964 | } |
| 965 | |
| 966 | /* For MI we need to deal with the "children" list population of |
| 967 | elided frames, so if MI output detected do not send newline. */ |
| 968 | if (! out->is_mi_like_p ()) |
| 969 | { |
| 970 | annotate_frame_end (); |
| 971 | out->text ("\n"); |
| 972 | } |
| 973 | |
| 974 | if (print_locals) |
| 975 | { |
| 976 | if (py_print_locals (filter, out, args_type, indent, |
| 977 | frame) == EXT_LANG_BT_ERROR) |
| 978 | return EXT_LANG_BT_ERROR; |
| 979 | } |
| 980 | |
| 981 | if ((flags & PRINT_HIDE) == 0) |
| 982 | { |
| 983 | /* Finally recursively print elided frames, if any. */ |
| 984 | gdbpy_ref<> elided (get_py_iter_from_func (filter, "elided")); |
| 985 | if (elided == NULL) |
| 986 | return EXT_LANG_BT_ERROR; |
| 987 | |
| 988 | if (elided != Py_None) |
| 989 | { |
| 990 | PyObject *item; |
| 991 | |
| 992 | ui_out_emit_list inner_list_emiter (out, "children"); |
| 993 | |
| 994 | indent++; |
| 995 | |
| 996 | while ((item = PyIter_Next (elided.get ()))) |
| 997 | { |
| 998 | gdbpy_ref<> item_ref (item); |
| 999 | |
| 1000 | enum ext_lang_bt_status success |
| 1001 | = py_print_frame (item, flags, args_type, out, indent, |
| 1002 | levels_printed); |
| 1003 | |
| 1004 | if (success == EXT_LANG_BT_ERROR) |
| 1005 | return EXT_LANG_BT_ERROR; |
| 1006 | } |
| 1007 | if (item == NULL && PyErr_Occurred ()) |
| 1008 | return EXT_LANG_BT_ERROR; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | return EXT_LANG_BT_OK; |
| 1013 | } |
| 1014 | |
| 1015 | /* Helper function to initiate frame filter invocation at starting |
| 1016 | frame FRAME. */ |
| 1017 | |
| 1018 | static PyObject * |
| 1019 | bootstrap_python_frame_filters (struct frame_info *frame, |
| 1020 | int frame_low, int frame_high) |
| 1021 | { |
| 1022 | gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame)); |
| 1023 | if (frame_obj == NULL) |
| 1024 | return NULL; |
| 1025 | |
| 1026 | gdbpy_ref<> module (PyImport_ImportModule ("gdb.frames")); |
| 1027 | if (module == NULL) |
| 1028 | return NULL; |
| 1029 | |
| 1030 | gdbpy_ref<> sort_func (PyObject_GetAttrString (module.get (), |
| 1031 | "execute_frame_filters")); |
| 1032 | if (sort_func == NULL) |
| 1033 | return NULL; |
| 1034 | |
| 1035 | gdbpy_ref<> py_frame_low (PyInt_FromLong (frame_low)); |
| 1036 | if (py_frame_low == NULL) |
| 1037 | return NULL; |
| 1038 | |
| 1039 | gdbpy_ref<> py_frame_high (PyInt_FromLong (frame_high)); |
| 1040 | if (py_frame_high == NULL) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | gdbpy_ref<> iterable (PyObject_CallFunctionObjArgs (sort_func.get (), |
| 1044 | frame_obj.get (), |
| 1045 | py_frame_low.get (), |
| 1046 | py_frame_high.get (), |
| 1047 | NULL)); |
| 1048 | if (iterable == NULL) |
| 1049 | return NULL; |
| 1050 | |
| 1051 | if (iterable != Py_None) |
| 1052 | return PyObject_GetIter (iterable.get ()); |
| 1053 | else |
| 1054 | return iterable.release (); |
| 1055 | } |
| 1056 | |
| 1057 | /* This is the only publicly exported function in this file. FRAME |
| 1058 | is the source frame to start frame-filter invocation. FLAGS is an |
| 1059 | integer holding the flags for printing. The following elements of |
| 1060 | the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS: |
| 1061 | PRINT_LEVEL is a flag indicating whether to print the frame's |
| 1062 | relative level in the output. PRINT_FRAME_INFO is a flag that |
| 1063 | indicates whether this function should print the frame |
| 1064 | information, PRINT_ARGS is a flag that indicates whether to print |
| 1065 | frame arguments, and PRINT_LOCALS, likewise, with frame local |
| 1066 | variables. ARGS_TYPE is an enumerator describing the argument |
| 1067 | format, OUT is the output stream to print. FRAME_LOW is the |
| 1068 | beginning of the slice of frames to print, and FRAME_HIGH is the |
| 1069 | upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error, |
| 1070 | or EXT_LANG_BT_OK on success. */ |
| 1071 | |
| 1072 | enum ext_lang_bt_status |
| 1073 | gdbpy_apply_frame_filter (const struct extension_language_defn *extlang, |
| 1074 | struct frame_info *frame, frame_filter_flags flags, |
| 1075 | enum ext_lang_frame_args args_type, |
| 1076 | struct ui_out *out, int frame_low, int frame_high) |
| 1077 | { |
| 1078 | struct gdbarch *gdbarch = NULL; |
| 1079 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
| 1080 | |
| 1081 | if (!gdb_python_initialized) |
| 1082 | return EXT_LANG_BT_NO_FILTERS; |
| 1083 | |
| 1084 | TRY |
| 1085 | { |
| 1086 | gdbarch = get_frame_arch (frame); |
| 1087 | } |
| 1088 | CATCH (except, RETURN_MASK_ERROR) |
| 1089 | { |
| 1090 | /* Let gdb try to print the stack trace. */ |
| 1091 | return EXT_LANG_BT_NO_FILTERS; |
| 1092 | } |
| 1093 | END_CATCH |
| 1094 | |
| 1095 | gdbpy_enter enter_py (gdbarch, current_language); |
| 1096 | |
| 1097 | /* When we're limiting the number of frames, be careful to request |
| 1098 | one extra frame, so that we can print a message if there are more |
| 1099 | frames. */ |
| 1100 | int frame_countdown = -1; |
| 1101 | if ((flags & PRINT_MORE_FRAMES) != 0 && frame_low >= 0 && frame_high >= 0) |
| 1102 | { |
| 1103 | ++frame_high; |
| 1104 | /* This has an extra +1 because it is checked before a frame is |
| 1105 | printed. */ |
| 1106 | frame_countdown = frame_high - frame_low + 1; |
| 1107 | } |
| 1108 | |
| 1109 | gdbpy_ref<> iterable (bootstrap_python_frame_filters (frame, frame_low, |
| 1110 | frame_high)); |
| 1111 | |
| 1112 | if (iterable == NULL) |
| 1113 | { |
| 1114 | /* Normally if there is an error GDB prints the exception, |
| 1115 | abandons the backtrace and exits. The user can then call "bt |
| 1116 | no-filters", and get a default backtrace (it would be |
| 1117 | confusing to automatically start a standard backtrace halfway |
| 1118 | through a Python filtered backtrace). However in the case |
| 1119 | where GDB cannot initialize the frame filters (most likely |
| 1120 | due to incorrect auto-load paths), GDB has printed nothing. |
| 1121 | In this case it is OK to print the default backtrace after |
| 1122 | printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS |
| 1123 | here to signify there are no filters after printing the |
| 1124 | initialization error. This return code will trigger a |
| 1125 | default backtrace. */ |
| 1126 | |
| 1127 | gdbpy_print_stack_or_quit (); |
| 1128 | return EXT_LANG_BT_NO_FILTERS; |
| 1129 | } |
| 1130 | |
| 1131 | /* If iterable is None, then there are no frame filters registered. |
| 1132 | If this is the case, defer to default GDB printing routines in MI |
| 1133 | and CLI. */ |
| 1134 | if (iterable == Py_None) |
| 1135 | return EXT_LANG_BT_NO_FILTERS; |
| 1136 | |
| 1137 | htab_up levels_printed (htab_create (20, |
| 1138 | htab_hash_pointer, |
| 1139 | htab_eq_pointer, |
| 1140 | NULL)); |
| 1141 | |
| 1142 | while (true) |
| 1143 | { |
| 1144 | gdbpy_ref<> item (PyIter_Next (iterable.get ())); |
| 1145 | |
| 1146 | if (item == NULL) |
| 1147 | { |
| 1148 | if (PyErr_Occurred ()) |
| 1149 | { |
| 1150 | gdbpy_print_stack_or_quit (); |
| 1151 | return EXT_LANG_BT_ERROR; |
| 1152 | } |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | if (frame_countdown != -1) |
| 1157 | { |
| 1158 | gdb_assert ((flags & PRINT_MORE_FRAMES) != 0); |
| 1159 | --frame_countdown; |
| 1160 | if (frame_countdown == 0) |
| 1161 | { |
| 1162 | /* We've printed all the frames we were asked to |
| 1163 | print, but more frames existed. */ |
| 1164 | printf_filtered (_("(More stack frames follow...)\n")); |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | TRY |
| 1170 | { |
| 1171 | success = py_print_frame (item.get (), flags, args_type, out, 0, |
| 1172 | levels_printed.get ()); |
| 1173 | } |
| 1174 | CATCH (except, RETURN_MASK_ERROR) |
| 1175 | { |
| 1176 | gdbpy_convert_exception (except); |
| 1177 | success = EXT_LANG_BT_ERROR; |
| 1178 | } |
| 1179 | END_CATCH |
| 1180 | |
| 1181 | /* Do not exit on error printing a single frame. Print the |
| 1182 | error and continue with other frames. */ |
| 1183 | if (success == EXT_LANG_BT_ERROR) |
| 1184 | gdbpy_print_stack_or_quit (); |
| 1185 | } |
| 1186 | |
| 1187 | return success; |
| 1188 | } |