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