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