gdb/python/py-framefilter.c - gdb

Data types defined

Functions defined

Source code

  1. /* Python frame filters

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "objfiles.h"
  16. #include "symtab.h"
  17. #include "language.h"
  18. #include "arch-utils.h"
  19. #include "python.h"
  20. #include "ui-out.h"
  21. #include "valprint.h"
  22. #include "annotate.h"
  23. #include "hashtab.h"
  24. #include "demangle.h"
  25. #include "mi/mi-cmds.h"
  26. #include "python-internal.h"

  27. enum mi_print_types
  28. {
  29.   MI_PRINT_ARGS,
  30.   MI_PRINT_LOCALS
  31. };

  32. /* Helper  function  to  extract  a  symbol, a  name  and  a  language
  33.    definition from a Python object that conforms to the "Symbol Value"
  34.    interface.  OBJ  is the Python  object to extract the  values from.
  35.    NAME is a  pass-through argument where the name of  the symbol will
  36.    be written.  NAME is allocated in  this function, but the caller is
  37.    responsible for clean up.  SYM is a pass-through argument where the
  38.    symbol will be written.  In the case of the API returning a string,
  39.    this will be set to NULL.  LANGUAGE is also a pass-through argument
  40.    denoting the language attributed to the Symbol.  In the case of SYM
  41.    being  NULL, this  will be  set to  the current  language.  Returns
  42.    EXT_LANG_BT_ERROR on error with the appropriate Python exception set, and
  43.    EXT_LANG_BT_OK on success.  */

  44. static enum ext_lang_bt_status
  45. extract_sym (PyObject *obj, char **name, struct symbol **sym,
  46.              const struct language_defn **language)
  47. {
  48.   PyObject *result = PyObject_CallMethod (obj, "symbol", NULL);

  49.   if (result == NULL)
  50.     return EXT_LANG_BT_ERROR;

  51.   /* For 'symbol' callback, the function can return a symbol or a
  52.      string.  */
  53.   if (gdbpy_is_string (result))
  54.     {
  55.       *name = python_string_to_host_string (result);
  56.       Py_DECREF (result);

  57.       if (*name == NULL)
  58.         return EXT_LANG_BT_ERROR;
  59.       /* If the API returns a string (and not a symbol), then there is
  60.         no symbol derived language available and the frame filter has
  61.         either overridden the symbol with a string, or supplied a
  62.         entirely synthetic symbol/value pairing.  In that case, use
  63.         python_language.  */
  64.       *language = python_language;
  65.       *sym = NULL;
  66.     }
  67.   else
  68.     {
  69.       /* This type checks 'result' during the conversion so we
  70.          just call it unconditionally and check the return.  */
  71.       *sym = symbol_object_to_symbol (result);

  72.       Py_DECREF (result);

  73.       if (*sym == NULL)
  74.         {
  75.           PyErr_SetString (PyExc_RuntimeError,
  76.                            _("Unexpected value.  Expecting a "
  77.                              "gdb.Symbol or a Python string."));
  78.           return EXT_LANG_BT_ERROR;
  79.         }

  80.       /* Duplicate the symbol name, so the caller has consistency
  81.          in garbage collection.  */
  82.       *name = xstrdup (SYMBOL_PRINT_NAME (*sym));

  83.       /* If a symbol is specified attempt to determine the language
  84.          from the symbol.  If mode is not "auto", then the language
  85.          has been explicitly set, use that.  */
  86.       if (language_mode == language_mode_auto)
  87.         *language = language_def (SYMBOL_LANGUAGE (*sym));
  88.       else
  89.         *language = current_language;
  90.     }

  91.   return EXT_LANG_BT_OK;
  92. }

  93. /* Helper function to extract a value from an object that conforms to
  94.    the "Symbol Value" interface.  OBJ is the Python object to extract
  95.    the value from.  VALUE is a pass-through argument where the value
  96.    will be written.  If the object does not have the value attribute,
  97.    or provides the Python None for a value, VALUE will be set to NULL
  98.    and this function will return as successful.  Returns EXT_LANG_BT_ERROR
  99.    on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
  100.    success.  */

  101. static enum ext_lang_bt_status
  102. extract_value (PyObject *obj, struct value **value)
  103. {
  104.   if (PyObject_HasAttrString (obj, "value"))
  105.     {
  106.       PyObject *vresult = PyObject_CallMethod (obj, "value", NULL);

  107.       if (vresult == NULL)
  108.         return EXT_LANG_BT_ERROR;

  109.       /* The Python code has returned 'None' for a value, so we set
  110.          value to NULL.  This flags that GDB should read the
  111.          value.  */
  112.       if (vresult == Py_None)
  113.         {
  114.           Py_DECREF (vresult);
  115.           *value = NULL;
  116.           return EXT_LANG_BT_OK;
  117.         }
  118.       else
  119.         {
  120.           *value = convert_value_from_python (vresult);
  121.           Py_DECREF (vresult);

  122.           if (*value == NULL)
  123.             return EXT_LANG_BT_ERROR;

  124.           return EXT_LANG_BT_OK;
  125.         }
  126.     }
  127.   else
  128.     *value = NULL;

  129.   return EXT_LANG_BT_OK;
  130. }

  131. /* MI prints only certain values according to the type of symbol and
  132.    also what the user has specified.  SYM is the symbol to check, and
  133.    MI_PRINT_TYPES is an enum specifying what the user wants emitted
  134.    for the MI command in question.  */
  135. static int
  136. mi_should_print (struct symbol *sym, enum mi_print_types type)
  137. {
  138.   int print_me = 0;

  139.   switch (SYMBOL_CLASS (sym))
  140.     {
  141.     default:
  142.     case LOC_UNDEF:        /* catches errors        */
  143.     case LOC_CONST:        /* constant              */
  144.     case LOC_TYPEDEF:        /* local typedef         */
  145.     case LOC_LABEL:        /* local label           */
  146.     case LOC_BLOCK:        /* local function        */
  147.     case LOC_CONST_BYTES:        /* loc. byte seq.        */
  148.     case LOC_UNRESOLVED:        /* unresolved static     */
  149.     case LOC_OPTIMIZED_OUT:        /* optimized out         */
  150.       print_me = 0;
  151.       break;

  152.     case LOC_ARG:        /* argument              */
  153.     case LOC_REF_ARG:        /* reference arg         */
  154.     case LOC_REGPARM_ADDR:        /* indirect register arg */
  155.     case LOC_LOCAL:        /* stack local           */
  156.     case LOC_STATIC:        /* static                */
  157.     case LOC_REGISTER:        /* register              */
  158.     case LOC_COMPUTED:        /* computed location     */
  159.       if (type == MI_PRINT_LOCALS)
  160.         print_me = ! SYMBOL_IS_ARGUMENT (sym);
  161.       else
  162.         print_me = SYMBOL_IS_ARGUMENT (sym);
  163.     }
  164.   return print_me;
  165. }

  166. /* Helper function which outputs a type name extracted from VAL to a
  167.    "type" field in the output stream OUT.  OUT is the ui-out structure
  168.    the type name will be output too, and VAL is the value that the
  169.    type will be extracted from.  Returns EXT_LANG_BT_ERROR on error, with
  170.    any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
  171.    success.  */

  172. static enum ext_lang_bt_status
  173. py_print_type (struct ui_out *out, struct value *val)
  174. {
  175.   volatile struct gdb_exception except;

  176.   TRY_CATCH (except, RETURN_MASK_ALL)
  177.     {
  178.       struct type *type;
  179.       struct ui_file *stb;
  180.       struct cleanup *cleanup;

  181.       stb = mem_fileopen ();
  182.       cleanup = make_cleanup_ui_file_delete (stb);
  183.       type = check_typedef (value_type (val));
  184.       type_print (value_type (val), "", stb, -1);
  185.       ui_out_field_stream (out, "type", stb);
  186.       do_cleanups (cleanup);
  187.     }
  188.   if (except.reason < 0)
  189.     {
  190.       gdbpy_convert_exception (except);
  191.       return EXT_LANG_BT_ERROR;
  192.     }

  193.   return EXT_LANG_BT_OK;
  194. }

  195. /* Helper function which outputs a value to an output field in a
  196.    stream.  OUT is the ui-out structure the value will be output to,
  197.    VAL is the value that will be printed, OPTS contains the value
  198.    printing options, ARGS_TYPE is an enumerator describing the
  199.    argument format, and LANGUAGE is the language_defn that the value
  200.    will be printed with.  Returns EXT_LANG_BT_ERROR on error, with any GDB
  201.    exceptions converted to a Python exception, or EXT_LANG_BT_OK on
  202.    success. */

  203. static enum ext_lang_bt_status
  204. py_print_value (struct ui_out *out, struct value *val,
  205.                 const struct value_print_options *opts,
  206.                 int indent,
  207.                 enum ext_lang_frame_args args_type,
  208.                 const struct language_defn *language)
  209. {
  210.   int should_print = 0;
  211.   volatile struct gdb_exception except;
  212.   int local_indent = (4 * indent);

  213.   /* Never set an indent level for common_val_print if MI.  */
  214.   if (ui_out_is_mi_like_p (out))
  215.     local_indent = 0;

  216.   /* MI does not print certain values, differentiated by type,
  217.      depending on what ARGS_TYPE indicates.  Test type against option.
  218.      For CLI print all values.  */
  219.   if (args_type == MI_PRINT_SIMPLE_VALUES
  220.       || args_type == MI_PRINT_ALL_VALUES)
  221.     {
  222.       struct type *type = NULL;

  223.       TRY_CATCH (except, RETURN_MASK_ALL)
  224.         {
  225.           type = check_typedef (value_type (val));
  226.         }
  227.       if (except.reason < 0)
  228.         {
  229.           gdbpy_convert_exception (except);
  230.           return EXT_LANG_BT_ERROR;
  231.         }

  232.       if (args_type == MI_PRINT_ALL_VALUES)
  233.         should_print = 1;
  234.       else if (args_type == MI_PRINT_SIMPLE_VALUES
  235.                && TYPE_CODE (type) != TYPE_CODE_ARRAY
  236.                && TYPE_CODE (type) != TYPE_CODE_STRUCT
  237.                && TYPE_CODE (type) != TYPE_CODE_UNION)
  238.         should_print = 1;
  239.     }
  240.   else if (args_type != NO_VALUES)
  241.     should_print = 1;

  242.   if (should_print)
  243.     {
  244.       TRY_CATCH (except, RETURN_MASK_ALL)
  245.         {
  246.           struct ui_file *stb;
  247.           struct cleanup *cleanup;

  248.           stb = mem_fileopen ();
  249.           cleanup = make_cleanup_ui_file_delete (stb);
  250.           common_val_print (val, stb, indent, opts, language);
  251.           ui_out_field_stream (out, "value", stb);
  252.           do_cleanups (cleanup);
  253.         }
  254.       if (except.reason < 0)
  255.         {
  256.           gdbpy_convert_exception (except);
  257.           return EXT_LANG_BT_ERROR;
  258.         }
  259.     }

  260.   return EXT_LANG_BT_OK;
  261. }

  262. /* Helper function to call a Python method and extract an iterator
  263.    from the result.  If the function returns anything but an iterator
  264.    the exception is preserved and NULL is returned.  FILTER is the
  265.    Python object to call, and FUNC is the name of the method.  Returns
  266.    a PyObject, or NULL on error with the appropriate exception set.
  267.    This function can return an iterator, or NULL.  */

  268. static PyObject *
  269. get_py_iter_from_func (PyObject *filter, char *func)
  270. {
  271.   if (PyObject_HasAttrString (filter, func))
  272.     {
  273.       PyObject *result = PyObject_CallMethod (filter, func, NULL);

  274.       if (result != NULL)
  275.         {
  276.           if (result == Py_None)
  277.             {
  278.               return result;
  279.             }
  280.           else
  281.             {
  282.               PyObject *iterator = PyObject_GetIter (result);

  283.               Py_DECREF (result);
  284.               return iterator;
  285.             }
  286.         }
  287.     }
  288.   else
  289.     Py_RETURN_NONE;

  290.   return NULL;
  291. }

  292. /*  Helper function to output a single frame argument and value to an
  293.     output stream.  This function will account for entry values if the
  294.     FV parameter is populated, the frame argument has entry values
  295.     associated with them, and the appropriate "set entry-value"
  296.     options are set.  Will output in CLI or MI like format depending
  297.     on the type of output stream detected.  OUT is the output stream,
  298.     SYM_NAME is the name of the symbol.  If SYM_NAME is populated then
  299.     it must have an accompanying value in the parameter FV.  FA is a
  300.     frame argument structure.  If FA is populated, both SYM_NAME and
  301.     FV are ignored.  OPTS contains the value printing options,
  302.     ARGS_TYPE is an enumerator describing the argument format,
  303.     PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
  304.     in MI output in commands where both arguments and locals are
  305.     printed.  Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
  306.     converted to a Python exception, or EXT_LANG_BT_OK on success.  */

  307. static enum ext_lang_bt_status
  308. py_print_single_arg (struct ui_out *out,
  309.                      const char *sym_name,
  310.                      struct frame_arg *fa,
  311.                      struct value *fv,
  312.                      const struct value_print_options *opts,
  313.                      enum ext_lang_frame_args args_type,
  314.                      int print_args_field,
  315.                      const struct language_defn *language)
  316. {
  317.   struct value *val;
  318.   volatile struct gdb_exception except;
  319.   enum ext_lang_bt_status retval = EXT_LANG_BT_OK;

  320.   if (fa != NULL)
  321.     {
  322.       if (fa->val == NULL && fa->error == NULL)
  323.         return EXT_LANG_BT_OK;
  324.       language = language_def (SYMBOL_LANGUAGE (fa->sym));
  325.       val = fa->val;
  326.     }
  327.   else
  328.     val = fv;

  329.   TRY_CATCH (except, RETURN_MASK_ALL)
  330.     {
  331.       struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);

  332.       /*  MI has varying rules for tuples, but generally if there is only
  333.       one element in each item in the list, do not start a tuple.  The
  334.       exception is -stack-list-variables which emits an ARGS="1" field
  335.       if the value is a frame argument.  This is denoted in this
  336.       function with PRINT_ARGS_FIELD which is flag from the caller to
  337.       emit the ARGS field.  */
  338.       if (ui_out_is_mi_like_p (out))
  339.         {
  340.           if (print_args_field || args_type != NO_VALUES)
  341.             make_cleanup_ui_out_tuple_begin_end (out, NULL);
  342.         }

  343.       annotate_arg_begin ();

  344.       /* If frame argument is populated, check for entry-values and the
  345.          entry value options.  */
  346.       if (fa != NULL)
  347.         {
  348.           struct ui_file *stb;

  349.           stb = mem_fileopen ();
  350.           make_cleanup_ui_file_delete (stb);
  351.           fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
  352.                                    SYMBOL_LANGUAGE (fa->sym),
  353.                                    DMGL_PARAMS | DMGL_ANSI);
  354.           if (fa->entry_kind == print_entry_values_compact)
  355.             {
  356.               fputs_filtered ("=", stb);

  357.               fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
  358.                                        SYMBOL_LANGUAGE (fa->sym),
  359.                                        DMGL_PARAMS | DMGL_ANSI);
  360.             }
  361.           if (fa->entry_kind == print_entry_values_only
  362.               || fa->entry_kind == print_entry_values_compact)
  363.             {
  364.               fputs_filtered ("@entry", stb);
  365.             }
  366.           ui_out_field_stream (out, "name", stb);
  367.         }
  368.       else
  369.         /* Otherwise, just output the name.  */
  370.         ui_out_field_string (out, "name", sym_name);

  371.       annotate_arg_name_end ();

  372.       if (! ui_out_is_mi_like_p (out))
  373.         ui_out_text (out, "=");

  374.       if (print_args_field)
  375.         ui_out_field_int (out, "arg", 1);

  376.       /* For MI print the type, but only for simple values.  This seems
  377.          weird, but this is how MI choose to format the various output
  378.          types.  */
  379.       if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL)
  380.         {
  381.           if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
  382.             {
  383.               retval = EXT_LANG_BT_ERROR;
  384.               do_cleanups (cleanups);
  385.               continue;
  386.             }
  387.         }

  388.       if (val != NULL)
  389.         annotate_arg_value (value_type (val));

  390.       /* If the output is to the CLI, and the user option "set print
  391.          frame-arguments" is set to none, just output "...".  */
  392.       if (! ui_out_is_mi_like_p (out) && args_type == NO_VALUES)
  393.         ui_out_field_string (out, "value", "...");
  394.       else
  395.         {
  396.           /* Otherwise, print the value for both MI and the CLI, except
  397.              for the case of MI_PRINT_NO_VALUES.  */
  398.           if (args_type != NO_VALUES)
  399.             {
  400.               if (val == NULL)
  401.                 {
  402.                   gdb_assert (fa != NULL && fa->error != NULL);
  403.                   ui_out_field_fmt (out, "value",
  404.                                     _("<error reading variable: %s>"),
  405.                                     fa->error);
  406.                 }
  407.               else if (py_print_value (out, val, opts, 0, args_type, language)
  408.                        == EXT_LANG_BT_ERROR)
  409.                 retval = EXT_LANG_BT_ERROR;
  410.             }
  411.         }

  412.       do_cleanups (cleanups);
  413.     }
  414.   if (except.reason < 0)
  415.     gdbpy_convert_exception (except);

  416.   return retval;
  417. }

  418. /* Helper function to loop over frame arguments provided by the
  419.    "frame_arguments" Python API.  Elements in the iterator must
  420.    conform to the "Symbol Value" interface.  ITER is the Python
  421.    iterable object, OUT is the output stream, ARGS_TYPE is an
  422.    enumerator describing the argument format, PRINT_ARGS_FIELD is a
  423.    flag which indicates if we output "ARGS=1" in MI output in commands
  424.    where both arguments and locals are printed, and FRAME is the
  425.    backing frame.  Returns EXT_LANG_BT_ERROR on error, with any GDB
  426.    exceptions converted to a Python exception, or EXT_LANG_BT_OK on
  427.    success.  */

  428. static enum ext_lang_bt_status
  429. enumerate_args (PyObject *iter,
  430.                 struct ui_out *out,
  431.                 enum ext_lang_frame_args args_type,
  432.                 int print_args_field,
  433.                 struct frame_info *frame)
  434. {
  435.   PyObject *item;
  436.   struct value_print_options opts;
  437.   volatile struct gdb_exception except;

  438.   get_user_print_options (&opts);

  439.   if (args_type == CLI_SCALAR_VALUES)
  440.     {
  441.       /* True in "summary" mode, false otherwise.  */
  442.       opts.summary = 1;
  443.     }

  444.   opts.deref_ref = 1;

  445.   TRY_CATCH (except, RETURN_MASK_ALL)
  446.     {
  447.       annotate_frame_args ();
  448.     }
  449.   if (except.reason < 0)
  450.     {
  451.       gdbpy_convert_exception (except);
  452.       goto error;
  453.     }

  454.   /*  Collect the first argument outside of the loop, so output of
  455.       commas in the argument output is correct.  At the end of the
  456.       loop block collect another item from the iterator, and, if it is
  457.       not null emit a comma.  */
  458.   item = PyIter_Next (iter);
  459.   if (item == NULL && PyErr_Occurred ())
  460.     goto error;

  461.   while (item)
  462.     {
  463.       const struct language_defn *language;
  464.       char *sym_name;
  465.       struct symbol *sym;
  466.       struct value *val;
  467.       enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;

  468.       success = extract_sym (item, &sym_name, &sym, &language);
  469.       if (success == EXT_LANG_BT_ERROR)
  470.         {
  471.           Py_DECREF (item);
  472.           goto error;
  473.         }

  474.       success = extract_value (item, &val);
  475.       if (success == EXT_LANG_BT_ERROR)
  476.         {
  477.           xfree (sym_name);
  478.           Py_DECREF (item);
  479.           goto error;
  480.         }

  481.       Py_DECREF (item);
  482.       item = NULL;

  483.       if (sym && ui_out_is_mi_like_p (out)
  484.           && ! mi_should_print (sym, MI_PRINT_ARGS))
  485.         {
  486.           xfree (sym_name);
  487.           continue;
  488.         }

  489.       /* If the object did not provide a value, read it using
  490.          read_frame_args and account for entry values, if any.  */
  491.       if (val == NULL)
  492.         {
  493.           struct frame_arg arg, entryarg;

  494.           /* If there is no value, and also no symbol, set error and
  495.              exit.  */
  496.           if (sym == NULL)
  497.             {
  498.               PyErr_SetString (PyExc_RuntimeError,
  499.                                _("No symbol or value provided."));
  500.               xfree (sym_name);
  501.               goto error;
  502.             }

  503.           TRY_CATCH (except, RETURN_MASK_ALL)
  504.             {
  505.               read_frame_arg (sym, frame, &arg, &entryarg);
  506.             }
  507.           if (except.reason < 0)
  508.             {
  509.               xfree (sym_name);
  510.               gdbpy_convert_exception (except);
  511.               goto error;
  512.             }

  513.           /* The object has not provided a value, so this is a frame
  514.              argument to be read by GDB.  In this case we have to
  515.              account for entry-values.  */

  516.           if (arg.entry_kind != print_entry_values_only)
  517.             {
  518.               if (py_print_single_arg (out, NULL, &arg,
  519.                                        NULL, &opts,
  520.                                        args_type,
  521.                                        print_args_field,
  522.                                        NULL) == EXT_LANG_BT_ERROR)
  523.                 {
  524.                   xfree (arg.error);
  525.                   xfree (entryarg.error);
  526.                   xfree (sym_name);
  527.                   goto error;
  528.                 }
  529.             }

  530.           if (entryarg.entry_kind != print_entry_values_no)
  531.             {
  532.               if (arg.entry_kind != print_entry_values_only)
  533.                 {
  534.                   TRY_CATCH (except, RETURN_MASK_ALL)
  535.                     {
  536.                       ui_out_text (out, ", ");
  537.                       ui_out_wrap_hint (out, "    ");
  538.                     }
  539.                   if (except.reason < 0)
  540.                     {
  541.                       xfree (arg.error);
  542.                       xfree (entryarg.error);
  543.                       xfree (sym_name);
  544.                       gdbpy_convert_exception (except);
  545.                       goto error;
  546.                     }
  547.                 }

  548.               if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
  549.                                        args_type, print_args_field, NULL)
  550.                   == EXT_LANG_BT_ERROR)
  551.                 {
  552.                       xfree (arg.error);
  553.                       xfree (entryarg.error);
  554.                       xfree (sym_name);
  555.                       goto error;
  556.                 }
  557.             }

  558.           xfree (arg.error);
  559.           xfree (entryarg.error);
  560.         }
  561.       else
  562.         {
  563.           /* If the object has provided a value, we just print that.  */
  564.           if (val != NULL)
  565.             {
  566.               if (py_print_single_arg (out, sym_name, NULL, val, &opts,
  567.                                        args_type, print_args_field,
  568.                                        language) == EXT_LANG_BT_ERROR)
  569.                 {
  570.                   xfree (sym_name);
  571.                   goto error;
  572.                 }
  573.             }
  574.         }

  575.       xfree (sym_name);

  576.       /* Collect the next item from the iterator.  If
  577.          this is the last item, do not print the
  578.          comma.  */
  579.       item = PyIter_Next (iter);
  580.       if (item != NULL)
  581.         {
  582.           TRY_CATCH (except, RETURN_MASK_ALL)
  583.             {
  584.               ui_out_text (out, ", ");
  585.             }
  586.           if (except.reason < 0)
  587.             {
  588.               Py_DECREF (item);
  589.               gdbpy_convert_exception (except);
  590.               goto error;
  591.             }
  592.         }
  593.       else if (PyErr_Occurred ())
  594.         goto error;

  595.       TRY_CATCH (except, RETURN_MASK_ALL)
  596.         {
  597.           annotate_arg_end ();
  598.         }
  599.       if (except.reason < 0)
  600.         {
  601.           Py_DECREF (item);
  602.           gdbpy_convert_exception (except);
  603.           goto error;
  604.         }
  605.     }

  606.   return EXT_LANG_BT_OK;

  607. error:
  608.   return EXT_LANG_BT_ERROR;
  609. }


  610. /* Helper function to loop over variables provided by the
  611.    "frame_locals" Python API.  Elements in the iterable must conform
  612.    to the "Symbol Value" interface.  ITER is the Python iterable
  613.    object, OUT is the output stream, INDENT is whether we should
  614.    indent the output (for CLI), ARGS_TYPE is an enumerator describing
  615.    the argument format, PRINT_ARGS_FIELD is flag which indicates
  616.    whether to output the ARGS field in the case of
  617.    -stack-list-variables and FRAME is the backing frame.  Returns
  618.    EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
  619.    exception, or EXT_LANG_BT_OK on success.  */

  620. static enum ext_lang_bt_status
  621. enumerate_locals (PyObject *iter,
  622.                   struct ui_out *out,
  623.                   int indent,
  624.                   enum ext_lang_frame_args args_type,
  625.                   int print_args_field,
  626.                   struct frame_info *frame)
  627. {
  628.   PyObject *item;
  629.   struct value_print_options opts;

  630.   get_user_print_options (&opts);
  631.   opts.deref_ref = 1;

  632.   while ((item = PyIter_Next (iter)))
  633.     {
  634.       const struct language_defn *language;
  635.       char *sym_name;
  636.       struct value *val;
  637.       enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
  638.       struct symbol *sym;
  639.       volatile struct gdb_exception except;
  640.       int local_indent = 8 + (8 * indent);
  641.       struct cleanup *locals_cleanups;

  642.       locals_cleanups = make_cleanup_py_decref (item);

  643.       success = extract_sym (item, &sym_name, &sym, &language);
  644.       if (success == EXT_LANG_BT_ERROR)
  645.         {
  646.           do_cleanups (locals_cleanups);
  647.           goto error;
  648.         }

  649.       make_cleanup (xfree, sym_name);

  650.       success = extract_value (item, &val);
  651.       if (success == EXT_LANG_BT_ERROR)
  652.         {
  653.           do_cleanups (locals_cleanups);
  654.           goto error;
  655.         }

  656.       if (sym != NULL && ui_out_is_mi_like_p (out)
  657.           && ! mi_should_print (sym, MI_PRINT_LOCALS))
  658.         {
  659.           do_cleanups (locals_cleanups);
  660.           continue;
  661.         }

  662.       /* If the object did not provide a value, read it.  */
  663.       if (val == NULL)
  664.         {
  665.           TRY_CATCH (except, RETURN_MASK_ALL)
  666.             {
  667.               val = read_var_value (sym, frame);
  668.             }
  669.           if (except.reason < 0)
  670.             {
  671.               gdbpy_convert_exception (except);
  672.               do_cleanups (locals_cleanups);
  673.               goto error;
  674.             }
  675.         }

  676.       /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
  677.          each output contains only one field.  The exception is
  678.          -stack-list-variables, which always provides a tuple.  */
  679.       if (ui_out_is_mi_like_p (out))
  680.         {
  681.           if (print_args_field || args_type != NO_VALUES)
  682.             make_cleanup_ui_out_tuple_begin_end (out, NULL);
  683.         }
  684.       TRY_CATCH (except, RETURN_MASK_ALL)
  685.         {
  686.           if (! ui_out_is_mi_like_p (out))
  687.             {
  688.               /* If the output is not MI we indent locals.  */
  689.               ui_out_spaces (out, local_indent);
  690.             }

  691.           ui_out_field_string (out, "name", sym_name);

  692.           if (! ui_out_is_mi_like_p (out))
  693.             ui_out_text (out, " = ");
  694.         }
  695.       if (except.reason < 0)
  696.         {
  697.           gdbpy_convert_exception (except);
  698.           do_cleanups (locals_cleanups);
  699.           goto error;
  700.         }

  701.       if (args_type == MI_PRINT_SIMPLE_VALUES)
  702.         {
  703.           if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
  704.             {
  705.               do_cleanups (locals_cleanups);
  706.               goto error;
  707.             }
  708.         }

  709.       /* CLI always prints values for locals.  MI uses the
  710.          simple/no/all system.  */
  711.       if (! ui_out_is_mi_like_p (out))
  712.         {
  713.           int val_indent = (indent + 1) * 4;

  714.           if (py_print_value (out, val, &opts, val_indent, args_type,
  715.                               language) == EXT_LANG_BT_ERROR)
  716.             {
  717.               do_cleanups (locals_cleanups);
  718.               goto error;
  719.             }
  720.         }
  721.       else
  722.         {
  723.           if (args_type != NO_VALUES)
  724.             {
  725.               if (py_print_value (out, val, &opts, 0, args_type,
  726.                                   language) == EXT_LANG_BT_ERROR)
  727.                 {
  728.                   do_cleanups (locals_cleanups);
  729.                   goto error;
  730.                 }
  731.             }
  732.         }

  733.       do_cleanups (locals_cleanups);

  734.       TRY_CATCH (except, RETURN_MASK_ALL)
  735.         {
  736.           ui_out_text (out, "\n");
  737.         }
  738.       if (except.reason < 0)
  739.         {
  740.           gdbpy_convert_exception (except);
  741.           goto error;
  742.         }
  743.     }

  744.   if (item == NULL && PyErr_Occurred ())
  745.     goto error;

  746.   return EXT_LANG_BT_OK;

  747. error:
  748.   return EXT_LANG_BT_ERROR;
  749. }

  750. /*  Helper function for -stack-list-variables.  Returns EXT_LANG_BT_ERROR on
  751.     error, or EXT_LANG_BT_OK on success.  */

  752. static enum ext_lang_bt_status
  753. py_mi_print_variables (PyObject *filter, struct ui_out *out,
  754.                        struct value_print_options *opts,
  755.                        enum ext_lang_frame_args args_type,
  756.                        struct frame_info *frame)
  757. {
  758.   struct cleanup *old_chain;
  759.   PyObject *args_iter;
  760.   PyObject *locals_iter;

  761.   args_iter = get_py_iter_from_func (filter, "frame_args");
  762.   old_chain = make_cleanup_py_xdecref (args_iter);
  763.   if (args_iter == NULL)
  764.     goto error;

  765.   locals_iter = get_py_iter_from_func (filter, "frame_locals");
  766.   if (locals_iter == NULL)
  767.     goto error;

  768.   make_cleanup_py_decref (locals_iter);
  769.   make_cleanup_ui_out_list_begin_end (out, "variables");

  770.   if (args_iter != Py_None)
  771.     if (enumerate_args (args_iter, out, args_type, 1, frame)
  772.         == EXT_LANG_BT_ERROR)
  773.       goto error;

  774.   if (locals_iter != Py_None)
  775.     if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame)
  776.         == EXT_LANG_BT_ERROR)
  777.       goto error;

  778.   do_cleanups (old_chain);
  779.   return EXT_LANG_BT_OK;

  780. error:
  781.   do_cleanups (old_chain);
  782.   return EXT_LANG_BT_ERROR;
  783. }

  784. /* Helper function for printing locals.  This function largely just
  785.    creates the wrapping tuple, and calls enumerate_locals.  Returns
  786.    EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success.  */

  787. static enum ext_lang_bt_status
  788. py_print_locals (PyObject *filter,
  789.                  struct ui_out *out,
  790.                  enum ext_lang_frame_args args_type,
  791.                  int indent,
  792.                  struct frame_info *frame)
  793. {
  794.   PyObject *locals_iter = get_py_iter_from_func (filter,
  795.                                                  "frame_locals");
  796.   struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter);

  797.   if (locals_iter == NULL)
  798.     goto locals_error;

  799.   make_cleanup_ui_out_list_begin_end (out, "locals");

  800.   if (locals_iter != Py_None)
  801.     if (enumerate_locals (locals_iter, out, indent, args_type,
  802.                           0, frame) == EXT_LANG_BT_ERROR)
  803.       goto locals_error;

  804.   do_cleanups (old_chain);
  805.   return EXT_LANG_BT_OK;

  806. locals_error:
  807.   do_cleanups (old_chain);
  808.   return EXT_LANG_BT_ERROR;
  809. }

  810. /* Helper function for printing frame arguments.  This function
  811.    largely just creates the wrapping tuple, and calls enumerate_args.
  812.    Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
  813.    a Python exception, or EXT_LANG_BT_OK on success.  */

  814. static enum ext_lang_bt_status
  815. py_print_args (PyObject *filter,
  816.                struct ui_out *out,
  817.                enum ext_lang_frame_args args_type,
  818.                struct frame_info *frame)
  819. {
  820.   PyObject *args_iter  = get_py_iter_from_func (filter, "frame_args");
  821.   struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter);
  822.   volatile struct gdb_exception except;

  823.   if (args_iter == NULL)
  824.     goto args_error;

  825.   make_cleanup_ui_out_list_begin_end (out, "args");

  826.   TRY_CATCH (except, RETURN_MASK_ALL)
  827.     {
  828.       annotate_frame_args ();
  829.       if (! ui_out_is_mi_like_p (out))
  830.         ui_out_text (out, " (");
  831.     }
  832.   if (except.reason < 0)
  833.     {
  834.       gdbpy_convert_exception (except);
  835.       goto args_error;
  836.     }

  837.   if (args_iter != Py_None)
  838.     if (enumerate_args (args_iter, out, args_type, 0, frame)
  839.         == EXT_LANG_BT_ERROR)
  840.       goto args_error;

  841.   TRY_CATCH (except, RETURN_MASK_ALL)
  842.     {
  843.       if (! ui_out_is_mi_like_p (out))
  844.         ui_out_text (out, ")");
  845.     }
  846.   if (except.reason < 0)
  847.     {
  848.       gdbpy_convert_exception (except);
  849.       goto args_error;
  850.     }

  851.   do_cleanups (old_chain);
  852.   return EXT_LANG_BT_OK;

  853. args_error:
  854.   do_cleanups (old_chain);
  855.   return EXT_LANG_BT_ERROR;
  856. }

  857. /*  Print a single frame to the designated output stream, detecting
  858.     whether the output is MI or console, and formatting the output
  859.     according to the conventions of that protocol.  FILTER is the
  860.     frame-filter associated with this frame.  FLAGS is an integer
  861.     describing the various print options.  The FLAGS variables is
  862.     described in "apply_frame_filter" function.  ARGS_TYPE is an
  863.     enumerator describing the argument format.  OUT is the output
  864.     stream to print, INDENT is the level of indention for this frame
  865.     (in the case of elided frames), and LEVELS_PRINTED is a hash-table
  866.     containing all the frames level that have already been printed.
  867.     If a frame level has been printed, do not print it again (in the
  868.     case of elided frames).  Returns EXT_LANG_BT_ERROR on error, with any
  869.     GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
  870.     on success.  */

  871. static enum ext_lang_bt_status
  872. py_print_frame (PyObject *filter, int flags,
  873.                 enum ext_lang_frame_args args_type,
  874.                 struct ui_out *out, int indent, htab_t levels_printed)
  875. {
  876.   int has_addr = 0;
  877.   CORE_ADDR address = 0;
  878.   struct gdbarch *gdbarch = NULL;
  879.   struct frame_info *frame = NULL;
  880.   struct cleanup *cleanup_stack = make_cleanup (null_cleanup, NULL);
  881.   struct value_print_options opts;
  882.   PyObject *py_inf_frame, *elided;
  883.   int print_level, print_frame_info, print_args, print_locals;
  884.   volatile struct gdb_exception except;

  885.   /* Extract print settings from FLAGS.  */
  886.   print_level = (flags & PRINT_LEVEL) ? 1 : 0;
  887.   print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0;
  888.   print_args = (flags & PRINT_ARGS) ? 1 : 0;
  889.   print_locals = (flags & PRINT_LOCALS) ? 1 : 0;

  890.   get_user_print_options (&opts);

  891.   /* Get the underlying frame.  This is needed to determine GDB
  892.   architecture, and also, in the cases of frame variables/arguments to
  893.   read them if they returned filter object requires us to do so.  */
  894.   py_inf_frame = PyObject_CallMethod (filter, "inferior_frame", NULL);
  895.   if (py_inf_frame == NULL)
  896.     goto error;

  897.   frame = frame_object_to_frame_info (py_inf_frame);;

  898.   Py_DECREF (py_inf_frame);

  899.   if (frame == NULL)
  900.     goto error;

  901.   TRY_CATCH (except, RETURN_MASK_ALL)
  902.     {
  903.       gdbarch = get_frame_arch (frame);
  904.     }
  905.   if (except.reason < 0)
  906.     {
  907.       gdbpy_convert_exception (except);
  908.       goto error;
  909.     }


  910.   /* stack-list-variables.  */
  911.   if (print_locals && print_args && ! print_frame_info)
  912.     {
  913.       if (py_mi_print_variables (filter, out, &opts,
  914.                                  args_type, frame) == EXT_LANG_BT_ERROR)
  915.         goto error;
  916.       else
  917.         {
  918.           do_cleanups (cleanup_stack);
  919.           return EXT_LANG_BT_COMPLETED;
  920.         }
  921.     }

  922.   /* -stack-list-locals does not require a
  923.      wrapping frame attribute.  */
  924.   if (print_frame_info || (print_args && ! print_locals))
  925.     make_cleanup_ui_out_tuple_begin_end (out, "frame");

  926.   if (print_frame_info)
  927.     {
  928.       /* Elided frames are also printed with this function (recursively)
  929.          and are printed with indention.  */
  930.       if (indent > 0)
  931.         {
  932.         TRY_CATCH (except, RETURN_MASK_ALL)
  933.           {
  934.             ui_out_spaces (out, indent*4);
  935.           }
  936.         if (except.reason < 0)
  937.           {
  938.             gdbpy_convert_exception (except);
  939.             goto error;
  940.           }
  941.         }

  942.       /* The address is required for frame annotations, and also for
  943.          address printing.  */
  944.       if (PyObject_HasAttrString (filter, "address"))
  945.         {
  946.           PyObject *paddr = PyObject_CallMethod (filter, "address", NULL);
  947.           if (paddr != NULL)
  948.             {
  949.               if (paddr != Py_None)
  950.                 {
  951.                   address = PyLong_AsLong (paddr);
  952.                   has_addr = 1;
  953.                 }
  954.               Py_DECREF (paddr);
  955.             }
  956.           else
  957.             goto error;
  958.         }
  959.     }

  960.   /* Print frame level.  MI does not require the level if
  961.      locals/variables only are being printed.  */
  962.   if ((print_frame_info || print_args) && print_level)
  963.     {
  964.       struct frame_info **slot;
  965.       int level;
  966.       volatile struct gdb_exception except;

  967.       slot = (struct frame_info **) htab_find_slot (levels_printed,
  968.                                                     frame, INSERT);
  969.       TRY_CATCH (except, RETURN_MASK_ALL)
  970.         {
  971.           level = frame_relative_level (frame);

  972.           /* Check if this frame has already been printed (there are cases
  973.              where elided synthetic dummy-frames have to 'borrow' the frame
  974.              architecture from the eliding frame.  If that is the case, do
  975.              not print 'level', but print spaces.  */
  976.           if (*slot == frame)
  977.             ui_out_field_skip (out, "level");
  978.           else
  979.             {
  980.               *slot = frame;
  981.               annotate_frame_begin (print_level ? level : 0,
  982.                                     gdbarch, address);
  983.               ui_out_text (out, "#");
  984.               ui_out_field_fmt_int (out, 2, ui_left, "level",
  985.                                     level);
  986.             }
  987.         }
  988.       if (except.reason < 0)
  989.         {
  990.           gdbpy_convert_exception (except);
  991.           goto error;
  992.         }
  993.     }

  994.   if (print_frame_info)
  995.     {
  996.       /* Print address to the address field.  If an address is not provided,
  997.          print nothing.  */
  998.       if (opts.addressprint && has_addr)
  999.         {
  1000.           TRY_CATCH (except, RETURN_MASK_ALL)
  1001.             {
  1002.               annotate_frame_address ();
  1003.               ui_out_field_core_addr (out, "addr", gdbarch, address);
  1004.               annotate_frame_address_end ();
  1005.               ui_out_text (out, " in ");
  1006.             }
  1007.           if (except.reason < 0)
  1008.             {
  1009.               gdbpy_convert_exception (except);
  1010.               goto error;
  1011.             }
  1012.         }

  1013.       /* Print frame function name.  */
  1014.       if (PyObject_HasAttrString (filter, "function"))
  1015.         {
  1016.           PyObject *py_func = PyObject_CallMethod (filter, "function", NULL);

  1017.           if (py_func != NULL)
  1018.             {
  1019.               const char *function = NULL;

  1020.               if (gdbpy_is_string (py_func))
  1021.                 {
  1022.                   char *function_to_free = NULL;

  1023.                   function = function_to_free =
  1024.                     python_string_to_host_string (py_func);

  1025.                   if (function == NULL)
  1026.                     {
  1027.                       Py_DECREF (py_func);
  1028.                       goto error;
  1029.                     }
  1030.                   make_cleanup (xfree, function_to_free);
  1031.                 }
  1032.               else if (PyLong_Check (py_func))
  1033.                 {
  1034.                   CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func);
  1035.                   struct bound_minimal_symbol msymbol;

  1036.                   if (PyErr_Occurred ())
  1037.                     goto error;

  1038.                   msymbol = lookup_minimal_symbol_by_pc (addr);
  1039.                   if (msymbol.minsym != NULL)
  1040.                     function = MSYMBOL_PRINT_NAME (msymbol.minsym);
  1041.                 }
  1042.               else if (py_func != Py_None)
  1043.                 {
  1044.                   PyErr_SetString (PyExc_RuntimeError,
  1045.                                    _("FrameDecorator.function: expecting a " \
  1046.                                      "String, integer or None."));
  1047.                   Py_DECREF (py_func);
  1048.                   goto error;
  1049.                 }


  1050.               TRY_CATCH (except, RETURN_MASK_ALL)
  1051.                 {
  1052.                   annotate_frame_function_name ();
  1053.                   if (function == NULL)
  1054.                     ui_out_field_skip (out, "func");
  1055.                   else
  1056.                     ui_out_field_string (out, "func", function);
  1057.                 }
  1058.               if (except.reason < 0)
  1059.                 {
  1060.                   Py_DECREF (py_func);
  1061.                   gdbpy_convert_exception (except);
  1062.                   goto error;
  1063.                 }
  1064.               Py_DECREF (py_func);
  1065.             }
  1066.           else
  1067.             goto error;
  1068.         }
  1069.     }


  1070.   /* Frame arguments.  Check the result, and error if something went
  1071.      wrong.  */
  1072.   if (print_args)
  1073.     {
  1074.       if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
  1075.         goto error;
  1076.     }

  1077.   /* File name/source/line number information.  */
  1078.   if (print_frame_info)
  1079.     {
  1080.       TRY_CATCH (except, RETURN_MASK_ALL)
  1081.         {
  1082.           annotate_frame_source_begin ();
  1083.         }
  1084.       if (except.reason < 0)
  1085.         {
  1086.           gdbpy_convert_exception (except);
  1087.           goto error;
  1088.         }

  1089.       if (PyObject_HasAttrString (filter, "filename"))
  1090.         {
  1091.           PyObject *py_fn = PyObject_CallMethod (filter, "filename",
  1092.                                                  NULL);
  1093.           if (py_fn != NULL)
  1094.             {
  1095.               if (py_fn != Py_None)
  1096.                 {
  1097.                   char *filename = python_string_to_host_string (py_fn);

  1098.                   if (filename == NULL)
  1099.                     {
  1100.                       Py_DECREF (py_fn);
  1101.                       goto error;
  1102.                     }

  1103.                   make_cleanup (xfree, filename);
  1104.                   TRY_CATCH (except, RETURN_MASK_ALL)
  1105.                     {
  1106.                       ui_out_wrap_hint (out, "   ");
  1107.                       ui_out_text (out, " at ");
  1108.                       annotate_frame_source_file ();
  1109.                       ui_out_field_string (out, "file", filename);
  1110.                       annotate_frame_source_file_end ();
  1111.                     }
  1112.                   if (except.reason < 0)
  1113.                     {
  1114.                       Py_DECREF (py_fn);
  1115.                       gdbpy_convert_exception (except);
  1116.                       goto error;
  1117.                     }
  1118.                 }
  1119.               Py_DECREF (py_fn);
  1120.             }
  1121.           else
  1122.             goto error;
  1123.         }

  1124.       if (PyObject_HasAttrString (filter, "line"))
  1125.         {
  1126.           PyObject *py_line = PyObject_CallMethod (filter, "line", NULL);
  1127.           int line;

  1128.           if (py_line != NULL)
  1129.             {
  1130.               if (py_line != Py_None)
  1131.                 {
  1132.                   line = PyLong_AsLong (py_line);
  1133.                   TRY_CATCH (except, RETURN_MASK_ALL)
  1134.                     {
  1135.                       ui_out_text (out, ":");
  1136.                       annotate_frame_source_line ();
  1137.                       ui_out_field_int (out, "line", line);
  1138.                     }
  1139.                   if (except.reason < 0)
  1140.                     {
  1141.                       Py_DECREF (py_line);
  1142.                       gdbpy_convert_exception (except);
  1143.                       goto error;
  1144.                     }
  1145.                 }
  1146.               Py_DECREF (py_line);
  1147.             }
  1148.           else
  1149.             goto error;
  1150.         }
  1151.     }

  1152.   /* For MI we need to deal with the "children" list population of
  1153.      elided frames, so if MI output detected do not send newline.  */
  1154.   if (! ui_out_is_mi_like_p (out))
  1155.     {
  1156.       TRY_CATCH (except, RETURN_MASK_ALL)
  1157.         {
  1158.           annotate_frame_end ();
  1159.           ui_out_text (out, "\n");
  1160.         }
  1161.       if (except.reason < 0)
  1162.         {
  1163.           gdbpy_convert_exception (except);
  1164.           goto error;
  1165.         }
  1166.     }

  1167.   if (print_locals)
  1168.     {
  1169.       if (py_print_locals (filter, out, args_type, indent,
  1170.                            frame) == EXT_LANG_BT_ERROR)
  1171.         goto error;
  1172.     }

  1173.   /* Finally recursively print elided frames, if any.  */
  1174.   elided  = get_py_iter_from_func (filter, "elided");
  1175.   if (elided == NULL)
  1176.     goto error;

  1177.   make_cleanup_py_decref (elided);
  1178.   if (elided != Py_None)
  1179.     {
  1180.       PyObject *item;

  1181.       make_cleanup_ui_out_list_begin_end (out, "children");

  1182.       if (! ui_out_is_mi_like_p (out))
  1183.         indent++;

  1184.       while ((item = PyIter_Next (elided)))
  1185.         {
  1186.           enum ext_lang_bt_status success = py_print_frame (item, flags,
  1187.                                                             args_type, out,
  1188.                                                             indent,
  1189.                                                             levels_printed);

  1190.           if (success == EXT_LANG_BT_ERROR)
  1191.             {
  1192.               Py_DECREF (item);
  1193.               goto error;
  1194.             }

  1195.           Py_DECREF (item);
  1196.         }
  1197.       if (item == NULL && PyErr_Occurred ())
  1198.         goto error;
  1199.     }


  1200.   do_cleanups (cleanup_stack);
  1201.   return EXT_LANG_BT_COMPLETED;

  1202. error:
  1203.   do_cleanups (cleanup_stack);
  1204.   return EXT_LANG_BT_ERROR;
  1205. }

  1206. /* Helper function to initiate frame filter invocation at starting
  1207.    frame FRAME.  */

  1208. static PyObject *
  1209. bootstrap_python_frame_filters (struct frame_info *frame,
  1210.                                 int frame_low, int frame_high)
  1211. {
  1212.   struct cleanup *cleanups =
  1213.     make_cleanup (null_cleanup, NULL);
  1214.   PyObject *module, *sort_func, *iterable, *frame_obj, *iterator;
  1215.   PyObject *py_frame_low, *py_frame_high;

  1216.   frame_obj = frame_info_to_frame_object (frame);
  1217.   if (frame_obj == NULL)
  1218.     goto error;
  1219.   make_cleanup_py_decref (frame_obj);

  1220.   module = PyImport_ImportModule ("gdb.frames");
  1221.   if (module == NULL)
  1222.     goto error;
  1223.   make_cleanup_py_decref (module);

  1224.   sort_func = PyObject_GetAttrString (module, "execute_frame_filters");
  1225.   if (sort_func == NULL)
  1226.     goto error;
  1227.   make_cleanup_py_decref (sort_func);

  1228.   py_frame_low = PyInt_FromLong (frame_low);
  1229.   if (py_frame_low == NULL)
  1230.     goto error;
  1231.   make_cleanup_py_decref (py_frame_low);

  1232.   py_frame_high = PyInt_FromLong (frame_high);
  1233.   if (py_frame_high == NULL)
  1234.     goto error;
  1235.   make_cleanup_py_decref (py_frame_high);

  1236.   iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj,
  1237.                                            py_frame_low,
  1238.                                            py_frame_high,
  1239.                                            NULL);
  1240.   if (iterable == NULL)
  1241.     goto error;

  1242.   do_cleanups (cleanups);

  1243.   if (iterable != Py_None)
  1244.     {
  1245.       iterator = PyObject_GetIter (iterable);
  1246.       Py_DECREF (iterable);
  1247.     }
  1248.   else
  1249.     {
  1250.       return iterable;
  1251.     }

  1252.   return iterator;

  1253. error:
  1254.   do_cleanups (cleanups);
  1255.   return NULL;
  1256. }

  1257. /*  This is the only publicly exported function in this file.  FRAME
  1258.     is the source frame to start frame-filter invocation.  FLAGS is an
  1259.     integer holding the flags for printing.  The following elements of
  1260.     the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
  1261.     PRINT_LEVEL is a flag indicating whether to print the frame's
  1262.     relative level in the output.  PRINT_FRAME_INFO is a flag that
  1263.     indicates whether this function should print the frame
  1264.     information, PRINT_ARGS is a flag that indicates whether to print
  1265.     frame arguments, and PRINT_LOCALS, likewise, with frame local
  1266.     variables.  ARGS_TYPE is an enumerator describing the argument
  1267.     format, OUT is the output stream to print.  FRAME_LOW is the
  1268.     beginning of the slice of frames to print, and FRAME_HIGH is the
  1269.     upper limit of the frames to count.  Returns EXT_LANG_BT_ERROR on error,
  1270.     or EXT_LANG_BT_COMPLETED on success.  */

  1271. enum ext_lang_bt_status
  1272. gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
  1273.                           struct frame_info *frame, int flags,
  1274.                           enum ext_lang_frame_args args_type,
  1275.                           struct ui_out *out, int frame_low, int frame_high)
  1276. {
  1277.   struct gdbarch *gdbarch = NULL;
  1278.   struct cleanup *cleanups;
  1279.   enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
  1280.   PyObject *iterable;
  1281.   volatile struct gdb_exception except;
  1282.   PyObject *item;
  1283.   htab_t levels_printed;

  1284.   if (!gdb_python_initialized)
  1285.     return EXT_LANG_BT_NO_FILTERS;

  1286.   TRY_CATCH (except, RETURN_MASK_ALL)
  1287.     {
  1288.       gdbarch = get_frame_arch (frame);
  1289.     }
  1290.   if (except.reason < 0)
  1291.     {
  1292.       /* Let gdb try to print the stack trace.  */
  1293.       return EXT_LANG_BT_NO_FILTERS;
  1294.     }

  1295.   cleanups = ensure_python_env (gdbarch, current_language);

  1296.   iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);

  1297.   if (iterable == NULL)
  1298.     {
  1299.       /* Normally if there is an error GDB prints the exception,
  1300.          abandons the backtrace and exits.  The user can then call "bt
  1301.          no-filters", and get a default backtrace (it would be
  1302.          confusing to automatically start a standard backtrace halfway
  1303.          through a Python filtered backtrace).  However in the case
  1304.          where GDB cannot initialize the frame filters (most likely
  1305.          due to incorrect auto-load paths), GDB has printed nothing.
  1306.          In this case it is OK to print the default backtrace after
  1307.          printing the error message.  GDB returns EXT_LANG_BT_NO_FILTERS
  1308.          here to signify there are no filters after printing the
  1309.          initialization error.  This return code will trigger a
  1310.          default backtrace.  */

  1311.       gdbpy_print_stack ();
  1312.       do_cleanups (cleanups);
  1313.       return EXT_LANG_BT_NO_FILTERS;
  1314.     }

  1315.   /* If iterable is None, then there are no frame filters registered.
  1316.      If this is the case, defer to default GDB printing routines in MI
  1317.      and CLI.  */
  1318.   make_cleanup_py_decref (iterable);
  1319.   if (iterable == Py_None)
  1320.     {
  1321.       success = EXT_LANG_BT_NO_FILTERS;
  1322.       goto done;
  1323.     }

  1324.   levels_printed = htab_create (20,
  1325.                                 htab_hash_pointer,
  1326.                                 htab_eq_pointer,
  1327.                                 NULL);
  1328.   make_cleanup_htab_delete (levels_printed);

  1329.   while ((item = PyIter_Next (iterable)))
  1330.     {
  1331.       success = py_print_frame (item, flags, args_type, out, 0,
  1332.                                 levels_printed);

  1333.       /* Do not exit on error printing a single frame.  Print the
  1334.          error and continue with other frames.  */
  1335.       if (success == EXT_LANG_BT_ERROR)
  1336.         gdbpy_print_stack ();

  1337.       Py_DECREF (item);
  1338.     }

  1339.   if (item == NULL && PyErr_Occurred ())
  1340.     goto error;

  1341. done:
  1342.   do_cleanups (cleanups);
  1343.   return success;

  1344.   /* Exit and abandon backtrace on error, printing the exception that
  1345.      is set.  */
  1346. error:
  1347.   gdbpy_print_stack ();
  1348.   do_cleanups (cleanups);
  1349.   return EXT_LANG_BT_ERROR;
  1350. }