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