GDB (API)
/home/stan/gdb/src/gdb/python/py-framefilter.c
Go to the documentation of this file.
00001 /* Python frame filters
00002 
00003    Copyright (C) 2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "objfiles.h"
00022 #include "symtab.h"
00023 #include "language.h"
00024 #include "exceptions.h"
00025 #include "arch-utils.h"
00026 #include "python.h"
00027 #include "ui-out.h"
00028 #include "valprint.h"
00029 #include "annotate.h"
00030 #include "hashtab.h"
00031 #include "demangle.h"
00032 #include "mi/mi-cmds.h"
00033 #include "python-internal.h"
00034 
00035 enum mi_print_types
00036 {
00037   MI_PRINT_ARGS,
00038   MI_PRINT_LOCALS
00039 };
00040 
00041 /* Helper  function  to  extract  a  symbol, a  name  and  a  language
00042    definition from a Python object that conforms to the "Symbol Value"
00043    interface.  OBJ  is the Python  object to extract the  values from.
00044    NAME is a  pass-through argument where the name of  the symbol will
00045    be written.  NAME is allocated in  this function, but the caller is
00046    responsible for clean up.  SYM is a pass-through argument where the
00047    symbol will be written.  In the case of the API returning a string,
00048    this will be set to NULL.  LANGUAGE is also a pass-through argument
00049    denoting the language attributed to the Symbol.  In the case of SYM
00050    being  NULL, this  will be  set to  the current  language.  Returns
00051    PY_BT_ERROR on error with the appropriate Python exception set, and
00052    PY_BT_OK on success.  */
00053 
00054 static enum py_bt_status
00055 extract_sym (PyObject *obj, char **name, struct symbol **sym,
00056              const struct language_defn **language)
00057 {
00058   PyObject *result = PyObject_CallMethod (obj, "symbol", NULL);
00059 
00060   if (result == NULL)
00061     return PY_BT_ERROR;
00062 
00063   /* For 'symbol' callback, the function can return a symbol or a
00064      string.  */
00065   if (gdbpy_is_string (result))
00066     {
00067       *name = python_string_to_host_string (result);
00068       Py_DECREF (result);
00069 
00070       if (*name == NULL)
00071         return PY_BT_ERROR;
00072       /* If the API returns a string (and not a symbol), then there is
00073         no symbol derived language available and the frame filter has
00074         either overridden the symbol with a string, or supplied a
00075         entirely synthetic symbol/value pairing.  In that case, use
00076         python_language.  */
00077       *language = python_language;
00078       *sym = NULL;
00079     }
00080   else
00081     {
00082       /* This type checks 'result' during the conversion so we
00083          just call it unconditionally and check the return.  */
00084       *sym = symbol_object_to_symbol (result);
00085 
00086       Py_DECREF (result);
00087 
00088       if (*sym == NULL)
00089         {
00090           PyErr_SetString (PyExc_RuntimeError,
00091                            _("Unexpected value.  Expecting a "
00092                              "gdb.Symbol or a Python string."));
00093           return PY_BT_ERROR;
00094         }
00095 
00096       /* Duplicate the symbol name, so the caller has consistency
00097          in garbage collection.  */
00098       *name = xstrdup (SYMBOL_PRINT_NAME (*sym));
00099 
00100       /* If a symbol is specified attempt to determine the language
00101          from the symbol.  If mode is not "auto", then the language
00102          has been explicitly set, use that.  */
00103       if (language_mode == language_mode_auto)
00104         *language = language_def (SYMBOL_LANGUAGE (*sym));
00105       else
00106         *language = current_language;
00107     }
00108 
00109   return PY_BT_OK;
00110 }
00111 
00112 /* Helper function to extract a value from an object that conforms to
00113    the "Symbol Value" interface.  OBJ is the Python object to extract
00114    the value from.  VALUE is a pass-through argument where the value
00115    will be written.  If the object does not have the value attribute,
00116    or provides the Python None for a value, VALUE will be set to NULL
00117    and this function will return as successful.  Returns PY_BT_ERROR
00118    on error with the appropriate Python exception set, and PY_BT_OK on
00119    success.  */
00120 
00121 static enum py_bt_status
00122 extract_value (PyObject *obj, struct value **value)
00123 {
00124   if (PyObject_HasAttrString (obj, "value"))
00125     {
00126       PyObject *vresult = PyObject_CallMethod (obj, "value", NULL);
00127 
00128       if (vresult == NULL)
00129         return PY_BT_ERROR;
00130 
00131       /* The Python code has returned 'None' for a value, so we set
00132          value to NULL.  This flags that GDB should read the
00133          value.  */
00134       if (vresult == Py_None)
00135         {
00136           Py_DECREF (vresult);
00137           *value = NULL;
00138           return PY_BT_OK;
00139         }
00140       else
00141         {
00142           *value = convert_value_from_python (vresult);
00143           Py_DECREF (vresult);
00144 
00145           if (*value == NULL)
00146             return PY_BT_ERROR;
00147 
00148           return PY_BT_OK;
00149         }
00150     }
00151   else
00152     *value = NULL;
00153 
00154   return PY_BT_OK;
00155 }
00156 
00157 /* MI prints only certain values according to the type of symbol and
00158    also what the user has specified.  SYM is the symbol to check, and
00159    MI_PRINT_TYPES is an enum specifying what the user wants emitted
00160    for the MI command in question.  */
00161 static int
00162 mi_should_print (struct symbol *sym, enum mi_print_types type)
00163 {
00164   int print_me = 0;
00165 
00166   switch (SYMBOL_CLASS (sym))
00167     {
00168     default:
00169     case LOC_UNDEF:     /* catches errors        */
00170     case LOC_CONST:     /* constant              */
00171     case LOC_TYPEDEF:   /* local typedef         */
00172     case LOC_LABEL:     /* local label           */
00173     case LOC_BLOCK:     /* local function        */
00174     case LOC_CONST_BYTES:       /* loc. byte seq.        */
00175     case LOC_UNRESOLVED:        /* unresolved static     */
00176     case LOC_OPTIMIZED_OUT:     /* optimized out         */
00177       print_me = 0;
00178       break;
00179 
00180     case LOC_ARG:       /* argument              */
00181     case LOC_REF_ARG:   /* reference arg         */
00182     case LOC_REGPARM_ADDR:      /* indirect register arg */
00183     case LOC_LOCAL:     /* stack local           */
00184     case LOC_STATIC:    /* static                */
00185     case LOC_REGISTER:  /* register              */
00186     case LOC_COMPUTED:  /* computed location     */
00187       if (type == MI_PRINT_LOCALS)
00188         print_me = ! SYMBOL_IS_ARGUMENT (sym);
00189       else
00190         print_me = SYMBOL_IS_ARGUMENT (sym);
00191     }
00192   return print_me;
00193 }
00194 
00195 /* Helper function which outputs a type name extracted from VAL to a
00196    "type" field in the output stream OUT.  OUT is the ui-out structure
00197    the type name will be output too, and VAL is the value that the
00198    type will be extracted from.  Returns PY_BT_ERROR on error, with
00199    any GDB exceptions converted to a Python exception, or PY_BT_OK on
00200    success.  */
00201 
00202 static enum py_bt_status
00203 py_print_type (struct ui_out *out, struct value *val)
00204 {
00205   volatile struct gdb_exception except;
00206 
00207   TRY_CATCH (except, RETURN_MASK_ALL)
00208     {
00209       struct type *type;
00210       struct ui_file *stb;
00211       struct cleanup *cleanup;
00212 
00213       stb = mem_fileopen ();
00214       cleanup = make_cleanup_ui_file_delete (stb);
00215       type = check_typedef (value_type (val));
00216       type_print (value_type (val), "", stb, -1);
00217       ui_out_field_stream (out, "type", stb);
00218       do_cleanups (cleanup);
00219     }
00220   if (except.reason < 0)
00221     {
00222       gdbpy_convert_exception (except);
00223       return PY_BT_ERROR;
00224     }
00225 
00226   return PY_BT_OK;
00227 }
00228 
00229 /* Helper function which outputs a value to an output field in a
00230    stream.  OUT is the ui-out structure the value will be output to,
00231    VAL is the value that will be printed, OPTS contains the value
00232    printing options, ARGS_TYPE is an enumerator describing the
00233    argument format, and LANGUAGE is the language_defn that the value
00234    will be printed with.  Returns PY_BT_ERROR on error, with any GDB
00235    exceptions converted to a Python exception, or PY_BT_OK on
00236    success. */
00237 
00238 static enum py_bt_status
00239 py_print_value (struct ui_out *out, struct value *val,
00240                 const struct value_print_options *opts,
00241                 int indent,
00242                 enum py_frame_args args_type,
00243                 const struct language_defn *language)
00244 {
00245   int should_print = 0;
00246   volatile struct gdb_exception except;
00247   int local_indent = (4 * indent);
00248 
00249   /* Never set an indent level for common_val_print if MI.  */
00250   if (ui_out_is_mi_like_p (out))
00251     local_indent = 0;
00252 
00253   /* MI does not print certain values, differentiated by type,
00254      depending on what ARGS_TYPE indicates.  Test type against option.
00255      For CLI print all values.  */
00256   if (args_type == MI_PRINT_SIMPLE_VALUES
00257       || args_type == MI_PRINT_ALL_VALUES)
00258     {
00259       struct type *type = NULL;
00260 
00261       TRY_CATCH (except, RETURN_MASK_ALL)
00262         {
00263           type = check_typedef (value_type (val));
00264         }
00265       if (except.reason < 0)
00266         {
00267           gdbpy_convert_exception (except);
00268           return PY_BT_ERROR;
00269         }
00270 
00271       if (args_type == MI_PRINT_ALL_VALUES)
00272         should_print = 1;
00273       else if (args_type == MI_PRINT_SIMPLE_VALUES
00274                && TYPE_CODE (type) != TYPE_CODE_ARRAY
00275                && TYPE_CODE (type) != TYPE_CODE_STRUCT
00276                && TYPE_CODE (type) != TYPE_CODE_UNION)
00277         should_print = 1;
00278     }
00279   else if (args_type != NO_VALUES)
00280     should_print = 1;
00281 
00282   if (should_print)
00283     {
00284       TRY_CATCH (except, RETURN_MASK_ALL)
00285         {
00286           struct ui_file *stb;
00287           struct cleanup *cleanup;
00288 
00289           stb = mem_fileopen ();
00290           cleanup = make_cleanup_ui_file_delete (stb);
00291           common_val_print (val, stb, indent, opts, language);
00292           ui_out_field_stream (out, "value", stb);
00293           do_cleanups (cleanup);
00294         }
00295       if (except.reason < 0)
00296         {
00297           gdbpy_convert_exception (except);
00298           return PY_BT_ERROR;
00299         }
00300     }
00301 
00302   return PY_BT_OK;
00303 }
00304 
00305 /* Helper function to call a Python method and extract an iterator
00306    from the result.  If the function returns anything but an iterator
00307    the exception is preserved and NULL is returned.  FILTER is the
00308    Python object to call, and FUNC is the name of the method.  Returns
00309    a PyObject, or NULL on error with the appropriate exception set.
00310    This function can return an iterator, or NULL.  */
00311 
00312 static PyObject *
00313 get_py_iter_from_func (PyObject *filter, char *func)
00314 {
00315   if (PyObject_HasAttrString (filter, func))
00316     {
00317       PyObject *result = PyObject_CallMethod (filter, func, NULL);
00318 
00319       if (result != NULL)
00320         {
00321           if (result == Py_None)
00322             {
00323               return result;
00324             }
00325           else
00326             {
00327               PyObject *iterator = PyObject_GetIter (result);
00328 
00329               Py_DECREF (result);
00330               return iterator;
00331             }
00332         }
00333     }
00334   else
00335     Py_RETURN_NONE;
00336 
00337   return NULL;
00338 }
00339 
00340 /*  Helper function to output a single frame argument and value to an
00341     output stream.  This function will account for entry values if the
00342     FV parameter is populated, the frame argument has entry values
00343     associated with them, and the appropriate "set entry-value"
00344     options are set.  Will output in CLI or MI like format depending
00345     on the type of output stream detected.  OUT is the output stream,
00346     SYM_NAME is the name of the symbol.  If SYM_NAME is populated then
00347     it must have an accompanying value in the parameter FV.  FA is a
00348     frame argument structure.  If FA is populated, both SYM_NAME and
00349     FV are ignored.  OPTS contains the value printing options,
00350     ARGS_TYPE is an enumerator describing the argument format,
00351     PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
00352     in MI output in commands where both arguments and locals are
00353     printed.  Returns PY_BT_ERROR on error, with any GDB exceptions
00354     converted to a Python exception, or PY_BT_OK on success.  */
00355 
00356 static enum py_bt_status
00357 py_print_single_arg (struct ui_out *out,
00358                      const char *sym_name,
00359                      struct frame_arg *fa,
00360                      struct value *fv,
00361                      const struct value_print_options *opts,
00362                      enum py_frame_args args_type,
00363                      int print_args_field,
00364                      const struct language_defn *language)
00365 {
00366   struct value *val;
00367   volatile struct gdb_exception except;
00368 
00369   if (fa != NULL)
00370     {
00371       language = language_def (SYMBOL_LANGUAGE (fa->sym));
00372       val = fa->val;
00373     }
00374   else
00375     val = fv;
00376 
00377   TRY_CATCH (except, RETURN_MASK_ALL)
00378     {
00379       struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
00380 
00381       /*  MI has varying rules for tuples, but generally if there is only
00382       one element in each item in the list, do not start a tuple.  The
00383       exception is -stack-list-variables which emits an ARGS="1" field
00384       if the value is a frame argument.  This is denoted in this
00385       function with PRINT_ARGS_FIELD which is flag from the caller to
00386       emit the ARGS field.  */
00387       if (ui_out_is_mi_like_p (out))
00388         {
00389           if (print_args_field || args_type != NO_VALUES)
00390             make_cleanup_ui_out_tuple_begin_end (out, NULL);
00391         }
00392 
00393       annotate_arg_begin ();
00394 
00395       /* If frame argument is populated, check for entry-values and the
00396          entry value options.  */
00397       if (fa != NULL)
00398         {
00399           struct ui_file *stb;
00400 
00401           stb = mem_fileopen ();
00402           make_cleanup_ui_file_delete (stb);
00403           fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
00404                                    SYMBOL_LANGUAGE (fa->sym),
00405                                    DMGL_PARAMS | DMGL_ANSI);
00406           if (fa->entry_kind == print_entry_values_compact)
00407             {
00408               fputs_filtered ("=", stb);
00409 
00410               fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
00411                                        SYMBOL_LANGUAGE (fa->sym),
00412                                        DMGL_PARAMS | DMGL_ANSI);
00413             }
00414           if (fa->entry_kind == print_entry_values_only
00415               || fa->entry_kind == print_entry_values_compact)
00416             {
00417               fputs_filtered ("@entry", stb);
00418             }
00419           ui_out_field_stream (out, "name", stb);
00420         }
00421       else
00422         /* Otherwise, just output the name.  */
00423         ui_out_field_string (out, "name", sym_name);
00424 
00425       annotate_arg_name_end ();
00426 
00427       if (! ui_out_is_mi_like_p (out))
00428         ui_out_text (out, "=");
00429 
00430       if (print_args_field)
00431         ui_out_field_int (out, "arg", 1);
00432 
00433       /* For MI print the type, but only for simple values.  This seems
00434          weird, but this is how MI choose to format the various output
00435          types.  */
00436       if (args_type == MI_PRINT_SIMPLE_VALUES)
00437         {
00438           if (py_print_type (out, val) == PY_BT_ERROR)
00439             {
00440               do_cleanups (cleanups);
00441               goto error;
00442             }
00443         }
00444 
00445       annotate_arg_value (value_type (val));
00446 
00447       /* If the output is to the CLI, and the user option "set print
00448          frame-arguments" is set to none, just output "...".  */
00449       if (! ui_out_is_mi_like_p (out) && args_type == NO_VALUES)
00450         ui_out_field_string (out, "value", "...");
00451       else
00452         {
00453           /* Otherwise, print the value for both MI and the CLI, except
00454              for the case of MI_PRINT_NO_VALUES.  */
00455           if (args_type != NO_VALUES)
00456             {
00457               if (py_print_value (out, val, opts, 0, args_type, language)
00458                   == PY_BT_ERROR)
00459                 {
00460                   do_cleanups (cleanups);
00461                   goto error;
00462                 }
00463             }
00464         }
00465 
00466       do_cleanups (cleanups);
00467     }
00468   if (except.reason < 0)
00469     {
00470       gdbpy_convert_exception (except);
00471       goto error;
00472     }
00473 
00474   return PY_BT_OK;
00475 
00476  error:
00477   return PY_BT_ERROR;
00478 }
00479 
00480 /* Helper function to loop over frame arguments provided by the
00481    "frame_arguments" Python API.  Elements in the iterator must
00482    conform to the "Symbol Value" interface.  ITER is the Python
00483    iterable object, OUT is the output stream, ARGS_TYPE is an
00484    enumerator describing the argument format, PRINT_ARGS_FIELD is a
00485    flag which indicates if we output "ARGS=1" in MI output in commands
00486    where both arguments and locals are printed, and FRAME is the
00487    backing frame.  Returns PY_BT_ERROR on error, with any GDB
00488    exceptions converted to a Python exception, or PY_BT_OK on
00489    success.  */
00490 
00491 static enum py_bt_status
00492 enumerate_args (PyObject *iter,
00493                 struct ui_out *out,
00494                 enum py_frame_args args_type,
00495                 int print_args_field,
00496                 struct frame_info *frame)
00497 {
00498   PyObject *item;
00499   struct value_print_options opts;
00500   volatile struct gdb_exception except;
00501 
00502   get_user_print_options (&opts);
00503 
00504   if (args_type == CLI_SCALAR_VALUES)
00505     {
00506       /* True in "summary" mode, false otherwise.  */
00507       opts.summary = 1;
00508     }
00509 
00510   opts.deref_ref = 1;
00511 
00512   TRY_CATCH (except, RETURN_MASK_ALL)
00513     {
00514       annotate_frame_args ();
00515     }
00516   if (except.reason < 0)
00517     {
00518       gdbpy_convert_exception (except);
00519       goto error;
00520     }
00521 
00522   /*  Collect the first argument outside of the loop, so output of
00523       commas in the argument output is correct.  At the end of the
00524       loop block collect another item from the iterator, and, if it is
00525       not null emit a comma.  */
00526   item = PyIter_Next (iter);
00527   if (item == NULL && PyErr_Occurred ())
00528     goto error;
00529 
00530   while (item)
00531     {
00532       const struct language_defn *language;
00533       char *sym_name;
00534       struct symbol *sym;
00535       struct value *val;
00536       enum py_bt_status success = PY_BT_ERROR;
00537 
00538       success = extract_sym (item, &sym_name, &sym, &language);
00539       if (success == PY_BT_ERROR)
00540         {
00541           Py_DECREF (item);
00542           goto error;
00543         }
00544 
00545       success = extract_value (item, &val);
00546       if (success == PY_BT_ERROR)
00547         {
00548           xfree (sym_name);
00549           Py_DECREF (item);
00550           goto error;
00551         }
00552 
00553       Py_DECREF (item);
00554       item = NULL;
00555 
00556       if (sym && ui_out_is_mi_like_p (out)
00557           && ! mi_should_print (sym, MI_PRINT_ARGS))
00558         {
00559           xfree (sym_name);
00560           continue;
00561         }
00562 
00563       /* If the object did not provide a value, read it using
00564          read_frame_args and account for entry values, if any.  */
00565       if (val == NULL)
00566         {
00567           struct frame_arg arg, entryarg;
00568 
00569           /* If there is no value, and also no symbol, set error and
00570              exit.  */
00571           if (sym == NULL)
00572             {
00573               PyErr_SetString (PyExc_RuntimeError,
00574                                _("No symbol or value provided."));
00575               xfree (sym_name);
00576               goto error;
00577             }
00578 
00579           TRY_CATCH (except, RETURN_MASK_ALL)
00580             {
00581               read_frame_arg (sym, frame, &arg, &entryarg);
00582             }
00583           if (except.reason < 0)
00584             {
00585               xfree (sym_name);
00586               gdbpy_convert_exception (except);
00587               goto error;
00588             }
00589 
00590           /* The object has not provided a value, so this is a frame
00591              argument to be read by GDB.  In this case we have to
00592              account for entry-values.  */
00593 
00594           if (arg.entry_kind != print_entry_values_only)
00595             {
00596               if (py_print_single_arg (out, NULL, &arg,
00597                                        NULL, &opts,
00598                                        args_type,
00599                                        print_args_field,
00600                                        NULL) == PY_BT_ERROR)
00601                 {
00602                   xfree (arg.error);
00603                   xfree (entryarg.error);
00604                   xfree (sym_name);
00605                   goto error;
00606                 }
00607             }
00608 
00609           if (entryarg.entry_kind != print_entry_values_no)
00610             {
00611               if (arg.entry_kind != print_entry_values_only)
00612                 {
00613                   TRY_CATCH (except, RETURN_MASK_ALL)
00614                     {
00615                       ui_out_text (out, ", ");
00616                       ui_out_wrap_hint (out, "    ");
00617                     }
00618                   if (except.reason < 0)
00619                     {
00620                       xfree (arg.error);
00621                       xfree (entryarg.error);
00622                       xfree (sym_name);
00623                       gdbpy_convert_exception (except);
00624                       goto error;
00625                     }
00626                 }
00627 
00628               if (py_print_single_arg (out, NULL, &entryarg, NULL,
00629                                       &opts, args_type,
00630                                       print_args_field, NULL) == PY_BT_ERROR)
00631                 {
00632                       xfree (arg.error);
00633                       xfree (entryarg.error);
00634                       xfree (sym_name);
00635                       goto error;
00636                 }
00637             }
00638 
00639           xfree (arg.error);
00640           xfree (entryarg.error);
00641         }
00642       else
00643         {
00644           /* If the object has provided a value, we just print that.  */
00645           if (val != NULL)
00646             {
00647               if (py_print_single_arg (out, sym_name, NULL, val, &opts,
00648                                        args_type, print_args_field,
00649                                        language) == PY_BT_ERROR)
00650                 {
00651                   xfree (sym_name);
00652                   goto error;
00653                 }
00654             }
00655         }
00656 
00657       xfree (sym_name);
00658 
00659       /* Collect the next item from the iterator.  If
00660          this is the last item, do not print the
00661          comma.  */
00662       item = PyIter_Next (iter);
00663       if (item != NULL)
00664         {
00665           TRY_CATCH (except, RETURN_MASK_ALL)
00666             {
00667               ui_out_text (out, ", ");
00668             }
00669           if (except.reason < 0)
00670             {
00671               Py_DECREF (item);
00672               gdbpy_convert_exception (except);
00673               goto error;
00674             }
00675         }
00676       else if (PyErr_Occurred ())
00677         goto error;
00678 
00679       TRY_CATCH (except, RETURN_MASK_ALL)
00680         {
00681           annotate_arg_end ();
00682         }
00683       if (except.reason < 0)
00684         {
00685           Py_DECREF (item);
00686           gdbpy_convert_exception (except);
00687           goto error;
00688         }
00689     }
00690 
00691   return PY_BT_OK;
00692 
00693  error:
00694   return PY_BT_ERROR;
00695 }
00696 
00697 
00698 /* Helper function to loop over variables provided by the
00699    "frame_locals" Python API.  Elements in the iterable must conform
00700    to the "Symbol Value" interface.  ITER is the Python iterable
00701    object, OUT is the output stream, INDENT is whether we should
00702    indent the output (for CLI), ARGS_TYPE is an enumerator describing
00703    the argument format, PRINT_ARGS_FIELD is flag which indicates
00704    whether to output the ARGS field in the case of
00705    -stack-list-variables and FRAME is the backing frame.  Returns
00706    PY_BT_ERROR on error, with any GDB exceptions converted to a Python
00707    exception, or PY_BT_OK on success.  */
00708 
00709 static enum py_bt_status
00710 enumerate_locals (PyObject *iter,
00711                   struct ui_out *out,
00712                   int indent,
00713                   enum py_frame_args args_type,
00714                   int print_args_field,
00715                   struct frame_info *frame)
00716 {
00717   PyObject *item;
00718   struct value_print_options opts;
00719 
00720   get_user_print_options (&opts);
00721   opts.deref_ref = 1;
00722 
00723   while ((item = PyIter_Next (iter)))
00724     {
00725       const struct language_defn *language;
00726       char *sym_name;
00727       struct value *val;
00728       enum py_bt_status  success = PY_BT_ERROR;
00729       struct symbol *sym;
00730       volatile struct gdb_exception except;
00731       int local_indent = 8 + (8 * indent);
00732       struct cleanup *locals_cleanups;
00733 
00734       locals_cleanups = make_cleanup_py_decref (item);
00735 
00736       success = extract_sym (item, &sym_name, &sym, &language);
00737       if (success == PY_BT_ERROR)
00738         {
00739           do_cleanups (locals_cleanups);
00740           goto error;
00741         }
00742 
00743       make_cleanup (xfree, sym_name);
00744 
00745       success = extract_value (item, &val);
00746       if (success == PY_BT_ERROR)
00747         {
00748           do_cleanups (locals_cleanups);
00749           goto error;
00750         }
00751 
00752       if (sym != NULL && ui_out_is_mi_like_p (out)
00753           && ! mi_should_print (sym, MI_PRINT_LOCALS))
00754         {
00755           do_cleanups (locals_cleanups);
00756           continue;
00757         }
00758 
00759       /* If the object did not provide a value, read it.  */
00760       if (val == NULL)
00761         {
00762           TRY_CATCH (except, RETURN_MASK_ALL)
00763             {
00764               val = read_var_value (sym, frame);
00765             }
00766           if (except.reason < 0)
00767             {
00768               gdbpy_convert_exception (except);
00769               do_cleanups (locals_cleanups);
00770               goto error;
00771             }
00772         }
00773 
00774       /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
00775          each output contains only one field.  The exception is
00776          -stack-list-variables, which always provides a tuple.  */
00777       if (ui_out_is_mi_like_p (out))
00778         {
00779           if (print_args_field || args_type != NO_VALUES)
00780             make_cleanup_ui_out_tuple_begin_end (out, NULL);
00781         }
00782       TRY_CATCH (except, RETURN_MASK_ALL)
00783         {
00784           if (! ui_out_is_mi_like_p (out))
00785             {
00786               /* If the output is not MI we indent locals.  */
00787               ui_out_spaces (out, local_indent);
00788             }
00789 
00790           ui_out_field_string (out, "name", sym_name);
00791 
00792           if (! ui_out_is_mi_like_p (out))
00793             ui_out_text (out, " = ");
00794         }
00795       if (except.reason < 0)
00796         {
00797           gdbpy_convert_exception (except);
00798           do_cleanups (locals_cleanups);
00799           goto error;
00800         }
00801 
00802       if (args_type == MI_PRINT_SIMPLE_VALUES)
00803         {
00804           if (py_print_type (out, val) == PY_BT_ERROR)
00805             {
00806               do_cleanups (locals_cleanups);
00807               goto error;
00808             }
00809         }
00810 
00811       /* CLI always prints values for locals.  MI uses the
00812          simple/no/all system.  */
00813       if (! ui_out_is_mi_like_p (out))
00814         {
00815           int val_indent = (indent + 1) * 4;
00816 
00817           if (py_print_value (out, val, &opts, val_indent, args_type,
00818                               language) ==  PY_BT_ERROR)
00819             {
00820               do_cleanups (locals_cleanups);
00821               goto error;
00822             }
00823         }
00824       else
00825         {
00826           if (args_type != NO_VALUES)
00827             {
00828               if (py_print_value (out, val, &opts, 0, args_type,
00829                                   language) ==  PY_BT_ERROR)
00830                 {
00831                   do_cleanups (locals_cleanups);
00832                   goto error;
00833                 }
00834             }
00835         }
00836 
00837       do_cleanups (locals_cleanups);
00838 
00839       TRY_CATCH (except, RETURN_MASK_ALL)
00840         {
00841           ui_out_text (out, "\n");
00842         }
00843       if (except.reason < 0)
00844         {
00845           gdbpy_convert_exception (except);
00846           goto error;
00847         }
00848     }
00849 
00850   if (item == NULL && PyErr_Occurred ())
00851     goto error;
00852 
00853   return PY_BT_OK;
00854 
00855  error:
00856   return PY_BT_ERROR;
00857 }
00858 
00859 /*  Helper function for -stack-list-variables.  Returns PY_BT_ERROR on
00860     error, or PY_BT_OK on success.  */
00861 
00862 static enum py_bt_status
00863 py_mi_print_variables (PyObject *filter, struct ui_out *out,
00864                        struct value_print_options *opts,
00865                        enum py_frame_args args_type,
00866                        struct frame_info *frame)
00867 {
00868   struct cleanup *old_chain;
00869   PyObject *args_iter;
00870   PyObject *locals_iter;
00871 
00872   args_iter = get_py_iter_from_func (filter, "frame_args");
00873   old_chain = make_cleanup_py_xdecref (args_iter);
00874   if (args_iter == NULL)
00875     goto error;
00876 
00877   locals_iter = get_py_iter_from_func (filter, "frame_locals");
00878   if (locals_iter == NULL)
00879     goto error;
00880 
00881   make_cleanup_py_decref (locals_iter);
00882   make_cleanup_ui_out_list_begin_end (out, "variables");
00883 
00884   if (args_iter != Py_None)
00885     if (enumerate_args (args_iter, out, args_type, 1, frame) == PY_BT_ERROR)
00886       goto error;
00887 
00888   if (locals_iter != Py_None)
00889     if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame)
00890         == PY_BT_ERROR)
00891       goto error;
00892 
00893   do_cleanups (old_chain);
00894   return PY_BT_OK;
00895 
00896  error:
00897   do_cleanups (old_chain);
00898   return PY_BT_ERROR;
00899 }
00900 
00901 /* Helper function for printing locals.  This function largely just
00902    creates the wrapping tuple, and calls enumerate_locals.  Returns
00903    PY_BT_ERROR on error, or PY_BT_OK on success.*/
00904 
00905 static enum py_bt_status
00906 py_print_locals (PyObject *filter,
00907                  struct ui_out *out,
00908                  enum py_frame_args args_type,
00909                  int indent,
00910                  struct frame_info *frame)
00911 {
00912   PyObject *locals_iter = get_py_iter_from_func (filter,
00913                                                  "frame_locals");
00914   struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter);
00915 
00916   if (locals_iter == NULL)
00917     goto locals_error;
00918 
00919   make_cleanup_ui_out_list_begin_end (out, "locals");
00920 
00921   if (locals_iter != Py_None)
00922     if (enumerate_locals (locals_iter, out, indent, args_type,
00923                           0, frame) == PY_BT_ERROR)
00924       goto locals_error;
00925 
00926   do_cleanups (old_chain);
00927   return PY_BT_OK;;
00928 
00929  locals_error:
00930   do_cleanups (old_chain);
00931   return PY_BT_ERROR;
00932 }
00933 
00934 /* Helper function for printing frame arguments.  This function
00935    largely just creates the wrapping tuple, and calls enumerate_args.
00936    Returns PY_BT_ERROR on error, with any GDB exceptions converted to
00937    a Python exception, or PY_BT_OK on success.  */
00938 
00939 static enum py_bt_status
00940 py_print_args (PyObject *filter,
00941                struct ui_out *out,
00942                enum py_frame_args args_type,
00943                struct frame_info *frame)
00944 {
00945   PyObject *args_iter  = get_py_iter_from_func (filter, "frame_args");
00946   struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter);
00947   volatile struct gdb_exception except;
00948 
00949   if (args_iter == NULL)
00950     goto args_error;
00951 
00952   make_cleanup_ui_out_list_begin_end (out, "args");
00953 
00954   TRY_CATCH (except, RETURN_MASK_ALL)
00955     {
00956       annotate_frame_args ();
00957       if (! ui_out_is_mi_like_p (out))
00958         ui_out_text (out, " (");
00959     }
00960   if (except.reason < 0)
00961     {
00962       gdbpy_convert_exception (except);
00963       goto args_error;
00964     }
00965 
00966   if (args_iter != Py_None)
00967     if (enumerate_args (args_iter, out, args_type, 0, frame) == PY_BT_ERROR)
00968       goto args_error;
00969 
00970   TRY_CATCH (except, RETURN_MASK_ALL)
00971     {
00972       if (! ui_out_is_mi_like_p (out))
00973         ui_out_text (out, ")");
00974     }
00975   if (except.reason < 0)
00976     {
00977       gdbpy_convert_exception (except);
00978       goto args_error;
00979     }
00980 
00981   do_cleanups (old_chain);
00982   return PY_BT_OK;
00983 
00984  args_error:
00985   do_cleanups (old_chain);
00986   return PY_BT_ERROR;
00987 }
00988 
00989 /*  Print a single frame to the designated output stream, detecting
00990     whether the output is MI or console, and formatting the output
00991     according to the conventions of that protocol.  FILTER is the
00992     frame-filter associated with this frame.  FLAGS is an integer
00993     describing the various print options.  The FLAGS variables is
00994     described in "apply_frame_filter" function.  ARGS_TYPE is an
00995     enumerator describing the argument format.  OUT is the output
00996     stream to print, INDENT is the level of indention for this frame
00997     (in the case of elided frames), and LEVELS_PRINTED is a hash-table
00998     containing all the frames level that have already been printed.
00999     If a frame level has been printed, do not print it again (in the
01000     case of elided frames).  Returns PY_BT_ERROR on error, with any
01001     GDB exceptions converted to a Python exception, or PY_BT_COMPLETED
01002     on success.  */
01003 
01004 static enum py_bt_status
01005 py_print_frame (PyObject *filter, int flags, enum py_frame_args args_type,
01006                 struct ui_out *out, int indent, htab_t levels_printed)
01007 {
01008   int has_addr = 0;
01009   CORE_ADDR address = 0;
01010   struct gdbarch *gdbarch = NULL;
01011   struct frame_info *frame = NULL;
01012   struct cleanup *cleanup_stack = make_cleanup (null_cleanup, NULL);
01013   struct value_print_options opts;
01014   PyObject *py_inf_frame, *elided;
01015   int print_level, print_frame_info, print_args, print_locals;
01016   volatile struct gdb_exception except;
01017 
01018   /* Extract print settings from FLAGS.  */
01019   print_level = (flags & PRINT_LEVEL) ? 1 : 0;
01020   print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0;
01021   print_args = (flags & PRINT_ARGS) ? 1 : 0;
01022   print_locals = (flags & PRINT_LOCALS) ? 1 : 0;
01023 
01024   get_user_print_options (&opts);
01025 
01026   /* Get the underlying frame.  This is needed to determine GDB
01027   architecture, and also, in the cases of frame variables/arguments to
01028   read them if they returned filter object requires us to do so.  */
01029   py_inf_frame = PyObject_CallMethod (filter, "inferior_frame", NULL);
01030   if (py_inf_frame == NULL)
01031     goto error;
01032 
01033   frame = frame_object_to_frame_info (py_inf_frame);;
01034 
01035   Py_DECREF (py_inf_frame);
01036 
01037   if (frame == NULL)
01038     goto error;
01039 
01040   TRY_CATCH (except, RETURN_MASK_ALL)
01041     {
01042       gdbarch = get_frame_arch (frame);
01043     }
01044   if (except.reason < 0)
01045     {
01046       gdbpy_convert_exception (except);
01047       goto error;
01048     }
01049 
01050 
01051   /* stack-list-variables.  */
01052   if (print_locals && print_args && ! print_frame_info)
01053     {
01054       if (py_mi_print_variables (filter, out, &opts,
01055                                  args_type, frame) == PY_BT_ERROR)
01056         goto error;
01057       else
01058         {
01059           do_cleanups (cleanup_stack);
01060           return PY_BT_COMPLETED;
01061         }
01062     }
01063 
01064   /* -stack-list-locals does not require a
01065      wrapping frame attribute.  */
01066   if (print_frame_info || (print_args && ! print_locals))
01067     make_cleanup_ui_out_tuple_begin_end (out, "frame");
01068 
01069   if (print_frame_info)
01070     {
01071       /* Elided frames are also printed with this function (recursively)
01072          and are printed with indention.  */
01073       if (indent > 0)
01074         {
01075         TRY_CATCH (except, RETURN_MASK_ALL)
01076           {
01077             ui_out_spaces (out, indent*4);
01078           }
01079         if (except.reason < 0)
01080           {
01081             gdbpy_convert_exception (except);
01082             goto error;
01083           }
01084         }
01085 
01086       /* The address is required for frame annotations, and also for
01087          address printing.  */
01088       if (PyObject_HasAttrString (filter, "address"))
01089         {
01090           PyObject *paddr = PyObject_CallMethod (filter, "address", NULL);
01091           if (paddr != NULL)
01092             {
01093               if (paddr != Py_None)
01094                 {
01095                   address = PyLong_AsLong (paddr);
01096                   has_addr = 1;
01097                 }
01098               Py_DECREF (paddr);
01099             }
01100           else
01101             goto error;
01102         }
01103     }
01104 
01105   /* Print frame level.  MI does not require the level if
01106      locals/variables only are being printed.  */
01107   if ((print_frame_info || print_args) && print_level)
01108     {
01109       struct frame_info **slot;
01110       int level;
01111       volatile struct gdb_exception except;
01112 
01113       slot = (struct frame_info **) htab_find_slot (levels_printed,
01114                                                     frame, INSERT);
01115       TRY_CATCH (except, RETURN_MASK_ALL)
01116         {
01117           level = frame_relative_level (frame);
01118 
01119           /* Check if this frame has already been printed (there are cases
01120              where elided synthetic dummy-frames have to 'borrow' the frame
01121              architecture from the eliding frame.  If that is the case, do
01122              not print 'level', but print spaces.  */
01123           if (*slot == frame)
01124             ui_out_field_skip (out, "level");
01125           else
01126             {
01127               *slot = frame;
01128               annotate_frame_begin (print_level ? level : 0,
01129                                     gdbarch, address);
01130               ui_out_text (out, "#");
01131               ui_out_field_fmt_int (out, 2, ui_left, "level",
01132                                     level);
01133             }
01134         }
01135       if (except.reason < 0)
01136         {
01137           gdbpy_convert_exception (except);
01138           goto error;
01139         }
01140     }
01141 
01142   if (print_frame_info)
01143     {
01144       /* Print address to the address field.  If an address is not provided,
01145          print nothing.  */
01146       if (opts.addressprint && has_addr)
01147         {
01148           TRY_CATCH (except, RETURN_MASK_ALL)
01149             {
01150               annotate_frame_address ();
01151               ui_out_field_core_addr (out, "addr", gdbarch, address);
01152               annotate_frame_address_end ();
01153               ui_out_text (out, " in ");
01154             }
01155           if (except.reason < 0)
01156             {
01157               gdbpy_convert_exception (except);
01158               goto error;
01159             }
01160         }
01161 
01162       /* Print frame function name.  */
01163       if (PyObject_HasAttrString (filter, "function"))
01164         {
01165           PyObject *py_func = PyObject_CallMethod (filter, "function", NULL);
01166 
01167           if (py_func != NULL)
01168             {
01169               const char *function = NULL;
01170 
01171               if (gdbpy_is_string (py_func))
01172                 {
01173                   char *function_to_free = NULL;
01174 
01175                   function = function_to_free =
01176                     python_string_to_host_string (py_func);
01177 
01178                   if (function == NULL)
01179                     {
01180                       Py_DECREF (py_func);
01181                       goto error;
01182                     }
01183                   make_cleanup (xfree, function_to_free);
01184                 }
01185               else if (PyLong_Check (py_func))
01186                 {
01187                   CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func);
01188                   struct bound_minimal_symbol msymbol;
01189 
01190                   if (PyErr_Occurred ())
01191                     goto error;
01192 
01193                   msymbol = lookup_minimal_symbol_by_pc (addr);
01194                   if (msymbol.minsym != NULL)
01195                     function = SYMBOL_PRINT_NAME (msymbol.minsym);
01196                 }
01197               else if (py_func != Py_None)
01198                 {
01199                   PyErr_SetString (PyExc_RuntimeError,
01200                                    _("FrameDecorator.function: expecting a " \
01201                                      "String, integer or None."));
01202                   Py_DECREF (py_func);
01203                   goto error;
01204                 }
01205 
01206 
01207               TRY_CATCH (except, RETURN_MASK_ALL)
01208                 {
01209                   annotate_frame_function_name ();
01210                   if (function == NULL)
01211                     ui_out_field_skip (out, "func");
01212                   else
01213                     ui_out_field_string (out, "func", function);
01214                 }
01215               if (except.reason < 0)
01216                 {
01217                   Py_DECREF (py_func);
01218                   gdbpy_convert_exception (except);
01219                   goto error;
01220                 }
01221             }
01222           Py_DECREF (py_func);
01223         }
01224       else
01225         goto error;
01226     }
01227 
01228 
01229   /* Frame arguments.  Check the result, and error if something went
01230      wrong.  */
01231   if (print_args)
01232     {
01233       if (py_print_args (filter, out, args_type, frame) == PY_BT_ERROR)
01234         goto error;
01235     }
01236 
01237   /* File name/source/line number information.  */
01238   if (print_frame_info)
01239     {
01240       TRY_CATCH (except, RETURN_MASK_ALL)
01241         {
01242           annotate_frame_source_begin ();
01243         }
01244       if (except.reason < 0)
01245         {
01246           gdbpy_convert_exception (except);
01247           goto error;
01248         }
01249 
01250       if (PyObject_HasAttrString (filter, "filename"))
01251         {
01252           PyObject *py_fn = PyObject_CallMethod (filter, "filename",
01253                                                  NULL);
01254           if (py_fn != NULL)
01255             {
01256               if (py_fn != Py_None)
01257                 {
01258                   char *filename = python_string_to_host_string (py_fn);
01259 
01260                   if (filename == NULL)
01261                     {
01262                       Py_DECREF (py_fn);
01263                       goto error;
01264                     }
01265 
01266                   make_cleanup (xfree, filename);
01267                   TRY_CATCH (except, RETURN_MASK_ALL)
01268                     {
01269                       ui_out_wrap_hint (out, "   ");
01270                       ui_out_text (out, " at ");
01271                       annotate_frame_source_file ();
01272                       ui_out_field_string (out, "file", filename);
01273                       annotate_frame_source_file_end ();
01274                     }
01275                   if (except.reason < 0)
01276                     {
01277                       Py_DECREF (py_fn);
01278                       gdbpy_convert_exception (except);
01279                       goto error;
01280                     }
01281                 }
01282               Py_DECREF (py_fn);
01283             }
01284           else
01285             goto error;
01286         }
01287 
01288       if (PyObject_HasAttrString (filter, "line"))
01289         {
01290           PyObject *py_line = PyObject_CallMethod (filter, "line", NULL);
01291           int line;
01292 
01293           if (py_line != NULL)
01294             {
01295               if (py_line != Py_None)
01296                 {
01297                   line = PyLong_AsLong (py_line);
01298                   TRY_CATCH (except, RETURN_MASK_ALL)
01299                     {
01300                       ui_out_text (out, ":");
01301                       annotate_frame_source_line ();
01302                       ui_out_field_int (out, "line", line);
01303                     }
01304                   if (except.reason < 0)
01305                     {
01306                       Py_DECREF (py_line);
01307                       gdbpy_convert_exception (except);
01308                       goto error;
01309                     }
01310                 }
01311               Py_DECREF (py_line);
01312             }
01313           else
01314             goto error;
01315         }
01316     }
01317 
01318   /* For MI we need to deal with the "children" list population of
01319      elided frames, so if MI output detected do not send newline.  */
01320   if (! ui_out_is_mi_like_p (out))
01321     {
01322       TRY_CATCH (except, RETURN_MASK_ALL)
01323         {
01324           annotate_frame_end ();
01325           ui_out_text (out, "\n");
01326         }
01327       if (except.reason < 0)
01328         {
01329           gdbpy_convert_exception (except);
01330           goto error;
01331         }
01332     }
01333 
01334   if (print_locals)
01335     {
01336       if (py_print_locals (filter, out, args_type, indent,
01337                            frame) == PY_BT_ERROR)
01338         goto error;
01339     }
01340 
01341   /* Finally recursively print elided frames, if any.  */
01342   elided  = get_py_iter_from_func (filter, "elided");
01343   if (elided == NULL)
01344     goto error;
01345 
01346   make_cleanup_py_decref (elided);
01347   if (elided != Py_None)
01348     {
01349       PyObject *item;
01350 
01351       make_cleanup_ui_out_list_begin_end (out, "children");
01352 
01353       if (! ui_out_is_mi_like_p (out))
01354         indent++;
01355 
01356       while ((item = PyIter_Next (elided)))
01357         {
01358           enum py_bt_status success = py_print_frame (item, flags,
01359                                                       args_type, out,
01360                                                       indent,
01361                                                       levels_printed);
01362 
01363           if (success == PY_BT_ERROR)
01364             {
01365               Py_DECREF (item);
01366               goto error;
01367             }
01368 
01369           Py_DECREF (item);
01370         }
01371       if (item == NULL && PyErr_Occurred ())
01372         goto error;
01373     }
01374 
01375 
01376   do_cleanups (cleanup_stack);
01377   return PY_BT_COMPLETED;
01378 
01379  error:
01380   do_cleanups (cleanup_stack);
01381   return PY_BT_ERROR;
01382 }
01383 
01384 /* Helper function to initiate frame filter invocation at starting
01385    frame FRAME.  */
01386 
01387 static PyObject *
01388 bootstrap_python_frame_filters (struct frame_info *frame,
01389                                 int frame_low, int frame_high)
01390 {
01391   struct cleanup *cleanups =
01392     make_cleanup (null_cleanup, NULL);
01393   PyObject *module, *sort_func, *iterable, *frame_obj, *iterator;
01394   PyObject *py_frame_low, *py_frame_high;
01395 
01396   frame_obj = frame_info_to_frame_object (frame);
01397   if (frame_obj == NULL)
01398     goto error;
01399   make_cleanup_py_decref (frame_obj);
01400 
01401   module = PyImport_ImportModule ("gdb.frames");
01402   if (module == NULL)
01403     goto error;
01404   make_cleanup_py_decref (module);
01405 
01406   sort_func = PyObject_GetAttrString (module, "execute_frame_filters");
01407   if (sort_func == NULL)
01408     goto error;
01409   make_cleanup_py_decref (sort_func);
01410 
01411   py_frame_low = PyInt_FromLong (frame_low);
01412   if (py_frame_low == NULL)
01413     goto error;
01414   make_cleanup_py_decref (py_frame_low);
01415 
01416   py_frame_high = PyInt_FromLong (frame_high);
01417   if (py_frame_high == NULL)
01418     goto error;
01419   make_cleanup_py_decref (py_frame_high);
01420 
01421   iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj,
01422                                            py_frame_low,
01423                                            py_frame_high,
01424                                            NULL);
01425   if (iterable == NULL)
01426     goto error;
01427 
01428   do_cleanups (cleanups);
01429 
01430   if (iterable != Py_None)
01431     {
01432       iterator = PyObject_GetIter (iterable);
01433       Py_DECREF (iterable);
01434     }
01435   else
01436     {
01437       return iterable;
01438     }
01439 
01440   return iterator;
01441 
01442  error:
01443   do_cleanups (cleanups);
01444   return NULL;
01445 }
01446 
01447 /*  This is the only publicly exported function in this file.  FRAME
01448     is the source frame to start frame-filter invocation.  FLAGS is an
01449     integer holding the flags for printing.  The following elements of
01450     the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
01451     PRINT_LEVEL is a flag indicating whether to print the frame's
01452     relative level in the output.  PRINT_FRAME_INFO is a flag that
01453     indicates whether this function should print the frame
01454     information, PRINT_ARGS is a flag that indicates whether to print
01455     frame arguments, and PRINT_LOCALS, likewise, with frame local
01456     variables.  ARGS_TYPE is an enumerator describing the argument
01457     format, OUT is the output stream to print.  FRAME_LOW is the
01458     beginning of the slice of frames to print, and FRAME_HIGH is the
01459     upper limit of the frames to count.  Returns PY_BT_ERROR on error,
01460     or PY_BT_COMPLETED on success.*/
01461 
01462 enum py_bt_status
01463 apply_frame_filter (struct frame_info *frame, int flags,
01464                     enum py_frame_args args_type,
01465                     struct ui_out *out, int frame_low,
01466                     int frame_high)
01467 
01468 {
01469   struct gdbarch *gdbarch = NULL;
01470   struct cleanup *cleanups;
01471   enum py_bt_status success = PY_BT_ERROR;
01472   PyObject *iterable;
01473   volatile struct gdb_exception except;
01474   PyObject *item;
01475   htab_t levels_printed;
01476 
01477   if (!gdb_python_initialized)
01478     return PY_BT_NO_FILTERS;
01479 
01480   cleanups = ensure_python_env (gdbarch, current_language);
01481 
01482   TRY_CATCH (except, RETURN_MASK_ALL)
01483     {
01484       gdbarch = get_frame_arch (frame);
01485     }
01486   if (except.reason < 0)
01487     {
01488       gdbpy_convert_exception (except);
01489       goto error;
01490     }
01491 
01492   iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);
01493 
01494   if (iterable == NULL)
01495     {
01496       /* Normally if there is an error GDB prints the exception,
01497          abandons the backtrace and exits.  The user can then call "bt
01498          no-filters", and get a default backtrace (it would be
01499          confusing to automatically start a standard backtrace halfway
01500          through a Python filtered backtrace).  However in the case
01501          where GDB cannot initialize the frame filters (most likely
01502          due to incorrect auto-load paths), GDB has printed nothing.
01503          In this case it is OK to print the default backtrace after
01504          printing the error message.  GDB returns PY_BT_NO_FILTERS
01505          here to signify there are no filters after printing the
01506          initialization error.  This return code will trigger a
01507          default backtrace.  */
01508 
01509       gdbpy_print_stack ();
01510       do_cleanups (cleanups);
01511       return PY_BT_NO_FILTERS;
01512     }
01513 
01514   /* If iterable is None, then there are no frame filters registered.
01515      If this is the case, defer to default GDB printing routines in MI
01516      and CLI.  */
01517   make_cleanup_py_decref (iterable);
01518   if (iterable == Py_None)
01519     {
01520       success = PY_BT_NO_FILTERS;
01521       goto done;
01522     }
01523 
01524   levels_printed = htab_create (20,
01525                                 htab_hash_pointer,
01526                                 htab_eq_pointer,
01527                                 NULL);
01528   make_cleanup_htab_delete (levels_printed);
01529 
01530   while ((item = PyIter_Next (iterable)))
01531     {
01532       success = py_print_frame (item, flags, args_type, out, 0,
01533                                 levels_printed);
01534 
01535       /* Do not exit on error printing a single frame.  Print the
01536          error and continue with other frames.  */
01537       if (success == PY_BT_ERROR)
01538         gdbpy_print_stack ();
01539 
01540       Py_DECREF (item);
01541     }
01542 
01543   if (item == NULL && PyErr_Occurred ())
01544     goto error;
01545 
01546  done:
01547   do_cleanups (cleanups);
01548   return success;
01549 
01550   /* Exit and abandon backtrace on error, printing the exception that
01551      is set.  */
01552  error:
01553   gdbpy_print_stack ();
01554   do_cleanups (cleanups);
01555   return PY_BT_ERROR;
01556 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines