Use gdbpy_ref rather than make_cleanup_py_decref
[binutils-gdb.git] / gdb / python / py-framefilter.c
1 /* Python frame filters
2
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
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"
24 #include "arch-utils.h"
25 #include "python.h"
26 #include "ui-out.h"
27 #include "valprint.h"
28 #include "annotate.h"
29 #include "hashtab.h"
30 #include "demangle.h"
31 #include "mi/mi-cmds.h"
32 #include "python-internal.h"
33 #include "py-ref.h"
34
35 enum mi_print_types
36 {
37 MI_PRINT_ARGS,
38 MI_PRINT_LOCALS
39 };
40
41 /* Helper function to extract a symbol, a name and a language
42 definition from a Python object that conforms to the "Symbol Value"
43 interface. OBJ is the Python object to extract the values from.
44 NAME is a pass-through argument where the name of the symbol will
45 be written. NAME is allocated in this function, but the caller is
46 responsible for clean up. SYM is a pass-through argument where the
47 symbol will be written and SYM_BLOCK is a pass-through argument to
48 write the block where the symbol lies in. In the case of the API
49 returning a string, this will be set to NULL. LANGUAGE is also a
50 pass-through argument denoting the language attributed to the
51 Symbol. In the case of SYM being NULL, this will be set to the
52 current language. Returns EXT_LANG_BT_ERROR on error with the
53 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
54
55 static enum ext_lang_bt_status
56 extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name,
57 struct symbol **sym, struct block **sym_block,
58 const struct language_defn **language)
59 {
60 gdbpy_ref result (PyObject_CallMethod (obj, "symbol", NULL));
61
62 if (result == NULL)
63 return EXT_LANG_BT_ERROR;
64
65 /* For 'symbol' callback, the function can return a symbol or a
66 string. */
67 if (gdbpy_is_string (result.get ()))
68 {
69 *name = python_string_to_host_string (result.get ());
70
71 if (*name == NULL)
72 return EXT_LANG_BT_ERROR;
73 /* If the API returns a string (and not a symbol), then there is
74 no symbol derived language available and the frame filter has
75 either overridden the symbol with a string, or supplied a
76 entirely synthetic symbol/value pairing. In that case, use
77 python_language. */
78 *language = python_language;
79 *sym = NULL;
80 *sym_block = NULL;
81 }
82 else
83 {
84 /* This type checks 'result' during the conversion so we
85 just call it unconditionally and check the return. */
86 *sym = symbol_object_to_symbol (result.get ());
87 /* TODO: currently, we have no way to recover the block in which SYMBOL
88 was found, so we have no block to return. Trying to evaluate SYMBOL
89 will yield an incorrect value when it's located in a FRAME and
90 evaluated from another frame (as permitted in nested functions). */
91 *sym_block = NULL;
92
93 if (*sym == NULL)
94 {
95 PyErr_SetString (PyExc_RuntimeError,
96 _("Unexpected value. Expecting a "
97 "gdb.Symbol or a Python string."));
98 return EXT_LANG_BT_ERROR;
99 }
100
101 /* Duplicate the symbol name, so the caller has consistency
102 in garbage collection. */
103 name->reset (xstrdup (SYMBOL_PRINT_NAME (*sym)));
104
105 /* If a symbol is specified attempt to determine the language
106 from the symbol. If mode is not "auto", then the language
107 has been explicitly set, use that. */
108 if (language_mode == language_mode_auto)
109 *language = language_def (SYMBOL_LANGUAGE (*sym));
110 else
111 *language = current_language;
112 }
113
114 return EXT_LANG_BT_OK;
115 }
116
117 /* Helper function to extract a value from an object that conforms to
118 the "Symbol Value" interface. OBJ is the Python object to extract
119 the value from. VALUE is a pass-through argument where the value
120 will be written. If the object does not have the value attribute,
121 or provides the Python None for a value, VALUE will be set to NULL
122 and this function will return as successful. Returns EXT_LANG_BT_ERROR
123 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
124 success. */
125
126 static enum ext_lang_bt_status
127 extract_value (PyObject *obj, struct value **value)
128 {
129 if (PyObject_HasAttrString (obj, "value"))
130 {
131 gdbpy_ref vresult (PyObject_CallMethod (obj, "value", NULL));
132
133 if (vresult == NULL)
134 return EXT_LANG_BT_ERROR;
135
136 /* The Python code has returned 'None' for a value, so we set
137 value to NULL. This flags that GDB should read the
138 value. */
139 if (vresult == Py_None)
140 {
141 *value = NULL;
142 return EXT_LANG_BT_OK;
143 }
144 else
145 {
146 *value = convert_value_from_python (vresult.get ());
147
148 if (*value == NULL)
149 return EXT_LANG_BT_ERROR;
150
151 return EXT_LANG_BT_OK;
152 }
153 }
154 else
155 *value = NULL;
156
157 return EXT_LANG_BT_OK;
158 }
159
160 /* MI prints only certain values according to the type of symbol and
161 also what the user has specified. SYM is the symbol to check, and
162 MI_PRINT_TYPES is an enum specifying what the user wants emitted
163 for the MI command in question. */
164 static int
165 mi_should_print (struct symbol *sym, enum mi_print_types type)
166 {
167 int print_me = 0;
168
169 switch (SYMBOL_CLASS (sym))
170 {
171 default:
172 case LOC_UNDEF: /* catches errors */
173 case LOC_CONST: /* constant */
174 case LOC_TYPEDEF: /* local typedef */
175 case LOC_LABEL: /* local label */
176 case LOC_BLOCK: /* local function */
177 case LOC_CONST_BYTES: /* loc. byte seq. */
178 case LOC_UNRESOLVED: /* unresolved static */
179 case LOC_OPTIMIZED_OUT: /* optimized out */
180 print_me = 0;
181 break;
182
183 case LOC_ARG: /* argument */
184 case LOC_REF_ARG: /* reference arg */
185 case LOC_REGPARM_ADDR: /* indirect register arg */
186 case LOC_LOCAL: /* stack local */
187 case LOC_STATIC: /* static */
188 case LOC_REGISTER: /* register */
189 case LOC_COMPUTED: /* computed location */
190 if (type == MI_PRINT_LOCALS)
191 print_me = ! SYMBOL_IS_ARGUMENT (sym);
192 else
193 print_me = SYMBOL_IS_ARGUMENT (sym);
194 }
195 return print_me;
196 }
197
198 /* Helper function which outputs a type name extracted from VAL to a
199 "type" field in the output stream OUT. OUT is the ui-out structure
200 the type name will be output too, and VAL is the value that the
201 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
202 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
203 success. */
204
205 static enum ext_lang_bt_status
206 py_print_type (struct ui_out *out, struct value *val)
207 {
208
209 TRY
210 {
211 struct ui_file *stb;
212 struct cleanup *cleanup;
213
214 stb = mem_fileopen ();
215 cleanup = make_cleanup_ui_file_delete (stb);
216 check_typedef (value_type (val));
217 type_print (value_type (val), "", stb, -1);
218 out->field_stream ("type", stb);
219 do_cleanups (cleanup);
220 }
221 CATCH (except, RETURN_MASK_ALL)
222 {
223 gdbpy_convert_exception (except);
224 return EXT_LANG_BT_ERROR;
225 }
226 END_CATCH
227
228 return EXT_LANG_BT_OK;
229 }
230
231 /* Helper function which outputs a value to an output field in a
232 stream. OUT is the ui-out structure the value will be output to,
233 VAL is the value that will be printed, OPTS contains the value
234 printing options, ARGS_TYPE is an enumerator describing the
235 argument format, and LANGUAGE is the language_defn that the value
236 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
237 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
238 success. */
239
240 static enum ext_lang_bt_status
241 py_print_value (struct ui_out *out, struct value *val,
242 const struct value_print_options *opts,
243 int indent,
244 enum ext_lang_frame_args args_type,
245 const struct language_defn *language)
246 {
247 int should_print = 0;
248
249 /* MI does not print certain values, differentiated by type,
250 depending on what ARGS_TYPE indicates. Test type against option.
251 For CLI print all values. */
252 if (args_type == MI_PRINT_SIMPLE_VALUES
253 || args_type == MI_PRINT_ALL_VALUES)
254 {
255 struct type *type = NULL;
256
257 TRY
258 {
259 type = check_typedef (value_type (val));
260 }
261 CATCH (except, RETURN_MASK_ALL)
262 {
263 gdbpy_convert_exception (except);
264 return EXT_LANG_BT_ERROR;
265 }
266 END_CATCH
267
268 if (args_type == MI_PRINT_ALL_VALUES)
269 should_print = 1;
270 else if (args_type == MI_PRINT_SIMPLE_VALUES
271 && TYPE_CODE (type) != TYPE_CODE_ARRAY
272 && TYPE_CODE (type) != TYPE_CODE_STRUCT
273 && TYPE_CODE (type) != TYPE_CODE_UNION)
274 should_print = 1;
275 }
276 else if (args_type != NO_VALUES)
277 should_print = 1;
278
279 if (should_print)
280 {
281 TRY
282 {
283 struct ui_file *stb;
284 struct cleanup *cleanup;
285
286 stb = mem_fileopen ();
287 cleanup = make_cleanup_ui_file_delete (stb);
288 common_val_print (val, stb, indent, opts, language);
289 out->field_stream ("value", stb);
290 do_cleanups (cleanup);
291 }
292 CATCH (except, RETURN_MASK_ALL)
293 {
294 gdbpy_convert_exception (except);
295 return EXT_LANG_BT_ERROR;
296 }
297 END_CATCH
298 }
299
300 return EXT_LANG_BT_OK;
301 }
302
303 /* Helper function to call a Python method and extract an iterator
304 from the result. If the function returns anything but an iterator
305 the exception is preserved and NULL is returned. FILTER is the
306 Python object to call, and FUNC is the name of the method. Returns
307 a PyObject, or NULL on error with the appropriate exception set.
308 This function can return an iterator, or NULL. */
309
310 static PyObject *
311 get_py_iter_from_func (PyObject *filter, char *func)
312 {
313 if (PyObject_HasAttrString (filter, func))
314 {
315 gdbpy_ref result (PyObject_CallMethod (filter, func, NULL));
316
317 if (result != NULL)
318 {
319 if (result == Py_None)
320 {
321 return result.release ();
322 }
323 else
324 {
325 return PyObject_GetIter (result.get ());
326 }
327 }
328 }
329 else
330 Py_RETURN_NONE;
331
332 return NULL;
333 }
334
335 /* Helper function to output a single frame argument and value to an
336 output stream. This function will account for entry values if the
337 FV parameter is populated, the frame argument has entry values
338 associated with them, and the appropriate "set entry-value"
339 options are set. Will output in CLI or MI like format depending
340 on the type of output stream detected. OUT is the output stream,
341 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
342 it must have an accompanying value in the parameter FV. FA is a
343 frame argument structure. If FA is populated, both SYM_NAME and
344 FV are ignored. OPTS contains the value printing options,
345 ARGS_TYPE is an enumerator describing the argument format,
346 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
347 in MI output in commands where both arguments and locals are
348 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
349 converted to a Python exception, or EXT_LANG_BT_OK on success. */
350
351 static enum ext_lang_bt_status
352 py_print_single_arg (struct ui_out *out,
353 const char *sym_name,
354 struct frame_arg *fa,
355 struct value *fv,
356 const struct value_print_options *opts,
357 enum ext_lang_frame_args args_type,
358 int print_args_field,
359 const struct language_defn *language)
360 {
361 struct value *val;
362 enum ext_lang_bt_status retval = EXT_LANG_BT_OK;
363
364 if (fa != NULL)
365 {
366 if (fa->val == NULL && fa->error == NULL)
367 return EXT_LANG_BT_OK;
368 language = language_def (SYMBOL_LANGUAGE (fa->sym));
369 val = fa->val;
370 }
371 else
372 val = fv;
373
374 TRY
375 {
376 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
377
378 /* MI has varying rules for tuples, but generally if there is only
379 one element in each item in the list, do not start a tuple. The
380 exception is -stack-list-variables which emits an ARGS="1" field
381 if the value is a frame argument. This is denoted in this
382 function with PRINT_ARGS_FIELD which is flag from the caller to
383 emit the ARGS field. */
384 if (out->is_mi_like_p ())
385 {
386 if (print_args_field || args_type != NO_VALUES)
387 make_cleanup_ui_out_tuple_begin_end (out, NULL);
388 }
389
390 annotate_arg_begin ();
391
392 /* If frame argument is populated, check for entry-values and the
393 entry value options. */
394 if (fa != NULL)
395 {
396 struct ui_file *stb;
397
398 stb = mem_fileopen ();
399 make_cleanup_ui_file_delete (stb);
400 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
401 SYMBOL_LANGUAGE (fa->sym),
402 DMGL_PARAMS | DMGL_ANSI);
403 if (fa->entry_kind == print_entry_values_compact)
404 {
405 fputs_filtered ("=", stb);
406
407 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
408 SYMBOL_LANGUAGE (fa->sym),
409 DMGL_PARAMS | DMGL_ANSI);
410 }
411 if (fa->entry_kind == print_entry_values_only
412 || fa->entry_kind == print_entry_values_compact)
413 {
414 fputs_filtered ("@entry", stb);
415 }
416 out->field_stream ("name", stb);
417 }
418 else
419 /* Otherwise, just output the name. */
420 out->field_string ("name", sym_name);
421
422 annotate_arg_name_end ();
423
424 if (! out->is_mi_like_p ())
425 out->text ("=");
426
427 if (print_args_field)
428 out->field_int ("arg", 1);
429
430 /* For MI print the type, but only for simple values. This seems
431 weird, but this is how MI choose to format the various output
432 types. */
433 if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL)
434 {
435 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
436 {
437 retval = EXT_LANG_BT_ERROR;
438 do_cleanups (cleanups);
439 }
440 }
441
442 if (retval != EXT_LANG_BT_ERROR)
443 {
444 if (val != NULL)
445 annotate_arg_value (value_type (val));
446
447 /* If the output is to the CLI, and the user option "set print
448 frame-arguments" is set to none, just output "...". */
449 if (! out->is_mi_like_p () && args_type == NO_VALUES)
450 out->field_string ("value", "...");
451 else
452 {
453 /* Otherwise, print the value for both MI and the CLI, except
454 for the case of MI_PRINT_NO_VALUES. */
455 if (args_type != NO_VALUES)
456 {
457 if (val == NULL)
458 {
459 gdb_assert (fa != NULL && fa->error != NULL);
460 out->field_fmt ("value",
461 _("<error reading variable: %s>"),
462 fa->error);
463 }
464 else if (py_print_value (out, val, opts, 0, args_type, language)
465 == EXT_LANG_BT_ERROR)
466 retval = EXT_LANG_BT_ERROR;
467 }
468 }
469
470 do_cleanups (cleanups);
471 }
472 }
473 CATCH (except, RETURN_MASK_ERROR)
474 {
475 gdbpy_convert_exception (except);
476 }
477 END_CATCH
478
479 return retval;
480 }
481
482 /* Helper function to loop over frame arguments provided by the
483 "frame_arguments" Python API. Elements in the iterator must
484 conform to the "Symbol Value" interface. ITER is the Python
485 iterable object, OUT is the output stream, ARGS_TYPE is an
486 enumerator describing the argument format, PRINT_ARGS_FIELD is a
487 flag which indicates if we output "ARGS=1" in MI output in commands
488 where both arguments and locals are printed, and FRAME is the
489 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
490 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
491 success. */
492
493 static enum ext_lang_bt_status
494 enumerate_args (PyObject *iter,
495 struct ui_out *out,
496 enum ext_lang_frame_args args_type,
497 int print_args_field,
498 struct frame_info *frame)
499 {
500 struct value_print_options opts;
501
502 get_user_print_options (&opts);
503
504 if (args_type == CLI_SCALAR_VALUES)
505 {
506 /* True in "summary" mode, false otherwise. */
507 opts.summary = 1;
508 }
509
510 opts.deref_ref = 1;
511
512 TRY
513 {
514 annotate_frame_args ();
515 }
516 CATCH (except, RETURN_MASK_ALL)
517 {
518 gdbpy_convert_exception (except);
519 return EXT_LANG_BT_ERROR;
520 }
521 END_CATCH
522
523 /* Collect the first argument outside of the loop, so output of
524 commas in the argument output is correct. At the end of the
525 loop block collect another item from the iterator, and, if it is
526 not null emit a comma. */
527 gdbpy_ref item (PyIter_Next (iter));
528 if (item == NULL && PyErr_Occurred ())
529 return EXT_LANG_BT_ERROR;
530
531 while (item != NULL)
532 {
533 const struct language_defn *language;
534 gdb::unique_xmalloc_ptr<char> sym_name;
535 struct symbol *sym;
536 struct block *sym_block;
537 struct value *val;
538 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
539
540 success = extract_sym (item.get (), &sym_name, &sym, &sym_block,
541 &language);
542 if (success == EXT_LANG_BT_ERROR)
543 return EXT_LANG_BT_ERROR;
544
545 success = extract_value (item.get (), &val);
546 if (success == EXT_LANG_BT_ERROR)
547 return EXT_LANG_BT_ERROR;
548
549 if (sym && out->is_mi_like_p ()
550 && ! mi_should_print (sym, MI_PRINT_ARGS))
551 continue;
552
553 /* If the object did not provide a value, read it using
554 read_frame_args and account for entry values, if any. */
555 if (val == NULL)
556 {
557 struct frame_arg arg, entryarg;
558
559 /* If there is no value, and also no symbol, set error and
560 exit. */
561 if (sym == NULL)
562 {
563 PyErr_SetString (PyExc_RuntimeError,
564 _("No symbol or value provided."));
565 return EXT_LANG_BT_ERROR;
566 }
567
568 TRY
569 {
570 read_frame_arg (sym, frame, &arg, &entryarg);
571 }
572 CATCH (except, RETURN_MASK_ALL)
573 {
574 gdbpy_convert_exception (except);
575 return EXT_LANG_BT_ERROR;
576 }
577 END_CATCH
578
579 /* The object has not provided a value, so this is a frame
580 argument to be read by GDB. In this case we have to
581 account for entry-values. */
582
583 if (arg.entry_kind != print_entry_values_only)
584 {
585 if (py_print_single_arg (out, NULL, &arg,
586 NULL, &opts,
587 args_type,
588 print_args_field,
589 NULL) == EXT_LANG_BT_ERROR)
590 {
591 xfree (arg.error);
592 xfree (entryarg.error);
593 return EXT_LANG_BT_ERROR;
594 }
595 }
596
597 if (entryarg.entry_kind != print_entry_values_no)
598 {
599 if (arg.entry_kind != print_entry_values_only)
600 {
601 TRY
602 {
603 out->text (", ");
604 out->wrap_hint (" ");
605 }
606 CATCH (except, RETURN_MASK_ALL)
607 {
608 xfree (arg.error);
609 xfree (entryarg.error);
610 gdbpy_convert_exception (except);
611 return EXT_LANG_BT_ERROR;
612 }
613 END_CATCH
614 }
615
616 if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
617 args_type, print_args_field, NULL)
618 == EXT_LANG_BT_ERROR)
619 {
620 xfree (arg.error);
621 xfree (entryarg.error);
622 return EXT_LANG_BT_ERROR;
623 }
624 }
625
626 xfree (arg.error);
627 xfree (entryarg.error);
628 }
629 else
630 {
631 /* If the object has provided a value, we just print that. */
632 if (val != NULL)
633 {
634 if (py_print_single_arg (out, sym_name.get (), NULL, val, &opts,
635 args_type, print_args_field,
636 language) == EXT_LANG_BT_ERROR)
637 return EXT_LANG_BT_ERROR;
638 }
639 }
640
641 /* Collect the next item from the iterator. If
642 this is the last item, do not print the
643 comma. */
644 item.reset (PyIter_Next (iter));
645 if (item != NULL)
646 {
647 TRY
648 {
649 out->text (", ");
650 }
651 CATCH (except, RETURN_MASK_ALL)
652 {
653 gdbpy_convert_exception (except);
654 return EXT_LANG_BT_ERROR;
655 }
656 END_CATCH
657 }
658 else if (PyErr_Occurred ())
659 return EXT_LANG_BT_ERROR;
660
661 TRY
662 {
663 annotate_arg_end ();
664 }
665 CATCH (except, RETURN_MASK_ALL)
666 {
667 gdbpy_convert_exception (except);
668 return EXT_LANG_BT_ERROR;
669 }
670 END_CATCH
671 }
672
673 return EXT_LANG_BT_OK;
674 }
675
676
677 /* Helper function to loop over variables provided by the
678 "frame_locals" Python API. Elements in the iterable must conform
679 to the "Symbol Value" interface. ITER is the Python iterable
680 object, OUT is the output stream, INDENT is whether we should
681 indent the output (for CLI), ARGS_TYPE is an enumerator describing
682 the argument format, PRINT_ARGS_FIELD is flag which indicates
683 whether to output the ARGS field in the case of
684 -stack-list-variables and FRAME is the backing frame. Returns
685 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
686 exception, or EXT_LANG_BT_OK on success. */
687
688 static enum ext_lang_bt_status
689 enumerate_locals (PyObject *iter,
690 struct ui_out *out,
691 int indent,
692 enum ext_lang_frame_args args_type,
693 int print_args_field,
694 struct frame_info *frame)
695 {
696 struct value_print_options opts;
697
698 get_user_print_options (&opts);
699 opts.deref_ref = 1;
700
701 while (true)
702 {
703 const struct language_defn *language;
704 gdb::unique_xmalloc_ptr<char> sym_name;
705 struct value *val;
706 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
707 struct symbol *sym;
708 struct block *sym_block;
709 int local_indent = 8 + (8 * indent);
710 struct cleanup *locals_cleanups;
711
712 gdbpy_ref item (PyIter_Next (iter));
713 if (item == NULL)
714 break;
715
716 locals_cleanups = make_cleanup (null_cleanup, NULL);
717
718 success = extract_sym (item.get (), &sym_name, &sym, &sym_block,
719 &language);
720 if (success == EXT_LANG_BT_ERROR)
721 {
722 do_cleanups (locals_cleanups);
723 goto error;
724 }
725
726 success = extract_value (item.get (), &val);
727 if (success == EXT_LANG_BT_ERROR)
728 {
729 do_cleanups (locals_cleanups);
730 goto error;
731 }
732
733 if (sym != NULL && out->is_mi_like_p ()
734 && ! mi_should_print (sym, MI_PRINT_LOCALS))
735 {
736 do_cleanups (locals_cleanups);
737 continue;
738 }
739
740 /* If the object did not provide a value, read it. */
741 if (val == NULL)
742 {
743 TRY
744 {
745 val = read_var_value (sym, sym_block, frame);
746 }
747 CATCH (except, RETURN_MASK_ERROR)
748 {
749 gdbpy_convert_exception (except);
750 do_cleanups (locals_cleanups);
751 goto error;
752 }
753 END_CATCH
754 }
755
756 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
757 each output contains only one field. The exception is
758 -stack-list-variables, which always provides a tuple. */
759 if (out->is_mi_like_p ())
760 {
761 if (print_args_field || args_type != NO_VALUES)
762 make_cleanup_ui_out_tuple_begin_end (out, NULL);
763 }
764 TRY
765 {
766 if (! out->is_mi_like_p ())
767 {
768 /* If the output is not MI we indent locals. */
769 out->spaces (local_indent);
770 }
771
772 out->field_string ("name", sym_name.get ());
773
774 if (! out->is_mi_like_p ())
775 out->text (" = ");
776 }
777 CATCH (except, RETURN_MASK_ERROR)
778 {
779 gdbpy_convert_exception (except);
780 do_cleanups (locals_cleanups);
781 goto error;
782 }
783 END_CATCH
784
785 if (args_type == MI_PRINT_SIMPLE_VALUES)
786 {
787 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
788 {
789 do_cleanups (locals_cleanups);
790 goto error;
791 }
792 }
793
794 /* CLI always prints values for locals. MI uses the
795 simple/no/all system. */
796 if (! out->is_mi_like_p ())
797 {
798 int val_indent = (indent + 1) * 4;
799
800 if (py_print_value (out, val, &opts, val_indent, args_type,
801 language) == EXT_LANG_BT_ERROR)
802 {
803 do_cleanups (locals_cleanups);
804 goto error;
805 }
806 }
807 else
808 {
809 if (args_type != NO_VALUES)
810 {
811 if (py_print_value (out, val, &opts, 0, args_type,
812 language) == EXT_LANG_BT_ERROR)
813 {
814 do_cleanups (locals_cleanups);
815 goto error;
816 }
817 }
818 }
819
820 do_cleanups (locals_cleanups);
821
822 TRY
823 {
824 out->text ("\n");
825 }
826 CATCH (except, RETURN_MASK_ERROR)
827 {
828 gdbpy_convert_exception (except);
829 goto error;
830 }
831 END_CATCH
832 }
833
834 if (!PyErr_Occurred ())
835 return EXT_LANG_BT_OK;
836
837 error:
838 return EXT_LANG_BT_ERROR;
839 }
840
841 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
842 error, or EXT_LANG_BT_OK on success. */
843
844 static enum ext_lang_bt_status
845 py_mi_print_variables (PyObject *filter, struct ui_out *out,
846 struct value_print_options *opts,
847 enum ext_lang_frame_args args_type,
848 struct frame_info *frame)
849 {
850 struct cleanup *old_chain;
851
852 gdbpy_ref args_iter (get_py_iter_from_func (filter, "frame_args"));
853 if (args_iter == NULL)
854 return EXT_LANG_BT_ERROR;
855
856 gdbpy_ref locals_iter (get_py_iter_from_func (filter, "frame_locals"));
857 if (locals_iter == NULL)
858 return EXT_LANG_BT_ERROR;
859
860 old_chain = make_cleanup_ui_out_list_begin_end (out, "variables");
861
862 if (args_iter != Py_None)
863 if (enumerate_args (args_iter.get (), out, args_type, 1, frame)
864 == EXT_LANG_BT_ERROR)
865 goto error;
866
867 if (locals_iter != Py_None)
868 if (enumerate_locals (locals_iter.get (), out, 1, args_type, 1, frame)
869 == EXT_LANG_BT_ERROR)
870 goto error;
871
872 do_cleanups (old_chain);
873 return EXT_LANG_BT_OK;
874
875 error:
876 do_cleanups (old_chain);
877 return EXT_LANG_BT_ERROR;
878 }
879
880 /* Helper function for printing locals. This function largely just
881 creates the wrapping tuple, and calls enumerate_locals. Returns
882 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
883
884 static enum ext_lang_bt_status
885 py_print_locals (PyObject *filter,
886 struct ui_out *out,
887 enum ext_lang_frame_args args_type,
888 int indent,
889 struct frame_info *frame)
890 {
891 struct cleanup *old_chain;
892
893 gdbpy_ref locals_iter (get_py_iter_from_func (filter, "frame_locals"));
894 if (locals_iter == NULL)
895 return EXT_LANG_BT_ERROR;
896
897 old_chain = make_cleanup_ui_out_list_begin_end (out, "locals");
898
899 if (locals_iter != Py_None)
900 if (enumerate_locals (locals_iter.get (), out, indent, args_type,
901 0, frame) == EXT_LANG_BT_ERROR)
902 goto locals_error;
903
904 do_cleanups (old_chain);
905 return EXT_LANG_BT_OK;
906
907 locals_error:
908 do_cleanups (old_chain);
909 return EXT_LANG_BT_ERROR;
910 }
911
912 /* Helper function for printing frame arguments. This function
913 largely just creates the wrapping tuple, and calls enumerate_args.
914 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
915 a Python exception, or EXT_LANG_BT_OK on success. */
916
917 static enum ext_lang_bt_status
918 py_print_args (PyObject *filter,
919 struct ui_out *out,
920 enum ext_lang_frame_args args_type,
921 struct frame_info *frame)
922 {
923 struct cleanup *old_chain;
924
925 gdbpy_ref args_iter (get_py_iter_from_func (filter, "frame_args"));
926 if (args_iter == NULL)
927 return EXT_LANG_BT_ERROR;
928
929 old_chain = make_cleanup_ui_out_list_begin_end (out, "args");
930
931 TRY
932 {
933 annotate_frame_args ();
934 if (! out->is_mi_like_p ())
935 out->text (" (");
936 }
937 CATCH (except, RETURN_MASK_ALL)
938 {
939 gdbpy_convert_exception (except);
940 goto args_error;
941 }
942 END_CATCH
943
944 if (args_iter != Py_None)
945 if (enumerate_args (args_iter.get (), out, args_type, 0, frame)
946 == EXT_LANG_BT_ERROR)
947 goto args_error;
948
949 TRY
950 {
951 if (! out->is_mi_like_p ())
952 out->text (")");
953 }
954 CATCH (except, RETURN_MASK_ALL)
955 {
956 gdbpy_convert_exception (except);
957 goto args_error;
958 }
959 END_CATCH
960
961 do_cleanups (old_chain);
962 return EXT_LANG_BT_OK;
963
964 args_error:
965 do_cleanups (old_chain);
966 return EXT_LANG_BT_ERROR;
967 }
968
969 /* Print a single frame to the designated output stream, detecting
970 whether the output is MI or console, and formatting the output
971 according to the conventions of that protocol. FILTER is the
972 frame-filter associated with this frame. FLAGS is an integer
973 describing the various print options. The FLAGS variables is
974 described in "apply_frame_filter" function. ARGS_TYPE is an
975 enumerator describing the argument format. OUT is the output
976 stream to print, INDENT is the level of indention for this frame
977 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
978 containing all the frames level that have already been printed.
979 If a frame level has been printed, do not print it again (in the
980 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
981 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
982 on success. It can also throw an exception RETURN_QUIT. */
983
984 static enum ext_lang_bt_status
985 py_print_frame (PyObject *filter, int flags,
986 enum ext_lang_frame_args args_type,
987 struct ui_out *out, int indent, htab_t levels_printed)
988 {
989 int has_addr = 0;
990 CORE_ADDR address = 0;
991 struct gdbarch *gdbarch = NULL;
992 struct frame_info *frame = NULL;
993 struct cleanup *cleanup_stack;
994 struct value_print_options opts;
995 int print_level, print_frame_info, print_args, print_locals;
996 gdb::unique_xmalloc_ptr<char> function_to_free;
997
998 /* Extract print settings from FLAGS. */
999 print_level = (flags & PRINT_LEVEL) ? 1 : 0;
1000 print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0;
1001 print_args = (flags & PRINT_ARGS) ? 1 : 0;
1002 print_locals = (flags & PRINT_LOCALS) ? 1 : 0;
1003
1004 get_user_print_options (&opts);
1005
1006 /* Get the underlying frame. This is needed to determine GDB
1007 architecture, and also, in the cases of frame variables/arguments to
1008 read them if they returned filter object requires us to do so. */
1009 gdbpy_ref py_inf_frame (PyObject_CallMethod (filter, "inferior_frame", NULL));
1010 if (py_inf_frame == NULL)
1011 return EXT_LANG_BT_ERROR;
1012
1013 frame = frame_object_to_frame_info (py_inf_frame.get ());
1014 if (frame == NULL)
1015 return EXT_LANG_BT_ERROR;
1016
1017 TRY
1018 {
1019 gdbarch = get_frame_arch (frame);
1020 }
1021 CATCH (except, RETURN_MASK_ERROR)
1022 {
1023 gdbpy_convert_exception (except);
1024 return EXT_LANG_BT_ERROR;
1025 }
1026 END_CATCH
1027
1028 /* stack-list-variables. */
1029 if (print_locals && print_args && ! print_frame_info)
1030 {
1031 if (py_mi_print_variables (filter, out, &opts,
1032 args_type, frame) == EXT_LANG_BT_ERROR)
1033 return EXT_LANG_BT_ERROR;
1034 return EXT_LANG_BT_COMPLETED;
1035 }
1036
1037 cleanup_stack = make_cleanup (null_cleanup, NULL);
1038
1039 /* -stack-list-locals does not require a
1040 wrapping frame attribute. */
1041 if (print_frame_info || (print_args && ! print_locals))
1042 make_cleanup_ui_out_tuple_begin_end (out, "frame");
1043
1044 if (print_frame_info)
1045 {
1046 /* Elided frames are also printed with this function (recursively)
1047 and are printed with indention. */
1048 if (indent > 0)
1049 {
1050 TRY
1051 {
1052 out->spaces (indent * 4);
1053 }
1054 CATCH (except, RETURN_MASK_ERROR)
1055 {
1056 gdbpy_convert_exception (except);
1057 do_cleanups (cleanup_stack);
1058 return EXT_LANG_BT_ERROR;
1059 }
1060 END_CATCH
1061 }
1062
1063 /* The address is required for frame annotations, and also for
1064 address printing. */
1065 if (PyObject_HasAttrString (filter, "address"))
1066 {
1067 gdbpy_ref paddr (PyObject_CallMethod (filter, "address", NULL));
1068
1069 if (paddr == NULL)
1070 {
1071 do_cleanups (cleanup_stack);
1072 return EXT_LANG_BT_ERROR;
1073 }
1074
1075 if (paddr != Py_None)
1076 {
1077 if (get_addr_from_python (paddr.get (), &address) < 0)
1078 {
1079 do_cleanups (cleanup_stack);
1080 return EXT_LANG_BT_ERROR;
1081 }
1082
1083 has_addr = 1;
1084 }
1085 }
1086 }
1087
1088 /* Print frame level. MI does not require the level if
1089 locals/variables only are being printed. */
1090 if ((print_frame_info || print_args) && print_level)
1091 {
1092 struct frame_info **slot;
1093 int level;
1094
1095 slot = (struct frame_info **) htab_find_slot (levels_printed,
1096 frame, INSERT);
1097 TRY
1098 {
1099 level = frame_relative_level (frame);
1100
1101 /* Check if this frame has already been printed (there are cases
1102 where elided synthetic dummy-frames have to 'borrow' the frame
1103 architecture from the eliding frame. If that is the case, do
1104 not print 'level', but print spaces. */
1105 if (*slot == frame)
1106 out->field_skip ("level");
1107 else
1108 {
1109 *slot = frame;
1110 annotate_frame_begin (print_level ? level : 0,
1111 gdbarch, address);
1112 out->text ("#");
1113 out->field_fmt_int (2, ui_left, "level",
1114 level);
1115 }
1116 }
1117 CATCH (except, RETURN_MASK_ERROR)
1118 {
1119 gdbpy_convert_exception (except);
1120 do_cleanups (cleanup_stack);
1121 return EXT_LANG_BT_ERROR;
1122 }
1123 END_CATCH
1124 }
1125
1126 if (print_frame_info)
1127 {
1128 /* Print address to the address field. If an address is not provided,
1129 print nothing. */
1130 if (opts.addressprint && has_addr)
1131 {
1132 TRY
1133 {
1134 annotate_frame_address ();
1135 out->field_core_addr ("addr", gdbarch, address);
1136 annotate_frame_address_end ();
1137 out->text (" in ");
1138 }
1139 CATCH (except, RETURN_MASK_ERROR)
1140 {
1141 gdbpy_convert_exception (except);
1142 do_cleanups (cleanup_stack);
1143 return EXT_LANG_BT_ERROR;
1144 }
1145 END_CATCH
1146 }
1147
1148 /* Print frame function name. */
1149 if (PyObject_HasAttrString (filter, "function"))
1150 {
1151 gdbpy_ref py_func (PyObject_CallMethod (filter, "function", NULL));
1152 const char *function = NULL;
1153
1154 if (py_func == NULL)
1155 {
1156 do_cleanups (cleanup_stack);
1157 return EXT_LANG_BT_ERROR;
1158 }
1159
1160 if (gdbpy_is_string (py_func.get ()))
1161 {
1162 function_to_free = python_string_to_host_string (py_func.get ());
1163
1164 if (function_to_free == NULL)
1165 {
1166 do_cleanups (cleanup_stack);
1167 return EXT_LANG_BT_ERROR;
1168 }
1169
1170 function = function_to_free.get ();
1171 }
1172 else if (PyLong_Check (py_func.get ()))
1173 {
1174 CORE_ADDR addr;
1175 struct bound_minimal_symbol msymbol;
1176
1177 if (get_addr_from_python (py_func.get (), &addr) < 0)
1178 {
1179 do_cleanups (cleanup_stack);
1180 return EXT_LANG_BT_ERROR;
1181 }
1182
1183 msymbol = lookup_minimal_symbol_by_pc (addr);
1184 if (msymbol.minsym != NULL)
1185 function = MSYMBOL_PRINT_NAME (msymbol.minsym);
1186 }
1187 else if (py_func != Py_None)
1188 {
1189 PyErr_SetString (PyExc_RuntimeError,
1190 _("FrameDecorator.function: expecting a " \
1191 "String, integer or None."));
1192 do_cleanups (cleanup_stack);
1193 return EXT_LANG_BT_ERROR;
1194 }
1195
1196 TRY
1197 {
1198 annotate_frame_function_name ();
1199 if (function == NULL)
1200 out->field_skip ("func");
1201 else
1202 out->field_string ("func", function);
1203 }
1204 CATCH (except, RETURN_MASK_ERROR)
1205 {
1206 gdbpy_convert_exception (except);
1207 do_cleanups (cleanup_stack);
1208 return EXT_LANG_BT_ERROR;
1209 }
1210 END_CATCH
1211 }
1212 }
1213
1214
1215 /* Frame arguments. Check the result, and error if something went
1216 wrong. */
1217 if (print_args)
1218 {
1219 if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
1220 {
1221 do_cleanups (cleanup_stack);
1222 return EXT_LANG_BT_ERROR;
1223 }
1224 }
1225
1226 /* File name/source/line number information. */
1227 if (print_frame_info)
1228 {
1229 TRY
1230 {
1231 annotate_frame_source_begin ();
1232 }
1233 CATCH (except, RETURN_MASK_ERROR)
1234 {
1235 gdbpy_convert_exception (except);
1236 do_cleanups (cleanup_stack);
1237 return EXT_LANG_BT_ERROR;
1238 }
1239 END_CATCH
1240
1241 if (PyObject_HasAttrString (filter, "filename"))
1242 {
1243 gdbpy_ref py_fn (PyObject_CallMethod (filter, "filename", NULL));
1244
1245 if (py_fn == NULL)
1246 {
1247 do_cleanups (cleanup_stack);
1248 return EXT_LANG_BT_ERROR;
1249 }
1250
1251 if (py_fn != Py_None)
1252 {
1253 gdb::unique_xmalloc_ptr<char>
1254 filename (python_string_to_host_string (py_fn.get ()));
1255
1256 if (filename == NULL)
1257 {
1258 do_cleanups (cleanup_stack);
1259 return EXT_LANG_BT_ERROR;
1260 }
1261
1262 TRY
1263 {
1264 out->wrap_hint (" ");
1265 out->text (" at ");
1266 annotate_frame_source_file ();
1267 out->field_string ("file", filename.get ());
1268 annotate_frame_source_file_end ();
1269 }
1270 CATCH (except, RETURN_MASK_ERROR)
1271 {
1272 gdbpy_convert_exception (except);
1273 do_cleanups (cleanup_stack);
1274 return EXT_LANG_BT_ERROR;
1275 }
1276 END_CATCH
1277 }
1278 }
1279
1280 if (PyObject_HasAttrString (filter, "line"))
1281 {
1282 gdbpy_ref py_line (PyObject_CallMethod (filter, "line", NULL));
1283 int line;
1284
1285 if (py_line == NULL)
1286 {
1287 do_cleanups (cleanup_stack);
1288 return EXT_LANG_BT_ERROR;
1289 }
1290
1291 if (py_line != Py_None)
1292 {
1293 line = PyLong_AsLong (py_line.get ());
1294 if (PyErr_Occurred ())
1295 {
1296 do_cleanups (cleanup_stack);
1297 return EXT_LANG_BT_ERROR;
1298 }
1299
1300 TRY
1301 {
1302 out->text (":");
1303 annotate_frame_source_line ();
1304 out->field_int ("line", line);
1305 }
1306 CATCH (except, RETURN_MASK_ERROR)
1307 {
1308 gdbpy_convert_exception (except);
1309 do_cleanups (cleanup_stack);
1310 return EXT_LANG_BT_ERROR;
1311 }
1312 END_CATCH
1313 }
1314 }
1315 }
1316
1317 /* For MI we need to deal with the "children" list population of
1318 elided frames, so if MI output detected do not send newline. */
1319 if (! out->is_mi_like_p ())
1320 {
1321 TRY
1322 {
1323 annotate_frame_end ();
1324 out->text ("\n");
1325 }
1326 CATCH (except, RETURN_MASK_ERROR)
1327 {
1328 gdbpy_convert_exception (except);
1329 do_cleanups (cleanup_stack);
1330 return EXT_LANG_BT_ERROR;
1331 }
1332 END_CATCH
1333 }
1334
1335 if (print_locals)
1336 {
1337 if (py_print_locals (filter, out, args_type, indent,
1338 frame) == EXT_LANG_BT_ERROR)
1339 {
1340 do_cleanups (cleanup_stack);
1341 return EXT_LANG_BT_ERROR;
1342 }
1343 }
1344
1345 {
1346 /* Finally recursively print elided frames, if any. */
1347 gdbpy_ref elided (get_py_iter_from_func (filter, "elided"));
1348 if (elided == NULL)
1349 {
1350 do_cleanups (cleanup_stack);
1351 return EXT_LANG_BT_ERROR;
1352 }
1353
1354 if (elided != Py_None)
1355 {
1356 PyObject *item;
1357
1358 make_cleanup_ui_out_list_begin_end (out, "children");
1359
1360 if (! out->is_mi_like_p ())
1361 indent++;
1362
1363 while ((item = PyIter_Next (elided.get ())))
1364 {
1365 gdbpy_ref item_ref (item);
1366
1367 enum ext_lang_bt_status success = py_print_frame (item, flags,
1368 args_type, out,
1369 indent,
1370 levels_printed);
1371
1372 if (success == EXT_LANG_BT_ERROR)
1373 {
1374 do_cleanups (cleanup_stack);
1375 return EXT_LANG_BT_ERROR;
1376 }
1377 }
1378 if (item == NULL && PyErr_Occurred ())
1379 {
1380 do_cleanups (cleanup_stack);
1381 return EXT_LANG_BT_ERROR;
1382 }
1383 }
1384 }
1385
1386 do_cleanups (cleanup_stack);
1387 return EXT_LANG_BT_COMPLETED;
1388 }
1389
1390 /* Helper function to initiate frame filter invocation at starting
1391 frame FRAME. */
1392
1393 static PyObject *
1394 bootstrap_python_frame_filters (struct frame_info *frame,
1395 int frame_low, int frame_high)
1396 {
1397 gdbpy_ref frame_obj (frame_info_to_frame_object (frame));
1398 if (frame_obj == NULL)
1399 return NULL;
1400
1401 gdbpy_ref module (PyImport_ImportModule ("gdb.frames"));
1402 if (module == NULL)
1403 return NULL;
1404
1405 gdbpy_ref sort_func (PyObject_GetAttrString (module.get (),
1406 "execute_frame_filters"));
1407 if (sort_func == NULL)
1408 return NULL;
1409
1410 gdbpy_ref py_frame_low (PyInt_FromLong (frame_low));
1411 if (py_frame_low == NULL)
1412 return NULL;
1413
1414 gdbpy_ref py_frame_high (PyInt_FromLong (frame_high));
1415 if (py_frame_high == NULL)
1416 return NULL;
1417
1418 gdbpy_ref iterable (PyObject_CallFunctionObjArgs (sort_func.get (),
1419 frame_obj.get (),
1420 py_frame_low.get (),
1421 py_frame_high.get (),
1422 NULL));
1423 if (iterable == NULL)
1424 return NULL;
1425
1426 if (iterable != Py_None)
1427 return PyObject_GetIter (iterable.get ());
1428 else
1429 return iterable.release ();
1430 }
1431
1432 /* This is the only publicly exported function in this file. FRAME
1433 is the source frame to start frame-filter invocation. FLAGS is an
1434 integer holding the flags for printing. The following elements of
1435 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1436 PRINT_LEVEL is a flag indicating whether to print the frame's
1437 relative level in the output. PRINT_FRAME_INFO is a flag that
1438 indicates whether this function should print the frame
1439 information, PRINT_ARGS is a flag that indicates whether to print
1440 frame arguments, and PRINT_LOCALS, likewise, with frame local
1441 variables. ARGS_TYPE is an enumerator describing the argument
1442 format, OUT is the output stream to print. FRAME_LOW is the
1443 beginning of the slice of frames to print, and FRAME_HIGH is the
1444 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1445 or EXT_LANG_BT_COMPLETED on success. */
1446
1447 enum ext_lang_bt_status
1448 gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
1449 struct frame_info *frame, int flags,
1450 enum ext_lang_frame_args args_type,
1451 struct ui_out *out, int frame_low, int frame_high)
1452 {
1453 struct gdbarch *gdbarch = NULL;
1454 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
1455
1456 if (!gdb_python_initialized)
1457 return EXT_LANG_BT_NO_FILTERS;
1458
1459 TRY
1460 {
1461 gdbarch = get_frame_arch (frame);
1462 }
1463 CATCH (except, RETURN_MASK_ALL)
1464 {
1465 /* Let gdb try to print the stack trace. */
1466 return EXT_LANG_BT_NO_FILTERS;
1467 }
1468 END_CATCH
1469
1470 gdbpy_enter enter_py (gdbarch, current_language);
1471
1472 gdbpy_ref iterable (bootstrap_python_frame_filters (frame, frame_low,
1473 frame_high));
1474
1475 if (iterable == NULL)
1476 {
1477 /* Normally if there is an error GDB prints the exception,
1478 abandons the backtrace and exits. The user can then call "bt
1479 no-filters", and get a default backtrace (it would be
1480 confusing to automatically start a standard backtrace halfway
1481 through a Python filtered backtrace). However in the case
1482 where GDB cannot initialize the frame filters (most likely
1483 due to incorrect auto-load paths), GDB has printed nothing.
1484 In this case it is OK to print the default backtrace after
1485 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1486 here to signify there are no filters after printing the
1487 initialization error. This return code will trigger a
1488 default backtrace. */
1489
1490 gdbpy_print_stack ();
1491 return EXT_LANG_BT_NO_FILTERS;
1492 }
1493
1494 /* If iterable is None, then there are no frame filters registered.
1495 If this is the case, defer to default GDB printing routines in MI
1496 and CLI. */
1497 if (iterable == Py_None)
1498 return EXT_LANG_BT_NO_FILTERS;
1499
1500 htab_up levels_printed (htab_create (20,
1501 htab_hash_pointer,
1502 htab_eq_pointer,
1503 NULL));
1504
1505 while (true)
1506 {
1507 gdbpy_ref item (PyIter_Next (iterable.get ()));
1508
1509 if (item == NULL)
1510 {
1511 if (PyErr_Occurred ())
1512 {
1513 gdbpy_print_stack ();
1514 return EXT_LANG_BT_ERROR;
1515 }
1516 break;
1517 }
1518
1519 success = py_print_frame (item.get (), flags, args_type, out, 0,
1520 levels_printed.get ());
1521
1522 /* Do not exit on error printing a single frame. Print the
1523 error and continue with other frames. */
1524 if (success == EXT_LANG_BT_ERROR)
1525 gdbpy_print_stack ();
1526 }
1527
1528 return success;
1529 }