xref: /dflybsd-src/contrib/gdb-7/gdb/varobj.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Implementation of the GDB variable objects API.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1999-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
65796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
75796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
85796c8dcSSimon Schubert    (at your option) any later version.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
115796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
125796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
135796c8dcSSimon Schubert    GNU General Public License for more details.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
165796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert #include "defs.h"
195796c8dcSSimon Schubert #include "exceptions.h"
205796c8dcSSimon Schubert #include "value.h"
215796c8dcSSimon Schubert #include "expression.h"
225796c8dcSSimon Schubert #include "frame.h"
235796c8dcSSimon Schubert #include "language.h"
245796c8dcSSimon Schubert #include "wrapper.h"
255796c8dcSSimon Schubert #include "gdbcmd.h"
265796c8dcSSimon Schubert #include "block.h"
275796c8dcSSimon Schubert #include "valprint.h"
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert #include "gdb_assert.h"
305796c8dcSSimon Schubert #include "gdb_string.h"
315796c8dcSSimon Schubert #include "gdb_regex.h"
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert #include "varobj.h"
345796c8dcSSimon Schubert #include "vec.h"
355796c8dcSSimon Schubert #include "gdbthread.h"
365796c8dcSSimon Schubert #include "inferior.h"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #if HAVE_PYTHON
395796c8dcSSimon Schubert #include "python/python.h"
405796c8dcSSimon Schubert #include "python/python-internal.h"
415796c8dcSSimon Schubert #else
425796c8dcSSimon Schubert typedef int PyObject;
435796c8dcSSimon Schubert #endif
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert /* Non-zero if we want to see trace of varobj level stuff.  */
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert int varobjdebug = 0;
485796c8dcSSimon Schubert static void
495796c8dcSSimon Schubert show_varobjdebug (struct ui_file *file, int from_tty,
505796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
515796c8dcSSimon Schubert {
525796c8dcSSimon Schubert   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
535796c8dcSSimon Schubert }
545796c8dcSSimon Schubert 
55c50c785cSJohn Marino /* String representations of gdb's format codes.  */
565796c8dcSSimon Schubert char *varobj_format_string[] =
575796c8dcSSimon Schubert   { "natural", "binary", "decimal", "hexadecimal", "octal" };
585796c8dcSSimon Schubert 
59c50c785cSJohn Marino /* String representations of gdb's known languages.  */
605796c8dcSSimon Schubert char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert /* True if we want to allow Python-based pretty-printing.  */
635796c8dcSSimon Schubert static int pretty_printing = 0;
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert void
665796c8dcSSimon Schubert varobj_enable_pretty_printing (void)
675796c8dcSSimon Schubert {
685796c8dcSSimon Schubert   pretty_printing = 1;
695796c8dcSSimon Schubert }
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert /* Data structures */
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert /* Every root variable has one of these structures saved in its
745796c8dcSSimon Schubert    varobj.  Members which must be free'd are noted.  */
755796c8dcSSimon Schubert struct varobj_root
765796c8dcSSimon Schubert {
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert   /* Alloc'd expression for this parent.  */
795796c8dcSSimon Schubert   struct expression *exp;
805796c8dcSSimon Schubert 
81c50c785cSJohn Marino   /* Block for which this expression is valid.  */
825796c8dcSSimon Schubert   struct block *valid_block;
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert   /* The frame for this expression.  This field is set iff valid_block is
855796c8dcSSimon Schubert      not NULL.  */
865796c8dcSSimon Schubert   struct frame_id frame;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert   /* The thread ID that this varobj_root belong to.  This field
895796c8dcSSimon Schubert      is only valid if valid_block is not NULL.
905796c8dcSSimon Schubert      When not 0, indicates which thread 'frame' belongs to.
915796c8dcSSimon Schubert      When 0, indicates that the thread list was empty when the varobj_root
925796c8dcSSimon Schubert      was created.  */
935796c8dcSSimon Schubert   int thread_id;
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert   /* If 1, the -var-update always recomputes the value in the
965796c8dcSSimon Schubert      current thread and frame.  Otherwise, variable object is
97c50c785cSJohn Marino      always updated in the specific scope/thread/frame.  */
985796c8dcSSimon Schubert   int floating;
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert   /* Flag that indicates validity: set to 0 when this varobj_root refers
1015796c8dcSSimon Schubert      to symbols that do not exist anymore.  */
1025796c8dcSSimon Schubert   int is_valid;
1035796c8dcSSimon Schubert 
104c50c785cSJohn Marino   /* Language info for this variable and its children.  */
1055796c8dcSSimon Schubert   struct language_specific *lang;
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert   /* The varobj for this root node.  */
1085796c8dcSSimon Schubert   struct varobj *rootvar;
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert   /* Next root variable */
1115796c8dcSSimon Schubert   struct varobj_root *next;
1125796c8dcSSimon Schubert };
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert /* Every variable in the system has a structure of this type defined
1155796c8dcSSimon Schubert    for it.  This structure holds all information necessary to manipulate
1165796c8dcSSimon Schubert    a particular object variable.  Members which must be freed are noted.  */
1175796c8dcSSimon Schubert struct varobj
1185796c8dcSSimon Schubert {
1195796c8dcSSimon Schubert 
120c50c785cSJohn Marino   /* Alloc'd name of the variable for this object.  If this variable is a
1215796c8dcSSimon Schubert      child, then this name will be the child's source name.
122c50c785cSJohn Marino      (bar, not foo.bar).  */
123c50c785cSJohn Marino   /* NOTE: This is the "expression".  */
1245796c8dcSSimon Schubert   char *name;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   /* Alloc'd expression for this child.  Can be used to create a
1275796c8dcSSimon Schubert      root variable corresponding to this child.  */
1285796c8dcSSimon Schubert   char *path_expr;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   /* The alloc'd name for this variable's object.  This is here for
1315796c8dcSSimon Schubert      convenience when constructing this object's children.  */
1325796c8dcSSimon Schubert   char *obj_name;
1335796c8dcSSimon Schubert 
134c50c785cSJohn Marino   /* Index of this variable in its parent or -1.  */
1355796c8dcSSimon Schubert   int index;
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert   /* The type of this variable.  This can be NULL
1385796c8dcSSimon Schubert      for artifial variable objects -- currently, the "accessibility"
1395796c8dcSSimon Schubert      variable objects in C++.  */
1405796c8dcSSimon Schubert   struct type *type;
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   /* The value of this expression or subexpression.  A NULL value
1435796c8dcSSimon Schubert      indicates there was an error getting this value.
1445796c8dcSSimon Schubert      Invariant: if varobj_value_is_changeable_p (this) is non-zero,
1455796c8dcSSimon Schubert      the value is either NULL, or not lazy.  */
1465796c8dcSSimon Schubert   struct value *value;
1475796c8dcSSimon Schubert 
148c50c785cSJohn Marino   /* The number of (immediate) children this variable has.  */
1495796c8dcSSimon Schubert   int num_children;
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert   /* If this object is a child, this points to its immediate parent.  */
1525796c8dcSSimon Schubert   struct varobj *parent;
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert   /* Children of this object.  */
1555796c8dcSSimon Schubert   VEC (varobj_p) *children;
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert   /* Whether the children of this varobj were requested.  This field is
1585796c8dcSSimon Schubert      used to decide if dynamic varobj should recompute their children.
1595796c8dcSSimon Schubert      In the event that the frontend never asked for the children, we
1605796c8dcSSimon Schubert      can avoid that.  */
1615796c8dcSSimon Schubert   int children_requested;
1625796c8dcSSimon Schubert 
163c50c785cSJohn Marino   /* Description of the root variable.  Points to root variable for
164c50c785cSJohn Marino      children.  */
1655796c8dcSSimon Schubert   struct varobj_root *root;
1665796c8dcSSimon Schubert 
167c50c785cSJohn Marino   /* The format of the output for this object.  */
1685796c8dcSSimon Schubert   enum varobj_display_formats format;
1695796c8dcSSimon Schubert 
170c50c785cSJohn Marino   /* 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 
296cf7f2e2dSJohn Marino #if HAVE_PYTHON
297cf7f2e2dSJohn Marino 
298c50c785cSJohn Marino static struct varobj *varobj_add_child (struct varobj *var,
299c50c785cSJohn Marino 					const char *name,
300c50c785cSJohn Marino 					struct value *value);
3015796c8dcSSimon Schubert 
302cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
303cf7f2e2dSJohn Marino 
3045796c8dcSSimon Schubert /* C implementation */
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert static int c_number_of_children (struct varobj *var);
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert static char *c_name_of_variable (struct varobj *parent);
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert static char *c_name_of_child (struct varobj *parent, int index);
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert static char *c_path_expr_of_child (struct varobj *child);
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert static struct value *c_value_of_root (struct varobj **var_handle);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert static struct value *c_value_of_child (struct varobj *parent, int index);
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert static struct type *c_type_of_child (struct varobj *parent, int index);
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert static char *c_value_of_variable (struct varobj *var,
3215796c8dcSSimon Schubert 				  enum varobj_display_formats format);
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert /* C++ implementation */
3245796c8dcSSimon Schubert 
3255796c8dcSSimon Schubert static int cplus_number_of_children (struct varobj *var);
3265796c8dcSSimon Schubert 
3275796c8dcSSimon Schubert static void cplus_class_num_children (struct type *type, int children[3]);
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert static char *cplus_name_of_variable (struct varobj *parent);
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert static char *cplus_name_of_child (struct varobj *parent, int index);
3325796c8dcSSimon Schubert 
3335796c8dcSSimon Schubert static char *cplus_path_expr_of_child (struct varobj *child);
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert static struct value *cplus_value_of_root (struct varobj **var_handle);
3365796c8dcSSimon Schubert 
3375796c8dcSSimon Schubert static struct value *cplus_value_of_child (struct varobj *parent, int index);
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert static struct type *cplus_type_of_child (struct varobj *parent, int index);
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert static char *cplus_value_of_variable (struct varobj *var,
3425796c8dcSSimon Schubert 				      enum varobj_display_formats format);
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert /* Java implementation */
3455796c8dcSSimon Schubert 
3465796c8dcSSimon Schubert static int java_number_of_children (struct varobj *var);
3475796c8dcSSimon Schubert 
3485796c8dcSSimon Schubert static char *java_name_of_variable (struct varobj *parent);
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert static char *java_name_of_child (struct varobj *parent, int index);
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert static char *java_path_expr_of_child (struct varobj *child);
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert static struct value *java_value_of_root (struct varobj **var_handle);
3555796c8dcSSimon Schubert 
3565796c8dcSSimon Schubert static struct value *java_value_of_child (struct varobj *parent, int index);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert static struct type *java_type_of_child (struct varobj *parent, int index);
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert static char *java_value_of_variable (struct varobj *var,
3615796c8dcSSimon Schubert 				     enum varobj_display_formats format);
3625796c8dcSSimon Schubert 
363*a45ae5f8SJohn Marino /* Ada implementation */
364*a45ae5f8SJohn Marino 
365*a45ae5f8SJohn Marino static int ada_number_of_children (struct varobj *var);
366*a45ae5f8SJohn Marino 
367*a45ae5f8SJohn Marino static char *ada_name_of_variable (struct varobj *parent);
368*a45ae5f8SJohn Marino 
369*a45ae5f8SJohn Marino static char *ada_name_of_child (struct varobj *parent, int index);
370*a45ae5f8SJohn Marino 
371*a45ae5f8SJohn Marino static char *ada_path_expr_of_child (struct varobj *child);
372*a45ae5f8SJohn Marino 
373*a45ae5f8SJohn Marino static struct value *ada_value_of_root (struct varobj **var_handle);
374*a45ae5f8SJohn Marino 
375*a45ae5f8SJohn Marino static struct value *ada_value_of_child (struct varobj *parent, int index);
376*a45ae5f8SJohn Marino 
377*a45ae5f8SJohn Marino static struct type *ada_type_of_child (struct varobj *parent, int index);
378*a45ae5f8SJohn Marino 
379*a45ae5f8SJohn Marino static char *ada_value_of_variable (struct varobj *var,
380*a45ae5f8SJohn Marino 				    enum varobj_display_formats format);
381*a45ae5f8SJohn Marino 
3825796c8dcSSimon Schubert /* The language specific vector */
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert struct language_specific
3855796c8dcSSimon Schubert {
3865796c8dcSSimon Schubert 
387c50c785cSJohn Marino   /* The language of this variable.  */
3885796c8dcSSimon Schubert   enum varobj_languages language;
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert   /* The number of children of PARENT.  */
3915796c8dcSSimon Schubert   int (*number_of_children) (struct varobj * parent);
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   /* The name (expression) of a root varobj.  */
3945796c8dcSSimon Schubert   char *(*name_of_variable) (struct varobj * parent);
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert   /* The name of the INDEX'th child of PARENT.  */
3975796c8dcSSimon Schubert   char *(*name_of_child) (struct varobj * parent, int index);
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   /* Returns the rooted expression of CHILD, which is a variable
4005796c8dcSSimon Schubert      obtain that has some parent.  */
4015796c8dcSSimon Schubert   char *(*path_expr_of_child) (struct varobj * child);
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert   /* The ``struct value *'' of the root variable ROOT.  */
4045796c8dcSSimon Schubert   struct value *(*value_of_root) (struct varobj ** root_handle);
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert   /* The ``struct value *'' of the INDEX'th child of PARENT.  */
4075796c8dcSSimon Schubert   struct value *(*value_of_child) (struct varobj * parent, int index);
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert   /* The type of the INDEX'th child of PARENT.  */
4105796c8dcSSimon Schubert   struct type *(*type_of_child) (struct varobj * parent, int index);
4115796c8dcSSimon Schubert 
4125796c8dcSSimon Schubert   /* The current value of VAR.  */
4135796c8dcSSimon Schubert   char *(*value_of_variable) (struct varobj * var,
4145796c8dcSSimon Schubert 			      enum varobj_display_formats format);
4155796c8dcSSimon Schubert };
4165796c8dcSSimon Schubert 
4175796c8dcSSimon Schubert /* Array of known source language routines.  */
4185796c8dcSSimon Schubert static struct language_specific languages[vlang_end] = {
419c50c785cSJohn Marino   /* Unknown (try treating as C).  */
4205796c8dcSSimon Schubert   {
4215796c8dcSSimon Schubert    vlang_unknown,
4225796c8dcSSimon Schubert    c_number_of_children,
4235796c8dcSSimon Schubert    c_name_of_variable,
4245796c8dcSSimon Schubert    c_name_of_child,
4255796c8dcSSimon Schubert    c_path_expr_of_child,
4265796c8dcSSimon Schubert    c_value_of_root,
4275796c8dcSSimon Schubert    c_value_of_child,
4285796c8dcSSimon Schubert    c_type_of_child,
4295796c8dcSSimon Schubert    c_value_of_variable}
4305796c8dcSSimon Schubert   ,
4315796c8dcSSimon Schubert   /* C */
4325796c8dcSSimon Schubert   {
4335796c8dcSSimon Schubert    vlang_c,
4345796c8dcSSimon Schubert    c_number_of_children,
4355796c8dcSSimon Schubert    c_name_of_variable,
4365796c8dcSSimon Schubert    c_name_of_child,
4375796c8dcSSimon Schubert    c_path_expr_of_child,
4385796c8dcSSimon Schubert    c_value_of_root,
4395796c8dcSSimon Schubert    c_value_of_child,
4405796c8dcSSimon Schubert    c_type_of_child,
4415796c8dcSSimon Schubert    c_value_of_variable}
4425796c8dcSSimon Schubert   ,
4435796c8dcSSimon Schubert   /* C++ */
4445796c8dcSSimon Schubert   {
4455796c8dcSSimon Schubert    vlang_cplus,
4465796c8dcSSimon Schubert    cplus_number_of_children,
4475796c8dcSSimon Schubert    cplus_name_of_variable,
4485796c8dcSSimon Schubert    cplus_name_of_child,
4495796c8dcSSimon Schubert    cplus_path_expr_of_child,
4505796c8dcSSimon Schubert    cplus_value_of_root,
4515796c8dcSSimon Schubert    cplus_value_of_child,
4525796c8dcSSimon Schubert    cplus_type_of_child,
4535796c8dcSSimon Schubert    cplus_value_of_variable}
4545796c8dcSSimon Schubert   ,
4555796c8dcSSimon Schubert   /* Java */
4565796c8dcSSimon Schubert   {
4575796c8dcSSimon Schubert    vlang_java,
4585796c8dcSSimon Schubert    java_number_of_children,
4595796c8dcSSimon Schubert    java_name_of_variable,
4605796c8dcSSimon Schubert    java_name_of_child,
4615796c8dcSSimon Schubert    java_path_expr_of_child,
4625796c8dcSSimon Schubert    java_value_of_root,
4635796c8dcSSimon Schubert    java_value_of_child,
4645796c8dcSSimon Schubert    java_type_of_child,
465*a45ae5f8SJohn Marino    java_value_of_variable},
466*a45ae5f8SJohn Marino   /* Ada */
467*a45ae5f8SJohn Marino   {
468*a45ae5f8SJohn Marino    vlang_ada,
469*a45ae5f8SJohn Marino    ada_number_of_children,
470*a45ae5f8SJohn Marino    ada_name_of_variable,
471*a45ae5f8SJohn Marino    ada_name_of_child,
472*a45ae5f8SJohn Marino    ada_path_expr_of_child,
473*a45ae5f8SJohn Marino    ada_value_of_root,
474*a45ae5f8SJohn Marino    ada_value_of_child,
475*a45ae5f8SJohn Marino    ada_type_of_child,
476*a45ae5f8SJohn Marino    ada_value_of_variable}
4775796c8dcSSimon Schubert };
4785796c8dcSSimon Schubert 
479c50c785cSJohn Marino /* A little convenience enum for dealing with C++/Java.  */
4805796c8dcSSimon Schubert enum vsections
4815796c8dcSSimon Schubert {
4825796c8dcSSimon Schubert   v_public = 0, v_private, v_protected
4835796c8dcSSimon Schubert };
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert /* Private data */
4865796c8dcSSimon Schubert 
487c50c785cSJohn Marino /* Mappings of varobj_display_formats enums to gdb's format codes.  */
4885796c8dcSSimon Schubert static int format_code[] = { 0, 't', 'd', 'x', 'o' };
4895796c8dcSSimon Schubert 
490c50c785cSJohn Marino /* Header of the list of root variable objects.  */
4915796c8dcSSimon Schubert static struct varobj_root *rootlist;
4925796c8dcSSimon Schubert 
493c50c785cSJohn Marino /* Prime number indicating the number of buckets in the hash table.  */
494c50c785cSJohn Marino /* A prime large enough to avoid too many colisions.  */
4955796c8dcSSimon Schubert #define VAROBJ_TABLE_SIZE 227
4965796c8dcSSimon Schubert 
497c50c785cSJohn Marino /* Pointer to the varobj hash table (built at run time).  */
4985796c8dcSSimon Schubert static struct vlist **varobj_table;
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert /* Is the variable X one of our "fake" children?  */
5015796c8dcSSimon Schubert #define CPLUS_FAKE_CHILD(x) \
5025796c8dcSSimon Schubert ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert /* API Implementation */
5065796c8dcSSimon Schubert static int
5075796c8dcSSimon Schubert is_root_p (struct varobj *var)
5085796c8dcSSimon Schubert {
5095796c8dcSSimon Schubert   return (var->root->rootvar == var);
5105796c8dcSSimon Schubert }
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert #ifdef HAVE_PYTHON
5135796c8dcSSimon Schubert /* Helper function to install a Python environment suitable for
5145796c8dcSSimon Schubert    use during operations on VAR.  */
5155796c8dcSSimon Schubert struct cleanup *
5165796c8dcSSimon Schubert varobj_ensure_python_env (struct varobj *var)
5175796c8dcSSimon Schubert {
5185796c8dcSSimon Schubert   return ensure_python_env (var->root->exp->gdbarch,
5195796c8dcSSimon Schubert 			    var->root->exp->language_defn);
5205796c8dcSSimon Schubert }
5215796c8dcSSimon Schubert #endif
5225796c8dcSSimon Schubert 
523c50c785cSJohn Marino /* Creates a varobj (not its children).  */
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert /* Return the full FRAME which corresponds to the given CORE_ADDR
5265796c8dcSSimon Schubert    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
5275796c8dcSSimon Schubert 
5285796c8dcSSimon Schubert static struct frame_info *
5295796c8dcSSimon Schubert find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
5305796c8dcSSimon Schubert {
5315796c8dcSSimon Schubert   struct frame_info *frame = NULL;
5325796c8dcSSimon Schubert 
5335796c8dcSSimon Schubert   if (frame_addr == (CORE_ADDR) 0)
5345796c8dcSSimon Schubert     return NULL;
5355796c8dcSSimon Schubert 
5365796c8dcSSimon Schubert   for (frame = get_current_frame ();
5375796c8dcSSimon Schubert        frame != NULL;
5385796c8dcSSimon Schubert        frame = get_prev_frame (frame))
5395796c8dcSSimon Schubert     {
5405796c8dcSSimon Schubert       /* The CORE_ADDR we get as argument was parsed from a string GDB
5415796c8dcSSimon Schubert 	 output as $fp.  This output got truncated to gdbarch_addr_bit.
5425796c8dcSSimon Schubert 	 Truncate the frame base address in the same manner before
5435796c8dcSSimon Schubert 	 comparing it against our argument.  */
5445796c8dcSSimon Schubert       CORE_ADDR frame_base = get_frame_base_address (frame);
5455796c8dcSSimon Schubert       int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
546cf7f2e2dSJohn Marino 
5475796c8dcSSimon Schubert       if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
5485796c8dcSSimon Schubert 	frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert       if (frame_base == frame_addr)
5515796c8dcSSimon Schubert 	return frame;
5525796c8dcSSimon Schubert     }
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert   return NULL;
5555796c8dcSSimon Schubert }
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert struct varobj *
5585796c8dcSSimon Schubert varobj_create (char *objname,
5595796c8dcSSimon Schubert 	       char *expression, CORE_ADDR frame, enum varobj_type type)
5605796c8dcSSimon Schubert {
5615796c8dcSSimon Schubert   struct varobj *var;
5625796c8dcSSimon Schubert   struct cleanup *old_chain;
5635796c8dcSSimon Schubert 
5645796c8dcSSimon Schubert   /* Fill out a varobj structure for the (root) variable being constructed.  */
5655796c8dcSSimon Schubert   var = new_root_variable ();
5665796c8dcSSimon Schubert   old_chain = make_cleanup_free_variable (var);
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   if (expression != NULL)
5695796c8dcSSimon Schubert     {
570c50c785cSJohn Marino       struct frame_info *fi;
571c50c785cSJohn Marino       struct frame_id old_id = null_frame_id;
572c50c785cSJohn Marino       struct block *block;
5735796c8dcSSimon Schubert       char *p;
5745796c8dcSSimon Schubert       enum varobj_languages lang;
5755796c8dcSSimon Schubert       struct value *value = NULL;
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert       /* Parse and evaluate the expression, filling in as much of the
5785796c8dcSSimon Schubert          variable's data as possible.  */
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert       if (has_stack_frames ())
5815796c8dcSSimon Schubert 	{
582c50c785cSJohn Marino 	  /* Allow creator to specify context of variable.  */
5835796c8dcSSimon Schubert 	  if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
5845796c8dcSSimon Schubert 	    fi = get_selected_frame (NULL);
5855796c8dcSSimon Schubert 	  else
5865796c8dcSSimon Schubert 	    /* FIXME: cagney/2002-11-23: This code should be doing a
5875796c8dcSSimon Schubert 	       lookup using the frame ID and not just the frame's
5885796c8dcSSimon Schubert 	       ``address''.  This, of course, means an interface
5895796c8dcSSimon Schubert 	       change.  However, with out that interface change ISAs,
5905796c8dcSSimon Schubert 	       such as the ia64 with its two stacks, won't work.
5915796c8dcSSimon Schubert 	       Similar goes for the case where there is a frameless
5925796c8dcSSimon Schubert 	       function.  */
5935796c8dcSSimon Schubert 	    fi = find_frame_addr_in_frame_chain (frame);
5945796c8dcSSimon Schubert 	}
5955796c8dcSSimon Schubert       else
5965796c8dcSSimon Schubert 	fi = NULL;
5975796c8dcSSimon Schubert 
598c50c785cSJohn Marino       /* frame = -2 means always use selected frame.  */
5995796c8dcSSimon Schubert       if (type == USE_SELECTED_FRAME)
6005796c8dcSSimon Schubert 	var->root->floating = 1;
6015796c8dcSSimon Schubert 
6025796c8dcSSimon Schubert       block = NULL;
6035796c8dcSSimon Schubert       if (fi != NULL)
6045796c8dcSSimon Schubert 	block = get_frame_block (fi, 0);
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert       p = expression;
6075796c8dcSSimon Schubert       innermost_block = NULL;
6085796c8dcSSimon Schubert       /* Wrap the call to parse expression, so we can
6095796c8dcSSimon Schubert          return a sensible error.  */
6105796c8dcSSimon Schubert       if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
6115796c8dcSSimon Schubert 	{
612*a45ae5f8SJohn Marino 	  do_cleanups (old_chain);
6135796c8dcSSimon Schubert 	  return NULL;
6145796c8dcSSimon Schubert 	}
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert       /* Don't allow variables to be created for types.  */
6175796c8dcSSimon Schubert       if (var->root->exp->elts[0].opcode == OP_TYPE)
6185796c8dcSSimon Schubert 	{
6195796c8dcSSimon Schubert 	  do_cleanups (old_chain);
6205796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
6215796c8dcSSimon Schubert 			      " as an expression.\n");
6225796c8dcSSimon Schubert 	  return NULL;
6235796c8dcSSimon Schubert 	}
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert       var->format = variable_default_display (var);
6265796c8dcSSimon Schubert       var->root->valid_block = innermost_block;
6275796c8dcSSimon Schubert       var->name = xstrdup (expression);
6285796c8dcSSimon Schubert       /* For a root var, the name and the expr are the same.  */
6295796c8dcSSimon Schubert       var->path_expr = xstrdup (expression);
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert       /* When the frame is different from the current frame,
6325796c8dcSSimon Schubert          we must select the appropriate frame before parsing
6335796c8dcSSimon Schubert          the expression, otherwise the value will not be current.
6345796c8dcSSimon Schubert          Since select_frame is so benign, just call it for all cases.  */
6355796c8dcSSimon Schubert       if (innermost_block)
6365796c8dcSSimon Schubert 	{
6375796c8dcSSimon Schubert 	  /* User could specify explicit FRAME-ADDR which was not found but
6385796c8dcSSimon Schubert 	     EXPRESSION is frame specific and we would not be able to evaluate
6395796c8dcSSimon Schubert 	     it correctly next time.  With VALID_BLOCK set we must also set
6405796c8dcSSimon Schubert 	     FRAME and THREAD_ID.  */
6415796c8dcSSimon Schubert 	  if (fi == NULL)
6425796c8dcSSimon Schubert 	    error (_("Failed to find the specified frame"));
6435796c8dcSSimon Schubert 
6445796c8dcSSimon Schubert 	  var->root->frame = get_frame_id (fi);
6455796c8dcSSimon Schubert 	  var->root->thread_id = pid_to_thread_id (inferior_ptid);
646c50c785cSJohn Marino 	  old_id = get_frame_id (get_selected_frame (NULL));
6475796c8dcSSimon Schubert 	  select_frame (fi);
6485796c8dcSSimon Schubert 	}
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert       /* We definitely need to catch errors here.
6515796c8dcSSimon Schubert          If evaluate_expression succeeds we got the value we wanted.
652c50c785cSJohn Marino          But if it fails, we still go on with a call to evaluate_type().  */
6535796c8dcSSimon Schubert       if (!gdb_evaluate_expression (var->root->exp, &value))
6545796c8dcSSimon Schubert 	{
6555796c8dcSSimon Schubert 	  /* Error getting the value.  Try to at least get the
6565796c8dcSSimon Schubert 	     right type.  */
6575796c8dcSSimon Schubert 	  struct value *type_only_value = evaluate_type (var->root->exp);
658cf7f2e2dSJohn Marino 
6595796c8dcSSimon Schubert 	  var->type = value_type (type_only_value);
6605796c8dcSSimon Schubert 	}
6615796c8dcSSimon Schubert       else
6625796c8dcSSimon Schubert 	var->type = value_type (value);
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert       install_new_value (var, value, 1 /* Initial assignment */);
6655796c8dcSSimon Schubert 
6665796c8dcSSimon Schubert       /* Set language info */
6675796c8dcSSimon Schubert       lang = variable_language (var);
6685796c8dcSSimon Schubert       var->root->lang = &languages[lang];
6695796c8dcSSimon Schubert 
670c50c785cSJohn Marino       /* Set ourselves as our root.  */
6715796c8dcSSimon Schubert       var->root->rootvar = var;
6725796c8dcSSimon Schubert 
673c50c785cSJohn Marino       /* Reset the selected frame.  */
674c50c785cSJohn Marino       if (frame_id_p (old_id))
675c50c785cSJohn Marino 	select_frame (frame_find_by_id (old_id));
6765796c8dcSSimon Schubert     }
6775796c8dcSSimon Schubert 
6785796c8dcSSimon Schubert   /* If the variable object name is null, that means this
6795796c8dcSSimon Schubert      is a temporary variable, so don't install it.  */
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert   if ((var != NULL) && (objname != NULL))
6825796c8dcSSimon Schubert     {
6835796c8dcSSimon Schubert       var->obj_name = xstrdup (objname);
6845796c8dcSSimon Schubert 
6855796c8dcSSimon Schubert       /* If a varobj name is duplicated, the install will fail so
686c50c785cSJohn Marino          we must cleanup.  */
6875796c8dcSSimon Schubert       if (!install_variable (var))
6885796c8dcSSimon Schubert 	{
6895796c8dcSSimon Schubert 	  do_cleanups (old_chain);
6905796c8dcSSimon Schubert 	  return NULL;
6915796c8dcSSimon Schubert 	}
6925796c8dcSSimon Schubert     }
6935796c8dcSSimon Schubert 
6945796c8dcSSimon Schubert   discard_cleanups (old_chain);
6955796c8dcSSimon Schubert   return var;
6965796c8dcSSimon Schubert }
6975796c8dcSSimon Schubert 
698c50c785cSJohn Marino /* Generates an unique name that can be used for a varobj.  */
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert char *
7015796c8dcSSimon Schubert varobj_gen_name (void)
7025796c8dcSSimon Schubert {
7035796c8dcSSimon Schubert   static int id = 0;
7045796c8dcSSimon Schubert   char *obj_name;
7055796c8dcSSimon Schubert 
706c50c785cSJohn Marino   /* Generate a name for this object.  */
7075796c8dcSSimon Schubert   id++;
7085796c8dcSSimon Schubert   obj_name = xstrprintf ("var%d", id);
7095796c8dcSSimon Schubert 
7105796c8dcSSimon Schubert   return obj_name;
7115796c8dcSSimon Schubert }
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert /* Given an OBJNAME, returns the pointer to the corresponding varobj.  Call
7145796c8dcSSimon Schubert    error if OBJNAME cannot be found.  */
7155796c8dcSSimon Schubert 
7165796c8dcSSimon Schubert struct varobj *
7175796c8dcSSimon Schubert varobj_get_handle (char *objname)
7185796c8dcSSimon Schubert {
7195796c8dcSSimon Schubert   struct vlist *cv;
7205796c8dcSSimon Schubert   const char *chp;
7215796c8dcSSimon Schubert   unsigned int index = 0;
7225796c8dcSSimon Schubert   unsigned int i = 1;
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert   for (chp = objname; *chp; chp++)
7255796c8dcSSimon Schubert     {
7265796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
7275796c8dcSSimon Schubert     }
7285796c8dcSSimon Schubert 
7295796c8dcSSimon Schubert   cv = *(varobj_table + index);
7305796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
7315796c8dcSSimon Schubert     cv = cv->next;
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert   if (cv == NULL)
7345796c8dcSSimon Schubert     error (_("Variable object not found"));
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert   return cv->var;
7375796c8dcSSimon Schubert }
7385796c8dcSSimon Schubert 
739c50c785cSJohn Marino /* Given the handle, return the name of the object.  */
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert char *
7425796c8dcSSimon Schubert varobj_get_objname (struct varobj *var)
7435796c8dcSSimon Schubert {
7445796c8dcSSimon Schubert   return var->obj_name;
7455796c8dcSSimon Schubert }
7465796c8dcSSimon Schubert 
747c50c785cSJohn Marino /* Given the handle, return the expression represented by the object.  */
7485796c8dcSSimon Schubert 
7495796c8dcSSimon Schubert char *
7505796c8dcSSimon Schubert varobj_get_expression (struct varobj *var)
7515796c8dcSSimon Schubert {
7525796c8dcSSimon Schubert   return name_of_variable (var);
7535796c8dcSSimon Schubert }
7545796c8dcSSimon Schubert 
7555796c8dcSSimon Schubert /* Deletes a varobj and all its children if only_children == 0,
756c50c785cSJohn Marino    otherwise deletes only the children; returns a malloc'ed list of
757c50c785cSJohn Marino    all the (malloc'ed) names of the variables that have been deleted
758c50c785cSJohn Marino    (NULL terminated).  */
7595796c8dcSSimon Schubert 
7605796c8dcSSimon Schubert int
7615796c8dcSSimon Schubert varobj_delete (struct varobj *var, char ***dellist, int only_children)
7625796c8dcSSimon Schubert {
7635796c8dcSSimon Schubert   int delcount;
7645796c8dcSSimon Schubert   int mycount;
7655796c8dcSSimon Schubert   struct cpstack *result = NULL;
7665796c8dcSSimon Schubert   char **cp;
7675796c8dcSSimon Schubert 
768c50c785cSJohn Marino   /* Initialize a stack for temporary results.  */
7695796c8dcSSimon Schubert   cppush (&result, NULL);
7705796c8dcSSimon Schubert 
7715796c8dcSSimon Schubert   if (only_children)
772c50c785cSJohn Marino     /* Delete only the variable children.  */
7735796c8dcSSimon Schubert     delcount = delete_variable (&result, var, 1 /* only the children */ );
7745796c8dcSSimon Schubert   else
775c50c785cSJohn Marino     /* Delete the variable and all its children.  */
7765796c8dcSSimon Schubert     delcount = delete_variable (&result, var, 0 /* parent+children */ );
7775796c8dcSSimon Schubert 
778c50c785cSJohn Marino   /* We may have been asked to return a list of what has been deleted.  */
7795796c8dcSSimon Schubert   if (dellist != NULL)
7805796c8dcSSimon Schubert     {
7815796c8dcSSimon Schubert       *dellist = xmalloc ((delcount + 1) * sizeof (char *));
7825796c8dcSSimon Schubert 
7835796c8dcSSimon Schubert       cp = *dellist;
7845796c8dcSSimon Schubert       mycount = delcount;
7855796c8dcSSimon Schubert       *cp = cppop (&result);
7865796c8dcSSimon Schubert       while ((*cp != NULL) && (mycount > 0))
7875796c8dcSSimon Schubert 	{
7885796c8dcSSimon Schubert 	  mycount--;
7895796c8dcSSimon Schubert 	  cp++;
7905796c8dcSSimon Schubert 	  *cp = cppop (&result);
7915796c8dcSSimon Schubert 	}
7925796c8dcSSimon Schubert 
7935796c8dcSSimon Schubert       if (mycount || (*cp != NULL))
7945796c8dcSSimon Schubert 	warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
7955796c8dcSSimon Schubert 		 mycount);
7965796c8dcSSimon Schubert     }
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert   return delcount;
7995796c8dcSSimon Schubert }
8005796c8dcSSimon Schubert 
801cf7f2e2dSJohn Marino #if HAVE_PYTHON
802cf7f2e2dSJohn Marino 
8035796c8dcSSimon Schubert /* Convenience function for varobj_set_visualizer.  Instantiate a
8045796c8dcSSimon Schubert    pretty-printer for a given value.  */
8055796c8dcSSimon Schubert static PyObject *
8065796c8dcSSimon Schubert instantiate_pretty_printer (PyObject *constructor, struct value *value)
8075796c8dcSSimon Schubert {
8085796c8dcSSimon Schubert   PyObject *val_obj = NULL;
8095796c8dcSSimon Schubert   PyObject *printer;
8105796c8dcSSimon Schubert 
8115796c8dcSSimon Schubert   val_obj = value_to_value_object (value);
8125796c8dcSSimon Schubert   if (! val_obj)
8135796c8dcSSimon Schubert     return NULL;
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert   printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
8165796c8dcSSimon Schubert   Py_DECREF (val_obj);
8175796c8dcSSimon Schubert   return printer;
8185796c8dcSSimon Schubert }
8195796c8dcSSimon Schubert 
820cf7f2e2dSJohn Marino #endif
821cf7f2e2dSJohn Marino 
822c50c785cSJohn Marino /* Set/Get variable object display format.  */
8235796c8dcSSimon Schubert 
8245796c8dcSSimon Schubert enum varobj_display_formats
8255796c8dcSSimon Schubert varobj_set_display_format (struct varobj *var,
8265796c8dcSSimon Schubert 			   enum varobj_display_formats format)
8275796c8dcSSimon Schubert {
8285796c8dcSSimon Schubert   switch (format)
8295796c8dcSSimon Schubert     {
8305796c8dcSSimon Schubert     case FORMAT_NATURAL:
8315796c8dcSSimon Schubert     case FORMAT_BINARY:
8325796c8dcSSimon Schubert     case FORMAT_DECIMAL:
8335796c8dcSSimon Schubert     case FORMAT_HEXADECIMAL:
8345796c8dcSSimon Schubert     case FORMAT_OCTAL:
8355796c8dcSSimon Schubert       var->format = format;
8365796c8dcSSimon Schubert       break;
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert     default:
8395796c8dcSSimon Schubert       var->format = variable_default_display (var);
8405796c8dcSSimon Schubert     }
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert   if (varobj_value_is_changeable_p (var)
8435796c8dcSSimon Schubert       && var->value && !value_lazy (var->value))
8445796c8dcSSimon Schubert     {
8455796c8dcSSimon Schubert       xfree (var->print_value);
8465796c8dcSSimon Schubert       var->print_value = value_get_print_value (var->value, var->format, var);
8475796c8dcSSimon Schubert     }
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert   return var->format;
8505796c8dcSSimon Schubert }
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert enum varobj_display_formats
8535796c8dcSSimon Schubert varobj_get_display_format (struct varobj *var)
8545796c8dcSSimon Schubert {
8555796c8dcSSimon Schubert   return var->format;
8565796c8dcSSimon Schubert }
8575796c8dcSSimon Schubert 
8585796c8dcSSimon Schubert char *
8595796c8dcSSimon Schubert varobj_get_display_hint (struct varobj *var)
8605796c8dcSSimon Schubert {
8615796c8dcSSimon Schubert   char *result = NULL;
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert #if HAVE_PYTHON
8645796c8dcSSimon Schubert   struct cleanup *back_to = varobj_ensure_python_env (var);
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert   if (var->pretty_printer)
8675796c8dcSSimon Schubert     result = gdbpy_get_display_hint (var->pretty_printer);
8685796c8dcSSimon Schubert 
8695796c8dcSSimon Schubert   do_cleanups (back_to);
8705796c8dcSSimon Schubert #endif
8715796c8dcSSimon Schubert 
8725796c8dcSSimon Schubert   return result;
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert /* Return true if the varobj has items after TO, false otherwise.  */
8765796c8dcSSimon Schubert 
8775796c8dcSSimon Schubert int
8785796c8dcSSimon Schubert varobj_has_more (struct varobj *var, int to)
8795796c8dcSSimon Schubert {
8805796c8dcSSimon Schubert   if (VEC_length (varobj_p, var->children) > to)
8815796c8dcSSimon Schubert     return 1;
8825796c8dcSSimon Schubert   return ((to == -1 || VEC_length (varobj_p, var->children) == to)
8835796c8dcSSimon Schubert 	  && var->saved_item != NULL);
8845796c8dcSSimon Schubert }
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert /* If the variable object is bound to a specific thread, that
8875796c8dcSSimon Schubert    is its evaluation can always be done in context of a frame
8885796c8dcSSimon Schubert    inside that thread, returns GDB id of the thread -- which
8895796c8dcSSimon Schubert    is always positive.  Otherwise, returns -1.  */
8905796c8dcSSimon Schubert int
8915796c8dcSSimon Schubert varobj_get_thread_id (struct varobj *var)
8925796c8dcSSimon Schubert {
8935796c8dcSSimon Schubert   if (var->root->valid_block && var->root->thread_id > 0)
8945796c8dcSSimon Schubert     return var->root->thread_id;
8955796c8dcSSimon Schubert   else
8965796c8dcSSimon Schubert     return -1;
8975796c8dcSSimon Schubert }
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert void
9005796c8dcSSimon Schubert varobj_set_frozen (struct varobj *var, int frozen)
9015796c8dcSSimon Schubert {
9025796c8dcSSimon Schubert   /* When a variable is unfrozen, we don't fetch its value.
9035796c8dcSSimon Schubert      The 'not_fetched' flag remains set, so next -var-update
9045796c8dcSSimon Schubert      won't complain.
9055796c8dcSSimon Schubert 
9065796c8dcSSimon Schubert      We don't fetch the value, because for structures the client
9075796c8dcSSimon Schubert      should do -var-update anyway.  It would be bad to have different
9085796c8dcSSimon Schubert      client-size logic for structure and other types.  */
9095796c8dcSSimon Schubert   var->frozen = frozen;
9105796c8dcSSimon Schubert }
9115796c8dcSSimon Schubert 
9125796c8dcSSimon Schubert int
9135796c8dcSSimon Schubert varobj_get_frozen (struct varobj *var)
9145796c8dcSSimon Schubert {
9155796c8dcSSimon Schubert   return var->frozen;
9165796c8dcSSimon Schubert }
9175796c8dcSSimon Schubert 
9185796c8dcSSimon Schubert /* A helper function that restricts a range to what is actually
9195796c8dcSSimon Schubert    available in a VEC.  This follows the usual rules for the meaning
9205796c8dcSSimon Schubert    of FROM and TO -- if either is negative, the entire range is
9215796c8dcSSimon Schubert    used.  */
9225796c8dcSSimon Schubert 
9235796c8dcSSimon Schubert static void
9245796c8dcSSimon Schubert restrict_range (VEC (varobj_p) *children, int *from, int *to)
9255796c8dcSSimon Schubert {
9265796c8dcSSimon Schubert   if (*from < 0 || *to < 0)
9275796c8dcSSimon Schubert     {
9285796c8dcSSimon Schubert       *from = 0;
9295796c8dcSSimon Schubert       *to = VEC_length (varobj_p, children);
9305796c8dcSSimon Schubert     }
9315796c8dcSSimon Schubert   else
9325796c8dcSSimon Schubert     {
9335796c8dcSSimon Schubert       if (*from > VEC_length (varobj_p, children))
9345796c8dcSSimon Schubert 	*from = VEC_length (varobj_p, children);
9355796c8dcSSimon Schubert       if (*to > VEC_length (varobj_p, children))
9365796c8dcSSimon Schubert 	*to = VEC_length (varobj_p, children);
9375796c8dcSSimon Schubert       if (*from > *to)
9385796c8dcSSimon Schubert 	*from = *to;
9395796c8dcSSimon Schubert     }
9405796c8dcSSimon Schubert }
9415796c8dcSSimon Schubert 
942cf7f2e2dSJohn Marino #if HAVE_PYTHON
943cf7f2e2dSJohn Marino 
9445796c8dcSSimon Schubert /* A helper for update_dynamic_varobj_children that installs a new
9455796c8dcSSimon Schubert    child when needed.  */
9465796c8dcSSimon Schubert 
9475796c8dcSSimon Schubert static void
9485796c8dcSSimon Schubert install_dynamic_child (struct varobj *var,
9495796c8dcSSimon Schubert 		       VEC (varobj_p) **changed,
9505796c8dcSSimon Schubert 		       VEC (varobj_p) **new,
9515796c8dcSSimon Schubert 		       VEC (varobj_p) **unchanged,
9525796c8dcSSimon Schubert 		       int *cchanged,
9535796c8dcSSimon Schubert 		       int index,
9545796c8dcSSimon Schubert 		       const char *name,
9555796c8dcSSimon Schubert 		       struct value *value)
9565796c8dcSSimon Schubert {
9575796c8dcSSimon Schubert   if (VEC_length (varobj_p, var->children) < index + 1)
9585796c8dcSSimon Schubert     {
9595796c8dcSSimon Schubert       /* There's no child yet.  */
9605796c8dcSSimon Schubert       struct varobj *child = varobj_add_child (var, name, value);
961cf7f2e2dSJohn Marino 
9625796c8dcSSimon Schubert       if (new)
9635796c8dcSSimon Schubert 	{
9645796c8dcSSimon Schubert 	  VEC_safe_push (varobj_p, *new, child);
9655796c8dcSSimon Schubert 	  *cchanged = 1;
9665796c8dcSSimon Schubert 	}
9675796c8dcSSimon Schubert     }
9685796c8dcSSimon Schubert   else
9695796c8dcSSimon Schubert     {
9705796c8dcSSimon Schubert       varobj_p existing = VEC_index (varobj_p, var->children, index);
971cf7f2e2dSJohn Marino 
9725796c8dcSSimon Schubert       if (install_new_value (existing, value, 0))
9735796c8dcSSimon Schubert 	{
9745796c8dcSSimon Schubert 	  if (changed)
9755796c8dcSSimon Schubert 	    VEC_safe_push (varobj_p, *changed, existing);
9765796c8dcSSimon Schubert 	}
9775796c8dcSSimon Schubert       else if (unchanged)
9785796c8dcSSimon Schubert 	VEC_safe_push (varobj_p, *unchanged, existing);
9795796c8dcSSimon Schubert     }
9805796c8dcSSimon Schubert }
9815796c8dcSSimon Schubert 
9825796c8dcSSimon Schubert static int
9835796c8dcSSimon Schubert dynamic_varobj_has_child_method (struct varobj *var)
9845796c8dcSSimon Schubert {
9855796c8dcSSimon Schubert   struct cleanup *back_to;
9865796c8dcSSimon Schubert   PyObject *printer = var->pretty_printer;
9875796c8dcSSimon Schubert   int result;
9885796c8dcSSimon Schubert 
9895796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
9905796c8dcSSimon Schubert   result = PyObject_HasAttr (printer, gdbpy_children_cst);
9915796c8dcSSimon Schubert   do_cleanups (back_to);
9925796c8dcSSimon Schubert   return result;
9935796c8dcSSimon Schubert }
9945796c8dcSSimon Schubert 
9955796c8dcSSimon Schubert #endif
9965796c8dcSSimon Schubert 
9975796c8dcSSimon Schubert static int
9985796c8dcSSimon Schubert update_dynamic_varobj_children (struct varobj *var,
9995796c8dcSSimon Schubert 				VEC (varobj_p) **changed,
10005796c8dcSSimon Schubert 				VEC (varobj_p) **new,
10015796c8dcSSimon Schubert 				VEC (varobj_p) **unchanged,
10025796c8dcSSimon Schubert 				int *cchanged,
10035796c8dcSSimon Schubert 				int update_children,
10045796c8dcSSimon Schubert 				int from,
10055796c8dcSSimon Schubert 				int to)
10065796c8dcSSimon Schubert {
10075796c8dcSSimon Schubert #if HAVE_PYTHON
10085796c8dcSSimon Schubert   struct cleanup *back_to;
10095796c8dcSSimon Schubert   PyObject *children;
10105796c8dcSSimon Schubert   int i;
10115796c8dcSSimon Schubert   PyObject *printer = var->pretty_printer;
10125796c8dcSSimon Schubert 
10135796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
10145796c8dcSSimon Schubert 
10155796c8dcSSimon Schubert   *cchanged = 0;
10165796c8dcSSimon Schubert   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
10175796c8dcSSimon Schubert     {
10185796c8dcSSimon Schubert       do_cleanups (back_to);
10195796c8dcSSimon Schubert       return 0;
10205796c8dcSSimon Schubert     }
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert   if (update_children || !var->child_iter)
10235796c8dcSSimon Schubert     {
10245796c8dcSSimon Schubert       children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
10255796c8dcSSimon Schubert 					     NULL);
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert       if (!children)
10285796c8dcSSimon Schubert 	{
10295796c8dcSSimon Schubert 	  gdbpy_print_stack ();
10305796c8dcSSimon Schubert 	  error (_("Null value returned for children"));
10315796c8dcSSimon Schubert 	}
10325796c8dcSSimon Schubert 
10335796c8dcSSimon Schubert       make_cleanup_py_decref (children);
10345796c8dcSSimon Schubert 
10355796c8dcSSimon Schubert       if (!PyIter_Check (children))
10365796c8dcSSimon Schubert 	error (_("Returned value is not iterable"));
10375796c8dcSSimon Schubert 
10385796c8dcSSimon Schubert       Py_XDECREF (var->child_iter);
10395796c8dcSSimon Schubert       var->child_iter = PyObject_GetIter (children);
10405796c8dcSSimon Schubert       if (!var->child_iter)
10415796c8dcSSimon Schubert 	{
10425796c8dcSSimon Schubert 	  gdbpy_print_stack ();
10435796c8dcSSimon Schubert 	  error (_("Could not get children iterator"));
10445796c8dcSSimon Schubert 	}
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert       Py_XDECREF (var->saved_item);
10475796c8dcSSimon Schubert       var->saved_item = NULL;
10485796c8dcSSimon Schubert 
10495796c8dcSSimon Schubert       i = 0;
10505796c8dcSSimon Schubert     }
10515796c8dcSSimon Schubert   else
10525796c8dcSSimon Schubert     i = VEC_length (varobj_p, var->children);
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert   /* We ask for one extra child, so that MI can report whether there
10555796c8dcSSimon Schubert      are more children.  */
10565796c8dcSSimon Schubert   for (; to < 0 || i < to + 1; ++i)
10575796c8dcSSimon Schubert     {
10585796c8dcSSimon Schubert       PyObject *item;
1059c50c785cSJohn Marino       int force_done = 0;
10605796c8dcSSimon Schubert 
10615796c8dcSSimon Schubert       /* See if there was a leftover from last time.  */
10625796c8dcSSimon Schubert       if (var->saved_item)
10635796c8dcSSimon Schubert 	{
10645796c8dcSSimon Schubert 	  item = var->saved_item;
10655796c8dcSSimon Schubert 	  var->saved_item = NULL;
10665796c8dcSSimon Schubert 	}
10675796c8dcSSimon Schubert       else
10685796c8dcSSimon Schubert 	item = PyIter_Next (var->child_iter);
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert       if (!item)
1071c50c785cSJohn Marino 	{
1072c50c785cSJohn Marino 	  /* Normal end of iteration.  */
1073c50c785cSJohn Marino 	  if (!PyErr_Occurred ())
10745796c8dcSSimon Schubert 	    break;
10755796c8dcSSimon Schubert 
1076c50c785cSJohn Marino 	  /* If we got a memory error, just use the text as the
1077c50c785cSJohn Marino 	     item.  */
1078c50c785cSJohn Marino 	  if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1079c50c785cSJohn Marino 	    {
1080c50c785cSJohn Marino 	      PyObject *type, *value, *trace;
1081c50c785cSJohn Marino 	      char *name_str, *value_str;
1082c50c785cSJohn Marino 
1083c50c785cSJohn Marino 	      PyErr_Fetch (&type, &value, &trace);
1084c50c785cSJohn Marino 	      value_str = gdbpy_exception_to_string (type, value);
1085c50c785cSJohn Marino 	      Py_XDECREF (type);
1086c50c785cSJohn Marino 	      Py_XDECREF (value);
1087c50c785cSJohn Marino 	      Py_XDECREF (trace);
1088c50c785cSJohn Marino 	      if (!value_str)
1089c50c785cSJohn Marino 		{
1090c50c785cSJohn Marino 		  gdbpy_print_stack ();
1091c50c785cSJohn Marino 		  break;
1092c50c785cSJohn Marino 		}
1093c50c785cSJohn Marino 
1094c50c785cSJohn Marino 	      name_str = xstrprintf ("<error at %d>", i);
1095c50c785cSJohn Marino 	      item = Py_BuildValue ("(ss)", name_str, value_str);
1096c50c785cSJohn Marino 	      xfree (name_str);
1097c50c785cSJohn Marino 	      xfree (value_str);
1098c50c785cSJohn Marino 	      if (!item)
1099c50c785cSJohn Marino 		{
1100c50c785cSJohn Marino 		  gdbpy_print_stack ();
1101c50c785cSJohn Marino 		  break;
1102c50c785cSJohn Marino 		}
1103c50c785cSJohn Marino 
1104c50c785cSJohn Marino 	      force_done = 1;
1105c50c785cSJohn Marino 	    }
1106c50c785cSJohn Marino 	  else
1107c50c785cSJohn Marino 	    {
1108c50c785cSJohn Marino 	      /* Any other kind of error.  */
1109c50c785cSJohn Marino 	      gdbpy_print_stack ();
1110c50c785cSJohn Marino 	      break;
1111c50c785cSJohn Marino 	    }
1112c50c785cSJohn Marino 	}
1113c50c785cSJohn Marino 
11145796c8dcSSimon Schubert       /* We don't want to push the extra child on any report list.  */
11155796c8dcSSimon Schubert       if (to < 0 || i < to)
11165796c8dcSSimon Schubert 	{
11175796c8dcSSimon Schubert 	  PyObject *py_v;
1118*a45ae5f8SJohn Marino 	  const char *name;
11195796c8dcSSimon Schubert 	  struct value *v;
11205796c8dcSSimon Schubert 	  struct cleanup *inner;
11215796c8dcSSimon Schubert 	  int can_mention = from < 0 || i >= from;
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert 	  inner = make_cleanup_py_decref (item);
11245796c8dcSSimon Schubert 
11255796c8dcSSimon Schubert 	  if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1126c50c785cSJohn Marino 	    {
1127c50c785cSJohn Marino 	      gdbpy_print_stack ();
11285796c8dcSSimon Schubert 	      error (_("Invalid item from the child list"));
1129c50c785cSJohn Marino 	    }
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert 	  v = convert_value_from_python (py_v);
1132c50c785cSJohn Marino 	  if (v == NULL)
1133c50c785cSJohn Marino 	    gdbpy_print_stack ();
11345796c8dcSSimon Schubert 	  install_dynamic_child (var, can_mention ? changed : NULL,
11355796c8dcSSimon Schubert 				 can_mention ? new : NULL,
11365796c8dcSSimon Schubert 				 can_mention ? unchanged : NULL,
11375796c8dcSSimon Schubert 				 can_mention ? cchanged : NULL, i, name, v);
11385796c8dcSSimon Schubert 	  do_cleanups (inner);
11395796c8dcSSimon Schubert 	}
11405796c8dcSSimon Schubert       else
11415796c8dcSSimon Schubert 	{
11425796c8dcSSimon Schubert 	  Py_XDECREF (var->saved_item);
11435796c8dcSSimon Schubert 	  var->saved_item = item;
11445796c8dcSSimon Schubert 
11455796c8dcSSimon Schubert 	  /* We want to truncate the child list just before this
11465796c8dcSSimon Schubert 	     element.  */
11475796c8dcSSimon Schubert 	  break;
11485796c8dcSSimon Schubert 	}
1149c50c785cSJohn Marino 
1150c50c785cSJohn Marino       if (force_done)
1151c50c785cSJohn Marino 	break;
11525796c8dcSSimon Schubert     }
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert   if (i < VEC_length (varobj_p, var->children))
11555796c8dcSSimon Schubert     {
11565796c8dcSSimon Schubert       int j;
1157cf7f2e2dSJohn Marino 
11585796c8dcSSimon Schubert       *cchanged = 1;
11595796c8dcSSimon Schubert       for (j = i; j < VEC_length (varobj_p, var->children); ++j)
11605796c8dcSSimon Schubert 	varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
11615796c8dcSSimon Schubert       VEC_truncate (varobj_p, var->children, i);
11625796c8dcSSimon Schubert     }
11635796c8dcSSimon Schubert 
11645796c8dcSSimon Schubert   /* If there are fewer children than requested, note that the list of
11655796c8dcSSimon Schubert      children changed.  */
11665796c8dcSSimon Schubert   if (to >= 0 && VEC_length (varobj_p, var->children) < to)
11675796c8dcSSimon Schubert     *cchanged = 1;
11685796c8dcSSimon Schubert 
11695796c8dcSSimon Schubert   var->num_children = VEC_length (varobj_p, var->children);
11705796c8dcSSimon Schubert 
11715796c8dcSSimon Schubert   do_cleanups (back_to);
11725796c8dcSSimon Schubert 
11735796c8dcSSimon Schubert   return 1;
11745796c8dcSSimon Schubert #else
11755796c8dcSSimon Schubert   gdb_assert (0 && "should never be called if Python is not enabled");
11765796c8dcSSimon Schubert #endif
11775796c8dcSSimon Schubert }
11785796c8dcSSimon Schubert 
11795796c8dcSSimon Schubert int
11805796c8dcSSimon Schubert varobj_get_num_children (struct varobj *var)
11815796c8dcSSimon Schubert {
11825796c8dcSSimon Schubert   if (var->num_children == -1)
11835796c8dcSSimon Schubert     {
11845796c8dcSSimon Schubert       if (var->pretty_printer)
11855796c8dcSSimon Schubert 	{
11865796c8dcSSimon Schubert 	  int dummy;
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert 	  /* If we have a dynamic varobj, don't report -1 children.
11895796c8dcSSimon Schubert 	     So, try to fetch some children first.  */
11905796c8dcSSimon Schubert 	  update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
11915796c8dcSSimon Schubert 					  0, 0, 0);
11925796c8dcSSimon Schubert 	}
11935796c8dcSSimon Schubert       else
11945796c8dcSSimon Schubert 	var->num_children = number_of_children (var);
11955796c8dcSSimon Schubert     }
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert   return var->num_children >= 0 ? var->num_children : 0;
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert /* Creates a list of the immediate children of a variable object;
1201c50c785cSJohn Marino    the return code is the number of such children or -1 on error.  */
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert VEC (varobj_p)*
12045796c8dcSSimon Schubert varobj_list_children (struct varobj *var, int *from, int *to)
12055796c8dcSSimon Schubert {
12065796c8dcSSimon Schubert   char *name;
12075796c8dcSSimon Schubert   int i, children_changed;
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert   var->children_requested = 1;
12105796c8dcSSimon Schubert 
12115796c8dcSSimon Schubert   if (var->pretty_printer)
12125796c8dcSSimon Schubert     {
12135796c8dcSSimon Schubert       /* This, in theory, can result in the number of children changing without
12145796c8dcSSimon Schubert 	 frontend noticing.  But well, calling -var-list-children on the same
12155796c8dcSSimon Schubert 	 varobj twice is not something a sane frontend would do.  */
12165796c8dcSSimon Schubert       update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
12175796c8dcSSimon Schubert 				      0, 0, *to);
12185796c8dcSSimon Schubert       restrict_range (var->children, from, to);
12195796c8dcSSimon Schubert       return var->children;
12205796c8dcSSimon Schubert     }
12215796c8dcSSimon Schubert 
12225796c8dcSSimon Schubert   if (var->num_children == -1)
12235796c8dcSSimon Schubert     var->num_children = number_of_children (var);
12245796c8dcSSimon Schubert 
12255796c8dcSSimon Schubert   /* If that failed, give up.  */
12265796c8dcSSimon Schubert   if (var->num_children == -1)
12275796c8dcSSimon Schubert     return var->children;
12285796c8dcSSimon Schubert 
12295796c8dcSSimon Schubert   /* If we're called when the list of children is not yet initialized,
12305796c8dcSSimon Schubert      allocate enough elements in it.  */
12315796c8dcSSimon Schubert   while (VEC_length (varobj_p, var->children) < var->num_children)
12325796c8dcSSimon Schubert     VEC_safe_push (varobj_p, var->children, NULL);
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert   for (i = 0; i < var->num_children; i++)
12355796c8dcSSimon Schubert     {
12365796c8dcSSimon Schubert       varobj_p existing = VEC_index (varobj_p, var->children, i);
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert       if (existing == NULL)
12395796c8dcSSimon Schubert 	{
12405796c8dcSSimon Schubert 	  /* Either it's the first call to varobj_list_children for
12415796c8dcSSimon Schubert 	     this variable object, and the child was never created,
12425796c8dcSSimon Schubert 	     or it was explicitly deleted by the client.  */
12435796c8dcSSimon Schubert 	  name = name_of_child (var, i);
12445796c8dcSSimon Schubert 	  existing = create_child (var, i, name);
12455796c8dcSSimon Schubert 	  VEC_replace (varobj_p, var->children, i, existing);
12465796c8dcSSimon Schubert 	}
12475796c8dcSSimon Schubert     }
12485796c8dcSSimon Schubert 
12495796c8dcSSimon Schubert   restrict_range (var->children, from, to);
12505796c8dcSSimon Schubert   return var->children;
12515796c8dcSSimon Schubert }
12525796c8dcSSimon Schubert 
1253cf7f2e2dSJohn Marino #if HAVE_PYTHON
1254cf7f2e2dSJohn Marino 
12555796c8dcSSimon Schubert static struct varobj *
12565796c8dcSSimon Schubert varobj_add_child (struct varobj *var, const char *name, struct value *value)
12575796c8dcSSimon Schubert {
12585796c8dcSSimon Schubert   varobj_p v = create_child_with_value (var,
12595796c8dcSSimon Schubert 					VEC_length (varobj_p, var->children),
12605796c8dcSSimon Schubert 					name, value);
1261cf7f2e2dSJohn Marino 
12625796c8dcSSimon Schubert   VEC_safe_push (varobj_p, var->children, v);
12635796c8dcSSimon Schubert   return v;
12645796c8dcSSimon Schubert }
12655796c8dcSSimon Schubert 
1266cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
1267cf7f2e2dSJohn Marino 
12685796c8dcSSimon Schubert /* Obtain the type of an object Variable as a string similar to the one gdb
1269c50c785cSJohn Marino    prints on the console.  */
12705796c8dcSSimon Schubert 
12715796c8dcSSimon Schubert char *
12725796c8dcSSimon Schubert varobj_get_type (struct varobj *var)
12735796c8dcSSimon Schubert {
12745796c8dcSSimon Schubert   /* For the "fake" variables, do not return a type.  (It's type is
12755796c8dcSSimon Schubert      NULL, too.)
12765796c8dcSSimon Schubert      Do not return a type for invalid variables as well.  */
12775796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
12785796c8dcSSimon Schubert     return NULL;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   return type_to_string (var->type);
12815796c8dcSSimon Schubert }
12825796c8dcSSimon Schubert 
12835796c8dcSSimon Schubert /* Obtain the type of an object variable.  */
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert struct type *
12865796c8dcSSimon Schubert varobj_get_gdb_type (struct varobj *var)
12875796c8dcSSimon Schubert {
12885796c8dcSSimon Schubert   return var->type;
12895796c8dcSSimon Schubert }
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert /* Return a pointer to the full rooted expression of varobj VAR.
12925796c8dcSSimon Schubert    If it has not been computed yet, compute it.  */
12935796c8dcSSimon Schubert char *
12945796c8dcSSimon Schubert varobj_get_path_expr (struct varobj *var)
12955796c8dcSSimon Schubert {
12965796c8dcSSimon Schubert   if (var->path_expr != NULL)
12975796c8dcSSimon Schubert     return var->path_expr;
12985796c8dcSSimon Schubert   else
12995796c8dcSSimon Schubert     {
13005796c8dcSSimon Schubert       /* For root varobjs, we initialize path_expr
13015796c8dcSSimon Schubert 	 when creating varobj, so here it should be
13025796c8dcSSimon Schubert 	 child varobj.  */
13035796c8dcSSimon Schubert       gdb_assert (!is_root_p (var));
13045796c8dcSSimon Schubert       return (*var->root->lang->path_expr_of_child) (var);
13055796c8dcSSimon Schubert     }
13065796c8dcSSimon Schubert }
13075796c8dcSSimon Schubert 
13085796c8dcSSimon Schubert enum varobj_languages
13095796c8dcSSimon Schubert varobj_get_language (struct varobj *var)
13105796c8dcSSimon Schubert {
13115796c8dcSSimon Schubert   return variable_language (var);
13125796c8dcSSimon Schubert }
13135796c8dcSSimon Schubert 
13145796c8dcSSimon Schubert int
13155796c8dcSSimon Schubert varobj_get_attributes (struct varobj *var)
13165796c8dcSSimon Schubert {
13175796c8dcSSimon Schubert   int attributes = 0;
13185796c8dcSSimon Schubert 
13195796c8dcSSimon Schubert   if (varobj_editable_p (var))
1320c50c785cSJohn Marino     /* FIXME: define masks for attributes.  */
13215796c8dcSSimon Schubert     attributes |= 0x00000001;	/* Editable */
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert   return attributes;
13245796c8dcSSimon Schubert }
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert int
13275796c8dcSSimon Schubert varobj_pretty_printed_p (struct varobj *var)
13285796c8dcSSimon Schubert {
13295796c8dcSSimon Schubert   return var->pretty_printer != NULL;
13305796c8dcSSimon Schubert }
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert char *
13335796c8dcSSimon Schubert varobj_get_formatted_value (struct varobj *var,
13345796c8dcSSimon Schubert 			    enum varobj_display_formats format)
13355796c8dcSSimon Schubert {
13365796c8dcSSimon Schubert   return my_value_of_variable (var, format);
13375796c8dcSSimon Schubert }
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert char *
13405796c8dcSSimon Schubert varobj_get_value (struct varobj *var)
13415796c8dcSSimon Schubert {
13425796c8dcSSimon Schubert   return my_value_of_variable (var, var->format);
13435796c8dcSSimon Schubert }
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert /* Set the value of an object variable (if it is editable) to the
1346c50c785cSJohn Marino    value of the given expression.  */
1347c50c785cSJohn Marino /* Note: Invokes functions that can call error().  */
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert int
13505796c8dcSSimon Schubert varobj_set_value (struct varobj *var, char *expression)
13515796c8dcSSimon Schubert {
13525796c8dcSSimon Schubert   struct value *val;
13535796c8dcSSimon Schubert 
13545796c8dcSSimon Schubert   /* The argument "expression" contains the variable's new value.
13555796c8dcSSimon Schubert      We need to first construct a legal expression for this -- ugh!  */
13565796c8dcSSimon Schubert   /* Does this cover all the bases?  */
13575796c8dcSSimon Schubert   struct expression *exp;
13585796c8dcSSimon Schubert   struct value *value;
13595796c8dcSSimon Schubert   int saved_input_radix = input_radix;
13605796c8dcSSimon Schubert   char *s = expression;
13615796c8dcSSimon Schubert 
13625796c8dcSSimon Schubert   gdb_assert (varobj_editable_p (var));
13635796c8dcSSimon Schubert 
1364c50c785cSJohn Marino   input_radix = 10;		/* ALWAYS reset to decimal temporarily.  */
13655796c8dcSSimon Schubert   exp = parse_exp_1 (&s, 0, 0);
13665796c8dcSSimon Schubert   if (!gdb_evaluate_expression (exp, &value))
13675796c8dcSSimon Schubert     {
13685796c8dcSSimon Schubert       /* We cannot proceed without a valid expression.  */
13695796c8dcSSimon Schubert       xfree (exp);
13705796c8dcSSimon Schubert       return 0;
13715796c8dcSSimon Schubert     }
13725796c8dcSSimon Schubert 
13735796c8dcSSimon Schubert   /* All types that are editable must also be changeable.  */
13745796c8dcSSimon Schubert   gdb_assert (varobj_value_is_changeable_p (var));
13755796c8dcSSimon Schubert 
13765796c8dcSSimon Schubert   /* The value of a changeable variable object must not be lazy.  */
13775796c8dcSSimon Schubert   gdb_assert (!value_lazy (var->value));
13785796c8dcSSimon Schubert 
13795796c8dcSSimon Schubert   /* Need to coerce the input.  We want to check if the
13805796c8dcSSimon Schubert      value of the variable object will be different
13815796c8dcSSimon Schubert      after assignment, and the first thing value_assign
13825796c8dcSSimon Schubert      does is coerce the input.
13835796c8dcSSimon Schubert      For example, if we are assigning an array to a pointer variable we
1384c50c785cSJohn Marino      should compare the pointer with the array's address, not with the
13855796c8dcSSimon Schubert      array's content.  */
13865796c8dcSSimon Schubert   value = coerce_array (value);
13875796c8dcSSimon Schubert 
13885796c8dcSSimon Schubert   /* The new value may be lazy.  gdb_value_assign, or
13895796c8dcSSimon Schubert      rather value_contents, will take care of this.
13905796c8dcSSimon Schubert      If fetching of the new value will fail, gdb_value_assign
13915796c8dcSSimon Schubert      with catch the exception.  */
13925796c8dcSSimon Schubert   if (!gdb_value_assign (var->value, value, &val))
13935796c8dcSSimon Schubert     return 0;
13945796c8dcSSimon Schubert 
13955796c8dcSSimon Schubert   /* If the value has changed, record it, so that next -var-update can
13965796c8dcSSimon Schubert      report this change.  If a variable had a value of '1', we've set it
13975796c8dcSSimon Schubert      to '333' and then set again to '1', when -var-update will report this
13985796c8dcSSimon Schubert      variable as changed -- because the first assignment has set the
13995796c8dcSSimon Schubert      'updated' flag.  There's no need to optimize that, because return value
14005796c8dcSSimon Schubert      of -var-update should be considered an approximation.  */
14015796c8dcSSimon Schubert   var->updated = install_new_value (var, val, 0 /* Compare values.  */);
14025796c8dcSSimon Schubert   input_radix = saved_input_radix;
14035796c8dcSSimon Schubert   return 1;
14045796c8dcSSimon Schubert }
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert #if HAVE_PYTHON
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert /* A helper function to install a constructor function and visualizer
14095796c8dcSSimon Schubert    in a varobj.  */
14105796c8dcSSimon Schubert 
14115796c8dcSSimon Schubert static void
14125796c8dcSSimon Schubert install_visualizer (struct varobj *var, PyObject *constructor,
14135796c8dcSSimon Schubert 		    PyObject *visualizer)
14145796c8dcSSimon Schubert {
14155796c8dcSSimon Schubert   Py_XDECREF (var->constructor);
14165796c8dcSSimon Schubert   var->constructor = constructor;
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert   Py_XDECREF (var->pretty_printer);
14195796c8dcSSimon Schubert   var->pretty_printer = visualizer;
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert   Py_XDECREF (var->child_iter);
14225796c8dcSSimon Schubert   var->child_iter = NULL;
14235796c8dcSSimon Schubert }
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert /* Install the default visualizer for VAR.  */
14265796c8dcSSimon Schubert 
14275796c8dcSSimon Schubert static void
14285796c8dcSSimon Schubert install_default_visualizer (struct varobj *var)
14295796c8dcSSimon Schubert {
1430c50c785cSJohn Marino   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
1431c50c785cSJohn Marino   if (CPLUS_FAKE_CHILD (var))
1432c50c785cSJohn Marino     return;
1433c50c785cSJohn Marino 
14345796c8dcSSimon Schubert   if (pretty_printing)
14355796c8dcSSimon Schubert     {
14365796c8dcSSimon Schubert       PyObject *pretty_printer = NULL;
14375796c8dcSSimon Schubert 
14385796c8dcSSimon Schubert       if (var->value)
14395796c8dcSSimon Schubert 	{
14405796c8dcSSimon Schubert 	  pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
14415796c8dcSSimon Schubert 	  if (! pretty_printer)
14425796c8dcSSimon Schubert 	    {
14435796c8dcSSimon Schubert 	      gdbpy_print_stack ();
14445796c8dcSSimon Schubert 	      error (_("Cannot instantiate printer for default visualizer"));
14455796c8dcSSimon Schubert 	    }
14465796c8dcSSimon Schubert 	}
14475796c8dcSSimon Schubert 
14485796c8dcSSimon Schubert       if (pretty_printer == Py_None)
14495796c8dcSSimon Schubert 	{
14505796c8dcSSimon Schubert 	  Py_DECREF (pretty_printer);
14515796c8dcSSimon Schubert 	  pretty_printer = NULL;
14525796c8dcSSimon Schubert 	}
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert       install_visualizer (var, NULL, pretty_printer);
14555796c8dcSSimon Schubert     }
14565796c8dcSSimon Schubert }
14575796c8dcSSimon Schubert 
14585796c8dcSSimon Schubert /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
14595796c8dcSSimon Schubert    make a new object.  */
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert static void
14625796c8dcSSimon Schubert construct_visualizer (struct varobj *var, PyObject *constructor)
14635796c8dcSSimon Schubert {
14645796c8dcSSimon Schubert   PyObject *pretty_printer;
14655796c8dcSSimon Schubert 
1466c50c785cSJohn Marino   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
1467c50c785cSJohn Marino   if (CPLUS_FAKE_CHILD (var))
1468c50c785cSJohn Marino     return;
1469c50c785cSJohn Marino 
14705796c8dcSSimon Schubert   Py_INCREF (constructor);
14715796c8dcSSimon Schubert   if (constructor == Py_None)
14725796c8dcSSimon Schubert     pretty_printer = NULL;
14735796c8dcSSimon Schubert   else
14745796c8dcSSimon Schubert     {
14755796c8dcSSimon Schubert       pretty_printer = instantiate_pretty_printer (constructor, var->value);
14765796c8dcSSimon Schubert       if (! pretty_printer)
14775796c8dcSSimon Schubert 	{
14785796c8dcSSimon Schubert 	  gdbpy_print_stack ();
14795796c8dcSSimon Schubert 	  Py_DECREF (constructor);
14805796c8dcSSimon Schubert 	  constructor = Py_None;
14815796c8dcSSimon Schubert 	  Py_INCREF (constructor);
14825796c8dcSSimon Schubert 	}
14835796c8dcSSimon Schubert 
14845796c8dcSSimon Schubert       if (pretty_printer == Py_None)
14855796c8dcSSimon Schubert 	{
14865796c8dcSSimon Schubert 	  Py_DECREF (pretty_printer);
14875796c8dcSSimon Schubert 	  pretty_printer = NULL;
14885796c8dcSSimon Schubert 	}
14895796c8dcSSimon Schubert     }
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert   install_visualizer (var, constructor, pretty_printer);
14925796c8dcSSimon Schubert }
14935796c8dcSSimon Schubert 
14945796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
14955796c8dcSSimon Schubert 
14965796c8dcSSimon Schubert /* A helper function for install_new_value.  This creates and installs
14975796c8dcSSimon Schubert    a visualizer for VAR, if appropriate.  */
14985796c8dcSSimon Schubert 
14995796c8dcSSimon Schubert static void
15005796c8dcSSimon Schubert install_new_value_visualizer (struct varobj *var)
15015796c8dcSSimon Schubert {
15025796c8dcSSimon Schubert #if HAVE_PYTHON
15035796c8dcSSimon Schubert   /* If the constructor is None, then we want the raw value.  If VAR
15045796c8dcSSimon Schubert      does not have a value, just skip this.  */
15055796c8dcSSimon Schubert   if (var->constructor != Py_None && var->value)
15065796c8dcSSimon Schubert     {
15075796c8dcSSimon Schubert       struct cleanup *cleanup;
15085796c8dcSSimon Schubert 
15095796c8dcSSimon Schubert       cleanup = varobj_ensure_python_env (var);
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert       if (!var->constructor)
15125796c8dcSSimon Schubert 	install_default_visualizer (var);
15135796c8dcSSimon Schubert       else
15145796c8dcSSimon Schubert 	construct_visualizer (var, var->constructor);
15155796c8dcSSimon Schubert 
15165796c8dcSSimon Schubert       do_cleanups (cleanup);
15175796c8dcSSimon Schubert     }
15185796c8dcSSimon Schubert #else
15195796c8dcSSimon Schubert   /* Do nothing.  */
15205796c8dcSSimon Schubert #endif
15215796c8dcSSimon Schubert }
15225796c8dcSSimon Schubert 
15235796c8dcSSimon Schubert /* Assign a new value to a variable object.  If INITIAL is non-zero,
15245796c8dcSSimon Schubert    this is the first assignement after the variable object was just
15255796c8dcSSimon Schubert    created, or changed type.  In that case, just assign the value
15265796c8dcSSimon Schubert    and return 0.
1527c50c785cSJohn Marino    Otherwise, assign the new value, and return 1 if the value is
1528c50c785cSJohn Marino    different from the current one, 0 otherwise.  The comparison is
1529c50c785cSJohn Marino    done on textual representation of value.  Therefore, some types
1530c50c785cSJohn Marino    need not be compared.  E.g.  for structures the reported value is
1531c50c785cSJohn Marino    always "{...}", so no comparison is necessary here.  If the old
1532c50c785cSJohn Marino    value was NULL and new one is not, or vice versa, we always return 1.
15335796c8dcSSimon Schubert 
15345796c8dcSSimon Schubert    The VALUE parameter should not be released -- the function will
15355796c8dcSSimon Schubert    take care of releasing it when needed.  */
15365796c8dcSSimon Schubert static int
15375796c8dcSSimon Schubert install_new_value (struct varobj *var, struct value *value, int initial)
15385796c8dcSSimon Schubert {
15395796c8dcSSimon Schubert   int changeable;
15405796c8dcSSimon Schubert   int need_to_fetch;
15415796c8dcSSimon Schubert   int changed = 0;
15425796c8dcSSimon Schubert   int intentionally_not_fetched = 0;
15435796c8dcSSimon Schubert   char *print_value = NULL;
15445796c8dcSSimon Schubert 
15455796c8dcSSimon Schubert   /* We need to know the varobj's type to decide if the value should
1546c50c785cSJohn Marino      be fetched or not.  C++ fake children (public/protected/private)
1547c50c785cSJohn Marino      don't have a type.  */
15485796c8dcSSimon Schubert   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
15495796c8dcSSimon Schubert   changeable = varobj_value_is_changeable_p (var);
15505796c8dcSSimon Schubert 
15515796c8dcSSimon Schubert   /* If the type has custom visualizer, we consider it to be always
15525796c8dcSSimon Schubert      changeable.  FIXME: need to make sure this behaviour will not
15535796c8dcSSimon Schubert      mess up read-sensitive values.  */
15545796c8dcSSimon Schubert   if (var->pretty_printer)
15555796c8dcSSimon Schubert     changeable = 1;
15565796c8dcSSimon Schubert 
15575796c8dcSSimon Schubert   need_to_fetch = changeable;
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert   /* We are not interested in the address of references, and given
15605796c8dcSSimon Schubert      that in C++ a reference is not rebindable, it cannot
15615796c8dcSSimon Schubert      meaningfully change.  So, get hold of the real value.  */
15625796c8dcSSimon Schubert   if (value)
15635796c8dcSSimon Schubert     value = coerce_ref (value);
15645796c8dcSSimon Schubert 
15655796c8dcSSimon Schubert   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
15665796c8dcSSimon Schubert     /* For unions, we need to fetch the value implicitly because
15675796c8dcSSimon Schubert        of implementation of union member fetch.  When gdb
15685796c8dcSSimon Schubert        creates a value for a field and the value of the enclosing
15695796c8dcSSimon Schubert        structure is not lazy,  it immediately copies the necessary
15705796c8dcSSimon Schubert        bytes from the enclosing values.  If the enclosing value is
15715796c8dcSSimon Schubert        lazy, the call to value_fetch_lazy on the field will read
15725796c8dcSSimon Schubert        the data from memory.  For unions, that means we'll read the
15735796c8dcSSimon Schubert        same memory more than once, which is not desirable.  So
15745796c8dcSSimon Schubert        fetch now.  */
15755796c8dcSSimon Schubert     need_to_fetch = 1;
15765796c8dcSSimon Schubert 
15775796c8dcSSimon Schubert   /* The new value might be lazy.  If the type is changeable,
15785796c8dcSSimon Schubert      that is we'll be comparing values of this type, fetch the
15795796c8dcSSimon Schubert      value now.  Otherwise, on the next update the old value
15805796c8dcSSimon Schubert      will be lazy, which means we've lost that old value.  */
15815796c8dcSSimon Schubert   if (need_to_fetch && value && value_lazy (value))
15825796c8dcSSimon Schubert     {
15835796c8dcSSimon Schubert       struct varobj *parent = var->parent;
15845796c8dcSSimon Schubert       int frozen = var->frozen;
1585cf7f2e2dSJohn Marino 
15865796c8dcSSimon Schubert       for (; !frozen && parent; parent = parent->parent)
15875796c8dcSSimon Schubert 	frozen |= parent->frozen;
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert       if (frozen && initial)
15905796c8dcSSimon Schubert 	{
15915796c8dcSSimon Schubert 	  /* For variables that are frozen, or are children of frozen
15925796c8dcSSimon Schubert 	     variables, we don't do fetch on initial assignment.
15935796c8dcSSimon Schubert 	     For non-initial assignemnt we do the fetch, since it means we're
15945796c8dcSSimon Schubert 	     explicitly asked to compare the new value with the old one.  */
15955796c8dcSSimon Schubert 	  intentionally_not_fetched = 1;
15965796c8dcSSimon Schubert 	}
15975796c8dcSSimon Schubert       else if (!gdb_value_fetch_lazy (value))
15985796c8dcSSimon Schubert 	{
15995796c8dcSSimon Schubert 	  /* Set the value to NULL, so that for the next -var-update,
16005796c8dcSSimon Schubert 	     we don't try to compare the new value with this value,
16015796c8dcSSimon Schubert 	     that we couldn't even read.  */
16025796c8dcSSimon Schubert 	  value = NULL;
16035796c8dcSSimon Schubert 	}
16045796c8dcSSimon Schubert     }
16055796c8dcSSimon Schubert 
16065796c8dcSSimon Schubert 
16075796c8dcSSimon Schubert   /* Below, we'll be comparing string rendering of old and new
16085796c8dcSSimon Schubert      values.  Don't get string rendering if the value is
16095796c8dcSSimon Schubert      lazy -- if it is, the code above has decided that the value
16105796c8dcSSimon Schubert      should not be fetched.  */
16115796c8dcSSimon Schubert   if (value && !value_lazy (value) && !var->pretty_printer)
16125796c8dcSSimon Schubert     print_value = value_get_print_value (value, var->format, var);
16135796c8dcSSimon Schubert 
16145796c8dcSSimon Schubert   /* If the type is changeable, compare the old and the new values.
16155796c8dcSSimon Schubert      If this is the initial assignment, we don't have any old value
16165796c8dcSSimon Schubert      to compare with.  */
16175796c8dcSSimon Schubert   if (!initial && changeable)
16185796c8dcSSimon Schubert     {
1619c50c785cSJohn Marino       /* If the value of the varobj was changed by -var-set-value,
1620c50c785cSJohn Marino 	 then the value in the varobj and in the target is the same.
1621c50c785cSJohn Marino 	 However, that value is different from the value that the
1622c50c785cSJohn Marino 	 varobj had after the previous -var-update.  So need to the
1623c50c785cSJohn Marino 	 varobj as changed.  */
16245796c8dcSSimon Schubert       if (var->updated)
16255796c8dcSSimon Schubert 	{
16265796c8dcSSimon Schubert 	  changed = 1;
16275796c8dcSSimon Schubert 	}
16285796c8dcSSimon Schubert       else if (! var->pretty_printer)
16295796c8dcSSimon Schubert 	{
16305796c8dcSSimon Schubert 	  /* Try to compare the values.  That requires that both
16315796c8dcSSimon Schubert 	     values are non-lazy.  */
16325796c8dcSSimon Schubert 	  if (var->not_fetched && value_lazy (var->value))
16335796c8dcSSimon Schubert 	    {
16345796c8dcSSimon Schubert 	      /* This is a frozen varobj and the value was never read.
16355796c8dcSSimon Schubert 		 Presumably, UI shows some "never read" indicator.
16365796c8dcSSimon Schubert 		 Now that we've fetched the real value, we need to report
16375796c8dcSSimon Schubert 		 this varobj as changed so that UI can show the real
16385796c8dcSSimon Schubert 		 value.  */
16395796c8dcSSimon Schubert 	      changed = 1;
16405796c8dcSSimon Schubert 	    }
16415796c8dcSSimon Schubert           else  if (var->value == NULL && value == NULL)
16425796c8dcSSimon Schubert 	    /* Equal.  */
16435796c8dcSSimon Schubert 	    ;
16445796c8dcSSimon Schubert 	  else if (var->value == NULL || value == NULL)
16455796c8dcSSimon Schubert 	    {
16465796c8dcSSimon Schubert 	      changed = 1;
16475796c8dcSSimon Schubert 	    }
16485796c8dcSSimon Schubert 	  else
16495796c8dcSSimon Schubert 	    {
16505796c8dcSSimon Schubert 	      gdb_assert (!value_lazy (var->value));
16515796c8dcSSimon Schubert 	      gdb_assert (!value_lazy (value));
16525796c8dcSSimon Schubert 
16535796c8dcSSimon Schubert 	      gdb_assert (var->print_value != NULL && print_value != NULL);
16545796c8dcSSimon Schubert 	      if (strcmp (var->print_value, print_value) != 0)
16555796c8dcSSimon Schubert 		changed = 1;
16565796c8dcSSimon Schubert 	    }
16575796c8dcSSimon Schubert 	}
16585796c8dcSSimon Schubert     }
16595796c8dcSSimon Schubert 
16605796c8dcSSimon Schubert   if (!initial && !changeable)
16615796c8dcSSimon Schubert     {
16625796c8dcSSimon Schubert       /* For values that are not changeable, we don't compare the values.
16635796c8dcSSimon Schubert 	 However, we want to notice if a value was not NULL and now is NULL,
16645796c8dcSSimon Schubert 	 or vise versa, so that we report when top-level varobjs come in scope
16655796c8dcSSimon Schubert 	 and leave the scope.  */
16665796c8dcSSimon Schubert       changed = (var->value != NULL) != (value != NULL);
16675796c8dcSSimon Schubert     }
16685796c8dcSSimon Schubert 
16695796c8dcSSimon Schubert   /* We must always keep the new value, since children depend on it.  */
16705796c8dcSSimon Schubert   if (var->value != NULL && var->value != value)
16715796c8dcSSimon Schubert     value_free (var->value);
16725796c8dcSSimon Schubert   var->value = value;
16735796c8dcSSimon Schubert   if (value != NULL)
16745796c8dcSSimon Schubert     value_incref (value);
16755796c8dcSSimon Schubert   if (value && value_lazy (value) && intentionally_not_fetched)
16765796c8dcSSimon Schubert     var->not_fetched = 1;
16775796c8dcSSimon Schubert   else
16785796c8dcSSimon Schubert     var->not_fetched = 0;
16795796c8dcSSimon Schubert   var->updated = 0;
16805796c8dcSSimon Schubert 
16815796c8dcSSimon Schubert   install_new_value_visualizer (var);
16825796c8dcSSimon Schubert 
16835796c8dcSSimon Schubert   /* If we installed a pretty-printer, re-compare the printed version
16845796c8dcSSimon Schubert      to see if the variable changed.  */
16855796c8dcSSimon Schubert   if (var->pretty_printer)
16865796c8dcSSimon Schubert     {
16875796c8dcSSimon Schubert       xfree (print_value);
16885796c8dcSSimon Schubert       print_value = value_get_print_value (var->value, var->format, var);
1689cf7f2e2dSJohn Marino       if ((var->print_value == NULL && print_value != NULL)
1690cf7f2e2dSJohn Marino 	  || (var->print_value != NULL && print_value == NULL)
1691cf7f2e2dSJohn Marino 	  || (var->print_value != NULL && print_value != NULL
1692cf7f2e2dSJohn Marino 	      && strcmp (var->print_value, print_value) != 0))
16935796c8dcSSimon Schubert 	changed = 1;
16945796c8dcSSimon Schubert     }
16955796c8dcSSimon Schubert   if (var->print_value)
16965796c8dcSSimon Schubert     xfree (var->print_value);
16975796c8dcSSimon Schubert   var->print_value = print_value;
16985796c8dcSSimon Schubert 
16995796c8dcSSimon Schubert   gdb_assert (!var->value || value_type (var->value));
17005796c8dcSSimon Schubert 
17015796c8dcSSimon Schubert   return changed;
17025796c8dcSSimon Schubert }
17035796c8dcSSimon Schubert 
17045796c8dcSSimon Schubert /* Return the requested range for a varobj.  VAR is the varobj.  FROM
17055796c8dcSSimon Schubert    and TO are out parameters; *FROM and *TO will be set to the
17065796c8dcSSimon Schubert    selected sub-range of VAR.  If no range was selected using
17075796c8dcSSimon Schubert    -var-set-update-range, then both will be -1.  */
17085796c8dcSSimon Schubert void
17095796c8dcSSimon Schubert varobj_get_child_range (struct varobj *var, int *from, int *to)
17105796c8dcSSimon Schubert {
17115796c8dcSSimon Schubert   *from = var->from;
17125796c8dcSSimon Schubert   *to = var->to;
17135796c8dcSSimon Schubert }
17145796c8dcSSimon Schubert 
17155796c8dcSSimon Schubert /* Set the selected sub-range of children of VAR to start at index
17165796c8dcSSimon Schubert    FROM and end at index TO.  If either FROM or TO is less than zero,
17175796c8dcSSimon Schubert    this is interpreted as a request for all children.  */
17185796c8dcSSimon Schubert void
17195796c8dcSSimon Schubert varobj_set_child_range (struct varobj *var, int from, int to)
17205796c8dcSSimon Schubert {
17215796c8dcSSimon Schubert   var->from = from;
17225796c8dcSSimon Schubert   var->to = to;
17235796c8dcSSimon Schubert }
17245796c8dcSSimon Schubert 
17255796c8dcSSimon Schubert void
17265796c8dcSSimon Schubert varobj_set_visualizer (struct varobj *var, const char *visualizer)
17275796c8dcSSimon Schubert {
17285796c8dcSSimon Schubert #if HAVE_PYTHON
1729cf7f2e2dSJohn Marino   PyObject *mainmod, *globals, *constructor;
1730cf7f2e2dSJohn Marino   struct cleanup *back_to;
17315796c8dcSSimon Schubert 
17325796c8dcSSimon Schubert   back_to = varobj_ensure_python_env (var);
17335796c8dcSSimon Schubert 
17345796c8dcSSimon Schubert   mainmod = PyImport_AddModule ("__main__");
17355796c8dcSSimon Schubert   globals = PyModule_GetDict (mainmod);
17365796c8dcSSimon Schubert   Py_INCREF (globals);
17375796c8dcSSimon Schubert   make_cleanup_py_decref (globals);
17385796c8dcSSimon Schubert 
17395796c8dcSSimon Schubert   constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
17405796c8dcSSimon Schubert 
17415796c8dcSSimon Schubert   if (! constructor)
17425796c8dcSSimon Schubert     {
17435796c8dcSSimon Schubert       gdbpy_print_stack ();
17445796c8dcSSimon Schubert       error (_("Could not evaluate visualizer expression: %s"), visualizer);
17455796c8dcSSimon Schubert     }
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert   construct_visualizer (var, constructor);
17485796c8dcSSimon Schubert   Py_XDECREF (constructor);
17495796c8dcSSimon Schubert 
17505796c8dcSSimon Schubert   /* If there are any children now, wipe them.  */
17515796c8dcSSimon Schubert   varobj_delete (var, NULL, 1 /* children only */);
17525796c8dcSSimon Schubert   var->num_children = -1;
17535796c8dcSSimon Schubert 
17545796c8dcSSimon Schubert   do_cleanups (back_to);
17555796c8dcSSimon Schubert #else
17565796c8dcSSimon Schubert   error (_("Python support required"));
17575796c8dcSSimon Schubert #endif
17585796c8dcSSimon Schubert }
17595796c8dcSSimon Schubert 
17605796c8dcSSimon Schubert /* Update the values for a variable and its children.  This is a
17615796c8dcSSimon Schubert    two-pronged attack.  First, re-parse the value for the root's
17625796c8dcSSimon Schubert    expression to see if it's changed.  Then go all the way
17635796c8dcSSimon Schubert    through its children, reconstructing them and noting if they've
17645796c8dcSSimon Schubert    changed.
17655796c8dcSSimon Schubert 
17665796c8dcSSimon Schubert    The EXPLICIT parameter specifies if this call is result
17675796c8dcSSimon Schubert    of MI request to update this specific variable, or
17685796c8dcSSimon Schubert    result of implicit -var-update *.  For implicit request, we don't
17695796c8dcSSimon Schubert    update frozen variables.
17705796c8dcSSimon Schubert 
17715796c8dcSSimon Schubert    NOTE: This function may delete the caller's varobj.  If it
17725796c8dcSSimon Schubert    returns TYPE_CHANGED, then it has done this and VARP will be modified
17735796c8dcSSimon Schubert    to point to the new varobj.  */
17745796c8dcSSimon Schubert 
17755796c8dcSSimon Schubert VEC(varobj_update_result) *varobj_update (struct varobj **varp, int explicit)
17765796c8dcSSimon Schubert {
17775796c8dcSSimon Schubert   int changed = 0;
17785796c8dcSSimon Schubert   int type_changed = 0;
17795796c8dcSSimon Schubert   int i;
17805796c8dcSSimon Schubert   struct value *new;
17815796c8dcSSimon Schubert   VEC (varobj_update_result) *stack = NULL;
17825796c8dcSSimon Schubert   VEC (varobj_update_result) *result = NULL;
17835796c8dcSSimon Schubert 
17845796c8dcSSimon Schubert   /* Frozen means frozen -- we don't check for any change in
17855796c8dcSSimon Schubert      this varobj, including its going out of scope, or
17865796c8dcSSimon Schubert      changing type.  One use case for frozen varobjs is
17875796c8dcSSimon Schubert      retaining previously evaluated expressions, and we don't
17885796c8dcSSimon Schubert      want them to be reevaluated at all.  */
17895796c8dcSSimon Schubert   if (!explicit && (*varp)->frozen)
17905796c8dcSSimon Schubert     return result;
17915796c8dcSSimon Schubert 
17925796c8dcSSimon Schubert   if (!(*varp)->root->is_valid)
17935796c8dcSSimon Schubert     {
1794cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1795cf7f2e2dSJohn Marino 
1796cf7f2e2dSJohn Marino       r.varobj = *varp;
17975796c8dcSSimon Schubert       r.status = VAROBJ_INVALID;
17985796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, result, &r);
17995796c8dcSSimon Schubert       return result;
18005796c8dcSSimon Schubert     }
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert   if ((*varp)->root->rootvar == *varp)
18035796c8dcSSimon Schubert     {
1804cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1805cf7f2e2dSJohn Marino 
1806cf7f2e2dSJohn Marino       r.varobj = *varp;
18075796c8dcSSimon Schubert       r.status = VAROBJ_IN_SCOPE;
18085796c8dcSSimon Schubert 
18095796c8dcSSimon Schubert       /* Update the root variable.  value_of_root can return NULL
18105796c8dcSSimon Schubert 	 if the variable is no longer around, i.e. we stepped out of
18115796c8dcSSimon Schubert 	 the frame in which a local existed.  We are letting the
18125796c8dcSSimon Schubert 	 value_of_root variable dispose of the varobj if the type
18135796c8dcSSimon Schubert 	 has changed.  */
18145796c8dcSSimon Schubert       new = value_of_root (varp, &type_changed);
18155796c8dcSSimon Schubert       r.varobj = *varp;
18165796c8dcSSimon Schubert 
18175796c8dcSSimon Schubert       r.type_changed = type_changed;
18185796c8dcSSimon Schubert       if (install_new_value ((*varp), new, type_changed))
18195796c8dcSSimon Schubert 	r.changed = 1;
18205796c8dcSSimon Schubert 
18215796c8dcSSimon Schubert       if (new == NULL)
18225796c8dcSSimon Schubert 	r.status = VAROBJ_NOT_IN_SCOPE;
18235796c8dcSSimon Schubert       r.value_installed = 1;
18245796c8dcSSimon Schubert 
18255796c8dcSSimon Schubert       if (r.status == VAROBJ_NOT_IN_SCOPE)
18265796c8dcSSimon Schubert 	{
18275796c8dcSSimon Schubert 	  if (r.type_changed || r.changed)
18285796c8dcSSimon Schubert 	    VEC_safe_push (varobj_update_result, result, &r);
18295796c8dcSSimon Schubert 	  return result;
18305796c8dcSSimon Schubert 	}
18315796c8dcSSimon Schubert 
18325796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, stack, &r);
18335796c8dcSSimon Schubert     }
18345796c8dcSSimon Schubert   else
18355796c8dcSSimon Schubert     {
1836cf7f2e2dSJohn Marino       varobj_update_result r = {0};
1837cf7f2e2dSJohn Marino 
1838cf7f2e2dSJohn Marino       r.varobj = *varp;
18395796c8dcSSimon Schubert       VEC_safe_push (varobj_update_result, stack, &r);
18405796c8dcSSimon Schubert     }
18415796c8dcSSimon Schubert 
18425796c8dcSSimon Schubert   /* Walk through the children, reconstructing them all.  */
18435796c8dcSSimon Schubert   while (!VEC_empty (varobj_update_result, stack))
18445796c8dcSSimon Schubert     {
18455796c8dcSSimon Schubert       varobj_update_result r = *(VEC_last (varobj_update_result, stack));
18465796c8dcSSimon Schubert       struct varobj *v = r.varobj;
18475796c8dcSSimon Schubert 
18485796c8dcSSimon Schubert       VEC_pop (varobj_update_result, stack);
18495796c8dcSSimon Schubert 
18505796c8dcSSimon Schubert       /* Update this variable, unless it's a root, which is already
18515796c8dcSSimon Schubert 	 updated.  */
18525796c8dcSSimon Schubert       if (!r.value_installed)
18535796c8dcSSimon Schubert 	{
18545796c8dcSSimon Schubert 	  new = value_of_child (v->parent, v->index);
18555796c8dcSSimon Schubert 	  if (install_new_value (v, new, 0 /* type not changed */))
18565796c8dcSSimon Schubert 	    {
18575796c8dcSSimon Schubert 	      r.changed = 1;
18585796c8dcSSimon Schubert 	      v->updated = 0;
18595796c8dcSSimon Schubert 	    }
18605796c8dcSSimon Schubert 	}
18615796c8dcSSimon Schubert 
18625796c8dcSSimon Schubert       /* We probably should not get children of a varobj that has a
18635796c8dcSSimon Schubert 	 pretty-printer, but for which -var-list-children was never
18645796c8dcSSimon Schubert 	 invoked.  */
18655796c8dcSSimon Schubert       if (v->pretty_printer)
18665796c8dcSSimon Schubert 	{
18675796c8dcSSimon Schubert 	  VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
18685796c8dcSSimon Schubert 	  int i, children_changed = 0;
18695796c8dcSSimon Schubert 
18705796c8dcSSimon Schubert 	  if (v->frozen)
18715796c8dcSSimon Schubert 	    continue;
18725796c8dcSSimon Schubert 
18735796c8dcSSimon Schubert 	  if (!v->children_requested)
18745796c8dcSSimon Schubert 	    {
18755796c8dcSSimon Schubert 	      int dummy;
18765796c8dcSSimon Schubert 
18775796c8dcSSimon Schubert 	      /* If we initially did not have potential children, but
18785796c8dcSSimon Schubert 		 now we do, consider the varobj as changed.
18795796c8dcSSimon Schubert 		 Otherwise, if children were never requested, consider
18805796c8dcSSimon Schubert 		 it as unchanged -- presumably, such varobj is not yet
18815796c8dcSSimon Schubert 		 expanded in the UI, so we need not bother getting
18825796c8dcSSimon Schubert 		 it.  */
18835796c8dcSSimon Schubert 	      if (!varobj_has_more (v, 0))
18845796c8dcSSimon Schubert 		{
18855796c8dcSSimon Schubert 		  update_dynamic_varobj_children (v, NULL, NULL, NULL,
18865796c8dcSSimon Schubert 						  &dummy, 0, 0, 0);
18875796c8dcSSimon Schubert 		  if (varobj_has_more (v, 0))
18885796c8dcSSimon Schubert 		    r.changed = 1;
18895796c8dcSSimon Schubert 		}
18905796c8dcSSimon Schubert 
18915796c8dcSSimon Schubert 	      if (r.changed)
18925796c8dcSSimon Schubert 		VEC_safe_push (varobj_update_result, result, &r);
18935796c8dcSSimon Schubert 
18945796c8dcSSimon Schubert 	      continue;
18955796c8dcSSimon Schubert 	    }
18965796c8dcSSimon Schubert 
18975796c8dcSSimon Schubert 	  /* If update_dynamic_varobj_children returns 0, then we have
18985796c8dcSSimon Schubert 	     a non-conforming pretty-printer, so we skip it.  */
18995796c8dcSSimon Schubert 	  if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
19005796c8dcSSimon Schubert 					      &children_changed, 1,
19015796c8dcSSimon Schubert 					      v->from, v->to))
19025796c8dcSSimon Schubert 	    {
19035796c8dcSSimon Schubert 	      if (children_changed || new)
19045796c8dcSSimon Schubert 		{
19055796c8dcSSimon Schubert 		  r.children_changed = 1;
19065796c8dcSSimon Schubert 		  r.new = new;
19075796c8dcSSimon Schubert 		}
19085796c8dcSSimon Schubert 	      /* Push in reverse order so that the first child is
19095796c8dcSSimon Schubert 		 popped from the work stack first, and so will be
19105796c8dcSSimon Schubert 		 added to result first.  This does not affect
19115796c8dcSSimon Schubert 		 correctness, just "nicer".  */
19125796c8dcSSimon Schubert 	      for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
19135796c8dcSSimon Schubert 		{
19145796c8dcSSimon Schubert 		  varobj_p tmp = VEC_index (varobj_p, changed, i);
1915cf7f2e2dSJohn Marino 		  varobj_update_result r = {0};
1916cf7f2e2dSJohn Marino 
1917cf7f2e2dSJohn Marino 		  r.varobj = tmp;
19185796c8dcSSimon Schubert 		  r.changed = 1;
19195796c8dcSSimon Schubert 		  r.value_installed = 1;
19205796c8dcSSimon Schubert 		  VEC_safe_push (varobj_update_result, stack, &r);
19215796c8dcSSimon Schubert 		}
19225796c8dcSSimon Schubert 	      for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
19235796c8dcSSimon Schubert 	      	{
19245796c8dcSSimon Schubert 		  varobj_p tmp = VEC_index (varobj_p, unchanged, i);
1925cf7f2e2dSJohn Marino 
19265796c8dcSSimon Schubert 	      	  if (!tmp->frozen)
19275796c8dcSSimon Schubert 	      	    {
1928cf7f2e2dSJohn Marino 	      	      varobj_update_result r = {0};
1929cf7f2e2dSJohn Marino 
1930cf7f2e2dSJohn Marino 		      r.varobj = tmp;
19315796c8dcSSimon Schubert 	      	      r.value_installed = 1;
19325796c8dcSSimon Schubert 	      	      VEC_safe_push (varobj_update_result, stack, &r);
19335796c8dcSSimon Schubert 	      	    }
19345796c8dcSSimon Schubert 	      	}
19355796c8dcSSimon Schubert 	      if (r.changed || r.children_changed)
19365796c8dcSSimon Schubert 		VEC_safe_push (varobj_update_result, result, &r);
19375796c8dcSSimon Schubert 
19385796c8dcSSimon Schubert 	      /* Free CHANGED and UNCHANGED, but not NEW, because NEW
19395796c8dcSSimon Schubert 		 has been put into the result vector.  */
19405796c8dcSSimon Schubert 	      VEC_free (varobj_p, changed);
19415796c8dcSSimon Schubert 	      VEC_free (varobj_p, unchanged);
19425796c8dcSSimon Schubert 
19435796c8dcSSimon Schubert 	      continue;
19445796c8dcSSimon Schubert 	    }
19455796c8dcSSimon Schubert 	}
19465796c8dcSSimon Schubert 
19475796c8dcSSimon Schubert       /* Push any children.  Use reverse order so that the first
19485796c8dcSSimon Schubert 	 child is popped from the work stack first, and so
19495796c8dcSSimon Schubert 	 will be added to result first.  This does not
19505796c8dcSSimon Schubert 	 affect correctness, just "nicer".  */
19515796c8dcSSimon Schubert       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
19525796c8dcSSimon Schubert 	{
19535796c8dcSSimon Schubert 	  varobj_p c = VEC_index (varobj_p, v->children, i);
1954cf7f2e2dSJohn Marino 
19555796c8dcSSimon Schubert 	  /* Child may be NULL if explicitly deleted by -var-delete.  */
19565796c8dcSSimon Schubert 	  if (c != NULL && !c->frozen)
19575796c8dcSSimon Schubert 	    {
1958cf7f2e2dSJohn Marino 	      varobj_update_result r = {0};
1959cf7f2e2dSJohn Marino 
1960cf7f2e2dSJohn Marino 	      r.varobj = c;
19615796c8dcSSimon Schubert 	      VEC_safe_push (varobj_update_result, stack, &r);
19625796c8dcSSimon Schubert 	    }
19635796c8dcSSimon Schubert 	}
19645796c8dcSSimon Schubert 
19655796c8dcSSimon Schubert       if (r.changed || r.type_changed)
19665796c8dcSSimon Schubert 	VEC_safe_push (varobj_update_result, result, &r);
19675796c8dcSSimon Schubert     }
19685796c8dcSSimon Schubert 
19695796c8dcSSimon Schubert   VEC_free (varobj_update_result, stack);
19705796c8dcSSimon Schubert 
19715796c8dcSSimon Schubert   return result;
19725796c8dcSSimon Schubert }
19735796c8dcSSimon Schubert 
19745796c8dcSSimon Schubert 
19755796c8dcSSimon Schubert /* Helper functions */
19765796c8dcSSimon Schubert 
19775796c8dcSSimon Schubert /*
19785796c8dcSSimon Schubert  * Variable object construction/destruction
19795796c8dcSSimon Schubert  */
19805796c8dcSSimon Schubert 
19815796c8dcSSimon Schubert static int
19825796c8dcSSimon Schubert delete_variable (struct cpstack **resultp, struct varobj *var,
19835796c8dcSSimon Schubert 		 int only_children_p)
19845796c8dcSSimon Schubert {
19855796c8dcSSimon Schubert   int delcount = 0;
19865796c8dcSSimon Schubert 
19875796c8dcSSimon Schubert   delete_variable_1 (resultp, &delcount, var,
19885796c8dcSSimon Schubert 		     only_children_p, 1 /* remove_from_parent_p */ );
19895796c8dcSSimon Schubert 
19905796c8dcSSimon Schubert   return delcount;
19915796c8dcSSimon Schubert }
19925796c8dcSSimon Schubert 
1993c50c785cSJohn Marino /* Delete the variable object VAR and its children.  */
19945796c8dcSSimon Schubert /* IMPORTANT NOTE: If we delete a variable which is a child
19955796c8dcSSimon Schubert    and the parent is not removed we dump core.  It must be always
1996c50c785cSJohn Marino    initially called with remove_from_parent_p set.  */
19975796c8dcSSimon Schubert static void
19985796c8dcSSimon Schubert delete_variable_1 (struct cpstack **resultp, int *delcountp,
19995796c8dcSSimon Schubert 		   struct varobj *var, int only_children_p,
20005796c8dcSSimon Schubert 		   int remove_from_parent_p)
20015796c8dcSSimon Schubert {
20025796c8dcSSimon Schubert   int i;
20035796c8dcSSimon Schubert 
20045796c8dcSSimon Schubert   /* Delete any children of this variable, too.  */
20055796c8dcSSimon Schubert   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
20065796c8dcSSimon Schubert     {
20075796c8dcSSimon Schubert       varobj_p child = VEC_index (varobj_p, var->children, i);
2008cf7f2e2dSJohn Marino 
20095796c8dcSSimon Schubert       if (!child)
20105796c8dcSSimon Schubert 	continue;
20115796c8dcSSimon Schubert       if (!remove_from_parent_p)
20125796c8dcSSimon Schubert 	child->parent = NULL;
20135796c8dcSSimon Schubert       delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
20145796c8dcSSimon Schubert     }
20155796c8dcSSimon Schubert   VEC_free (varobj_p, var->children);
20165796c8dcSSimon Schubert 
2017c50c785cSJohn Marino   /* if we were called to delete only the children we are done here.  */
20185796c8dcSSimon Schubert   if (only_children_p)
20195796c8dcSSimon Schubert     return;
20205796c8dcSSimon Schubert 
2021c50c785cSJohn Marino   /* Otherwise, add it to the list of deleted ones and proceed to do so.  */
20225796c8dcSSimon Schubert   /* If the name is null, this is a temporary variable, that has not
20235796c8dcSSimon Schubert      yet been installed, don't report it, it belongs to the caller...  */
20245796c8dcSSimon Schubert   if (var->obj_name != NULL)
20255796c8dcSSimon Schubert     {
20265796c8dcSSimon Schubert       cppush (resultp, xstrdup (var->obj_name));
20275796c8dcSSimon Schubert       *delcountp = *delcountp + 1;
20285796c8dcSSimon Schubert     }
20295796c8dcSSimon Schubert 
2030c50c785cSJohn Marino   /* If this variable has a parent, remove it from its parent's list.  */
20315796c8dcSSimon Schubert   /* OPTIMIZATION: if the parent of this variable is also being deleted,
20325796c8dcSSimon Schubert      (as indicated by remove_from_parent_p) we don't bother doing an
20335796c8dcSSimon Schubert      expensive list search to find the element to remove when we are
2034c50c785cSJohn Marino      discarding the list afterwards.  */
20355796c8dcSSimon Schubert   if ((remove_from_parent_p) && (var->parent != NULL))
20365796c8dcSSimon Schubert     {
20375796c8dcSSimon Schubert       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
20385796c8dcSSimon Schubert     }
20395796c8dcSSimon Schubert 
20405796c8dcSSimon Schubert   if (var->obj_name != NULL)
20415796c8dcSSimon Schubert     uninstall_variable (var);
20425796c8dcSSimon Schubert 
2043c50c785cSJohn Marino   /* Free memory associated with this variable.  */
20445796c8dcSSimon Schubert   free_variable (var);
20455796c8dcSSimon Schubert }
20465796c8dcSSimon Schubert 
20475796c8dcSSimon Schubert /* Install the given variable VAR with the object name VAR->OBJ_NAME.  */
20485796c8dcSSimon Schubert static int
20495796c8dcSSimon Schubert install_variable (struct varobj *var)
20505796c8dcSSimon Schubert {
20515796c8dcSSimon Schubert   struct vlist *cv;
20525796c8dcSSimon Schubert   struct vlist *newvl;
20535796c8dcSSimon Schubert   const char *chp;
20545796c8dcSSimon Schubert   unsigned int index = 0;
20555796c8dcSSimon Schubert   unsigned int i = 1;
20565796c8dcSSimon Schubert 
20575796c8dcSSimon Schubert   for (chp = var->obj_name; *chp; chp++)
20585796c8dcSSimon Schubert     {
20595796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
20605796c8dcSSimon Schubert     }
20615796c8dcSSimon Schubert 
20625796c8dcSSimon Schubert   cv = *(varobj_table + index);
20635796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
20645796c8dcSSimon Schubert     cv = cv->next;
20655796c8dcSSimon Schubert 
20665796c8dcSSimon Schubert   if (cv != NULL)
20675796c8dcSSimon Schubert     error (_("Duplicate variable object name"));
20685796c8dcSSimon Schubert 
2069c50c785cSJohn Marino   /* Add varobj to hash table.  */
20705796c8dcSSimon Schubert   newvl = xmalloc (sizeof (struct vlist));
20715796c8dcSSimon Schubert   newvl->next = *(varobj_table + index);
20725796c8dcSSimon Schubert   newvl->var = var;
20735796c8dcSSimon Schubert   *(varobj_table + index) = newvl;
20745796c8dcSSimon Schubert 
2075c50c785cSJohn Marino   /* If root, add varobj to root list.  */
20765796c8dcSSimon Schubert   if (is_root_p (var))
20775796c8dcSSimon Schubert     {
2078c50c785cSJohn Marino       /* Add to list of root variables.  */
20795796c8dcSSimon Schubert       if (rootlist == NULL)
20805796c8dcSSimon Schubert 	var->root->next = NULL;
20815796c8dcSSimon Schubert       else
20825796c8dcSSimon Schubert 	var->root->next = rootlist;
20835796c8dcSSimon Schubert       rootlist = var->root;
20845796c8dcSSimon Schubert     }
20855796c8dcSSimon Schubert 
20865796c8dcSSimon Schubert   return 1;			/* OK */
20875796c8dcSSimon Schubert }
20885796c8dcSSimon Schubert 
20895796c8dcSSimon Schubert /* Unistall the object VAR.  */
20905796c8dcSSimon Schubert static void
20915796c8dcSSimon Schubert uninstall_variable (struct varobj *var)
20925796c8dcSSimon Schubert {
20935796c8dcSSimon Schubert   struct vlist *cv;
20945796c8dcSSimon Schubert   struct vlist *prev;
20955796c8dcSSimon Schubert   struct varobj_root *cr;
20965796c8dcSSimon Schubert   struct varobj_root *prer;
20975796c8dcSSimon Schubert   const char *chp;
20985796c8dcSSimon Schubert   unsigned int index = 0;
20995796c8dcSSimon Schubert   unsigned int i = 1;
21005796c8dcSSimon Schubert 
2101c50c785cSJohn Marino   /* Remove varobj from hash table.  */
21025796c8dcSSimon Schubert   for (chp = var->obj_name; *chp; chp++)
21035796c8dcSSimon Schubert     {
21045796c8dcSSimon Schubert       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
21055796c8dcSSimon Schubert     }
21065796c8dcSSimon Schubert 
21075796c8dcSSimon Schubert   cv = *(varobj_table + index);
21085796c8dcSSimon Schubert   prev = NULL;
21095796c8dcSSimon Schubert   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
21105796c8dcSSimon Schubert     {
21115796c8dcSSimon Schubert       prev = cv;
21125796c8dcSSimon Schubert       cv = cv->next;
21135796c8dcSSimon Schubert     }
21145796c8dcSSimon Schubert 
21155796c8dcSSimon Schubert   if (varobjdebug)
21165796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
21175796c8dcSSimon Schubert 
21185796c8dcSSimon Schubert   if (cv == NULL)
21195796c8dcSSimon Schubert     {
21205796c8dcSSimon Schubert       warning
21215796c8dcSSimon Schubert 	("Assertion failed: Could not find variable object \"%s\" to delete",
21225796c8dcSSimon Schubert 	 var->obj_name);
21235796c8dcSSimon Schubert       return;
21245796c8dcSSimon Schubert     }
21255796c8dcSSimon Schubert 
21265796c8dcSSimon Schubert   if (prev == NULL)
21275796c8dcSSimon Schubert     *(varobj_table + index) = cv->next;
21285796c8dcSSimon Schubert   else
21295796c8dcSSimon Schubert     prev->next = cv->next;
21305796c8dcSSimon Schubert 
21315796c8dcSSimon Schubert   xfree (cv);
21325796c8dcSSimon Schubert 
2133c50c785cSJohn Marino   /* If root, remove varobj from root list.  */
21345796c8dcSSimon Schubert   if (is_root_p (var))
21355796c8dcSSimon Schubert     {
2136c50c785cSJohn Marino       /* Remove from list of root variables.  */
21375796c8dcSSimon Schubert       if (rootlist == var->root)
21385796c8dcSSimon Schubert 	rootlist = var->root->next;
21395796c8dcSSimon Schubert       else
21405796c8dcSSimon Schubert 	{
21415796c8dcSSimon Schubert 	  prer = NULL;
21425796c8dcSSimon Schubert 	  cr = rootlist;
21435796c8dcSSimon Schubert 	  while ((cr != NULL) && (cr->rootvar != var))
21445796c8dcSSimon Schubert 	    {
21455796c8dcSSimon Schubert 	      prer = cr;
21465796c8dcSSimon Schubert 	      cr = cr->next;
21475796c8dcSSimon Schubert 	    }
21485796c8dcSSimon Schubert 	  if (cr == NULL)
21495796c8dcSSimon Schubert 	    {
2150c50c785cSJohn Marino 	      warning (_("Assertion failed: Could not find "
2151c50c785cSJohn Marino 		         "varobj \"%s\" in root list"),
21525796c8dcSSimon Schubert 		       var->obj_name);
21535796c8dcSSimon Schubert 	      return;
21545796c8dcSSimon Schubert 	    }
21555796c8dcSSimon Schubert 	  if (prer == NULL)
21565796c8dcSSimon Schubert 	    rootlist = NULL;
21575796c8dcSSimon Schubert 	  else
21585796c8dcSSimon Schubert 	    prer->next = cr->next;
21595796c8dcSSimon Schubert 	}
21605796c8dcSSimon Schubert     }
21615796c8dcSSimon Schubert 
21625796c8dcSSimon Schubert }
21635796c8dcSSimon Schubert 
2164c50c785cSJohn Marino /* Create and install a child of the parent of the given name.  */
21655796c8dcSSimon Schubert static struct varobj *
21665796c8dcSSimon Schubert create_child (struct varobj *parent, int index, char *name)
21675796c8dcSSimon Schubert {
21685796c8dcSSimon Schubert   return create_child_with_value (parent, index, name,
21695796c8dcSSimon Schubert 				  value_of_child (parent, index));
21705796c8dcSSimon Schubert }
21715796c8dcSSimon Schubert 
21725796c8dcSSimon Schubert static struct varobj *
21735796c8dcSSimon Schubert create_child_with_value (struct varobj *parent, int index, const char *name,
21745796c8dcSSimon Schubert 			 struct value *value)
21755796c8dcSSimon Schubert {
21765796c8dcSSimon Schubert   struct varobj *child;
21775796c8dcSSimon Schubert   char *childs_name;
21785796c8dcSSimon Schubert 
21795796c8dcSSimon Schubert   child = new_variable ();
21805796c8dcSSimon Schubert 
2181c50c785cSJohn Marino   /* Name is allocated by name_of_child.  */
21825796c8dcSSimon Schubert   /* FIXME: xstrdup should not be here.  */
21835796c8dcSSimon Schubert   child->name = xstrdup (name);
21845796c8dcSSimon Schubert   child->index = index;
21855796c8dcSSimon Schubert   child->parent = parent;
21865796c8dcSSimon Schubert   child->root = parent->root;
21875796c8dcSSimon Schubert   childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
21885796c8dcSSimon Schubert   child->obj_name = childs_name;
21895796c8dcSSimon Schubert   install_variable (child);
21905796c8dcSSimon Schubert 
21915796c8dcSSimon Schubert   /* Compute the type of the child.  Must do this before
21925796c8dcSSimon Schubert      calling install_new_value.  */
21935796c8dcSSimon Schubert   if (value != NULL)
21945796c8dcSSimon Schubert     /* If the child had no evaluation errors, var->value
21955796c8dcSSimon Schubert        will be non-NULL and contain a valid type.  */
21965796c8dcSSimon Schubert     child->type = value_type (value);
21975796c8dcSSimon Schubert   else
21985796c8dcSSimon Schubert     /* Otherwise, we must compute the type.  */
21995796c8dcSSimon Schubert     child->type = (*child->root->lang->type_of_child) (child->parent,
22005796c8dcSSimon Schubert 						       child->index);
22015796c8dcSSimon Schubert   install_new_value (child, value, 1);
22025796c8dcSSimon Schubert 
22035796c8dcSSimon Schubert   return child;
22045796c8dcSSimon Schubert }
22055796c8dcSSimon Schubert 
22065796c8dcSSimon Schubert 
22075796c8dcSSimon Schubert /*
22085796c8dcSSimon Schubert  * Miscellaneous utility functions.
22095796c8dcSSimon Schubert  */
22105796c8dcSSimon Schubert 
2211c50c785cSJohn Marino /* Allocate memory and initialize a new variable.  */
22125796c8dcSSimon Schubert static struct varobj *
22135796c8dcSSimon Schubert new_variable (void)
22145796c8dcSSimon Schubert {
22155796c8dcSSimon Schubert   struct varobj *var;
22165796c8dcSSimon Schubert 
22175796c8dcSSimon Schubert   var = (struct varobj *) xmalloc (sizeof (struct varobj));
22185796c8dcSSimon Schubert   var->name = NULL;
22195796c8dcSSimon Schubert   var->path_expr = NULL;
22205796c8dcSSimon Schubert   var->obj_name = NULL;
22215796c8dcSSimon Schubert   var->index = -1;
22225796c8dcSSimon Schubert   var->type = NULL;
22235796c8dcSSimon Schubert   var->value = NULL;
22245796c8dcSSimon Schubert   var->num_children = -1;
22255796c8dcSSimon Schubert   var->parent = NULL;
22265796c8dcSSimon Schubert   var->children = NULL;
22275796c8dcSSimon Schubert   var->format = 0;
22285796c8dcSSimon Schubert   var->root = NULL;
22295796c8dcSSimon Schubert   var->updated = 0;
22305796c8dcSSimon Schubert   var->print_value = NULL;
22315796c8dcSSimon Schubert   var->frozen = 0;
22325796c8dcSSimon Schubert   var->not_fetched = 0;
22335796c8dcSSimon Schubert   var->children_requested = 0;
22345796c8dcSSimon Schubert   var->from = -1;
22355796c8dcSSimon Schubert   var->to = -1;
22365796c8dcSSimon Schubert   var->constructor = 0;
22375796c8dcSSimon Schubert   var->pretty_printer = 0;
22385796c8dcSSimon Schubert   var->child_iter = 0;
22395796c8dcSSimon Schubert   var->saved_item = 0;
22405796c8dcSSimon Schubert 
22415796c8dcSSimon Schubert   return var;
22425796c8dcSSimon Schubert }
22435796c8dcSSimon Schubert 
2244c50c785cSJohn Marino /* Allocate memory and initialize a new root variable.  */
22455796c8dcSSimon Schubert static struct varobj *
22465796c8dcSSimon Schubert new_root_variable (void)
22475796c8dcSSimon Schubert {
22485796c8dcSSimon Schubert   struct varobj *var = new_variable ();
2249cf7f2e2dSJohn Marino 
2250c50c785cSJohn Marino   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
22515796c8dcSSimon Schubert   var->root->lang = NULL;
22525796c8dcSSimon Schubert   var->root->exp = NULL;
22535796c8dcSSimon Schubert   var->root->valid_block = NULL;
22545796c8dcSSimon Schubert   var->root->frame = null_frame_id;
22555796c8dcSSimon Schubert   var->root->floating = 0;
22565796c8dcSSimon Schubert   var->root->rootvar = NULL;
22575796c8dcSSimon Schubert   var->root->is_valid = 1;
22585796c8dcSSimon Schubert 
22595796c8dcSSimon Schubert   return var;
22605796c8dcSSimon Schubert }
22615796c8dcSSimon Schubert 
22625796c8dcSSimon Schubert /* Free any allocated memory associated with VAR.  */
22635796c8dcSSimon Schubert static void
22645796c8dcSSimon Schubert free_variable (struct varobj *var)
22655796c8dcSSimon Schubert {
22665796c8dcSSimon Schubert #if HAVE_PYTHON
22675796c8dcSSimon Schubert   if (var->pretty_printer)
22685796c8dcSSimon Schubert     {
22695796c8dcSSimon Schubert       struct cleanup *cleanup = varobj_ensure_python_env (var);
22705796c8dcSSimon Schubert       Py_XDECREF (var->constructor);
22715796c8dcSSimon Schubert       Py_XDECREF (var->pretty_printer);
22725796c8dcSSimon Schubert       Py_XDECREF (var->child_iter);
22735796c8dcSSimon Schubert       Py_XDECREF (var->saved_item);
22745796c8dcSSimon Schubert       do_cleanups (cleanup);
22755796c8dcSSimon Schubert     }
22765796c8dcSSimon Schubert #endif
22775796c8dcSSimon Schubert 
22785796c8dcSSimon Schubert   value_free (var->value);
22795796c8dcSSimon Schubert 
22805796c8dcSSimon Schubert   /* Free the expression if this is a root variable.  */
22815796c8dcSSimon Schubert   if (is_root_p (var))
22825796c8dcSSimon Schubert     {
22835796c8dcSSimon Schubert       xfree (var->root->exp);
22845796c8dcSSimon Schubert       xfree (var->root);
22855796c8dcSSimon Schubert     }
22865796c8dcSSimon Schubert 
22875796c8dcSSimon Schubert   xfree (var->name);
22885796c8dcSSimon Schubert   xfree (var->obj_name);
22895796c8dcSSimon Schubert   xfree (var->print_value);
22905796c8dcSSimon Schubert   xfree (var->path_expr);
22915796c8dcSSimon Schubert   xfree (var);
22925796c8dcSSimon Schubert }
22935796c8dcSSimon Schubert 
22945796c8dcSSimon Schubert static void
22955796c8dcSSimon Schubert do_free_variable_cleanup (void *var)
22965796c8dcSSimon Schubert {
22975796c8dcSSimon Schubert   free_variable (var);
22985796c8dcSSimon Schubert }
22995796c8dcSSimon Schubert 
23005796c8dcSSimon Schubert static struct cleanup *
23015796c8dcSSimon Schubert make_cleanup_free_variable (struct varobj *var)
23025796c8dcSSimon Schubert {
23035796c8dcSSimon Schubert   return make_cleanup (do_free_variable_cleanup, var);
23045796c8dcSSimon Schubert }
23055796c8dcSSimon Schubert 
23065796c8dcSSimon Schubert /* This returns the type of the variable.  It also skips past typedefs
23075796c8dcSSimon Schubert    to return the real type of the variable.
23085796c8dcSSimon Schubert 
23095796c8dcSSimon Schubert    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
23105796c8dcSSimon Schubert    except within get_target_type and get_type.  */
23115796c8dcSSimon Schubert static struct type *
23125796c8dcSSimon Schubert get_type (struct varobj *var)
23135796c8dcSSimon Schubert {
23145796c8dcSSimon Schubert   struct type *type;
23155796c8dcSSimon Schubert 
2316cf7f2e2dSJohn Marino   type = var->type;
23175796c8dcSSimon Schubert   if (type != NULL)
23185796c8dcSSimon Schubert     type = check_typedef (type);
23195796c8dcSSimon Schubert 
23205796c8dcSSimon Schubert   return type;
23215796c8dcSSimon Schubert }
23225796c8dcSSimon Schubert 
23235796c8dcSSimon Schubert /* Return the type of the value that's stored in VAR,
23245796c8dcSSimon Schubert    or that would have being stored there if the
23255796c8dcSSimon Schubert    value were accessible.
23265796c8dcSSimon Schubert 
23275796c8dcSSimon Schubert    This differs from VAR->type in that VAR->type is always
23285796c8dcSSimon Schubert    the true type of the expession in the source language.
23295796c8dcSSimon Schubert    The return value of this function is the type we're
23305796c8dcSSimon Schubert    actually storing in varobj, and using for displaying
23315796c8dcSSimon Schubert    the values and for comparing previous and new values.
23325796c8dcSSimon Schubert 
23335796c8dcSSimon Schubert    For example, top-level references are always stripped.  */
23345796c8dcSSimon Schubert static struct type *
23355796c8dcSSimon Schubert get_value_type (struct varobj *var)
23365796c8dcSSimon Schubert {
23375796c8dcSSimon Schubert   struct type *type;
23385796c8dcSSimon Schubert 
23395796c8dcSSimon Schubert   if (var->value)
23405796c8dcSSimon Schubert     type = value_type (var->value);
23415796c8dcSSimon Schubert   else
23425796c8dcSSimon Schubert     type = var->type;
23435796c8dcSSimon Schubert 
23445796c8dcSSimon Schubert   type = check_typedef (type);
23455796c8dcSSimon Schubert 
23465796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
23475796c8dcSSimon Schubert     type = get_target_type (type);
23485796c8dcSSimon Schubert 
23495796c8dcSSimon Schubert   type = check_typedef (type);
23505796c8dcSSimon Schubert 
23515796c8dcSSimon Schubert   return type;
23525796c8dcSSimon Schubert }
23535796c8dcSSimon Schubert 
23545796c8dcSSimon Schubert /* This returns the target type (or NULL) of TYPE, also skipping
23555796c8dcSSimon Schubert    past typedefs, just like get_type ().
23565796c8dcSSimon Schubert 
23575796c8dcSSimon Schubert    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
23585796c8dcSSimon Schubert    except within get_target_type and get_type.  */
23595796c8dcSSimon Schubert static struct type *
23605796c8dcSSimon Schubert get_target_type (struct type *type)
23615796c8dcSSimon Schubert {
23625796c8dcSSimon Schubert   if (type != NULL)
23635796c8dcSSimon Schubert     {
23645796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
23655796c8dcSSimon Schubert       if (type != NULL)
23665796c8dcSSimon Schubert 	type = check_typedef (type);
23675796c8dcSSimon Schubert     }
23685796c8dcSSimon Schubert 
23695796c8dcSSimon Schubert   return type;
23705796c8dcSSimon Schubert }
23715796c8dcSSimon Schubert 
23725796c8dcSSimon Schubert /* What is the default display for this variable? We assume that
23735796c8dcSSimon Schubert    everything is "natural".  Any exceptions?  */
23745796c8dcSSimon Schubert static enum varobj_display_formats
23755796c8dcSSimon Schubert variable_default_display (struct varobj *var)
23765796c8dcSSimon Schubert {
23775796c8dcSSimon Schubert   return FORMAT_NATURAL;
23785796c8dcSSimon Schubert }
23795796c8dcSSimon Schubert 
2380c50c785cSJohn Marino /* FIXME: The following should be generic for any pointer.  */
23815796c8dcSSimon Schubert static void
23825796c8dcSSimon Schubert cppush (struct cpstack **pstack, char *name)
23835796c8dcSSimon Schubert {
23845796c8dcSSimon Schubert   struct cpstack *s;
23855796c8dcSSimon Schubert 
23865796c8dcSSimon Schubert   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
23875796c8dcSSimon Schubert   s->name = name;
23885796c8dcSSimon Schubert   s->next = *pstack;
23895796c8dcSSimon Schubert   *pstack = s;
23905796c8dcSSimon Schubert }
23915796c8dcSSimon Schubert 
2392c50c785cSJohn Marino /* FIXME: The following should be generic for any pointer.  */
23935796c8dcSSimon Schubert static char *
23945796c8dcSSimon Schubert cppop (struct cpstack **pstack)
23955796c8dcSSimon Schubert {
23965796c8dcSSimon Schubert   struct cpstack *s;
23975796c8dcSSimon Schubert   char *v;
23985796c8dcSSimon Schubert 
23995796c8dcSSimon Schubert   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
24005796c8dcSSimon Schubert     return NULL;
24015796c8dcSSimon Schubert 
24025796c8dcSSimon Schubert   s = *pstack;
24035796c8dcSSimon Schubert   v = s->name;
24045796c8dcSSimon Schubert   *pstack = (*pstack)->next;
24055796c8dcSSimon Schubert   xfree (s);
24065796c8dcSSimon Schubert 
24075796c8dcSSimon Schubert   return v;
24085796c8dcSSimon Schubert }
24095796c8dcSSimon Schubert 
24105796c8dcSSimon Schubert /*
24115796c8dcSSimon Schubert  * Language-dependencies
24125796c8dcSSimon Schubert  */
24135796c8dcSSimon Schubert 
24145796c8dcSSimon Schubert /* Common entry points */
24155796c8dcSSimon Schubert 
24165796c8dcSSimon Schubert /* Get the language of variable VAR.  */
24175796c8dcSSimon Schubert static enum varobj_languages
24185796c8dcSSimon Schubert variable_language (struct varobj *var)
24195796c8dcSSimon Schubert {
24205796c8dcSSimon Schubert   enum varobj_languages lang;
24215796c8dcSSimon Schubert 
24225796c8dcSSimon Schubert   switch (var->root->exp->language_defn->la_language)
24235796c8dcSSimon Schubert     {
24245796c8dcSSimon Schubert     default:
24255796c8dcSSimon Schubert     case language_c:
24265796c8dcSSimon Schubert       lang = vlang_c;
24275796c8dcSSimon Schubert       break;
24285796c8dcSSimon Schubert     case language_cplus:
24295796c8dcSSimon Schubert       lang = vlang_cplus;
24305796c8dcSSimon Schubert       break;
24315796c8dcSSimon Schubert     case language_java:
24325796c8dcSSimon Schubert       lang = vlang_java;
24335796c8dcSSimon Schubert       break;
2434*a45ae5f8SJohn Marino     case language_ada:
2435*a45ae5f8SJohn Marino       lang = vlang_ada;
2436*a45ae5f8SJohn Marino       break;
24375796c8dcSSimon Schubert     }
24385796c8dcSSimon Schubert 
24395796c8dcSSimon Schubert   return lang;
24405796c8dcSSimon Schubert }
24415796c8dcSSimon Schubert 
24425796c8dcSSimon Schubert /* Return the number of children for a given variable.
24435796c8dcSSimon Schubert    The result of this function is defined by the language
24445796c8dcSSimon Schubert    implementation.  The number of children returned by this function
24455796c8dcSSimon Schubert    is the number of children that the user will see in the variable
24465796c8dcSSimon Schubert    display.  */
24475796c8dcSSimon Schubert static int
24485796c8dcSSimon Schubert number_of_children (struct varobj *var)
24495796c8dcSSimon Schubert {
2450c50c785cSJohn Marino   return (*var->root->lang->number_of_children) (var);
24515796c8dcSSimon Schubert }
24525796c8dcSSimon Schubert 
2453c50c785cSJohn Marino /* What is the expression for the root varobj VAR? Returns a malloc'd
2454c50c785cSJohn Marino    string.  */
24555796c8dcSSimon Schubert static char *
24565796c8dcSSimon Schubert name_of_variable (struct varobj *var)
24575796c8dcSSimon Schubert {
24585796c8dcSSimon Schubert   return (*var->root->lang->name_of_variable) (var);
24595796c8dcSSimon Schubert }
24605796c8dcSSimon Schubert 
2461c50c785cSJohn Marino /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2462c50c785cSJohn Marino    string.  */
24635796c8dcSSimon Schubert static char *
24645796c8dcSSimon Schubert name_of_child (struct varobj *var, int index)
24655796c8dcSSimon Schubert {
24665796c8dcSSimon Schubert   return (*var->root->lang->name_of_child) (var, index);
24675796c8dcSSimon Schubert }
24685796c8dcSSimon Schubert 
24695796c8dcSSimon Schubert /* What is the ``struct value *'' of the root variable VAR?
24705796c8dcSSimon Schubert    For floating variable object, evaluation can get us a value
24715796c8dcSSimon Schubert    of different type from what is stored in varobj already.  In
24725796c8dcSSimon Schubert    that case:
24735796c8dcSSimon Schubert    - *type_changed will be set to 1
24745796c8dcSSimon Schubert    - old varobj will be freed, and new one will be
24755796c8dcSSimon Schubert    created, with the same name.
24765796c8dcSSimon Schubert    - *var_handle will be set to the new varobj
24775796c8dcSSimon Schubert    Otherwise, *type_changed will be set to 0.  */
24785796c8dcSSimon Schubert static struct value *
24795796c8dcSSimon Schubert value_of_root (struct varobj **var_handle, int *type_changed)
24805796c8dcSSimon Schubert {
24815796c8dcSSimon Schubert   struct varobj *var;
24825796c8dcSSimon Schubert 
24835796c8dcSSimon Schubert   if (var_handle == NULL)
24845796c8dcSSimon Schubert     return NULL;
24855796c8dcSSimon Schubert 
24865796c8dcSSimon Schubert   var = *var_handle;
24875796c8dcSSimon Schubert 
24885796c8dcSSimon Schubert   /* This should really be an exception, since this should
24895796c8dcSSimon Schubert      only get called with a root variable.  */
24905796c8dcSSimon Schubert 
24915796c8dcSSimon Schubert   if (!is_root_p (var))
24925796c8dcSSimon Schubert     return NULL;
24935796c8dcSSimon Schubert 
24945796c8dcSSimon Schubert   if (var->root->floating)
24955796c8dcSSimon Schubert     {
24965796c8dcSSimon Schubert       struct varobj *tmp_var;
24975796c8dcSSimon Schubert       char *old_type, *new_type;
24985796c8dcSSimon Schubert 
24995796c8dcSSimon Schubert       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
25005796c8dcSSimon Schubert 			       USE_SELECTED_FRAME);
25015796c8dcSSimon Schubert       if (tmp_var == NULL)
25025796c8dcSSimon Schubert 	{
25035796c8dcSSimon Schubert 	  return NULL;
25045796c8dcSSimon Schubert 	}
25055796c8dcSSimon Schubert       old_type = varobj_get_type (var);
25065796c8dcSSimon Schubert       new_type = varobj_get_type (tmp_var);
25075796c8dcSSimon Schubert       if (strcmp (old_type, new_type) == 0)
25085796c8dcSSimon Schubert 	{
25095796c8dcSSimon Schubert 	  /* The expression presently stored inside var->root->exp
25105796c8dcSSimon Schubert 	     remembers the locations of local variables relatively to
25115796c8dcSSimon Schubert 	     the frame where the expression was created (in DWARF location
25125796c8dcSSimon Schubert 	     button, for example).  Naturally, those locations are not
25135796c8dcSSimon Schubert 	     correct in other frames, so update the expression.  */
25145796c8dcSSimon Schubert 
25155796c8dcSSimon Schubert          struct expression *tmp_exp = var->root->exp;
2516cf7f2e2dSJohn Marino 
25175796c8dcSSimon Schubert          var->root->exp = tmp_var->root->exp;
25185796c8dcSSimon Schubert          tmp_var->root->exp = tmp_exp;
25195796c8dcSSimon Schubert 
25205796c8dcSSimon Schubert 	  varobj_delete (tmp_var, NULL, 0);
25215796c8dcSSimon Schubert 	  *type_changed = 0;
25225796c8dcSSimon Schubert 	}
25235796c8dcSSimon Schubert       else
25245796c8dcSSimon Schubert 	{
25255796c8dcSSimon Schubert 	  tmp_var->obj_name = xstrdup (var->obj_name);
25265796c8dcSSimon Schubert 	  tmp_var->from = var->from;
25275796c8dcSSimon Schubert 	  tmp_var->to = var->to;
25285796c8dcSSimon Schubert 	  varobj_delete (var, NULL, 0);
25295796c8dcSSimon Schubert 
25305796c8dcSSimon Schubert 	  install_variable (tmp_var);
25315796c8dcSSimon Schubert 	  *var_handle = tmp_var;
25325796c8dcSSimon Schubert 	  var = *var_handle;
25335796c8dcSSimon Schubert 	  *type_changed = 1;
25345796c8dcSSimon Schubert 	}
25355796c8dcSSimon Schubert       xfree (old_type);
25365796c8dcSSimon Schubert       xfree (new_type);
25375796c8dcSSimon Schubert     }
25385796c8dcSSimon Schubert   else
25395796c8dcSSimon Schubert     {
25405796c8dcSSimon Schubert       *type_changed = 0;
25415796c8dcSSimon Schubert     }
25425796c8dcSSimon Schubert 
25435796c8dcSSimon Schubert   return (*var->root->lang->value_of_root) (var_handle);
25445796c8dcSSimon Schubert }
25455796c8dcSSimon Schubert 
25465796c8dcSSimon Schubert /* What is the ``struct value *'' for the INDEX'th child of PARENT?  */
25475796c8dcSSimon Schubert static struct value *
25485796c8dcSSimon Schubert value_of_child (struct varobj *parent, int index)
25495796c8dcSSimon Schubert {
25505796c8dcSSimon Schubert   struct value *value;
25515796c8dcSSimon Schubert 
25525796c8dcSSimon Schubert   value = (*parent->root->lang->value_of_child) (parent, index);
25535796c8dcSSimon Schubert 
25545796c8dcSSimon Schubert   return value;
25555796c8dcSSimon Schubert }
25565796c8dcSSimon Schubert 
25575796c8dcSSimon Schubert /* GDB already has a command called "value_of_variable".  Sigh.  */
25585796c8dcSSimon Schubert static char *
25595796c8dcSSimon Schubert my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
25605796c8dcSSimon Schubert {
25615796c8dcSSimon Schubert   if (var->root->is_valid)
25625796c8dcSSimon Schubert     {
25635796c8dcSSimon Schubert       if (var->pretty_printer)
25645796c8dcSSimon Schubert 	return value_get_print_value (var->value, var->format, var);
25655796c8dcSSimon Schubert       return (*var->root->lang->value_of_variable) (var, format);
25665796c8dcSSimon Schubert     }
25675796c8dcSSimon Schubert   else
25685796c8dcSSimon Schubert     return NULL;
25695796c8dcSSimon Schubert }
25705796c8dcSSimon Schubert 
25715796c8dcSSimon Schubert static char *
25725796c8dcSSimon Schubert value_get_print_value (struct value *value, enum varobj_display_formats format,
25735796c8dcSSimon Schubert 		       struct varobj *var)
25745796c8dcSSimon Schubert {
25755796c8dcSSimon Schubert   struct ui_file *stb;
25765796c8dcSSimon Schubert   struct cleanup *old_chain;
25775796c8dcSSimon Schubert   gdb_byte *thevalue = NULL;
25785796c8dcSSimon Schubert   struct value_print_options opts;
2579cf7f2e2dSJohn Marino   struct type *type = NULL;
2580cf7f2e2dSJohn Marino   long len = 0;
2581cf7f2e2dSJohn Marino   char *encoding = NULL;
2582cf7f2e2dSJohn Marino   struct gdbarch *gdbarch = NULL;
2583c50c785cSJohn Marino   /* Initialize it just to avoid a GCC false warning.  */
2584c50c785cSJohn Marino   CORE_ADDR str_addr = 0;
2585c50c785cSJohn Marino   int string_print = 0;
25865796c8dcSSimon Schubert 
25875796c8dcSSimon Schubert   if (value == NULL)
25885796c8dcSSimon Schubert     return NULL;
25895796c8dcSSimon Schubert 
2590c50c785cSJohn Marino   stb = mem_fileopen ();
2591c50c785cSJohn Marino   old_chain = make_cleanup_ui_file_delete (stb);
2592c50c785cSJohn Marino 
2593cf7f2e2dSJohn Marino   gdbarch = get_type_arch (value_type (value));
25945796c8dcSSimon Schubert #if HAVE_PYTHON
25955796c8dcSSimon Schubert   {
25965796c8dcSSimon Schubert     PyObject *value_formatter = var->pretty_printer;
25975796c8dcSSimon Schubert 
2598c50c785cSJohn Marino     varobj_ensure_python_env (var);
2599c50c785cSJohn Marino 
26005796c8dcSSimon Schubert     if (value_formatter)
26015796c8dcSSimon Schubert       {
26025796c8dcSSimon Schubert 	/* First check to see if we have any children at all.  If so,
26035796c8dcSSimon Schubert 	   we simply return {...}.  */
26045796c8dcSSimon Schubert 	if (dynamic_varobj_has_child_method (var))
2605c50c785cSJohn Marino 	  {
2606c50c785cSJohn Marino 	    do_cleanups (old_chain);
26075796c8dcSSimon Schubert 	    return xstrdup ("{...}");
2608c50c785cSJohn Marino 	  }
26095796c8dcSSimon Schubert 
26105796c8dcSSimon Schubert 	if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
26115796c8dcSSimon Schubert 	  {
26125796c8dcSSimon Schubert 	    struct value *replacement;
26135796c8dcSSimon Schubert 	    PyObject *output = NULL;
26145796c8dcSSimon Schubert 
26155796c8dcSSimon Schubert 	    output = apply_varobj_pretty_printer (value_formatter,
2616c50c785cSJohn Marino 						  &replacement,
2617c50c785cSJohn Marino 						  stb);
2618*a45ae5f8SJohn Marino 
2619*a45ae5f8SJohn Marino 	    /* If we have string like output ...  */
26205796c8dcSSimon Schubert 	    if (output)
26215796c8dcSSimon Schubert 	      {
2622c50c785cSJohn Marino 		make_cleanup_py_decref (output);
2623c50c785cSJohn Marino 
2624*a45ae5f8SJohn Marino 		/* If this is a lazy string, extract it.  For lazy
2625*a45ae5f8SJohn Marino 		   strings we always print as a string, so set
2626*a45ae5f8SJohn Marino 		   string_print.  */
2627cf7f2e2dSJohn Marino 		if (gdbpy_is_lazy_string (output))
2628cf7f2e2dSJohn Marino 		  {
2629c50c785cSJohn Marino 		    gdbpy_extract_lazy_string (output, &str_addr, &type,
2630cf7f2e2dSJohn Marino 					       &len, &encoding);
2631c50c785cSJohn Marino 		    make_cleanup (free_current_contents, &encoding);
2632cf7f2e2dSJohn Marino 		    string_print = 1;
2633cf7f2e2dSJohn Marino 		  }
2634cf7f2e2dSJohn Marino 		else
2635cf7f2e2dSJohn Marino 		  {
2636*a45ae5f8SJohn Marino 		    /* If it is a regular (non-lazy) string, extract
2637*a45ae5f8SJohn Marino 		       it and copy the contents into THEVALUE.  If the
2638*a45ae5f8SJohn Marino 		       hint says to print it as a string, set
2639*a45ae5f8SJohn Marino 		       string_print.  Otherwise just return the extracted
2640*a45ae5f8SJohn Marino 		       string as a value.  */
2641*a45ae5f8SJohn Marino 
26425796c8dcSSimon Schubert 		    PyObject *py_str
26435796c8dcSSimon Schubert 		      = python_string_to_target_python_string (output);
2644cf7f2e2dSJohn Marino 
26455796c8dcSSimon Schubert 		    if (py_str)
26465796c8dcSSimon Schubert 		      {
26475796c8dcSSimon Schubert 			char *s = PyString_AsString (py_str);
2648*a45ae5f8SJohn Marino 			char *hint;
2649*a45ae5f8SJohn Marino 
2650*a45ae5f8SJohn Marino 			hint = gdbpy_get_display_hint (value_formatter);
2651*a45ae5f8SJohn Marino 			if (hint)
2652*a45ae5f8SJohn Marino 			  {
2653*a45ae5f8SJohn Marino 			    if (!strcmp (hint, "string"))
2654*a45ae5f8SJohn Marino 			      string_print = 1;
2655*a45ae5f8SJohn Marino 			    xfree (hint);
2656*a45ae5f8SJohn Marino 			  }
2657cf7f2e2dSJohn Marino 
26585796c8dcSSimon Schubert 			len = PyString_Size (py_str);
26595796c8dcSSimon Schubert 			thevalue = xmemdup (s, len + 1, len + 1);
2660cf7f2e2dSJohn Marino 			type = builtin_type (gdbarch)->builtin_char;
26615796c8dcSSimon Schubert 			Py_DECREF (py_str);
2662c50c785cSJohn Marino 
2663c50c785cSJohn Marino 			if (!string_print)
26645796c8dcSSimon Schubert 			  {
2665c50c785cSJohn Marino 			    do_cleanups (old_chain);
26665796c8dcSSimon Schubert 			    return thevalue;
26675796c8dcSSimon Schubert 			  }
2668c50c785cSJohn Marino 
2669c50c785cSJohn Marino 			make_cleanup (xfree, thevalue);
2670c50c785cSJohn Marino 		      }
2671c50c785cSJohn Marino 		    else
2672c50c785cSJohn Marino 		      gdbpy_print_stack ();
2673c50c785cSJohn Marino 		  }
2674c50c785cSJohn Marino 	      }
2675*a45ae5f8SJohn Marino 	    /* If the printer returned a replacement value, set VALUE
2676*a45ae5f8SJohn Marino 	       to REPLACEMENT.  If there is not a replacement value,
2677*a45ae5f8SJohn Marino 	       just use the value passed to this function.  */
26785796c8dcSSimon Schubert 	    if (replacement)
26795796c8dcSSimon Schubert 	      value = replacement;
26805796c8dcSSimon Schubert 	  }
26815796c8dcSSimon Schubert       }
26825796c8dcSSimon Schubert   }
26835796c8dcSSimon Schubert #endif
26845796c8dcSSimon Schubert 
26855796c8dcSSimon Schubert   get_formatted_print_options (&opts, format_code[(int) format]);
26865796c8dcSSimon Schubert   opts.deref_ref = 0;
26875796c8dcSSimon Schubert   opts.raw = 1;
2688*a45ae5f8SJohn Marino 
2689*a45ae5f8SJohn Marino   /* If the THEVALUE has contents, it is a regular string.  */
26905796c8dcSSimon Schubert   if (thevalue)
2691cf7f2e2dSJohn Marino     LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2692c50c785cSJohn Marino   else if (string_print)
2693*a45ae5f8SJohn Marino     /* Otherwise, if string_print is set, and it is not a regular
2694*a45ae5f8SJohn Marino        string, it is a lazy string.  */
2695c50c785cSJohn Marino     val_print_string (type, encoding, str_addr, len, stb, &opts);
26965796c8dcSSimon Schubert   else
2697*a45ae5f8SJohn Marino     /* All other cases.  */
26985796c8dcSSimon Schubert     common_val_print (value, stb, 0, &opts, current_language);
2699*a45ae5f8SJohn Marino 
27005796c8dcSSimon Schubert   thevalue = ui_file_xstrdup (stb, NULL);
27015796c8dcSSimon Schubert 
27025796c8dcSSimon Schubert   do_cleanups (old_chain);
27035796c8dcSSimon Schubert   return thevalue;
27045796c8dcSSimon Schubert }
27055796c8dcSSimon Schubert 
27065796c8dcSSimon Schubert int
27075796c8dcSSimon Schubert varobj_editable_p (struct varobj *var)
27085796c8dcSSimon Schubert {
27095796c8dcSSimon Schubert   struct type *type;
27105796c8dcSSimon Schubert 
27115796c8dcSSimon Schubert   if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
27125796c8dcSSimon Schubert     return 0;
27135796c8dcSSimon Schubert 
27145796c8dcSSimon Schubert   type = get_value_type (var);
27155796c8dcSSimon Schubert 
27165796c8dcSSimon Schubert   switch (TYPE_CODE (type))
27175796c8dcSSimon Schubert     {
27185796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
27195796c8dcSSimon Schubert     case TYPE_CODE_UNION:
27205796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
27215796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
27225796c8dcSSimon Schubert     case TYPE_CODE_METHOD:
27235796c8dcSSimon Schubert       return 0;
27245796c8dcSSimon Schubert       break;
27255796c8dcSSimon Schubert 
27265796c8dcSSimon Schubert     default:
27275796c8dcSSimon Schubert       return 1;
27285796c8dcSSimon Schubert       break;
27295796c8dcSSimon Schubert     }
27305796c8dcSSimon Schubert }
27315796c8dcSSimon Schubert 
27325796c8dcSSimon Schubert /* Return non-zero if changes in value of VAR
27335796c8dcSSimon Schubert    must be detected and reported by -var-update.
27345796c8dcSSimon Schubert    Return zero is -var-update should never report
27355796c8dcSSimon Schubert    changes of such values.  This makes sense for structures
27365796c8dcSSimon Schubert    (since the changes in children values will be reported separately),
27375796c8dcSSimon Schubert    or for artifical objects (like 'public' pseudo-field in C++).
27385796c8dcSSimon Schubert 
27395796c8dcSSimon Schubert    Return value of 0 means that gdb need not call value_fetch_lazy
27405796c8dcSSimon Schubert    for the value of this variable object.  */
27415796c8dcSSimon Schubert static int
27425796c8dcSSimon Schubert varobj_value_is_changeable_p (struct varobj *var)
27435796c8dcSSimon Schubert {
27445796c8dcSSimon Schubert   int r;
27455796c8dcSSimon Schubert   struct type *type;
27465796c8dcSSimon Schubert 
27475796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var))
27485796c8dcSSimon Schubert     return 0;
27495796c8dcSSimon Schubert 
27505796c8dcSSimon Schubert   type = get_value_type (var);
27515796c8dcSSimon Schubert 
27525796c8dcSSimon Schubert   switch (TYPE_CODE (type))
27535796c8dcSSimon Schubert     {
27545796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
27555796c8dcSSimon Schubert     case TYPE_CODE_UNION:
27565796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
27575796c8dcSSimon Schubert       r = 0;
27585796c8dcSSimon Schubert       break;
27595796c8dcSSimon Schubert 
27605796c8dcSSimon Schubert     default:
27615796c8dcSSimon Schubert       r = 1;
27625796c8dcSSimon Schubert     }
27635796c8dcSSimon Schubert 
27645796c8dcSSimon Schubert   return r;
27655796c8dcSSimon Schubert }
27665796c8dcSSimon Schubert 
27675796c8dcSSimon Schubert /* Return 1 if that varobj is floating, that is is always evaluated in the
27685796c8dcSSimon Schubert    selected frame, and not bound to thread/frame.  Such variable objects
27695796c8dcSSimon Schubert    are created using '@' as frame specifier to -var-create.  */
27705796c8dcSSimon Schubert int
27715796c8dcSSimon Schubert varobj_floating_p (struct varobj *var)
27725796c8dcSSimon Schubert {
27735796c8dcSSimon Schubert   return var->root->floating;
27745796c8dcSSimon Schubert }
27755796c8dcSSimon Schubert 
27765796c8dcSSimon Schubert /* Given the value and the type of a variable object,
27775796c8dcSSimon Schubert    adjust the value and type to those necessary
27785796c8dcSSimon Schubert    for getting children of the variable object.
27795796c8dcSSimon Schubert    This includes dereferencing top-level references
27805796c8dcSSimon Schubert    to all types and dereferencing pointers to
27815796c8dcSSimon Schubert    structures.
27825796c8dcSSimon Schubert 
27835796c8dcSSimon Schubert    Both TYPE and *TYPE should be non-null.  VALUE
27845796c8dcSSimon Schubert    can be null if we want to only translate type.
27855796c8dcSSimon Schubert    *VALUE can be null as well -- if the parent
27865796c8dcSSimon Schubert    value is not known.
27875796c8dcSSimon Schubert 
27885796c8dcSSimon Schubert    If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
27895796c8dcSSimon Schubert    depending on whether pointer was dereferenced
27905796c8dcSSimon Schubert    in this function.  */
27915796c8dcSSimon Schubert static void
27925796c8dcSSimon Schubert adjust_value_for_child_access (struct value **value,
27935796c8dcSSimon Schubert 				  struct type **type,
27945796c8dcSSimon Schubert 				  int *was_ptr)
27955796c8dcSSimon Schubert {
27965796c8dcSSimon Schubert   gdb_assert (type && *type);
27975796c8dcSSimon Schubert 
27985796c8dcSSimon Schubert   if (was_ptr)
27995796c8dcSSimon Schubert     *was_ptr = 0;
28005796c8dcSSimon Schubert 
28015796c8dcSSimon Schubert   *type = check_typedef (*type);
28025796c8dcSSimon Schubert 
28035796c8dcSSimon Schubert   /* The type of value stored in varobj, that is passed
28045796c8dcSSimon Schubert      to us, is already supposed to be
28055796c8dcSSimon Schubert      reference-stripped.  */
28065796c8dcSSimon Schubert 
28075796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
28085796c8dcSSimon Schubert 
28095796c8dcSSimon Schubert   /* Pointers to structures are treated just like
28105796c8dcSSimon Schubert      structures when accessing children.  Don't
28115796c8dcSSimon Schubert      dererences pointers to other types.  */
28125796c8dcSSimon Schubert   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
28135796c8dcSSimon Schubert     {
28145796c8dcSSimon Schubert       struct type *target_type = get_target_type (*type);
28155796c8dcSSimon Schubert       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
28165796c8dcSSimon Schubert 	  || TYPE_CODE (target_type) == TYPE_CODE_UNION)
28175796c8dcSSimon Schubert 	{
28185796c8dcSSimon Schubert 	  if (value && *value)
28195796c8dcSSimon Schubert 	    {
28205796c8dcSSimon Schubert 	      int success = gdb_value_ind (*value, value);
2821cf7f2e2dSJohn Marino 
28225796c8dcSSimon Schubert 	      if (!success)
28235796c8dcSSimon Schubert 		*value = NULL;
28245796c8dcSSimon Schubert 	    }
28255796c8dcSSimon Schubert 	  *type = target_type;
28265796c8dcSSimon Schubert 	  if (was_ptr)
28275796c8dcSSimon Schubert 	    *was_ptr = 1;
28285796c8dcSSimon Schubert 	}
28295796c8dcSSimon Schubert     }
28305796c8dcSSimon Schubert 
28315796c8dcSSimon Schubert   /* The 'get_target_type' function calls check_typedef on
28325796c8dcSSimon Schubert      result, so we can immediately check type code.  No
28335796c8dcSSimon Schubert      need to call check_typedef here.  */
28345796c8dcSSimon Schubert }
28355796c8dcSSimon Schubert 
28365796c8dcSSimon Schubert /* C */
28375796c8dcSSimon Schubert static int
28385796c8dcSSimon Schubert c_number_of_children (struct varobj *var)
28395796c8dcSSimon Schubert {
28405796c8dcSSimon Schubert   struct type *type = get_value_type (var);
28415796c8dcSSimon Schubert   int children = 0;
28425796c8dcSSimon Schubert   struct type *target;
28435796c8dcSSimon Schubert 
28445796c8dcSSimon Schubert   adjust_value_for_child_access (NULL, &type, NULL);
28455796c8dcSSimon Schubert   target = get_target_type (type);
28465796c8dcSSimon Schubert 
28475796c8dcSSimon Schubert   switch (TYPE_CODE (type))
28485796c8dcSSimon Schubert     {
28495796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
28505796c8dcSSimon Schubert       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
28515796c8dcSSimon Schubert 	  && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
28525796c8dcSSimon Schubert 	children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
28535796c8dcSSimon Schubert       else
28545796c8dcSSimon Schubert 	/* If we don't know how many elements there are, don't display
28555796c8dcSSimon Schubert 	   any.  */
28565796c8dcSSimon Schubert 	children = 0;
28575796c8dcSSimon Schubert       break;
28585796c8dcSSimon Schubert 
28595796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
28605796c8dcSSimon Schubert     case TYPE_CODE_UNION:
28615796c8dcSSimon Schubert       children = TYPE_NFIELDS (type);
28625796c8dcSSimon Schubert       break;
28635796c8dcSSimon Schubert 
28645796c8dcSSimon Schubert     case TYPE_CODE_PTR:
28655796c8dcSSimon Schubert       /* The type here is a pointer to non-struct.  Typically, pointers
28665796c8dcSSimon Schubert 	 have one child, except for function ptrs, which have no children,
28675796c8dcSSimon Schubert 	 and except for void*, as we don't know what to show.
28685796c8dcSSimon Schubert 
28695796c8dcSSimon Schubert          We can show char* so we allow it to be dereferenced.  If you decide
28705796c8dcSSimon Schubert          to test for it, please mind that a little magic is necessary to
28715796c8dcSSimon Schubert          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
2872c50c785cSJohn Marino          TYPE_NAME == "char".  */
28735796c8dcSSimon Schubert       if (TYPE_CODE (target) == TYPE_CODE_FUNC
28745796c8dcSSimon Schubert 	  || TYPE_CODE (target) == TYPE_CODE_VOID)
28755796c8dcSSimon Schubert 	children = 0;
28765796c8dcSSimon Schubert       else
28775796c8dcSSimon Schubert 	children = 1;
28785796c8dcSSimon Schubert       break;
28795796c8dcSSimon Schubert 
28805796c8dcSSimon Schubert     default:
2881c50c785cSJohn Marino       /* Other types have no children.  */
28825796c8dcSSimon Schubert       break;
28835796c8dcSSimon Schubert     }
28845796c8dcSSimon Schubert 
28855796c8dcSSimon Schubert   return children;
28865796c8dcSSimon Schubert }
28875796c8dcSSimon Schubert 
28885796c8dcSSimon Schubert static char *
28895796c8dcSSimon Schubert c_name_of_variable (struct varobj *parent)
28905796c8dcSSimon Schubert {
28915796c8dcSSimon Schubert   return xstrdup (parent->name);
28925796c8dcSSimon Schubert }
28935796c8dcSSimon Schubert 
28945796c8dcSSimon Schubert /* Return the value of element TYPE_INDEX of a structure
28955796c8dcSSimon Schubert    value VALUE.  VALUE's type should be a structure,
28965796c8dcSSimon Schubert    or union, or a typedef to struct/union.
28975796c8dcSSimon Schubert 
28985796c8dcSSimon Schubert    Returns NULL if getting the value fails.  Never throws.  */
28995796c8dcSSimon Schubert static struct value *
29005796c8dcSSimon Schubert value_struct_element_index (struct value *value, int type_index)
29015796c8dcSSimon Schubert {
29025796c8dcSSimon Schubert   struct value *result = NULL;
29035796c8dcSSimon Schubert   volatile struct gdb_exception e;
29045796c8dcSSimon Schubert   struct type *type = value_type (value);
2905cf7f2e2dSJohn Marino 
29065796c8dcSSimon Schubert   type = check_typedef (type);
29075796c8dcSSimon Schubert 
29085796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
29095796c8dcSSimon Schubert 	      || TYPE_CODE (type) == TYPE_CODE_UNION);
29105796c8dcSSimon Schubert 
29115796c8dcSSimon Schubert   TRY_CATCH (e, RETURN_MASK_ERROR)
29125796c8dcSSimon Schubert     {
29135796c8dcSSimon Schubert       if (field_is_static (&TYPE_FIELD (type, type_index)))
29145796c8dcSSimon Schubert 	result = value_static_field (type, type_index);
29155796c8dcSSimon Schubert       else
29165796c8dcSSimon Schubert 	result = value_primitive_field (value, 0, type_index, type);
29175796c8dcSSimon Schubert     }
29185796c8dcSSimon Schubert   if (e.reason < 0)
29195796c8dcSSimon Schubert     {
29205796c8dcSSimon Schubert       return NULL;
29215796c8dcSSimon Schubert     }
29225796c8dcSSimon Schubert   else
29235796c8dcSSimon Schubert     {
29245796c8dcSSimon Schubert       return result;
29255796c8dcSSimon Schubert     }
29265796c8dcSSimon Schubert }
29275796c8dcSSimon Schubert 
29285796c8dcSSimon Schubert /* Obtain the information about child INDEX of the variable
29295796c8dcSSimon Schubert    object PARENT.
29305796c8dcSSimon Schubert    If CNAME is not null, sets *CNAME to the name of the child relative
29315796c8dcSSimon Schubert    to the parent.
29325796c8dcSSimon Schubert    If CVALUE is not null, sets *CVALUE to the value of the child.
29335796c8dcSSimon Schubert    If CTYPE is not null, sets *CTYPE to the type of the child.
29345796c8dcSSimon Schubert 
29355796c8dcSSimon Schubert    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
29365796c8dcSSimon Schubert    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
29375796c8dcSSimon Schubert    to NULL.  */
29385796c8dcSSimon Schubert static void
29395796c8dcSSimon Schubert c_describe_child (struct varobj *parent, int index,
29405796c8dcSSimon Schubert 		  char **cname, struct value **cvalue, struct type **ctype,
29415796c8dcSSimon Schubert 		  char **cfull_expression)
29425796c8dcSSimon Schubert {
29435796c8dcSSimon Schubert   struct value *value = parent->value;
29445796c8dcSSimon Schubert   struct type *type = get_value_type (parent);
29455796c8dcSSimon Schubert   char *parent_expression = NULL;
29465796c8dcSSimon Schubert   int was_ptr;
29475796c8dcSSimon Schubert 
29485796c8dcSSimon Schubert   if (cname)
29495796c8dcSSimon Schubert     *cname = NULL;
29505796c8dcSSimon Schubert   if (cvalue)
29515796c8dcSSimon Schubert     *cvalue = NULL;
29525796c8dcSSimon Schubert   if (ctype)
29535796c8dcSSimon Schubert     *ctype = NULL;
29545796c8dcSSimon Schubert   if (cfull_expression)
29555796c8dcSSimon Schubert     {
29565796c8dcSSimon Schubert       *cfull_expression = NULL;
29575796c8dcSSimon Schubert       parent_expression = varobj_get_path_expr (parent);
29585796c8dcSSimon Schubert     }
29595796c8dcSSimon Schubert   adjust_value_for_child_access (&value, &type, &was_ptr);
29605796c8dcSSimon Schubert 
29615796c8dcSSimon Schubert   switch (TYPE_CODE (type))
29625796c8dcSSimon Schubert     {
29635796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
29645796c8dcSSimon Schubert       if (cname)
2965c50c785cSJohn Marino 	*cname
2966c50c785cSJohn Marino 	  = xstrdup (int_string (index
2967cf7f2e2dSJohn Marino 				 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
2968cf7f2e2dSJohn Marino 				 10, 1, 0, 0));
29695796c8dcSSimon Schubert 
29705796c8dcSSimon Schubert       if (cvalue && value)
29715796c8dcSSimon Schubert 	{
29725796c8dcSSimon Schubert 	  int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2973cf7f2e2dSJohn Marino 
29745796c8dcSSimon Schubert 	  gdb_value_subscript (value, real_index, cvalue);
29755796c8dcSSimon Schubert 	}
29765796c8dcSSimon Schubert 
29775796c8dcSSimon Schubert       if (ctype)
29785796c8dcSSimon Schubert 	*ctype = get_target_type (type);
29795796c8dcSSimon Schubert 
29805796c8dcSSimon Schubert       if (cfull_expression)
2981cf7f2e2dSJohn Marino 	*cfull_expression =
2982cf7f2e2dSJohn Marino 	  xstrprintf ("(%s)[%s]", parent_expression,
2983cf7f2e2dSJohn Marino 		      int_string (index
2984cf7f2e2dSJohn Marino 				  + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
2985cf7f2e2dSJohn Marino 				  10, 1, 0, 0));
29865796c8dcSSimon Schubert 
29875796c8dcSSimon Schubert 
29885796c8dcSSimon Schubert       break;
29895796c8dcSSimon Schubert 
29905796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
29915796c8dcSSimon Schubert     case TYPE_CODE_UNION:
29925796c8dcSSimon Schubert       if (cname)
29935796c8dcSSimon Schubert 	*cname = xstrdup (TYPE_FIELD_NAME (type, index));
29945796c8dcSSimon Schubert 
29955796c8dcSSimon Schubert       if (cvalue && value)
29965796c8dcSSimon Schubert 	{
29975796c8dcSSimon Schubert 	  /* For C, varobj index is the same as type index.  */
29985796c8dcSSimon Schubert 	  *cvalue = value_struct_element_index (value, index);
29995796c8dcSSimon Schubert 	}
30005796c8dcSSimon Schubert 
30015796c8dcSSimon Schubert       if (ctype)
30025796c8dcSSimon Schubert 	*ctype = TYPE_FIELD_TYPE (type, index);
30035796c8dcSSimon Schubert 
30045796c8dcSSimon Schubert       if (cfull_expression)
30055796c8dcSSimon Schubert 	{
30065796c8dcSSimon Schubert 	  char *join = was_ptr ? "->" : ".";
3007cf7f2e2dSJohn Marino 
30085796c8dcSSimon Schubert 	  *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
30095796c8dcSSimon Schubert 					  TYPE_FIELD_NAME (type, index));
30105796c8dcSSimon Schubert 	}
30115796c8dcSSimon Schubert 
30125796c8dcSSimon Schubert       break;
30135796c8dcSSimon Schubert 
30145796c8dcSSimon Schubert     case TYPE_CODE_PTR:
30155796c8dcSSimon Schubert       if (cname)
30165796c8dcSSimon Schubert 	*cname = xstrprintf ("*%s", parent->name);
30175796c8dcSSimon Schubert 
30185796c8dcSSimon Schubert       if (cvalue && value)
30195796c8dcSSimon Schubert 	{
30205796c8dcSSimon Schubert 	  int success = gdb_value_ind (value, cvalue);
3021cf7f2e2dSJohn Marino 
30225796c8dcSSimon Schubert 	  if (!success)
30235796c8dcSSimon Schubert 	    *cvalue = NULL;
30245796c8dcSSimon Schubert 	}
30255796c8dcSSimon Schubert 
30265796c8dcSSimon Schubert       /* Don't use get_target_type because it calls
30275796c8dcSSimon Schubert 	 check_typedef and here, we want to show the true
30285796c8dcSSimon Schubert 	 declared type of the variable.  */
30295796c8dcSSimon Schubert       if (ctype)
30305796c8dcSSimon Schubert 	*ctype = TYPE_TARGET_TYPE (type);
30315796c8dcSSimon Schubert 
30325796c8dcSSimon Schubert       if (cfull_expression)
30335796c8dcSSimon Schubert 	*cfull_expression = xstrprintf ("*(%s)", parent_expression);
30345796c8dcSSimon Schubert 
30355796c8dcSSimon Schubert       break;
30365796c8dcSSimon Schubert 
30375796c8dcSSimon Schubert     default:
3038c50c785cSJohn Marino       /* This should not happen.  */
30395796c8dcSSimon Schubert       if (cname)
30405796c8dcSSimon Schubert 	*cname = xstrdup ("???");
30415796c8dcSSimon Schubert       if (cfull_expression)
30425796c8dcSSimon Schubert 	*cfull_expression = xstrdup ("???");
30435796c8dcSSimon Schubert       /* Don't set value and type, we don't know then.  */
30445796c8dcSSimon Schubert     }
30455796c8dcSSimon Schubert }
30465796c8dcSSimon Schubert 
30475796c8dcSSimon Schubert static char *
30485796c8dcSSimon Schubert c_name_of_child (struct varobj *parent, int index)
30495796c8dcSSimon Schubert {
30505796c8dcSSimon Schubert   char *name;
3051cf7f2e2dSJohn Marino 
30525796c8dcSSimon Schubert   c_describe_child (parent, index, &name, NULL, NULL, NULL);
30535796c8dcSSimon Schubert   return name;
30545796c8dcSSimon Schubert }
30555796c8dcSSimon Schubert 
30565796c8dcSSimon Schubert static char *
30575796c8dcSSimon Schubert c_path_expr_of_child (struct varobj *child)
30585796c8dcSSimon Schubert {
30595796c8dcSSimon Schubert   c_describe_child (child->parent, child->index, NULL, NULL, NULL,
30605796c8dcSSimon Schubert 		    &child->path_expr);
30615796c8dcSSimon Schubert   return child->path_expr;
30625796c8dcSSimon Schubert }
30635796c8dcSSimon Schubert 
30645796c8dcSSimon Schubert /* If frame associated with VAR can be found, switch
30655796c8dcSSimon Schubert    to it and return 1.  Otherwise, return 0.  */
30665796c8dcSSimon Schubert static int
30675796c8dcSSimon Schubert check_scope (struct varobj *var)
30685796c8dcSSimon Schubert {
30695796c8dcSSimon Schubert   struct frame_info *fi;
30705796c8dcSSimon Schubert   int scope;
30715796c8dcSSimon Schubert 
30725796c8dcSSimon Schubert   fi = frame_find_by_id (var->root->frame);
30735796c8dcSSimon Schubert   scope = fi != NULL;
30745796c8dcSSimon Schubert 
30755796c8dcSSimon Schubert   if (fi)
30765796c8dcSSimon Schubert     {
30775796c8dcSSimon Schubert       CORE_ADDR pc = get_frame_pc (fi);
3078cf7f2e2dSJohn Marino 
30795796c8dcSSimon Schubert       if (pc <  BLOCK_START (var->root->valid_block) ||
30805796c8dcSSimon Schubert 	  pc >= BLOCK_END (var->root->valid_block))
30815796c8dcSSimon Schubert 	scope = 0;
30825796c8dcSSimon Schubert       else
30835796c8dcSSimon Schubert 	select_frame (fi);
30845796c8dcSSimon Schubert     }
30855796c8dcSSimon Schubert   return scope;
30865796c8dcSSimon Schubert }
30875796c8dcSSimon Schubert 
30885796c8dcSSimon Schubert static struct value *
30895796c8dcSSimon Schubert c_value_of_root (struct varobj **var_handle)
30905796c8dcSSimon Schubert {
30915796c8dcSSimon Schubert   struct value *new_val = NULL;
30925796c8dcSSimon Schubert   struct varobj *var = *var_handle;
30935796c8dcSSimon Schubert   int within_scope = 0;
30945796c8dcSSimon Schubert   struct cleanup *back_to;
30955796c8dcSSimon Schubert 
30965796c8dcSSimon Schubert   /*  Only root variables can be updated...  */
30975796c8dcSSimon Schubert   if (!is_root_p (var))
3098c50c785cSJohn Marino     /* Not a root var.  */
30995796c8dcSSimon Schubert     return NULL;
31005796c8dcSSimon Schubert 
31015796c8dcSSimon Schubert   back_to = make_cleanup_restore_current_thread ();
31025796c8dcSSimon Schubert 
31035796c8dcSSimon Schubert   /* Determine whether the variable is still around.  */
31045796c8dcSSimon Schubert   if (var->root->valid_block == NULL || var->root->floating)
31055796c8dcSSimon Schubert     within_scope = 1;
31065796c8dcSSimon Schubert   else if (var->root->thread_id == 0)
31075796c8dcSSimon Schubert     {
31085796c8dcSSimon Schubert       /* The program was single-threaded when the variable object was
31095796c8dcSSimon Schubert 	 created.  Technically, it's possible that the program became
31105796c8dcSSimon Schubert 	 multi-threaded since then, but we don't support such
31115796c8dcSSimon Schubert 	 scenario yet.  */
31125796c8dcSSimon Schubert       within_scope = check_scope (var);
31135796c8dcSSimon Schubert     }
31145796c8dcSSimon Schubert   else
31155796c8dcSSimon Schubert     {
31165796c8dcSSimon Schubert       ptid_t ptid = thread_id_to_pid (var->root->thread_id);
31175796c8dcSSimon Schubert       if (in_thread_list (ptid))
31185796c8dcSSimon Schubert 	{
31195796c8dcSSimon Schubert 	  switch_to_thread (ptid);
31205796c8dcSSimon Schubert 	  within_scope = check_scope (var);
31215796c8dcSSimon Schubert 	}
31225796c8dcSSimon Schubert     }
31235796c8dcSSimon Schubert 
31245796c8dcSSimon Schubert   if (within_scope)
31255796c8dcSSimon Schubert     {
31265796c8dcSSimon Schubert       /* We need to catch errors here, because if evaluate
31275796c8dcSSimon Schubert          expression fails we want to just return NULL.  */
31285796c8dcSSimon Schubert       gdb_evaluate_expression (var->root->exp, &new_val);
31295796c8dcSSimon Schubert       return new_val;
31305796c8dcSSimon Schubert     }
31315796c8dcSSimon Schubert 
31325796c8dcSSimon Schubert   do_cleanups (back_to);
31335796c8dcSSimon Schubert 
31345796c8dcSSimon Schubert   return NULL;
31355796c8dcSSimon Schubert }
31365796c8dcSSimon Schubert 
31375796c8dcSSimon Schubert static struct value *
31385796c8dcSSimon Schubert c_value_of_child (struct varobj *parent, int index)
31395796c8dcSSimon Schubert {
31405796c8dcSSimon Schubert   struct value *value = NULL;
31415796c8dcSSimon Schubert 
3142cf7f2e2dSJohn Marino   c_describe_child (parent, index, NULL, &value, NULL, NULL);
31435796c8dcSSimon Schubert   return value;
31445796c8dcSSimon Schubert }
31455796c8dcSSimon Schubert 
31465796c8dcSSimon Schubert static struct type *
31475796c8dcSSimon Schubert c_type_of_child (struct varobj *parent, int index)
31485796c8dcSSimon Schubert {
31495796c8dcSSimon Schubert   struct type *type = NULL;
3150cf7f2e2dSJohn Marino 
31515796c8dcSSimon Schubert   c_describe_child (parent, index, NULL, NULL, &type, NULL);
31525796c8dcSSimon Schubert   return type;
31535796c8dcSSimon Schubert }
31545796c8dcSSimon Schubert 
31555796c8dcSSimon Schubert static char *
31565796c8dcSSimon Schubert c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
31575796c8dcSSimon Schubert {
31585796c8dcSSimon Schubert   /* BOGUS: if val_print sees a struct/class, or a reference to one,
31595796c8dcSSimon Schubert      it will print out its children instead of "{...}".  So we need to
31605796c8dcSSimon Schubert      catch that case explicitly.  */
31615796c8dcSSimon Schubert   struct type *type = get_type (var);
31625796c8dcSSimon Schubert 
31635796c8dcSSimon Schubert   /* If we have a custom formatter, return whatever string it has
31645796c8dcSSimon Schubert      produced.  */
31655796c8dcSSimon Schubert   if (var->pretty_printer && var->print_value)
31665796c8dcSSimon Schubert     return xstrdup (var->print_value);
31675796c8dcSSimon Schubert 
31685796c8dcSSimon Schubert   /* Strip top-level references.  */
31695796c8dcSSimon Schubert   while (TYPE_CODE (type) == TYPE_CODE_REF)
31705796c8dcSSimon Schubert     type = check_typedef (TYPE_TARGET_TYPE (type));
31715796c8dcSSimon Schubert 
31725796c8dcSSimon Schubert   switch (TYPE_CODE (type))
31735796c8dcSSimon Schubert     {
31745796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
31755796c8dcSSimon Schubert     case TYPE_CODE_UNION:
31765796c8dcSSimon Schubert       return xstrdup ("{...}");
31775796c8dcSSimon Schubert       /* break; */
31785796c8dcSSimon Schubert 
31795796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
31805796c8dcSSimon Schubert       {
31815796c8dcSSimon Schubert 	char *number;
3182cf7f2e2dSJohn Marino 
31835796c8dcSSimon Schubert 	number = xstrprintf ("[%d]", var->num_children);
31845796c8dcSSimon Schubert 	return (number);
31855796c8dcSSimon Schubert       }
31865796c8dcSSimon Schubert       /* break; */
31875796c8dcSSimon Schubert 
31885796c8dcSSimon Schubert     default:
31895796c8dcSSimon Schubert       {
31905796c8dcSSimon Schubert 	if (var->value == NULL)
31915796c8dcSSimon Schubert 	  {
31925796c8dcSSimon Schubert 	    /* This can happen if we attempt to get the value of a struct
31935796c8dcSSimon Schubert 	       member when the parent is an invalid pointer.  This is an
31945796c8dcSSimon Schubert 	       error condition, so we should tell the caller.  */
31955796c8dcSSimon Schubert 	    return NULL;
31965796c8dcSSimon Schubert 	  }
31975796c8dcSSimon Schubert 	else
31985796c8dcSSimon Schubert 	  {
31995796c8dcSSimon Schubert 	    if (var->not_fetched && value_lazy (var->value))
32005796c8dcSSimon Schubert 	      /* Frozen variable and no value yet.  We don't
32015796c8dcSSimon Schubert 		 implicitly fetch the value.  MI response will
32025796c8dcSSimon Schubert 		 use empty string for the value, which is OK.  */
32035796c8dcSSimon Schubert 	      return NULL;
32045796c8dcSSimon Schubert 
32055796c8dcSSimon Schubert 	    gdb_assert (varobj_value_is_changeable_p (var));
32065796c8dcSSimon Schubert 	    gdb_assert (!value_lazy (var->value));
32075796c8dcSSimon Schubert 
32085796c8dcSSimon Schubert 	    /* If the specified format is the current one,
3209c50c785cSJohn Marino 	       we can reuse print_value.  */
32105796c8dcSSimon Schubert 	    if (format == var->format)
32115796c8dcSSimon Schubert 	      return xstrdup (var->print_value);
32125796c8dcSSimon Schubert 	    else
32135796c8dcSSimon Schubert 	      return value_get_print_value (var->value, format, var);
32145796c8dcSSimon Schubert 	  }
32155796c8dcSSimon Schubert       }
32165796c8dcSSimon Schubert     }
32175796c8dcSSimon Schubert }
32185796c8dcSSimon Schubert 
32195796c8dcSSimon Schubert 
32205796c8dcSSimon Schubert /* C++ */
32215796c8dcSSimon Schubert 
32225796c8dcSSimon Schubert static int
32235796c8dcSSimon Schubert cplus_number_of_children (struct varobj *var)
32245796c8dcSSimon Schubert {
32255796c8dcSSimon Schubert   struct type *type;
32265796c8dcSSimon Schubert   int children, dont_know;
32275796c8dcSSimon Schubert 
32285796c8dcSSimon Schubert   dont_know = 1;
32295796c8dcSSimon Schubert   children = 0;
32305796c8dcSSimon Schubert 
32315796c8dcSSimon Schubert   if (!CPLUS_FAKE_CHILD (var))
32325796c8dcSSimon Schubert     {
32335796c8dcSSimon Schubert       type = get_value_type (var);
32345796c8dcSSimon Schubert       adjust_value_for_child_access (NULL, &type, NULL);
32355796c8dcSSimon Schubert 
32365796c8dcSSimon Schubert       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
32375796c8dcSSimon Schubert 	  ((TYPE_CODE (type)) == TYPE_CODE_UNION))
32385796c8dcSSimon Schubert 	{
32395796c8dcSSimon Schubert 	  int kids[3];
32405796c8dcSSimon Schubert 
32415796c8dcSSimon Schubert 	  cplus_class_num_children (type, kids);
32425796c8dcSSimon Schubert 	  if (kids[v_public] != 0)
32435796c8dcSSimon Schubert 	    children++;
32445796c8dcSSimon Schubert 	  if (kids[v_private] != 0)
32455796c8dcSSimon Schubert 	    children++;
32465796c8dcSSimon Schubert 	  if (kids[v_protected] != 0)
32475796c8dcSSimon Schubert 	    children++;
32485796c8dcSSimon Schubert 
3249c50c785cSJohn Marino 	  /* Add any baseclasses.  */
32505796c8dcSSimon Schubert 	  children += TYPE_N_BASECLASSES (type);
32515796c8dcSSimon Schubert 	  dont_know = 0;
32525796c8dcSSimon Schubert 
3253c50c785cSJohn Marino 	  /* FIXME: save children in var.  */
32545796c8dcSSimon Schubert 	}
32555796c8dcSSimon Schubert     }
32565796c8dcSSimon Schubert   else
32575796c8dcSSimon Schubert     {
32585796c8dcSSimon Schubert       int kids[3];
32595796c8dcSSimon Schubert 
32605796c8dcSSimon Schubert       type = get_value_type (var->parent);
32615796c8dcSSimon Schubert       adjust_value_for_child_access (NULL, &type, NULL);
32625796c8dcSSimon Schubert 
32635796c8dcSSimon Schubert       cplus_class_num_children (type, kids);
32645796c8dcSSimon Schubert       if (strcmp (var->name, "public") == 0)
32655796c8dcSSimon Schubert 	children = kids[v_public];
32665796c8dcSSimon Schubert       else if (strcmp (var->name, "private") == 0)
32675796c8dcSSimon Schubert 	children = kids[v_private];
32685796c8dcSSimon Schubert       else
32695796c8dcSSimon Schubert 	children = kids[v_protected];
32705796c8dcSSimon Schubert       dont_know = 0;
32715796c8dcSSimon Schubert     }
32725796c8dcSSimon Schubert 
32735796c8dcSSimon Schubert   if (dont_know)
32745796c8dcSSimon Schubert     children = c_number_of_children (var);
32755796c8dcSSimon Schubert 
32765796c8dcSSimon Schubert   return children;
32775796c8dcSSimon Schubert }
32785796c8dcSSimon Schubert 
32795796c8dcSSimon Schubert /* Compute # of public, private, and protected variables in this class.
32805796c8dcSSimon Schubert    That means we need to descend into all baseclasses and find out
32815796c8dcSSimon Schubert    how many are there, too.  */
32825796c8dcSSimon Schubert static void
32835796c8dcSSimon Schubert cplus_class_num_children (struct type *type, int children[3])
32845796c8dcSSimon Schubert {
3285cf7f2e2dSJohn Marino   int i, vptr_fieldno;
3286cf7f2e2dSJohn Marino   struct type *basetype = NULL;
32875796c8dcSSimon Schubert 
32885796c8dcSSimon Schubert   children[v_public] = 0;
32895796c8dcSSimon Schubert   children[v_private] = 0;
32905796c8dcSSimon Schubert   children[v_protected] = 0;
32915796c8dcSSimon Schubert 
3292cf7f2e2dSJohn Marino   vptr_fieldno = get_vptr_fieldno (type, &basetype);
32935796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
32945796c8dcSSimon Schubert     {
3295cf7f2e2dSJohn Marino       /* If we have a virtual table pointer, omit it.  Even if virtual
3296cf7f2e2dSJohn Marino 	 table pointers are not specifically marked in the debug info,
3297cf7f2e2dSJohn Marino 	 they should be artificial.  */
3298cf7f2e2dSJohn Marino       if ((type == basetype && i == vptr_fieldno)
3299cf7f2e2dSJohn Marino 	  || TYPE_FIELD_ARTIFICIAL (type, i))
33005796c8dcSSimon Schubert 	continue;
33015796c8dcSSimon Schubert 
33025796c8dcSSimon Schubert       if (TYPE_FIELD_PROTECTED (type, i))
33035796c8dcSSimon Schubert 	children[v_protected]++;
33045796c8dcSSimon Schubert       else if (TYPE_FIELD_PRIVATE (type, i))
33055796c8dcSSimon Schubert 	children[v_private]++;
33065796c8dcSSimon Schubert       else
33075796c8dcSSimon Schubert 	children[v_public]++;
33085796c8dcSSimon Schubert     }
33095796c8dcSSimon Schubert }
33105796c8dcSSimon Schubert 
33115796c8dcSSimon Schubert static char *
33125796c8dcSSimon Schubert cplus_name_of_variable (struct varobj *parent)
33135796c8dcSSimon Schubert {
33145796c8dcSSimon Schubert   return c_name_of_variable (parent);
33155796c8dcSSimon Schubert }
33165796c8dcSSimon Schubert 
33175796c8dcSSimon Schubert enum accessibility { private_field, protected_field, public_field };
33185796c8dcSSimon Schubert 
33195796c8dcSSimon Schubert /* Check if field INDEX of TYPE has the specified accessibility.
33205796c8dcSSimon Schubert    Return 0 if so and 1 otherwise.  */
33215796c8dcSSimon Schubert static int
33225796c8dcSSimon Schubert match_accessibility (struct type *type, int index, enum accessibility acc)
33235796c8dcSSimon Schubert {
33245796c8dcSSimon Schubert   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
33255796c8dcSSimon Schubert     return 1;
33265796c8dcSSimon Schubert   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
33275796c8dcSSimon Schubert     return 1;
33285796c8dcSSimon Schubert   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
33295796c8dcSSimon Schubert 	   && !TYPE_FIELD_PROTECTED (type, index))
33305796c8dcSSimon Schubert     return 1;
33315796c8dcSSimon Schubert   else
33325796c8dcSSimon Schubert     return 0;
33335796c8dcSSimon Schubert }
33345796c8dcSSimon Schubert 
33355796c8dcSSimon Schubert static void
33365796c8dcSSimon Schubert cplus_describe_child (struct varobj *parent, int index,
33375796c8dcSSimon Schubert 		      char **cname, struct value **cvalue, struct type **ctype,
33385796c8dcSSimon Schubert 		      char **cfull_expression)
33395796c8dcSSimon Schubert {
33405796c8dcSSimon Schubert   struct value *value;
33415796c8dcSSimon Schubert   struct type *type;
33425796c8dcSSimon Schubert   int was_ptr;
33435796c8dcSSimon Schubert   char *parent_expression = NULL;
33445796c8dcSSimon Schubert 
33455796c8dcSSimon Schubert   if (cname)
33465796c8dcSSimon Schubert     *cname = NULL;
33475796c8dcSSimon Schubert   if (cvalue)
33485796c8dcSSimon Schubert     *cvalue = NULL;
33495796c8dcSSimon Schubert   if (ctype)
33505796c8dcSSimon Schubert     *ctype = NULL;
33515796c8dcSSimon Schubert   if (cfull_expression)
33525796c8dcSSimon Schubert     *cfull_expression = NULL;
33535796c8dcSSimon Schubert 
33545796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (parent))
33555796c8dcSSimon Schubert     {
33565796c8dcSSimon Schubert       value = parent->parent->value;
33575796c8dcSSimon Schubert       type = get_value_type (parent->parent);
33585796c8dcSSimon Schubert       if (cfull_expression)
33595796c8dcSSimon Schubert 	parent_expression = varobj_get_path_expr (parent->parent);
33605796c8dcSSimon Schubert     }
33615796c8dcSSimon Schubert   else
33625796c8dcSSimon Schubert     {
33635796c8dcSSimon Schubert       value = parent->value;
33645796c8dcSSimon Schubert       type = get_value_type (parent);
33655796c8dcSSimon Schubert       if (cfull_expression)
33665796c8dcSSimon Schubert 	parent_expression = varobj_get_path_expr (parent);
33675796c8dcSSimon Schubert     }
33685796c8dcSSimon Schubert 
33695796c8dcSSimon Schubert   adjust_value_for_child_access (&value, &type, &was_ptr);
33705796c8dcSSimon Schubert 
33715796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
33725796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_UNION)
33735796c8dcSSimon Schubert     {
33745796c8dcSSimon Schubert       char *join = was_ptr ? "->" : ".";
3375cf7f2e2dSJohn Marino 
33765796c8dcSSimon Schubert       if (CPLUS_FAKE_CHILD (parent))
33775796c8dcSSimon Schubert 	{
33785796c8dcSSimon Schubert 	  /* The fields of the class type are ordered as they
33795796c8dcSSimon Schubert 	     appear in the class.  We are given an index for a
33805796c8dcSSimon Schubert 	     particular access control type ("public","protected",
33815796c8dcSSimon Schubert 	     or "private").  We must skip over fields that don't
33825796c8dcSSimon Schubert 	     have the access control we are looking for to properly
33835796c8dcSSimon Schubert 	     find the indexed field.  */
33845796c8dcSSimon Schubert 	  int type_index = TYPE_N_BASECLASSES (type);
33855796c8dcSSimon Schubert 	  enum accessibility acc = public_field;
3386cf7f2e2dSJohn Marino 	  int vptr_fieldno;
3387cf7f2e2dSJohn Marino 	  struct type *basetype = NULL;
3388cf7f2e2dSJohn Marino 
3389cf7f2e2dSJohn Marino 	  vptr_fieldno = get_vptr_fieldno (type, &basetype);
33905796c8dcSSimon Schubert 	  if (strcmp (parent->name, "private") == 0)
33915796c8dcSSimon Schubert 	    acc = private_field;
33925796c8dcSSimon Schubert 	  else if (strcmp (parent->name, "protected") == 0)
33935796c8dcSSimon Schubert 	    acc = protected_field;
33945796c8dcSSimon Schubert 
33955796c8dcSSimon Schubert 	  while (index >= 0)
33965796c8dcSSimon Schubert 	    {
3397cf7f2e2dSJohn Marino 	      if ((type == basetype && type_index == vptr_fieldno)
3398cf7f2e2dSJohn Marino 		  || TYPE_FIELD_ARTIFICIAL (type, type_index))
33995796c8dcSSimon Schubert 		; /* ignore vptr */
34005796c8dcSSimon Schubert 	      else if (match_accessibility (type, type_index, acc))
34015796c8dcSSimon Schubert 		    --index;
34025796c8dcSSimon Schubert 		  ++type_index;
34035796c8dcSSimon Schubert 	    }
34045796c8dcSSimon Schubert 	  --type_index;
34055796c8dcSSimon Schubert 
34065796c8dcSSimon Schubert 	  if (cname)
34075796c8dcSSimon Schubert 	    *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
34085796c8dcSSimon Schubert 
34095796c8dcSSimon Schubert 	  if (cvalue && value)
34105796c8dcSSimon Schubert 	    *cvalue = value_struct_element_index (value, type_index);
34115796c8dcSSimon Schubert 
34125796c8dcSSimon Schubert 	  if (ctype)
34135796c8dcSSimon Schubert 	    *ctype = TYPE_FIELD_TYPE (type, type_index);
34145796c8dcSSimon Schubert 
34155796c8dcSSimon Schubert 	  if (cfull_expression)
3416c50c785cSJohn Marino 	    *cfull_expression
3417c50c785cSJohn Marino 	      = xstrprintf ("((%s)%s%s)", parent_expression,
34185796c8dcSSimon Schubert 			    join,
34195796c8dcSSimon Schubert 			    TYPE_FIELD_NAME (type, type_index));
34205796c8dcSSimon Schubert 	}
34215796c8dcSSimon Schubert       else if (index < TYPE_N_BASECLASSES (type))
34225796c8dcSSimon Schubert 	{
34235796c8dcSSimon Schubert 	  /* This is a baseclass.  */
34245796c8dcSSimon Schubert 	  if (cname)
34255796c8dcSSimon Schubert 	    *cname = xstrdup (TYPE_FIELD_NAME (type, index));
34265796c8dcSSimon Schubert 
34275796c8dcSSimon Schubert 	  if (cvalue && value)
34285796c8dcSSimon Schubert 	    *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
34295796c8dcSSimon Schubert 
34305796c8dcSSimon Schubert 	  if (ctype)
34315796c8dcSSimon Schubert 	    {
34325796c8dcSSimon Schubert 	      *ctype = TYPE_FIELD_TYPE (type, index);
34335796c8dcSSimon Schubert 	    }
34345796c8dcSSimon Schubert 
34355796c8dcSSimon Schubert 	  if (cfull_expression)
34365796c8dcSSimon Schubert 	    {
34375796c8dcSSimon Schubert 	      char *ptr = was_ptr ? "*" : "";
3438cf7f2e2dSJohn Marino 
34395796c8dcSSimon Schubert 	      /* Cast the parent to the base' type.  Note that in gdb,
34405796c8dcSSimon Schubert 		 expression like
34415796c8dcSSimon Schubert 		         (Base1)d
34425796c8dcSSimon Schubert 		 will create an lvalue, for all appearences, so we don't
34435796c8dcSSimon Schubert 		 need to use more fancy:
34445796c8dcSSimon Schubert 		         *(Base1*)(&d)
3445*a45ae5f8SJohn Marino 		 construct.
3446*a45ae5f8SJohn Marino 
3447*a45ae5f8SJohn Marino 		 When we are in the scope of the base class or of one
3448*a45ae5f8SJohn Marino 		 of its children, the type field name will be interpreted
3449*a45ae5f8SJohn Marino 		 as a constructor, if it exists.  Therefore, we must
3450*a45ae5f8SJohn Marino 		 indicate that the name is a class name by using the
3451*a45ae5f8SJohn Marino 		 'class' keyword.  See PR mi/11912  */
3452*a45ae5f8SJohn Marino 	      *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
34535796c8dcSSimon Schubert 					      ptr,
34545796c8dcSSimon Schubert 					      TYPE_FIELD_NAME (type, index),
34555796c8dcSSimon Schubert 					      ptr,
34565796c8dcSSimon Schubert 					      parent_expression);
34575796c8dcSSimon Schubert 	    }
34585796c8dcSSimon Schubert 	}
34595796c8dcSSimon Schubert       else
34605796c8dcSSimon Schubert 	{
34615796c8dcSSimon Schubert 	  char *access = NULL;
34625796c8dcSSimon Schubert 	  int children[3];
3463cf7f2e2dSJohn Marino 
34645796c8dcSSimon Schubert 	  cplus_class_num_children (type, children);
34655796c8dcSSimon Schubert 
34665796c8dcSSimon Schubert 	  /* Everything beyond the baseclasses can
34675796c8dcSSimon Schubert 	     only be "public", "private", or "protected"
34685796c8dcSSimon Schubert 
34695796c8dcSSimon Schubert 	     The special "fake" children are always output by varobj in
34705796c8dcSSimon Schubert 	     this order.  So if INDEX == 2, it MUST be "protected".  */
34715796c8dcSSimon Schubert 	  index -= TYPE_N_BASECLASSES (type);
34725796c8dcSSimon Schubert 	  switch (index)
34735796c8dcSSimon Schubert 	    {
34745796c8dcSSimon Schubert 	    case 0:
34755796c8dcSSimon Schubert 	      if (children[v_public] > 0)
34765796c8dcSSimon Schubert 	 	access = "public";
34775796c8dcSSimon Schubert 	      else if (children[v_private] > 0)
34785796c8dcSSimon Schubert 	 	access = "private";
34795796c8dcSSimon Schubert 	      else
34805796c8dcSSimon Schubert 	 	access = "protected";
34815796c8dcSSimon Schubert 	      break;
34825796c8dcSSimon Schubert 	    case 1:
34835796c8dcSSimon Schubert 	      if (children[v_public] > 0)
34845796c8dcSSimon Schubert 		{
34855796c8dcSSimon Schubert 		  if (children[v_private] > 0)
34865796c8dcSSimon Schubert 		    access = "private";
34875796c8dcSSimon Schubert 		  else
34885796c8dcSSimon Schubert 		    access = "protected";
34895796c8dcSSimon Schubert 		}
34905796c8dcSSimon Schubert 	      else if (children[v_private] > 0)
34915796c8dcSSimon Schubert 	 	access = "protected";
34925796c8dcSSimon Schubert 	      break;
34935796c8dcSSimon Schubert 	    case 2:
3494c50c785cSJohn Marino 	      /* Must be protected.  */
34955796c8dcSSimon Schubert 	      access = "protected";
34965796c8dcSSimon Schubert 	      break;
34975796c8dcSSimon Schubert 	    default:
34985796c8dcSSimon Schubert 	      /* error!  */
34995796c8dcSSimon Schubert 	      break;
35005796c8dcSSimon Schubert 	    }
35015796c8dcSSimon Schubert 
35025796c8dcSSimon Schubert 	  gdb_assert (access);
35035796c8dcSSimon Schubert 	  if (cname)
35045796c8dcSSimon Schubert 	    *cname = xstrdup (access);
35055796c8dcSSimon Schubert 
35065796c8dcSSimon Schubert 	  /* Value and type and full expression are null here.  */
35075796c8dcSSimon Schubert 	}
35085796c8dcSSimon Schubert     }
35095796c8dcSSimon Schubert   else
35105796c8dcSSimon Schubert     {
35115796c8dcSSimon Schubert       c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
35125796c8dcSSimon Schubert     }
35135796c8dcSSimon Schubert }
35145796c8dcSSimon Schubert 
35155796c8dcSSimon Schubert static char *
35165796c8dcSSimon Schubert cplus_name_of_child (struct varobj *parent, int index)
35175796c8dcSSimon Schubert {
35185796c8dcSSimon Schubert   char *name = NULL;
3519cf7f2e2dSJohn Marino 
35205796c8dcSSimon Schubert   cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
35215796c8dcSSimon Schubert   return name;
35225796c8dcSSimon Schubert }
35235796c8dcSSimon Schubert 
35245796c8dcSSimon Schubert static char *
35255796c8dcSSimon Schubert cplus_path_expr_of_child (struct varobj *child)
35265796c8dcSSimon Schubert {
35275796c8dcSSimon Schubert   cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
35285796c8dcSSimon Schubert 			&child->path_expr);
35295796c8dcSSimon Schubert   return child->path_expr;
35305796c8dcSSimon Schubert }
35315796c8dcSSimon Schubert 
35325796c8dcSSimon Schubert static struct value *
35335796c8dcSSimon Schubert cplus_value_of_root (struct varobj **var_handle)
35345796c8dcSSimon Schubert {
35355796c8dcSSimon Schubert   return c_value_of_root (var_handle);
35365796c8dcSSimon Schubert }
35375796c8dcSSimon Schubert 
35385796c8dcSSimon Schubert static struct value *
35395796c8dcSSimon Schubert cplus_value_of_child (struct varobj *parent, int index)
35405796c8dcSSimon Schubert {
35415796c8dcSSimon Schubert   struct value *value = NULL;
3542cf7f2e2dSJohn Marino 
35435796c8dcSSimon Schubert   cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
35445796c8dcSSimon Schubert   return value;
35455796c8dcSSimon Schubert }
35465796c8dcSSimon Schubert 
35475796c8dcSSimon Schubert static struct type *
35485796c8dcSSimon Schubert cplus_type_of_child (struct varobj *parent, int index)
35495796c8dcSSimon Schubert {
35505796c8dcSSimon Schubert   struct type *type = NULL;
3551cf7f2e2dSJohn Marino 
35525796c8dcSSimon Schubert   cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
35535796c8dcSSimon Schubert   return type;
35545796c8dcSSimon Schubert }
35555796c8dcSSimon Schubert 
35565796c8dcSSimon Schubert static char *
3557cf7f2e2dSJohn Marino cplus_value_of_variable (struct varobj *var,
3558cf7f2e2dSJohn Marino 			 enum varobj_display_formats format)
35595796c8dcSSimon Schubert {
35605796c8dcSSimon Schubert 
35615796c8dcSSimon Schubert   /* If we have one of our special types, don't print out
35625796c8dcSSimon Schubert      any value.  */
35635796c8dcSSimon Schubert   if (CPLUS_FAKE_CHILD (var))
35645796c8dcSSimon Schubert     return xstrdup ("");
35655796c8dcSSimon Schubert 
35665796c8dcSSimon Schubert   return c_value_of_variable (var, format);
35675796c8dcSSimon Schubert }
35685796c8dcSSimon Schubert 
35695796c8dcSSimon Schubert /* Java */
35705796c8dcSSimon Schubert 
35715796c8dcSSimon Schubert static int
35725796c8dcSSimon Schubert java_number_of_children (struct varobj *var)
35735796c8dcSSimon Schubert {
35745796c8dcSSimon Schubert   return cplus_number_of_children (var);
35755796c8dcSSimon Schubert }
35765796c8dcSSimon Schubert 
35775796c8dcSSimon Schubert static char *
35785796c8dcSSimon Schubert java_name_of_variable (struct varobj *parent)
35795796c8dcSSimon Schubert {
35805796c8dcSSimon Schubert   char *p, *name;
35815796c8dcSSimon Schubert 
35825796c8dcSSimon Schubert   name = cplus_name_of_variable (parent);
35835796c8dcSSimon Schubert   /* If  the name has "-" in it, it is because we
35845796c8dcSSimon Schubert      needed to escape periods in the name...  */
35855796c8dcSSimon Schubert   p = name;
35865796c8dcSSimon Schubert 
35875796c8dcSSimon Schubert   while (*p != '\000')
35885796c8dcSSimon Schubert     {
35895796c8dcSSimon Schubert       if (*p == '-')
35905796c8dcSSimon Schubert 	*p = '.';
35915796c8dcSSimon Schubert       p++;
35925796c8dcSSimon Schubert     }
35935796c8dcSSimon Schubert 
35945796c8dcSSimon Schubert   return name;
35955796c8dcSSimon Schubert }
35965796c8dcSSimon Schubert 
35975796c8dcSSimon Schubert static char *
35985796c8dcSSimon Schubert java_name_of_child (struct varobj *parent, int index)
35995796c8dcSSimon Schubert {
36005796c8dcSSimon Schubert   char *name, *p;
36015796c8dcSSimon Schubert 
36025796c8dcSSimon Schubert   name = cplus_name_of_child (parent, index);
36035796c8dcSSimon Schubert   /* Escape any periods in the name...  */
36045796c8dcSSimon Schubert   p = name;
36055796c8dcSSimon Schubert 
36065796c8dcSSimon Schubert   while (*p != '\000')
36075796c8dcSSimon Schubert     {
36085796c8dcSSimon Schubert       if (*p == '.')
36095796c8dcSSimon Schubert 	*p = '-';
36105796c8dcSSimon Schubert       p++;
36115796c8dcSSimon Schubert     }
36125796c8dcSSimon Schubert 
36135796c8dcSSimon Schubert   return name;
36145796c8dcSSimon Schubert }
36155796c8dcSSimon Schubert 
36165796c8dcSSimon Schubert static char *
36175796c8dcSSimon Schubert java_path_expr_of_child (struct varobj *child)
36185796c8dcSSimon Schubert {
36195796c8dcSSimon Schubert   return NULL;
36205796c8dcSSimon Schubert }
36215796c8dcSSimon Schubert 
36225796c8dcSSimon Schubert static struct value *
36235796c8dcSSimon Schubert java_value_of_root (struct varobj **var_handle)
36245796c8dcSSimon Schubert {
36255796c8dcSSimon Schubert   return cplus_value_of_root (var_handle);
36265796c8dcSSimon Schubert }
36275796c8dcSSimon Schubert 
36285796c8dcSSimon Schubert static struct value *
36295796c8dcSSimon Schubert java_value_of_child (struct varobj *parent, int index)
36305796c8dcSSimon Schubert {
36315796c8dcSSimon Schubert   return cplus_value_of_child (parent, index);
36325796c8dcSSimon Schubert }
36335796c8dcSSimon Schubert 
36345796c8dcSSimon Schubert static struct type *
36355796c8dcSSimon Schubert java_type_of_child (struct varobj *parent, int index)
36365796c8dcSSimon Schubert {
36375796c8dcSSimon Schubert   return cplus_type_of_child (parent, index);
36385796c8dcSSimon Schubert }
36395796c8dcSSimon Schubert 
36405796c8dcSSimon Schubert static char *
36415796c8dcSSimon Schubert java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
36425796c8dcSSimon Schubert {
36435796c8dcSSimon Schubert   return cplus_value_of_variable (var, format);
36445796c8dcSSimon Schubert }
36455796c8dcSSimon Schubert 
3646*a45ae5f8SJohn Marino /* Ada specific callbacks for VAROBJs.  */
3647*a45ae5f8SJohn Marino 
3648*a45ae5f8SJohn Marino static int
3649*a45ae5f8SJohn Marino ada_number_of_children (struct varobj *var)
3650*a45ae5f8SJohn Marino {
3651*a45ae5f8SJohn Marino   return c_number_of_children (var);
3652*a45ae5f8SJohn Marino }
3653*a45ae5f8SJohn Marino 
3654*a45ae5f8SJohn Marino static char *
3655*a45ae5f8SJohn Marino ada_name_of_variable (struct varobj *parent)
3656*a45ae5f8SJohn Marino {
3657*a45ae5f8SJohn Marino   return c_name_of_variable (parent);
3658*a45ae5f8SJohn Marino }
3659*a45ae5f8SJohn Marino 
3660*a45ae5f8SJohn Marino static char *
3661*a45ae5f8SJohn Marino ada_name_of_child (struct varobj *parent, int index)
3662*a45ae5f8SJohn Marino {
3663*a45ae5f8SJohn Marino   return c_name_of_child (parent, index);
3664*a45ae5f8SJohn Marino }
3665*a45ae5f8SJohn Marino 
3666*a45ae5f8SJohn Marino static char*
3667*a45ae5f8SJohn Marino ada_path_expr_of_child (struct varobj *child)
3668*a45ae5f8SJohn Marino {
3669*a45ae5f8SJohn Marino   return c_path_expr_of_child (child);
3670*a45ae5f8SJohn Marino }
3671*a45ae5f8SJohn Marino 
3672*a45ae5f8SJohn Marino static struct value *
3673*a45ae5f8SJohn Marino ada_value_of_root (struct varobj **var_handle)
3674*a45ae5f8SJohn Marino {
3675*a45ae5f8SJohn Marino   return c_value_of_root (var_handle);
3676*a45ae5f8SJohn Marino }
3677*a45ae5f8SJohn Marino 
3678*a45ae5f8SJohn Marino static struct value *
3679*a45ae5f8SJohn Marino ada_value_of_child (struct varobj *parent, int index)
3680*a45ae5f8SJohn Marino {
3681*a45ae5f8SJohn Marino   return c_value_of_child (parent, index);
3682*a45ae5f8SJohn Marino }
3683*a45ae5f8SJohn Marino 
3684*a45ae5f8SJohn Marino static struct type *
3685*a45ae5f8SJohn Marino ada_type_of_child (struct varobj *parent, int index)
3686*a45ae5f8SJohn Marino {
3687*a45ae5f8SJohn Marino   return c_type_of_child (parent, index);
3688*a45ae5f8SJohn Marino }
3689*a45ae5f8SJohn Marino 
3690*a45ae5f8SJohn Marino static char *
3691*a45ae5f8SJohn Marino ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3692*a45ae5f8SJohn Marino {
3693*a45ae5f8SJohn Marino   return c_value_of_variable (var, format);
3694*a45ae5f8SJohn Marino }
3695*a45ae5f8SJohn Marino 
36965796c8dcSSimon Schubert /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
36975796c8dcSSimon Schubert    with an arbitrary caller supplied DATA pointer.  */
36985796c8dcSSimon Schubert 
36995796c8dcSSimon Schubert void
37005796c8dcSSimon Schubert all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
37015796c8dcSSimon Schubert {
37025796c8dcSSimon Schubert   struct varobj_root *var_root, *var_root_next;
37035796c8dcSSimon Schubert 
37045796c8dcSSimon Schubert   /* Iterate "safely" - handle if the callee deletes its passed VAROBJ.  */
37055796c8dcSSimon Schubert 
37065796c8dcSSimon Schubert   for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
37075796c8dcSSimon Schubert     {
37085796c8dcSSimon Schubert       var_root_next = var_root->next;
37095796c8dcSSimon Schubert 
37105796c8dcSSimon Schubert       (*func) (var_root->rootvar, data);
37115796c8dcSSimon Schubert     }
37125796c8dcSSimon Schubert }
37135796c8dcSSimon Schubert 
37145796c8dcSSimon Schubert extern void _initialize_varobj (void);
37155796c8dcSSimon Schubert void
37165796c8dcSSimon Schubert _initialize_varobj (void)
37175796c8dcSSimon Schubert {
37185796c8dcSSimon Schubert   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
37195796c8dcSSimon Schubert 
37205796c8dcSSimon Schubert   varobj_table = xmalloc (sizeof_table);
37215796c8dcSSimon Schubert   memset (varobj_table, 0, sizeof_table);
37225796c8dcSSimon Schubert 
37235796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
3724c50c785cSJohn Marino 			    &varobjdebug,
3725c50c785cSJohn Marino 			    _("Set varobj debugging."),
3726c50c785cSJohn Marino 			    _("Show varobj debugging."),
3727c50c785cSJohn Marino 			    _("When non-zero, varobj debugging is enabled."),
3728c50c785cSJohn Marino 			    NULL, show_varobjdebug,
37295796c8dcSSimon Schubert 			    &setlist, &showlist);
37305796c8dcSSimon Schubert }
37315796c8dcSSimon Schubert 
37325796c8dcSSimon Schubert /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
37335796c8dcSSimon Schubert    defined on globals.  It is a helper for varobj_invalidate.  */
37345796c8dcSSimon Schubert 
37355796c8dcSSimon Schubert static void
37365796c8dcSSimon Schubert varobj_invalidate_iter (struct varobj *var, void *unused)
37375796c8dcSSimon Schubert {
37385796c8dcSSimon Schubert   /* Floating varobjs are reparsed on each stop, so we don't care if the
37395796c8dcSSimon Schubert      presently parsed expression refers to something that's gone.  */
37405796c8dcSSimon Schubert   if (var->root->floating)
37415796c8dcSSimon Schubert     return;
37425796c8dcSSimon Schubert 
37435796c8dcSSimon Schubert   /* global var must be re-evaluated.  */
37445796c8dcSSimon Schubert   if (var->root->valid_block == NULL)
37455796c8dcSSimon Schubert     {
37465796c8dcSSimon Schubert       struct varobj *tmp_var;
37475796c8dcSSimon Schubert 
37485796c8dcSSimon Schubert       /* Try to create a varobj with same expression.  If we succeed
37495796c8dcSSimon Schubert 	 replace the old varobj, otherwise invalidate it.  */
37505796c8dcSSimon Schubert       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
37515796c8dcSSimon Schubert 			       USE_CURRENT_FRAME);
37525796c8dcSSimon Schubert       if (tmp_var != NULL)
37535796c8dcSSimon Schubert 	{
37545796c8dcSSimon Schubert 	  tmp_var->obj_name = xstrdup (var->obj_name);
37555796c8dcSSimon Schubert 	  varobj_delete (var, NULL, 0);
37565796c8dcSSimon Schubert 	  install_variable (tmp_var);
37575796c8dcSSimon Schubert 	}
37585796c8dcSSimon Schubert       else
37595796c8dcSSimon Schubert 	var->root->is_valid = 0;
37605796c8dcSSimon Schubert     }
37615796c8dcSSimon Schubert   else /* locals must be invalidated.  */
37625796c8dcSSimon Schubert     var->root->is_valid = 0;
37635796c8dcSSimon Schubert }
37645796c8dcSSimon Schubert 
37655796c8dcSSimon Schubert /* Invalidate the varobjs that are tied to locals and re-create the ones that
37665796c8dcSSimon Schubert    are defined on globals.
37675796c8dcSSimon Schubert    Invalidated varobjs will be always printed in_scope="invalid".  */
37685796c8dcSSimon Schubert 
37695796c8dcSSimon Schubert void
37705796c8dcSSimon Schubert varobj_invalidate (void)
37715796c8dcSSimon Schubert {
37725796c8dcSSimon Schubert   all_root_varobjs (varobj_invalidate_iter, NULL);
37735796c8dcSSimon Schubert }
3774