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