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