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