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