xref: /dflybsd-src/contrib/gdb-7/gdb/varobj.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Implementation of the GDB variable objects API.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4*cf7f2e2dSJohn Marino    2009, 2010 Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert #include "defs.h"
205796c8dcSSimon Schubert #include "exceptions.h"
215796c8dcSSimon Schubert #include "value.h"
225796c8dcSSimon Schubert #include "expression.h"
235796c8dcSSimon Schubert #include "frame.h"
245796c8dcSSimon Schubert #include "language.h"
255796c8dcSSimon Schubert #include "wrapper.h"
265796c8dcSSimon Schubert #include "gdbcmd.h"
275796c8dcSSimon Schubert #include "block.h"
285796c8dcSSimon Schubert #include "valprint.h"
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert #include "gdb_assert.h"
315796c8dcSSimon Schubert #include "gdb_string.h"
325796c8dcSSimon Schubert #include "gdb_regex.h"
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert #include "varobj.h"
355796c8dcSSimon Schubert #include "vec.h"
365796c8dcSSimon Schubert #include "gdbthread.h"
375796c8dcSSimon Schubert #include "inferior.h"
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert #if HAVE_PYTHON
405796c8dcSSimon Schubert #include "python/python.h"
415796c8dcSSimon Schubert #include "python/python-internal.h"
425796c8dcSSimon Schubert #else
435796c8dcSSimon Schubert typedef int PyObject;
445796c8dcSSimon Schubert #endif
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* Non-zero if we want to see trace of varobj level stuff.  */
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert int varobjdebug = 0;
495796c8dcSSimon Schubert static void
505796c8dcSSimon Schubert show_varobjdebug (struct ui_file *file, int from_tty,
515796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
545796c8dcSSimon Schubert }
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert /* String representations of gdb's format codes */
575796c8dcSSimon Schubert char *varobj_format_string[] =
585796c8dcSSimon Schubert   { "natural", "binary", "decimal", "hexadecimal", "octal" };
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert /* String representations of gdb's known languages */
615796c8dcSSimon Schubert char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert /* True if we want to allow Python-based pretty-printing.  */
645796c8dcSSimon Schubert static int pretty_printing = 0;
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert void
675796c8dcSSimon Schubert varobj_enable_pretty_printing (void)
685796c8dcSSimon Schubert {
695796c8dcSSimon Schubert   pretty_printing = 1;
705796c8dcSSimon Schubert }
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Data structures */
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert /* Every root variable has one of these structures saved in its
755796c8dcSSimon Schubert    varobj. Members which must be free'd are noted. */
765796c8dcSSimon Schubert struct varobj_root
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert   /* Alloc'd expression for this parent. */
805796c8dcSSimon Schubert   struct expression *exp;
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert   /* Block for which this expression is valid */
835796c8dcSSimon Schubert   struct block *valid_block;
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert   /* The frame for this expression.  This field is set iff valid_block is
865796c8dcSSimon Schubert      not NULL.  */
875796c8dcSSimon Schubert   struct frame_id frame;
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert   /* The thread ID that this varobj_root belong to.  This field
905796c8dcSSimon Schubert      is only valid if valid_block is not NULL.
915796c8dcSSimon Schubert      When not 0, indicates which thread 'frame' belongs to.
925796c8dcSSimon Schubert      When 0, indicates that the thread list was empty when the varobj_root
935796c8dcSSimon Schubert      was created.  */
945796c8dcSSimon Schubert   int thread_id;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert   /* If 1, the -var-update always recomputes the value in the
975796c8dcSSimon Schubert      current thread and frame.  Otherwise, variable object is
985796c8dcSSimon Schubert      always updated in the specific scope/thread/frame  */
995796c8dcSSimon Schubert   int floating;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert   /* Flag that indicates validity: set to 0 when this varobj_root refers
1025796c8dcSSimon Schubert      to symbols that do not exist anymore.  */
1035796c8dcSSimon Schubert   int is_valid;
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert   /* Language info for this variable and its children */
1065796c8dcSSimon Schubert   struct language_specific *lang;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   /* The varobj for this root node. */
1095796c8dcSSimon Schubert   struct varobj *rootvar;
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert   /* Next root variable */
1125796c8dcSSimon Schubert   struct varobj_root *next;
1135796c8dcSSimon Schubert };
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert /* Every variable in the system has a structure of this type defined
1165796c8dcSSimon Schubert    for it. This structure holds all information necessary to manipulate
1175796c8dcSSimon Schubert    a particular object variable. Members which must be freed are noted. */
1185796c8dcSSimon Schubert struct varobj
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert   /* Alloc'd name of the variable for this object.. If this variable is a
1225796c8dcSSimon Schubert      child, then this name will be the child's source name.
1235796c8dcSSimon Schubert      (bar, not foo.bar) */
1245796c8dcSSimon Schubert   /* NOTE: This is the "expression" */
1255796c8dcSSimon Schubert   char *name;
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert   /* Alloc'd expression for this child.  Can be used to create a
1285796c8dcSSimon Schubert      root variable corresponding to this child.  */
1295796c8dcSSimon Schubert   char *path_expr;
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert   /* The alloc'd name for this variable's object. This is here for
1325796c8dcSSimon Schubert      convenience when constructing this object's children. */
1335796c8dcSSimon Schubert   char *obj_name;
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert   /* Index of this variable in its parent or -1 */
1365796c8dcSSimon Schubert   int index;
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   /* The type of this variable.  This can be NULL
1395796c8dcSSimon Schubert      for artifial variable objects -- currently, the "accessibility"
1405796c8dcSSimon Schubert      variable objects in C++.  */
1415796c8dcSSimon Schubert   struct type *type;
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert   /* The value of this expression or subexpression.  A NULL value
1445796c8dcSSimon Schubert      indicates there was an error getting this value.
1455796c8dcSSimon Schubert      Invariant: if varobj_value_is_changeable_p (this) is non-zero,
1465796c8dcSSimon Schubert      the value is either NULL, or not lazy.  */
1475796c8dcSSimon Schubert   struct value *value;
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   /* The number of (immediate) children this variable has */
1505796c8dcSSimon Schubert   int num_children;
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert   /* If this object is a child, this points to its immediate parent. */
1535796c8dcSSimon Schubert   struct varobj *parent;
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   /* Children of this object.  */
1565796c8dcSSimon Schubert   VEC (varobj_p) *children;
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   /* Whether the children of this varobj were requested.  This field is
1595796c8dcSSimon Schubert      used to decide if dynamic varobj should recompute their children.
1605796c8dcSSimon Schubert      In the event that the frontend never asked for the children, we
1615796c8dcSSimon Schubert      can avoid that.  */
1625796c8dcSSimon Schubert   int children_requested;
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert   /* Description of the root variable. Points to root variable for children. */
1655796c8dcSSimon Schubert   struct varobj_root *root;
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   /* The format of the output for this object */
1685796c8dcSSimon Schubert   enum varobj_display_formats format;
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert   /* Was this variable updated via a varobj_set_value operation */
1715796c8dcSSimon Schubert   int updated;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert   /* Last print value.  */
1745796c8dcSSimon Schubert   char *print_value;
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert   /* Is this variable frozen.  Frozen variables are never implicitly
1775796c8dcSSimon Schubert      updated by -var-update *
1785796c8dcSSimon Schubert      or -var-update <direct-or-indirect-parent>.  */
1795796c8dcSSimon Schubert   int frozen;
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   /* Is the value of this variable intentionally not fetched?  It is
1825796c8dcSSimon Schubert      not fetched if either the variable is frozen, or any parents is
1835796c8dcSSimon Schubert      frozen.  */
1845796c8dcSSimon Schubert   int not_fetched;
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert   /* Sub-range of children which the MI consumer has requested.  If
1875796c8dcSSimon Schubert      FROM < 0 or TO < 0, means that all children have been
1885796c8dcSSimon Schubert      requested.  */
1895796c8dcSSimon Schubert   int from;
1905796c8dcSSimon Schubert   int to;
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert   /* The pretty-printer constructor.  If NULL, then the default
1935796c8dcSSimon Schubert      pretty-printer will be looked up.  If None, then no
1945796c8dcSSimon Schubert      pretty-printer will be installed.  */
1955796c8dcSSimon Schubert   PyObject *constructor;
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert   /* The pretty-printer that has been constructed.  If NULL, then a
1985796c8dcSSimon Schubert      new printer object is needed, and one will be constructed.  */
1995796c8dcSSimon Schubert   PyObject *pretty_printer;
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert   /* The iterator returned by the printer's 'children' method, or NULL
2025796c8dcSSimon Schubert      if not available.  */
2035796c8dcSSimon Schubert   PyObject *child_iter;
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert   /* We request one extra item from the iterator, so that we can
2065796c8dcSSimon Schubert      report to the caller whether there are more items than we have
2075796c8dcSSimon Schubert      already reported.  However, we don't want to install this value
2085796c8dcSSimon Schubert      when we read it, because that will mess up future updates.  So,
2095796c8dcSSimon Schubert      we stash it here instead.  */
2105796c8dcSSimon Schubert   PyObject *saved_item;
2115796c8dcSSimon Schubert };
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert struct cpstack
2145796c8dcSSimon Schubert {
2155796c8dcSSimon Schubert   char *name;
2165796c8dcSSimon Schubert   struct cpstack *next;
2175796c8dcSSimon Schubert };
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert /* A list of varobjs */
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert struct vlist
2225796c8dcSSimon Schubert {
2235796c8dcSSimon Schubert   struct varobj *var;
2245796c8dcSSimon Schubert   struct vlist *next;
2255796c8dcSSimon Schubert };
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert /* Private function prototypes */
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert /* Helper functions for the above subcommands. */
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert static int delete_variable (struct cpstack **, struct varobj *, int);
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert static void delete_variable_1 (struct cpstack **, int *,
2345796c8dcSSimon Schubert 			       struct varobj *, int, int);
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert static int install_variable (struct varobj *);
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert static void uninstall_variable (struct varobj *);
2395796c8dcSSimon Schubert 
2405796c8dcSSimon Schubert static struct varobj *create_child (struct varobj *, int, char *);
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert static struct varobj *
2435796c8dcSSimon Schubert create_child_with_value (struct varobj *parent, int index, const char *name,
2445796c8dcSSimon Schubert 			 struct value *value);
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert /* Utility routines */
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert static struct varobj *new_variable (void);
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert static struct varobj *new_root_variable (void);
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert static void free_variable (struct varobj *var);
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert static struct cleanup *make_cleanup_free_variable (struct varobj *var);
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert static struct type *get_type (struct varobj *var);
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert static struct type *get_value_type (struct varobj *var);
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert static struct type *get_target_type (struct type *);
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert static enum varobj_display_formats variable_default_display (struct varobj *);
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert static void cppush (struct cpstack **pstack, char *name);
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert static char *cppop (struct cpstack **pstack);
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert static int install_new_value (struct varobj *var, struct value *value,
2695796c8dcSSimon Schubert 			      int initial);
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert /* Language-specific routines. */
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert static enum varobj_languages variable_language (struct varobj *var);
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert static int number_of_children (struct varobj *);
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert static char *name_of_variable (struct varobj *);
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert static char *name_of_child (struct varobj *, int);
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert static struct value *value_of_root (struct varobj **var_handle, int *);
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert static struct value *value_of_child (struct varobj *parent, int index);
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert static char *my_value_of_variable (struct varobj *var,
2865796c8dcSSimon Schubert 				   enum varobj_display_formats format);
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert static char *value_get_print_value (struct value *value,
2895796c8dcSSimon Schubert 				    enum varobj_display_formats format,
2905796c8dcSSimon Schubert 				    struct varobj *var);
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert static int varobj_value_is_changeable_p (struct varobj *var);
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert static int is_root_p (struct varobj *var);
2955796c8dcSSimon Schubert 
296*cf7f2e2dSJohn Marino #if HAVE_PYTHON
297*cf7f2e2dSJohn Marino 
2985796c8dcSSimon Schubert static struct varobj *
2995796c8dcSSimon Schubert varobj_add_child (struct varobj *var, const char *name, struct value *value);
3005796c8dcSSimon Schubert 
301*cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
302*cf7f2e2dSJohn Marino 
3035796c8dcSSimon Schubert /* C implementation */
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert static int c_number_of_children (struct varobj *var);
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert static char *c_name_of_variable (struct varobj *parent);
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert static char *c_name_of_child (struct varobj *parent, int index);
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert static char *c_path_expr_of_child (struct varobj *child);
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert static struct value *c_value_of_root (struct varobj **var_handle);
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert static struct value *c_value_of_child (struct varobj *parent, int index);
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert static struct type *c_type_of_child (struct varobj *parent, int index);
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert static char *c_value_of_variable (struct varobj *var,
3205796c8dcSSimon Schubert 				  enum varobj_display_formats format);
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert /* C++ implementation */
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert static int cplus_number_of_children (struct varobj *var);
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert static void cplus_class_num_children (struct type *type, int children[3]);
3275796c8dcSSimon Schubert 
3285796c8dcSSimon Schubert static char *cplus_name_of_variable (struct varobj *parent);
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert static char *cplus_name_of_child (struct varobj *parent, int index);
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert static char *cplus_path_expr_of_child (struct varobj *child);
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert static struct value *cplus_value_of_root (struct varobj **var_handle);
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert static struct value *cplus_value_of_child (struct varobj *parent, int index);
3375796c8dcSSimon Schubert 
3385796c8dcSSimon Schubert static struct type *cplus_type_of_child (struct varobj *parent, int index);
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert static char *cplus_value_of_variable (struct varobj *var,
3415796c8dcSSimon Schubert 				      enum varobj_display_formats format);
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert /* Java implementation */
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert static int java_number_of_children (struct varobj *var);
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert static char *java_name_of_variable (struct varobj *parent);
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert static char *java_name_of_child (struct varobj *parent, int index);
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert static char *java_path_expr_of_child (struct varobj *child);
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert static struct value *java_value_of_root (struct varobj **var_handle);
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert static struct value *java_value_of_child (struct varobj *parent, int index);
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert static struct type *java_type_of_child (struct varobj *parent, int index);
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert static char *java_value_of_variable (struct varobj *var,
3605796c8dcSSimon Schubert 				     enum varobj_display_formats format);
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert /* The language specific vector */
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert struct language_specific
3655796c8dcSSimon Schubert {
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert   /* The language of this variable */
3685796c8dcSSimon Schubert   enum varobj_languages language;
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   /* The number of children of PARENT. */
3715796c8dcSSimon Schubert   int (*number_of_children) (struct varobj * parent);
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert   /* The name (expression) of a root varobj. */
3745796c8dcSSimon Schubert   char *(*name_of_variable) (struct varobj * parent);
3755796c8dcSSimon Schubert 
3765796c8dcSSimon Schubert   /* The name of the INDEX'th child of PARENT. */
3775796c8dcSSimon Schubert   char *(*name_of_child) (struct varobj * parent, int index);
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert   /* Returns the rooted expression of CHILD, which is a variable
3805796c8dcSSimon Schubert      obtain that has some parent.  */
3815796c8dcSSimon Schubert   char *(*path_expr_of_child) (struct varobj * child);
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert   /* The ``struct value *'' of the root variable ROOT. */
3845796c8dcSSimon Schubert   struct value *(*value_of_root) (struct varobj ** root_handle);
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   /* The ``struct value *'' of the INDEX'th child of PARENT. */
3875796c8dcSSimon Schubert   struct value *(*value_of_child) (struct varobj * parent, int index);
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert   /* The type of the INDEX'th child of PARENT. */
3905796c8dcSSimon Schubert   struct type *(*type_of_child) (struct varobj * parent, int index);
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert   /* The current value of VAR. */
3935796c8dcSSimon Schubert   char *(*value_of_variable) (struct varobj * var,
3945796c8dcSSimon Schubert 			      enum varobj_display_formats format);
3955796c8dcSSimon Schubert };
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert /* Array of known source language routines. */
3985796c8dcSSimon Schubert static struct language_specific languages[vlang_end] = {
3995796c8dcSSimon Schubert   /* Unknown (try treating as C */
4005796c8dcSSimon Schubert   {
4015796c8dcSSimon Schubert    vlang_unknown,
4025796c8dcSSimon Schubert    c_number_of_children,
4035796c8dcSSimon Schubert    c_name_of_variable,
4045796c8dcSSimon Schubert    c_name_of_child,
4055796c8dcSSimon Schubert    c_path_expr_of_child,
4065796c8dcSSimon Schubert    c_value_of_root,
4075796c8dcSSimon Schubert    c_value_of_child,
4085796c8dcSSimon Schubert    c_type_of_child,
4095796c8dcSSimon Schubert    c_value_of_variable}
4105796c8dcSSimon Schubert   ,
4115796c8dcSSimon Schubert   /* C */
4125796c8dcSSimon Schubert   {
4135796c8dcSSimon Schubert    vlang_c,
4145796c8dcSSimon Schubert    c_number_of_children,
4155796c8dcSSimon Schubert    c_name_of_variable,
4165796c8dcSSimon Schubert    c_name_of_child,
4175796c8dcSSimon Schubert    c_path_expr_of_child,
4185796c8dcSSimon Schubert    c_value_of_root,
4195796c8dcSSimon Schubert    c_value_of_child,
4205796c8dcSSimon Schubert    c_type_of_child,
4215796c8dcSSimon Schubert    c_value_of_variable}
4225796c8dcSSimon Schubert   ,
4235796c8dcSSimon Schubert   /* C++ */
4245796c8dcSSimon Schubert   {
4255796c8dcSSimon Schubert    vlang_cplus,
4265796c8dcSSimon Schubert    cplus_number_of_children,
4275796c8dcSSimon Schubert    cplus_name_of_variable,
4285796c8dcSSimon Schubert    cplus_name_of_child,
4295796c8dcSSimon Schubert    cplus_path_expr_of_child,
4305796c8dcSSimon Schubert    cplus_value_of_root,
4315796c8dcSSimon Schubert    cplus_value_of_child,
4325796c8dcSSimon Schubert    cplus_type_of_child,
4335796c8dcSSimon Schubert    cplus_value_of_variable}
4345796c8dcSSimon Schubert   ,
4355796c8dcSSimon Schubert   /* Java */
4365796c8dcSSimon Schubert   {
4375796c8dcSSimon Schubert    vlang_java,
4385796c8dcSSimon Schubert    java_number_of_children,
4395796c8dcSSimon Schubert    java_name_of_variable,
4405796c8dcSSimon Schubert    java_name_of_child,
4415796c8dcSSimon Schubert    java_path_expr_of_child,
4425796c8dcSSimon Schubert    java_value_of_root,
4435796c8dcSSimon Schubert    java_value_of_child,
4445796c8dcSSimon Schubert    java_type_of_child,
4455796c8dcSSimon Schubert    java_value_of_variable}
4465796c8dcSSimon Schubert };
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert /* A little convenience enum for dealing with C++/Java */
4495796c8dcSSimon Schubert enum vsections
4505796c8dcSSimon Schubert {
4515796c8dcSSimon Schubert   v_public = 0, v_private, v_protected
4525796c8dcSSimon Schubert };
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert /* Private data */
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert /* Mappings of varobj_display_formats enums to gdb's format codes */
4575796c8dcSSimon Schubert static int format_code[] = { 0, 't', 'd', 'x', 'o' };
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert /* Header of the list of root variable objects */
4605796c8dcSSimon Schubert static struct varobj_root *rootlist;
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert /* Prime number indicating the number of buckets in the hash table */
4635796c8dcSSimon Schubert /* A prime large enough to avoid too many colisions */
4645796c8dcSSimon Schubert #define VAROBJ_TABLE_SIZE 227
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert /* Pointer to the varobj hash table (built at run time) */
4675796c8dcSSimon Schubert static struct vlist **varobj_table;
4685796c8dcSSimon Schubert 
4695796c8dcSSimon Schubert /* Is the variable X one of our "fake" children? */
4705796c8dcSSimon Schubert #define CPLUS_FAKE_CHILD(x) \
4715796c8dcSSimon Schubert ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert /* API Implementation */
4755796c8dcSSimon Schubert static int
4765796c8dcSSimon Schubert is_root_p (struct varobj *var)
4775796c8dcSSimon Schubert {
4785796c8dcSSimon Schubert   return (var->root->rootvar == var);
4795796c8dcSSimon Schubert }
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert #ifdef HAVE_PYTHON
4825796c8dcSSimon Schubert /* Helper function to install a Python environment suitable for
4835796c8dcSSimon Schubert    use during operations on VAR.  */
4845796c8dcSSimon Schubert struct cleanup *
4855796c8dcSSimon Schubert varobj_ensure_python_env (struct varobj *var)
4865796c8dcSSimon Schubert {
4875796c8dcSSimon Schubert   return ensure_python_env (var->root->exp->gdbarch,
4885796c8dcSSimon Schubert 			    var->root->exp->language_defn);
4895796c8dcSSimon Schubert }
4905796c8dcSSimon Schubert #endif
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert /* Creates a varobj (not its children) */
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert /* Return the full FRAME which corresponds to the given CORE_ADDR
4955796c8dcSSimon Schubert    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert static struct frame_info *
4985796c8dcSSimon Schubert find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
4995796c8dcSSimon Schubert {
5005796c8dcSSimon Schubert   struct frame_info *frame = NULL;
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert   if (frame_addr == (CORE_ADDR) 0)
5035796c8dcSSimon Schubert     return NULL;
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert   for (frame = get_current_frame ();
5065796c8dcSSimon Schubert        frame != NULL;
5075796c8dcSSimon Schubert        frame = get_prev_frame (frame))
5085796c8dcSSimon Schubert     {
5095796c8dcSSimon Schubert       /* The CORE_ADDR we get as argument was parsed from a string GDB
5105796c8dcSSimon Schubert 	 output as $fp.  This output got truncated to gdbarch_addr_bit.
5115796c8dcSSimon Schubert 	 Truncate the frame base address in the same manner before
5125796c8dcSSimon Schubert 	 comparing it against our argument.  */
5135796c8dcSSimon Schubert       CORE_ADDR frame_base = get_frame_base_address (frame);
5145796c8dcSSimon Schubert       int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
515*cf7f2e2dSJohn Marino 
5165796c8dcSSimon Schubert       if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
5175796c8dcSSimon Schubert 	frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert       if (frame_base == frame_addr)
5205796c8dcSSimon Schubert 	return frame;
5215796c8dcSSimon Schubert     }
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert   return NULL;
5245796c8dcSSimon Schubert }
5255796c8dcSSimon Schubert 
5265796c8dcSSimon Schubert struct varobj *
5275796c8dcSSimon Schubert varobj_create (char *objname,
5285796c8dcSSimon Schubert 	       char *expression, CORE_ADDR frame, enum varobj_type type)
5295796c8dcSSimon Schubert {
5305796c8dcSSimon Schubert   struct varobj *var;
5315796c8dcSSimon Schubert   struct frame_info *fi;
5325796c8dcSSimon Schubert   struct frame_info *old_fi = NULL;
5335796c8dcSSimon Schubert   struct block *block;
5345796c8dcSSimon Schubert   struct cleanup *old_chain;
5355796c8dcSSimon Schubert 
5365796c8dcSSimon Schubert   /* Fill out a varobj structure for the (root) variable being constructed. */
5375796c8dcSSimon Schubert   var = new_root_variable ();
5385796c8dcSSimon Schubert   old_chain = make_cleanup_free_variable (var);
5395796c8dcSSimon Schubert 
5405796c8dcSSimon Schubert   if (expression != NULL)
5415796c8dcSSimon Schubert     {
5425796c8dcSSimon Schubert       char *p;
5435796c8dcSSimon Schubert       enum varobj_languages lang;
5445796c8dcSSimon Schubert       struct value *value = NULL;
5455796c8dcSSimon Schubert 
5465796c8dcSSimon Schubert       /* Parse and evaluate the expression, filling in as much of the
5475796c8dcSSimon Schubert          variable's data as possible.  */
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert       if (has_stack_frames ())
5505796c8dcSSimon Schubert 	{
5515796c8dcSSimon Schubert 	  /* Allow creator to specify context of variable */
5525796c8dcSSimon Schubert 	  if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
5535796c8dcSSimon Schubert 	    fi = get_selected_frame (NULL);
5545796c8dcSSimon Schubert 	  else
5555796c8dcSSimon Schubert 	    /* FIXME: cagney/2002-11-23: This code should be doing a
5565796c8dcSSimon Schubert 	       lookup using the frame ID and not just the frame's
5575796c8dcSSimon Schubert 	       ``address''.  This, of course, means an interface
5585796c8dcSSimon Schubert 	       change.  However, with out that interface change ISAs,
5595796c8dcSSimon Schubert 	       such as the ia64 with its two stacks, won't work.
5605796c8dcSSimon Schubert 	       Similar goes for the case where there is a frameless
5615796c8dcSSimon Schubert 	       function.  */
5625796c8dcSSimon Schubert 	    fi = find_frame_addr_in_frame_chain (frame);
5635796c8dcSSimon Schubert 	}
5645796c8dcSSimon Schubert       else
5655796c8dcSSimon Schubert 	fi = NULL;
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert       /* frame = -2 means always use selected frame */
5685796c8dcSSimon Schubert       if (type == USE_SELECTED_FRAME)
5695796c8dcSSimon Schubert 	var->root->floating = 1;
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert       block = NULL;
5725796c8dcSSimon Schubert       if (fi != NULL)
5735796c8dcSSimon Schubert 	block = get_frame_block (fi, 0);
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert       p = expression;
5765796c8dcSSimon Schubert       innermost_block = NULL;
5775796c8dcSSimon Schubert       /* Wrap the call to parse expression, so we can
5785796c8dcSSimon Schubert          return a sensible error. */
5795796c8dcSSimon Schubert       if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
5805796c8dcSSimon Schubert 	{
5815796c8dcSSimon Schubert 	  return NULL;
5825796c8dcSSimon Schubert 	}
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert       /* Don't allow variables to be created for types. */
5855796c8dcSSimon Schubert       if (var->root->exp->elts[0].opcode == OP_TYPE)
5865796c8dcSSimon Schubert 	{
5875796c8dcSSimon Schubert 	  do_cleanups (old_chain);
5885796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
5895796c8dcSSimon Schubert 			      " as an expression.\n");
5905796c8dcSSimon Schubert 	  return NULL;
5915796c8dcSSimon Schubert 	}
5925796c8dcSSimon Schubert 
5935796c8dcSSimon Schubert       var->format = variable_default_display (var);
5945796c8dcSSimon Schubert       var->root->valid_block = innermost_block;
5955796c8dcSSimon Schubert       var->name = xstrdup (expression);
5965796c8dcSSimon Schubert       /* For a root var, the name and the expr are the same.  */
5975796c8dcSSimon Schubert       var->path_expr = xstrdup (expression);
5985796c8dcSSimon Schubert 
5995796c8dcSSimon Schubert       /* When the frame is different from the current frame,
6005796c8dcSSimon Schubert          we must select the appropriate frame before parsing
6015796c8dcSSimon Schubert          the expression, otherwise the value will not be current.
6025796c8dcSSimon Schubert          Since select_frame is so benign, just call it for all cases. */
6035796c8dcSSimon Schubert       if (innermost_block)
6045796c8dcSSimon Schubert 	{
6055796c8dcSSimon Schubert 	  /* User could specify explicit FRAME-ADDR which was not found but
6065796c8dcSSimon Schubert 	     EXPRESSION is frame specific and we would not be able to evaluate
6075796c8dcSSimon Schubert 	     it correctly next time.  With VALID_BLOCK set we must also set
6085796c8dcSSimon Schubert 	     FRAME and THREAD_ID.  */
6095796c8dcSSimon Schubert 	  if (fi == NULL)
6105796c8dcSSimon Schubert 	    error (_("Failed to find the specified frame"));
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert 	  var->root->frame = get_frame_id (fi);
6135796c8dcSSimon Schubert 	  var->root->thread_id = pid_to_thread_id (inferior_ptid);
6145796c8dcSSimon Schubert 	  old_fi = get_selected_frame (NULL);
6155796c8dcSSimon Schubert 	  select_frame (fi);
6165796c8dcSSimon Schubert 	}
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert       /* We definitely need to catch errors here.
6195796c8dcSSimon Schubert          If evaluate_expression succeeds we got the value we wanted.
6205796c8dcSSimon Schubert          But if it fails, we still go on with a call to evaluate_type()  */
6215796c8dcSSimon Schubert       if (!gdb_evaluate_expression (var->root->exp, &value))
6225796c8dcSSimon Schubert 	{
6235796c8dcSSimon Schubert 	  /* Error getting the value.  Try to at least get the
6245796c8dcSSimon Schubert 	     right type.  */
6255796c8dcSSimon Schubert 	  struct value *type_only_value = evaluate_type (var->root->exp);
626*cf7f2e2dSJohn Marino 
6275796c8dcSSimon Schubert 	  var->type = value_type (type_only_value);
6285796c8dcSSimon Schubert 	}
6295796c8dcSSimon Schubert       else
6305796c8dcSSimon Schubert 	var->type = value_type (value);
6315796c8dcSSimon Schubert 
6325796c8dcSSimon Schubert       install_new_value (var, value, 1 /* Initial assignment */);
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert       /* Set language info */
6355796c8dcSSimon Schubert       lang = variable_language (var);
6365796c8dcSSimon Schubert       var->root->lang = &languages[lang];
6375796c8dcSSimon Schubert 
6385796c8dcSSimon Schubert       /* Set ourselves as our root */
6395796c8dcSSimon Schubert       var->root->rootvar = var;
6405796c8dcSSimon Schubert 
6415796c8dcSSimon Schubert       /* Reset the selected frame */
6425796c8dcSSimon Schubert       if (old_fi != NULL)
6435796c8dcSSimon Schubert 	select_frame (old_fi);
6445796c8dcSSimon Schubert     }
6455796c8dcSSimon Schubert 
6465796c8dcSSimon Schubert   /* If the variable object name is null, that means this
6475796c8dcSSimon Schubert      is a temporary variable, so don't install it. */
6485796c8dcSSimon Schubert 
6495796c8dcSSimon Schubert   if ((var != NULL) && (objname != NULL))
6505796c8dcSSimon Schubert     {
6515796c8dcSSimon Schubert       var->obj_name = xstrdup (objname);
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert       /* If a varobj name is duplicated, the install will fail so
6545796c8dcSSimon Schubert          we must clenup */
6555796c8dcSSimon Schubert       if (!install_variable (var))
6565796c8dcSSimon Schubert 	{
6575796c8dcSSimon Schubert 	  do_cleanups (old_chain);
6585796c8dcSSimon Schubert 	  return NULL;
6595796c8dcSSimon Schubert 	}
6605796c8dcSSimon Schubert     }
6615796c8dcSSimon Schubert 
6625796c8dcSSimon Schubert   discard_cleanups (old_chain);
6635796c8dcSSimon Schubert   return var;
6645796c8dcSSimon Schubert }
6655796c8dcSSimon Schubert 
6665796c8dcSSimon Schubert /* Generates an unique name that can be used for a varobj */
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert char *
6695796c8dcSSimon Schubert varobj_gen_name (void)
6705796c8dcSSimon Schubert {
6715796c8dcSSimon Schubert   static int id = 0;
6725796c8dcSSimon Schubert   char *obj_name;
6735796c8dcSSimon Schubert 
6745796c8dcSSimon Schubert   /* generate a name for this object */
6755796c8dcSSimon Schubert   id++;
6765796c8dcSSimon Schubert   obj_name = xstrprintf ("var%d", id);
6775796c8dcSSimon Schubert 
6785796c8dcSSimon Schubert   return obj_name;
6795796c8dcSSimon Schubert }
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert /* Given an OBJNAME, returns the pointer to the corresponding varobj.  Call
6825796c8dcSSimon Schubert    error if OBJNAME cannot be found.  */
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert struct varobj *
6855796c8dcSSimon Schubert varobj_get_handle (char *objname)
6865796c8dcSSimon Schubert {
6875796c8dcSSimon Schubert   struct vlist *cv;
6885796c8dcSSimon Schubert   const char *chp;
6895796c8dcSSimon Schubert   unsigned int index = 0;
6905796c8dcSSimon Schubert   unsigned int i = 1;
6915796c8dcSSimon Schubert 
6925796c8dcSSimon Schubert   for (chp = objname; *chp; chp++)
6935796c8dcSSimon Schubert     {
6945796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
6955796c8dcSSimon Schubert     }
6965796c8dcSSimon Schubert 
6975796c8dcSSimon Schubert   cv = *(varobj_table + index);
6985796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
6995796c8dcSSimon Schubert     cv = cv->next;
7005796c8dcSSimon Schubert 
7015796c8dcSSimon Schubert   if (cv == NULL)
7025796c8dcSSimon Schubert     error (_("Variable object not found"));
7035796c8dcSSimon Schubert 
7045796c8dcSSimon Schubert   return cv->var;
7055796c8dcSSimon Schubert }
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert /* Given the handle, return the name of the object */
7085796c8dcSSimon Schubert 
7095796c8dcSSimon Schubert char *
7105796c8dcSSimon Schubert varobj_get_objname (struct varobj *var)
7115796c8dcSSimon Schubert {
7125796c8dcSSimon Schubert   return var->obj_name;
7135796c8dcSSimon Schubert }
7145796c8dcSSimon Schubert 
7155796c8dcSSimon Schubert /* Given the handle, return the expression represented by the object */
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert char *
7185796c8dcSSimon Schubert varobj_get_expression (struct varobj *var)
7195796c8dcSSimon Schubert {
7205796c8dcSSimon Schubert   return name_of_variable (var);
7215796c8dcSSimon Schubert }
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert /* Deletes a varobj and all its children if only_children == 0,
7245796c8dcSSimon Schubert    otherwise deletes only the children; returns a malloc'ed list of all the
7255796c8dcSSimon Schubert    (malloc'ed) names of the variables that have been deleted (NULL terminated) */
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert int
7285796c8dcSSimon Schubert varobj_delete (struct varobj *var, char ***dellist, int only_children)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert   int delcount;
7315796c8dcSSimon Schubert   int mycount;
7325796c8dcSSimon Schubert   struct cpstack *result = NULL;
7335796c8dcSSimon Schubert   char **cp;
7345796c8dcSSimon Schubert 
7355796c8dcSSimon Schubert   /* Initialize a stack for temporary results */
7365796c8dcSSimon Schubert   cppush (&result, NULL);
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert   if (only_children)
7395796c8dcSSimon Schubert     /* Delete only the variable children */
7405796c8dcSSimon Schubert     delcount = delete_variable (&result, var, 1 /* only the children */ );
7415796c8dcSSimon Schubert   else
7425796c8dcSSimon Schubert     /* Delete the variable and all its children */
7435796c8dcSSimon Schubert     delcount = delete_variable (&result, var, 0 /* parent+children */ );
7445796c8dcSSimon Schubert 
7455796c8dcSSimon Schubert   /* We may have been asked to return a list of what has been deleted */
7465796c8dcSSimon Schubert   if (dellist != NULL)
7475796c8dcSSimon Schubert     {
7485796c8dcSSimon Schubert       *dellist = xmalloc ((delcount + 1) * sizeof (char *));
7495796c8dcSSimon Schubert 
7505796c8dcSSimon Schubert       cp = *dellist;
7515796c8dcSSimon Schubert       mycount = delcount;
7525796c8dcSSimon Schubert       *cp = cppop (&result);
7535796c8dcSSimon Schubert       while ((*cp != NULL) && (mycount > 0))
7545796c8dcSSimon Schubert 	{
7555796c8dcSSimon Schubert 	  mycount--;
7565796c8dcSSimon Schubert 	  cp++;
7575796c8dcSSimon Schubert 	  *cp = cppop (&result);
7585796c8dcSSimon Schubert 	}
7595796c8dcSSimon Schubert 
7605796c8dcSSimon Schubert       if (mycount || (*cp != NULL))
7615796c8dcSSimon Schubert 	warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
7625796c8dcSSimon Schubert 		 mycount);
7635796c8dcSSimon Schubert     }
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert   return delcount;
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert 
768*cf7f2e2dSJohn Marino #if HAVE_PYTHON
769*cf7f2e2dSJohn Marino 
7705796c8dcSSimon Schubert /* Convenience function for varobj_set_visualizer.  Instantiate a
7715796c8dcSSimon Schubert    pretty-printer for a given value.  */
7725796c8dcSSimon Schubert static PyObject *
7735796c8dcSSimon Schubert instantiate_pretty_printer (PyObject *constructor, struct value *value)
7745796c8dcSSimon Schubert {
7755796c8dcSSimon Schubert   PyObject *val_obj = NULL;
7765796c8dcSSimon Schubert   PyObject *printer;
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert   val_obj = value_to_value_object (value);
7795796c8dcSSimon Schubert   if (! val_obj)
7805796c8dcSSimon Schubert     return NULL;
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert   printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
7835796c8dcSSimon Schubert   Py_DECREF (val_obj);
7845796c8dcSSimon Schubert   return printer;
7855796c8dcSSimon Schubert   return NULL;
7865796c8dcSSimon Schubert }
7875796c8dcSSimon Schubert 
788*cf7f2e2dSJohn Marino #endif
789*cf7f2e2dSJohn Marino 
7905796c8dcSSimon Schubert /* Set/Get variable object display format */
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert enum varobj_display_formats
7935796c8dcSSimon Schubert varobj_set_display_format (struct varobj *var,
7945796c8dcSSimon Schubert 			   enum varobj_display_formats format)
7955796c8dcSSimon Schubert {
7965796c8dcSSimon Schubert   switch (format)
7975796c8dcSSimon Schubert     {
7985796c8dcSSimon Schubert     case FORMAT_NATURAL:
7995796c8dcSSimon Schubert     case FORMAT_BINARY:
8005796c8dcSSimon Schubert     case FORMAT_DECIMAL:
8015796c8dcSSimon Schubert     case FORMAT_HEXADECIMAL:
8025796c8dcSSimon Schubert     case FORMAT_OCTAL:
8035796c8dcSSimon Schubert       var->format = format;
8045796c8dcSSimon Schubert       break;
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert     default:
8075796c8dcSSimon Schubert       var->format = variable_default_display (var);
8085796c8dcSSimon Schubert     }
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert   if (varobj_value_is_changeable_p (var)
8115796c8dcSSimon Schubert       && var->value && !value_lazy (var->value))
8125796c8dcSSimon Schubert     {
8135796c8dcSSimon Schubert       xfree (var->print_value);
8145796c8dcSSimon Schubert       var->print_value = value_get_print_value (var->value, var->format, var);
8155796c8dcSSimon Schubert     }
8165796c8dcSSimon Schubert 
8175796c8dcSSimon Schubert   return var->format;
8185796c8dcSSimon Schubert }
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert enum varobj_display_formats
8215796c8dcSSimon Schubert varobj_get_display_format (struct varobj *var)
8225796c8dcSSimon Schubert {
8235796c8dcSSimon Schubert   return var->format;
8245796c8dcSSimon Schubert }
8255796c8dcSSimon Schubert 
8265796c8dcSSimon Schubert char *
8275796c8dcSSimon Schubert varobj_get_display_hint (struct varobj *var)
8285796c8dcSSimon Schubert {
8295796c8dcSSimon Schubert   char *result = NULL;
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert #if HAVE_PYTHON
8325796c8dcSSimon Schubert   struct cleanup *back_to = varobj_ensure_python_env (var);
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert   if (var->pretty_printer)
8355796c8dcSSimon Schubert     result = gdbpy_get_display_hint (var->pretty_printer);
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert   do_cleanups (back_to);
8385796c8dcSSimon Schubert #endif
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert   return result;
8415796c8dcSSimon Schubert }
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert /* Return true if the varobj has items after TO, false otherwise.  */
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert int
8465796c8dcSSimon Schubert varobj_has_more (struct varobj *var, int to)
8475796c8dcSSimon Schubert {
8485796c8dcSSimon Schubert   if (VEC_length (varobj_p, var->children) > to)
8495796c8dcSSimon Schubert     return 1;
8505796c8dcSSimon Schubert   return ((to == -1 || VEC_length (varobj_p, var->children) == to)
8515796c8dcSSimon Schubert 	  && var->saved_item != NULL);
8525796c8dcSSimon Schubert }
8535796c8dcSSimon Schubert 
8545796c8dcSSimon Schubert /* If the variable object is bound to a specific thread, that
8555796c8dcSSimon Schubert    is its evaluation can always be done in context of a frame
8565796c8dcSSimon Schubert    inside that thread, returns GDB id of the thread -- which
8575796c8dcSSimon Schubert    is always positive.  Otherwise, returns -1. */
8585796c8dcSSimon Schubert int
8595796c8dcSSimon Schubert varobj_get_thread_id (struct varobj *var)
8605796c8dcSSimon Schubert {
8615796c8dcSSimon Schubert   if (var->root->valid_block && var->root->thread_id > 0)
8625796c8dcSSimon Schubert     return var->root->thread_id;
8635796c8dcSSimon Schubert   else
8645796c8dcSSimon Schubert     return -1;
8655796c8dcSSimon Schubert }
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert void
8685796c8dcSSimon Schubert varobj_set_frozen (struct varobj *var, int frozen)
8695796c8dcSSimon Schubert {
8705796c8dcSSimon Schubert   /* When a variable is unfrozen, we don't fetch its value.
8715796c8dcSSimon Schubert      The 'not_fetched' flag remains set, so next -var-update
8725796c8dcSSimon Schubert      won't complain.
8735796c8dcSSimon Schubert 
8745796c8dcSSimon Schubert      We don't fetch the value, because for structures the client
8755796c8dcSSimon Schubert      should do -var-update anyway.  It would be bad to have different
8765796c8dcSSimon Schubert      client-size logic for structure and other types.  */
8775796c8dcSSimon Schubert   var->frozen = frozen;
8785796c8dcSSimon Schubert }
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert int
8815796c8dcSSimon Schubert varobj_get_frozen (struct varobj *var)
8825796c8dcSSimon Schubert {
8835796c8dcSSimon Schubert   return var->frozen;
8845796c8dcSSimon Schubert }
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert /* A helper function that restricts a range to what is actually
8875796c8dcSSimon Schubert    available in a VEC.  This follows the usual rules for the meaning
8885796c8dcSSimon Schubert    of FROM and TO -- if either is negative, the entire range is
8895796c8dcSSimon Schubert    used.  */
8905796c8dcSSimon Schubert 
8915796c8dcSSimon Schubert static void
8925796c8dcSSimon Schubert restrict_range (VEC (varobj_p) *children, int *from, int *to)
8935796c8dcSSimon Schubert {
8945796c8dcSSimon Schubert   if (*from < 0 || *to < 0)
8955796c8dcSSimon Schubert     {
8965796c8dcSSimon Schubert       *from = 0;
8975796c8dcSSimon Schubert       *to = VEC_length (varobj_p, children);
8985796c8dcSSimon Schubert     }
8995796c8dcSSimon Schubert   else
9005796c8dcSSimon Schubert     {
9015796c8dcSSimon Schubert       if (*from > VEC_length (varobj_p, children))
9025796c8dcSSimon Schubert 	*from = VEC_length (varobj_p, children);
9035796c8dcSSimon Schubert       if (*to > VEC_length (varobj_p, children))
9045796c8dcSSimon Schubert 	*to = VEC_length (varobj_p, children);
9055796c8dcSSimon Schubert       if (*from > *to)
9065796c8dcSSimon Schubert 	*from = *to;
9075796c8dcSSimon Schubert     }
9085796c8dcSSimon Schubert }
9095796c8dcSSimon Schubert 
910*cf7f2e2dSJohn Marino #if HAVE_PYTHON
911*cf7f2e2dSJohn Marino 
9125796c8dcSSimon Schubert /* A helper for update_dynamic_varobj_children that installs a new
9135796c8dcSSimon Schubert    child when needed.  */
9145796c8dcSSimon Schubert 
9155796c8dcSSimon Schubert static void
9165796c8dcSSimon Schubert install_dynamic_child (struct varobj *var,
9175796c8dcSSimon Schubert 		       VEC (varobj_p) **changed,
9185796c8dcSSimon Schubert 		       VEC (varobj_p) **new,
9195796c8dcSSimon Schubert 		       VEC (varobj_p) **unchanged,
9205796c8dcSSimon Schubert 		       int *cchanged,
9215796c8dcSSimon Schubert 		       int index,
9225796c8dcSSimon Schubert 		       const char *name,
9235796c8dcSSimon Schubert 		       struct value *value)
9245796c8dcSSimon Schubert {
9255796c8dcSSimon Schubert   if (VEC_length (varobj_p, var->children) < index + 1)
9265796c8dcSSimon Schubert     {
9275796c8dcSSimon Schubert       /* There's no child yet.  */
9285796c8dcSSimon Schubert       struct varobj *child = varobj_add_child (var, name, value);
929*cf7f2e2dSJohn Marino 
9305796c8dcSSimon Schubert       if (new)
9315796c8dcSSimon Schubert 	{
9325796c8dcSSimon Schubert 	  VEC_safe_push (varobj_p, *new, child);
9335796c8dcSSimon Schubert 	  *cchanged = 1;
9345796c8dcSSimon Schubert 	}
9355796c8dcSSimon Schubert     }
9365796c8dcSSimon Schubert   else
9375796c8dcSSimon Schubert     {
9385796c8dcSSimon Schubert       varobj_p existing = VEC_index (varobj_p, var->children, index);
939*cf7f2e2dSJohn Marino 
9405796c8dcSSimon Schubert       if (install_new_value (existing, value, 0))
9415796c8dcSSimon Schubert 	{
9425796c8dcSSimon Schubert 	  if (changed)
9435796c8dcSSimon Schubert 	    VEC_safe_push (varobj_p, *changed, existing);
9445796c8dcSSimon Schubert 	}
9455796c8dcSSimon Schubert       else if (unchanged)
9465796c8dcSSimon Schubert 	VEC_safe_push (varobj_p, *unchanged, existing);
9475796c8dcSSimon Schubert     }
9485796c8dcSSimon Schubert }
9495796c8dcSSimon Schubert 
9505796c8dcSSimon Schubert static int
9515796c8dcSSimon Schubert dynamic_varobj_has_child_method (struct varobj *var)
9525796c8dcSSimon Schubert {
9535796c8dcSSimon Schubert   struct cleanup *back_to;
9545796c8dcSSimon Schubert   PyObject *printer = var->pretty_printer;
9555796c8dcSSimon Schubert   int result;
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
9585796c8dcSSimon Schubert   result = PyObject_HasAttr (printer, gdbpy_children_cst);
9595796c8dcSSimon Schubert   do_cleanups (back_to);
9605796c8dcSSimon Schubert   return result;
9615796c8dcSSimon Schubert }
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert #endif
9645796c8dcSSimon Schubert 
9655796c8dcSSimon Schubert static int
9665796c8dcSSimon Schubert update_dynamic_varobj_children (struct varobj *var,
9675796c8dcSSimon Schubert 				VEC (varobj_p) **changed,
9685796c8dcSSimon Schubert 				VEC (varobj_p) **new,
9695796c8dcSSimon Schubert 				VEC (varobj_p) **unchanged,
9705796c8dcSSimon Schubert 				int *cchanged,
9715796c8dcSSimon Schubert 				int update_children,
9725796c8dcSSimon Schubert 				int from,
9735796c8dcSSimon Schubert 				int to)
9745796c8dcSSimon Schubert {
9755796c8dcSSimon Schubert #if HAVE_PYTHON
9765796c8dcSSimon Schubert   struct cleanup *back_to;
9775796c8dcSSimon Schubert   PyObject *children;
9785796c8dcSSimon Schubert   int i;
9795796c8dcSSimon Schubert   PyObject *printer = var->pretty_printer;
9805796c8dcSSimon Schubert 
9815796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
9825796c8dcSSimon Schubert 
9835796c8dcSSimon Schubert   *cchanged = 0;
9845796c8dcSSimon Schubert   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
9855796c8dcSSimon Schubert     {
9865796c8dcSSimon Schubert       do_cleanups (back_to);
9875796c8dcSSimon Schubert       return 0;
9885796c8dcSSimon Schubert     }
9895796c8dcSSimon Schubert 
9905796c8dcSSimon Schubert   if (update_children || !var->child_iter)
9915796c8dcSSimon Schubert     {
9925796c8dcSSimon Schubert       children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
9935796c8dcSSimon Schubert 					     NULL);
9945796c8dcSSimon Schubert 
9955796c8dcSSimon Schubert       if (!children)
9965796c8dcSSimon Schubert 	{
9975796c8dcSSimon Schubert 	  gdbpy_print_stack ();
9985796c8dcSSimon Schubert 	  error (_("Null value returned for children"));
9995796c8dcSSimon Schubert 	}
10005796c8dcSSimon Schubert 
10015796c8dcSSimon Schubert       make_cleanup_py_decref (children);
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert       if (!PyIter_Check (children))
10045796c8dcSSimon Schubert 	error (_("Returned value is not iterable"));
10055796c8dcSSimon Schubert 
10065796c8dcSSimon Schubert       Py_XDECREF (var->child_iter);
10075796c8dcSSimon Schubert       var->child_iter = PyObject_GetIter (children);
10085796c8dcSSimon Schubert       if (!var->child_iter)
10095796c8dcSSimon Schubert 	{
10105796c8dcSSimon Schubert 	  gdbpy_print_stack ();
10115796c8dcSSimon Schubert 	  error (_("Could not get children iterator"));
10125796c8dcSSimon Schubert 	}
10135796c8dcSSimon Schubert 
10145796c8dcSSimon Schubert       Py_XDECREF (var->saved_item);
10155796c8dcSSimon Schubert       var->saved_item = NULL;
10165796c8dcSSimon Schubert 
10175796c8dcSSimon Schubert       i = 0;
10185796c8dcSSimon Schubert     }
10195796c8dcSSimon Schubert   else
10205796c8dcSSimon Schubert     i = VEC_length (varobj_p, var->children);
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert   /* We ask for one extra child, so that MI can report whether there
10235796c8dcSSimon Schubert      are more children.  */
10245796c8dcSSimon Schubert   for (; to < 0 || i < to + 1; ++i)
10255796c8dcSSimon Schubert     {
10265796c8dcSSimon Schubert       PyObject *item;
10275796c8dcSSimon Schubert 
10285796c8dcSSimon Schubert       /* See if there was a leftover from last time.  */
10295796c8dcSSimon Schubert       if (var->saved_item)
10305796c8dcSSimon Schubert 	{
10315796c8dcSSimon Schubert 	  item = var->saved_item;
10325796c8dcSSimon Schubert 	  var->saved_item = NULL;
10335796c8dcSSimon Schubert 	}
10345796c8dcSSimon Schubert       else
10355796c8dcSSimon Schubert 	item = PyIter_Next (var->child_iter);
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert       if (!item)
10385796c8dcSSimon Schubert 	break;
10395796c8dcSSimon Schubert 
10405796c8dcSSimon Schubert       /* We don't want to push the extra child on any report list.  */
10415796c8dcSSimon Schubert       if (to < 0 || i < to)
10425796c8dcSSimon Schubert 	{
10435796c8dcSSimon Schubert 	  PyObject *py_v;
10445796c8dcSSimon Schubert 	  char *name;
10455796c8dcSSimon Schubert 	  struct value *v;
10465796c8dcSSimon Schubert 	  struct cleanup *inner;
10475796c8dcSSimon Schubert 	  int can_mention = from < 0 || i >= from;
10485796c8dcSSimon Schubert 
10495796c8dcSSimon Schubert 	  inner = make_cleanup_py_decref (item);
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert 	  if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
10525796c8dcSSimon Schubert 	    error (_("Invalid item from the child list"));
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert 	  v = convert_value_from_python (py_v);
10555796c8dcSSimon Schubert 	  install_dynamic_child (var, can_mention ? changed : NULL,
10565796c8dcSSimon Schubert 				 can_mention ? new : NULL,
10575796c8dcSSimon Schubert 				 can_mention ? unchanged : NULL,
10585796c8dcSSimon Schubert 				 can_mention ? cchanged : NULL, i, name, v);
10595796c8dcSSimon Schubert 	  do_cleanups (inner);
10605796c8dcSSimon Schubert 	}
10615796c8dcSSimon Schubert       else
10625796c8dcSSimon Schubert 	{
10635796c8dcSSimon Schubert 	  Py_XDECREF (var->saved_item);
10645796c8dcSSimon Schubert 	  var->saved_item = item;
10655796c8dcSSimon Schubert 
10665796c8dcSSimon Schubert 	  /* We want to truncate the child list just before this
10675796c8dcSSimon Schubert 	     element.  */
10685796c8dcSSimon Schubert 	  break;
10695796c8dcSSimon Schubert 	}
10705796c8dcSSimon Schubert     }
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert   if (i < VEC_length (varobj_p, var->children))
10735796c8dcSSimon Schubert     {
10745796c8dcSSimon Schubert       int j;
1075*cf7f2e2dSJohn Marino 
10765796c8dcSSimon Schubert       *cchanged = 1;
10775796c8dcSSimon Schubert       for (j = i; j < VEC_length (varobj_p, var->children); ++j)
10785796c8dcSSimon Schubert 	varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
10795796c8dcSSimon Schubert       VEC_truncate (varobj_p, var->children, i);
10805796c8dcSSimon Schubert     }
10815796c8dcSSimon Schubert 
10825796c8dcSSimon Schubert   /* If there are fewer children than requested, note that the list of
10835796c8dcSSimon Schubert      children changed.  */
10845796c8dcSSimon Schubert   if (to >= 0 && VEC_length (varobj_p, var->children) < to)
10855796c8dcSSimon Schubert     *cchanged = 1;
10865796c8dcSSimon Schubert 
10875796c8dcSSimon Schubert   var->num_children = VEC_length (varobj_p, var->children);
10885796c8dcSSimon Schubert 
10895796c8dcSSimon Schubert   do_cleanups (back_to);
10905796c8dcSSimon Schubert 
10915796c8dcSSimon Schubert   return 1;
10925796c8dcSSimon Schubert #else
10935796c8dcSSimon Schubert   gdb_assert (0 && "should never be called if Python is not enabled");
10945796c8dcSSimon Schubert #endif
10955796c8dcSSimon Schubert }
10965796c8dcSSimon Schubert 
10975796c8dcSSimon Schubert int
10985796c8dcSSimon Schubert varobj_get_num_children (struct varobj *var)
10995796c8dcSSimon Schubert {
11005796c8dcSSimon Schubert   if (var->num_children == -1)
11015796c8dcSSimon Schubert     {
11025796c8dcSSimon Schubert       if (var->pretty_printer)
11035796c8dcSSimon Schubert 	{
11045796c8dcSSimon Schubert 	  int dummy;
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert 	  /* If we have a dynamic varobj, don't report -1 children.
11075796c8dcSSimon Schubert 	     So, try to fetch some children first.  */
11085796c8dcSSimon Schubert 	  update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
11095796c8dcSSimon Schubert 					  0, 0, 0);
11105796c8dcSSimon Schubert 	}
11115796c8dcSSimon Schubert       else
11125796c8dcSSimon Schubert 	var->num_children = number_of_children (var);
11135796c8dcSSimon Schubert     }
11145796c8dcSSimon Schubert 
11155796c8dcSSimon Schubert   return var->num_children >= 0 ? var->num_children : 0;
11165796c8dcSSimon Schubert }
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert /* Creates a list of the immediate children of a variable object;
11195796c8dcSSimon Schubert    the return code is the number of such children or -1 on error */
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert VEC (varobj_p)*
11225796c8dcSSimon Schubert varobj_list_children (struct varobj *var, int *from, int *to)
11235796c8dcSSimon Schubert {
11245796c8dcSSimon Schubert   char *name;
11255796c8dcSSimon Schubert   int i, children_changed;
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert   var->children_requested = 1;
11285796c8dcSSimon Schubert 
11295796c8dcSSimon Schubert   if (var->pretty_printer)
11305796c8dcSSimon Schubert     {
11315796c8dcSSimon Schubert       /* This, in theory, can result in the number of children changing without
11325796c8dcSSimon Schubert 	 frontend noticing.  But well, calling -var-list-children on the same
11335796c8dcSSimon Schubert 	 varobj twice is not something a sane frontend would do.  */
11345796c8dcSSimon Schubert       update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
11355796c8dcSSimon Schubert 				      0, 0, *to);
11365796c8dcSSimon Schubert       restrict_range (var->children, from, to);
11375796c8dcSSimon Schubert       return var->children;
11385796c8dcSSimon Schubert     }
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert   if (var->num_children == -1)
11415796c8dcSSimon Schubert     var->num_children = number_of_children (var);
11425796c8dcSSimon Schubert 
11435796c8dcSSimon Schubert   /* If that failed, give up.  */
11445796c8dcSSimon Schubert   if (var->num_children == -1)
11455796c8dcSSimon Schubert     return var->children;
11465796c8dcSSimon Schubert 
11475796c8dcSSimon Schubert   /* If we're called when the list of children is not yet initialized,
11485796c8dcSSimon Schubert      allocate enough elements in it.  */
11495796c8dcSSimon Schubert   while (VEC_length (varobj_p, var->children) < var->num_children)
11505796c8dcSSimon Schubert     VEC_safe_push (varobj_p, var->children, NULL);
11515796c8dcSSimon Schubert 
11525796c8dcSSimon Schubert   for (i = 0; i < var->num_children; i++)
11535796c8dcSSimon Schubert     {
11545796c8dcSSimon Schubert       varobj_p existing = VEC_index (varobj_p, var->children, i);
11555796c8dcSSimon Schubert 
11565796c8dcSSimon Schubert       if (existing == NULL)
11575796c8dcSSimon Schubert 	{
11585796c8dcSSimon Schubert 	  /* Either it's the first call to varobj_list_children for
11595796c8dcSSimon Schubert 	     this variable object, and the child was never created,
11605796c8dcSSimon Schubert 	     or it was explicitly deleted by the client.  */
11615796c8dcSSimon Schubert 	  name = name_of_child (var, i);
11625796c8dcSSimon Schubert 	  existing = create_child (var, i, name);
11635796c8dcSSimon Schubert 	  VEC_replace (varobj_p, var->children, i, existing);
11645796c8dcSSimon Schubert 	}
11655796c8dcSSimon Schubert     }
11665796c8dcSSimon Schubert 
11675796c8dcSSimon Schubert   restrict_range (var->children, from, to);
11685796c8dcSSimon Schubert   return var->children;
11695796c8dcSSimon Schubert }
11705796c8dcSSimon Schubert 
1171*cf7f2e2dSJohn Marino #if HAVE_PYTHON
1172*cf7f2e2dSJohn Marino 
11735796c8dcSSimon Schubert static struct varobj *
11745796c8dcSSimon Schubert varobj_add_child (struct varobj *var, const char *name, struct value *value)
11755796c8dcSSimon Schubert {
11765796c8dcSSimon Schubert   varobj_p v = create_child_with_value (var,
11775796c8dcSSimon Schubert 					VEC_length (varobj_p, var->children),
11785796c8dcSSimon Schubert 					name, value);
1179*cf7f2e2dSJohn Marino 
11805796c8dcSSimon Schubert   VEC_safe_push (varobj_p, var->children, v);
11815796c8dcSSimon Schubert   return v;
11825796c8dcSSimon Schubert }
11835796c8dcSSimon Schubert 
1184*cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
1185*cf7f2e2dSJohn Marino 
11865796c8dcSSimon Schubert /* Obtain the type of an object Variable as a string similar to the one gdb
11875796c8dcSSimon Schubert    prints on the console */
11885796c8dcSSimon Schubert 
11895796c8dcSSimon Schubert char *
11905796c8dcSSimon Schubert varobj_get_type (struct varobj *var)
11915796c8dcSSimon Schubert {
11925796c8dcSSimon Schubert   /* For the "fake" variables, do not return a type. (It's type is
11935796c8dcSSimon Schubert      NULL, too.)
11945796c8dcSSimon Schubert      Do not return a type for invalid variables as well.  */
11955796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
11965796c8dcSSimon Schubert     return NULL;
11975796c8dcSSimon Schubert 
11985796c8dcSSimon Schubert   return type_to_string (var->type);
11995796c8dcSSimon Schubert }
12005796c8dcSSimon Schubert 
12015796c8dcSSimon Schubert /* Obtain the type of an object variable.  */
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert struct type *
12045796c8dcSSimon Schubert varobj_get_gdb_type (struct varobj *var)
12055796c8dcSSimon Schubert {
12065796c8dcSSimon Schubert   return var->type;
12075796c8dcSSimon Schubert }
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert /* Return a pointer to the full rooted expression of varobj VAR.
12105796c8dcSSimon Schubert    If it has not been computed yet, compute it.  */
12115796c8dcSSimon Schubert char *
12125796c8dcSSimon Schubert varobj_get_path_expr (struct varobj *var)
12135796c8dcSSimon Schubert {
12145796c8dcSSimon Schubert   if (var->path_expr != NULL)
12155796c8dcSSimon Schubert     return var->path_expr;
12165796c8dcSSimon Schubert   else
12175796c8dcSSimon Schubert     {
12185796c8dcSSimon Schubert       /* For root varobjs, we initialize path_expr
12195796c8dcSSimon Schubert 	 when creating varobj, so here it should be
12205796c8dcSSimon Schubert 	 child varobj.  */
12215796c8dcSSimon Schubert       gdb_assert (!is_root_p (var));
12225796c8dcSSimon Schubert       return (*var->root->lang->path_expr_of_child) (var);
12235796c8dcSSimon Schubert     }
12245796c8dcSSimon Schubert }
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert enum varobj_languages
12275796c8dcSSimon Schubert varobj_get_language (struct varobj *var)
12285796c8dcSSimon Schubert {
12295796c8dcSSimon Schubert   return variable_language (var);
12305796c8dcSSimon Schubert }
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert int
12335796c8dcSSimon Schubert varobj_get_attributes (struct varobj *var)
12345796c8dcSSimon Schubert {
12355796c8dcSSimon Schubert   int attributes = 0;
12365796c8dcSSimon Schubert 
12375796c8dcSSimon Schubert   if (varobj_editable_p (var))
12385796c8dcSSimon Schubert     /* FIXME: define masks for attributes */
12395796c8dcSSimon Schubert     attributes |= 0x00000001;	/* Editable */
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert   return attributes;
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert int
12455796c8dcSSimon Schubert varobj_pretty_printed_p (struct varobj *var)
12465796c8dcSSimon Schubert {
12475796c8dcSSimon Schubert   return var->pretty_printer != NULL;
12485796c8dcSSimon Schubert }
12495796c8dcSSimon Schubert 
12505796c8dcSSimon Schubert char *
12515796c8dcSSimon Schubert varobj_get_formatted_value (struct varobj *var,
12525796c8dcSSimon Schubert 			    enum varobj_display_formats format)
12535796c8dcSSimon Schubert {
12545796c8dcSSimon Schubert   return my_value_of_variable (var, format);
12555796c8dcSSimon Schubert }
12565796c8dcSSimon Schubert 
12575796c8dcSSimon Schubert char *
12585796c8dcSSimon Schubert varobj_get_value (struct varobj *var)
12595796c8dcSSimon Schubert {
12605796c8dcSSimon Schubert   return my_value_of_variable (var, var->format);
12615796c8dcSSimon Schubert }
12625796c8dcSSimon Schubert 
12635796c8dcSSimon Schubert /* Set the value of an object variable (if it is editable) to the
12645796c8dcSSimon Schubert    value of the given expression */
12655796c8dcSSimon Schubert /* Note: Invokes functions that can call error() */
12665796c8dcSSimon Schubert 
12675796c8dcSSimon Schubert int
12685796c8dcSSimon Schubert varobj_set_value (struct varobj *var, char *expression)
12695796c8dcSSimon Schubert {
12705796c8dcSSimon Schubert   struct value *val;
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert   /* The argument "expression" contains the variable's new value.
12735796c8dcSSimon Schubert      We need to first construct a legal expression for this -- ugh! */
12745796c8dcSSimon Schubert   /* Does this cover all the bases? */
12755796c8dcSSimon Schubert   struct expression *exp;
12765796c8dcSSimon Schubert   struct value *value;
12775796c8dcSSimon Schubert   int saved_input_radix = input_radix;
12785796c8dcSSimon Schubert   char *s = expression;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   gdb_assert (varobj_editable_p (var));
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert   input_radix = 10;		/* ALWAYS reset to decimal temporarily */
12835796c8dcSSimon Schubert   exp = parse_exp_1 (&s, 0, 0);
12845796c8dcSSimon Schubert   if (!gdb_evaluate_expression (exp, &value))
12855796c8dcSSimon Schubert     {
12865796c8dcSSimon Schubert       /* We cannot proceed without a valid expression. */
12875796c8dcSSimon Schubert       xfree (exp);
12885796c8dcSSimon Schubert       return 0;
12895796c8dcSSimon Schubert     }
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert   /* All types that are editable must also be changeable.  */
12925796c8dcSSimon Schubert   gdb_assert (varobj_value_is_changeable_p (var));
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert   /* The value of a changeable variable object must not be lazy.  */
12955796c8dcSSimon Schubert   gdb_assert (!value_lazy (var->value));
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert   /* Need to coerce the input.  We want to check if the
12985796c8dcSSimon Schubert      value of the variable object will be different
12995796c8dcSSimon Schubert      after assignment, and the first thing value_assign
13005796c8dcSSimon Schubert      does is coerce the input.
13015796c8dcSSimon Schubert      For example, if we are assigning an array to a pointer variable we
13025796c8dcSSimon Schubert      should compare the pointer with the the array's address, not with the
13035796c8dcSSimon Schubert      array's content.  */
13045796c8dcSSimon Schubert   value = coerce_array (value);
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert   /* The new value may be lazy.  gdb_value_assign, or
13075796c8dcSSimon Schubert      rather value_contents, will take care of this.
13085796c8dcSSimon Schubert      If fetching of the new value will fail, gdb_value_assign
13095796c8dcSSimon Schubert      with catch the exception.  */
13105796c8dcSSimon Schubert   if (!gdb_value_assign (var->value, value, &val))
13115796c8dcSSimon Schubert     return 0;
13125796c8dcSSimon Schubert 
13135796c8dcSSimon Schubert   /* If the value has changed, record it, so that next -var-update can
13145796c8dcSSimon Schubert      report this change.  If a variable had a value of '1', we've set it
13155796c8dcSSimon Schubert      to '333' and then set again to '1', when -var-update will report this
13165796c8dcSSimon Schubert      variable as changed -- because the first assignment has set the
13175796c8dcSSimon Schubert      'updated' flag.  There's no need to optimize that, because return value
13185796c8dcSSimon Schubert      of -var-update should be considered an approximation.  */
13195796c8dcSSimon Schubert   var->updated = install_new_value (var, val, 0 /* Compare values. */);
13205796c8dcSSimon Schubert   input_radix = saved_input_radix;
13215796c8dcSSimon Schubert   return 1;
13225796c8dcSSimon Schubert }
13235796c8dcSSimon Schubert 
13245796c8dcSSimon Schubert #if HAVE_PYTHON
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert /* A helper function to install a constructor function and visualizer
13275796c8dcSSimon Schubert    in a varobj.  */
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert static void
13305796c8dcSSimon Schubert install_visualizer (struct varobj *var, PyObject *constructor,
13315796c8dcSSimon Schubert 		    PyObject *visualizer)
13325796c8dcSSimon Schubert {
13335796c8dcSSimon Schubert   Py_XDECREF (var->constructor);
13345796c8dcSSimon Schubert   var->constructor = constructor;
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert   Py_XDECREF (var->pretty_printer);
13375796c8dcSSimon Schubert   var->pretty_printer = visualizer;
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert   Py_XDECREF (var->child_iter);
13405796c8dcSSimon Schubert   var->child_iter = NULL;
13415796c8dcSSimon Schubert }
13425796c8dcSSimon Schubert 
13435796c8dcSSimon Schubert /* Install the default visualizer for VAR.  */
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert static void
13465796c8dcSSimon Schubert install_default_visualizer (struct varobj *var)
13475796c8dcSSimon Schubert {
13485796c8dcSSimon Schubert   if (pretty_printing)
13495796c8dcSSimon Schubert     {
13505796c8dcSSimon Schubert       PyObject *pretty_printer = NULL;
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert       if (var->value)
13535796c8dcSSimon Schubert 	{
13545796c8dcSSimon Schubert 	  pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
13555796c8dcSSimon Schubert 	  if (! pretty_printer)
13565796c8dcSSimon Schubert 	    {
13575796c8dcSSimon Schubert 	      gdbpy_print_stack ();
13585796c8dcSSimon Schubert 	      error (_("Cannot instantiate printer for default visualizer"));
13595796c8dcSSimon Schubert 	    }
13605796c8dcSSimon Schubert 	}
13615796c8dcSSimon Schubert 
13625796c8dcSSimon Schubert       if (pretty_printer == Py_None)
13635796c8dcSSimon Schubert 	{
13645796c8dcSSimon Schubert 	  Py_DECREF (pretty_printer);
13655796c8dcSSimon Schubert 	  pretty_printer = NULL;
13665796c8dcSSimon Schubert 	}
13675796c8dcSSimon Schubert 
13685796c8dcSSimon Schubert       install_visualizer (var, NULL, pretty_printer);
13695796c8dcSSimon Schubert     }
13705796c8dcSSimon Schubert }
13715796c8dcSSimon Schubert 
13725796c8dcSSimon Schubert /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
13735796c8dcSSimon Schubert    make a new object.  */
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert static void
13765796c8dcSSimon Schubert construct_visualizer (struct varobj *var, PyObject *constructor)
13775796c8dcSSimon Schubert {
13785796c8dcSSimon Schubert   PyObject *pretty_printer;
13795796c8dcSSimon Schubert 
13805796c8dcSSimon Schubert   Py_INCREF (constructor);
13815796c8dcSSimon Schubert   if (constructor == Py_None)
13825796c8dcSSimon Schubert     pretty_printer = NULL;
13835796c8dcSSimon Schubert   else
13845796c8dcSSimon Schubert     {
13855796c8dcSSimon Schubert       pretty_printer = instantiate_pretty_printer (constructor, var->value);
13865796c8dcSSimon Schubert       if (! pretty_printer)
13875796c8dcSSimon Schubert 	{
13885796c8dcSSimon Schubert 	  gdbpy_print_stack ();
13895796c8dcSSimon Schubert 	  Py_DECREF (constructor);
13905796c8dcSSimon Schubert 	  constructor = Py_None;
13915796c8dcSSimon Schubert 	  Py_INCREF (constructor);
13925796c8dcSSimon Schubert 	}
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert       if (pretty_printer == Py_None)
13955796c8dcSSimon Schubert 	{
13965796c8dcSSimon Schubert 	  Py_DECREF (pretty_printer);
13975796c8dcSSimon Schubert 	  pretty_printer = NULL;
13985796c8dcSSimon Schubert 	}
13995796c8dcSSimon Schubert     }
14005796c8dcSSimon Schubert 
14015796c8dcSSimon Schubert   install_visualizer (var, constructor, pretty_printer);
14025796c8dcSSimon Schubert }
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert /* A helper function for install_new_value.  This creates and installs
14075796c8dcSSimon Schubert    a visualizer for VAR, if appropriate.  */
14085796c8dcSSimon Schubert 
14095796c8dcSSimon Schubert static void
14105796c8dcSSimon Schubert install_new_value_visualizer (struct varobj *var)
14115796c8dcSSimon Schubert {
14125796c8dcSSimon Schubert #if HAVE_PYTHON
14135796c8dcSSimon Schubert   /* If the constructor is None, then we want the raw value.  If VAR
14145796c8dcSSimon Schubert      does not have a value, just skip this.  */
14155796c8dcSSimon Schubert   if (var->constructor != Py_None && var->value)
14165796c8dcSSimon Schubert     {
14175796c8dcSSimon Schubert       struct cleanup *cleanup;
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert       cleanup = varobj_ensure_python_env (var);
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert       if (!var->constructor)
14225796c8dcSSimon Schubert 	install_default_visualizer (var);
14235796c8dcSSimon Schubert       else
14245796c8dcSSimon Schubert 	construct_visualizer (var, var->constructor);
14255796c8dcSSimon Schubert 
14265796c8dcSSimon Schubert       do_cleanups (cleanup);
14275796c8dcSSimon Schubert     }
14285796c8dcSSimon Schubert #else
14295796c8dcSSimon Schubert   /* Do nothing.  */
14305796c8dcSSimon Schubert #endif
14315796c8dcSSimon Schubert }
14325796c8dcSSimon Schubert 
14335796c8dcSSimon Schubert /* Assign a new value to a variable object.  If INITIAL is non-zero,
14345796c8dcSSimon Schubert    this is the first assignement after the variable object was just
14355796c8dcSSimon Schubert    created, or changed type.  In that case, just assign the value
14365796c8dcSSimon Schubert    and return 0.
14375796c8dcSSimon Schubert    Otherwise, assign the new value, and return 1 if the value is different
14385796c8dcSSimon Schubert    from the current one, 0 otherwise. The comparison is done on textual
14395796c8dcSSimon Schubert    representation of value. Therefore, some types need not be compared. E.g.
14405796c8dcSSimon Schubert    for structures the reported value is always "{...}", so no comparison is
14415796c8dcSSimon Schubert    necessary here. If the old value was NULL and new one is not, or vice versa,
14425796c8dcSSimon Schubert    we always return 1.
14435796c8dcSSimon Schubert 
14445796c8dcSSimon Schubert    The VALUE parameter should not be released -- the function will
14455796c8dcSSimon Schubert    take care of releasing it when needed.  */
14465796c8dcSSimon Schubert static int
14475796c8dcSSimon Schubert install_new_value (struct varobj *var, struct value *value, int initial)
14485796c8dcSSimon Schubert {
14495796c8dcSSimon Schubert   int changeable;
14505796c8dcSSimon Schubert   int need_to_fetch;
14515796c8dcSSimon Schubert   int changed = 0;
14525796c8dcSSimon Schubert   int intentionally_not_fetched = 0;
14535796c8dcSSimon Schubert   char *print_value = NULL;
14545796c8dcSSimon Schubert 
14555796c8dcSSimon Schubert   /* We need to know the varobj's type to decide if the value should
14565796c8dcSSimon Schubert      be fetched or not.  C++ fake children (public/protected/private) don't have
14575796c8dcSSimon Schubert      a type. */
14585796c8dcSSimon Schubert   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
14595796c8dcSSimon Schubert   changeable = varobj_value_is_changeable_p (var);
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert   /* If the type has custom visualizer, we consider it to be always
14625796c8dcSSimon Schubert      changeable. FIXME: need to make sure this behaviour will not
14635796c8dcSSimon Schubert      mess up read-sensitive values.  */
14645796c8dcSSimon Schubert   if (var->pretty_printer)
14655796c8dcSSimon Schubert     changeable = 1;
14665796c8dcSSimon Schubert 
14675796c8dcSSimon Schubert   need_to_fetch = changeable;
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert   /* We are not interested in the address of references, and given
14705796c8dcSSimon Schubert      that in C++ a reference is not rebindable, it cannot
14715796c8dcSSimon Schubert      meaningfully change.  So, get hold of the real value.  */
14725796c8dcSSimon Schubert   if (value)
14735796c8dcSSimon Schubert     value = coerce_ref (value);
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
14765796c8dcSSimon Schubert     /* For unions, we need to fetch the value implicitly because
14775796c8dcSSimon Schubert        of implementation of union member fetch.  When gdb
14785796c8dcSSimon Schubert        creates a value for a field and the value of the enclosing
14795796c8dcSSimon Schubert        structure is not lazy,  it immediately copies the necessary
14805796c8dcSSimon Schubert        bytes from the enclosing values.  If the enclosing value is
14815796c8dcSSimon Schubert        lazy, the call to value_fetch_lazy on the field will read
14825796c8dcSSimon Schubert        the data from memory.  For unions, that means we'll read the
14835796c8dcSSimon Schubert        same memory more than once, which is not desirable.  So
14845796c8dcSSimon Schubert        fetch now.  */
14855796c8dcSSimon Schubert     need_to_fetch = 1;
14865796c8dcSSimon Schubert 
14875796c8dcSSimon Schubert   /* The new value might be lazy.  If the type is changeable,
14885796c8dcSSimon Schubert      that is we'll be comparing values of this type, fetch the
14895796c8dcSSimon Schubert      value now.  Otherwise, on the next update the old value
14905796c8dcSSimon Schubert      will be lazy, which means we've lost that old value.  */
14915796c8dcSSimon Schubert   if (need_to_fetch && value && value_lazy (value))
14925796c8dcSSimon Schubert     {
14935796c8dcSSimon Schubert       struct varobj *parent = var->parent;
14945796c8dcSSimon Schubert       int frozen = var->frozen;
1495*cf7f2e2dSJohn Marino 
14965796c8dcSSimon Schubert       for (; !frozen && parent; parent = parent->parent)
14975796c8dcSSimon Schubert 	frozen |= parent->frozen;
14985796c8dcSSimon Schubert 
14995796c8dcSSimon Schubert       if (frozen && initial)
15005796c8dcSSimon Schubert 	{
15015796c8dcSSimon Schubert 	  /* For variables that are frozen, or are children of frozen
15025796c8dcSSimon Schubert 	     variables, we don't do fetch on initial assignment.
15035796c8dcSSimon Schubert 	     For non-initial assignemnt we do the fetch, since it means we're
15045796c8dcSSimon Schubert 	     explicitly asked to compare the new value with the old one.  */
15055796c8dcSSimon Schubert 	  intentionally_not_fetched = 1;
15065796c8dcSSimon Schubert 	}
15075796c8dcSSimon Schubert       else if (!gdb_value_fetch_lazy (value))
15085796c8dcSSimon Schubert 	{
15095796c8dcSSimon Schubert 	  /* Set the value to NULL, so that for the next -var-update,
15105796c8dcSSimon Schubert 	     we don't try to compare the new value with this value,
15115796c8dcSSimon Schubert 	     that we couldn't even read.  */
15125796c8dcSSimon Schubert 	  value = NULL;
15135796c8dcSSimon Schubert 	}
15145796c8dcSSimon Schubert     }
15155796c8dcSSimon Schubert 
15165796c8dcSSimon Schubert 
15175796c8dcSSimon Schubert   /* Below, we'll be comparing string rendering of old and new
15185796c8dcSSimon Schubert      values.  Don't get string rendering if the value is
15195796c8dcSSimon Schubert      lazy -- if it is, the code above has decided that the value
15205796c8dcSSimon Schubert      should not be fetched.  */
15215796c8dcSSimon Schubert   if (value && !value_lazy (value) && !var->pretty_printer)
15225796c8dcSSimon Schubert     print_value = value_get_print_value (value, var->format, var);
15235796c8dcSSimon Schubert 
15245796c8dcSSimon Schubert   /* If the type is changeable, compare the old and the new values.
15255796c8dcSSimon Schubert      If this is the initial assignment, we don't have any old value
15265796c8dcSSimon Schubert      to compare with.  */
15275796c8dcSSimon Schubert   if (!initial && changeable)
15285796c8dcSSimon Schubert     {
15295796c8dcSSimon Schubert       /* If the value of the varobj was changed by -var-set-value, then the
15305796c8dcSSimon Schubert 	 value in the varobj and in the target is the same.  However, that value
15315796c8dcSSimon Schubert 	 is different from the value that the varobj had after the previous
15325796c8dcSSimon Schubert 	 -var-update. So need to the varobj as changed.  */
15335796c8dcSSimon Schubert       if (var->updated)
15345796c8dcSSimon Schubert 	{
15355796c8dcSSimon Schubert 	  changed = 1;
15365796c8dcSSimon Schubert 	}
15375796c8dcSSimon Schubert       else if (! var->pretty_printer)
15385796c8dcSSimon Schubert 	{
15395796c8dcSSimon Schubert 	  /* Try to compare the values.  That requires that both
15405796c8dcSSimon Schubert 	     values are non-lazy.  */
15415796c8dcSSimon Schubert 	  if (var->not_fetched && value_lazy (var->value))
15425796c8dcSSimon Schubert 	    {
15435796c8dcSSimon Schubert 	      /* This is a frozen varobj and the value was never read.
15445796c8dcSSimon Schubert 		 Presumably, UI shows some "never read" indicator.
15455796c8dcSSimon Schubert 		 Now that we've fetched the real value, we need to report
15465796c8dcSSimon Schubert 		 this varobj as changed so that UI can show the real
15475796c8dcSSimon Schubert 		 value.  */
15485796c8dcSSimon Schubert 	      changed = 1;
15495796c8dcSSimon Schubert 	    }
15505796c8dcSSimon Schubert           else  if (var->value == NULL && value == NULL)
15515796c8dcSSimon Schubert 	    /* Equal. */
15525796c8dcSSimon Schubert 	    ;
15535796c8dcSSimon Schubert 	  else if (var->value == NULL || value == NULL)
15545796c8dcSSimon Schubert 	    {
15555796c8dcSSimon Schubert 	      changed = 1;
15565796c8dcSSimon Schubert 	    }
15575796c8dcSSimon Schubert 	  else
15585796c8dcSSimon Schubert 	    {
15595796c8dcSSimon Schubert 	      gdb_assert (!value_lazy (var->value));
15605796c8dcSSimon Schubert 	      gdb_assert (!value_lazy (value));
15615796c8dcSSimon Schubert 
15625796c8dcSSimon Schubert 	      gdb_assert (var->print_value != NULL && print_value != NULL);
15635796c8dcSSimon Schubert 	      if (strcmp (var->print_value, print_value) != 0)
15645796c8dcSSimon Schubert 		changed = 1;
15655796c8dcSSimon Schubert 	    }
15665796c8dcSSimon Schubert 	}
15675796c8dcSSimon Schubert     }
15685796c8dcSSimon Schubert 
15695796c8dcSSimon Schubert   if (!initial && !changeable)
15705796c8dcSSimon Schubert     {
15715796c8dcSSimon Schubert       /* For values that are not changeable, we don't compare the values.
15725796c8dcSSimon Schubert 	 However, we want to notice if a value was not NULL and now is NULL,
15735796c8dcSSimon Schubert 	 or vise versa, so that we report when top-level varobjs come in scope
15745796c8dcSSimon Schubert 	 and leave the scope.  */
15755796c8dcSSimon Schubert       changed = (var->value != NULL) != (value != NULL);
15765796c8dcSSimon Schubert     }
15775796c8dcSSimon Schubert 
15785796c8dcSSimon Schubert   /* We must always keep the new value, since children depend on it.  */
15795796c8dcSSimon Schubert   if (var->value != NULL && var->value != value)
15805796c8dcSSimon Schubert     value_free (var->value);
15815796c8dcSSimon Schubert   var->value = value;
15825796c8dcSSimon Schubert   if (value != NULL)
15835796c8dcSSimon Schubert     value_incref (value);
15845796c8dcSSimon Schubert   if (value && value_lazy (value) && intentionally_not_fetched)
15855796c8dcSSimon Schubert     var->not_fetched = 1;
15865796c8dcSSimon Schubert   else
15875796c8dcSSimon Schubert     var->not_fetched = 0;
15885796c8dcSSimon Schubert   var->updated = 0;
15895796c8dcSSimon Schubert 
15905796c8dcSSimon Schubert   install_new_value_visualizer (var);
15915796c8dcSSimon Schubert 
15925796c8dcSSimon Schubert   /* If we installed a pretty-printer, re-compare the printed version
15935796c8dcSSimon Schubert      to see if the variable changed.  */
15945796c8dcSSimon Schubert   if (var->pretty_printer)
15955796c8dcSSimon Schubert     {
15965796c8dcSSimon Schubert       xfree (print_value);
15975796c8dcSSimon Schubert       print_value = value_get_print_value (var->value, var->format, var);
1598*cf7f2e2dSJohn Marino       if ((var->print_value == NULL && print_value != NULL)
1599*cf7f2e2dSJohn Marino 	  || (var->print_value != NULL && print_value == NULL)
1600*cf7f2e2dSJohn Marino 	  || (var->print_value != NULL && print_value != NULL
1601*cf7f2e2dSJohn Marino 	      && strcmp (var->print_value, print_value) != 0))
16025796c8dcSSimon Schubert 	changed = 1;
16035796c8dcSSimon Schubert     }
16045796c8dcSSimon Schubert   if (var->print_value)
16055796c8dcSSimon Schubert     xfree (var->print_value);
16065796c8dcSSimon Schubert   var->print_value = print_value;
16075796c8dcSSimon Schubert 
16085796c8dcSSimon Schubert   gdb_assert (!var->value || value_type (var->value));
16095796c8dcSSimon Schubert 
16105796c8dcSSimon Schubert   return changed;
16115796c8dcSSimon Schubert }
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert /* Return the requested range for a varobj.  VAR is the varobj.  FROM
16145796c8dcSSimon Schubert    and TO are out parameters; *FROM and *TO will be set to the
16155796c8dcSSimon Schubert    selected sub-range of VAR.  If no range was selected using
16165796c8dcSSimon Schubert    -var-set-update-range, then both will be -1.  */
16175796c8dcSSimon Schubert void
16185796c8dcSSimon Schubert varobj_get_child_range (struct varobj *var, int *from, int *to)
16195796c8dcSSimon Schubert {
16205796c8dcSSimon Schubert   *from = var->from;
16215796c8dcSSimon Schubert   *to = var->to;
16225796c8dcSSimon Schubert }
16235796c8dcSSimon Schubert 
16245796c8dcSSimon Schubert /* Set the selected sub-range of children of VAR to start at index
16255796c8dcSSimon Schubert    FROM and end at index TO.  If either FROM or TO is less than zero,
16265796c8dcSSimon Schubert    this is interpreted as a request for all children.  */
16275796c8dcSSimon Schubert void
16285796c8dcSSimon Schubert varobj_set_child_range (struct varobj *var, int from, int to)
16295796c8dcSSimon Schubert {
16305796c8dcSSimon Schubert   var->from = from;
16315796c8dcSSimon Schubert   var->to = to;
16325796c8dcSSimon Schubert }
16335796c8dcSSimon Schubert 
16345796c8dcSSimon Schubert void
16355796c8dcSSimon Schubert varobj_set_visualizer (struct varobj *var, const char *visualizer)
16365796c8dcSSimon Schubert {
16375796c8dcSSimon Schubert #if HAVE_PYTHON
1638*cf7f2e2dSJohn Marino   PyObject *mainmod, *globals, *constructor;
1639*cf7f2e2dSJohn Marino   struct cleanup *back_to;
16405796c8dcSSimon Schubert 
16415796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
16425796c8dcSSimon Schubert 
16435796c8dcSSimon Schubert   mainmod = PyImport_AddModule ("__main__");
16445796c8dcSSimon Schubert   globals = PyModule_GetDict (mainmod);
16455796c8dcSSimon Schubert   Py_INCREF (globals);
16465796c8dcSSimon Schubert   make_cleanup_py_decref (globals);
16475796c8dcSSimon Schubert 
16485796c8dcSSimon Schubert   constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
16495796c8dcSSimon Schubert 
16505796c8dcSSimon Schubert   if (! constructor)
16515796c8dcSSimon Schubert     {
16525796c8dcSSimon Schubert       gdbpy_print_stack ();
16535796c8dcSSimon Schubert       error (_("Could not evaluate visualizer expression: %s"), visualizer);
16545796c8dcSSimon Schubert     }
16555796c8dcSSimon Schubert 
16565796c8dcSSimon Schubert   construct_visualizer (var, constructor);
16575796c8dcSSimon Schubert   Py_XDECREF (constructor);
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert   /* If there are any children now, wipe them.  */
16605796c8dcSSimon Schubert   varobj_delete (var, NULL, 1 /* children only */);
16615796c8dcSSimon Schubert   var->num_children = -1;
16625796c8dcSSimon Schubert 
16635796c8dcSSimon Schubert   do_cleanups (back_to);
16645796c8dcSSimon Schubert #else
16655796c8dcSSimon Schubert   error (_("Python support required"));
16665796c8dcSSimon Schubert #endif
16675796c8dcSSimon Schubert }
16685796c8dcSSimon Schubert 
16695796c8dcSSimon Schubert /* Update the values for a variable and its children.  This is a
16705796c8dcSSimon Schubert    two-pronged attack.  First, re-parse the value for the root's
16715796c8dcSSimon Schubert    expression to see if it's changed.  Then go all the way
16725796c8dcSSimon Schubert    through its children, reconstructing them and noting if they've
16735796c8dcSSimon Schubert    changed.
16745796c8dcSSimon Schubert 
16755796c8dcSSimon Schubert    The EXPLICIT parameter specifies if this call is result
16765796c8dcSSimon Schubert    of MI request to update this specific variable, or
16775796c8dcSSimon Schubert    result of implicit -var-update *. For implicit request, we don't
16785796c8dcSSimon Schubert    update frozen variables.
16795796c8dcSSimon Schubert 
16805796c8dcSSimon Schubert    NOTE: This function may delete the caller's varobj. If it
16815796c8dcSSimon Schubert    returns TYPE_CHANGED, then it has done this and VARP will be modified
16825796c8dcSSimon Schubert    to point to the new varobj.  */
16835796c8dcSSimon Schubert 
16845796c8dcSSimon Schubert VEC(varobj_update_result) *varobj_update (struct varobj **varp, int explicit)
16855796c8dcSSimon Schubert {
16865796c8dcSSimon Schubert   int changed = 0;
16875796c8dcSSimon Schubert   int type_changed = 0;
16885796c8dcSSimon Schubert   int i;
16895796c8dcSSimon Schubert   struct value *new;
16905796c8dcSSimon Schubert   VEC (varobj_update_result) *stack = NULL;
16915796c8dcSSimon Schubert   VEC (varobj_update_result) *result = NULL;
16925796c8dcSSimon Schubert 
16935796c8dcSSimon Schubert   /* Frozen means frozen -- we don't check for any change in
16945796c8dcSSimon Schubert      this varobj, including its going out of scope, or
16955796c8dcSSimon Schubert      changing type.  One use case for frozen varobjs is
16965796c8dcSSimon Schubert      retaining previously evaluated expressions, and we don't
16975796c8dcSSimon Schubert      want them to be reevaluated at all.  */
16985796c8dcSSimon Schubert   if (!explicit && (*varp)->frozen)
16995796c8dcSSimon Schubert     return result;
17005796c8dcSSimon Schubert 
17015796c8dcSSimon Schubert   if (!(*varp)->root->is_valid)
17025796c8dcSSimon Schubert     {
1703*cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1704*cf7f2e2dSJohn Marino 
1705*cf7f2e2dSJohn Marino       r.varobj = *varp;
17065796c8dcSSimon Schubert       r.status = VAROBJ_INVALID;
17075796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, result, &r);
17085796c8dcSSimon Schubert       return result;
17095796c8dcSSimon Schubert     }
17105796c8dcSSimon Schubert 
17115796c8dcSSimon Schubert   if ((*varp)->root->rootvar == *varp)
17125796c8dcSSimon Schubert     {
1713*cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1714*cf7f2e2dSJohn Marino 
1715*cf7f2e2dSJohn Marino       r.varobj = *varp;
17165796c8dcSSimon Schubert       r.status = VAROBJ_IN_SCOPE;
17175796c8dcSSimon Schubert 
17185796c8dcSSimon Schubert       /* Update the root variable. value_of_root can return NULL
17195796c8dcSSimon Schubert 	 if the variable is no longer around, i.e. we stepped out of
17205796c8dcSSimon Schubert 	 the frame in which a local existed. We are letting the
17215796c8dcSSimon Schubert 	 value_of_root variable dispose of the varobj if the type
17225796c8dcSSimon Schubert 	 has changed.  */
17235796c8dcSSimon Schubert       new = value_of_root (varp, &type_changed);
17245796c8dcSSimon Schubert       r.varobj = *varp;
17255796c8dcSSimon Schubert 
17265796c8dcSSimon Schubert       r.type_changed = type_changed;
17275796c8dcSSimon Schubert       if (install_new_value ((*varp), new, type_changed))
17285796c8dcSSimon Schubert 	r.changed = 1;
17295796c8dcSSimon Schubert 
17305796c8dcSSimon Schubert       if (new == NULL)
17315796c8dcSSimon Schubert 	r.status = VAROBJ_NOT_IN_SCOPE;
17325796c8dcSSimon Schubert       r.value_installed = 1;
17335796c8dcSSimon Schubert 
17345796c8dcSSimon Schubert       if (r.status == VAROBJ_NOT_IN_SCOPE)
17355796c8dcSSimon Schubert 	{
17365796c8dcSSimon Schubert 	  if (r.type_changed || r.changed)
17375796c8dcSSimon Schubert 	    VEC_safe_push (varobj_update_result, result, &r);
17385796c8dcSSimon Schubert 	  return result;
17395796c8dcSSimon Schubert 	}
17405796c8dcSSimon Schubert 
17415796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, stack, &r);
17425796c8dcSSimon Schubert     }
17435796c8dcSSimon Schubert   else
17445796c8dcSSimon Schubert     {
1745*cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1746*cf7f2e2dSJohn Marino 
1747*cf7f2e2dSJohn Marino       r.varobj = *varp;
17485796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, stack, &r);
17495796c8dcSSimon Schubert     }
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert   /* Walk through the children, reconstructing them all.  */
17525796c8dcSSimon Schubert   while (!VEC_empty (varobj_update_result, stack))
17535796c8dcSSimon Schubert     {
17545796c8dcSSimon Schubert       varobj_update_result r = *(VEC_last (varobj_update_result, stack));
17555796c8dcSSimon Schubert       struct varobj *v = r.varobj;
17565796c8dcSSimon Schubert 
17575796c8dcSSimon Schubert       VEC_pop (varobj_update_result, stack);
17585796c8dcSSimon Schubert 
17595796c8dcSSimon Schubert       /* Update this variable, unless it's a root, which is already
17605796c8dcSSimon Schubert 	 updated.  */
17615796c8dcSSimon Schubert       if (!r.value_installed)
17625796c8dcSSimon Schubert 	{
17635796c8dcSSimon Schubert 	  new = value_of_child (v->parent, v->index);
17645796c8dcSSimon Schubert 	  if (install_new_value (v, new, 0 /* type not changed */))
17655796c8dcSSimon Schubert 	    {
17665796c8dcSSimon Schubert 	      r.changed = 1;
17675796c8dcSSimon Schubert 	      v->updated = 0;
17685796c8dcSSimon Schubert 	    }
17695796c8dcSSimon Schubert 	}
17705796c8dcSSimon Schubert 
17715796c8dcSSimon Schubert       /* We probably should not get children of a varobj that has a
17725796c8dcSSimon Schubert 	 pretty-printer, but for which -var-list-children was never
17735796c8dcSSimon Schubert 	 invoked.    */
17745796c8dcSSimon Schubert       if (v->pretty_printer)
17755796c8dcSSimon Schubert 	{
17765796c8dcSSimon Schubert 	  VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
17775796c8dcSSimon Schubert 	  int i, children_changed = 0;
17785796c8dcSSimon Schubert 
17795796c8dcSSimon Schubert 	  if (v->frozen)
17805796c8dcSSimon Schubert 	    continue;
17815796c8dcSSimon Schubert 
17825796c8dcSSimon Schubert 	  if (!v->children_requested)
17835796c8dcSSimon Schubert 	    {
17845796c8dcSSimon Schubert 	      int dummy;
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert 	      /* If we initially did not have potential children, but
17875796c8dcSSimon Schubert 		 now we do, consider the varobj as changed.
17885796c8dcSSimon Schubert 		 Otherwise, if children were never requested, consider
17895796c8dcSSimon Schubert 		 it as unchanged -- presumably, such varobj is not yet
17905796c8dcSSimon Schubert 		 expanded in the UI, so we need not bother getting
17915796c8dcSSimon Schubert 		 it.  */
17925796c8dcSSimon Schubert 	      if (!varobj_has_more (v, 0))
17935796c8dcSSimon Schubert 		{
17945796c8dcSSimon Schubert 		  update_dynamic_varobj_children (v, NULL, NULL, NULL,
17955796c8dcSSimon Schubert 						  &dummy, 0, 0, 0);
17965796c8dcSSimon Schubert 		  if (varobj_has_more (v, 0))
17975796c8dcSSimon Schubert 		    r.changed = 1;
17985796c8dcSSimon Schubert 		}
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert 	      if (r.changed)
18015796c8dcSSimon Schubert 		VEC_safe_push (varobj_update_result, result, &r);
18025796c8dcSSimon Schubert 
18035796c8dcSSimon Schubert 	      continue;
18045796c8dcSSimon Schubert 	    }
18055796c8dcSSimon Schubert 
18065796c8dcSSimon Schubert 	  /* If update_dynamic_varobj_children returns 0, then we have
18075796c8dcSSimon Schubert 	     a non-conforming pretty-printer, so we skip it.  */
18085796c8dcSSimon Schubert 	  if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
18095796c8dcSSimon Schubert 					      &children_changed, 1,
18105796c8dcSSimon Schubert 					      v->from, v->to))
18115796c8dcSSimon Schubert 	    {
18125796c8dcSSimon Schubert 	      if (children_changed || new)
18135796c8dcSSimon Schubert 		{
18145796c8dcSSimon Schubert 		  r.children_changed = 1;
18155796c8dcSSimon Schubert 		  r.new = new;
18165796c8dcSSimon Schubert 		}
18175796c8dcSSimon Schubert 	      /* Push in reverse order so that the first child is
18185796c8dcSSimon Schubert 		 popped from the work stack first, and so will be
18195796c8dcSSimon Schubert 		 added to result first.  This does not affect
18205796c8dcSSimon Schubert 		 correctness, just "nicer".  */
18215796c8dcSSimon Schubert 	      for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
18225796c8dcSSimon Schubert 		{
18235796c8dcSSimon Schubert 		  varobj_p tmp = VEC_index (varobj_p, changed, i);
1824*cf7f2e2dSJohn Marino 		  varobj_update_result r = {0};
1825*cf7f2e2dSJohn Marino 
1826*cf7f2e2dSJohn Marino 		  r.varobj = tmp;
18275796c8dcSSimon Schubert 		  r.changed = 1;
18285796c8dcSSimon Schubert 		  r.value_installed = 1;
18295796c8dcSSimon Schubert 		  VEC_safe_push (varobj_update_result, stack, &r);
18305796c8dcSSimon Schubert 		}
18315796c8dcSSimon Schubert 	      for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
18325796c8dcSSimon Schubert 	      	{
18335796c8dcSSimon Schubert 		  varobj_p tmp = VEC_index (varobj_p, unchanged, i);
1834*cf7f2e2dSJohn Marino 
18355796c8dcSSimon Schubert 	      	  if (!tmp->frozen)
18365796c8dcSSimon Schubert 	      	    {
1837*cf7f2e2dSJohn Marino 	      	      varobj_update_result r = {0};
1838*cf7f2e2dSJohn Marino 
1839*cf7f2e2dSJohn Marino 		      r.varobj = tmp;
18405796c8dcSSimon Schubert 	      	      r.value_installed = 1;
18415796c8dcSSimon Schubert 	      	      VEC_safe_push (varobj_update_result, stack, &r);
18425796c8dcSSimon Schubert 	      	    }
18435796c8dcSSimon Schubert 	      	}
18445796c8dcSSimon Schubert 	      if (r.changed || r.children_changed)
18455796c8dcSSimon Schubert 		VEC_safe_push (varobj_update_result, result, &r);
18465796c8dcSSimon Schubert 
18475796c8dcSSimon Schubert 	      /* Free CHANGED and UNCHANGED, but not NEW, because NEW
18485796c8dcSSimon Schubert 		 has been put into the result vector.  */
18495796c8dcSSimon Schubert 	      VEC_free (varobj_p, changed);
18505796c8dcSSimon Schubert 	      VEC_free (varobj_p, unchanged);
18515796c8dcSSimon Schubert 
18525796c8dcSSimon Schubert 	      continue;
18535796c8dcSSimon Schubert 	    }
18545796c8dcSSimon Schubert 	}
18555796c8dcSSimon Schubert 
18565796c8dcSSimon Schubert       /* Push any children.  Use reverse order so that the first
18575796c8dcSSimon Schubert 	 child is popped from the work stack first, and so
18585796c8dcSSimon Schubert 	 will be added to result first.  This does not
18595796c8dcSSimon Schubert 	 affect correctness, just "nicer".  */
18605796c8dcSSimon Schubert       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
18615796c8dcSSimon Schubert 	{
18625796c8dcSSimon Schubert 	  varobj_p c = VEC_index (varobj_p, v->children, i);
1863*cf7f2e2dSJohn Marino 
18645796c8dcSSimon Schubert 	  /* Child may be NULL if explicitly deleted by -var-delete.  */
18655796c8dcSSimon Schubert 	  if (c != NULL && !c->frozen)
18665796c8dcSSimon Schubert 	    {
1867*cf7f2e2dSJohn Marino 	      varobj_update_result r = {0};
1868*cf7f2e2dSJohn Marino 
1869*cf7f2e2dSJohn Marino 	      r.varobj = c;
18705796c8dcSSimon Schubert 	      VEC_safe_push (varobj_update_result, stack, &r);
18715796c8dcSSimon Schubert 	    }
18725796c8dcSSimon Schubert 	}
18735796c8dcSSimon Schubert 
18745796c8dcSSimon Schubert       if (r.changed || r.type_changed)
18755796c8dcSSimon Schubert 	VEC_safe_push (varobj_update_result, result, &r);
18765796c8dcSSimon Schubert     }
18775796c8dcSSimon Schubert 
18785796c8dcSSimon Schubert   VEC_free (varobj_update_result, stack);
18795796c8dcSSimon Schubert 
18805796c8dcSSimon Schubert   return result;
18815796c8dcSSimon Schubert }
18825796c8dcSSimon Schubert 
18835796c8dcSSimon Schubert 
18845796c8dcSSimon Schubert /* Helper functions */
18855796c8dcSSimon Schubert 
18865796c8dcSSimon Schubert /*
18875796c8dcSSimon Schubert  * Variable object construction/destruction
18885796c8dcSSimon Schubert  */
18895796c8dcSSimon Schubert 
18905796c8dcSSimon Schubert static int
18915796c8dcSSimon Schubert delete_variable (struct cpstack **resultp, struct varobj *var,
18925796c8dcSSimon Schubert 		 int only_children_p)
18935796c8dcSSimon Schubert {
18945796c8dcSSimon Schubert   int delcount = 0;
18955796c8dcSSimon Schubert 
18965796c8dcSSimon Schubert   delete_variable_1 (resultp, &delcount, var,
18975796c8dcSSimon Schubert 		     only_children_p, 1 /* remove_from_parent_p */ );
18985796c8dcSSimon Schubert 
18995796c8dcSSimon Schubert   return delcount;
19005796c8dcSSimon Schubert }
19015796c8dcSSimon Schubert 
19025796c8dcSSimon Schubert /* Delete the variable object VAR and its children */
19035796c8dcSSimon Schubert /* IMPORTANT NOTE: If we delete a variable which is a child
19045796c8dcSSimon Schubert    and the parent is not removed we dump core.  It must be always
19055796c8dcSSimon Schubert    initially called with remove_from_parent_p set */
19065796c8dcSSimon Schubert static void
19075796c8dcSSimon Schubert delete_variable_1 (struct cpstack **resultp, int *delcountp,
19085796c8dcSSimon Schubert 		   struct varobj *var, int only_children_p,
19095796c8dcSSimon Schubert 		   int remove_from_parent_p)
19105796c8dcSSimon Schubert {
19115796c8dcSSimon Schubert   int i;
19125796c8dcSSimon Schubert 
19135796c8dcSSimon Schubert   /* Delete any children of this variable, too. */
19145796c8dcSSimon Schubert   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
19155796c8dcSSimon Schubert     {
19165796c8dcSSimon Schubert       varobj_p child = VEC_index (varobj_p, var->children, i);
1917*cf7f2e2dSJohn Marino 
19185796c8dcSSimon Schubert       if (!child)
19195796c8dcSSimon Schubert 	continue;
19205796c8dcSSimon Schubert       if (!remove_from_parent_p)
19215796c8dcSSimon Schubert 	child->parent = NULL;
19225796c8dcSSimon Schubert       delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
19235796c8dcSSimon Schubert     }
19245796c8dcSSimon Schubert   VEC_free (varobj_p, var->children);
19255796c8dcSSimon Schubert 
19265796c8dcSSimon Schubert   /* if we were called to delete only the children we are done here */
19275796c8dcSSimon Schubert   if (only_children_p)
19285796c8dcSSimon Schubert     return;
19295796c8dcSSimon Schubert 
19305796c8dcSSimon Schubert   /* Otherwise, add it to the list of deleted ones and proceed to do so */
19315796c8dcSSimon Schubert   /* If the name is null, this is a temporary variable, that has not
19325796c8dcSSimon Schubert      yet been installed, don't report it, it belongs to the caller... */
19335796c8dcSSimon Schubert   if (var->obj_name != NULL)
19345796c8dcSSimon Schubert     {
19355796c8dcSSimon Schubert       cppush (resultp, xstrdup (var->obj_name));
19365796c8dcSSimon Schubert       *delcountp = *delcountp + 1;
19375796c8dcSSimon Schubert     }
19385796c8dcSSimon Schubert 
19395796c8dcSSimon Schubert   /* If this variable has a parent, remove it from its parent's list */
19405796c8dcSSimon Schubert   /* OPTIMIZATION: if the parent of this variable is also being deleted,
19415796c8dcSSimon Schubert      (as indicated by remove_from_parent_p) we don't bother doing an
19425796c8dcSSimon Schubert      expensive list search to find the element to remove when we are
19435796c8dcSSimon Schubert      discarding the list afterwards */
19445796c8dcSSimon Schubert   if ((remove_from_parent_p) && (var->parent != NULL))
19455796c8dcSSimon Schubert     {
19465796c8dcSSimon Schubert       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
19475796c8dcSSimon Schubert     }
19485796c8dcSSimon Schubert 
19495796c8dcSSimon Schubert   if (var->obj_name != NULL)
19505796c8dcSSimon Schubert     uninstall_variable (var);
19515796c8dcSSimon Schubert 
19525796c8dcSSimon Schubert   /* Free memory associated with this variable */
19535796c8dcSSimon Schubert   free_variable (var);
19545796c8dcSSimon Schubert }
19555796c8dcSSimon Schubert 
19565796c8dcSSimon Schubert /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
19575796c8dcSSimon Schubert static int
19585796c8dcSSimon Schubert install_variable (struct varobj *var)
19595796c8dcSSimon Schubert {
19605796c8dcSSimon Schubert   struct vlist *cv;
19615796c8dcSSimon Schubert   struct vlist *newvl;
19625796c8dcSSimon Schubert   const char *chp;
19635796c8dcSSimon Schubert   unsigned int index = 0;
19645796c8dcSSimon Schubert   unsigned int i = 1;
19655796c8dcSSimon Schubert 
19665796c8dcSSimon Schubert   for (chp = var->obj_name; *chp; chp++)
19675796c8dcSSimon Schubert     {
19685796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
19695796c8dcSSimon Schubert     }
19705796c8dcSSimon Schubert 
19715796c8dcSSimon Schubert   cv = *(varobj_table + index);
19725796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
19735796c8dcSSimon Schubert     cv = cv->next;
19745796c8dcSSimon Schubert 
19755796c8dcSSimon Schubert   if (cv != NULL)
19765796c8dcSSimon Schubert     error (_("Duplicate variable object name"));
19775796c8dcSSimon Schubert 
19785796c8dcSSimon Schubert   /* Add varobj to hash table */
19795796c8dcSSimon Schubert   newvl = xmalloc (sizeof (struct vlist));
19805796c8dcSSimon Schubert   newvl->next = *(varobj_table + index);
19815796c8dcSSimon Schubert   newvl->var = var;
19825796c8dcSSimon Schubert   *(varobj_table + index) = newvl;
19835796c8dcSSimon Schubert 
19845796c8dcSSimon Schubert   /* If root, add varobj to root list */
19855796c8dcSSimon Schubert   if (is_root_p (var))
19865796c8dcSSimon Schubert     {
19875796c8dcSSimon Schubert       /* Add to list of root variables */
19885796c8dcSSimon Schubert       if (rootlist == NULL)
19895796c8dcSSimon Schubert 	var->root->next = NULL;
19905796c8dcSSimon Schubert       else
19915796c8dcSSimon Schubert 	var->root->next = rootlist;
19925796c8dcSSimon Schubert       rootlist = var->root;
19935796c8dcSSimon Schubert     }
19945796c8dcSSimon Schubert 
19955796c8dcSSimon Schubert   return 1;			/* OK */
19965796c8dcSSimon Schubert }
19975796c8dcSSimon Schubert 
19985796c8dcSSimon Schubert /* Unistall the object VAR. */
19995796c8dcSSimon Schubert static void
20005796c8dcSSimon Schubert uninstall_variable (struct varobj *var)
20015796c8dcSSimon Schubert {
20025796c8dcSSimon Schubert   struct vlist *cv;
20035796c8dcSSimon Schubert   struct vlist *prev;
20045796c8dcSSimon Schubert   struct varobj_root *cr;
20055796c8dcSSimon Schubert   struct varobj_root *prer;
20065796c8dcSSimon Schubert   const char *chp;
20075796c8dcSSimon Schubert   unsigned int index = 0;
20085796c8dcSSimon Schubert   unsigned int i = 1;
20095796c8dcSSimon Schubert 
20105796c8dcSSimon Schubert   /* Remove varobj from hash table */
20115796c8dcSSimon Schubert   for (chp = var->obj_name; *chp; chp++)
20125796c8dcSSimon Schubert     {
20135796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
20145796c8dcSSimon Schubert     }
20155796c8dcSSimon Schubert 
20165796c8dcSSimon Schubert   cv = *(varobj_table + index);
20175796c8dcSSimon Schubert   prev = NULL;
20185796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
20195796c8dcSSimon Schubert     {
20205796c8dcSSimon Schubert       prev = cv;
20215796c8dcSSimon Schubert       cv = cv->next;
20225796c8dcSSimon Schubert     }
20235796c8dcSSimon Schubert 
20245796c8dcSSimon Schubert   if (varobjdebug)
20255796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
20265796c8dcSSimon Schubert 
20275796c8dcSSimon Schubert   if (cv == NULL)
20285796c8dcSSimon Schubert     {
20295796c8dcSSimon Schubert       warning
20305796c8dcSSimon Schubert 	("Assertion failed: Could not find variable object \"%s\" to delete",
20315796c8dcSSimon Schubert 	 var->obj_name);
20325796c8dcSSimon Schubert       return;
20335796c8dcSSimon Schubert     }
20345796c8dcSSimon Schubert 
20355796c8dcSSimon Schubert   if (prev == NULL)
20365796c8dcSSimon Schubert     *(varobj_table + index) = cv->next;
20375796c8dcSSimon Schubert   else
20385796c8dcSSimon Schubert     prev->next = cv->next;
20395796c8dcSSimon Schubert 
20405796c8dcSSimon Schubert   xfree (cv);
20415796c8dcSSimon Schubert 
20425796c8dcSSimon Schubert   /* If root, remove varobj from root list */
20435796c8dcSSimon Schubert   if (is_root_p (var))
20445796c8dcSSimon Schubert     {
20455796c8dcSSimon Schubert       /* Remove from list of root variables */
20465796c8dcSSimon Schubert       if (rootlist == var->root)
20475796c8dcSSimon Schubert 	rootlist = var->root->next;
20485796c8dcSSimon Schubert       else
20495796c8dcSSimon Schubert 	{
20505796c8dcSSimon Schubert 	  prer = NULL;
20515796c8dcSSimon Schubert 	  cr = rootlist;
20525796c8dcSSimon Schubert 	  while ((cr != NULL) && (cr->rootvar != var))
20535796c8dcSSimon Schubert 	    {
20545796c8dcSSimon Schubert 	      prer = cr;
20555796c8dcSSimon Schubert 	      cr = cr->next;
20565796c8dcSSimon Schubert 	    }
20575796c8dcSSimon Schubert 	  if (cr == NULL)
20585796c8dcSSimon Schubert 	    {
20595796c8dcSSimon Schubert 	      warning
20605796c8dcSSimon Schubert 		("Assertion failed: Could not find varobj \"%s\" in root list",
20615796c8dcSSimon Schubert 		 var->obj_name);
20625796c8dcSSimon Schubert 	      return;
20635796c8dcSSimon Schubert 	    }
20645796c8dcSSimon Schubert 	  if (prer == NULL)
20655796c8dcSSimon Schubert 	    rootlist = NULL;
20665796c8dcSSimon Schubert 	  else
20675796c8dcSSimon Schubert 	    prer->next = cr->next;
20685796c8dcSSimon Schubert 	}
20695796c8dcSSimon Schubert     }
20705796c8dcSSimon Schubert 
20715796c8dcSSimon Schubert }
20725796c8dcSSimon Schubert 
20735796c8dcSSimon Schubert /* Create and install a child of the parent of the given name */
20745796c8dcSSimon Schubert static struct varobj *
20755796c8dcSSimon Schubert create_child (struct varobj *parent, int index, char *name)
20765796c8dcSSimon Schubert {
20775796c8dcSSimon Schubert   return create_child_with_value (parent, index, name,
20785796c8dcSSimon Schubert 				  value_of_child (parent, index));
20795796c8dcSSimon Schubert }
20805796c8dcSSimon Schubert 
20815796c8dcSSimon Schubert static struct varobj *
20825796c8dcSSimon Schubert create_child_with_value (struct varobj *parent, int index, const char *name,
20835796c8dcSSimon Schubert 			 struct value *value)
20845796c8dcSSimon Schubert {
20855796c8dcSSimon Schubert   struct varobj *child;
20865796c8dcSSimon Schubert   char *childs_name;
20875796c8dcSSimon Schubert 
20885796c8dcSSimon Schubert   child = new_variable ();
20895796c8dcSSimon Schubert 
20905796c8dcSSimon Schubert   /* name is allocated by name_of_child */
20915796c8dcSSimon Schubert   /* FIXME: xstrdup should not be here.  */
20925796c8dcSSimon Schubert   child->name = xstrdup (name);
20935796c8dcSSimon Schubert   child->index = index;
20945796c8dcSSimon Schubert   child->parent = parent;
20955796c8dcSSimon Schubert   child->root = parent->root;
20965796c8dcSSimon Schubert   childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
20975796c8dcSSimon Schubert   child->obj_name = childs_name;
20985796c8dcSSimon Schubert   install_variable (child);
20995796c8dcSSimon Schubert 
21005796c8dcSSimon Schubert   /* Compute the type of the child.  Must do this before
21015796c8dcSSimon Schubert      calling install_new_value.  */
21025796c8dcSSimon Schubert   if (value != NULL)
21035796c8dcSSimon Schubert     /* If the child had no evaluation errors, var->value
21045796c8dcSSimon Schubert        will be non-NULL and contain a valid type. */
21055796c8dcSSimon Schubert     child->type = value_type (value);
21065796c8dcSSimon Schubert   else
21075796c8dcSSimon Schubert     /* Otherwise, we must compute the type. */
21085796c8dcSSimon Schubert     child->type = (*child->root->lang->type_of_child) (child->parent,
21095796c8dcSSimon Schubert 						       child->index);
21105796c8dcSSimon Schubert   install_new_value (child, value, 1);
21115796c8dcSSimon Schubert 
21125796c8dcSSimon Schubert   return child;
21135796c8dcSSimon Schubert }
21145796c8dcSSimon Schubert 
21155796c8dcSSimon Schubert 
21165796c8dcSSimon Schubert /*
21175796c8dcSSimon Schubert  * Miscellaneous utility functions.
21185796c8dcSSimon Schubert  */
21195796c8dcSSimon Schubert 
21205796c8dcSSimon Schubert /* Allocate memory and initialize a new variable */
21215796c8dcSSimon Schubert static struct varobj *
21225796c8dcSSimon Schubert new_variable (void)
21235796c8dcSSimon Schubert {
21245796c8dcSSimon Schubert   struct varobj *var;
21255796c8dcSSimon Schubert 
21265796c8dcSSimon Schubert   var = (struct varobj *) xmalloc (sizeof (struct varobj));
21275796c8dcSSimon Schubert   var->name = NULL;
21285796c8dcSSimon Schubert   var->path_expr = NULL;
21295796c8dcSSimon Schubert   var->obj_name = NULL;
21305796c8dcSSimon Schubert   var->index = -1;
21315796c8dcSSimon Schubert   var->type = NULL;
21325796c8dcSSimon Schubert   var->value = NULL;
21335796c8dcSSimon Schubert   var->num_children = -1;
21345796c8dcSSimon Schubert   var->parent = NULL;
21355796c8dcSSimon Schubert   var->children = NULL;
21365796c8dcSSimon Schubert   var->format = 0;
21375796c8dcSSimon Schubert   var->root = NULL;
21385796c8dcSSimon Schubert   var->updated = 0;
21395796c8dcSSimon Schubert   var->print_value = NULL;
21405796c8dcSSimon Schubert   var->frozen = 0;
21415796c8dcSSimon Schubert   var->not_fetched = 0;
21425796c8dcSSimon Schubert   var->children_requested = 0;
21435796c8dcSSimon Schubert   var->from = -1;
21445796c8dcSSimon Schubert   var->to = -1;
21455796c8dcSSimon Schubert   var->constructor = 0;
21465796c8dcSSimon Schubert   var->pretty_printer = 0;
21475796c8dcSSimon Schubert   var->child_iter = 0;
21485796c8dcSSimon Schubert   var->saved_item = 0;
21495796c8dcSSimon Schubert 
21505796c8dcSSimon Schubert   return var;
21515796c8dcSSimon Schubert }
21525796c8dcSSimon Schubert 
21535796c8dcSSimon Schubert /* Allocate memory and initialize a new root variable */
21545796c8dcSSimon Schubert static struct varobj *
21555796c8dcSSimon Schubert new_root_variable (void)
21565796c8dcSSimon Schubert {
21575796c8dcSSimon Schubert   struct varobj *var = new_variable ();
2158*cf7f2e2dSJohn Marino 
21595796c8dcSSimon Schubert   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
21605796c8dcSSimon Schubert   var->root->lang = NULL;
21615796c8dcSSimon Schubert   var->root->exp = NULL;
21625796c8dcSSimon Schubert   var->root->valid_block = NULL;
21635796c8dcSSimon Schubert   var->root->frame = null_frame_id;
21645796c8dcSSimon Schubert   var->root->floating = 0;
21655796c8dcSSimon Schubert   var->root->rootvar = NULL;
21665796c8dcSSimon Schubert   var->root->is_valid = 1;
21675796c8dcSSimon Schubert 
21685796c8dcSSimon Schubert   return var;
21695796c8dcSSimon Schubert }
21705796c8dcSSimon Schubert 
21715796c8dcSSimon Schubert /* Free any allocated memory associated with VAR. */
21725796c8dcSSimon Schubert static void
21735796c8dcSSimon Schubert free_variable (struct varobj *var)
21745796c8dcSSimon Schubert {
21755796c8dcSSimon Schubert #if HAVE_PYTHON
21765796c8dcSSimon Schubert   if (var->pretty_printer)
21775796c8dcSSimon Schubert     {
21785796c8dcSSimon Schubert       struct cleanup *cleanup = varobj_ensure_python_env (var);
21795796c8dcSSimon Schubert       Py_XDECREF (var->constructor);
21805796c8dcSSimon Schubert       Py_XDECREF (var->pretty_printer);
21815796c8dcSSimon Schubert       Py_XDECREF (var->child_iter);
21825796c8dcSSimon Schubert       Py_XDECREF (var->saved_item);
21835796c8dcSSimon Schubert       do_cleanups (cleanup);
21845796c8dcSSimon Schubert     }
21855796c8dcSSimon Schubert #endif
21865796c8dcSSimon Schubert 
21875796c8dcSSimon Schubert   value_free (var->value);
21885796c8dcSSimon Schubert 
21895796c8dcSSimon Schubert   /* Free the expression if this is a root variable. */
21905796c8dcSSimon Schubert   if (is_root_p (var))
21915796c8dcSSimon Schubert     {
21925796c8dcSSimon Schubert       xfree (var->root->exp);
21935796c8dcSSimon Schubert       xfree (var->root);
21945796c8dcSSimon Schubert     }
21955796c8dcSSimon Schubert 
21965796c8dcSSimon Schubert   xfree (var->name);
21975796c8dcSSimon Schubert   xfree (var->obj_name);
21985796c8dcSSimon Schubert   xfree (var->print_value);
21995796c8dcSSimon Schubert   xfree (var->path_expr);
22005796c8dcSSimon Schubert   xfree (var);
22015796c8dcSSimon Schubert }
22025796c8dcSSimon Schubert 
22035796c8dcSSimon Schubert static void
22045796c8dcSSimon Schubert do_free_variable_cleanup (void *var)
22055796c8dcSSimon Schubert {
22065796c8dcSSimon Schubert   free_variable (var);
22075796c8dcSSimon Schubert }
22085796c8dcSSimon Schubert 
22095796c8dcSSimon Schubert static struct cleanup *
22105796c8dcSSimon Schubert make_cleanup_free_variable (struct varobj *var)
22115796c8dcSSimon Schubert {
22125796c8dcSSimon Schubert   return make_cleanup (do_free_variable_cleanup, var);
22135796c8dcSSimon Schubert }
22145796c8dcSSimon Schubert 
22155796c8dcSSimon Schubert /* This returns the type of the variable. It also skips past typedefs
22165796c8dcSSimon Schubert    to return the real type of the variable.
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
22195796c8dcSSimon Schubert    except within get_target_type and get_type. */
22205796c8dcSSimon Schubert static struct type *
22215796c8dcSSimon Schubert get_type (struct varobj *var)
22225796c8dcSSimon Schubert {
22235796c8dcSSimon Schubert   struct type *type;
22245796c8dcSSimon Schubert 
2225*cf7f2e2dSJohn Marino   type = var->type;
22265796c8dcSSimon Schubert   if (type != NULL)
22275796c8dcSSimon Schubert     type = check_typedef (type);
22285796c8dcSSimon Schubert 
22295796c8dcSSimon Schubert   return type;
22305796c8dcSSimon Schubert }
22315796c8dcSSimon Schubert 
22325796c8dcSSimon Schubert /* Return the type of the value that's stored in VAR,
22335796c8dcSSimon Schubert    or that would have being stored there if the
22345796c8dcSSimon Schubert    value were accessible.
22355796c8dcSSimon Schubert 
22365796c8dcSSimon Schubert    This differs from VAR->type in that VAR->type is always
22375796c8dcSSimon Schubert    the true type of the expession in the source language.
22385796c8dcSSimon Schubert    The return value of this function is the type we're
22395796c8dcSSimon Schubert    actually storing in varobj, and using for displaying
22405796c8dcSSimon Schubert    the values and for comparing previous and new values.
22415796c8dcSSimon Schubert 
22425796c8dcSSimon Schubert    For example, top-level references are always stripped.  */
22435796c8dcSSimon Schubert static struct type *
22445796c8dcSSimon Schubert get_value_type (struct varobj *var)
22455796c8dcSSimon Schubert {
22465796c8dcSSimon Schubert   struct type *type;
22475796c8dcSSimon Schubert 
22485796c8dcSSimon Schubert   if (var->value)
22495796c8dcSSimon Schubert     type = value_type (var->value);
22505796c8dcSSimon Schubert   else
22515796c8dcSSimon Schubert     type = var->type;
22525796c8dcSSimon Schubert 
22535796c8dcSSimon Schubert   type = check_typedef (type);
22545796c8dcSSimon Schubert 
22555796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
22565796c8dcSSimon Schubert     type = get_target_type (type);
22575796c8dcSSimon Schubert 
22585796c8dcSSimon Schubert   type = check_typedef (type);
22595796c8dcSSimon Schubert 
22605796c8dcSSimon Schubert   return type;
22615796c8dcSSimon Schubert }
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert /* This returns the target type (or NULL) of TYPE, also skipping
22645796c8dcSSimon Schubert    past typedefs, just like get_type ().
22655796c8dcSSimon Schubert 
22665796c8dcSSimon Schubert    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
22675796c8dcSSimon Schubert    except within get_target_type and get_type. */
22685796c8dcSSimon Schubert static struct type *
22695796c8dcSSimon Schubert get_target_type (struct type *type)
22705796c8dcSSimon Schubert {
22715796c8dcSSimon Schubert   if (type != NULL)
22725796c8dcSSimon Schubert     {
22735796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
22745796c8dcSSimon Schubert       if (type != NULL)
22755796c8dcSSimon Schubert 	type = check_typedef (type);
22765796c8dcSSimon Schubert     }
22775796c8dcSSimon Schubert 
22785796c8dcSSimon Schubert   return type;
22795796c8dcSSimon Schubert }
22805796c8dcSSimon Schubert 
22815796c8dcSSimon Schubert /* What is the default display for this variable? We assume that
22825796c8dcSSimon Schubert    everything is "natural". Any exceptions? */
22835796c8dcSSimon Schubert static enum varobj_display_formats
22845796c8dcSSimon Schubert variable_default_display (struct varobj *var)
22855796c8dcSSimon Schubert {
22865796c8dcSSimon Schubert   return FORMAT_NATURAL;
22875796c8dcSSimon Schubert }
22885796c8dcSSimon Schubert 
22895796c8dcSSimon Schubert /* FIXME: The following should be generic for any pointer */
22905796c8dcSSimon Schubert static void
22915796c8dcSSimon Schubert cppush (struct cpstack **pstack, char *name)
22925796c8dcSSimon Schubert {
22935796c8dcSSimon Schubert   struct cpstack *s;
22945796c8dcSSimon Schubert 
22955796c8dcSSimon Schubert   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
22965796c8dcSSimon Schubert   s->name = name;
22975796c8dcSSimon Schubert   s->next = *pstack;
22985796c8dcSSimon Schubert   *pstack = s;
22995796c8dcSSimon Schubert }
23005796c8dcSSimon Schubert 
23015796c8dcSSimon Schubert /* FIXME: The following should be generic for any pointer */
23025796c8dcSSimon Schubert static char *
23035796c8dcSSimon Schubert cppop (struct cpstack **pstack)
23045796c8dcSSimon Schubert {
23055796c8dcSSimon Schubert   struct cpstack *s;
23065796c8dcSSimon Schubert   char *v;
23075796c8dcSSimon Schubert 
23085796c8dcSSimon Schubert   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
23095796c8dcSSimon Schubert     return NULL;
23105796c8dcSSimon Schubert 
23115796c8dcSSimon Schubert   s = *pstack;
23125796c8dcSSimon Schubert   v = s->name;
23135796c8dcSSimon Schubert   *pstack = (*pstack)->next;
23145796c8dcSSimon Schubert   xfree (s);
23155796c8dcSSimon Schubert 
23165796c8dcSSimon Schubert   return v;
23175796c8dcSSimon Schubert }
23185796c8dcSSimon Schubert 
23195796c8dcSSimon Schubert /*
23205796c8dcSSimon Schubert  * Language-dependencies
23215796c8dcSSimon Schubert  */
23225796c8dcSSimon Schubert 
23235796c8dcSSimon Schubert /* Common entry points */
23245796c8dcSSimon Schubert 
23255796c8dcSSimon Schubert /* Get the language of variable VAR. */
23265796c8dcSSimon Schubert static enum varobj_languages
23275796c8dcSSimon Schubert variable_language (struct varobj *var)
23285796c8dcSSimon Schubert {
23295796c8dcSSimon Schubert   enum varobj_languages lang;
23305796c8dcSSimon Schubert 
23315796c8dcSSimon Schubert   switch (var->root->exp->language_defn->la_language)
23325796c8dcSSimon Schubert     {
23335796c8dcSSimon Schubert     default:
23345796c8dcSSimon Schubert     case language_c:
23355796c8dcSSimon Schubert       lang = vlang_c;
23365796c8dcSSimon Schubert       break;
23375796c8dcSSimon Schubert     case language_cplus:
23385796c8dcSSimon Schubert       lang = vlang_cplus;
23395796c8dcSSimon Schubert       break;
23405796c8dcSSimon Schubert     case language_java:
23415796c8dcSSimon Schubert       lang = vlang_java;
23425796c8dcSSimon Schubert       break;
23435796c8dcSSimon Schubert     }
23445796c8dcSSimon Schubert 
23455796c8dcSSimon Schubert   return lang;
23465796c8dcSSimon Schubert }
23475796c8dcSSimon Schubert 
23485796c8dcSSimon Schubert /* Return the number of children for a given variable.
23495796c8dcSSimon Schubert    The result of this function is defined by the language
23505796c8dcSSimon Schubert    implementation. The number of children returned by this function
23515796c8dcSSimon Schubert    is the number of children that the user will see in the variable
23525796c8dcSSimon Schubert    display. */
23535796c8dcSSimon Schubert static int
23545796c8dcSSimon Schubert number_of_children (struct varobj *var)
23555796c8dcSSimon Schubert {
23565796c8dcSSimon Schubert   return (*var->root->lang->number_of_children) (var);;
23575796c8dcSSimon Schubert }
23585796c8dcSSimon Schubert 
23595796c8dcSSimon Schubert /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
23605796c8dcSSimon Schubert static char *
23615796c8dcSSimon Schubert name_of_variable (struct varobj *var)
23625796c8dcSSimon Schubert {
23635796c8dcSSimon Schubert   return (*var->root->lang->name_of_variable) (var);
23645796c8dcSSimon Schubert }
23655796c8dcSSimon Schubert 
23665796c8dcSSimon Schubert /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
23675796c8dcSSimon Schubert static char *
23685796c8dcSSimon Schubert name_of_child (struct varobj *var, int index)
23695796c8dcSSimon Schubert {
23705796c8dcSSimon Schubert   return (*var->root->lang->name_of_child) (var, index);
23715796c8dcSSimon Schubert }
23725796c8dcSSimon Schubert 
23735796c8dcSSimon Schubert /* What is the ``struct value *'' of the root variable VAR?
23745796c8dcSSimon Schubert    For floating variable object, evaluation can get us a value
23755796c8dcSSimon Schubert    of different type from what is stored in varobj already.  In
23765796c8dcSSimon Schubert    that case:
23775796c8dcSSimon Schubert    - *type_changed will be set to 1
23785796c8dcSSimon Schubert    - old varobj will be freed, and new one will be
23795796c8dcSSimon Schubert    created, with the same name.
23805796c8dcSSimon Schubert    - *var_handle will be set to the new varobj
23815796c8dcSSimon Schubert    Otherwise, *type_changed will be set to 0.  */
23825796c8dcSSimon Schubert static struct value *
23835796c8dcSSimon Schubert value_of_root (struct varobj **var_handle, int *type_changed)
23845796c8dcSSimon Schubert {
23855796c8dcSSimon Schubert   struct varobj *var;
23865796c8dcSSimon Schubert 
23875796c8dcSSimon Schubert   if (var_handle == NULL)
23885796c8dcSSimon Schubert     return NULL;
23895796c8dcSSimon Schubert 
23905796c8dcSSimon Schubert   var = *var_handle;
23915796c8dcSSimon Schubert 
23925796c8dcSSimon Schubert   /* This should really be an exception, since this should
23935796c8dcSSimon Schubert      only get called with a root variable. */
23945796c8dcSSimon Schubert 
23955796c8dcSSimon Schubert   if (!is_root_p (var))
23965796c8dcSSimon Schubert     return NULL;
23975796c8dcSSimon Schubert 
23985796c8dcSSimon Schubert   if (var->root->floating)
23995796c8dcSSimon Schubert     {
24005796c8dcSSimon Schubert       struct varobj *tmp_var;
24015796c8dcSSimon Schubert       char *old_type, *new_type;
24025796c8dcSSimon Schubert 
24035796c8dcSSimon Schubert       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
24045796c8dcSSimon Schubert 			       USE_SELECTED_FRAME);
24055796c8dcSSimon Schubert       if (tmp_var == NULL)
24065796c8dcSSimon Schubert 	{
24075796c8dcSSimon Schubert 	  return NULL;
24085796c8dcSSimon Schubert 	}
24095796c8dcSSimon Schubert       old_type = varobj_get_type (var);
24105796c8dcSSimon Schubert       new_type = varobj_get_type (tmp_var);
24115796c8dcSSimon Schubert       if (strcmp (old_type, new_type) == 0)
24125796c8dcSSimon Schubert 	{
24135796c8dcSSimon Schubert 	  /* The expression presently stored inside var->root->exp
24145796c8dcSSimon Schubert 	     remembers the locations of local variables relatively to
24155796c8dcSSimon Schubert 	     the frame where the expression was created (in DWARF location
24165796c8dcSSimon Schubert 	     button, for example).  Naturally, those locations are not
24175796c8dcSSimon Schubert 	     correct in other frames, so update the expression.  */
24185796c8dcSSimon Schubert 
24195796c8dcSSimon Schubert          struct expression *tmp_exp = var->root->exp;
2420*cf7f2e2dSJohn Marino 
24215796c8dcSSimon Schubert          var->root->exp = tmp_var->root->exp;
24225796c8dcSSimon Schubert          tmp_var->root->exp = tmp_exp;
24235796c8dcSSimon Schubert 
24245796c8dcSSimon Schubert 	  varobj_delete (tmp_var, NULL, 0);
24255796c8dcSSimon Schubert 	  *type_changed = 0;
24265796c8dcSSimon Schubert 	}
24275796c8dcSSimon Schubert       else
24285796c8dcSSimon Schubert 	{
24295796c8dcSSimon Schubert 	  tmp_var->obj_name = xstrdup (var->obj_name);
24305796c8dcSSimon Schubert 	  tmp_var->from = var->from;
24315796c8dcSSimon Schubert 	  tmp_var->to = var->to;
24325796c8dcSSimon Schubert 	  varobj_delete (var, NULL, 0);
24335796c8dcSSimon Schubert 
24345796c8dcSSimon Schubert 	  install_variable (tmp_var);
24355796c8dcSSimon Schubert 	  *var_handle = tmp_var;
24365796c8dcSSimon Schubert 	  var = *var_handle;
24375796c8dcSSimon Schubert 	  *type_changed = 1;
24385796c8dcSSimon Schubert 	}
24395796c8dcSSimon Schubert       xfree (old_type);
24405796c8dcSSimon Schubert       xfree (new_type);
24415796c8dcSSimon Schubert     }
24425796c8dcSSimon Schubert   else
24435796c8dcSSimon Schubert     {
24445796c8dcSSimon Schubert       *type_changed = 0;
24455796c8dcSSimon Schubert     }
24465796c8dcSSimon Schubert 
24475796c8dcSSimon Schubert   return (*var->root->lang->value_of_root) (var_handle);
24485796c8dcSSimon Schubert }
24495796c8dcSSimon Schubert 
24505796c8dcSSimon Schubert /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
24515796c8dcSSimon Schubert static struct value *
24525796c8dcSSimon Schubert value_of_child (struct varobj *parent, int index)
24535796c8dcSSimon Schubert {
24545796c8dcSSimon Schubert   struct value *value;
24555796c8dcSSimon Schubert 
24565796c8dcSSimon Schubert   value = (*parent->root->lang->value_of_child) (parent, index);
24575796c8dcSSimon Schubert 
24585796c8dcSSimon Schubert   return value;
24595796c8dcSSimon Schubert }
24605796c8dcSSimon Schubert 
24615796c8dcSSimon Schubert /* GDB already has a command called "value_of_variable". Sigh. */
24625796c8dcSSimon Schubert static char *
24635796c8dcSSimon Schubert my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
24645796c8dcSSimon Schubert {
24655796c8dcSSimon Schubert   if (var->root->is_valid)
24665796c8dcSSimon Schubert     {
24675796c8dcSSimon Schubert       if (var->pretty_printer)
24685796c8dcSSimon Schubert 	return value_get_print_value (var->value, var->format, var);
24695796c8dcSSimon Schubert       return (*var->root->lang->value_of_variable) (var, format);
24705796c8dcSSimon Schubert     }
24715796c8dcSSimon Schubert   else
24725796c8dcSSimon Schubert     return NULL;
24735796c8dcSSimon Schubert }
24745796c8dcSSimon Schubert 
24755796c8dcSSimon Schubert static char *
24765796c8dcSSimon Schubert value_get_print_value (struct value *value, enum varobj_display_formats format,
24775796c8dcSSimon Schubert 		       struct varobj *var)
24785796c8dcSSimon Schubert {
24795796c8dcSSimon Schubert   struct ui_file *stb;
24805796c8dcSSimon Schubert   struct cleanup *old_chain;
24815796c8dcSSimon Schubert   gdb_byte *thevalue = NULL;
24825796c8dcSSimon Schubert   struct value_print_options opts;
2483*cf7f2e2dSJohn Marino   struct type *type = NULL;
2484*cf7f2e2dSJohn Marino   long len = 0;
2485*cf7f2e2dSJohn Marino   char *encoding = NULL;
2486*cf7f2e2dSJohn Marino   struct gdbarch *gdbarch = NULL;
24875796c8dcSSimon Schubert 
24885796c8dcSSimon Schubert   if (value == NULL)
24895796c8dcSSimon Schubert     return NULL;
24905796c8dcSSimon Schubert 
2491*cf7f2e2dSJohn Marino   gdbarch = get_type_arch (value_type (value));
24925796c8dcSSimon Schubert #if HAVE_PYTHON
24935796c8dcSSimon Schubert   {
24945796c8dcSSimon Schubert     struct cleanup *back_to = varobj_ensure_python_env (var);
24955796c8dcSSimon Schubert     PyObject *value_formatter = var->pretty_printer;
24965796c8dcSSimon Schubert 
24975796c8dcSSimon Schubert     if (value_formatter)
24985796c8dcSSimon Schubert       {
24995796c8dcSSimon Schubert 	/* First check to see if we have any children at all.  If so,
25005796c8dcSSimon Schubert 	   we simply return {...}.  */
25015796c8dcSSimon Schubert 	if (dynamic_varobj_has_child_method (var))
25025796c8dcSSimon Schubert 	  return xstrdup ("{...}");
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert 	if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
25055796c8dcSSimon Schubert 	  {
25065796c8dcSSimon Schubert 	    char *hint;
25075796c8dcSSimon Schubert 	    struct value *replacement;
25085796c8dcSSimon Schubert 	    int string_print = 0;
25095796c8dcSSimon Schubert 	    PyObject *output = NULL;
25105796c8dcSSimon Schubert 
25115796c8dcSSimon Schubert 	    hint = gdbpy_get_display_hint (value_formatter);
25125796c8dcSSimon Schubert 	    if (hint)
25135796c8dcSSimon Schubert 	      {
25145796c8dcSSimon Schubert 		if (!strcmp (hint, "string"))
25155796c8dcSSimon Schubert 		  string_print = 1;
25165796c8dcSSimon Schubert 		xfree (hint);
25175796c8dcSSimon Schubert 	      }
25185796c8dcSSimon Schubert 
25195796c8dcSSimon Schubert 	    output = apply_varobj_pretty_printer (value_formatter,
25205796c8dcSSimon Schubert 						  &replacement);
25215796c8dcSSimon Schubert 	    if (output)
25225796c8dcSSimon Schubert 	      {
2523*cf7f2e2dSJohn Marino 		if (gdbpy_is_lazy_string (output))
2524*cf7f2e2dSJohn Marino 		  {
2525*cf7f2e2dSJohn Marino 		    thevalue = gdbpy_extract_lazy_string (output, &type,
2526*cf7f2e2dSJohn Marino 							  &len, &encoding);
2527*cf7f2e2dSJohn Marino 		    string_print = 1;
2528*cf7f2e2dSJohn Marino 		  }
2529*cf7f2e2dSJohn Marino 		else
2530*cf7f2e2dSJohn Marino 		  {
25315796c8dcSSimon Schubert 		    PyObject *py_str
25325796c8dcSSimon Schubert 		      = python_string_to_target_python_string (output);
2533*cf7f2e2dSJohn Marino 
25345796c8dcSSimon Schubert 		    if (py_str)
25355796c8dcSSimon Schubert 		      {
25365796c8dcSSimon Schubert 			char *s = PyString_AsString (py_str);
2537*cf7f2e2dSJohn Marino 
25385796c8dcSSimon Schubert 			len = PyString_Size (py_str);
25395796c8dcSSimon Schubert 			thevalue = xmemdup (s, len + 1, len + 1);
2540*cf7f2e2dSJohn Marino 			type = builtin_type (gdbarch)->builtin_char;
25415796c8dcSSimon Schubert 			Py_DECREF (py_str);
25425796c8dcSSimon Schubert 		      }
2543*cf7f2e2dSJohn Marino 		  }
25445796c8dcSSimon Schubert 		Py_DECREF (output);
25455796c8dcSSimon Schubert 	      }
25465796c8dcSSimon Schubert 	    if (thevalue && !string_print)
25475796c8dcSSimon Schubert 	      {
25485796c8dcSSimon Schubert 		do_cleanups (back_to);
2549*cf7f2e2dSJohn Marino 		xfree (encoding);
25505796c8dcSSimon Schubert 		return thevalue;
25515796c8dcSSimon Schubert 	      }
25525796c8dcSSimon Schubert 	    if (replacement)
25535796c8dcSSimon Schubert 	      value = replacement;
25545796c8dcSSimon Schubert 	  }
25555796c8dcSSimon Schubert       }
25565796c8dcSSimon Schubert     do_cleanups (back_to);
25575796c8dcSSimon Schubert   }
25585796c8dcSSimon Schubert #endif
25595796c8dcSSimon Schubert 
25605796c8dcSSimon Schubert   stb = mem_fileopen ();
25615796c8dcSSimon Schubert   old_chain = make_cleanup_ui_file_delete (stb);
25625796c8dcSSimon Schubert 
25635796c8dcSSimon Schubert   get_formatted_print_options (&opts, format_code[(int) format]);
25645796c8dcSSimon Schubert   opts.deref_ref = 0;
25655796c8dcSSimon Schubert   opts.raw = 1;
25665796c8dcSSimon Schubert   if (thevalue)
25675796c8dcSSimon Schubert     {
25685796c8dcSSimon Schubert       make_cleanup (xfree, thevalue);
2569*cf7f2e2dSJohn Marino       make_cleanup (xfree, encoding);
2570*cf7f2e2dSJohn Marino       LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
25715796c8dcSSimon Schubert     }
25725796c8dcSSimon Schubert   else
25735796c8dcSSimon Schubert     common_val_print (value, stb, 0, &opts, current_language);
25745796c8dcSSimon Schubert   thevalue = ui_file_xstrdup (stb, NULL);
25755796c8dcSSimon Schubert 
25765796c8dcSSimon Schubert   do_cleanups (old_chain);
25775796c8dcSSimon Schubert   return thevalue;
25785796c8dcSSimon Schubert }
25795796c8dcSSimon Schubert 
25805796c8dcSSimon Schubert int
25815796c8dcSSimon Schubert varobj_editable_p (struct varobj *var)
25825796c8dcSSimon Schubert {
25835796c8dcSSimon Schubert   struct type *type;
25845796c8dcSSimon Schubert 
25855796c8dcSSimon Schubert   if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
25865796c8dcSSimon Schubert     return 0;
25875796c8dcSSimon Schubert 
25885796c8dcSSimon Schubert   type = get_value_type (var);
25895796c8dcSSimon Schubert 
25905796c8dcSSimon Schubert   switch (TYPE_CODE (type))
25915796c8dcSSimon Schubert     {
25925796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
25935796c8dcSSimon Schubert     case TYPE_CODE_UNION:
25945796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
25955796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
25965796c8dcSSimon Schubert     case TYPE_CODE_METHOD:
25975796c8dcSSimon Schubert       return 0;
25985796c8dcSSimon Schubert       break;
25995796c8dcSSimon Schubert 
26005796c8dcSSimon Schubert     default:
26015796c8dcSSimon Schubert       return 1;
26025796c8dcSSimon Schubert       break;
26035796c8dcSSimon Schubert     }
26045796c8dcSSimon Schubert }
26055796c8dcSSimon Schubert 
26065796c8dcSSimon Schubert /* Return non-zero if changes in value of VAR
26075796c8dcSSimon Schubert    must be detected and reported by -var-update.
26085796c8dcSSimon Schubert    Return zero is -var-update should never report
26095796c8dcSSimon Schubert    changes of such values.  This makes sense for structures
26105796c8dcSSimon Schubert    (since the changes in children values will be reported separately),
26115796c8dcSSimon Schubert    or for artifical objects (like 'public' pseudo-field in C++).
26125796c8dcSSimon Schubert 
26135796c8dcSSimon Schubert    Return value of 0 means that gdb need not call value_fetch_lazy
26145796c8dcSSimon Schubert    for the value of this variable object.  */
26155796c8dcSSimon Schubert static int
26165796c8dcSSimon Schubert varobj_value_is_changeable_p (struct varobj *var)
26175796c8dcSSimon Schubert {
26185796c8dcSSimon Schubert   int r;
26195796c8dcSSimon Schubert   struct type *type;
26205796c8dcSSimon Schubert 
26215796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var))
26225796c8dcSSimon Schubert     return 0;
26235796c8dcSSimon Schubert 
26245796c8dcSSimon Schubert   type = get_value_type (var);
26255796c8dcSSimon Schubert 
26265796c8dcSSimon Schubert   switch (TYPE_CODE (type))
26275796c8dcSSimon Schubert     {
26285796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
26295796c8dcSSimon Schubert     case TYPE_CODE_UNION:
26305796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
26315796c8dcSSimon Schubert       r = 0;
26325796c8dcSSimon Schubert       break;
26335796c8dcSSimon Schubert 
26345796c8dcSSimon Schubert     default:
26355796c8dcSSimon Schubert       r = 1;
26365796c8dcSSimon Schubert     }
26375796c8dcSSimon Schubert 
26385796c8dcSSimon Schubert   return r;
26395796c8dcSSimon Schubert }
26405796c8dcSSimon Schubert 
26415796c8dcSSimon Schubert /* Return 1 if that varobj is floating, that is is always evaluated in the
26425796c8dcSSimon Schubert    selected frame, and not bound to thread/frame.  Such variable objects
26435796c8dcSSimon Schubert    are created using '@' as frame specifier to -var-create.  */
26445796c8dcSSimon Schubert int
26455796c8dcSSimon Schubert varobj_floating_p (struct varobj *var)
26465796c8dcSSimon Schubert {
26475796c8dcSSimon Schubert   return var->root->floating;
26485796c8dcSSimon Schubert }
26495796c8dcSSimon Schubert 
26505796c8dcSSimon Schubert /* Given the value and the type of a variable object,
26515796c8dcSSimon Schubert    adjust the value and type to those necessary
26525796c8dcSSimon Schubert    for getting children of the variable object.
26535796c8dcSSimon Schubert    This includes dereferencing top-level references
26545796c8dcSSimon Schubert    to all types and dereferencing pointers to
26555796c8dcSSimon Schubert    structures.
26565796c8dcSSimon Schubert 
26575796c8dcSSimon Schubert    Both TYPE and *TYPE should be non-null. VALUE
26585796c8dcSSimon Schubert    can be null if we want to only translate type.
26595796c8dcSSimon Schubert    *VALUE can be null as well -- if the parent
26605796c8dcSSimon Schubert    value is not known.
26615796c8dcSSimon Schubert 
26625796c8dcSSimon Schubert    If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
26635796c8dcSSimon Schubert    depending on whether pointer was dereferenced
26645796c8dcSSimon Schubert    in this function.  */
26655796c8dcSSimon Schubert static void
26665796c8dcSSimon Schubert adjust_value_for_child_access (struct value **value,
26675796c8dcSSimon Schubert 				  struct type **type,
26685796c8dcSSimon Schubert 				  int *was_ptr)
26695796c8dcSSimon Schubert {
26705796c8dcSSimon Schubert   gdb_assert (type && *type);
26715796c8dcSSimon Schubert 
26725796c8dcSSimon Schubert   if (was_ptr)
26735796c8dcSSimon Schubert     *was_ptr = 0;
26745796c8dcSSimon Schubert 
26755796c8dcSSimon Schubert   *type = check_typedef (*type);
26765796c8dcSSimon Schubert 
26775796c8dcSSimon Schubert   /* The type of value stored in varobj, that is passed
26785796c8dcSSimon Schubert      to us, is already supposed to be
26795796c8dcSSimon Schubert      reference-stripped.  */
26805796c8dcSSimon Schubert 
26815796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
26825796c8dcSSimon Schubert 
26835796c8dcSSimon Schubert   /* Pointers to structures are treated just like
26845796c8dcSSimon Schubert      structures when accessing children.  Don't
26855796c8dcSSimon Schubert      dererences pointers to other types.  */
26865796c8dcSSimon Schubert   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
26875796c8dcSSimon Schubert     {
26885796c8dcSSimon Schubert       struct type *target_type = get_target_type (*type);
26895796c8dcSSimon Schubert       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
26905796c8dcSSimon Schubert 	  || TYPE_CODE (target_type) == TYPE_CODE_UNION)
26915796c8dcSSimon Schubert 	{
26925796c8dcSSimon Schubert 	  if (value && *value)
26935796c8dcSSimon Schubert 	    {
26945796c8dcSSimon Schubert 	      int success = gdb_value_ind (*value, value);
2695*cf7f2e2dSJohn Marino 
26965796c8dcSSimon Schubert 	      if (!success)
26975796c8dcSSimon Schubert 		*value = NULL;
26985796c8dcSSimon Schubert 	    }
26995796c8dcSSimon Schubert 	  *type = target_type;
27005796c8dcSSimon Schubert 	  if (was_ptr)
27015796c8dcSSimon Schubert 	    *was_ptr = 1;
27025796c8dcSSimon Schubert 	}
27035796c8dcSSimon Schubert     }
27045796c8dcSSimon Schubert 
27055796c8dcSSimon Schubert   /* The 'get_target_type' function calls check_typedef on
27065796c8dcSSimon Schubert      result, so we can immediately check type code.  No
27075796c8dcSSimon Schubert      need to call check_typedef here.  */
27085796c8dcSSimon Schubert }
27095796c8dcSSimon Schubert 
27105796c8dcSSimon Schubert /* C */
27115796c8dcSSimon Schubert static int
27125796c8dcSSimon Schubert c_number_of_children (struct varobj *var)
27135796c8dcSSimon Schubert {
27145796c8dcSSimon Schubert   struct type *type = get_value_type (var);
27155796c8dcSSimon Schubert   int children = 0;
27165796c8dcSSimon Schubert   struct type *target;
27175796c8dcSSimon Schubert 
27185796c8dcSSimon Schubert   adjust_value_for_child_access (NULL, &type, NULL);
27195796c8dcSSimon Schubert   target = get_target_type (type);
27205796c8dcSSimon Schubert 
27215796c8dcSSimon Schubert   switch (TYPE_CODE (type))
27225796c8dcSSimon Schubert     {
27235796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
27245796c8dcSSimon Schubert       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
27255796c8dcSSimon Schubert 	  && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
27265796c8dcSSimon Schubert 	children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
27275796c8dcSSimon Schubert       else
27285796c8dcSSimon Schubert 	/* If we don't know how many elements there are, don't display
27295796c8dcSSimon Schubert 	   any.  */
27305796c8dcSSimon Schubert 	children = 0;
27315796c8dcSSimon Schubert       break;
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
27345796c8dcSSimon Schubert     case TYPE_CODE_UNION:
27355796c8dcSSimon Schubert       children = TYPE_NFIELDS (type);
27365796c8dcSSimon Schubert       break;
27375796c8dcSSimon Schubert 
27385796c8dcSSimon Schubert     case TYPE_CODE_PTR:
27395796c8dcSSimon Schubert       /* The type here is a pointer to non-struct. Typically, pointers
27405796c8dcSSimon Schubert 	 have one child, except for function ptrs, which have no children,
27415796c8dcSSimon Schubert 	 and except for void*, as we don't know what to show.
27425796c8dcSSimon Schubert 
27435796c8dcSSimon Schubert          We can show char* so we allow it to be dereferenced.  If you decide
27445796c8dcSSimon Schubert          to test for it, please mind that a little magic is necessary to
27455796c8dcSSimon Schubert          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
27465796c8dcSSimon Schubert          TYPE_NAME == "char" */
27475796c8dcSSimon Schubert       if (TYPE_CODE (target) == TYPE_CODE_FUNC
27485796c8dcSSimon Schubert 	  || TYPE_CODE (target) == TYPE_CODE_VOID)
27495796c8dcSSimon Schubert 	children = 0;
27505796c8dcSSimon Schubert       else
27515796c8dcSSimon Schubert 	children = 1;
27525796c8dcSSimon Schubert       break;
27535796c8dcSSimon Schubert 
27545796c8dcSSimon Schubert     default:
27555796c8dcSSimon Schubert       /* Other types have no children */
27565796c8dcSSimon Schubert       break;
27575796c8dcSSimon Schubert     }
27585796c8dcSSimon Schubert 
27595796c8dcSSimon Schubert   return children;
27605796c8dcSSimon Schubert }
27615796c8dcSSimon Schubert 
27625796c8dcSSimon Schubert static char *
27635796c8dcSSimon Schubert c_name_of_variable (struct varobj *parent)
27645796c8dcSSimon Schubert {
27655796c8dcSSimon Schubert   return xstrdup (parent->name);
27665796c8dcSSimon Schubert }
27675796c8dcSSimon Schubert 
27685796c8dcSSimon Schubert /* Return the value of element TYPE_INDEX of a structure
27695796c8dcSSimon Schubert    value VALUE.  VALUE's type should be a structure,
27705796c8dcSSimon Schubert    or union, or a typedef to struct/union.
27715796c8dcSSimon Schubert 
27725796c8dcSSimon Schubert    Returns NULL if getting the value fails.  Never throws.  */
27735796c8dcSSimon Schubert static struct value *
27745796c8dcSSimon Schubert value_struct_element_index (struct value *value, int type_index)
27755796c8dcSSimon Schubert {
27765796c8dcSSimon Schubert   struct value *result = NULL;
27775796c8dcSSimon Schubert   volatile struct gdb_exception e;
27785796c8dcSSimon Schubert   struct type *type = value_type (value);
2779*cf7f2e2dSJohn Marino 
27805796c8dcSSimon Schubert   type = check_typedef (type);
27815796c8dcSSimon Schubert 
27825796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
27835796c8dcSSimon Schubert 	      || TYPE_CODE (type) == TYPE_CODE_UNION);
27845796c8dcSSimon Schubert 
27855796c8dcSSimon Schubert   TRY_CATCH (e, RETURN_MASK_ERROR)
27865796c8dcSSimon Schubert     {
27875796c8dcSSimon Schubert       if (field_is_static (&TYPE_FIELD (type, type_index)))
27885796c8dcSSimon Schubert 	result = value_static_field (type, type_index);
27895796c8dcSSimon Schubert       else
27905796c8dcSSimon Schubert 	result = value_primitive_field (value, 0, type_index, type);
27915796c8dcSSimon Schubert     }
27925796c8dcSSimon Schubert   if (e.reason < 0)
27935796c8dcSSimon Schubert     {
27945796c8dcSSimon Schubert       return NULL;
27955796c8dcSSimon Schubert     }
27965796c8dcSSimon Schubert   else
27975796c8dcSSimon Schubert     {
27985796c8dcSSimon Schubert       return result;
27995796c8dcSSimon Schubert     }
28005796c8dcSSimon Schubert }
28015796c8dcSSimon Schubert 
28025796c8dcSSimon Schubert /* Obtain the information about child INDEX of the variable
28035796c8dcSSimon Schubert    object PARENT.
28045796c8dcSSimon Schubert    If CNAME is not null, sets *CNAME to the name of the child relative
28055796c8dcSSimon Schubert    to the parent.
28065796c8dcSSimon Schubert    If CVALUE is not null, sets *CVALUE to the value of the child.
28075796c8dcSSimon Schubert    If CTYPE is not null, sets *CTYPE to the type of the child.
28085796c8dcSSimon Schubert 
28095796c8dcSSimon Schubert    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
28105796c8dcSSimon Schubert    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
28115796c8dcSSimon Schubert    to NULL.  */
28125796c8dcSSimon Schubert static void
28135796c8dcSSimon Schubert c_describe_child (struct varobj *parent, int index,
28145796c8dcSSimon Schubert 		  char **cname, struct value **cvalue, struct type **ctype,
28155796c8dcSSimon Schubert 		  char **cfull_expression)
28165796c8dcSSimon Schubert {
28175796c8dcSSimon Schubert   struct value *value = parent->value;
28185796c8dcSSimon Schubert   struct type *type = get_value_type (parent);
28195796c8dcSSimon Schubert   char *parent_expression = NULL;
28205796c8dcSSimon Schubert   int was_ptr;
28215796c8dcSSimon Schubert 
28225796c8dcSSimon Schubert   if (cname)
28235796c8dcSSimon Schubert     *cname = NULL;
28245796c8dcSSimon Schubert   if (cvalue)
28255796c8dcSSimon Schubert     *cvalue = NULL;
28265796c8dcSSimon Schubert   if (ctype)
28275796c8dcSSimon Schubert     *ctype = NULL;
28285796c8dcSSimon Schubert   if (cfull_expression)
28295796c8dcSSimon Schubert     {
28305796c8dcSSimon Schubert       *cfull_expression = NULL;
28315796c8dcSSimon Schubert       parent_expression = varobj_get_path_expr (parent);
28325796c8dcSSimon Schubert     }
28335796c8dcSSimon Schubert   adjust_value_for_child_access (&value, &type, &was_ptr);
28345796c8dcSSimon Schubert 
28355796c8dcSSimon Schubert   switch (TYPE_CODE (type))
28365796c8dcSSimon Schubert     {
28375796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
28385796c8dcSSimon Schubert       if (cname)
2839*cf7f2e2dSJohn Marino 	*cname = xstrdup (int_string (index
2840*cf7f2e2dSJohn Marino 				      + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
2841*cf7f2e2dSJohn Marino 				      10, 1, 0, 0));
28425796c8dcSSimon Schubert 
28435796c8dcSSimon Schubert       if (cvalue && value)
28445796c8dcSSimon Schubert 	{
28455796c8dcSSimon Schubert 	  int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2846*cf7f2e2dSJohn Marino 
28475796c8dcSSimon Schubert 	  gdb_value_subscript (value, real_index, cvalue);
28485796c8dcSSimon Schubert 	}
28495796c8dcSSimon Schubert 
28505796c8dcSSimon Schubert       if (ctype)
28515796c8dcSSimon Schubert 	*ctype = get_target_type (type);
28525796c8dcSSimon Schubert 
28535796c8dcSSimon Schubert       if (cfull_expression)
2854*cf7f2e2dSJohn Marino 	*cfull_expression =
2855*cf7f2e2dSJohn Marino 	  xstrprintf ("(%s)[%s]", parent_expression,
2856*cf7f2e2dSJohn Marino 		      int_string (index
2857*cf7f2e2dSJohn Marino 				  + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
2858*cf7f2e2dSJohn Marino 				  10, 1, 0, 0));
28595796c8dcSSimon Schubert 
28605796c8dcSSimon Schubert 
28615796c8dcSSimon Schubert       break;
28625796c8dcSSimon Schubert 
28635796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
28645796c8dcSSimon Schubert     case TYPE_CODE_UNION:
28655796c8dcSSimon Schubert       if (cname)
28665796c8dcSSimon Schubert 	*cname = xstrdup (TYPE_FIELD_NAME (type, index));
28675796c8dcSSimon Schubert 
28685796c8dcSSimon Schubert       if (cvalue && value)
28695796c8dcSSimon Schubert 	{
28705796c8dcSSimon Schubert 	  /* For C, varobj index is the same as type index.  */
28715796c8dcSSimon Schubert 	  *cvalue = value_struct_element_index (value, index);
28725796c8dcSSimon Schubert 	}
28735796c8dcSSimon Schubert 
28745796c8dcSSimon Schubert       if (ctype)
28755796c8dcSSimon Schubert 	*ctype = TYPE_FIELD_TYPE (type, index);
28765796c8dcSSimon Schubert 
28775796c8dcSSimon Schubert       if (cfull_expression)
28785796c8dcSSimon Schubert 	{
28795796c8dcSSimon Schubert 	  char *join = was_ptr ? "->" : ".";
2880*cf7f2e2dSJohn Marino 
28815796c8dcSSimon Schubert 	  *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
28825796c8dcSSimon Schubert 					  TYPE_FIELD_NAME (type, index));
28835796c8dcSSimon Schubert 	}
28845796c8dcSSimon Schubert 
28855796c8dcSSimon Schubert       break;
28865796c8dcSSimon Schubert 
28875796c8dcSSimon Schubert     case TYPE_CODE_PTR:
28885796c8dcSSimon Schubert       if (cname)
28895796c8dcSSimon Schubert 	*cname = xstrprintf ("*%s", parent->name);
28905796c8dcSSimon Schubert 
28915796c8dcSSimon Schubert       if (cvalue && value)
28925796c8dcSSimon Schubert 	{
28935796c8dcSSimon Schubert 	  int success = gdb_value_ind (value, cvalue);
2894*cf7f2e2dSJohn Marino 
28955796c8dcSSimon Schubert 	  if (!success)
28965796c8dcSSimon Schubert 	    *cvalue = NULL;
28975796c8dcSSimon Schubert 	}
28985796c8dcSSimon Schubert 
28995796c8dcSSimon Schubert       /* Don't use get_target_type because it calls
29005796c8dcSSimon Schubert 	 check_typedef and here, we want to show the true
29015796c8dcSSimon Schubert 	 declared type of the variable.  */
29025796c8dcSSimon Schubert       if (ctype)
29035796c8dcSSimon Schubert 	*ctype = TYPE_TARGET_TYPE (type);
29045796c8dcSSimon Schubert 
29055796c8dcSSimon Schubert       if (cfull_expression)
29065796c8dcSSimon Schubert 	*cfull_expression = xstrprintf ("*(%s)", parent_expression);
29075796c8dcSSimon Schubert 
29085796c8dcSSimon Schubert       break;
29095796c8dcSSimon Schubert 
29105796c8dcSSimon Schubert     default:
29115796c8dcSSimon Schubert       /* This should not happen */
29125796c8dcSSimon Schubert       if (cname)
29135796c8dcSSimon Schubert 	*cname = xstrdup ("???");
29145796c8dcSSimon Schubert       if (cfull_expression)
29155796c8dcSSimon Schubert 	*cfull_expression = xstrdup ("???");
29165796c8dcSSimon Schubert       /* Don't set value and type, we don't know then. */
29175796c8dcSSimon Schubert     }
29185796c8dcSSimon Schubert }
29195796c8dcSSimon Schubert 
29205796c8dcSSimon Schubert static char *
29215796c8dcSSimon Schubert c_name_of_child (struct varobj *parent, int index)
29225796c8dcSSimon Schubert {
29235796c8dcSSimon Schubert   char *name;
2924*cf7f2e2dSJohn Marino 
29255796c8dcSSimon Schubert   c_describe_child (parent, index, &name, NULL, NULL, NULL);
29265796c8dcSSimon Schubert   return name;
29275796c8dcSSimon Schubert }
29285796c8dcSSimon Schubert 
29295796c8dcSSimon Schubert static char *
29305796c8dcSSimon Schubert c_path_expr_of_child (struct varobj *child)
29315796c8dcSSimon Schubert {
29325796c8dcSSimon Schubert   c_describe_child (child->parent, child->index, NULL, NULL, NULL,
29335796c8dcSSimon Schubert 		    &child->path_expr);
29345796c8dcSSimon Schubert   return child->path_expr;
29355796c8dcSSimon Schubert }
29365796c8dcSSimon Schubert 
29375796c8dcSSimon Schubert /* If frame associated with VAR can be found, switch
29385796c8dcSSimon Schubert    to it and return 1.  Otherwise, return 0.  */
29395796c8dcSSimon Schubert static int
29405796c8dcSSimon Schubert check_scope (struct varobj *var)
29415796c8dcSSimon Schubert {
29425796c8dcSSimon Schubert   struct frame_info *fi;
29435796c8dcSSimon Schubert   int scope;
29445796c8dcSSimon Schubert 
29455796c8dcSSimon Schubert   fi = frame_find_by_id (var->root->frame);
29465796c8dcSSimon Schubert   scope = fi != NULL;
29475796c8dcSSimon Schubert 
29485796c8dcSSimon Schubert   if (fi)
29495796c8dcSSimon Schubert     {
29505796c8dcSSimon Schubert       CORE_ADDR pc = get_frame_pc (fi);
2951*cf7f2e2dSJohn Marino 
29525796c8dcSSimon Schubert       if (pc <  BLOCK_START (var->root->valid_block) ||
29535796c8dcSSimon Schubert 	  pc >= BLOCK_END (var->root->valid_block))
29545796c8dcSSimon Schubert 	scope = 0;
29555796c8dcSSimon Schubert       else
29565796c8dcSSimon Schubert 	select_frame (fi);
29575796c8dcSSimon Schubert     }
29585796c8dcSSimon Schubert   return scope;
29595796c8dcSSimon Schubert }
29605796c8dcSSimon Schubert 
29615796c8dcSSimon Schubert static struct value *
29625796c8dcSSimon Schubert c_value_of_root (struct varobj **var_handle)
29635796c8dcSSimon Schubert {
29645796c8dcSSimon Schubert   struct value *new_val = NULL;
29655796c8dcSSimon Schubert   struct varobj *var = *var_handle;
29665796c8dcSSimon Schubert   int within_scope = 0;
29675796c8dcSSimon Schubert   struct cleanup *back_to;
29685796c8dcSSimon Schubert 
29695796c8dcSSimon Schubert   /*  Only root variables can be updated... */
29705796c8dcSSimon Schubert   if (!is_root_p (var))
29715796c8dcSSimon Schubert     /* Not a root var */
29725796c8dcSSimon Schubert     return NULL;
29735796c8dcSSimon Schubert 
29745796c8dcSSimon Schubert   back_to = make_cleanup_restore_current_thread ();
29755796c8dcSSimon Schubert 
29765796c8dcSSimon Schubert   /* Determine whether the variable is still around. */
29775796c8dcSSimon Schubert   if (var->root->valid_block == NULL || var->root->floating)
29785796c8dcSSimon Schubert     within_scope = 1;
29795796c8dcSSimon Schubert   else if (var->root->thread_id == 0)
29805796c8dcSSimon Schubert     {
29815796c8dcSSimon Schubert       /* The program was single-threaded when the variable object was
29825796c8dcSSimon Schubert 	 created.  Technically, it's possible that the program became
29835796c8dcSSimon Schubert 	 multi-threaded since then, but we don't support such
29845796c8dcSSimon Schubert 	 scenario yet.  */
29855796c8dcSSimon Schubert       within_scope = check_scope (var);
29865796c8dcSSimon Schubert     }
29875796c8dcSSimon Schubert   else
29885796c8dcSSimon Schubert     {
29895796c8dcSSimon Schubert       ptid_t ptid = thread_id_to_pid (var->root->thread_id);
29905796c8dcSSimon Schubert       if (in_thread_list (ptid))
29915796c8dcSSimon Schubert 	{
29925796c8dcSSimon Schubert 	  switch_to_thread (ptid);
29935796c8dcSSimon Schubert 	  within_scope = check_scope (var);
29945796c8dcSSimon Schubert 	}
29955796c8dcSSimon Schubert     }
29965796c8dcSSimon Schubert 
29975796c8dcSSimon Schubert   if (within_scope)
29985796c8dcSSimon Schubert     {
29995796c8dcSSimon Schubert       /* We need to catch errors here, because if evaluate
30005796c8dcSSimon Schubert          expression fails we want to just return NULL.  */
30015796c8dcSSimon Schubert       gdb_evaluate_expression (var->root->exp, &new_val);
30025796c8dcSSimon Schubert       return new_val;
30035796c8dcSSimon Schubert     }
30045796c8dcSSimon Schubert 
30055796c8dcSSimon Schubert   do_cleanups (back_to);
30065796c8dcSSimon Schubert 
30075796c8dcSSimon Schubert   return NULL;
30085796c8dcSSimon Schubert }
30095796c8dcSSimon Schubert 
30105796c8dcSSimon Schubert static struct value *
30115796c8dcSSimon Schubert c_value_of_child (struct varobj *parent, int index)
30125796c8dcSSimon Schubert {
30135796c8dcSSimon Schubert   struct value *value = NULL;
30145796c8dcSSimon Schubert 
3015*cf7f2e2dSJohn Marino   c_describe_child (parent, index, NULL, &value, NULL, NULL);
30165796c8dcSSimon Schubert   return value;
30175796c8dcSSimon Schubert }
30185796c8dcSSimon Schubert 
30195796c8dcSSimon Schubert static struct type *
30205796c8dcSSimon Schubert c_type_of_child (struct varobj *parent, int index)
30215796c8dcSSimon Schubert {
30225796c8dcSSimon Schubert   struct type *type = NULL;
3023*cf7f2e2dSJohn Marino 
30245796c8dcSSimon Schubert   c_describe_child (parent, index, NULL, NULL, &type, NULL);
30255796c8dcSSimon Schubert   return type;
30265796c8dcSSimon Schubert }
30275796c8dcSSimon Schubert 
30285796c8dcSSimon Schubert static char *
30295796c8dcSSimon Schubert c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
30305796c8dcSSimon Schubert {
30315796c8dcSSimon Schubert   /* BOGUS: if val_print sees a struct/class, or a reference to one,
30325796c8dcSSimon Schubert      it will print out its children instead of "{...}".  So we need to
30335796c8dcSSimon Schubert      catch that case explicitly.  */
30345796c8dcSSimon Schubert   struct type *type = get_type (var);
30355796c8dcSSimon Schubert 
30365796c8dcSSimon Schubert   /* If we have a custom formatter, return whatever string it has
30375796c8dcSSimon Schubert      produced.  */
30385796c8dcSSimon Schubert   if (var->pretty_printer && var->print_value)
30395796c8dcSSimon Schubert     return xstrdup (var->print_value);
30405796c8dcSSimon Schubert 
30415796c8dcSSimon Schubert   /* Strip top-level references. */
30425796c8dcSSimon Schubert   while (TYPE_CODE (type) == TYPE_CODE_REF)
30435796c8dcSSimon Schubert     type = check_typedef (TYPE_TARGET_TYPE (type));
30445796c8dcSSimon Schubert 
30455796c8dcSSimon Schubert   switch (TYPE_CODE (type))
30465796c8dcSSimon Schubert     {
30475796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
30485796c8dcSSimon Schubert     case TYPE_CODE_UNION:
30495796c8dcSSimon Schubert       return xstrdup ("{...}");
30505796c8dcSSimon Schubert       /* break; */
30515796c8dcSSimon Schubert 
30525796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
30535796c8dcSSimon Schubert       {
30545796c8dcSSimon Schubert 	char *number;
3055*cf7f2e2dSJohn Marino 
30565796c8dcSSimon Schubert 	number = xstrprintf ("[%d]", var->num_children);
30575796c8dcSSimon Schubert 	return (number);
30585796c8dcSSimon Schubert       }
30595796c8dcSSimon Schubert       /* break; */
30605796c8dcSSimon Schubert 
30615796c8dcSSimon Schubert     default:
30625796c8dcSSimon Schubert       {
30635796c8dcSSimon Schubert 	if (var->value == NULL)
30645796c8dcSSimon Schubert 	  {
30655796c8dcSSimon Schubert 	    /* This can happen if we attempt to get the value of a struct
30665796c8dcSSimon Schubert 	       member when the parent is an invalid pointer. This is an
30675796c8dcSSimon Schubert 	       error condition, so we should tell the caller. */
30685796c8dcSSimon Schubert 	    return NULL;
30695796c8dcSSimon Schubert 	  }
30705796c8dcSSimon Schubert 	else
30715796c8dcSSimon Schubert 	  {
30725796c8dcSSimon Schubert 	    if (var->not_fetched && value_lazy (var->value))
30735796c8dcSSimon Schubert 	      /* Frozen variable and no value yet.  We don't
30745796c8dcSSimon Schubert 		 implicitly fetch the value.  MI response will
30755796c8dcSSimon Schubert 		 use empty string for the value, which is OK.  */
30765796c8dcSSimon Schubert 	      return NULL;
30775796c8dcSSimon Schubert 
30785796c8dcSSimon Schubert 	    gdb_assert (varobj_value_is_changeable_p (var));
30795796c8dcSSimon Schubert 	    gdb_assert (!value_lazy (var->value));
30805796c8dcSSimon Schubert 
30815796c8dcSSimon Schubert 	    /* If the specified format is the current one,
30825796c8dcSSimon Schubert 	       we can reuse print_value */
30835796c8dcSSimon Schubert 	    if (format == var->format)
30845796c8dcSSimon Schubert 	      return xstrdup (var->print_value);
30855796c8dcSSimon Schubert 	    else
30865796c8dcSSimon Schubert 	      return value_get_print_value (var->value, format, var);
30875796c8dcSSimon Schubert 	  }
30885796c8dcSSimon Schubert       }
30895796c8dcSSimon Schubert     }
30905796c8dcSSimon Schubert }
30915796c8dcSSimon Schubert 
30925796c8dcSSimon Schubert 
30935796c8dcSSimon Schubert /* C++ */
30945796c8dcSSimon Schubert 
30955796c8dcSSimon Schubert static int
30965796c8dcSSimon Schubert cplus_number_of_children (struct varobj *var)
30975796c8dcSSimon Schubert {
30985796c8dcSSimon Schubert   struct type *type;
30995796c8dcSSimon Schubert   int children, dont_know;
31005796c8dcSSimon Schubert 
31015796c8dcSSimon Schubert   dont_know = 1;
31025796c8dcSSimon Schubert   children = 0;
31035796c8dcSSimon Schubert 
31045796c8dcSSimon Schubert   if (!CPLUS_FAKE_CHILD (var))
31055796c8dcSSimon Schubert     {
31065796c8dcSSimon Schubert       type = get_value_type (var);
31075796c8dcSSimon Schubert       adjust_value_for_child_access (NULL, &type, NULL);
31085796c8dcSSimon Schubert 
31095796c8dcSSimon Schubert       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
31105796c8dcSSimon Schubert 	  ((TYPE_CODE (type)) == TYPE_CODE_UNION))
31115796c8dcSSimon Schubert 	{
31125796c8dcSSimon Schubert 	  int kids[3];
31135796c8dcSSimon Schubert 
31145796c8dcSSimon Schubert 	  cplus_class_num_children (type, kids);
31155796c8dcSSimon Schubert 	  if (kids[v_public] != 0)
31165796c8dcSSimon Schubert 	    children++;
31175796c8dcSSimon Schubert 	  if (kids[v_private] != 0)
31185796c8dcSSimon Schubert 	    children++;
31195796c8dcSSimon Schubert 	  if (kids[v_protected] != 0)
31205796c8dcSSimon Schubert 	    children++;
31215796c8dcSSimon Schubert 
31225796c8dcSSimon Schubert 	  /* Add any baseclasses */
31235796c8dcSSimon Schubert 	  children += TYPE_N_BASECLASSES (type);
31245796c8dcSSimon Schubert 	  dont_know = 0;
31255796c8dcSSimon Schubert 
31265796c8dcSSimon Schubert 	  /* FIXME: save children in var */
31275796c8dcSSimon Schubert 	}
31285796c8dcSSimon Schubert     }
31295796c8dcSSimon Schubert   else
31305796c8dcSSimon Schubert     {
31315796c8dcSSimon Schubert       int kids[3];
31325796c8dcSSimon Schubert 
31335796c8dcSSimon Schubert       type = get_value_type (var->parent);
31345796c8dcSSimon Schubert       adjust_value_for_child_access (NULL, &type, NULL);
31355796c8dcSSimon Schubert 
31365796c8dcSSimon Schubert       cplus_class_num_children (type, kids);
31375796c8dcSSimon Schubert       if (strcmp (var->name, "public") == 0)
31385796c8dcSSimon Schubert 	children = kids[v_public];
31395796c8dcSSimon Schubert       else if (strcmp (var->name, "private") == 0)
31405796c8dcSSimon Schubert 	children = kids[v_private];
31415796c8dcSSimon Schubert       else
31425796c8dcSSimon Schubert 	children = kids[v_protected];
31435796c8dcSSimon Schubert       dont_know = 0;
31445796c8dcSSimon Schubert     }
31455796c8dcSSimon Schubert 
31465796c8dcSSimon Schubert   if (dont_know)
31475796c8dcSSimon Schubert     children = c_number_of_children (var);
31485796c8dcSSimon Schubert 
31495796c8dcSSimon Schubert   return children;
31505796c8dcSSimon Schubert }
31515796c8dcSSimon Schubert 
31525796c8dcSSimon Schubert /* Compute # of public, private, and protected variables in this class.
31535796c8dcSSimon Schubert    That means we need to descend into all baseclasses and find out
31545796c8dcSSimon Schubert    how many are there, too. */
31555796c8dcSSimon Schubert static void
31565796c8dcSSimon Schubert cplus_class_num_children (struct type *type, int children[3])
31575796c8dcSSimon Schubert {
3158*cf7f2e2dSJohn Marino   int i, vptr_fieldno;
3159*cf7f2e2dSJohn Marino   struct type *basetype = NULL;
31605796c8dcSSimon Schubert 
31615796c8dcSSimon Schubert   children[v_public] = 0;
31625796c8dcSSimon Schubert   children[v_private] = 0;
31635796c8dcSSimon Schubert   children[v_protected] = 0;
31645796c8dcSSimon Schubert 
3165*cf7f2e2dSJohn Marino   vptr_fieldno = get_vptr_fieldno (type, &basetype);
31665796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
31675796c8dcSSimon Schubert     {
3168*cf7f2e2dSJohn Marino       /* If we have a virtual table pointer, omit it.  Even if virtual
3169*cf7f2e2dSJohn Marino 	 table pointers are not specifically marked in the debug info,
3170*cf7f2e2dSJohn Marino 	 they should be artificial.  */
3171*cf7f2e2dSJohn Marino       if ((type == basetype && i == vptr_fieldno)
3172*cf7f2e2dSJohn Marino 	  || TYPE_FIELD_ARTIFICIAL (type, i))
31735796c8dcSSimon Schubert 	continue;
31745796c8dcSSimon Schubert 
31755796c8dcSSimon Schubert       if (TYPE_FIELD_PROTECTED (type, i))
31765796c8dcSSimon Schubert 	children[v_protected]++;
31775796c8dcSSimon Schubert       else if (TYPE_FIELD_PRIVATE (type, i))
31785796c8dcSSimon Schubert 	children[v_private]++;
31795796c8dcSSimon Schubert       else
31805796c8dcSSimon Schubert 	children[v_public]++;
31815796c8dcSSimon Schubert     }
31825796c8dcSSimon Schubert }
31835796c8dcSSimon Schubert 
31845796c8dcSSimon Schubert static char *
31855796c8dcSSimon Schubert cplus_name_of_variable (struct varobj *parent)
31865796c8dcSSimon Schubert {
31875796c8dcSSimon Schubert   return c_name_of_variable (parent);
31885796c8dcSSimon Schubert }
31895796c8dcSSimon Schubert 
31905796c8dcSSimon Schubert enum accessibility { private_field, protected_field, public_field };
31915796c8dcSSimon Schubert 
31925796c8dcSSimon Schubert /* Check if field INDEX of TYPE has the specified accessibility.
31935796c8dcSSimon Schubert    Return 0 if so and 1 otherwise.  */
31945796c8dcSSimon Schubert static int
31955796c8dcSSimon Schubert match_accessibility (struct type *type, int index, enum accessibility acc)
31965796c8dcSSimon Schubert {
31975796c8dcSSimon Schubert   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
31985796c8dcSSimon Schubert     return 1;
31995796c8dcSSimon Schubert   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
32005796c8dcSSimon Schubert     return 1;
32015796c8dcSSimon Schubert   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
32025796c8dcSSimon Schubert 	   && !TYPE_FIELD_PROTECTED (type, index))
32035796c8dcSSimon Schubert     return 1;
32045796c8dcSSimon Schubert   else
32055796c8dcSSimon Schubert     return 0;
32065796c8dcSSimon Schubert }
32075796c8dcSSimon Schubert 
32085796c8dcSSimon Schubert static void
32095796c8dcSSimon Schubert cplus_describe_child (struct varobj *parent, int index,
32105796c8dcSSimon Schubert 		      char **cname, struct value **cvalue, struct type **ctype,
32115796c8dcSSimon Schubert 		      char **cfull_expression)
32125796c8dcSSimon Schubert {
32135796c8dcSSimon Schubert   struct value *value;
32145796c8dcSSimon Schubert   struct type *type;
32155796c8dcSSimon Schubert   int was_ptr;
32165796c8dcSSimon Schubert   char *parent_expression = NULL;
32175796c8dcSSimon Schubert 
32185796c8dcSSimon Schubert   if (cname)
32195796c8dcSSimon Schubert     *cname = NULL;
32205796c8dcSSimon Schubert   if (cvalue)
32215796c8dcSSimon Schubert     *cvalue = NULL;
32225796c8dcSSimon Schubert   if (ctype)
32235796c8dcSSimon Schubert     *ctype = NULL;
32245796c8dcSSimon Schubert   if (cfull_expression)
32255796c8dcSSimon Schubert     *cfull_expression = NULL;
32265796c8dcSSimon Schubert 
32275796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (parent))
32285796c8dcSSimon Schubert     {
32295796c8dcSSimon Schubert       value = parent->parent->value;
32305796c8dcSSimon Schubert       type = get_value_type (parent->parent);
32315796c8dcSSimon Schubert       if (cfull_expression)
32325796c8dcSSimon Schubert 	parent_expression = varobj_get_path_expr (parent->parent);
32335796c8dcSSimon Schubert     }
32345796c8dcSSimon Schubert   else
32355796c8dcSSimon Schubert     {
32365796c8dcSSimon Schubert       value = parent->value;
32375796c8dcSSimon Schubert       type = get_value_type (parent);
32385796c8dcSSimon Schubert       if (cfull_expression)
32395796c8dcSSimon Schubert 	parent_expression = varobj_get_path_expr (parent);
32405796c8dcSSimon Schubert     }
32415796c8dcSSimon Schubert 
32425796c8dcSSimon Schubert   adjust_value_for_child_access (&value, &type, &was_ptr);
32435796c8dcSSimon Schubert 
32445796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
32455796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_UNION)
32465796c8dcSSimon Schubert     {
32475796c8dcSSimon Schubert       char *join = was_ptr ? "->" : ".";
3248*cf7f2e2dSJohn Marino 
32495796c8dcSSimon Schubert       if (CPLUS_FAKE_CHILD (parent))
32505796c8dcSSimon Schubert 	{
32515796c8dcSSimon Schubert 	  /* The fields of the class type are ordered as they
32525796c8dcSSimon Schubert 	     appear in the class.  We are given an index for a
32535796c8dcSSimon Schubert 	     particular access control type ("public","protected",
32545796c8dcSSimon Schubert 	     or "private").  We must skip over fields that don't
32555796c8dcSSimon Schubert 	     have the access control we are looking for to properly
32565796c8dcSSimon Schubert 	     find the indexed field. */
32575796c8dcSSimon Schubert 	  int type_index = TYPE_N_BASECLASSES (type);
32585796c8dcSSimon Schubert 	  enum accessibility acc = public_field;
3259*cf7f2e2dSJohn Marino 	  int vptr_fieldno;
3260*cf7f2e2dSJohn Marino 	  struct type *basetype = NULL;
3261*cf7f2e2dSJohn Marino 
3262*cf7f2e2dSJohn Marino 	  vptr_fieldno = get_vptr_fieldno (type, &basetype);
32635796c8dcSSimon Schubert 	  if (strcmp (parent->name, "private") == 0)
32645796c8dcSSimon Schubert 	    acc = private_field;
32655796c8dcSSimon Schubert 	  else if (strcmp (parent->name, "protected") == 0)
32665796c8dcSSimon Schubert 	    acc = protected_field;
32675796c8dcSSimon Schubert 
32685796c8dcSSimon Schubert 	  while (index >= 0)
32695796c8dcSSimon Schubert 	    {
3270*cf7f2e2dSJohn Marino 	      if ((type == basetype && type_index == vptr_fieldno)
3271*cf7f2e2dSJohn Marino 		  || TYPE_FIELD_ARTIFICIAL (type, type_index))
32725796c8dcSSimon Schubert 		; /* ignore vptr */
32735796c8dcSSimon Schubert 	      else if (match_accessibility (type, type_index, acc))
32745796c8dcSSimon Schubert 		    --index;
32755796c8dcSSimon Schubert 		  ++type_index;
32765796c8dcSSimon Schubert 	    }
32775796c8dcSSimon Schubert 	  --type_index;
32785796c8dcSSimon Schubert 
32795796c8dcSSimon Schubert 	  if (cname)
32805796c8dcSSimon Schubert 	    *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
32815796c8dcSSimon Schubert 
32825796c8dcSSimon Schubert 	  if (cvalue && value)
32835796c8dcSSimon Schubert 	    *cvalue = value_struct_element_index (value, type_index);
32845796c8dcSSimon Schubert 
32855796c8dcSSimon Schubert 	  if (ctype)
32865796c8dcSSimon Schubert 	    *ctype = TYPE_FIELD_TYPE (type, type_index);
32875796c8dcSSimon Schubert 
32885796c8dcSSimon Schubert 	  if (cfull_expression)
32895796c8dcSSimon Schubert 	    *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
32905796c8dcSSimon Schubert 					    join,
32915796c8dcSSimon Schubert 					    TYPE_FIELD_NAME (type, type_index));
32925796c8dcSSimon Schubert 	}
32935796c8dcSSimon Schubert       else if (index < TYPE_N_BASECLASSES (type))
32945796c8dcSSimon Schubert 	{
32955796c8dcSSimon Schubert 	  /* This is a baseclass.  */
32965796c8dcSSimon Schubert 	  if (cname)
32975796c8dcSSimon Schubert 	    *cname = xstrdup (TYPE_FIELD_NAME (type, index));
32985796c8dcSSimon Schubert 
32995796c8dcSSimon Schubert 	  if (cvalue && value)
33005796c8dcSSimon Schubert 	    *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
33015796c8dcSSimon Schubert 
33025796c8dcSSimon Schubert 	  if (ctype)
33035796c8dcSSimon Schubert 	    {
33045796c8dcSSimon Schubert 	      *ctype = TYPE_FIELD_TYPE (type, index);
33055796c8dcSSimon Schubert 	    }
33065796c8dcSSimon Schubert 
33075796c8dcSSimon Schubert 	  if (cfull_expression)
33085796c8dcSSimon Schubert 	    {
33095796c8dcSSimon Schubert 	      char *ptr = was_ptr ? "*" : "";
3310*cf7f2e2dSJohn Marino 
33115796c8dcSSimon Schubert 	      /* Cast the parent to the base' type. Note that in gdb,
33125796c8dcSSimon Schubert 		 expression like
33135796c8dcSSimon Schubert 		         (Base1)d
33145796c8dcSSimon Schubert 		 will create an lvalue, for all appearences, so we don't
33155796c8dcSSimon Schubert 		 need to use more fancy:
33165796c8dcSSimon Schubert 		         *(Base1*)(&d)
33175796c8dcSSimon Schubert 		 construct.  */
33185796c8dcSSimon Schubert 	      *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
33195796c8dcSSimon Schubert 					      ptr,
33205796c8dcSSimon Schubert 					      TYPE_FIELD_NAME (type, index),
33215796c8dcSSimon Schubert 					      ptr,
33225796c8dcSSimon Schubert 					      parent_expression);
33235796c8dcSSimon Schubert 	    }
33245796c8dcSSimon Schubert 	}
33255796c8dcSSimon Schubert       else
33265796c8dcSSimon Schubert 	{
33275796c8dcSSimon Schubert 	  char *access = NULL;
33285796c8dcSSimon Schubert 	  int children[3];
3329*cf7f2e2dSJohn Marino 
33305796c8dcSSimon Schubert 	  cplus_class_num_children (type, children);
33315796c8dcSSimon Schubert 
33325796c8dcSSimon Schubert 	  /* Everything beyond the baseclasses can
33335796c8dcSSimon Schubert 	     only be "public", "private", or "protected"
33345796c8dcSSimon Schubert 
33355796c8dcSSimon Schubert 	     The special "fake" children are always output by varobj in
33365796c8dcSSimon Schubert 	     this order. So if INDEX == 2, it MUST be "protected". */
33375796c8dcSSimon Schubert 	  index -= TYPE_N_BASECLASSES (type);
33385796c8dcSSimon Schubert 	  switch (index)
33395796c8dcSSimon Schubert 	    {
33405796c8dcSSimon Schubert 	    case 0:
33415796c8dcSSimon Schubert 	      if (children[v_public] > 0)
33425796c8dcSSimon Schubert 	 	access = "public";
33435796c8dcSSimon Schubert 	      else if (children[v_private] > 0)
33445796c8dcSSimon Schubert 	 	access = "private";
33455796c8dcSSimon Schubert 	      else
33465796c8dcSSimon Schubert 	 	access = "protected";
33475796c8dcSSimon Schubert 	      break;
33485796c8dcSSimon Schubert 	    case 1:
33495796c8dcSSimon Schubert 	      if (children[v_public] > 0)
33505796c8dcSSimon Schubert 		{
33515796c8dcSSimon Schubert 		  if (children[v_private] > 0)
33525796c8dcSSimon Schubert 		    access = "private";
33535796c8dcSSimon Schubert 		  else
33545796c8dcSSimon Schubert 		    access = "protected";
33555796c8dcSSimon Schubert 		}
33565796c8dcSSimon Schubert 	      else if (children[v_private] > 0)
33575796c8dcSSimon Schubert 	 	access = "protected";
33585796c8dcSSimon Schubert 	      break;
33595796c8dcSSimon Schubert 	    case 2:
33605796c8dcSSimon Schubert 	      /* Must be protected */
33615796c8dcSSimon Schubert 	      access = "protected";
33625796c8dcSSimon Schubert 	      break;
33635796c8dcSSimon Schubert 	    default:
33645796c8dcSSimon Schubert 	      /* error! */
33655796c8dcSSimon Schubert 	      break;
33665796c8dcSSimon Schubert 	    }
33675796c8dcSSimon Schubert 
33685796c8dcSSimon Schubert 	  gdb_assert (access);
33695796c8dcSSimon Schubert 	  if (cname)
33705796c8dcSSimon Schubert 	    *cname = xstrdup (access);
33715796c8dcSSimon Schubert 
33725796c8dcSSimon Schubert 	  /* Value and type and full expression are null here.  */
33735796c8dcSSimon Schubert 	}
33745796c8dcSSimon Schubert     }
33755796c8dcSSimon Schubert   else
33765796c8dcSSimon Schubert     {
33775796c8dcSSimon Schubert       c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
33785796c8dcSSimon Schubert     }
33795796c8dcSSimon Schubert }
33805796c8dcSSimon Schubert 
33815796c8dcSSimon Schubert static char *
33825796c8dcSSimon Schubert cplus_name_of_child (struct varobj *parent, int index)
33835796c8dcSSimon Schubert {
33845796c8dcSSimon Schubert   char *name = NULL;
3385*cf7f2e2dSJohn Marino 
33865796c8dcSSimon Schubert   cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
33875796c8dcSSimon Schubert   return name;
33885796c8dcSSimon Schubert }
33895796c8dcSSimon Schubert 
33905796c8dcSSimon Schubert static char *
33915796c8dcSSimon Schubert cplus_path_expr_of_child (struct varobj *child)
33925796c8dcSSimon Schubert {
33935796c8dcSSimon Schubert   cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
33945796c8dcSSimon Schubert 			&child->path_expr);
33955796c8dcSSimon Schubert   return child->path_expr;
33965796c8dcSSimon Schubert }
33975796c8dcSSimon Schubert 
33985796c8dcSSimon Schubert static struct value *
33995796c8dcSSimon Schubert cplus_value_of_root (struct varobj **var_handle)
34005796c8dcSSimon Schubert {
34015796c8dcSSimon Schubert   return c_value_of_root (var_handle);
34025796c8dcSSimon Schubert }
34035796c8dcSSimon Schubert 
34045796c8dcSSimon Schubert static struct value *
34055796c8dcSSimon Schubert cplus_value_of_child (struct varobj *parent, int index)
34065796c8dcSSimon Schubert {
34075796c8dcSSimon Schubert   struct value *value = NULL;
3408*cf7f2e2dSJohn Marino 
34095796c8dcSSimon Schubert   cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
34105796c8dcSSimon Schubert   return value;
34115796c8dcSSimon Schubert }
34125796c8dcSSimon Schubert 
34135796c8dcSSimon Schubert static struct type *
34145796c8dcSSimon Schubert cplus_type_of_child (struct varobj *parent, int index)
34155796c8dcSSimon Schubert {
34165796c8dcSSimon Schubert   struct type *type = NULL;
3417*cf7f2e2dSJohn Marino 
34185796c8dcSSimon Schubert   cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
34195796c8dcSSimon Schubert   return type;
34205796c8dcSSimon Schubert }
34215796c8dcSSimon Schubert 
34225796c8dcSSimon Schubert static char *
3423*cf7f2e2dSJohn Marino cplus_value_of_variable (struct varobj *var,
3424*cf7f2e2dSJohn Marino 			 enum varobj_display_formats format)
34255796c8dcSSimon Schubert {
34265796c8dcSSimon Schubert 
34275796c8dcSSimon Schubert   /* If we have one of our special types, don't print out
34285796c8dcSSimon Schubert      any value. */
34295796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var))
34305796c8dcSSimon Schubert     return xstrdup ("");
34315796c8dcSSimon Schubert 
34325796c8dcSSimon Schubert   return c_value_of_variable (var, format);
34335796c8dcSSimon Schubert }
34345796c8dcSSimon Schubert 
34355796c8dcSSimon Schubert /* Java */
34365796c8dcSSimon Schubert 
34375796c8dcSSimon Schubert static int
34385796c8dcSSimon Schubert java_number_of_children (struct varobj *var)
34395796c8dcSSimon Schubert {
34405796c8dcSSimon Schubert   return cplus_number_of_children (var);
34415796c8dcSSimon Schubert }
34425796c8dcSSimon Schubert 
34435796c8dcSSimon Schubert static char *
34445796c8dcSSimon Schubert java_name_of_variable (struct varobj *parent)
34455796c8dcSSimon Schubert {
34465796c8dcSSimon Schubert   char *p, *name;
34475796c8dcSSimon Schubert 
34485796c8dcSSimon Schubert   name = cplus_name_of_variable (parent);
34495796c8dcSSimon Schubert   /* If  the name has "-" in it, it is because we
34505796c8dcSSimon Schubert      needed to escape periods in the name... */
34515796c8dcSSimon Schubert   p = name;
34525796c8dcSSimon Schubert 
34535796c8dcSSimon Schubert   while (*p != '\000')
34545796c8dcSSimon Schubert     {
34555796c8dcSSimon Schubert       if (*p == '-')
34565796c8dcSSimon Schubert 	*p = '.';
34575796c8dcSSimon Schubert       p++;
34585796c8dcSSimon Schubert     }
34595796c8dcSSimon Schubert 
34605796c8dcSSimon Schubert   return name;
34615796c8dcSSimon Schubert }
34625796c8dcSSimon Schubert 
34635796c8dcSSimon Schubert static char *
34645796c8dcSSimon Schubert java_name_of_child (struct varobj *parent, int index)
34655796c8dcSSimon Schubert {
34665796c8dcSSimon Schubert   char *name, *p;
34675796c8dcSSimon Schubert 
34685796c8dcSSimon Schubert   name = cplus_name_of_child (parent, index);
34695796c8dcSSimon Schubert   /* Escape any periods in the name... */
34705796c8dcSSimon Schubert   p = name;
34715796c8dcSSimon Schubert 
34725796c8dcSSimon Schubert   while (*p != '\000')
34735796c8dcSSimon Schubert     {
34745796c8dcSSimon Schubert       if (*p == '.')
34755796c8dcSSimon Schubert 	*p = '-';
34765796c8dcSSimon Schubert       p++;
34775796c8dcSSimon Schubert     }
34785796c8dcSSimon Schubert 
34795796c8dcSSimon Schubert   return name;
34805796c8dcSSimon Schubert }
34815796c8dcSSimon Schubert 
34825796c8dcSSimon Schubert static char *
34835796c8dcSSimon Schubert java_path_expr_of_child (struct varobj *child)
34845796c8dcSSimon Schubert {
34855796c8dcSSimon Schubert   return NULL;
34865796c8dcSSimon Schubert }
34875796c8dcSSimon Schubert 
34885796c8dcSSimon Schubert static struct value *
34895796c8dcSSimon Schubert java_value_of_root (struct varobj **var_handle)
34905796c8dcSSimon Schubert {
34915796c8dcSSimon Schubert   return cplus_value_of_root (var_handle);
34925796c8dcSSimon Schubert }
34935796c8dcSSimon Schubert 
34945796c8dcSSimon Schubert static struct value *
34955796c8dcSSimon Schubert java_value_of_child (struct varobj *parent, int index)
34965796c8dcSSimon Schubert {
34975796c8dcSSimon Schubert   return cplus_value_of_child (parent, index);
34985796c8dcSSimon Schubert }
34995796c8dcSSimon Schubert 
35005796c8dcSSimon Schubert static struct type *
35015796c8dcSSimon Schubert java_type_of_child (struct varobj *parent, int index)
35025796c8dcSSimon Schubert {
35035796c8dcSSimon Schubert   return cplus_type_of_child (parent, index);
35045796c8dcSSimon Schubert }
35055796c8dcSSimon Schubert 
35065796c8dcSSimon Schubert static char *
35075796c8dcSSimon Schubert java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
35085796c8dcSSimon Schubert {
35095796c8dcSSimon Schubert   return cplus_value_of_variable (var, format);
35105796c8dcSSimon Schubert }
35115796c8dcSSimon Schubert 
35125796c8dcSSimon Schubert /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
35135796c8dcSSimon Schubert    with an arbitrary caller supplied DATA pointer.  */
35145796c8dcSSimon Schubert 
35155796c8dcSSimon Schubert void
35165796c8dcSSimon Schubert all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
35175796c8dcSSimon Schubert {
35185796c8dcSSimon Schubert   struct varobj_root *var_root, *var_root_next;
35195796c8dcSSimon Schubert 
35205796c8dcSSimon Schubert   /* Iterate "safely" - handle if the callee deletes its passed VAROBJ.  */
35215796c8dcSSimon Schubert 
35225796c8dcSSimon Schubert   for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
35235796c8dcSSimon Schubert     {
35245796c8dcSSimon Schubert       var_root_next = var_root->next;
35255796c8dcSSimon Schubert 
35265796c8dcSSimon Schubert       (*func) (var_root->rootvar, data);
35275796c8dcSSimon Schubert     }
35285796c8dcSSimon Schubert }
35295796c8dcSSimon Schubert 
35305796c8dcSSimon Schubert extern void _initialize_varobj (void);
35315796c8dcSSimon Schubert void
35325796c8dcSSimon Schubert _initialize_varobj (void)
35335796c8dcSSimon Schubert {
35345796c8dcSSimon Schubert   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
35355796c8dcSSimon Schubert 
35365796c8dcSSimon Schubert   varobj_table = xmalloc (sizeof_table);
35375796c8dcSSimon Schubert   memset (varobj_table, 0, sizeof_table);
35385796c8dcSSimon Schubert 
35395796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
35405796c8dcSSimon Schubert 			    &varobjdebug, _("\
35415796c8dcSSimon Schubert Set varobj debugging."), _("\
35425796c8dcSSimon Schubert Show varobj debugging."), _("\
35435796c8dcSSimon Schubert When non-zero, varobj debugging is enabled."),
35445796c8dcSSimon Schubert 			    NULL,
35455796c8dcSSimon Schubert 			    show_varobjdebug,
35465796c8dcSSimon Schubert 			    &setlist, &showlist);
35475796c8dcSSimon Schubert }
35485796c8dcSSimon Schubert 
35495796c8dcSSimon Schubert /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
35505796c8dcSSimon Schubert    defined on globals.  It is a helper for varobj_invalidate.  */
35515796c8dcSSimon Schubert 
35525796c8dcSSimon Schubert static void
35535796c8dcSSimon Schubert varobj_invalidate_iter (struct varobj *var, void *unused)
35545796c8dcSSimon Schubert {
35555796c8dcSSimon Schubert   /* Floating varobjs are reparsed on each stop, so we don't care if the
35565796c8dcSSimon Schubert      presently parsed expression refers to something that's gone.  */
35575796c8dcSSimon Schubert   if (var->root->floating)
35585796c8dcSSimon Schubert     return;
35595796c8dcSSimon Schubert 
35605796c8dcSSimon Schubert   /* global var must be re-evaluated.  */
35615796c8dcSSimon Schubert   if (var->root->valid_block == NULL)
35625796c8dcSSimon Schubert     {
35635796c8dcSSimon Schubert       struct varobj *tmp_var;
35645796c8dcSSimon Schubert 
35655796c8dcSSimon Schubert       /* Try to create a varobj with same expression.  If we succeed
35665796c8dcSSimon Schubert 	 replace the old varobj, otherwise invalidate it.  */
35675796c8dcSSimon Schubert       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
35685796c8dcSSimon Schubert 			       USE_CURRENT_FRAME);
35695796c8dcSSimon Schubert       if (tmp_var != NULL)
35705796c8dcSSimon Schubert 	{
35715796c8dcSSimon Schubert 	  tmp_var->obj_name = xstrdup (var->obj_name);
35725796c8dcSSimon Schubert 	  varobj_delete (var, NULL, 0);
35735796c8dcSSimon Schubert 	  install_variable (tmp_var);
35745796c8dcSSimon Schubert 	}
35755796c8dcSSimon Schubert       else
35765796c8dcSSimon Schubert 	var->root->is_valid = 0;
35775796c8dcSSimon Schubert     }
35785796c8dcSSimon Schubert   else /* locals must be invalidated.  */
35795796c8dcSSimon Schubert     var->root->is_valid = 0;
35805796c8dcSSimon Schubert }
35815796c8dcSSimon Schubert 
35825796c8dcSSimon Schubert /* Invalidate the varobjs that are tied to locals and re-create the ones that
35835796c8dcSSimon Schubert    are defined on globals.
35845796c8dcSSimon Schubert    Invalidated varobjs will be always printed in_scope="invalid".  */
35855796c8dcSSimon Schubert 
35865796c8dcSSimon Schubert void
35875796c8dcSSimon Schubert varobj_invalidate (void)
35885796c8dcSSimon Schubert {
35895796c8dcSSimon Schubert   all_root_varobjs (varobj_invalidate_iter, NULL);
35905796c8dcSSimon Schubert }
3591