15796c8dcSSimon Schubert /* Implementation of the GDB variable objects API.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1999-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
65796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
75796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
85796c8dcSSimon Schubert (at your option) any later version.
95796c8dcSSimon Schubert
105796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
115796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
125796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135796c8dcSSimon Schubert GNU General Public License for more details.
145796c8dcSSimon Schubert
155796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
165796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
175796c8dcSSimon Schubert
185796c8dcSSimon Schubert #include "defs.h"
195796c8dcSSimon Schubert #include "exceptions.h"
205796c8dcSSimon Schubert #include "value.h"
215796c8dcSSimon Schubert #include "expression.h"
225796c8dcSSimon Schubert #include "frame.h"
235796c8dcSSimon Schubert #include "language.h"
245796c8dcSSimon Schubert #include "gdbcmd.h"
255796c8dcSSimon Schubert #include "block.h"
265796c8dcSSimon Schubert #include "valprint.h"
275796c8dcSSimon Schubert
285796c8dcSSimon Schubert #include "gdb_assert.h"
295796c8dcSSimon Schubert #include "gdb_string.h"
305796c8dcSSimon Schubert #include "gdb_regex.h"
315796c8dcSSimon Schubert
325796c8dcSSimon Schubert #include "varobj.h"
335796c8dcSSimon Schubert #include "vec.h"
345796c8dcSSimon Schubert #include "gdbthread.h"
355796c8dcSSimon Schubert #include "inferior.h"
36*ef5ccd6cSJohn Marino #include "ada-varobj.h"
37*ef5ccd6cSJohn Marino #include "ada-lang.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
46*ef5ccd6cSJohn Marino /* The names of varobjs representing anonymous structs or unions. */
47*ef5ccd6cSJohn Marino #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48*ef5ccd6cSJohn Marino #define ANONYMOUS_UNION_NAME _("<anonymous union>")
49*ef5ccd6cSJohn Marino
505796c8dcSSimon Schubert /* Non-zero if we want to see trace of varobj level stuff. */
515796c8dcSSimon Schubert
52*ef5ccd6cSJohn Marino unsigned int varobjdebug = 0;
535796c8dcSSimon Schubert static void
show_varobjdebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)545796c8dcSSimon Schubert show_varobjdebug (struct ui_file *file, int from_tty,
555796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
565796c8dcSSimon Schubert {
575796c8dcSSimon Schubert fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
585796c8dcSSimon Schubert }
595796c8dcSSimon Schubert
60c50c785cSJohn Marino /* String representations of gdb's format codes. */
615796c8dcSSimon Schubert char *varobj_format_string[] =
625796c8dcSSimon Schubert { "natural", "binary", "decimal", "hexadecimal", "octal" };
635796c8dcSSimon Schubert
64c50c785cSJohn Marino /* String representations of gdb's known languages. */
655796c8dcSSimon Schubert char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
665796c8dcSSimon Schubert
675796c8dcSSimon Schubert /* True if we want to allow Python-based pretty-printing. */
685796c8dcSSimon Schubert static int pretty_printing = 0;
695796c8dcSSimon Schubert
705796c8dcSSimon Schubert void
varobj_enable_pretty_printing(void)715796c8dcSSimon Schubert varobj_enable_pretty_printing (void)
725796c8dcSSimon Schubert {
735796c8dcSSimon Schubert pretty_printing = 1;
745796c8dcSSimon Schubert }
755796c8dcSSimon Schubert
765796c8dcSSimon Schubert /* Data structures */
775796c8dcSSimon Schubert
785796c8dcSSimon Schubert /* Every root variable has one of these structures saved in its
795796c8dcSSimon Schubert varobj. Members which must be free'd are noted. */
805796c8dcSSimon Schubert struct varobj_root
815796c8dcSSimon Schubert {
825796c8dcSSimon Schubert
835796c8dcSSimon Schubert /* Alloc'd expression for this parent. */
845796c8dcSSimon Schubert struct expression *exp;
855796c8dcSSimon Schubert
86c50c785cSJohn Marino /* Block for which this expression is valid. */
87*ef5ccd6cSJohn Marino const struct block *valid_block;
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert /* The frame for this expression. This field is set iff valid_block is
905796c8dcSSimon Schubert not NULL. */
915796c8dcSSimon Schubert struct frame_id frame;
925796c8dcSSimon Schubert
935796c8dcSSimon Schubert /* The thread ID that this varobj_root belong to. This field
945796c8dcSSimon Schubert is only valid if valid_block is not NULL.
955796c8dcSSimon Schubert When not 0, indicates which thread 'frame' belongs to.
965796c8dcSSimon Schubert When 0, indicates that the thread list was empty when the varobj_root
975796c8dcSSimon Schubert was created. */
985796c8dcSSimon Schubert int thread_id;
995796c8dcSSimon Schubert
1005796c8dcSSimon Schubert /* If 1, the -var-update always recomputes the value in the
1015796c8dcSSimon Schubert current thread and frame. Otherwise, variable object is
102c50c785cSJohn Marino always updated in the specific scope/thread/frame. */
1035796c8dcSSimon Schubert int floating;
1045796c8dcSSimon Schubert
1055796c8dcSSimon Schubert /* Flag that indicates validity: set to 0 when this varobj_root refers
1065796c8dcSSimon Schubert to symbols that do not exist anymore. */
1075796c8dcSSimon Schubert int is_valid;
1085796c8dcSSimon Schubert
109c50c785cSJohn Marino /* Language info for this variable and its children. */
1105796c8dcSSimon Schubert struct language_specific *lang;
1115796c8dcSSimon Schubert
1125796c8dcSSimon Schubert /* The varobj for this root node. */
1135796c8dcSSimon Schubert struct varobj *rootvar;
1145796c8dcSSimon Schubert
1155796c8dcSSimon Schubert /* Next root variable */
1165796c8dcSSimon Schubert struct varobj_root *next;
1175796c8dcSSimon Schubert };
1185796c8dcSSimon Schubert
1195796c8dcSSimon Schubert /* Every variable in the system has a structure of this type defined
1205796c8dcSSimon Schubert for it. This structure holds all information necessary to manipulate
1215796c8dcSSimon Schubert a particular object variable. Members which must be freed are noted. */
1225796c8dcSSimon Schubert struct varobj
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert
125c50c785cSJohn Marino /* Alloc'd name of the variable for this object. If this variable is a
1265796c8dcSSimon Schubert child, then this name will be the child's source name.
127c50c785cSJohn Marino (bar, not foo.bar). */
128c50c785cSJohn Marino /* NOTE: This is the "expression". */
1295796c8dcSSimon Schubert char *name;
1305796c8dcSSimon Schubert
1315796c8dcSSimon Schubert /* Alloc'd expression for this child. Can be used to create a
1325796c8dcSSimon Schubert root variable corresponding to this child. */
1335796c8dcSSimon Schubert char *path_expr;
1345796c8dcSSimon Schubert
1355796c8dcSSimon Schubert /* The alloc'd name for this variable's object. This is here for
1365796c8dcSSimon Schubert convenience when constructing this object's children. */
1375796c8dcSSimon Schubert char *obj_name;
1385796c8dcSSimon Schubert
139c50c785cSJohn Marino /* Index of this variable in its parent or -1. */
1405796c8dcSSimon Schubert int index;
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert /* The type of this variable. This can be NULL
1435796c8dcSSimon Schubert for artifial variable objects -- currently, the "accessibility"
1445796c8dcSSimon Schubert variable objects in C++. */
1455796c8dcSSimon Schubert struct type *type;
1465796c8dcSSimon Schubert
1475796c8dcSSimon Schubert /* The value of this expression or subexpression. A NULL value
1485796c8dcSSimon Schubert indicates there was an error getting this value.
1495796c8dcSSimon Schubert Invariant: if varobj_value_is_changeable_p (this) is non-zero,
1505796c8dcSSimon Schubert the value is either NULL, or not lazy. */
1515796c8dcSSimon Schubert struct value *value;
1525796c8dcSSimon Schubert
153c50c785cSJohn Marino /* The number of (immediate) children this variable has. */
1545796c8dcSSimon Schubert int num_children;
1555796c8dcSSimon Schubert
1565796c8dcSSimon Schubert /* If this object is a child, this points to its immediate parent. */
1575796c8dcSSimon Schubert struct varobj *parent;
1585796c8dcSSimon Schubert
1595796c8dcSSimon Schubert /* Children of this object. */
1605796c8dcSSimon Schubert VEC (varobj_p) *children;
1615796c8dcSSimon Schubert
1625796c8dcSSimon Schubert /* Whether the children of this varobj were requested. This field is
1635796c8dcSSimon Schubert used to decide if dynamic varobj should recompute their children.
1645796c8dcSSimon Schubert In the event that the frontend never asked for the children, we
1655796c8dcSSimon Schubert can avoid that. */
1665796c8dcSSimon Schubert int children_requested;
1675796c8dcSSimon Schubert
168c50c785cSJohn Marino /* Description of the root variable. Points to root variable for
169c50c785cSJohn Marino children. */
1705796c8dcSSimon Schubert struct varobj_root *root;
1715796c8dcSSimon Schubert
172c50c785cSJohn Marino /* The format of the output for this object. */
1735796c8dcSSimon Schubert enum varobj_display_formats format;
1745796c8dcSSimon Schubert
175c50c785cSJohn Marino /* Was this variable updated via a varobj_set_value operation. */
1765796c8dcSSimon Schubert int updated;
1775796c8dcSSimon Schubert
1785796c8dcSSimon Schubert /* Last print value. */
1795796c8dcSSimon Schubert char *print_value;
1805796c8dcSSimon Schubert
1815796c8dcSSimon Schubert /* Is this variable frozen. Frozen variables are never implicitly
1825796c8dcSSimon Schubert updated by -var-update *
1835796c8dcSSimon Schubert or -var-update <direct-or-indirect-parent>. */
1845796c8dcSSimon Schubert int frozen;
1855796c8dcSSimon Schubert
1865796c8dcSSimon Schubert /* Is the value of this variable intentionally not fetched? It is
1875796c8dcSSimon Schubert not fetched if either the variable is frozen, or any parents is
1885796c8dcSSimon Schubert frozen. */
1895796c8dcSSimon Schubert int not_fetched;
1905796c8dcSSimon Schubert
1915796c8dcSSimon Schubert /* Sub-range of children which the MI consumer has requested. If
1925796c8dcSSimon Schubert FROM < 0 or TO < 0, means that all children have been
1935796c8dcSSimon Schubert requested. */
1945796c8dcSSimon Schubert int from;
1955796c8dcSSimon Schubert int to;
1965796c8dcSSimon Schubert
1975796c8dcSSimon Schubert /* The pretty-printer constructor. If NULL, then the default
1985796c8dcSSimon Schubert pretty-printer will be looked up. If None, then no
1995796c8dcSSimon Schubert pretty-printer will be installed. */
2005796c8dcSSimon Schubert PyObject *constructor;
2015796c8dcSSimon Schubert
2025796c8dcSSimon Schubert /* The pretty-printer that has been constructed. If NULL, then a
2035796c8dcSSimon Schubert new printer object is needed, and one will be constructed. */
2045796c8dcSSimon Schubert PyObject *pretty_printer;
2055796c8dcSSimon Schubert
2065796c8dcSSimon Schubert /* The iterator returned by the printer's 'children' method, or NULL
2075796c8dcSSimon Schubert if not available. */
2085796c8dcSSimon Schubert PyObject *child_iter;
2095796c8dcSSimon Schubert
2105796c8dcSSimon Schubert /* We request one extra item from the iterator, so that we can
2115796c8dcSSimon Schubert report to the caller whether there are more items than we have
2125796c8dcSSimon Schubert already reported. However, we don't want to install this value
2135796c8dcSSimon Schubert when we read it, because that will mess up future updates. So,
2145796c8dcSSimon Schubert we stash it here instead. */
2155796c8dcSSimon Schubert PyObject *saved_item;
2165796c8dcSSimon Schubert };
2175796c8dcSSimon Schubert
2185796c8dcSSimon Schubert struct cpstack
2195796c8dcSSimon Schubert {
2205796c8dcSSimon Schubert char *name;
2215796c8dcSSimon Schubert struct cpstack *next;
2225796c8dcSSimon Schubert };
2235796c8dcSSimon Schubert
2245796c8dcSSimon Schubert /* A list of varobjs */
2255796c8dcSSimon Schubert
2265796c8dcSSimon Schubert struct vlist
2275796c8dcSSimon Schubert {
2285796c8dcSSimon Schubert struct varobj *var;
2295796c8dcSSimon Schubert struct vlist *next;
2305796c8dcSSimon Schubert };
2315796c8dcSSimon Schubert
2325796c8dcSSimon Schubert /* Private function prototypes */
2335796c8dcSSimon Schubert
2345796c8dcSSimon Schubert /* Helper functions for the above subcommands. */
2355796c8dcSSimon Schubert
2365796c8dcSSimon Schubert static int delete_variable (struct cpstack **, struct varobj *, int);
2375796c8dcSSimon Schubert
2385796c8dcSSimon Schubert static void delete_variable_1 (struct cpstack **, int *,
2395796c8dcSSimon Schubert struct varobj *, int, int);
2405796c8dcSSimon Schubert
2415796c8dcSSimon Schubert static int install_variable (struct varobj *);
2425796c8dcSSimon Schubert
2435796c8dcSSimon Schubert static void uninstall_variable (struct varobj *);
2445796c8dcSSimon Schubert
2455796c8dcSSimon Schubert static struct varobj *create_child (struct varobj *, int, char *);
2465796c8dcSSimon Schubert
2475796c8dcSSimon Schubert static struct varobj *
2485796c8dcSSimon Schubert create_child_with_value (struct varobj *parent, int index, const char *name,
2495796c8dcSSimon Schubert struct value *value);
2505796c8dcSSimon Schubert
2515796c8dcSSimon Schubert /* Utility routines */
2525796c8dcSSimon Schubert
2535796c8dcSSimon Schubert static struct varobj *new_variable (void);
2545796c8dcSSimon Schubert
2555796c8dcSSimon Schubert static struct varobj *new_root_variable (void);
2565796c8dcSSimon Schubert
2575796c8dcSSimon Schubert static void free_variable (struct varobj *var);
2585796c8dcSSimon Schubert
2595796c8dcSSimon Schubert static struct cleanup *make_cleanup_free_variable (struct varobj *var);
2605796c8dcSSimon Schubert
2615796c8dcSSimon Schubert static struct type *get_type (struct varobj *var);
2625796c8dcSSimon Schubert
2635796c8dcSSimon Schubert static struct type *get_value_type (struct varobj *var);
2645796c8dcSSimon Schubert
2655796c8dcSSimon Schubert static struct type *get_target_type (struct type *);
2665796c8dcSSimon Schubert
2675796c8dcSSimon Schubert static enum varobj_display_formats variable_default_display (struct varobj *);
2685796c8dcSSimon Schubert
2695796c8dcSSimon Schubert static void cppush (struct cpstack **pstack, char *name);
2705796c8dcSSimon Schubert
2715796c8dcSSimon Schubert static char *cppop (struct cpstack **pstack);
2725796c8dcSSimon Schubert
273*ef5ccd6cSJohn Marino static int update_type_if_necessary (struct varobj *var,
274*ef5ccd6cSJohn Marino struct value *new_value);
275*ef5ccd6cSJohn Marino
2765796c8dcSSimon Schubert static int install_new_value (struct varobj *var, struct value *value,
2775796c8dcSSimon Schubert int initial);
2785796c8dcSSimon Schubert
2795796c8dcSSimon Schubert /* Language-specific routines. */
2805796c8dcSSimon Schubert
2815796c8dcSSimon Schubert static enum varobj_languages variable_language (struct varobj *var);
2825796c8dcSSimon Schubert
2835796c8dcSSimon Schubert static int number_of_children (struct varobj *);
2845796c8dcSSimon Schubert
2855796c8dcSSimon Schubert static char *name_of_variable (struct varobj *);
2865796c8dcSSimon Schubert
2875796c8dcSSimon Schubert static char *name_of_child (struct varobj *, int);
2885796c8dcSSimon Schubert
2895796c8dcSSimon Schubert static struct value *value_of_root (struct varobj **var_handle, int *);
2905796c8dcSSimon Schubert
2915796c8dcSSimon Schubert static struct value *value_of_child (struct varobj *parent, int index);
2925796c8dcSSimon Schubert
2935796c8dcSSimon Schubert static char *my_value_of_variable (struct varobj *var,
2945796c8dcSSimon Schubert enum varobj_display_formats format);
2955796c8dcSSimon Schubert
2965796c8dcSSimon Schubert static char *value_get_print_value (struct value *value,
2975796c8dcSSimon Schubert enum varobj_display_formats format,
2985796c8dcSSimon Schubert struct varobj *var);
2995796c8dcSSimon Schubert
3005796c8dcSSimon Schubert static int varobj_value_is_changeable_p (struct varobj *var);
3015796c8dcSSimon Schubert
3025796c8dcSSimon Schubert static int is_root_p (struct varobj *var);
3035796c8dcSSimon Schubert
304cf7f2e2dSJohn Marino #if HAVE_PYTHON
305cf7f2e2dSJohn Marino
306c50c785cSJohn Marino static struct varobj *varobj_add_child (struct varobj *var,
307c50c785cSJohn Marino const char *name,
308c50c785cSJohn Marino struct value *value);
3095796c8dcSSimon Schubert
310cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
311cf7f2e2dSJohn Marino
312*ef5ccd6cSJohn Marino static int default_value_is_changeable_p (struct varobj *var);
313*ef5ccd6cSJohn Marino
3145796c8dcSSimon Schubert /* C implementation */
3155796c8dcSSimon Schubert
3165796c8dcSSimon Schubert static int c_number_of_children (struct varobj *var);
3175796c8dcSSimon Schubert
3185796c8dcSSimon Schubert static char *c_name_of_variable (struct varobj *parent);
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert static char *c_name_of_child (struct varobj *parent, int index);
3215796c8dcSSimon Schubert
3225796c8dcSSimon Schubert static char *c_path_expr_of_child (struct varobj *child);
3235796c8dcSSimon Schubert
3245796c8dcSSimon Schubert static struct value *c_value_of_root (struct varobj **var_handle);
3255796c8dcSSimon Schubert
3265796c8dcSSimon Schubert static struct value *c_value_of_child (struct varobj *parent, int index);
3275796c8dcSSimon Schubert
3285796c8dcSSimon Schubert static struct type *c_type_of_child (struct varobj *parent, int index);
3295796c8dcSSimon Schubert
3305796c8dcSSimon Schubert static char *c_value_of_variable (struct varobj *var,
3315796c8dcSSimon Schubert enum varobj_display_formats format);
3325796c8dcSSimon Schubert
3335796c8dcSSimon Schubert /* C++ implementation */
3345796c8dcSSimon Schubert
3355796c8dcSSimon Schubert static int cplus_number_of_children (struct varobj *var);
3365796c8dcSSimon Schubert
3375796c8dcSSimon Schubert static void cplus_class_num_children (struct type *type, int children[3]);
3385796c8dcSSimon Schubert
3395796c8dcSSimon Schubert static char *cplus_name_of_variable (struct varobj *parent);
3405796c8dcSSimon Schubert
3415796c8dcSSimon Schubert static char *cplus_name_of_child (struct varobj *parent, int index);
3425796c8dcSSimon Schubert
3435796c8dcSSimon Schubert static char *cplus_path_expr_of_child (struct varobj *child);
3445796c8dcSSimon Schubert
3455796c8dcSSimon Schubert static struct value *cplus_value_of_root (struct varobj **var_handle);
3465796c8dcSSimon Schubert
3475796c8dcSSimon Schubert static struct value *cplus_value_of_child (struct varobj *parent, int index);
3485796c8dcSSimon Schubert
3495796c8dcSSimon Schubert static struct type *cplus_type_of_child (struct varobj *parent, int index);
3505796c8dcSSimon Schubert
3515796c8dcSSimon Schubert static char *cplus_value_of_variable (struct varobj *var,
3525796c8dcSSimon Schubert enum varobj_display_formats format);
3535796c8dcSSimon Schubert
3545796c8dcSSimon Schubert /* Java implementation */
3555796c8dcSSimon Schubert
3565796c8dcSSimon Schubert static int java_number_of_children (struct varobj *var);
3575796c8dcSSimon Schubert
3585796c8dcSSimon Schubert static char *java_name_of_variable (struct varobj *parent);
3595796c8dcSSimon Schubert
3605796c8dcSSimon Schubert static char *java_name_of_child (struct varobj *parent, int index);
3615796c8dcSSimon Schubert
3625796c8dcSSimon Schubert static char *java_path_expr_of_child (struct varobj *child);
3635796c8dcSSimon Schubert
3645796c8dcSSimon Schubert static struct value *java_value_of_root (struct varobj **var_handle);
3655796c8dcSSimon Schubert
3665796c8dcSSimon Schubert static struct value *java_value_of_child (struct varobj *parent, int index);
3675796c8dcSSimon Schubert
3685796c8dcSSimon Schubert static struct type *java_type_of_child (struct varobj *parent, int index);
3695796c8dcSSimon Schubert
3705796c8dcSSimon Schubert static char *java_value_of_variable (struct varobj *var,
3715796c8dcSSimon Schubert enum varobj_display_formats format);
3725796c8dcSSimon Schubert
373a45ae5f8SJohn Marino /* Ada implementation */
374a45ae5f8SJohn Marino
375a45ae5f8SJohn Marino static int ada_number_of_children (struct varobj *var);
376a45ae5f8SJohn Marino
377a45ae5f8SJohn Marino static char *ada_name_of_variable (struct varobj *parent);
378a45ae5f8SJohn Marino
379a45ae5f8SJohn Marino static char *ada_name_of_child (struct varobj *parent, int index);
380a45ae5f8SJohn Marino
381a45ae5f8SJohn Marino static char *ada_path_expr_of_child (struct varobj *child);
382a45ae5f8SJohn Marino
383a45ae5f8SJohn Marino static struct value *ada_value_of_root (struct varobj **var_handle);
384a45ae5f8SJohn Marino
385a45ae5f8SJohn Marino static struct value *ada_value_of_child (struct varobj *parent, int index);
386a45ae5f8SJohn Marino
387a45ae5f8SJohn Marino static struct type *ada_type_of_child (struct varobj *parent, int index);
388a45ae5f8SJohn Marino
389a45ae5f8SJohn Marino static char *ada_value_of_variable (struct varobj *var,
390a45ae5f8SJohn Marino enum varobj_display_formats format);
391a45ae5f8SJohn Marino
392*ef5ccd6cSJohn Marino static int ada_value_is_changeable_p (struct varobj *var);
393*ef5ccd6cSJohn Marino
394*ef5ccd6cSJohn Marino static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
395*ef5ccd6cSJohn Marino struct type *new_type);
396*ef5ccd6cSJohn Marino
3975796c8dcSSimon Schubert /* The language specific vector */
3985796c8dcSSimon Schubert
3995796c8dcSSimon Schubert struct language_specific
4005796c8dcSSimon Schubert {
4015796c8dcSSimon Schubert
402c50c785cSJohn Marino /* The language of this variable. */
4035796c8dcSSimon Schubert enum varobj_languages language;
4045796c8dcSSimon Schubert
4055796c8dcSSimon Schubert /* The number of children of PARENT. */
4065796c8dcSSimon Schubert int (*number_of_children) (struct varobj * parent);
4075796c8dcSSimon Schubert
4085796c8dcSSimon Schubert /* The name (expression) of a root varobj. */
4095796c8dcSSimon Schubert char *(*name_of_variable) (struct varobj * parent);
4105796c8dcSSimon Schubert
4115796c8dcSSimon Schubert /* The name of the INDEX'th child of PARENT. */
4125796c8dcSSimon Schubert char *(*name_of_child) (struct varobj * parent, int index);
4135796c8dcSSimon Schubert
4145796c8dcSSimon Schubert /* Returns the rooted expression of CHILD, which is a variable
4155796c8dcSSimon Schubert obtain that has some parent. */
4165796c8dcSSimon Schubert char *(*path_expr_of_child) (struct varobj * child);
4175796c8dcSSimon Schubert
4185796c8dcSSimon Schubert /* The ``struct value *'' of the root variable ROOT. */
4195796c8dcSSimon Schubert struct value *(*value_of_root) (struct varobj ** root_handle);
4205796c8dcSSimon Schubert
4215796c8dcSSimon Schubert /* The ``struct value *'' of the INDEX'th child of PARENT. */
4225796c8dcSSimon Schubert struct value *(*value_of_child) (struct varobj * parent, int index);
4235796c8dcSSimon Schubert
4245796c8dcSSimon Schubert /* The type of the INDEX'th child of PARENT. */
4255796c8dcSSimon Schubert struct type *(*type_of_child) (struct varobj * parent, int index);
4265796c8dcSSimon Schubert
4275796c8dcSSimon Schubert /* The current value of VAR. */
4285796c8dcSSimon Schubert char *(*value_of_variable) (struct varobj * var,
4295796c8dcSSimon Schubert enum varobj_display_formats format);
430*ef5ccd6cSJohn Marino
431*ef5ccd6cSJohn Marino /* Return non-zero if changes in value of VAR must be detected and
432*ef5ccd6cSJohn Marino reported by -var-update. Return zero if -var-update should never
433*ef5ccd6cSJohn Marino report changes of such values. This makes sense for structures
434*ef5ccd6cSJohn Marino (since the changes in children values will be reported separately),
435*ef5ccd6cSJohn Marino or for artifical objects (like 'public' pseudo-field in C++).
436*ef5ccd6cSJohn Marino
437*ef5ccd6cSJohn Marino Return value of 0 means that gdb need not call value_fetch_lazy
438*ef5ccd6cSJohn Marino for the value of this variable object. */
439*ef5ccd6cSJohn Marino int (*value_is_changeable_p) (struct varobj *var);
440*ef5ccd6cSJohn Marino
441*ef5ccd6cSJohn Marino /* Return nonzero if the type of VAR has mutated.
442*ef5ccd6cSJohn Marino
443*ef5ccd6cSJohn Marino VAR's value is still the varobj's previous value, while NEW_VALUE
444*ef5ccd6cSJohn Marino is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
445*ef5ccd6cSJohn Marino may be NULL indicating that there is no value available (the varobj
446*ef5ccd6cSJohn Marino may be out of scope, of may be the child of a null pointer, for
447*ef5ccd6cSJohn Marino instance). NEW_TYPE, on the other hand, must never be NULL.
448*ef5ccd6cSJohn Marino
449*ef5ccd6cSJohn Marino This function should also be able to assume that var's number of
450*ef5ccd6cSJohn Marino children is set (not < 0).
451*ef5ccd6cSJohn Marino
452*ef5ccd6cSJohn Marino Languages where types do not mutate can set this to NULL. */
453*ef5ccd6cSJohn Marino int (*value_has_mutated) (struct varobj *var, struct value *new_value,
454*ef5ccd6cSJohn Marino struct type *new_type);
4555796c8dcSSimon Schubert };
4565796c8dcSSimon Schubert
4575796c8dcSSimon Schubert /* Array of known source language routines. */
4585796c8dcSSimon Schubert static struct language_specific languages[vlang_end] = {
459c50c785cSJohn Marino /* Unknown (try treating as C). */
4605796c8dcSSimon Schubert {
4615796c8dcSSimon Schubert vlang_unknown,
4625796c8dcSSimon Schubert c_number_of_children,
4635796c8dcSSimon Schubert c_name_of_variable,
4645796c8dcSSimon Schubert c_name_of_child,
4655796c8dcSSimon Schubert c_path_expr_of_child,
4665796c8dcSSimon Schubert c_value_of_root,
4675796c8dcSSimon Schubert c_value_of_child,
4685796c8dcSSimon Schubert c_type_of_child,
469*ef5ccd6cSJohn Marino c_value_of_variable,
470*ef5ccd6cSJohn Marino default_value_is_changeable_p,
471*ef5ccd6cSJohn Marino NULL /* value_has_mutated */}
4725796c8dcSSimon Schubert ,
4735796c8dcSSimon Schubert /* C */
4745796c8dcSSimon Schubert {
4755796c8dcSSimon Schubert vlang_c,
4765796c8dcSSimon Schubert c_number_of_children,
4775796c8dcSSimon Schubert c_name_of_variable,
4785796c8dcSSimon Schubert c_name_of_child,
4795796c8dcSSimon Schubert c_path_expr_of_child,
4805796c8dcSSimon Schubert c_value_of_root,
4815796c8dcSSimon Schubert c_value_of_child,
4825796c8dcSSimon Schubert c_type_of_child,
483*ef5ccd6cSJohn Marino c_value_of_variable,
484*ef5ccd6cSJohn Marino default_value_is_changeable_p,
485*ef5ccd6cSJohn Marino NULL /* value_has_mutated */}
4865796c8dcSSimon Schubert ,
4875796c8dcSSimon Schubert /* C++ */
4885796c8dcSSimon Schubert {
4895796c8dcSSimon Schubert vlang_cplus,
4905796c8dcSSimon Schubert cplus_number_of_children,
4915796c8dcSSimon Schubert cplus_name_of_variable,
4925796c8dcSSimon Schubert cplus_name_of_child,
4935796c8dcSSimon Schubert cplus_path_expr_of_child,
4945796c8dcSSimon Schubert cplus_value_of_root,
4955796c8dcSSimon Schubert cplus_value_of_child,
4965796c8dcSSimon Schubert cplus_type_of_child,
497*ef5ccd6cSJohn Marino cplus_value_of_variable,
498*ef5ccd6cSJohn Marino default_value_is_changeable_p,
499*ef5ccd6cSJohn Marino NULL /* value_has_mutated */}
5005796c8dcSSimon Schubert ,
5015796c8dcSSimon Schubert /* Java */
5025796c8dcSSimon Schubert {
5035796c8dcSSimon Schubert vlang_java,
5045796c8dcSSimon Schubert java_number_of_children,
5055796c8dcSSimon Schubert java_name_of_variable,
5065796c8dcSSimon Schubert java_name_of_child,
5075796c8dcSSimon Schubert java_path_expr_of_child,
5085796c8dcSSimon Schubert java_value_of_root,
5095796c8dcSSimon Schubert java_value_of_child,
5105796c8dcSSimon Schubert java_type_of_child,
511*ef5ccd6cSJohn Marino java_value_of_variable,
512*ef5ccd6cSJohn Marino default_value_is_changeable_p,
513*ef5ccd6cSJohn Marino NULL /* value_has_mutated */},
514a45ae5f8SJohn Marino /* Ada */
515a45ae5f8SJohn Marino {
516a45ae5f8SJohn Marino vlang_ada,
517a45ae5f8SJohn Marino ada_number_of_children,
518a45ae5f8SJohn Marino ada_name_of_variable,
519a45ae5f8SJohn Marino ada_name_of_child,
520a45ae5f8SJohn Marino ada_path_expr_of_child,
521a45ae5f8SJohn Marino ada_value_of_root,
522a45ae5f8SJohn Marino ada_value_of_child,
523a45ae5f8SJohn Marino ada_type_of_child,
524*ef5ccd6cSJohn Marino ada_value_of_variable,
525*ef5ccd6cSJohn Marino ada_value_is_changeable_p,
526*ef5ccd6cSJohn Marino ada_value_has_mutated}
5275796c8dcSSimon Schubert };
5285796c8dcSSimon Schubert
529c50c785cSJohn Marino /* A little convenience enum for dealing with C++/Java. */
5305796c8dcSSimon Schubert enum vsections
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert v_public = 0, v_private, v_protected
5335796c8dcSSimon Schubert };
5345796c8dcSSimon Schubert
5355796c8dcSSimon Schubert /* Private data */
5365796c8dcSSimon Schubert
537c50c785cSJohn Marino /* Mappings of varobj_display_formats enums to gdb's format codes. */
5385796c8dcSSimon Schubert static int format_code[] = { 0, 't', 'd', 'x', 'o' };
5395796c8dcSSimon Schubert
540c50c785cSJohn Marino /* Header of the list of root variable objects. */
5415796c8dcSSimon Schubert static struct varobj_root *rootlist;
5425796c8dcSSimon Schubert
543c50c785cSJohn Marino /* Prime number indicating the number of buckets in the hash table. */
544c50c785cSJohn Marino /* A prime large enough to avoid too many colisions. */
5455796c8dcSSimon Schubert #define VAROBJ_TABLE_SIZE 227
5465796c8dcSSimon Schubert
547c50c785cSJohn Marino /* Pointer to the varobj hash table (built at run time). */
5485796c8dcSSimon Schubert static struct vlist **varobj_table;
5495796c8dcSSimon Schubert
5505796c8dcSSimon Schubert /* Is the variable X one of our "fake" children? */
5515796c8dcSSimon Schubert #define CPLUS_FAKE_CHILD(x) \
5525796c8dcSSimon Schubert ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
5535796c8dcSSimon Schubert
5545796c8dcSSimon Schubert
5555796c8dcSSimon Schubert /* API Implementation */
5565796c8dcSSimon Schubert static int
is_root_p(struct varobj * var)5575796c8dcSSimon Schubert is_root_p (struct varobj *var)
5585796c8dcSSimon Schubert {
5595796c8dcSSimon Schubert return (var->root->rootvar == var);
5605796c8dcSSimon Schubert }
5615796c8dcSSimon Schubert
5625796c8dcSSimon Schubert #ifdef HAVE_PYTHON
5635796c8dcSSimon Schubert /* Helper function to install a Python environment suitable for
5645796c8dcSSimon Schubert use during operations on VAR. */
565*ef5ccd6cSJohn Marino static struct cleanup *
varobj_ensure_python_env(struct varobj * var)5665796c8dcSSimon Schubert varobj_ensure_python_env (struct varobj *var)
5675796c8dcSSimon Schubert {
5685796c8dcSSimon Schubert return ensure_python_env (var->root->exp->gdbarch,
5695796c8dcSSimon Schubert var->root->exp->language_defn);
5705796c8dcSSimon Schubert }
5715796c8dcSSimon Schubert #endif
5725796c8dcSSimon Schubert
573c50c785cSJohn Marino /* Creates a varobj (not its children). */
5745796c8dcSSimon Schubert
5755796c8dcSSimon Schubert /* Return the full FRAME which corresponds to the given CORE_ADDR
5765796c8dcSSimon Schubert or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
5775796c8dcSSimon Schubert
5785796c8dcSSimon Schubert static struct frame_info *
find_frame_addr_in_frame_chain(CORE_ADDR frame_addr)5795796c8dcSSimon Schubert find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
5805796c8dcSSimon Schubert {
5815796c8dcSSimon Schubert struct frame_info *frame = NULL;
5825796c8dcSSimon Schubert
5835796c8dcSSimon Schubert if (frame_addr == (CORE_ADDR) 0)
5845796c8dcSSimon Schubert return NULL;
5855796c8dcSSimon Schubert
5865796c8dcSSimon Schubert for (frame = get_current_frame ();
5875796c8dcSSimon Schubert frame != NULL;
5885796c8dcSSimon Schubert frame = get_prev_frame (frame))
5895796c8dcSSimon Schubert {
5905796c8dcSSimon Schubert /* The CORE_ADDR we get as argument was parsed from a string GDB
5915796c8dcSSimon Schubert output as $fp. This output got truncated to gdbarch_addr_bit.
5925796c8dcSSimon Schubert Truncate the frame base address in the same manner before
5935796c8dcSSimon Schubert comparing it against our argument. */
5945796c8dcSSimon Schubert CORE_ADDR frame_base = get_frame_base_address (frame);
5955796c8dcSSimon Schubert int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
596cf7f2e2dSJohn Marino
5975796c8dcSSimon Schubert if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
5985796c8dcSSimon Schubert frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
5995796c8dcSSimon Schubert
6005796c8dcSSimon Schubert if (frame_base == frame_addr)
6015796c8dcSSimon Schubert return frame;
6025796c8dcSSimon Schubert }
6035796c8dcSSimon Schubert
6045796c8dcSSimon Schubert return NULL;
6055796c8dcSSimon Schubert }
6065796c8dcSSimon Schubert
6075796c8dcSSimon Schubert struct varobj *
varobj_create(char * objname,char * expression,CORE_ADDR frame,enum varobj_type type)6085796c8dcSSimon Schubert varobj_create (char *objname,
6095796c8dcSSimon Schubert char *expression, CORE_ADDR frame, enum varobj_type type)
6105796c8dcSSimon Schubert {
6115796c8dcSSimon Schubert struct varobj *var;
6125796c8dcSSimon Schubert struct cleanup *old_chain;
6135796c8dcSSimon Schubert
6145796c8dcSSimon Schubert /* Fill out a varobj structure for the (root) variable being constructed. */
6155796c8dcSSimon Schubert var = new_root_variable ();
6165796c8dcSSimon Schubert old_chain = make_cleanup_free_variable (var);
6175796c8dcSSimon Schubert
6185796c8dcSSimon Schubert if (expression != NULL)
6195796c8dcSSimon Schubert {
620c50c785cSJohn Marino struct frame_info *fi;
621c50c785cSJohn Marino struct frame_id old_id = null_frame_id;
622c50c785cSJohn Marino struct block *block;
623*ef5ccd6cSJohn Marino const char *p;
6245796c8dcSSimon Schubert enum varobj_languages lang;
6255796c8dcSSimon Schubert struct value *value = NULL;
626*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
627*ef5ccd6cSJohn Marino CORE_ADDR pc;
6285796c8dcSSimon Schubert
6295796c8dcSSimon Schubert /* Parse and evaluate the expression, filling in as much of the
6305796c8dcSSimon Schubert variable's data as possible. */
6315796c8dcSSimon Schubert
6325796c8dcSSimon Schubert if (has_stack_frames ())
6335796c8dcSSimon Schubert {
634c50c785cSJohn Marino /* Allow creator to specify context of variable. */
6355796c8dcSSimon Schubert if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
6365796c8dcSSimon Schubert fi = get_selected_frame (NULL);
6375796c8dcSSimon Schubert else
6385796c8dcSSimon Schubert /* FIXME: cagney/2002-11-23: This code should be doing a
6395796c8dcSSimon Schubert lookup using the frame ID and not just the frame's
6405796c8dcSSimon Schubert ``address''. This, of course, means an interface
6415796c8dcSSimon Schubert change. However, with out that interface change ISAs,
6425796c8dcSSimon Schubert such as the ia64 with its two stacks, won't work.
6435796c8dcSSimon Schubert Similar goes for the case where there is a frameless
6445796c8dcSSimon Schubert function. */
6455796c8dcSSimon Schubert fi = find_frame_addr_in_frame_chain (frame);
6465796c8dcSSimon Schubert }
6475796c8dcSSimon Schubert else
6485796c8dcSSimon Schubert fi = NULL;
6495796c8dcSSimon Schubert
650c50c785cSJohn Marino /* frame = -2 means always use selected frame. */
6515796c8dcSSimon Schubert if (type == USE_SELECTED_FRAME)
6525796c8dcSSimon Schubert var->root->floating = 1;
6535796c8dcSSimon Schubert
654*ef5ccd6cSJohn Marino pc = 0;
6555796c8dcSSimon Schubert block = NULL;
6565796c8dcSSimon Schubert if (fi != NULL)
657*ef5ccd6cSJohn Marino {
6585796c8dcSSimon Schubert block = get_frame_block (fi, 0);
659*ef5ccd6cSJohn Marino pc = get_frame_pc (fi);
660*ef5ccd6cSJohn Marino }
6615796c8dcSSimon Schubert
6625796c8dcSSimon Schubert p = expression;
6635796c8dcSSimon Schubert innermost_block = NULL;
6645796c8dcSSimon Schubert /* Wrap the call to parse expression, so we can
6655796c8dcSSimon Schubert return a sensible error. */
666*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
667*ef5ccd6cSJohn Marino {
668*ef5ccd6cSJohn Marino var->root->exp = parse_exp_1 (&p, pc, block, 0);
669*ef5ccd6cSJohn Marino }
670*ef5ccd6cSJohn Marino
671*ef5ccd6cSJohn Marino if (except.reason < 0)
6725796c8dcSSimon Schubert {
673a45ae5f8SJohn Marino do_cleanups (old_chain);
6745796c8dcSSimon Schubert return NULL;
6755796c8dcSSimon Schubert }
6765796c8dcSSimon Schubert
6775796c8dcSSimon Schubert /* Don't allow variables to be created for types. */
678*ef5ccd6cSJohn Marino if (var->root->exp->elts[0].opcode == OP_TYPE
679*ef5ccd6cSJohn Marino || var->root->exp->elts[0].opcode == OP_TYPEOF
680*ef5ccd6cSJohn Marino || var->root->exp->elts[0].opcode == OP_DECLTYPE)
6815796c8dcSSimon Schubert {
6825796c8dcSSimon Schubert do_cleanups (old_chain);
6835796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
6845796c8dcSSimon Schubert " as an expression.\n");
6855796c8dcSSimon Schubert return NULL;
6865796c8dcSSimon Schubert }
6875796c8dcSSimon Schubert
6885796c8dcSSimon Schubert var->format = variable_default_display (var);
6895796c8dcSSimon Schubert var->root->valid_block = innermost_block;
6905796c8dcSSimon Schubert var->name = xstrdup (expression);
6915796c8dcSSimon Schubert /* For a root var, the name and the expr are the same. */
6925796c8dcSSimon Schubert var->path_expr = xstrdup (expression);
6935796c8dcSSimon Schubert
6945796c8dcSSimon Schubert /* When the frame is different from the current frame,
6955796c8dcSSimon Schubert we must select the appropriate frame before parsing
6965796c8dcSSimon Schubert the expression, otherwise the value will not be current.
6975796c8dcSSimon Schubert Since select_frame is so benign, just call it for all cases. */
6985796c8dcSSimon Schubert if (innermost_block)
6995796c8dcSSimon Schubert {
7005796c8dcSSimon Schubert /* User could specify explicit FRAME-ADDR which was not found but
7015796c8dcSSimon Schubert EXPRESSION is frame specific and we would not be able to evaluate
7025796c8dcSSimon Schubert it correctly next time. With VALID_BLOCK set we must also set
7035796c8dcSSimon Schubert FRAME and THREAD_ID. */
7045796c8dcSSimon Schubert if (fi == NULL)
7055796c8dcSSimon Schubert error (_("Failed to find the specified frame"));
7065796c8dcSSimon Schubert
7075796c8dcSSimon Schubert var->root->frame = get_frame_id (fi);
7085796c8dcSSimon Schubert var->root->thread_id = pid_to_thread_id (inferior_ptid);
709c50c785cSJohn Marino old_id = get_frame_id (get_selected_frame (NULL));
7105796c8dcSSimon Schubert select_frame (fi);
7115796c8dcSSimon Schubert }
7125796c8dcSSimon Schubert
7135796c8dcSSimon Schubert /* We definitely need to catch errors here.
7145796c8dcSSimon Schubert If evaluate_expression succeeds we got the value we wanted.
715c50c785cSJohn Marino But if it fails, we still go on with a call to evaluate_type(). */
716*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
717*ef5ccd6cSJohn Marino {
718*ef5ccd6cSJohn Marino value = evaluate_expression (var->root->exp);
719*ef5ccd6cSJohn Marino }
720*ef5ccd6cSJohn Marino
721*ef5ccd6cSJohn Marino if (except.reason < 0)
7225796c8dcSSimon Schubert {
7235796c8dcSSimon Schubert /* Error getting the value. Try to at least get the
7245796c8dcSSimon Schubert right type. */
7255796c8dcSSimon Schubert struct value *type_only_value = evaluate_type (var->root->exp);
726cf7f2e2dSJohn Marino
7275796c8dcSSimon Schubert var->type = value_type (type_only_value);
7285796c8dcSSimon Schubert }
7295796c8dcSSimon Schubert else
730*ef5ccd6cSJohn Marino {
731*ef5ccd6cSJohn Marino int real_type_found = 0;
7325796c8dcSSimon Schubert
733*ef5ccd6cSJohn Marino var->type = value_actual_type (value, 0, &real_type_found);
734*ef5ccd6cSJohn Marino if (real_type_found)
735*ef5ccd6cSJohn Marino value = value_cast (var->type, value);
736*ef5ccd6cSJohn Marino }
7375796c8dcSSimon Schubert
7385796c8dcSSimon Schubert /* Set language info */
7395796c8dcSSimon Schubert lang = variable_language (var);
7405796c8dcSSimon Schubert var->root->lang = &languages[lang];
7415796c8dcSSimon Schubert
742*ef5ccd6cSJohn Marino install_new_value (var, value, 1 /* Initial assignment */);
743*ef5ccd6cSJohn Marino
744c50c785cSJohn Marino /* Set ourselves as our root. */
7455796c8dcSSimon Schubert var->root->rootvar = var;
7465796c8dcSSimon Schubert
747c50c785cSJohn Marino /* Reset the selected frame. */
748c50c785cSJohn Marino if (frame_id_p (old_id))
749c50c785cSJohn Marino select_frame (frame_find_by_id (old_id));
7505796c8dcSSimon Schubert }
7515796c8dcSSimon Schubert
7525796c8dcSSimon Schubert /* If the variable object name is null, that means this
7535796c8dcSSimon Schubert is a temporary variable, so don't install it. */
7545796c8dcSSimon Schubert
7555796c8dcSSimon Schubert if ((var != NULL) && (objname != NULL))
7565796c8dcSSimon Schubert {
7575796c8dcSSimon Schubert var->obj_name = xstrdup (objname);
7585796c8dcSSimon Schubert
7595796c8dcSSimon Schubert /* If a varobj name is duplicated, the install will fail so
760c50c785cSJohn Marino we must cleanup. */
7615796c8dcSSimon Schubert if (!install_variable (var))
7625796c8dcSSimon Schubert {
7635796c8dcSSimon Schubert do_cleanups (old_chain);
7645796c8dcSSimon Schubert return NULL;
7655796c8dcSSimon Schubert }
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert
7685796c8dcSSimon Schubert discard_cleanups (old_chain);
7695796c8dcSSimon Schubert return var;
7705796c8dcSSimon Schubert }
7715796c8dcSSimon Schubert
772c50c785cSJohn Marino /* Generates an unique name that can be used for a varobj. */
7735796c8dcSSimon Schubert
7745796c8dcSSimon Schubert char *
varobj_gen_name(void)7755796c8dcSSimon Schubert varobj_gen_name (void)
7765796c8dcSSimon Schubert {
7775796c8dcSSimon Schubert static int id = 0;
7785796c8dcSSimon Schubert char *obj_name;
7795796c8dcSSimon Schubert
780c50c785cSJohn Marino /* Generate a name for this object. */
7815796c8dcSSimon Schubert id++;
7825796c8dcSSimon Schubert obj_name = xstrprintf ("var%d", id);
7835796c8dcSSimon Schubert
7845796c8dcSSimon Schubert return obj_name;
7855796c8dcSSimon Schubert }
7865796c8dcSSimon Schubert
7875796c8dcSSimon Schubert /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
7885796c8dcSSimon Schubert error if OBJNAME cannot be found. */
7895796c8dcSSimon Schubert
7905796c8dcSSimon Schubert struct varobj *
varobj_get_handle(char * objname)7915796c8dcSSimon Schubert varobj_get_handle (char *objname)
7925796c8dcSSimon Schubert {
7935796c8dcSSimon Schubert struct vlist *cv;
7945796c8dcSSimon Schubert const char *chp;
7955796c8dcSSimon Schubert unsigned int index = 0;
7965796c8dcSSimon Schubert unsigned int i = 1;
7975796c8dcSSimon Schubert
7985796c8dcSSimon Schubert for (chp = objname; *chp; chp++)
7995796c8dcSSimon Schubert {
8005796c8dcSSimon Schubert index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
8015796c8dcSSimon Schubert }
8025796c8dcSSimon Schubert
8035796c8dcSSimon Schubert cv = *(varobj_table + index);
8045796c8dcSSimon Schubert while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
8055796c8dcSSimon Schubert cv = cv->next;
8065796c8dcSSimon Schubert
8075796c8dcSSimon Schubert if (cv == NULL)
8085796c8dcSSimon Schubert error (_("Variable object not found"));
8095796c8dcSSimon Schubert
8105796c8dcSSimon Schubert return cv->var;
8115796c8dcSSimon Schubert }
8125796c8dcSSimon Schubert
813c50c785cSJohn Marino /* Given the handle, return the name of the object. */
8145796c8dcSSimon Schubert
8155796c8dcSSimon Schubert char *
varobj_get_objname(struct varobj * var)8165796c8dcSSimon Schubert varobj_get_objname (struct varobj *var)
8175796c8dcSSimon Schubert {
8185796c8dcSSimon Schubert return var->obj_name;
8195796c8dcSSimon Schubert }
8205796c8dcSSimon Schubert
821c50c785cSJohn Marino /* Given the handle, return the expression represented by the object. */
8225796c8dcSSimon Schubert
8235796c8dcSSimon Schubert char *
varobj_get_expression(struct varobj * var)8245796c8dcSSimon Schubert varobj_get_expression (struct varobj *var)
8255796c8dcSSimon Schubert {
8265796c8dcSSimon Schubert return name_of_variable (var);
8275796c8dcSSimon Schubert }
8285796c8dcSSimon Schubert
8295796c8dcSSimon Schubert /* Deletes a varobj and all its children if only_children == 0,
830c50c785cSJohn Marino otherwise deletes only the children; returns a malloc'ed list of
831c50c785cSJohn Marino all the (malloc'ed) names of the variables that have been deleted
832c50c785cSJohn Marino (NULL terminated). */
8335796c8dcSSimon Schubert
8345796c8dcSSimon Schubert int
varobj_delete(struct varobj * var,char *** dellist,int only_children)8355796c8dcSSimon Schubert varobj_delete (struct varobj *var, char ***dellist, int only_children)
8365796c8dcSSimon Schubert {
8375796c8dcSSimon Schubert int delcount;
8385796c8dcSSimon Schubert int mycount;
8395796c8dcSSimon Schubert struct cpstack *result = NULL;
8405796c8dcSSimon Schubert char **cp;
8415796c8dcSSimon Schubert
842c50c785cSJohn Marino /* Initialize a stack for temporary results. */
8435796c8dcSSimon Schubert cppush (&result, NULL);
8445796c8dcSSimon Schubert
8455796c8dcSSimon Schubert if (only_children)
846c50c785cSJohn Marino /* Delete only the variable children. */
8475796c8dcSSimon Schubert delcount = delete_variable (&result, var, 1 /* only the children */ );
8485796c8dcSSimon Schubert else
849c50c785cSJohn Marino /* Delete the variable and all its children. */
8505796c8dcSSimon Schubert delcount = delete_variable (&result, var, 0 /* parent+children */ );
8515796c8dcSSimon Schubert
852c50c785cSJohn Marino /* We may have been asked to return a list of what has been deleted. */
8535796c8dcSSimon Schubert if (dellist != NULL)
8545796c8dcSSimon Schubert {
8555796c8dcSSimon Schubert *dellist = xmalloc ((delcount + 1) * sizeof (char *));
8565796c8dcSSimon Schubert
8575796c8dcSSimon Schubert cp = *dellist;
8585796c8dcSSimon Schubert mycount = delcount;
8595796c8dcSSimon Schubert *cp = cppop (&result);
8605796c8dcSSimon Schubert while ((*cp != NULL) && (mycount > 0))
8615796c8dcSSimon Schubert {
8625796c8dcSSimon Schubert mycount--;
8635796c8dcSSimon Schubert cp++;
8645796c8dcSSimon Schubert *cp = cppop (&result);
8655796c8dcSSimon Schubert }
8665796c8dcSSimon Schubert
8675796c8dcSSimon Schubert if (mycount || (*cp != NULL))
8685796c8dcSSimon Schubert warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
8695796c8dcSSimon Schubert mycount);
8705796c8dcSSimon Schubert }
8715796c8dcSSimon Schubert
8725796c8dcSSimon Schubert return delcount;
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert
875cf7f2e2dSJohn Marino #if HAVE_PYTHON
876cf7f2e2dSJohn Marino
8775796c8dcSSimon Schubert /* Convenience function for varobj_set_visualizer. Instantiate a
8785796c8dcSSimon Schubert pretty-printer for a given value. */
8795796c8dcSSimon Schubert static PyObject *
instantiate_pretty_printer(PyObject * constructor,struct value * value)8805796c8dcSSimon Schubert instantiate_pretty_printer (PyObject *constructor, struct value *value)
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert PyObject *val_obj = NULL;
8835796c8dcSSimon Schubert PyObject *printer;
8845796c8dcSSimon Schubert
8855796c8dcSSimon Schubert val_obj = value_to_value_object (value);
8865796c8dcSSimon Schubert if (! val_obj)
8875796c8dcSSimon Schubert return NULL;
8885796c8dcSSimon Schubert
8895796c8dcSSimon Schubert printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
8905796c8dcSSimon Schubert Py_DECREF (val_obj);
8915796c8dcSSimon Schubert return printer;
8925796c8dcSSimon Schubert }
8935796c8dcSSimon Schubert
894cf7f2e2dSJohn Marino #endif
895cf7f2e2dSJohn Marino
896c50c785cSJohn Marino /* Set/Get variable object display format. */
8975796c8dcSSimon Schubert
8985796c8dcSSimon Schubert enum varobj_display_formats
varobj_set_display_format(struct varobj * var,enum varobj_display_formats format)8995796c8dcSSimon Schubert varobj_set_display_format (struct varobj *var,
9005796c8dcSSimon Schubert enum varobj_display_formats format)
9015796c8dcSSimon Schubert {
9025796c8dcSSimon Schubert switch (format)
9035796c8dcSSimon Schubert {
9045796c8dcSSimon Schubert case FORMAT_NATURAL:
9055796c8dcSSimon Schubert case FORMAT_BINARY:
9065796c8dcSSimon Schubert case FORMAT_DECIMAL:
9075796c8dcSSimon Schubert case FORMAT_HEXADECIMAL:
9085796c8dcSSimon Schubert case FORMAT_OCTAL:
9095796c8dcSSimon Schubert var->format = format;
9105796c8dcSSimon Schubert break;
9115796c8dcSSimon Schubert
9125796c8dcSSimon Schubert default:
9135796c8dcSSimon Schubert var->format = variable_default_display (var);
9145796c8dcSSimon Schubert }
9155796c8dcSSimon Schubert
9165796c8dcSSimon Schubert if (varobj_value_is_changeable_p (var)
9175796c8dcSSimon Schubert && var->value && !value_lazy (var->value))
9185796c8dcSSimon Schubert {
9195796c8dcSSimon Schubert xfree (var->print_value);
9205796c8dcSSimon Schubert var->print_value = value_get_print_value (var->value, var->format, var);
9215796c8dcSSimon Schubert }
9225796c8dcSSimon Schubert
9235796c8dcSSimon Schubert return var->format;
9245796c8dcSSimon Schubert }
9255796c8dcSSimon Schubert
9265796c8dcSSimon Schubert enum varobj_display_formats
varobj_get_display_format(struct varobj * var)9275796c8dcSSimon Schubert varobj_get_display_format (struct varobj *var)
9285796c8dcSSimon Schubert {
9295796c8dcSSimon Schubert return var->format;
9305796c8dcSSimon Schubert }
9315796c8dcSSimon Schubert
9325796c8dcSSimon Schubert char *
varobj_get_display_hint(struct varobj * var)9335796c8dcSSimon Schubert varobj_get_display_hint (struct varobj *var)
9345796c8dcSSimon Schubert {
9355796c8dcSSimon Schubert char *result = NULL;
9365796c8dcSSimon Schubert
9375796c8dcSSimon Schubert #if HAVE_PYTHON
9385796c8dcSSimon Schubert struct cleanup *back_to = varobj_ensure_python_env (var);
9395796c8dcSSimon Schubert
9405796c8dcSSimon Schubert if (var->pretty_printer)
9415796c8dcSSimon Schubert result = gdbpy_get_display_hint (var->pretty_printer);
9425796c8dcSSimon Schubert
9435796c8dcSSimon Schubert do_cleanups (back_to);
9445796c8dcSSimon Schubert #endif
9455796c8dcSSimon Schubert
9465796c8dcSSimon Schubert return result;
9475796c8dcSSimon Schubert }
9485796c8dcSSimon Schubert
9495796c8dcSSimon Schubert /* Return true if the varobj has items after TO, false otherwise. */
9505796c8dcSSimon Schubert
9515796c8dcSSimon Schubert int
varobj_has_more(struct varobj * var,int to)9525796c8dcSSimon Schubert varobj_has_more (struct varobj *var, int to)
9535796c8dcSSimon Schubert {
9545796c8dcSSimon Schubert if (VEC_length (varobj_p, var->children) > to)
9555796c8dcSSimon Schubert return 1;
9565796c8dcSSimon Schubert return ((to == -1 || VEC_length (varobj_p, var->children) == to)
9575796c8dcSSimon Schubert && var->saved_item != NULL);
9585796c8dcSSimon Schubert }
9595796c8dcSSimon Schubert
9605796c8dcSSimon Schubert /* If the variable object is bound to a specific thread, that
9615796c8dcSSimon Schubert is its evaluation can always be done in context of a frame
9625796c8dcSSimon Schubert inside that thread, returns GDB id of the thread -- which
9635796c8dcSSimon Schubert is always positive. Otherwise, returns -1. */
9645796c8dcSSimon Schubert int
varobj_get_thread_id(struct varobj * var)9655796c8dcSSimon Schubert varobj_get_thread_id (struct varobj *var)
9665796c8dcSSimon Schubert {
9675796c8dcSSimon Schubert if (var->root->valid_block && var->root->thread_id > 0)
9685796c8dcSSimon Schubert return var->root->thread_id;
9695796c8dcSSimon Schubert else
9705796c8dcSSimon Schubert return -1;
9715796c8dcSSimon Schubert }
9725796c8dcSSimon Schubert
9735796c8dcSSimon Schubert void
varobj_set_frozen(struct varobj * var,int frozen)9745796c8dcSSimon Schubert varobj_set_frozen (struct varobj *var, int frozen)
9755796c8dcSSimon Schubert {
9765796c8dcSSimon Schubert /* When a variable is unfrozen, we don't fetch its value.
9775796c8dcSSimon Schubert The 'not_fetched' flag remains set, so next -var-update
9785796c8dcSSimon Schubert won't complain.
9795796c8dcSSimon Schubert
9805796c8dcSSimon Schubert We don't fetch the value, because for structures the client
9815796c8dcSSimon Schubert should do -var-update anyway. It would be bad to have different
9825796c8dcSSimon Schubert client-size logic for structure and other types. */
9835796c8dcSSimon Schubert var->frozen = frozen;
9845796c8dcSSimon Schubert }
9855796c8dcSSimon Schubert
9865796c8dcSSimon Schubert int
varobj_get_frozen(struct varobj * var)9875796c8dcSSimon Schubert varobj_get_frozen (struct varobj *var)
9885796c8dcSSimon Schubert {
9895796c8dcSSimon Schubert return var->frozen;
9905796c8dcSSimon Schubert }
9915796c8dcSSimon Schubert
9925796c8dcSSimon Schubert /* A helper function that restricts a range to what is actually
9935796c8dcSSimon Schubert available in a VEC. This follows the usual rules for the meaning
9945796c8dcSSimon Schubert of FROM and TO -- if either is negative, the entire range is
9955796c8dcSSimon Schubert used. */
9965796c8dcSSimon Schubert
9975796c8dcSSimon Schubert static void
restrict_range(VEC (varobj_p)* children,int * from,int * to)9985796c8dcSSimon Schubert restrict_range (VEC (varobj_p) *children, int *from, int *to)
9995796c8dcSSimon Schubert {
10005796c8dcSSimon Schubert if (*from < 0 || *to < 0)
10015796c8dcSSimon Schubert {
10025796c8dcSSimon Schubert *from = 0;
10035796c8dcSSimon Schubert *to = VEC_length (varobj_p, children);
10045796c8dcSSimon Schubert }
10055796c8dcSSimon Schubert else
10065796c8dcSSimon Schubert {
10075796c8dcSSimon Schubert if (*from > VEC_length (varobj_p, children))
10085796c8dcSSimon Schubert *from = VEC_length (varobj_p, children);
10095796c8dcSSimon Schubert if (*to > VEC_length (varobj_p, children))
10105796c8dcSSimon Schubert *to = VEC_length (varobj_p, children);
10115796c8dcSSimon Schubert if (*from > *to)
10125796c8dcSSimon Schubert *from = *to;
10135796c8dcSSimon Schubert }
10145796c8dcSSimon Schubert }
10155796c8dcSSimon Schubert
1016cf7f2e2dSJohn Marino #if HAVE_PYTHON
1017cf7f2e2dSJohn Marino
10185796c8dcSSimon Schubert /* A helper for update_dynamic_varobj_children that installs a new
10195796c8dcSSimon Schubert child when needed. */
10205796c8dcSSimon Schubert
10215796c8dcSSimon Schubert static void
install_dynamic_child(struct varobj * var,VEC (varobj_p)** changed,VEC (varobj_p)** type_changed,VEC (varobj_p)** new,VEC (varobj_p)** unchanged,int * cchanged,int index,const char * name,struct value * value)10225796c8dcSSimon Schubert install_dynamic_child (struct varobj *var,
10235796c8dcSSimon Schubert VEC (varobj_p) **changed,
1024*ef5ccd6cSJohn Marino VEC (varobj_p) **type_changed,
10255796c8dcSSimon Schubert VEC (varobj_p) **new,
10265796c8dcSSimon Schubert VEC (varobj_p) **unchanged,
10275796c8dcSSimon Schubert int *cchanged,
10285796c8dcSSimon Schubert int index,
10295796c8dcSSimon Schubert const char *name,
10305796c8dcSSimon Schubert struct value *value)
10315796c8dcSSimon Schubert {
10325796c8dcSSimon Schubert if (VEC_length (varobj_p, var->children) < index + 1)
10335796c8dcSSimon Schubert {
10345796c8dcSSimon Schubert /* There's no child yet. */
10355796c8dcSSimon Schubert struct varobj *child = varobj_add_child (var, name, value);
1036cf7f2e2dSJohn Marino
10375796c8dcSSimon Schubert if (new)
10385796c8dcSSimon Schubert {
10395796c8dcSSimon Schubert VEC_safe_push (varobj_p, *new, child);
10405796c8dcSSimon Schubert *cchanged = 1;
10415796c8dcSSimon Schubert }
10425796c8dcSSimon Schubert }
10435796c8dcSSimon Schubert else
10445796c8dcSSimon Schubert {
10455796c8dcSSimon Schubert varobj_p existing = VEC_index (varobj_p, var->children, index);
1046cf7f2e2dSJohn Marino
1047*ef5ccd6cSJohn Marino int type_updated = update_type_if_necessary (existing, value);
1048*ef5ccd6cSJohn Marino if (type_updated)
1049*ef5ccd6cSJohn Marino {
1050*ef5ccd6cSJohn Marino if (type_changed)
1051*ef5ccd6cSJohn Marino VEC_safe_push (varobj_p, *type_changed, existing);
1052*ef5ccd6cSJohn Marino }
10535796c8dcSSimon Schubert if (install_new_value (existing, value, 0))
10545796c8dcSSimon Schubert {
1055*ef5ccd6cSJohn Marino if (!type_updated && changed)
10565796c8dcSSimon Schubert VEC_safe_push (varobj_p, *changed, existing);
10575796c8dcSSimon Schubert }
1058*ef5ccd6cSJohn Marino else if (!type_updated && unchanged)
10595796c8dcSSimon Schubert VEC_safe_push (varobj_p, *unchanged, existing);
10605796c8dcSSimon Schubert }
10615796c8dcSSimon Schubert }
10625796c8dcSSimon Schubert
10635796c8dcSSimon Schubert static int
dynamic_varobj_has_child_method(struct varobj * var)10645796c8dcSSimon Schubert dynamic_varobj_has_child_method (struct varobj *var)
10655796c8dcSSimon Schubert {
10665796c8dcSSimon Schubert struct cleanup *back_to;
10675796c8dcSSimon Schubert PyObject *printer = var->pretty_printer;
10685796c8dcSSimon Schubert int result;
10695796c8dcSSimon Schubert
10705796c8dcSSimon Schubert back_to = varobj_ensure_python_env (var);
10715796c8dcSSimon Schubert result = PyObject_HasAttr (printer, gdbpy_children_cst);
10725796c8dcSSimon Schubert do_cleanups (back_to);
10735796c8dcSSimon Schubert return result;
10745796c8dcSSimon Schubert }
10755796c8dcSSimon Schubert
10765796c8dcSSimon Schubert #endif
10775796c8dcSSimon Schubert
10785796c8dcSSimon Schubert static int
update_dynamic_varobj_children(struct varobj * var,VEC (varobj_p)** changed,VEC (varobj_p)** type_changed,VEC (varobj_p)** new,VEC (varobj_p)** unchanged,int * cchanged,int update_children,int from,int to)10795796c8dcSSimon Schubert update_dynamic_varobj_children (struct varobj *var,
10805796c8dcSSimon Schubert VEC (varobj_p) **changed,
1081*ef5ccd6cSJohn Marino VEC (varobj_p) **type_changed,
10825796c8dcSSimon Schubert VEC (varobj_p) **new,
10835796c8dcSSimon Schubert VEC (varobj_p) **unchanged,
10845796c8dcSSimon Schubert int *cchanged,
10855796c8dcSSimon Schubert int update_children,
10865796c8dcSSimon Schubert int from,
10875796c8dcSSimon Schubert int to)
10885796c8dcSSimon Schubert {
10895796c8dcSSimon Schubert #if HAVE_PYTHON
10905796c8dcSSimon Schubert struct cleanup *back_to;
10915796c8dcSSimon Schubert PyObject *children;
10925796c8dcSSimon Schubert int i;
10935796c8dcSSimon Schubert PyObject *printer = var->pretty_printer;
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert back_to = varobj_ensure_python_env (var);
10965796c8dcSSimon Schubert
10975796c8dcSSimon Schubert *cchanged = 0;
10985796c8dcSSimon Schubert if (!PyObject_HasAttr (printer, gdbpy_children_cst))
10995796c8dcSSimon Schubert {
11005796c8dcSSimon Schubert do_cleanups (back_to);
11015796c8dcSSimon Schubert return 0;
11025796c8dcSSimon Schubert }
11035796c8dcSSimon Schubert
11045796c8dcSSimon Schubert if (update_children || !var->child_iter)
11055796c8dcSSimon Schubert {
11065796c8dcSSimon Schubert children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
11075796c8dcSSimon Schubert NULL);
11085796c8dcSSimon Schubert
11095796c8dcSSimon Schubert if (!children)
11105796c8dcSSimon Schubert {
11115796c8dcSSimon Schubert gdbpy_print_stack ();
11125796c8dcSSimon Schubert error (_("Null value returned for children"));
11135796c8dcSSimon Schubert }
11145796c8dcSSimon Schubert
11155796c8dcSSimon Schubert make_cleanup_py_decref (children);
11165796c8dcSSimon Schubert
11175796c8dcSSimon Schubert Py_XDECREF (var->child_iter);
11185796c8dcSSimon Schubert var->child_iter = PyObject_GetIter (children);
11195796c8dcSSimon Schubert if (!var->child_iter)
11205796c8dcSSimon Schubert {
11215796c8dcSSimon Schubert gdbpy_print_stack ();
11225796c8dcSSimon Schubert error (_("Could not get children iterator"));
11235796c8dcSSimon Schubert }
11245796c8dcSSimon Schubert
11255796c8dcSSimon Schubert Py_XDECREF (var->saved_item);
11265796c8dcSSimon Schubert var->saved_item = NULL;
11275796c8dcSSimon Schubert
11285796c8dcSSimon Schubert i = 0;
11295796c8dcSSimon Schubert }
11305796c8dcSSimon Schubert else
11315796c8dcSSimon Schubert i = VEC_length (varobj_p, var->children);
11325796c8dcSSimon Schubert
11335796c8dcSSimon Schubert /* We ask for one extra child, so that MI can report whether there
11345796c8dcSSimon Schubert are more children. */
11355796c8dcSSimon Schubert for (; to < 0 || i < to + 1; ++i)
11365796c8dcSSimon Schubert {
11375796c8dcSSimon Schubert PyObject *item;
1138c50c785cSJohn Marino int force_done = 0;
11395796c8dcSSimon Schubert
11405796c8dcSSimon Schubert /* See if there was a leftover from last time. */
11415796c8dcSSimon Schubert if (var->saved_item)
11425796c8dcSSimon Schubert {
11435796c8dcSSimon Schubert item = var->saved_item;
11445796c8dcSSimon Schubert var->saved_item = NULL;
11455796c8dcSSimon Schubert }
11465796c8dcSSimon Schubert else
11475796c8dcSSimon Schubert item = PyIter_Next (var->child_iter);
11485796c8dcSSimon Schubert
11495796c8dcSSimon Schubert if (!item)
1150c50c785cSJohn Marino {
1151c50c785cSJohn Marino /* Normal end of iteration. */
1152c50c785cSJohn Marino if (!PyErr_Occurred ())
11535796c8dcSSimon Schubert break;
11545796c8dcSSimon Schubert
1155c50c785cSJohn Marino /* If we got a memory error, just use the text as the
1156c50c785cSJohn Marino item. */
1157c50c785cSJohn Marino if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1158c50c785cSJohn Marino {
1159c50c785cSJohn Marino PyObject *type, *value, *trace;
1160c50c785cSJohn Marino char *name_str, *value_str;
1161c50c785cSJohn Marino
1162c50c785cSJohn Marino PyErr_Fetch (&type, &value, &trace);
1163c50c785cSJohn Marino value_str = gdbpy_exception_to_string (type, value);
1164c50c785cSJohn Marino Py_XDECREF (type);
1165c50c785cSJohn Marino Py_XDECREF (value);
1166c50c785cSJohn Marino Py_XDECREF (trace);
1167c50c785cSJohn Marino if (!value_str)
1168c50c785cSJohn Marino {
1169c50c785cSJohn Marino gdbpy_print_stack ();
1170c50c785cSJohn Marino break;
1171c50c785cSJohn Marino }
1172c50c785cSJohn Marino
1173c50c785cSJohn Marino name_str = xstrprintf ("<error at %d>", i);
1174c50c785cSJohn Marino item = Py_BuildValue ("(ss)", name_str, value_str);
1175c50c785cSJohn Marino xfree (name_str);
1176c50c785cSJohn Marino xfree (value_str);
1177c50c785cSJohn Marino if (!item)
1178c50c785cSJohn Marino {
1179c50c785cSJohn Marino gdbpy_print_stack ();
1180c50c785cSJohn Marino break;
1181c50c785cSJohn Marino }
1182c50c785cSJohn Marino
1183c50c785cSJohn Marino force_done = 1;
1184c50c785cSJohn Marino }
1185c50c785cSJohn Marino else
1186c50c785cSJohn Marino {
1187c50c785cSJohn Marino /* Any other kind of error. */
1188c50c785cSJohn Marino gdbpy_print_stack ();
1189c50c785cSJohn Marino break;
1190c50c785cSJohn Marino }
1191c50c785cSJohn Marino }
1192c50c785cSJohn Marino
11935796c8dcSSimon Schubert /* We don't want to push the extra child on any report list. */
11945796c8dcSSimon Schubert if (to < 0 || i < to)
11955796c8dcSSimon Schubert {
11965796c8dcSSimon Schubert PyObject *py_v;
1197a45ae5f8SJohn Marino const char *name;
11985796c8dcSSimon Schubert struct value *v;
11995796c8dcSSimon Schubert struct cleanup *inner;
12005796c8dcSSimon Schubert int can_mention = from < 0 || i >= from;
12015796c8dcSSimon Schubert
12025796c8dcSSimon Schubert inner = make_cleanup_py_decref (item);
12035796c8dcSSimon Schubert
12045796c8dcSSimon Schubert if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1205c50c785cSJohn Marino {
1206c50c785cSJohn Marino gdbpy_print_stack ();
12075796c8dcSSimon Schubert error (_("Invalid item from the child list"));
1208c50c785cSJohn Marino }
12095796c8dcSSimon Schubert
12105796c8dcSSimon Schubert v = convert_value_from_python (py_v);
1211c50c785cSJohn Marino if (v == NULL)
1212c50c785cSJohn Marino gdbpy_print_stack ();
12135796c8dcSSimon Schubert install_dynamic_child (var, can_mention ? changed : NULL,
1214*ef5ccd6cSJohn Marino can_mention ? type_changed : NULL,
12155796c8dcSSimon Schubert can_mention ? new : NULL,
12165796c8dcSSimon Schubert can_mention ? unchanged : NULL,
12175796c8dcSSimon Schubert can_mention ? cchanged : NULL, i, name, v);
12185796c8dcSSimon Schubert do_cleanups (inner);
12195796c8dcSSimon Schubert }
12205796c8dcSSimon Schubert else
12215796c8dcSSimon Schubert {
12225796c8dcSSimon Schubert Py_XDECREF (var->saved_item);
12235796c8dcSSimon Schubert var->saved_item = item;
12245796c8dcSSimon Schubert
12255796c8dcSSimon Schubert /* We want to truncate the child list just before this
12265796c8dcSSimon Schubert element. */
12275796c8dcSSimon Schubert break;
12285796c8dcSSimon Schubert }
1229c50c785cSJohn Marino
1230c50c785cSJohn Marino if (force_done)
1231c50c785cSJohn Marino break;
12325796c8dcSSimon Schubert }
12335796c8dcSSimon Schubert
12345796c8dcSSimon Schubert if (i < VEC_length (varobj_p, var->children))
12355796c8dcSSimon Schubert {
12365796c8dcSSimon Schubert int j;
1237cf7f2e2dSJohn Marino
12385796c8dcSSimon Schubert *cchanged = 1;
12395796c8dcSSimon Schubert for (j = i; j < VEC_length (varobj_p, var->children); ++j)
12405796c8dcSSimon Schubert varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
12415796c8dcSSimon Schubert VEC_truncate (varobj_p, var->children, i);
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert
12445796c8dcSSimon Schubert /* If there are fewer children than requested, note that the list of
12455796c8dcSSimon Schubert children changed. */
12465796c8dcSSimon Schubert if (to >= 0 && VEC_length (varobj_p, var->children) < to)
12475796c8dcSSimon Schubert *cchanged = 1;
12485796c8dcSSimon Schubert
12495796c8dcSSimon Schubert var->num_children = VEC_length (varobj_p, var->children);
12505796c8dcSSimon Schubert
12515796c8dcSSimon Schubert do_cleanups (back_to);
12525796c8dcSSimon Schubert
12535796c8dcSSimon Schubert return 1;
12545796c8dcSSimon Schubert #else
12555796c8dcSSimon Schubert gdb_assert (0 && "should never be called if Python is not enabled");
12565796c8dcSSimon Schubert #endif
12575796c8dcSSimon Schubert }
12585796c8dcSSimon Schubert
12595796c8dcSSimon Schubert int
varobj_get_num_children(struct varobj * var)12605796c8dcSSimon Schubert varobj_get_num_children (struct varobj *var)
12615796c8dcSSimon Schubert {
12625796c8dcSSimon Schubert if (var->num_children == -1)
12635796c8dcSSimon Schubert {
12645796c8dcSSimon Schubert if (var->pretty_printer)
12655796c8dcSSimon Schubert {
12665796c8dcSSimon Schubert int dummy;
12675796c8dcSSimon Schubert
12685796c8dcSSimon Schubert /* If we have a dynamic varobj, don't report -1 children.
12695796c8dcSSimon Schubert So, try to fetch some children first. */
1270*ef5ccd6cSJohn Marino update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
12715796c8dcSSimon Schubert 0, 0, 0);
12725796c8dcSSimon Schubert }
12735796c8dcSSimon Schubert else
12745796c8dcSSimon Schubert var->num_children = number_of_children (var);
12755796c8dcSSimon Schubert }
12765796c8dcSSimon Schubert
12775796c8dcSSimon Schubert return var->num_children >= 0 ? var->num_children : 0;
12785796c8dcSSimon Schubert }
12795796c8dcSSimon Schubert
12805796c8dcSSimon Schubert /* Creates a list of the immediate children of a variable object;
1281c50c785cSJohn Marino the return code is the number of such children or -1 on error. */
12825796c8dcSSimon Schubert
VEC(varobj_p)12835796c8dcSSimon Schubert VEC (varobj_p)*
12845796c8dcSSimon Schubert varobj_list_children (struct varobj *var, int *from, int *to)
12855796c8dcSSimon Schubert {
12865796c8dcSSimon Schubert char *name;
12875796c8dcSSimon Schubert int i, children_changed;
12885796c8dcSSimon Schubert
12895796c8dcSSimon Schubert var->children_requested = 1;
12905796c8dcSSimon Schubert
12915796c8dcSSimon Schubert if (var->pretty_printer)
12925796c8dcSSimon Schubert {
12935796c8dcSSimon Schubert /* This, in theory, can result in the number of children changing without
12945796c8dcSSimon Schubert frontend noticing. But well, calling -var-list-children on the same
12955796c8dcSSimon Schubert varobj twice is not something a sane frontend would do. */
1296*ef5ccd6cSJohn Marino update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
1297*ef5ccd6cSJohn Marino &children_changed, 0, 0, *to);
12985796c8dcSSimon Schubert restrict_range (var->children, from, to);
12995796c8dcSSimon Schubert return var->children;
13005796c8dcSSimon Schubert }
13015796c8dcSSimon Schubert
13025796c8dcSSimon Schubert if (var->num_children == -1)
13035796c8dcSSimon Schubert var->num_children = number_of_children (var);
13045796c8dcSSimon Schubert
13055796c8dcSSimon Schubert /* If that failed, give up. */
13065796c8dcSSimon Schubert if (var->num_children == -1)
13075796c8dcSSimon Schubert return var->children;
13085796c8dcSSimon Schubert
13095796c8dcSSimon Schubert /* If we're called when the list of children is not yet initialized,
13105796c8dcSSimon Schubert allocate enough elements in it. */
13115796c8dcSSimon Schubert while (VEC_length (varobj_p, var->children) < var->num_children)
13125796c8dcSSimon Schubert VEC_safe_push (varobj_p, var->children, NULL);
13135796c8dcSSimon Schubert
13145796c8dcSSimon Schubert for (i = 0; i < var->num_children; i++)
13155796c8dcSSimon Schubert {
13165796c8dcSSimon Schubert varobj_p existing = VEC_index (varobj_p, var->children, i);
13175796c8dcSSimon Schubert
13185796c8dcSSimon Schubert if (existing == NULL)
13195796c8dcSSimon Schubert {
13205796c8dcSSimon Schubert /* Either it's the first call to varobj_list_children for
13215796c8dcSSimon Schubert this variable object, and the child was never created,
13225796c8dcSSimon Schubert or it was explicitly deleted by the client. */
13235796c8dcSSimon Schubert name = name_of_child (var, i);
13245796c8dcSSimon Schubert existing = create_child (var, i, name);
13255796c8dcSSimon Schubert VEC_replace (varobj_p, var->children, i, existing);
13265796c8dcSSimon Schubert }
13275796c8dcSSimon Schubert }
13285796c8dcSSimon Schubert
13295796c8dcSSimon Schubert restrict_range (var->children, from, to);
13305796c8dcSSimon Schubert return var->children;
13315796c8dcSSimon Schubert }
13325796c8dcSSimon Schubert
1333cf7f2e2dSJohn Marino #if HAVE_PYTHON
1334cf7f2e2dSJohn Marino
13355796c8dcSSimon Schubert static struct varobj *
varobj_add_child(struct varobj * var,const char * name,struct value * value)13365796c8dcSSimon Schubert varobj_add_child (struct varobj *var, const char *name, struct value *value)
13375796c8dcSSimon Schubert {
13385796c8dcSSimon Schubert varobj_p v = create_child_with_value (var,
13395796c8dcSSimon Schubert VEC_length (varobj_p, var->children),
13405796c8dcSSimon Schubert name, value);
1341cf7f2e2dSJohn Marino
13425796c8dcSSimon Schubert VEC_safe_push (varobj_p, var->children, v);
13435796c8dcSSimon Schubert return v;
13445796c8dcSSimon Schubert }
13455796c8dcSSimon Schubert
1346cf7f2e2dSJohn Marino #endif /* HAVE_PYTHON */
1347cf7f2e2dSJohn Marino
13485796c8dcSSimon Schubert /* Obtain the type of an object Variable as a string similar to the one gdb
1349c50c785cSJohn Marino prints on the console. */
13505796c8dcSSimon Schubert
13515796c8dcSSimon Schubert char *
varobj_get_type(struct varobj * var)13525796c8dcSSimon Schubert varobj_get_type (struct varobj *var)
13535796c8dcSSimon Schubert {
13545796c8dcSSimon Schubert /* For the "fake" variables, do not return a type. (It's type is
13555796c8dcSSimon Schubert NULL, too.)
13565796c8dcSSimon Schubert Do not return a type for invalid variables as well. */
13575796c8dcSSimon Schubert if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
13585796c8dcSSimon Schubert return NULL;
13595796c8dcSSimon Schubert
13605796c8dcSSimon Schubert return type_to_string (var->type);
13615796c8dcSSimon Schubert }
13625796c8dcSSimon Schubert
13635796c8dcSSimon Schubert /* Obtain the type of an object variable. */
13645796c8dcSSimon Schubert
13655796c8dcSSimon Schubert struct type *
varobj_get_gdb_type(struct varobj * var)13665796c8dcSSimon Schubert varobj_get_gdb_type (struct varobj *var)
13675796c8dcSSimon Schubert {
13685796c8dcSSimon Schubert return var->type;
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert
1371*ef5ccd6cSJohn Marino /* Is VAR a path expression parent, i.e., can it be used to construct
1372*ef5ccd6cSJohn Marino a valid path expression? */
1373*ef5ccd6cSJohn Marino
1374*ef5ccd6cSJohn Marino static int
is_path_expr_parent(struct varobj * var)1375*ef5ccd6cSJohn Marino is_path_expr_parent (struct varobj *var)
1376*ef5ccd6cSJohn Marino {
1377*ef5ccd6cSJohn Marino struct type *type;
1378*ef5ccd6cSJohn Marino
1379*ef5ccd6cSJohn Marino /* "Fake" children are not path_expr parents. */
1380*ef5ccd6cSJohn Marino if (CPLUS_FAKE_CHILD (var))
1381*ef5ccd6cSJohn Marino return 0;
1382*ef5ccd6cSJohn Marino
1383*ef5ccd6cSJohn Marino type = get_value_type (var);
1384*ef5ccd6cSJohn Marino
1385*ef5ccd6cSJohn Marino /* Anonymous unions and structs are also not path_expr parents. */
1386*ef5ccd6cSJohn Marino return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1387*ef5ccd6cSJohn Marino || TYPE_CODE (type) == TYPE_CODE_UNION)
1388*ef5ccd6cSJohn Marino && TYPE_NAME (type) == NULL);
1389*ef5ccd6cSJohn Marino }
1390*ef5ccd6cSJohn Marino
1391*ef5ccd6cSJohn Marino /* Return the path expression parent for VAR. */
1392*ef5ccd6cSJohn Marino
1393*ef5ccd6cSJohn Marino static struct varobj *
get_path_expr_parent(struct varobj * var)1394*ef5ccd6cSJohn Marino get_path_expr_parent (struct varobj *var)
1395*ef5ccd6cSJohn Marino {
1396*ef5ccd6cSJohn Marino struct varobj *parent = var;
1397*ef5ccd6cSJohn Marino
1398*ef5ccd6cSJohn Marino while (!is_root_p (parent) && !is_path_expr_parent (parent))
1399*ef5ccd6cSJohn Marino parent = parent->parent;
1400*ef5ccd6cSJohn Marino
1401*ef5ccd6cSJohn Marino return parent;
1402*ef5ccd6cSJohn Marino }
1403*ef5ccd6cSJohn Marino
14045796c8dcSSimon Schubert /* Return a pointer to the full rooted expression of varobj VAR.
14055796c8dcSSimon Schubert If it has not been computed yet, compute it. */
14065796c8dcSSimon Schubert char *
varobj_get_path_expr(struct varobj * var)14075796c8dcSSimon Schubert varobj_get_path_expr (struct varobj *var)
14085796c8dcSSimon Schubert {
14095796c8dcSSimon Schubert if (var->path_expr != NULL)
14105796c8dcSSimon Schubert return var->path_expr;
14115796c8dcSSimon Schubert else
14125796c8dcSSimon Schubert {
14135796c8dcSSimon Schubert /* For root varobjs, we initialize path_expr
14145796c8dcSSimon Schubert when creating varobj, so here it should be
14155796c8dcSSimon Schubert child varobj. */
14165796c8dcSSimon Schubert gdb_assert (!is_root_p (var));
14175796c8dcSSimon Schubert return (*var->root->lang->path_expr_of_child) (var);
14185796c8dcSSimon Schubert }
14195796c8dcSSimon Schubert }
14205796c8dcSSimon Schubert
14215796c8dcSSimon Schubert enum varobj_languages
varobj_get_language(struct varobj * var)14225796c8dcSSimon Schubert varobj_get_language (struct varobj *var)
14235796c8dcSSimon Schubert {
14245796c8dcSSimon Schubert return variable_language (var);
14255796c8dcSSimon Schubert }
14265796c8dcSSimon Schubert
14275796c8dcSSimon Schubert int
varobj_get_attributes(struct varobj * var)14285796c8dcSSimon Schubert varobj_get_attributes (struct varobj *var)
14295796c8dcSSimon Schubert {
14305796c8dcSSimon Schubert int attributes = 0;
14315796c8dcSSimon Schubert
14325796c8dcSSimon Schubert if (varobj_editable_p (var))
1433c50c785cSJohn Marino /* FIXME: define masks for attributes. */
14345796c8dcSSimon Schubert attributes |= 0x00000001; /* Editable */
14355796c8dcSSimon Schubert
14365796c8dcSSimon Schubert return attributes;
14375796c8dcSSimon Schubert }
14385796c8dcSSimon Schubert
14395796c8dcSSimon Schubert int
varobj_pretty_printed_p(struct varobj * var)14405796c8dcSSimon Schubert varobj_pretty_printed_p (struct varobj *var)
14415796c8dcSSimon Schubert {
14425796c8dcSSimon Schubert return var->pretty_printer != NULL;
14435796c8dcSSimon Schubert }
14445796c8dcSSimon Schubert
14455796c8dcSSimon Schubert char *
varobj_get_formatted_value(struct varobj * var,enum varobj_display_formats format)14465796c8dcSSimon Schubert varobj_get_formatted_value (struct varobj *var,
14475796c8dcSSimon Schubert enum varobj_display_formats format)
14485796c8dcSSimon Schubert {
14495796c8dcSSimon Schubert return my_value_of_variable (var, format);
14505796c8dcSSimon Schubert }
14515796c8dcSSimon Schubert
14525796c8dcSSimon Schubert char *
varobj_get_value(struct varobj * var)14535796c8dcSSimon Schubert varobj_get_value (struct varobj *var)
14545796c8dcSSimon Schubert {
14555796c8dcSSimon Schubert return my_value_of_variable (var, var->format);
14565796c8dcSSimon Schubert }
14575796c8dcSSimon Schubert
14585796c8dcSSimon Schubert /* Set the value of an object variable (if it is editable) to the
1459c50c785cSJohn Marino value of the given expression. */
1460c50c785cSJohn Marino /* Note: Invokes functions that can call error(). */
14615796c8dcSSimon Schubert
14625796c8dcSSimon Schubert int
varobj_set_value(struct varobj * var,char * expression)14635796c8dcSSimon Schubert varobj_set_value (struct varobj *var, char *expression)
14645796c8dcSSimon Schubert {
1465*ef5ccd6cSJohn Marino struct value *val = NULL; /* Initialize to keep gcc happy. */
14665796c8dcSSimon Schubert /* The argument "expression" contains the variable's new value.
14675796c8dcSSimon Schubert We need to first construct a legal expression for this -- ugh! */
14685796c8dcSSimon Schubert /* Does this cover all the bases? */
14695796c8dcSSimon Schubert struct expression *exp;
1470*ef5ccd6cSJohn Marino struct value *value = NULL; /* Initialize to keep gcc happy. */
14715796c8dcSSimon Schubert int saved_input_radix = input_radix;
1472*ef5ccd6cSJohn Marino const char *s = expression;
1473*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
14745796c8dcSSimon Schubert
14755796c8dcSSimon Schubert gdb_assert (varobj_editable_p (var));
14765796c8dcSSimon Schubert
1477c50c785cSJohn Marino input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1478*ef5ccd6cSJohn Marino exp = parse_exp_1 (&s, 0, 0, 0);
1479*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
1480*ef5ccd6cSJohn Marino {
1481*ef5ccd6cSJohn Marino value = evaluate_expression (exp);
1482*ef5ccd6cSJohn Marino }
1483*ef5ccd6cSJohn Marino
1484*ef5ccd6cSJohn Marino if (except.reason < 0)
14855796c8dcSSimon Schubert {
14865796c8dcSSimon Schubert /* We cannot proceed without a valid expression. */
14875796c8dcSSimon Schubert xfree (exp);
14885796c8dcSSimon Schubert return 0;
14895796c8dcSSimon Schubert }
14905796c8dcSSimon Schubert
14915796c8dcSSimon Schubert /* All types that are editable must also be changeable. */
14925796c8dcSSimon Schubert gdb_assert (varobj_value_is_changeable_p (var));
14935796c8dcSSimon Schubert
14945796c8dcSSimon Schubert /* The value of a changeable variable object must not be lazy. */
14955796c8dcSSimon Schubert gdb_assert (!value_lazy (var->value));
14965796c8dcSSimon Schubert
14975796c8dcSSimon Schubert /* Need to coerce the input. We want to check if the
14985796c8dcSSimon Schubert value of the variable object will be different
14995796c8dcSSimon Schubert after assignment, and the first thing value_assign
15005796c8dcSSimon Schubert does is coerce the input.
15015796c8dcSSimon Schubert For example, if we are assigning an array to a pointer variable we
1502c50c785cSJohn Marino should compare the pointer with the array's address, not with the
15035796c8dcSSimon Schubert array's content. */
15045796c8dcSSimon Schubert value = coerce_array (value);
15055796c8dcSSimon Schubert
1506*ef5ccd6cSJohn Marino /* The new value may be lazy. value_assign, or
1507*ef5ccd6cSJohn Marino rather value_contents, will take care of this. */
1508*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
1509*ef5ccd6cSJohn Marino {
1510*ef5ccd6cSJohn Marino val = value_assign (var->value, value);
1511*ef5ccd6cSJohn Marino }
1512*ef5ccd6cSJohn Marino
1513*ef5ccd6cSJohn Marino if (except.reason < 0)
15145796c8dcSSimon Schubert return 0;
15155796c8dcSSimon Schubert
15165796c8dcSSimon Schubert /* If the value has changed, record it, so that next -var-update can
15175796c8dcSSimon Schubert report this change. If a variable had a value of '1', we've set it
15185796c8dcSSimon Schubert to '333' and then set again to '1', when -var-update will report this
15195796c8dcSSimon Schubert variable as changed -- because the first assignment has set the
15205796c8dcSSimon Schubert 'updated' flag. There's no need to optimize that, because return value
15215796c8dcSSimon Schubert of -var-update should be considered an approximation. */
15225796c8dcSSimon Schubert var->updated = install_new_value (var, val, 0 /* Compare values. */);
15235796c8dcSSimon Schubert input_radix = saved_input_radix;
15245796c8dcSSimon Schubert return 1;
15255796c8dcSSimon Schubert }
15265796c8dcSSimon Schubert
15275796c8dcSSimon Schubert #if HAVE_PYTHON
15285796c8dcSSimon Schubert
15295796c8dcSSimon Schubert /* A helper function to install a constructor function and visualizer
15305796c8dcSSimon Schubert in a varobj. */
15315796c8dcSSimon Schubert
15325796c8dcSSimon Schubert static void
install_visualizer(struct varobj * var,PyObject * constructor,PyObject * visualizer)15335796c8dcSSimon Schubert install_visualizer (struct varobj *var, PyObject *constructor,
15345796c8dcSSimon Schubert PyObject *visualizer)
15355796c8dcSSimon Schubert {
15365796c8dcSSimon Schubert Py_XDECREF (var->constructor);
15375796c8dcSSimon Schubert var->constructor = constructor;
15385796c8dcSSimon Schubert
15395796c8dcSSimon Schubert Py_XDECREF (var->pretty_printer);
15405796c8dcSSimon Schubert var->pretty_printer = visualizer;
15415796c8dcSSimon Schubert
15425796c8dcSSimon Schubert Py_XDECREF (var->child_iter);
15435796c8dcSSimon Schubert var->child_iter = NULL;
15445796c8dcSSimon Schubert }
15455796c8dcSSimon Schubert
15465796c8dcSSimon Schubert /* Install the default visualizer for VAR. */
15475796c8dcSSimon Schubert
15485796c8dcSSimon Schubert static void
install_default_visualizer(struct varobj * var)15495796c8dcSSimon Schubert install_default_visualizer (struct varobj *var)
15505796c8dcSSimon Schubert {
1551c50c785cSJohn Marino /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1552c50c785cSJohn Marino if (CPLUS_FAKE_CHILD (var))
1553c50c785cSJohn Marino return;
1554c50c785cSJohn Marino
15555796c8dcSSimon Schubert if (pretty_printing)
15565796c8dcSSimon Schubert {
15575796c8dcSSimon Schubert PyObject *pretty_printer = NULL;
15585796c8dcSSimon Schubert
15595796c8dcSSimon Schubert if (var->value)
15605796c8dcSSimon Schubert {
15615796c8dcSSimon Schubert pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
15625796c8dcSSimon Schubert if (! pretty_printer)
15635796c8dcSSimon Schubert {
15645796c8dcSSimon Schubert gdbpy_print_stack ();
15655796c8dcSSimon Schubert error (_("Cannot instantiate printer for default visualizer"));
15665796c8dcSSimon Schubert }
15675796c8dcSSimon Schubert }
15685796c8dcSSimon Schubert
15695796c8dcSSimon Schubert if (pretty_printer == Py_None)
15705796c8dcSSimon Schubert {
15715796c8dcSSimon Schubert Py_DECREF (pretty_printer);
15725796c8dcSSimon Schubert pretty_printer = NULL;
15735796c8dcSSimon Schubert }
15745796c8dcSSimon Schubert
15755796c8dcSSimon Schubert install_visualizer (var, NULL, pretty_printer);
15765796c8dcSSimon Schubert }
15775796c8dcSSimon Schubert }
15785796c8dcSSimon Schubert
15795796c8dcSSimon Schubert /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
15805796c8dcSSimon Schubert make a new object. */
15815796c8dcSSimon Schubert
15825796c8dcSSimon Schubert static void
construct_visualizer(struct varobj * var,PyObject * constructor)15835796c8dcSSimon Schubert construct_visualizer (struct varobj *var, PyObject *constructor)
15845796c8dcSSimon Schubert {
15855796c8dcSSimon Schubert PyObject *pretty_printer;
15865796c8dcSSimon Schubert
1587c50c785cSJohn Marino /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1588c50c785cSJohn Marino if (CPLUS_FAKE_CHILD (var))
1589c50c785cSJohn Marino return;
1590c50c785cSJohn Marino
15915796c8dcSSimon Schubert Py_INCREF (constructor);
15925796c8dcSSimon Schubert if (constructor == Py_None)
15935796c8dcSSimon Schubert pretty_printer = NULL;
15945796c8dcSSimon Schubert else
15955796c8dcSSimon Schubert {
15965796c8dcSSimon Schubert pretty_printer = instantiate_pretty_printer (constructor, var->value);
15975796c8dcSSimon Schubert if (! pretty_printer)
15985796c8dcSSimon Schubert {
15995796c8dcSSimon Schubert gdbpy_print_stack ();
16005796c8dcSSimon Schubert Py_DECREF (constructor);
16015796c8dcSSimon Schubert constructor = Py_None;
16025796c8dcSSimon Schubert Py_INCREF (constructor);
16035796c8dcSSimon Schubert }
16045796c8dcSSimon Schubert
16055796c8dcSSimon Schubert if (pretty_printer == Py_None)
16065796c8dcSSimon Schubert {
16075796c8dcSSimon Schubert Py_DECREF (pretty_printer);
16085796c8dcSSimon Schubert pretty_printer = NULL;
16095796c8dcSSimon Schubert }
16105796c8dcSSimon Schubert }
16115796c8dcSSimon Schubert
16125796c8dcSSimon Schubert install_visualizer (var, constructor, pretty_printer);
16135796c8dcSSimon Schubert }
16145796c8dcSSimon Schubert
16155796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
16165796c8dcSSimon Schubert
16175796c8dcSSimon Schubert /* A helper function for install_new_value. This creates and installs
16185796c8dcSSimon Schubert a visualizer for VAR, if appropriate. */
16195796c8dcSSimon Schubert
16205796c8dcSSimon Schubert static void
install_new_value_visualizer(struct varobj * var)16215796c8dcSSimon Schubert install_new_value_visualizer (struct varobj *var)
16225796c8dcSSimon Schubert {
16235796c8dcSSimon Schubert #if HAVE_PYTHON
16245796c8dcSSimon Schubert /* If the constructor is None, then we want the raw value. If VAR
16255796c8dcSSimon Schubert does not have a value, just skip this. */
16265796c8dcSSimon Schubert if (var->constructor != Py_None && var->value)
16275796c8dcSSimon Schubert {
16285796c8dcSSimon Schubert struct cleanup *cleanup;
16295796c8dcSSimon Schubert
16305796c8dcSSimon Schubert cleanup = varobj_ensure_python_env (var);
16315796c8dcSSimon Schubert
16325796c8dcSSimon Schubert if (!var->constructor)
16335796c8dcSSimon Schubert install_default_visualizer (var);
16345796c8dcSSimon Schubert else
16355796c8dcSSimon Schubert construct_visualizer (var, var->constructor);
16365796c8dcSSimon Schubert
16375796c8dcSSimon Schubert do_cleanups (cleanup);
16385796c8dcSSimon Schubert }
16395796c8dcSSimon Schubert #else
16405796c8dcSSimon Schubert /* Do nothing. */
16415796c8dcSSimon Schubert #endif
16425796c8dcSSimon Schubert }
16435796c8dcSSimon Schubert
1644*ef5ccd6cSJohn Marino /* When using RTTI to determine variable type it may be changed in runtime when
1645*ef5ccd6cSJohn Marino the variable value is changed. This function checks whether type of varobj
1646*ef5ccd6cSJohn Marino VAR will change when a new value NEW_VALUE is assigned and if it is so
1647*ef5ccd6cSJohn Marino updates the type of VAR. */
1648*ef5ccd6cSJohn Marino
1649*ef5ccd6cSJohn Marino static int
update_type_if_necessary(struct varobj * var,struct value * new_value)1650*ef5ccd6cSJohn Marino update_type_if_necessary (struct varobj *var, struct value *new_value)
1651*ef5ccd6cSJohn Marino {
1652*ef5ccd6cSJohn Marino if (new_value)
1653*ef5ccd6cSJohn Marino {
1654*ef5ccd6cSJohn Marino struct value_print_options opts;
1655*ef5ccd6cSJohn Marino
1656*ef5ccd6cSJohn Marino get_user_print_options (&opts);
1657*ef5ccd6cSJohn Marino if (opts.objectprint)
1658*ef5ccd6cSJohn Marino {
1659*ef5ccd6cSJohn Marino struct type *new_type;
1660*ef5ccd6cSJohn Marino char *curr_type_str, *new_type_str;
1661*ef5ccd6cSJohn Marino
1662*ef5ccd6cSJohn Marino new_type = value_actual_type (new_value, 0, 0);
1663*ef5ccd6cSJohn Marino new_type_str = type_to_string (new_type);
1664*ef5ccd6cSJohn Marino curr_type_str = varobj_get_type (var);
1665*ef5ccd6cSJohn Marino if (strcmp (curr_type_str, new_type_str) != 0)
1666*ef5ccd6cSJohn Marino {
1667*ef5ccd6cSJohn Marino var->type = new_type;
1668*ef5ccd6cSJohn Marino
1669*ef5ccd6cSJohn Marino /* This information may be not valid for a new type. */
1670*ef5ccd6cSJohn Marino varobj_delete (var, NULL, 1);
1671*ef5ccd6cSJohn Marino VEC_free (varobj_p, var->children);
1672*ef5ccd6cSJohn Marino var->num_children = -1;
1673*ef5ccd6cSJohn Marino return 1;
1674*ef5ccd6cSJohn Marino }
1675*ef5ccd6cSJohn Marino }
1676*ef5ccd6cSJohn Marino }
1677*ef5ccd6cSJohn Marino
1678*ef5ccd6cSJohn Marino return 0;
1679*ef5ccd6cSJohn Marino }
1680*ef5ccd6cSJohn Marino
16815796c8dcSSimon Schubert /* Assign a new value to a variable object. If INITIAL is non-zero,
16825796c8dcSSimon Schubert this is the first assignement after the variable object was just
16835796c8dcSSimon Schubert created, or changed type. In that case, just assign the value
16845796c8dcSSimon Schubert and return 0.
1685c50c785cSJohn Marino Otherwise, assign the new value, and return 1 if the value is
1686c50c785cSJohn Marino different from the current one, 0 otherwise. The comparison is
1687c50c785cSJohn Marino done on textual representation of value. Therefore, some types
1688c50c785cSJohn Marino need not be compared. E.g. for structures the reported value is
1689c50c785cSJohn Marino always "{...}", so no comparison is necessary here. If the old
1690c50c785cSJohn Marino value was NULL and new one is not, or vice versa, we always return 1.
16915796c8dcSSimon Schubert
16925796c8dcSSimon Schubert The VALUE parameter should not be released -- the function will
16935796c8dcSSimon Schubert take care of releasing it when needed. */
16945796c8dcSSimon Schubert static int
install_new_value(struct varobj * var,struct value * value,int initial)16955796c8dcSSimon Schubert install_new_value (struct varobj *var, struct value *value, int initial)
16965796c8dcSSimon Schubert {
16975796c8dcSSimon Schubert int changeable;
16985796c8dcSSimon Schubert int need_to_fetch;
16995796c8dcSSimon Schubert int changed = 0;
17005796c8dcSSimon Schubert int intentionally_not_fetched = 0;
17015796c8dcSSimon Schubert char *print_value = NULL;
17025796c8dcSSimon Schubert
17035796c8dcSSimon Schubert /* We need to know the varobj's type to decide if the value should
1704c50c785cSJohn Marino be fetched or not. C++ fake children (public/protected/private)
1705c50c785cSJohn Marino don't have a type. */
17065796c8dcSSimon Schubert gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
17075796c8dcSSimon Schubert changeable = varobj_value_is_changeable_p (var);
17085796c8dcSSimon Schubert
17095796c8dcSSimon Schubert /* If the type has custom visualizer, we consider it to be always
17105796c8dcSSimon Schubert changeable. FIXME: need to make sure this behaviour will not
17115796c8dcSSimon Schubert mess up read-sensitive values. */
17125796c8dcSSimon Schubert if (var->pretty_printer)
17135796c8dcSSimon Schubert changeable = 1;
17145796c8dcSSimon Schubert
17155796c8dcSSimon Schubert need_to_fetch = changeable;
17165796c8dcSSimon Schubert
17175796c8dcSSimon Schubert /* We are not interested in the address of references, and given
17185796c8dcSSimon Schubert that in C++ a reference is not rebindable, it cannot
17195796c8dcSSimon Schubert meaningfully change. So, get hold of the real value. */
17205796c8dcSSimon Schubert if (value)
17215796c8dcSSimon Schubert value = coerce_ref (value);
17225796c8dcSSimon Schubert
17235796c8dcSSimon Schubert if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
17245796c8dcSSimon Schubert /* For unions, we need to fetch the value implicitly because
17255796c8dcSSimon Schubert of implementation of union member fetch. When gdb
17265796c8dcSSimon Schubert creates a value for a field and the value of the enclosing
17275796c8dcSSimon Schubert structure is not lazy, it immediately copies the necessary
17285796c8dcSSimon Schubert bytes from the enclosing values. If the enclosing value is
17295796c8dcSSimon Schubert lazy, the call to value_fetch_lazy on the field will read
17305796c8dcSSimon Schubert the data from memory. For unions, that means we'll read the
17315796c8dcSSimon Schubert same memory more than once, which is not desirable. So
17325796c8dcSSimon Schubert fetch now. */
17335796c8dcSSimon Schubert need_to_fetch = 1;
17345796c8dcSSimon Schubert
17355796c8dcSSimon Schubert /* The new value might be lazy. If the type is changeable,
17365796c8dcSSimon Schubert that is we'll be comparing values of this type, fetch the
17375796c8dcSSimon Schubert value now. Otherwise, on the next update the old value
17385796c8dcSSimon Schubert will be lazy, which means we've lost that old value. */
17395796c8dcSSimon Schubert if (need_to_fetch && value && value_lazy (value))
17405796c8dcSSimon Schubert {
17415796c8dcSSimon Schubert struct varobj *parent = var->parent;
17425796c8dcSSimon Schubert int frozen = var->frozen;
1743cf7f2e2dSJohn Marino
17445796c8dcSSimon Schubert for (; !frozen && parent; parent = parent->parent)
17455796c8dcSSimon Schubert frozen |= parent->frozen;
17465796c8dcSSimon Schubert
17475796c8dcSSimon Schubert if (frozen && initial)
17485796c8dcSSimon Schubert {
17495796c8dcSSimon Schubert /* For variables that are frozen, or are children of frozen
17505796c8dcSSimon Schubert variables, we don't do fetch on initial assignment.
17515796c8dcSSimon Schubert For non-initial assignemnt we do the fetch, since it means we're
17525796c8dcSSimon Schubert explicitly asked to compare the new value with the old one. */
17535796c8dcSSimon Schubert intentionally_not_fetched = 1;
17545796c8dcSSimon Schubert }
1755*ef5ccd6cSJohn Marino else
1756*ef5ccd6cSJohn Marino {
1757*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
1758*ef5ccd6cSJohn Marino
1759*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
1760*ef5ccd6cSJohn Marino {
1761*ef5ccd6cSJohn Marino value_fetch_lazy (value);
1762*ef5ccd6cSJohn Marino }
1763*ef5ccd6cSJohn Marino
1764*ef5ccd6cSJohn Marino if (except.reason < 0)
17655796c8dcSSimon Schubert {
17665796c8dcSSimon Schubert /* Set the value to NULL, so that for the next -var-update,
17675796c8dcSSimon Schubert we don't try to compare the new value with this value,
17685796c8dcSSimon Schubert that we couldn't even read. */
17695796c8dcSSimon Schubert value = NULL;
17705796c8dcSSimon Schubert }
17715796c8dcSSimon Schubert }
1772*ef5ccd6cSJohn Marino }
17735796c8dcSSimon Schubert
1774*ef5ccd6cSJohn Marino /* Get a reference now, before possibly passing it to any Python
1775*ef5ccd6cSJohn Marino code that might release it. */
1776*ef5ccd6cSJohn Marino if (value != NULL)
1777*ef5ccd6cSJohn Marino value_incref (value);
17785796c8dcSSimon Schubert
17795796c8dcSSimon Schubert /* Below, we'll be comparing string rendering of old and new
17805796c8dcSSimon Schubert values. Don't get string rendering if the value is
17815796c8dcSSimon Schubert lazy -- if it is, the code above has decided that the value
17825796c8dcSSimon Schubert should not be fetched. */
17835796c8dcSSimon Schubert if (value && !value_lazy (value) && !var->pretty_printer)
17845796c8dcSSimon Schubert print_value = value_get_print_value (value, var->format, var);
17855796c8dcSSimon Schubert
17865796c8dcSSimon Schubert /* If the type is changeable, compare the old and the new values.
17875796c8dcSSimon Schubert If this is the initial assignment, we don't have any old value
17885796c8dcSSimon Schubert to compare with. */
17895796c8dcSSimon Schubert if (!initial && changeable)
17905796c8dcSSimon Schubert {
1791c50c785cSJohn Marino /* If the value of the varobj was changed by -var-set-value,
1792c50c785cSJohn Marino then the value in the varobj and in the target is the same.
1793c50c785cSJohn Marino However, that value is different from the value that the
1794c50c785cSJohn Marino varobj had after the previous -var-update. So need to the
1795c50c785cSJohn Marino varobj as changed. */
17965796c8dcSSimon Schubert if (var->updated)
17975796c8dcSSimon Schubert {
17985796c8dcSSimon Schubert changed = 1;
17995796c8dcSSimon Schubert }
18005796c8dcSSimon Schubert else if (! var->pretty_printer)
18015796c8dcSSimon Schubert {
18025796c8dcSSimon Schubert /* Try to compare the values. That requires that both
18035796c8dcSSimon Schubert values are non-lazy. */
18045796c8dcSSimon Schubert if (var->not_fetched && value_lazy (var->value))
18055796c8dcSSimon Schubert {
18065796c8dcSSimon Schubert /* This is a frozen varobj and the value was never read.
18075796c8dcSSimon Schubert Presumably, UI shows some "never read" indicator.
18085796c8dcSSimon Schubert Now that we've fetched the real value, we need to report
18095796c8dcSSimon Schubert this varobj as changed so that UI can show the real
18105796c8dcSSimon Schubert value. */
18115796c8dcSSimon Schubert changed = 1;
18125796c8dcSSimon Schubert }
18135796c8dcSSimon Schubert else if (var->value == NULL && value == NULL)
18145796c8dcSSimon Schubert /* Equal. */
18155796c8dcSSimon Schubert ;
18165796c8dcSSimon Schubert else if (var->value == NULL || value == NULL)
18175796c8dcSSimon Schubert {
18185796c8dcSSimon Schubert changed = 1;
18195796c8dcSSimon Schubert }
18205796c8dcSSimon Schubert else
18215796c8dcSSimon Schubert {
18225796c8dcSSimon Schubert gdb_assert (!value_lazy (var->value));
18235796c8dcSSimon Schubert gdb_assert (!value_lazy (value));
18245796c8dcSSimon Schubert
18255796c8dcSSimon Schubert gdb_assert (var->print_value != NULL && print_value != NULL);
18265796c8dcSSimon Schubert if (strcmp (var->print_value, print_value) != 0)
18275796c8dcSSimon Schubert changed = 1;
18285796c8dcSSimon Schubert }
18295796c8dcSSimon Schubert }
18305796c8dcSSimon Schubert }
18315796c8dcSSimon Schubert
18325796c8dcSSimon Schubert if (!initial && !changeable)
18335796c8dcSSimon Schubert {
18345796c8dcSSimon Schubert /* For values that are not changeable, we don't compare the values.
18355796c8dcSSimon Schubert However, we want to notice if a value was not NULL and now is NULL,
18365796c8dcSSimon Schubert or vise versa, so that we report when top-level varobjs come in scope
18375796c8dcSSimon Schubert and leave the scope. */
18385796c8dcSSimon Schubert changed = (var->value != NULL) != (value != NULL);
18395796c8dcSSimon Schubert }
18405796c8dcSSimon Schubert
18415796c8dcSSimon Schubert /* We must always keep the new value, since children depend on it. */
18425796c8dcSSimon Schubert if (var->value != NULL && var->value != value)
18435796c8dcSSimon Schubert value_free (var->value);
18445796c8dcSSimon Schubert var->value = value;
18455796c8dcSSimon Schubert if (value && value_lazy (value) && intentionally_not_fetched)
18465796c8dcSSimon Schubert var->not_fetched = 1;
18475796c8dcSSimon Schubert else
18485796c8dcSSimon Schubert var->not_fetched = 0;
18495796c8dcSSimon Schubert var->updated = 0;
18505796c8dcSSimon Schubert
18515796c8dcSSimon Schubert install_new_value_visualizer (var);
18525796c8dcSSimon Schubert
18535796c8dcSSimon Schubert /* If we installed a pretty-printer, re-compare the printed version
18545796c8dcSSimon Schubert to see if the variable changed. */
18555796c8dcSSimon Schubert if (var->pretty_printer)
18565796c8dcSSimon Schubert {
18575796c8dcSSimon Schubert xfree (print_value);
18585796c8dcSSimon Schubert print_value = value_get_print_value (var->value, var->format, var);
1859cf7f2e2dSJohn Marino if ((var->print_value == NULL && print_value != NULL)
1860cf7f2e2dSJohn Marino || (var->print_value != NULL && print_value == NULL)
1861cf7f2e2dSJohn Marino || (var->print_value != NULL && print_value != NULL
1862cf7f2e2dSJohn Marino && strcmp (var->print_value, print_value) != 0))
18635796c8dcSSimon Schubert changed = 1;
18645796c8dcSSimon Schubert }
18655796c8dcSSimon Schubert if (var->print_value)
18665796c8dcSSimon Schubert xfree (var->print_value);
18675796c8dcSSimon Schubert var->print_value = print_value;
18685796c8dcSSimon Schubert
18695796c8dcSSimon Schubert gdb_assert (!var->value || value_type (var->value));
18705796c8dcSSimon Schubert
18715796c8dcSSimon Schubert return changed;
18725796c8dcSSimon Schubert }
18735796c8dcSSimon Schubert
18745796c8dcSSimon Schubert /* Return the requested range for a varobj. VAR is the varobj. FROM
18755796c8dcSSimon Schubert and TO are out parameters; *FROM and *TO will be set to the
18765796c8dcSSimon Schubert selected sub-range of VAR. If no range was selected using
18775796c8dcSSimon Schubert -var-set-update-range, then both will be -1. */
18785796c8dcSSimon Schubert void
varobj_get_child_range(struct varobj * var,int * from,int * to)18795796c8dcSSimon Schubert varobj_get_child_range (struct varobj *var, int *from, int *to)
18805796c8dcSSimon Schubert {
18815796c8dcSSimon Schubert *from = var->from;
18825796c8dcSSimon Schubert *to = var->to;
18835796c8dcSSimon Schubert }
18845796c8dcSSimon Schubert
18855796c8dcSSimon Schubert /* Set the selected sub-range of children of VAR to start at index
18865796c8dcSSimon Schubert FROM and end at index TO. If either FROM or TO is less than zero,
18875796c8dcSSimon Schubert this is interpreted as a request for all children. */
18885796c8dcSSimon Schubert void
varobj_set_child_range(struct varobj * var,int from,int to)18895796c8dcSSimon Schubert varobj_set_child_range (struct varobj *var, int from, int to)
18905796c8dcSSimon Schubert {
18915796c8dcSSimon Schubert var->from = from;
18925796c8dcSSimon Schubert var->to = to;
18935796c8dcSSimon Schubert }
18945796c8dcSSimon Schubert
18955796c8dcSSimon Schubert void
varobj_set_visualizer(struct varobj * var,const char * visualizer)18965796c8dcSSimon Schubert varobj_set_visualizer (struct varobj *var, const char *visualizer)
18975796c8dcSSimon Schubert {
18985796c8dcSSimon Schubert #if HAVE_PYTHON
1899cf7f2e2dSJohn Marino PyObject *mainmod, *globals, *constructor;
1900cf7f2e2dSJohn Marino struct cleanup *back_to;
19015796c8dcSSimon Schubert
19025796c8dcSSimon Schubert back_to = varobj_ensure_python_env (var);
19035796c8dcSSimon Schubert
19045796c8dcSSimon Schubert mainmod = PyImport_AddModule ("__main__");
19055796c8dcSSimon Schubert globals = PyModule_GetDict (mainmod);
19065796c8dcSSimon Schubert Py_INCREF (globals);
19075796c8dcSSimon Schubert make_cleanup_py_decref (globals);
19085796c8dcSSimon Schubert
19095796c8dcSSimon Schubert constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
19105796c8dcSSimon Schubert
19115796c8dcSSimon Schubert if (! constructor)
19125796c8dcSSimon Schubert {
19135796c8dcSSimon Schubert gdbpy_print_stack ();
19145796c8dcSSimon Schubert error (_("Could not evaluate visualizer expression: %s"), visualizer);
19155796c8dcSSimon Schubert }
19165796c8dcSSimon Schubert
19175796c8dcSSimon Schubert construct_visualizer (var, constructor);
19185796c8dcSSimon Schubert Py_XDECREF (constructor);
19195796c8dcSSimon Schubert
19205796c8dcSSimon Schubert /* If there are any children now, wipe them. */
19215796c8dcSSimon Schubert varobj_delete (var, NULL, 1 /* children only */);
19225796c8dcSSimon Schubert var->num_children = -1;
19235796c8dcSSimon Schubert
19245796c8dcSSimon Schubert do_cleanups (back_to);
19255796c8dcSSimon Schubert #else
19265796c8dcSSimon Schubert error (_("Python support required"));
19275796c8dcSSimon Schubert #endif
19285796c8dcSSimon Schubert }
19295796c8dcSSimon Schubert
1930*ef5ccd6cSJohn Marino /* If NEW_VALUE is the new value of the given varobj (var), return
1931*ef5ccd6cSJohn Marino non-zero if var has mutated. In other words, if the type of
1932*ef5ccd6cSJohn Marino the new value is different from the type of the varobj's old
1933*ef5ccd6cSJohn Marino value.
1934*ef5ccd6cSJohn Marino
1935*ef5ccd6cSJohn Marino NEW_VALUE may be NULL, if the varobj is now out of scope. */
1936*ef5ccd6cSJohn Marino
1937*ef5ccd6cSJohn Marino static int
varobj_value_has_mutated(struct varobj * var,struct value * new_value,struct type * new_type)1938*ef5ccd6cSJohn Marino varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1939*ef5ccd6cSJohn Marino struct type *new_type)
1940*ef5ccd6cSJohn Marino {
1941*ef5ccd6cSJohn Marino /* If we haven't previously computed the number of children in var,
1942*ef5ccd6cSJohn Marino it does not matter from the front-end's perspective whether
1943*ef5ccd6cSJohn Marino the type has mutated or not. For all intents and purposes,
1944*ef5ccd6cSJohn Marino it has not mutated. */
1945*ef5ccd6cSJohn Marino if (var->num_children < 0)
1946*ef5ccd6cSJohn Marino return 0;
1947*ef5ccd6cSJohn Marino
1948*ef5ccd6cSJohn Marino if (var->root->lang->value_has_mutated)
1949*ef5ccd6cSJohn Marino return var->root->lang->value_has_mutated (var, new_value, new_type);
1950*ef5ccd6cSJohn Marino else
1951*ef5ccd6cSJohn Marino return 0;
1952*ef5ccd6cSJohn Marino }
1953*ef5ccd6cSJohn Marino
19545796c8dcSSimon Schubert /* Update the values for a variable and its children. This is a
19555796c8dcSSimon Schubert two-pronged attack. First, re-parse the value for the root's
19565796c8dcSSimon Schubert expression to see if it's changed. Then go all the way
19575796c8dcSSimon Schubert through its children, reconstructing them and noting if they've
19585796c8dcSSimon Schubert changed.
19595796c8dcSSimon Schubert
19605796c8dcSSimon Schubert The EXPLICIT parameter specifies if this call is result
19615796c8dcSSimon Schubert of MI request to update this specific variable, or
19625796c8dcSSimon Schubert result of implicit -var-update *. For implicit request, we don't
19635796c8dcSSimon Schubert update frozen variables.
19645796c8dcSSimon Schubert
19655796c8dcSSimon Schubert NOTE: This function may delete the caller's varobj. If it
19665796c8dcSSimon Schubert returns TYPE_CHANGED, then it has done this and VARP will be modified
19675796c8dcSSimon Schubert to point to the new varobj. */
19685796c8dcSSimon Schubert
VEC(varobj_update_result)1969*ef5ccd6cSJohn Marino VEC(varobj_update_result) *
1970*ef5ccd6cSJohn Marino varobj_update (struct varobj **varp, int explicit)
19715796c8dcSSimon Schubert {
19725796c8dcSSimon Schubert int type_changed = 0;
19735796c8dcSSimon Schubert int i;
19745796c8dcSSimon Schubert struct value *new;
19755796c8dcSSimon Schubert VEC (varobj_update_result) *stack = NULL;
19765796c8dcSSimon Schubert VEC (varobj_update_result) *result = NULL;
19775796c8dcSSimon Schubert
19785796c8dcSSimon Schubert /* Frozen means frozen -- we don't check for any change in
19795796c8dcSSimon Schubert this varobj, including its going out of scope, or
19805796c8dcSSimon Schubert changing type. One use case for frozen varobjs is
19815796c8dcSSimon Schubert retaining previously evaluated expressions, and we don't
19825796c8dcSSimon Schubert want them to be reevaluated at all. */
19835796c8dcSSimon Schubert if (!explicit && (*varp)->frozen)
19845796c8dcSSimon Schubert return result;
19855796c8dcSSimon Schubert
19865796c8dcSSimon Schubert if (!(*varp)->root->is_valid)
19875796c8dcSSimon Schubert {
1988cf7f2e2dSJohn Marino varobj_update_result r = {0};
1989cf7f2e2dSJohn Marino
1990cf7f2e2dSJohn Marino r.varobj = *varp;
19915796c8dcSSimon Schubert r.status = VAROBJ_INVALID;
19925796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, result, &r);
19935796c8dcSSimon Schubert return result;
19945796c8dcSSimon Schubert }
19955796c8dcSSimon Schubert
19965796c8dcSSimon Schubert if ((*varp)->root->rootvar == *varp)
19975796c8dcSSimon Schubert {
1998cf7f2e2dSJohn Marino varobj_update_result r = {0};
1999cf7f2e2dSJohn Marino
2000cf7f2e2dSJohn Marino r.varobj = *varp;
20015796c8dcSSimon Schubert r.status = VAROBJ_IN_SCOPE;
20025796c8dcSSimon Schubert
20035796c8dcSSimon Schubert /* Update the root variable. value_of_root can return NULL
20045796c8dcSSimon Schubert if the variable is no longer around, i.e. we stepped out of
20055796c8dcSSimon Schubert the frame in which a local existed. We are letting the
20065796c8dcSSimon Schubert value_of_root variable dispose of the varobj if the type
20075796c8dcSSimon Schubert has changed. */
20085796c8dcSSimon Schubert new = value_of_root (varp, &type_changed);
2009*ef5ccd6cSJohn Marino if (update_type_if_necessary(*varp, new))
2010*ef5ccd6cSJohn Marino type_changed = 1;
20115796c8dcSSimon Schubert r.varobj = *varp;
20125796c8dcSSimon Schubert r.type_changed = type_changed;
20135796c8dcSSimon Schubert if (install_new_value ((*varp), new, type_changed))
20145796c8dcSSimon Schubert r.changed = 1;
20155796c8dcSSimon Schubert
20165796c8dcSSimon Schubert if (new == NULL)
20175796c8dcSSimon Schubert r.status = VAROBJ_NOT_IN_SCOPE;
20185796c8dcSSimon Schubert r.value_installed = 1;
20195796c8dcSSimon Schubert
20205796c8dcSSimon Schubert if (r.status == VAROBJ_NOT_IN_SCOPE)
20215796c8dcSSimon Schubert {
20225796c8dcSSimon Schubert if (r.type_changed || r.changed)
20235796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, result, &r);
20245796c8dcSSimon Schubert return result;
20255796c8dcSSimon Schubert }
20265796c8dcSSimon Schubert
20275796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, stack, &r);
20285796c8dcSSimon Schubert }
20295796c8dcSSimon Schubert else
20305796c8dcSSimon Schubert {
2031cf7f2e2dSJohn Marino varobj_update_result r = {0};
2032cf7f2e2dSJohn Marino
2033cf7f2e2dSJohn Marino r.varobj = *varp;
20345796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, stack, &r);
20355796c8dcSSimon Schubert }
20365796c8dcSSimon Schubert
20375796c8dcSSimon Schubert /* Walk through the children, reconstructing them all. */
20385796c8dcSSimon Schubert while (!VEC_empty (varobj_update_result, stack))
20395796c8dcSSimon Schubert {
20405796c8dcSSimon Schubert varobj_update_result r = *(VEC_last (varobj_update_result, stack));
20415796c8dcSSimon Schubert struct varobj *v = r.varobj;
20425796c8dcSSimon Schubert
20435796c8dcSSimon Schubert VEC_pop (varobj_update_result, stack);
20445796c8dcSSimon Schubert
20455796c8dcSSimon Schubert /* Update this variable, unless it's a root, which is already
20465796c8dcSSimon Schubert updated. */
20475796c8dcSSimon Schubert if (!r.value_installed)
20485796c8dcSSimon Schubert {
2049*ef5ccd6cSJohn Marino struct type *new_type;
2050*ef5ccd6cSJohn Marino
20515796c8dcSSimon Schubert new = value_of_child (v->parent, v->index);
2052*ef5ccd6cSJohn Marino if (update_type_if_necessary(v, new))
2053*ef5ccd6cSJohn Marino r.type_changed = 1;
2054*ef5ccd6cSJohn Marino if (new)
2055*ef5ccd6cSJohn Marino new_type = value_type (new);
2056*ef5ccd6cSJohn Marino else
2057*ef5ccd6cSJohn Marino new_type = v->root->lang->type_of_child (v->parent, v->index);
2058*ef5ccd6cSJohn Marino
2059*ef5ccd6cSJohn Marino if (varobj_value_has_mutated (v, new, new_type))
2060*ef5ccd6cSJohn Marino {
2061*ef5ccd6cSJohn Marino /* The children are no longer valid; delete them now.
2062*ef5ccd6cSJohn Marino Report the fact that its type changed as well. */
2063*ef5ccd6cSJohn Marino varobj_delete (v, NULL, 1 /* only_children */);
2064*ef5ccd6cSJohn Marino v->num_children = -1;
2065*ef5ccd6cSJohn Marino v->to = -1;
2066*ef5ccd6cSJohn Marino v->from = -1;
2067*ef5ccd6cSJohn Marino v->type = new_type;
2068*ef5ccd6cSJohn Marino r.type_changed = 1;
2069*ef5ccd6cSJohn Marino }
2070*ef5ccd6cSJohn Marino
2071*ef5ccd6cSJohn Marino if (install_new_value (v, new, r.type_changed))
20725796c8dcSSimon Schubert {
20735796c8dcSSimon Schubert r.changed = 1;
20745796c8dcSSimon Schubert v->updated = 0;
20755796c8dcSSimon Schubert }
20765796c8dcSSimon Schubert }
20775796c8dcSSimon Schubert
20785796c8dcSSimon Schubert /* We probably should not get children of a varobj that has a
20795796c8dcSSimon Schubert pretty-printer, but for which -var-list-children was never
20805796c8dcSSimon Schubert invoked. */
20815796c8dcSSimon Schubert if (v->pretty_printer)
20825796c8dcSSimon Schubert {
2083*ef5ccd6cSJohn Marino VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0;
2084*ef5ccd6cSJohn Marino VEC (varobj_p) *new = 0;
20855796c8dcSSimon Schubert int i, children_changed = 0;
20865796c8dcSSimon Schubert
20875796c8dcSSimon Schubert if (v->frozen)
20885796c8dcSSimon Schubert continue;
20895796c8dcSSimon Schubert
20905796c8dcSSimon Schubert if (!v->children_requested)
20915796c8dcSSimon Schubert {
20925796c8dcSSimon Schubert int dummy;
20935796c8dcSSimon Schubert
20945796c8dcSSimon Schubert /* If we initially did not have potential children, but
20955796c8dcSSimon Schubert now we do, consider the varobj as changed.
20965796c8dcSSimon Schubert Otherwise, if children were never requested, consider
20975796c8dcSSimon Schubert it as unchanged -- presumably, such varobj is not yet
20985796c8dcSSimon Schubert expanded in the UI, so we need not bother getting
20995796c8dcSSimon Schubert it. */
21005796c8dcSSimon Schubert if (!varobj_has_more (v, 0))
21015796c8dcSSimon Schubert {
2102*ef5ccd6cSJohn Marino update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
21035796c8dcSSimon Schubert &dummy, 0, 0, 0);
21045796c8dcSSimon Schubert if (varobj_has_more (v, 0))
21055796c8dcSSimon Schubert r.changed = 1;
21065796c8dcSSimon Schubert }
21075796c8dcSSimon Schubert
21085796c8dcSSimon Schubert if (r.changed)
21095796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, result, &r);
21105796c8dcSSimon Schubert
21115796c8dcSSimon Schubert continue;
21125796c8dcSSimon Schubert }
21135796c8dcSSimon Schubert
21145796c8dcSSimon Schubert /* If update_dynamic_varobj_children returns 0, then we have
21155796c8dcSSimon Schubert a non-conforming pretty-printer, so we skip it. */
2116*ef5ccd6cSJohn Marino if (update_dynamic_varobj_children (v, &changed, &type_changed, &new,
2117*ef5ccd6cSJohn Marino &unchanged, &children_changed, 1,
21185796c8dcSSimon Schubert v->from, v->to))
21195796c8dcSSimon Schubert {
21205796c8dcSSimon Schubert if (children_changed || new)
21215796c8dcSSimon Schubert {
21225796c8dcSSimon Schubert r.children_changed = 1;
21235796c8dcSSimon Schubert r.new = new;
21245796c8dcSSimon Schubert }
21255796c8dcSSimon Schubert /* Push in reverse order so that the first child is
21265796c8dcSSimon Schubert popped from the work stack first, and so will be
21275796c8dcSSimon Schubert added to result first. This does not affect
21285796c8dcSSimon Schubert correctness, just "nicer". */
2129*ef5ccd6cSJohn Marino for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i)
2130*ef5ccd6cSJohn Marino {
2131*ef5ccd6cSJohn Marino varobj_p tmp = VEC_index (varobj_p, type_changed, i);
2132*ef5ccd6cSJohn Marino varobj_update_result r = {0};
2133*ef5ccd6cSJohn Marino
2134*ef5ccd6cSJohn Marino /* Type may change only if value was changed. */
2135*ef5ccd6cSJohn Marino r.varobj = tmp;
2136*ef5ccd6cSJohn Marino r.changed = 1;
2137*ef5ccd6cSJohn Marino r.type_changed = 1;
2138*ef5ccd6cSJohn Marino r.value_installed = 1;
2139*ef5ccd6cSJohn Marino VEC_safe_push (varobj_update_result, stack, &r);
2140*ef5ccd6cSJohn Marino }
21415796c8dcSSimon Schubert for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
21425796c8dcSSimon Schubert {
21435796c8dcSSimon Schubert varobj_p tmp = VEC_index (varobj_p, changed, i);
2144cf7f2e2dSJohn Marino varobj_update_result r = {0};
2145cf7f2e2dSJohn Marino
2146cf7f2e2dSJohn Marino r.varobj = tmp;
21475796c8dcSSimon Schubert r.changed = 1;
21485796c8dcSSimon Schubert r.value_installed = 1;
21495796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, stack, &r);
21505796c8dcSSimon Schubert }
21515796c8dcSSimon Schubert for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
21525796c8dcSSimon Schubert {
21535796c8dcSSimon Schubert varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2154cf7f2e2dSJohn Marino
21555796c8dcSSimon Schubert if (!tmp->frozen)
21565796c8dcSSimon Schubert {
2157cf7f2e2dSJohn Marino varobj_update_result r = {0};
2158cf7f2e2dSJohn Marino
2159cf7f2e2dSJohn Marino r.varobj = tmp;
21605796c8dcSSimon Schubert r.value_installed = 1;
21615796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, stack, &r);
21625796c8dcSSimon Schubert }
21635796c8dcSSimon Schubert }
21645796c8dcSSimon Schubert if (r.changed || r.children_changed)
21655796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, result, &r);
21665796c8dcSSimon Schubert
2167*ef5ccd6cSJohn Marino /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
2168*ef5ccd6cSJohn Marino because NEW has been put into the result vector. */
21695796c8dcSSimon Schubert VEC_free (varobj_p, changed);
2170*ef5ccd6cSJohn Marino VEC_free (varobj_p, type_changed);
21715796c8dcSSimon Schubert VEC_free (varobj_p, unchanged);
21725796c8dcSSimon Schubert
21735796c8dcSSimon Schubert continue;
21745796c8dcSSimon Schubert }
21755796c8dcSSimon Schubert }
21765796c8dcSSimon Schubert
21775796c8dcSSimon Schubert /* Push any children. Use reverse order so that the first
21785796c8dcSSimon Schubert child is popped from the work stack first, and so
21795796c8dcSSimon Schubert will be added to result first. This does not
21805796c8dcSSimon Schubert affect correctness, just "nicer". */
21815796c8dcSSimon Schubert for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
21825796c8dcSSimon Schubert {
21835796c8dcSSimon Schubert varobj_p c = VEC_index (varobj_p, v->children, i);
2184cf7f2e2dSJohn Marino
21855796c8dcSSimon Schubert /* Child may be NULL if explicitly deleted by -var-delete. */
21865796c8dcSSimon Schubert if (c != NULL && !c->frozen)
21875796c8dcSSimon Schubert {
2188cf7f2e2dSJohn Marino varobj_update_result r = {0};
2189cf7f2e2dSJohn Marino
2190cf7f2e2dSJohn Marino r.varobj = c;
21915796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, stack, &r);
21925796c8dcSSimon Schubert }
21935796c8dcSSimon Schubert }
21945796c8dcSSimon Schubert
21955796c8dcSSimon Schubert if (r.changed || r.type_changed)
21965796c8dcSSimon Schubert VEC_safe_push (varobj_update_result, result, &r);
21975796c8dcSSimon Schubert }
21985796c8dcSSimon Schubert
21995796c8dcSSimon Schubert VEC_free (varobj_update_result, stack);
22005796c8dcSSimon Schubert
22015796c8dcSSimon Schubert return result;
22025796c8dcSSimon Schubert }
22035796c8dcSSimon Schubert
22045796c8dcSSimon Schubert
22055796c8dcSSimon Schubert /* Helper functions */
22065796c8dcSSimon Schubert
22075796c8dcSSimon Schubert /*
22085796c8dcSSimon Schubert * Variable object construction/destruction
22095796c8dcSSimon Schubert */
22105796c8dcSSimon Schubert
22115796c8dcSSimon Schubert static int
delete_variable(struct cpstack ** resultp,struct varobj * var,int only_children_p)22125796c8dcSSimon Schubert delete_variable (struct cpstack **resultp, struct varobj *var,
22135796c8dcSSimon Schubert int only_children_p)
22145796c8dcSSimon Schubert {
22155796c8dcSSimon Schubert int delcount = 0;
22165796c8dcSSimon Schubert
22175796c8dcSSimon Schubert delete_variable_1 (resultp, &delcount, var,
22185796c8dcSSimon Schubert only_children_p, 1 /* remove_from_parent_p */ );
22195796c8dcSSimon Schubert
22205796c8dcSSimon Schubert return delcount;
22215796c8dcSSimon Schubert }
22225796c8dcSSimon Schubert
2223c50c785cSJohn Marino /* Delete the variable object VAR and its children. */
22245796c8dcSSimon Schubert /* IMPORTANT NOTE: If we delete a variable which is a child
22255796c8dcSSimon Schubert and the parent is not removed we dump core. It must be always
2226c50c785cSJohn Marino initially called with remove_from_parent_p set. */
22275796c8dcSSimon Schubert static void
delete_variable_1(struct cpstack ** resultp,int * delcountp,struct varobj * var,int only_children_p,int remove_from_parent_p)22285796c8dcSSimon Schubert delete_variable_1 (struct cpstack **resultp, int *delcountp,
22295796c8dcSSimon Schubert struct varobj *var, int only_children_p,
22305796c8dcSSimon Schubert int remove_from_parent_p)
22315796c8dcSSimon Schubert {
22325796c8dcSSimon Schubert int i;
22335796c8dcSSimon Schubert
22345796c8dcSSimon Schubert /* Delete any children of this variable, too. */
22355796c8dcSSimon Schubert for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
22365796c8dcSSimon Schubert {
22375796c8dcSSimon Schubert varobj_p child = VEC_index (varobj_p, var->children, i);
2238cf7f2e2dSJohn Marino
22395796c8dcSSimon Schubert if (!child)
22405796c8dcSSimon Schubert continue;
22415796c8dcSSimon Schubert if (!remove_from_parent_p)
22425796c8dcSSimon Schubert child->parent = NULL;
22435796c8dcSSimon Schubert delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
22445796c8dcSSimon Schubert }
22455796c8dcSSimon Schubert VEC_free (varobj_p, var->children);
22465796c8dcSSimon Schubert
2247c50c785cSJohn Marino /* if we were called to delete only the children we are done here. */
22485796c8dcSSimon Schubert if (only_children_p)
22495796c8dcSSimon Schubert return;
22505796c8dcSSimon Schubert
2251c50c785cSJohn Marino /* Otherwise, add it to the list of deleted ones and proceed to do so. */
22525796c8dcSSimon Schubert /* If the name is null, this is a temporary variable, that has not
22535796c8dcSSimon Schubert yet been installed, don't report it, it belongs to the caller... */
22545796c8dcSSimon Schubert if (var->obj_name != NULL)
22555796c8dcSSimon Schubert {
22565796c8dcSSimon Schubert cppush (resultp, xstrdup (var->obj_name));
22575796c8dcSSimon Schubert *delcountp = *delcountp + 1;
22585796c8dcSSimon Schubert }
22595796c8dcSSimon Schubert
2260c50c785cSJohn Marino /* If this variable has a parent, remove it from its parent's list. */
22615796c8dcSSimon Schubert /* OPTIMIZATION: if the parent of this variable is also being deleted,
22625796c8dcSSimon Schubert (as indicated by remove_from_parent_p) we don't bother doing an
22635796c8dcSSimon Schubert expensive list search to find the element to remove when we are
2264c50c785cSJohn Marino discarding the list afterwards. */
22655796c8dcSSimon Schubert if ((remove_from_parent_p) && (var->parent != NULL))
22665796c8dcSSimon Schubert {
22675796c8dcSSimon Schubert VEC_replace (varobj_p, var->parent->children, var->index, NULL);
22685796c8dcSSimon Schubert }
22695796c8dcSSimon Schubert
22705796c8dcSSimon Schubert if (var->obj_name != NULL)
22715796c8dcSSimon Schubert uninstall_variable (var);
22725796c8dcSSimon Schubert
2273c50c785cSJohn Marino /* Free memory associated with this variable. */
22745796c8dcSSimon Schubert free_variable (var);
22755796c8dcSSimon Schubert }
22765796c8dcSSimon Schubert
22775796c8dcSSimon Schubert /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
22785796c8dcSSimon Schubert static int
install_variable(struct varobj * var)22795796c8dcSSimon Schubert install_variable (struct varobj *var)
22805796c8dcSSimon Schubert {
22815796c8dcSSimon Schubert struct vlist *cv;
22825796c8dcSSimon Schubert struct vlist *newvl;
22835796c8dcSSimon Schubert const char *chp;
22845796c8dcSSimon Schubert unsigned int index = 0;
22855796c8dcSSimon Schubert unsigned int i = 1;
22865796c8dcSSimon Schubert
22875796c8dcSSimon Schubert for (chp = var->obj_name; *chp; chp++)
22885796c8dcSSimon Schubert {
22895796c8dcSSimon Schubert index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
22905796c8dcSSimon Schubert }
22915796c8dcSSimon Schubert
22925796c8dcSSimon Schubert cv = *(varobj_table + index);
22935796c8dcSSimon Schubert while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
22945796c8dcSSimon Schubert cv = cv->next;
22955796c8dcSSimon Schubert
22965796c8dcSSimon Schubert if (cv != NULL)
22975796c8dcSSimon Schubert error (_("Duplicate variable object name"));
22985796c8dcSSimon Schubert
2299c50c785cSJohn Marino /* Add varobj to hash table. */
23005796c8dcSSimon Schubert newvl = xmalloc (sizeof (struct vlist));
23015796c8dcSSimon Schubert newvl->next = *(varobj_table + index);
23025796c8dcSSimon Schubert newvl->var = var;
23035796c8dcSSimon Schubert *(varobj_table + index) = newvl;
23045796c8dcSSimon Schubert
2305c50c785cSJohn Marino /* If root, add varobj to root list. */
23065796c8dcSSimon Schubert if (is_root_p (var))
23075796c8dcSSimon Schubert {
2308c50c785cSJohn Marino /* Add to list of root variables. */
23095796c8dcSSimon Schubert if (rootlist == NULL)
23105796c8dcSSimon Schubert var->root->next = NULL;
23115796c8dcSSimon Schubert else
23125796c8dcSSimon Schubert var->root->next = rootlist;
23135796c8dcSSimon Schubert rootlist = var->root;
23145796c8dcSSimon Schubert }
23155796c8dcSSimon Schubert
23165796c8dcSSimon Schubert return 1; /* OK */
23175796c8dcSSimon Schubert }
23185796c8dcSSimon Schubert
23195796c8dcSSimon Schubert /* Unistall the object VAR. */
23205796c8dcSSimon Schubert static void
uninstall_variable(struct varobj * var)23215796c8dcSSimon Schubert uninstall_variable (struct varobj *var)
23225796c8dcSSimon Schubert {
23235796c8dcSSimon Schubert struct vlist *cv;
23245796c8dcSSimon Schubert struct vlist *prev;
23255796c8dcSSimon Schubert struct varobj_root *cr;
23265796c8dcSSimon Schubert struct varobj_root *prer;
23275796c8dcSSimon Schubert const char *chp;
23285796c8dcSSimon Schubert unsigned int index = 0;
23295796c8dcSSimon Schubert unsigned int i = 1;
23305796c8dcSSimon Schubert
2331c50c785cSJohn Marino /* Remove varobj from hash table. */
23325796c8dcSSimon Schubert for (chp = var->obj_name; *chp; chp++)
23335796c8dcSSimon Schubert {
23345796c8dcSSimon Schubert index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
23355796c8dcSSimon Schubert }
23365796c8dcSSimon Schubert
23375796c8dcSSimon Schubert cv = *(varobj_table + index);
23385796c8dcSSimon Schubert prev = NULL;
23395796c8dcSSimon Schubert while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
23405796c8dcSSimon Schubert {
23415796c8dcSSimon Schubert prev = cv;
23425796c8dcSSimon Schubert cv = cv->next;
23435796c8dcSSimon Schubert }
23445796c8dcSSimon Schubert
23455796c8dcSSimon Schubert if (varobjdebug)
23465796c8dcSSimon Schubert fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
23475796c8dcSSimon Schubert
23485796c8dcSSimon Schubert if (cv == NULL)
23495796c8dcSSimon Schubert {
23505796c8dcSSimon Schubert warning
23515796c8dcSSimon Schubert ("Assertion failed: Could not find variable object \"%s\" to delete",
23525796c8dcSSimon Schubert var->obj_name);
23535796c8dcSSimon Schubert return;
23545796c8dcSSimon Schubert }
23555796c8dcSSimon Schubert
23565796c8dcSSimon Schubert if (prev == NULL)
23575796c8dcSSimon Schubert *(varobj_table + index) = cv->next;
23585796c8dcSSimon Schubert else
23595796c8dcSSimon Schubert prev->next = cv->next;
23605796c8dcSSimon Schubert
23615796c8dcSSimon Schubert xfree (cv);
23625796c8dcSSimon Schubert
2363c50c785cSJohn Marino /* If root, remove varobj from root list. */
23645796c8dcSSimon Schubert if (is_root_p (var))
23655796c8dcSSimon Schubert {
2366c50c785cSJohn Marino /* Remove from list of root variables. */
23675796c8dcSSimon Schubert if (rootlist == var->root)
23685796c8dcSSimon Schubert rootlist = var->root->next;
23695796c8dcSSimon Schubert else
23705796c8dcSSimon Schubert {
23715796c8dcSSimon Schubert prer = NULL;
23725796c8dcSSimon Schubert cr = rootlist;
23735796c8dcSSimon Schubert while ((cr != NULL) && (cr->rootvar != var))
23745796c8dcSSimon Schubert {
23755796c8dcSSimon Schubert prer = cr;
23765796c8dcSSimon Schubert cr = cr->next;
23775796c8dcSSimon Schubert }
23785796c8dcSSimon Schubert if (cr == NULL)
23795796c8dcSSimon Schubert {
2380c50c785cSJohn Marino warning (_("Assertion failed: Could not find "
2381c50c785cSJohn Marino "varobj \"%s\" in root list"),
23825796c8dcSSimon Schubert var->obj_name);
23835796c8dcSSimon Schubert return;
23845796c8dcSSimon Schubert }
23855796c8dcSSimon Schubert if (prer == NULL)
23865796c8dcSSimon Schubert rootlist = NULL;
23875796c8dcSSimon Schubert else
23885796c8dcSSimon Schubert prer->next = cr->next;
23895796c8dcSSimon Schubert }
23905796c8dcSSimon Schubert }
23915796c8dcSSimon Schubert
23925796c8dcSSimon Schubert }
23935796c8dcSSimon Schubert
2394c50c785cSJohn Marino /* Create and install a child of the parent of the given name. */
23955796c8dcSSimon Schubert static struct varobj *
create_child(struct varobj * parent,int index,char * name)23965796c8dcSSimon Schubert create_child (struct varobj *parent, int index, char *name)
23975796c8dcSSimon Schubert {
23985796c8dcSSimon Schubert return create_child_with_value (parent, index, name,
23995796c8dcSSimon Schubert value_of_child (parent, index));
24005796c8dcSSimon Schubert }
24015796c8dcSSimon Schubert
2402*ef5ccd6cSJohn Marino /* Does CHILD represent a child with no name? This happens when
2403*ef5ccd6cSJohn Marino the child is an anonmous struct or union and it has no field name
2404*ef5ccd6cSJohn Marino in its parent variable.
2405*ef5ccd6cSJohn Marino
2406*ef5ccd6cSJohn Marino This has already been determined by *_describe_child. The easiest
2407*ef5ccd6cSJohn Marino thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2408*ef5ccd6cSJohn Marino
2409*ef5ccd6cSJohn Marino static int
is_anonymous_child(struct varobj * child)2410*ef5ccd6cSJohn Marino is_anonymous_child (struct varobj *child)
2411*ef5ccd6cSJohn Marino {
2412*ef5ccd6cSJohn Marino return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2413*ef5ccd6cSJohn Marino || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2414*ef5ccd6cSJohn Marino }
2415*ef5ccd6cSJohn Marino
24165796c8dcSSimon Schubert static struct varobj *
create_child_with_value(struct varobj * parent,int index,const char * name,struct value * value)24175796c8dcSSimon Schubert create_child_with_value (struct varobj *parent, int index, const char *name,
24185796c8dcSSimon Schubert struct value *value)
24195796c8dcSSimon Schubert {
24205796c8dcSSimon Schubert struct varobj *child;
24215796c8dcSSimon Schubert char *childs_name;
24225796c8dcSSimon Schubert
24235796c8dcSSimon Schubert child = new_variable ();
24245796c8dcSSimon Schubert
2425c50c785cSJohn Marino /* Name is allocated by name_of_child. */
24265796c8dcSSimon Schubert /* FIXME: xstrdup should not be here. */
24275796c8dcSSimon Schubert child->name = xstrdup (name);
24285796c8dcSSimon Schubert child->index = index;
24295796c8dcSSimon Schubert child->parent = parent;
24305796c8dcSSimon Schubert child->root = parent->root;
2431*ef5ccd6cSJohn Marino
2432*ef5ccd6cSJohn Marino if (is_anonymous_child (child))
2433*ef5ccd6cSJohn Marino childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2434*ef5ccd6cSJohn Marino else
24355796c8dcSSimon Schubert childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
24365796c8dcSSimon Schubert child->obj_name = childs_name;
2437*ef5ccd6cSJohn Marino
24385796c8dcSSimon Schubert install_variable (child);
24395796c8dcSSimon Schubert
24405796c8dcSSimon Schubert /* Compute the type of the child. Must do this before
24415796c8dcSSimon Schubert calling install_new_value. */
24425796c8dcSSimon Schubert if (value != NULL)
24435796c8dcSSimon Schubert /* If the child had no evaluation errors, var->value
24445796c8dcSSimon Schubert will be non-NULL and contain a valid type. */
2445*ef5ccd6cSJohn Marino child->type = value_actual_type (value, 0, NULL);
24465796c8dcSSimon Schubert else
24475796c8dcSSimon Schubert /* Otherwise, we must compute the type. */
24485796c8dcSSimon Schubert child->type = (*child->root->lang->type_of_child) (child->parent,
24495796c8dcSSimon Schubert child->index);
24505796c8dcSSimon Schubert install_new_value (child, value, 1);
24515796c8dcSSimon Schubert
24525796c8dcSSimon Schubert return child;
24535796c8dcSSimon Schubert }
24545796c8dcSSimon Schubert
24555796c8dcSSimon Schubert
24565796c8dcSSimon Schubert /*
24575796c8dcSSimon Schubert * Miscellaneous utility functions.
24585796c8dcSSimon Schubert */
24595796c8dcSSimon Schubert
2460c50c785cSJohn Marino /* Allocate memory and initialize a new variable. */
24615796c8dcSSimon Schubert static struct varobj *
new_variable(void)24625796c8dcSSimon Schubert new_variable (void)
24635796c8dcSSimon Schubert {
24645796c8dcSSimon Schubert struct varobj *var;
24655796c8dcSSimon Schubert
24665796c8dcSSimon Schubert var = (struct varobj *) xmalloc (sizeof (struct varobj));
24675796c8dcSSimon Schubert var->name = NULL;
24685796c8dcSSimon Schubert var->path_expr = NULL;
24695796c8dcSSimon Schubert var->obj_name = NULL;
24705796c8dcSSimon Schubert var->index = -1;
24715796c8dcSSimon Schubert var->type = NULL;
24725796c8dcSSimon Schubert var->value = NULL;
24735796c8dcSSimon Schubert var->num_children = -1;
24745796c8dcSSimon Schubert var->parent = NULL;
24755796c8dcSSimon Schubert var->children = NULL;
24765796c8dcSSimon Schubert var->format = 0;
24775796c8dcSSimon Schubert var->root = NULL;
24785796c8dcSSimon Schubert var->updated = 0;
24795796c8dcSSimon Schubert var->print_value = NULL;
24805796c8dcSSimon Schubert var->frozen = 0;
24815796c8dcSSimon Schubert var->not_fetched = 0;
24825796c8dcSSimon Schubert var->children_requested = 0;
24835796c8dcSSimon Schubert var->from = -1;
24845796c8dcSSimon Schubert var->to = -1;
24855796c8dcSSimon Schubert var->constructor = 0;
24865796c8dcSSimon Schubert var->pretty_printer = 0;
24875796c8dcSSimon Schubert var->child_iter = 0;
24885796c8dcSSimon Schubert var->saved_item = 0;
24895796c8dcSSimon Schubert
24905796c8dcSSimon Schubert return var;
24915796c8dcSSimon Schubert }
24925796c8dcSSimon Schubert
2493c50c785cSJohn Marino /* Allocate memory and initialize a new root variable. */
24945796c8dcSSimon Schubert static struct varobj *
new_root_variable(void)24955796c8dcSSimon Schubert new_root_variable (void)
24965796c8dcSSimon Schubert {
24975796c8dcSSimon Schubert struct varobj *var = new_variable ();
2498cf7f2e2dSJohn Marino
2499c50c785cSJohn Marino var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
25005796c8dcSSimon Schubert var->root->lang = NULL;
25015796c8dcSSimon Schubert var->root->exp = NULL;
25025796c8dcSSimon Schubert var->root->valid_block = NULL;
25035796c8dcSSimon Schubert var->root->frame = null_frame_id;
25045796c8dcSSimon Schubert var->root->floating = 0;
25055796c8dcSSimon Schubert var->root->rootvar = NULL;
25065796c8dcSSimon Schubert var->root->is_valid = 1;
25075796c8dcSSimon Schubert
25085796c8dcSSimon Schubert return var;
25095796c8dcSSimon Schubert }
25105796c8dcSSimon Schubert
25115796c8dcSSimon Schubert /* Free any allocated memory associated with VAR. */
25125796c8dcSSimon Schubert static void
free_variable(struct varobj * var)25135796c8dcSSimon Schubert free_variable (struct varobj *var)
25145796c8dcSSimon Schubert {
25155796c8dcSSimon Schubert #if HAVE_PYTHON
25165796c8dcSSimon Schubert if (var->pretty_printer)
25175796c8dcSSimon Schubert {
25185796c8dcSSimon Schubert struct cleanup *cleanup = varobj_ensure_python_env (var);
25195796c8dcSSimon Schubert Py_XDECREF (var->constructor);
25205796c8dcSSimon Schubert Py_XDECREF (var->pretty_printer);
25215796c8dcSSimon Schubert Py_XDECREF (var->child_iter);
25225796c8dcSSimon Schubert Py_XDECREF (var->saved_item);
25235796c8dcSSimon Schubert do_cleanups (cleanup);
25245796c8dcSSimon Schubert }
25255796c8dcSSimon Schubert #endif
25265796c8dcSSimon Schubert
25275796c8dcSSimon Schubert value_free (var->value);
25285796c8dcSSimon Schubert
25295796c8dcSSimon Schubert /* Free the expression if this is a root variable. */
25305796c8dcSSimon Schubert if (is_root_p (var))
25315796c8dcSSimon Schubert {
25325796c8dcSSimon Schubert xfree (var->root->exp);
25335796c8dcSSimon Schubert xfree (var->root);
25345796c8dcSSimon Schubert }
25355796c8dcSSimon Schubert
25365796c8dcSSimon Schubert xfree (var->name);
25375796c8dcSSimon Schubert xfree (var->obj_name);
25385796c8dcSSimon Schubert xfree (var->print_value);
25395796c8dcSSimon Schubert xfree (var->path_expr);
25405796c8dcSSimon Schubert xfree (var);
25415796c8dcSSimon Schubert }
25425796c8dcSSimon Schubert
25435796c8dcSSimon Schubert static void
do_free_variable_cleanup(void * var)25445796c8dcSSimon Schubert do_free_variable_cleanup (void *var)
25455796c8dcSSimon Schubert {
25465796c8dcSSimon Schubert free_variable (var);
25475796c8dcSSimon Schubert }
25485796c8dcSSimon Schubert
25495796c8dcSSimon Schubert static struct cleanup *
make_cleanup_free_variable(struct varobj * var)25505796c8dcSSimon Schubert make_cleanup_free_variable (struct varobj *var)
25515796c8dcSSimon Schubert {
25525796c8dcSSimon Schubert return make_cleanup (do_free_variable_cleanup, var);
25535796c8dcSSimon Schubert }
25545796c8dcSSimon Schubert
25555796c8dcSSimon Schubert /* This returns the type of the variable. It also skips past typedefs
25565796c8dcSSimon Schubert to return the real type of the variable.
25575796c8dcSSimon Schubert
25585796c8dcSSimon Schubert NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
25595796c8dcSSimon Schubert except within get_target_type and get_type. */
25605796c8dcSSimon Schubert static struct type *
get_type(struct varobj * var)25615796c8dcSSimon Schubert get_type (struct varobj *var)
25625796c8dcSSimon Schubert {
25635796c8dcSSimon Schubert struct type *type;
25645796c8dcSSimon Schubert
2565cf7f2e2dSJohn Marino type = var->type;
25665796c8dcSSimon Schubert if (type != NULL)
25675796c8dcSSimon Schubert type = check_typedef (type);
25685796c8dcSSimon Schubert
25695796c8dcSSimon Schubert return type;
25705796c8dcSSimon Schubert }
25715796c8dcSSimon Schubert
25725796c8dcSSimon Schubert /* Return the type of the value that's stored in VAR,
25735796c8dcSSimon Schubert or that would have being stored there if the
25745796c8dcSSimon Schubert value were accessible.
25755796c8dcSSimon Schubert
25765796c8dcSSimon Schubert This differs from VAR->type in that VAR->type is always
25775796c8dcSSimon Schubert the true type of the expession in the source language.
25785796c8dcSSimon Schubert The return value of this function is the type we're
25795796c8dcSSimon Schubert actually storing in varobj, and using for displaying
25805796c8dcSSimon Schubert the values and for comparing previous and new values.
25815796c8dcSSimon Schubert
25825796c8dcSSimon Schubert For example, top-level references are always stripped. */
25835796c8dcSSimon Schubert static struct type *
get_value_type(struct varobj * var)25845796c8dcSSimon Schubert get_value_type (struct varobj *var)
25855796c8dcSSimon Schubert {
25865796c8dcSSimon Schubert struct type *type;
25875796c8dcSSimon Schubert
25885796c8dcSSimon Schubert if (var->value)
25895796c8dcSSimon Schubert type = value_type (var->value);
25905796c8dcSSimon Schubert else
25915796c8dcSSimon Schubert type = var->type;
25925796c8dcSSimon Schubert
25935796c8dcSSimon Schubert type = check_typedef (type);
25945796c8dcSSimon Schubert
25955796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_REF)
25965796c8dcSSimon Schubert type = get_target_type (type);
25975796c8dcSSimon Schubert
25985796c8dcSSimon Schubert type = check_typedef (type);
25995796c8dcSSimon Schubert
26005796c8dcSSimon Schubert return type;
26015796c8dcSSimon Schubert }
26025796c8dcSSimon Schubert
26035796c8dcSSimon Schubert /* This returns the target type (or NULL) of TYPE, also skipping
26045796c8dcSSimon Schubert past typedefs, just like get_type ().
26055796c8dcSSimon Schubert
26065796c8dcSSimon Schubert NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
26075796c8dcSSimon Schubert except within get_target_type and get_type. */
26085796c8dcSSimon Schubert static struct type *
get_target_type(struct type * type)26095796c8dcSSimon Schubert get_target_type (struct type *type)
26105796c8dcSSimon Schubert {
26115796c8dcSSimon Schubert if (type != NULL)
26125796c8dcSSimon Schubert {
26135796c8dcSSimon Schubert type = TYPE_TARGET_TYPE (type);
26145796c8dcSSimon Schubert if (type != NULL)
26155796c8dcSSimon Schubert type = check_typedef (type);
26165796c8dcSSimon Schubert }
26175796c8dcSSimon Schubert
26185796c8dcSSimon Schubert return type;
26195796c8dcSSimon Schubert }
26205796c8dcSSimon Schubert
26215796c8dcSSimon Schubert /* What is the default display for this variable? We assume that
26225796c8dcSSimon Schubert everything is "natural". Any exceptions? */
26235796c8dcSSimon Schubert static enum varobj_display_formats
variable_default_display(struct varobj * var)26245796c8dcSSimon Schubert variable_default_display (struct varobj *var)
26255796c8dcSSimon Schubert {
26265796c8dcSSimon Schubert return FORMAT_NATURAL;
26275796c8dcSSimon Schubert }
26285796c8dcSSimon Schubert
2629c50c785cSJohn Marino /* FIXME: The following should be generic for any pointer. */
26305796c8dcSSimon Schubert static void
cppush(struct cpstack ** pstack,char * name)26315796c8dcSSimon Schubert cppush (struct cpstack **pstack, char *name)
26325796c8dcSSimon Schubert {
26335796c8dcSSimon Schubert struct cpstack *s;
26345796c8dcSSimon Schubert
26355796c8dcSSimon Schubert s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
26365796c8dcSSimon Schubert s->name = name;
26375796c8dcSSimon Schubert s->next = *pstack;
26385796c8dcSSimon Schubert *pstack = s;
26395796c8dcSSimon Schubert }
26405796c8dcSSimon Schubert
2641c50c785cSJohn Marino /* FIXME: The following should be generic for any pointer. */
26425796c8dcSSimon Schubert static char *
cppop(struct cpstack ** pstack)26435796c8dcSSimon Schubert cppop (struct cpstack **pstack)
26445796c8dcSSimon Schubert {
26455796c8dcSSimon Schubert struct cpstack *s;
26465796c8dcSSimon Schubert char *v;
26475796c8dcSSimon Schubert
26485796c8dcSSimon Schubert if ((*pstack)->name == NULL && (*pstack)->next == NULL)
26495796c8dcSSimon Schubert return NULL;
26505796c8dcSSimon Schubert
26515796c8dcSSimon Schubert s = *pstack;
26525796c8dcSSimon Schubert v = s->name;
26535796c8dcSSimon Schubert *pstack = (*pstack)->next;
26545796c8dcSSimon Schubert xfree (s);
26555796c8dcSSimon Schubert
26565796c8dcSSimon Schubert return v;
26575796c8dcSSimon Schubert }
26585796c8dcSSimon Schubert
26595796c8dcSSimon Schubert /*
26605796c8dcSSimon Schubert * Language-dependencies
26615796c8dcSSimon Schubert */
26625796c8dcSSimon Schubert
26635796c8dcSSimon Schubert /* Common entry points */
26645796c8dcSSimon Schubert
26655796c8dcSSimon Schubert /* Get the language of variable VAR. */
26665796c8dcSSimon Schubert static enum varobj_languages
variable_language(struct varobj * var)26675796c8dcSSimon Schubert variable_language (struct varobj *var)
26685796c8dcSSimon Schubert {
26695796c8dcSSimon Schubert enum varobj_languages lang;
26705796c8dcSSimon Schubert
26715796c8dcSSimon Schubert switch (var->root->exp->language_defn->la_language)
26725796c8dcSSimon Schubert {
26735796c8dcSSimon Schubert default:
26745796c8dcSSimon Schubert case language_c:
26755796c8dcSSimon Schubert lang = vlang_c;
26765796c8dcSSimon Schubert break;
26775796c8dcSSimon Schubert case language_cplus:
26785796c8dcSSimon Schubert lang = vlang_cplus;
26795796c8dcSSimon Schubert break;
26805796c8dcSSimon Schubert case language_java:
26815796c8dcSSimon Schubert lang = vlang_java;
26825796c8dcSSimon Schubert break;
2683a45ae5f8SJohn Marino case language_ada:
2684a45ae5f8SJohn Marino lang = vlang_ada;
2685a45ae5f8SJohn Marino break;
26865796c8dcSSimon Schubert }
26875796c8dcSSimon Schubert
26885796c8dcSSimon Schubert return lang;
26895796c8dcSSimon Schubert }
26905796c8dcSSimon Schubert
26915796c8dcSSimon Schubert /* Return the number of children for a given variable.
26925796c8dcSSimon Schubert The result of this function is defined by the language
26935796c8dcSSimon Schubert implementation. The number of children returned by this function
26945796c8dcSSimon Schubert is the number of children that the user will see in the variable
26955796c8dcSSimon Schubert display. */
26965796c8dcSSimon Schubert static int
number_of_children(struct varobj * var)26975796c8dcSSimon Schubert number_of_children (struct varobj *var)
26985796c8dcSSimon Schubert {
2699c50c785cSJohn Marino return (*var->root->lang->number_of_children) (var);
27005796c8dcSSimon Schubert }
27015796c8dcSSimon Schubert
2702c50c785cSJohn Marino /* What is the expression for the root varobj VAR? Returns a malloc'd
2703c50c785cSJohn Marino string. */
27045796c8dcSSimon Schubert static char *
name_of_variable(struct varobj * var)27055796c8dcSSimon Schubert name_of_variable (struct varobj *var)
27065796c8dcSSimon Schubert {
27075796c8dcSSimon Schubert return (*var->root->lang->name_of_variable) (var);
27085796c8dcSSimon Schubert }
27095796c8dcSSimon Schubert
2710c50c785cSJohn Marino /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2711c50c785cSJohn Marino string. */
27125796c8dcSSimon Schubert static char *
name_of_child(struct varobj * var,int index)27135796c8dcSSimon Schubert name_of_child (struct varobj *var, int index)
27145796c8dcSSimon Schubert {
27155796c8dcSSimon Schubert return (*var->root->lang->name_of_child) (var, index);
27165796c8dcSSimon Schubert }
27175796c8dcSSimon Schubert
27185796c8dcSSimon Schubert /* What is the ``struct value *'' of the root variable VAR?
27195796c8dcSSimon Schubert For floating variable object, evaluation can get us a value
27205796c8dcSSimon Schubert of different type from what is stored in varobj already. In
27215796c8dcSSimon Schubert that case:
27225796c8dcSSimon Schubert - *type_changed will be set to 1
27235796c8dcSSimon Schubert - old varobj will be freed, and new one will be
27245796c8dcSSimon Schubert created, with the same name.
27255796c8dcSSimon Schubert - *var_handle will be set to the new varobj
27265796c8dcSSimon Schubert Otherwise, *type_changed will be set to 0. */
27275796c8dcSSimon Schubert static struct value *
value_of_root(struct varobj ** var_handle,int * type_changed)27285796c8dcSSimon Schubert value_of_root (struct varobj **var_handle, int *type_changed)
27295796c8dcSSimon Schubert {
27305796c8dcSSimon Schubert struct varobj *var;
27315796c8dcSSimon Schubert
27325796c8dcSSimon Schubert if (var_handle == NULL)
27335796c8dcSSimon Schubert return NULL;
27345796c8dcSSimon Schubert
27355796c8dcSSimon Schubert var = *var_handle;
27365796c8dcSSimon Schubert
27375796c8dcSSimon Schubert /* This should really be an exception, since this should
27385796c8dcSSimon Schubert only get called with a root variable. */
27395796c8dcSSimon Schubert
27405796c8dcSSimon Schubert if (!is_root_p (var))
27415796c8dcSSimon Schubert return NULL;
27425796c8dcSSimon Schubert
27435796c8dcSSimon Schubert if (var->root->floating)
27445796c8dcSSimon Schubert {
27455796c8dcSSimon Schubert struct varobj *tmp_var;
27465796c8dcSSimon Schubert char *old_type, *new_type;
27475796c8dcSSimon Schubert
27485796c8dcSSimon Schubert tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
27495796c8dcSSimon Schubert USE_SELECTED_FRAME);
27505796c8dcSSimon Schubert if (tmp_var == NULL)
27515796c8dcSSimon Schubert {
27525796c8dcSSimon Schubert return NULL;
27535796c8dcSSimon Schubert }
27545796c8dcSSimon Schubert old_type = varobj_get_type (var);
27555796c8dcSSimon Schubert new_type = varobj_get_type (tmp_var);
27565796c8dcSSimon Schubert if (strcmp (old_type, new_type) == 0)
27575796c8dcSSimon Schubert {
27585796c8dcSSimon Schubert /* The expression presently stored inside var->root->exp
27595796c8dcSSimon Schubert remembers the locations of local variables relatively to
27605796c8dcSSimon Schubert the frame where the expression was created (in DWARF location
27615796c8dcSSimon Schubert button, for example). Naturally, those locations are not
27625796c8dcSSimon Schubert correct in other frames, so update the expression. */
27635796c8dcSSimon Schubert
27645796c8dcSSimon Schubert struct expression *tmp_exp = var->root->exp;
2765cf7f2e2dSJohn Marino
27665796c8dcSSimon Schubert var->root->exp = tmp_var->root->exp;
27675796c8dcSSimon Schubert tmp_var->root->exp = tmp_exp;
27685796c8dcSSimon Schubert
27695796c8dcSSimon Schubert varobj_delete (tmp_var, NULL, 0);
27705796c8dcSSimon Schubert *type_changed = 0;
27715796c8dcSSimon Schubert }
27725796c8dcSSimon Schubert else
27735796c8dcSSimon Schubert {
27745796c8dcSSimon Schubert tmp_var->obj_name = xstrdup (var->obj_name);
27755796c8dcSSimon Schubert tmp_var->from = var->from;
27765796c8dcSSimon Schubert tmp_var->to = var->to;
27775796c8dcSSimon Schubert varobj_delete (var, NULL, 0);
27785796c8dcSSimon Schubert
27795796c8dcSSimon Schubert install_variable (tmp_var);
27805796c8dcSSimon Schubert *var_handle = tmp_var;
27815796c8dcSSimon Schubert var = *var_handle;
27825796c8dcSSimon Schubert *type_changed = 1;
27835796c8dcSSimon Schubert }
27845796c8dcSSimon Schubert xfree (old_type);
27855796c8dcSSimon Schubert xfree (new_type);
27865796c8dcSSimon Schubert }
27875796c8dcSSimon Schubert else
27885796c8dcSSimon Schubert {
27895796c8dcSSimon Schubert *type_changed = 0;
27905796c8dcSSimon Schubert }
27915796c8dcSSimon Schubert
2792*ef5ccd6cSJohn Marino {
2793*ef5ccd6cSJohn Marino struct value *value;
2794*ef5ccd6cSJohn Marino
2795*ef5ccd6cSJohn Marino value = (*var->root->lang->value_of_root) (var_handle);
2796*ef5ccd6cSJohn Marino if (var->value == NULL || value == NULL)
2797*ef5ccd6cSJohn Marino {
2798*ef5ccd6cSJohn Marino /* For root varobj-s, a NULL value indicates a scoping issue.
2799*ef5ccd6cSJohn Marino So, nothing to do in terms of checking for mutations. */
2800*ef5ccd6cSJohn Marino }
2801*ef5ccd6cSJohn Marino else if (varobj_value_has_mutated (var, value, value_type (value)))
2802*ef5ccd6cSJohn Marino {
2803*ef5ccd6cSJohn Marino /* The type has mutated, so the children are no longer valid.
2804*ef5ccd6cSJohn Marino Just delete them, and tell our caller that the type has
2805*ef5ccd6cSJohn Marino changed. */
2806*ef5ccd6cSJohn Marino varobj_delete (var, NULL, 1 /* only_children */);
2807*ef5ccd6cSJohn Marino var->num_children = -1;
2808*ef5ccd6cSJohn Marino var->to = -1;
2809*ef5ccd6cSJohn Marino var->from = -1;
2810*ef5ccd6cSJohn Marino *type_changed = 1;
2811*ef5ccd6cSJohn Marino }
2812*ef5ccd6cSJohn Marino return value;
2813*ef5ccd6cSJohn Marino }
28145796c8dcSSimon Schubert }
28155796c8dcSSimon Schubert
28165796c8dcSSimon Schubert /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
28175796c8dcSSimon Schubert static struct value *
value_of_child(struct varobj * parent,int index)28185796c8dcSSimon Schubert value_of_child (struct varobj *parent, int index)
28195796c8dcSSimon Schubert {
28205796c8dcSSimon Schubert struct value *value;
28215796c8dcSSimon Schubert
28225796c8dcSSimon Schubert value = (*parent->root->lang->value_of_child) (parent, index);
28235796c8dcSSimon Schubert
28245796c8dcSSimon Schubert return value;
28255796c8dcSSimon Schubert }
28265796c8dcSSimon Schubert
28275796c8dcSSimon Schubert /* GDB already has a command called "value_of_variable". Sigh. */
28285796c8dcSSimon Schubert static char *
my_value_of_variable(struct varobj * var,enum varobj_display_formats format)28295796c8dcSSimon Schubert my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
28305796c8dcSSimon Schubert {
28315796c8dcSSimon Schubert if (var->root->is_valid)
28325796c8dcSSimon Schubert {
28335796c8dcSSimon Schubert if (var->pretty_printer)
28345796c8dcSSimon Schubert return value_get_print_value (var->value, var->format, var);
28355796c8dcSSimon Schubert return (*var->root->lang->value_of_variable) (var, format);
28365796c8dcSSimon Schubert }
28375796c8dcSSimon Schubert else
28385796c8dcSSimon Schubert return NULL;
28395796c8dcSSimon Schubert }
28405796c8dcSSimon Schubert
28415796c8dcSSimon Schubert static char *
value_get_print_value(struct value * value,enum varobj_display_formats format,struct varobj * var)28425796c8dcSSimon Schubert value_get_print_value (struct value *value, enum varobj_display_formats format,
28435796c8dcSSimon Schubert struct varobj *var)
28445796c8dcSSimon Schubert {
28455796c8dcSSimon Schubert struct ui_file *stb;
28465796c8dcSSimon Schubert struct cleanup *old_chain;
2847*ef5ccd6cSJohn Marino char *thevalue = NULL;
28485796c8dcSSimon Schubert struct value_print_options opts;
2849cf7f2e2dSJohn Marino struct type *type = NULL;
2850cf7f2e2dSJohn Marino long len = 0;
2851cf7f2e2dSJohn Marino char *encoding = NULL;
2852cf7f2e2dSJohn Marino struct gdbarch *gdbarch = NULL;
2853c50c785cSJohn Marino /* Initialize it just to avoid a GCC false warning. */
2854c50c785cSJohn Marino CORE_ADDR str_addr = 0;
2855c50c785cSJohn Marino int string_print = 0;
28565796c8dcSSimon Schubert
28575796c8dcSSimon Schubert if (value == NULL)
28585796c8dcSSimon Schubert return NULL;
28595796c8dcSSimon Schubert
2860c50c785cSJohn Marino stb = mem_fileopen ();
2861c50c785cSJohn Marino old_chain = make_cleanup_ui_file_delete (stb);
2862c50c785cSJohn Marino
2863cf7f2e2dSJohn Marino gdbarch = get_type_arch (value_type (value));
28645796c8dcSSimon Schubert #if HAVE_PYTHON
28655796c8dcSSimon Schubert {
28665796c8dcSSimon Schubert PyObject *value_formatter = var->pretty_printer;
28675796c8dcSSimon Schubert
2868c50c785cSJohn Marino varobj_ensure_python_env (var);
2869c50c785cSJohn Marino
28705796c8dcSSimon Schubert if (value_formatter)
28715796c8dcSSimon Schubert {
28725796c8dcSSimon Schubert /* First check to see if we have any children at all. If so,
28735796c8dcSSimon Schubert we simply return {...}. */
28745796c8dcSSimon Schubert if (dynamic_varobj_has_child_method (var))
2875c50c785cSJohn Marino {
2876c50c785cSJohn Marino do_cleanups (old_chain);
28775796c8dcSSimon Schubert return xstrdup ("{...}");
2878c50c785cSJohn Marino }
28795796c8dcSSimon Schubert
28805796c8dcSSimon Schubert if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
28815796c8dcSSimon Schubert {
28825796c8dcSSimon Schubert struct value *replacement;
28835796c8dcSSimon Schubert PyObject *output = NULL;
28845796c8dcSSimon Schubert
28855796c8dcSSimon Schubert output = apply_varobj_pretty_printer (value_formatter,
2886c50c785cSJohn Marino &replacement,
2887c50c785cSJohn Marino stb);
2888a45ae5f8SJohn Marino
2889a45ae5f8SJohn Marino /* If we have string like output ... */
28905796c8dcSSimon Schubert if (output)
28915796c8dcSSimon Schubert {
2892c50c785cSJohn Marino make_cleanup_py_decref (output);
2893c50c785cSJohn Marino
2894a45ae5f8SJohn Marino /* If this is a lazy string, extract it. For lazy
2895a45ae5f8SJohn Marino strings we always print as a string, so set
2896a45ae5f8SJohn Marino string_print. */
2897cf7f2e2dSJohn Marino if (gdbpy_is_lazy_string (output))
2898cf7f2e2dSJohn Marino {
2899c50c785cSJohn Marino gdbpy_extract_lazy_string (output, &str_addr, &type,
2900cf7f2e2dSJohn Marino &len, &encoding);
2901c50c785cSJohn Marino make_cleanup (free_current_contents, &encoding);
2902cf7f2e2dSJohn Marino string_print = 1;
2903cf7f2e2dSJohn Marino }
2904cf7f2e2dSJohn Marino else
2905cf7f2e2dSJohn Marino {
2906a45ae5f8SJohn Marino /* If it is a regular (non-lazy) string, extract
2907a45ae5f8SJohn Marino it and copy the contents into THEVALUE. If the
2908a45ae5f8SJohn Marino hint says to print it as a string, set
2909a45ae5f8SJohn Marino string_print. Otherwise just return the extracted
2910a45ae5f8SJohn Marino string as a value. */
2911a45ae5f8SJohn Marino
2912*ef5ccd6cSJohn Marino char *s = python_string_to_target_string (output);
2913cf7f2e2dSJohn Marino
2914*ef5ccd6cSJohn Marino if (s)
29155796c8dcSSimon Schubert {
2916a45ae5f8SJohn Marino char *hint;
2917a45ae5f8SJohn Marino
2918a45ae5f8SJohn Marino hint = gdbpy_get_display_hint (value_formatter);
2919a45ae5f8SJohn Marino if (hint)
2920a45ae5f8SJohn Marino {
2921a45ae5f8SJohn Marino if (!strcmp (hint, "string"))
2922a45ae5f8SJohn Marino string_print = 1;
2923a45ae5f8SJohn Marino xfree (hint);
2924a45ae5f8SJohn Marino }
2925cf7f2e2dSJohn Marino
2926*ef5ccd6cSJohn Marino len = strlen (s);
29275796c8dcSSimon Schubert thevalue = xmemdup (s, len + 1, len + 1);
2928cf7f2e2dSJohn Marino type = builtin_type (gdbarch)->builtin_char;
2929*ef5ccd6cSJohn Marino xfree (s);
2930c50c785cSJohn Marino
2931c50c785cSJohn Marino if (!string_print)
29325796c8dcSSimon Schubert {
2933c50c785cSJohn Marino do_cleanups (old_chain);
29345796c8dcSSimon Schubert return thevalue;
29355796c8dcSSimon Schubert }
2936c50c785cSJohn Marino
2937c50c785cSJohn Marino make_cleanup (xfree, thevalue);
2938c50c785cSJohn Marino }
2939c50c785cSJohn Marino else
2940c50c785cSJohn Marino gdbpy_print_stack ();
2941c50c785cSJohn Marino }
2942c50c785cSJohn Marino }
2943a45ae5f8SJohn Marino /* If the printer returned a replacement value, set VALUE
2944a45ae5f8SJohn Marino to REPLACEMENT. If there is not a replacement value,
2945a45ae5f8SJohn Marino just use the value passed to this function. */
29465796c8dcSSimon Schubert if (replacement)
29475796c8dcSSimon Schubert value = replacement;
29485796c8dcSSimon Schubert }
29495796c8dcSSimon Schubert }
29505796c8dcSSimon Schubert }
29515796c8dcSSimon Schubert #endif
29525796c8dcSSimon Schubert
29535796c8dcSSimon Schubert get_formatted_print_options (&opts, format_code[(int) format]);
29545796c8dcSSimon Schubert opts.deref_ref = 0;
29555796c8dcSSimon Schubert opts.raw = 1;
2956a45ae5f8SJohn Marino
2957a45ae5f8SJohn Marino /* If the THEVALUE has contents, it is a regular string. */
29585796c8dcSSimon Schubert if (thevalue)
2959*ef5ccd6cSJohn Marino LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
2960c50c785cSJohn Marino else if (string_print)
2961a45ae5f8SJohn Marino /* Otherwise, if string_print is set, and it is not a regular
2962a45ae5f8SJohn Marino string, it is a lazy string. */
2963c50c785cSJohn Marino val_print_string (type, encoding, str_addr, len, stb, &opts);
29645796c8dcSSimon Schubert else
2965a45ae5f8SJohn Marino /* All other cases. */
29665796c8dcSSimon Schubert common_val_print (value, stb, 0, &opts, current_language);
2967a45ae5f8SJohn Marino
29685796c8dcSSimon Schubert thevalue = ui_file_xstrdup (stb, NULL);
29695796c8dcSSimon Schubert
29705796c8dcSSimon Schubert do_cleanups (old_chain);
29715796c8dcSSimon Schubert return thevalue;
29725796c8dcSSimon Schubert }
29735796c8dcSSimon Schubert
29745796c8dcSSimon Schubert int
varobj_editable_p(struct varobj * var)29755796c8dcSSimon Schubert varobj_editable_p (struct varobj *var)
29765796c8dcSSimon Schubert {
29775796c8dcSSimon Schubert struct type *type;
29785796c8dcSSimon Schubert
29795796c8dcSSimon Schubert if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
29805796c8dcSSimon Schubert return 0;
29815796c8dcSSimon Schubert
29825796c8dcSSimon Schubert type = get_value_type (var);
29835796c8dcSSimon Schubert
29845796c8dcSSimon Schubert switch (TYPE_CODE (type))
29855796c8dcSSimon Schubert {
29865796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
29875796c8dcSSimon Schubert case TYPE_CODE_UNION:
29885796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
29895796c8dcSSimon Schubert case TYPE_CODE_FUNC:
29905796c8dcSSimon Schubert case TYPE_CODE_METHOD:
29915796c8dcSSimon Schubert return 0;
29925796c8dcSSimon Schubert break;
29935796c8dcSSimon Schubert
29945796c8dcSSimon Schubert default:
29955796c8dcSSimon Schubert return 1;
29965796c8dcSSimon Schubert break;
29975796c8dcSSimon Schubert }
29985796c8dcSSimon Schubert }
29995796c8dcSSimon Schubert
3000*ef5ccd6cSJohn Marino /* Call VAR's value_is_changeable_p language-specific callback. */
30015796c8dcSSimon Schubert
30025796c8dcSSimon Schubert static int
varobj_value_is_changeable_p(struct varobj * var)30035796c8dcSSimon Schubert varobj_value_is_changeable_p (struct varobj *var)
30045796c8dcSSimon Schubert {
3005*ef5ccd6cSJohn Marino return var->root->lang->value_is_changeable_p (var);
30065796c8dcSSimon Schubert }
30075796c8dcSSimon Schubert
30085796c8dcSSimon Schubert /* Return 1 if that varobj is floating, that is is always evaluated in the
30095796c8dcSSimon Schubert selected frame, and not bound to thread/frame. Such variable objects
30105796c8dcSSimon Schubert are created using '@' as frame specifier to -var-create. */
30115796c8dcSSimon Schubert int
varobj_floating_p(struct varobj * var)30125796c8dcSSimon Schubert varobj_floating_p (struct varobj *var)
30135796c8dcSSimon Schubert {
30145796c8dcSSimon Schubert return var->root->floating;
30155796c8dcSSimon Schubert }
30165796c8dcSSimon Schubert
30175796c8dcSSimon Schubert /* Given the value and the type of a variable object,
30185796c8dcSSimon Schubert adjust the value and type to those necessary
30195796c8dcSSimon Schubert for getting children of the variable object.
30205796c8dcSSimon Schubert This includes dereferencing top-level references
30215796c8dcSSimon Schubert to all types and dereferencing pointers to
30225796c8dcSSimon Schubert structures.
30235796c8dcSSimon Schubert
3024*ef5ccd6cSJohn Marino If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
3025*ef5ccd6cSJohn Marino value will be fetched and if it differs from static type
3026*ef5ccd6cSJohn Marino the value will be casted to it.
3027*ef5ccd6cSJohn Marino
30285796c8dcSSimon Schubert Both TYPE and *TYPE should be non-null. VALUE
30295796c8dcSSimon Schubert can be null if we want to only translate type.
30305796c8dcSSimon Schubert *VALUE can be null as well -- if the parent
30315796c8dcSSimon Schubert value is not known.
30325796c8dcSSimon Schubert
30335796c8dcSSimon Schubert If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
30345796c8dcSSimon Schubert depending on whether pointer was dereferenced
30355796c8dcSSimon Schubert in this function. */
30365796c8dcSSimon Schubert static void
adjust_value_for_child_access(struct value ** value,struct type ** type,int * was_ptr,int lookup_actual_type)30375796c8dcSSimon Schubert adjust_value_for_child_access (struct value **value,
30385796c8dcSSimon Schubert struct type **type,
3039*ef5ccd6cSJohn Marino int *was_ptr,
3040*ef5ccd6cSJohn Marino int lookup_actual_type)
30415796c8dcSSimon Schubert {
30425796c8dcSSimon Schubert gdb_assert (type && *type);
30435796c8dcSSimon Schubert
30445796c8dcSSimon Schubert if (was_ptr)
30455796c8dcSSimon Schubert *was_ptr = 0;
30465796c8dcSSimon Schubert
30475796c8dcSSimon Schubert *type = check_typedef (*type);
30485796c8dcSSimon Schubert
30495796c8dcSSimon Schubert /* The type of value stored in varobj, that is passed
30505796c8dcSSimon Schubert to us, is already supposed to be
30515796c8dcSSimon Schubert reference-stripped. */
30525796c8dcSSimon Schubert
30535796c8dcSSimon Schubert gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
30545796c8dcSSimon Schubert
30555796c8dcSSimon Schubert /* Pointers to structures are treated just like
30565796c8dcSSimon Schubert structures when accessing children. Don't
30575796c8dcSSimon Schubert dererences pointers to other types. */
30585796c8dcSSimon Schubert if (TYPE_CODE (*type) == TYPE_CODE_PTR)
30595796c8dcSSimon Schubert {
30605796c8dcSSimon Schubert struct type *target_type = get_target_type (*type);
30615796c8dcSSimon Schubert if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
30625796c8dcSSimon Schubert || TYPE_CODE (target_type) == TYPE_CODE_UNION)
30635796c8dcSSimon Schubert {
30645796c8dcSSimon Schubert if (value && *value)
30655796c8dcSSimon Schubert {
3066*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
3067cf7f2e2dSJohn Marino
3068*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
3069*ef5ccd6cSJohn Marino {
3070*ef5ccd6cSJohn Marino *value = value_ind (*value);
3071*ef5ccd6cSJohn Marino }
3072*ef5ccd6cSJohn Marino
3073*ef5ccd6cSJohn Marino if (except.reason < 0)
30745796c8dcSSimon Schubert *value = NULL;
30755796c8dcSSimon Schubert }
30765796c8dcSSimon Schubert *type = target_type;
30775796c8dcSSimon Schubert if (was_ptr)
30785796c8dcSSimon Schubert *was_ptr = 1;
30795796c8dcSSimon Schubert }
30805796c8dcSSimon Schubert }
30815796c8dcSSimon Schubert
30825796c8dcSSimon Schubert /* The 'get_target_type' function calls check_typedef on
30835796c8dcSSimon Schubert result, so we can immediately check type code. No
30845796c8dcSSimon Schubert need to call check_typedef here. */
3085*ef5ccd6cSJohn Marino
3086*ef5ccd6cSJohn Marino /* Access a real type of the value (if necessary and possible). */
3087*ef5ccd6cSJohn Marino if (value && *value && lookup_actual_type)
3088*ef5ccd6cSJohn Marino {
3089*ef5ccd6cSJohn Marino struct type *enclosing_type;
3090*ef5ccd6cSJohn Marino int real_type_found = 0;
3091*ef5ccd6cSJohn Marino
3092*ef5ccd6cSJohn Marino enclosing_type = value_actual_type (*value, 1, &real_type_found);
3093*ef5ccd6cSJohn Marino if (real_type_found)
3094*ef5ccd6cSJohn Marino {
3095*ef5ccd6cSJohn Marino *type = enclosing_type;
3096*ef5ccd6cSJohn Marino *value = value_cast (enclosing_type, *value);
3097*ef5ccd6cSJohn Marino }
3098*ef5ccd6cSJohn Marino }
3099*ef5ccd6cSJohn Marino }
3100*ef5ccd6cSJohn Marino
3101*ef5ccd6cSJohn Marino /* Implement the "value_is_changeable_p" varobj callback for most
3102*ef5ccd6cSJohn Marino languages. */
3103*ef5ccd6cSJohn Marino
3104*ef5ccd6cSJohn Marino static int
default_value_is_changeable_p(struct varobj * var)3105*ef5ccd6cSJohn Marino default_value_is_changeable_p (struct varobj *var)
3106*ef5ccd6cSJohn Marino {
3107*ef5ccd6cSJohn Marino int r;
3108*ef5ccd6cSJohn Marino struct type *type;
3109*ef5ccd6cSJohn Marino
3110*ef5ccd6cSJohn Marino if (CPLUS_FAKE_CHILD (var))
3111*ef5ccd6cSJohn Marino return 0;
3112*ef5ccd6cSJohn Marino
3113*ef5ccd6cSJohn Marino type = get_value_type (var);
3114*ef5ccd6cSJohn Marino
3115*ef5ccd6cSJohn Marino switch (TYPE_CODE (type))
3116*ef5ccd6cSJohn Marino {
3117*ef5ccd6cSJohn Marino case TYPE_CODE_STRUCT:
3118*ef5ccd6cSJohn Marino case TYPE_CODE_UNION:
3119*ef5ccd6cSJohn Marino case TYPE_CODE_ARRAY:
3120*ef5ccd6cSJohn Marino r = 0;
3121*ef5ccd6cSJohn Marino break;
3122*ef5ccd6cSJohn Marino
3123*ef5ccd6cSJohn Marino default:
3124*ef5ccd6cSJohn Marino r = 1;
3125*ef5ccd6cSJohn Marino }
3126*ef5ccd6cSJohn Marino
3127*ef5ccd6cSJohn Marino return r;
31285796c8dcSSimon Schubert }
31295796c8dcSSimon Schubert
31305796c8dcSSimon Schubert /* C */
3131*ef5ccd6cSJohn Marino
31325796c8dcSSimon Schubert static int
c_number_of_children(struct varobj * var)31335796c8dcSSimon Schubert c_number_of_children (struct varobj *var)
31345796c8dcSSimon Schubert {
31355796c8dcSSimon Schubert struct type *type = get_value_type (var);
31365796c8dcSSimon Schubert int children = 0;
31375796c8dcSSimon Schubert struct type *target;
31385796c8dcSSimon Schubert
3139*ef5ccd6cSJohn Marino adjust_value_for_child_access (NULL, &type, NULL, 0);
31405796c8dcSSimon Schubert target = get_target_type (type);
31415796c8dcSSimon Schubert
31425796c8dcSSimon Schubert switch (TYPE_CODE (type))
31435796c8dcSSimon Schubert {
31445796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
31455796c8dcSSimon Schubert if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
31465796c8dcSSimon Schubert && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
31475796c8dcSSimon Schubert children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
31485796c8dcSSimon Schubert else
31495796c8dcSSimon Schubert /* If we don't know how many elements there are, don't display
31505796c8dcSSimon Schubert any. */
31515796c8dcSSimon Schubert children = 0;
31525796c8dcSSimon Schubert break;
31535796c8dcSSimon Schubert
31545796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
31555796c8dcSSimon Schubert case TYPE_CODE_UNION:
31565796c8dcSSimon Schubert children = TYPE_NFIELDS (type);
31575796c8dcSSimon Schubert break;
31585796c8dcSSimon Schubert
31595796c8dcSSimon Schubert case TYPE_CODE_PTR:
31605796c8dcSSimon Schubert /* The type here is a pointer to non-struct. Typically, pointers
31615796c8dcSSimon Schubert have one child, except for function ptrs, which have no children,
31625796c8dcSSimon Schubert and except for void*, as we don't know what to show.
31635796c8dcSSimon Schubert
31645796c8dcSSimon Schubert We can show char* so we allow it to be dereferenced. If you decide
31655796c8dcSSimon Schubert to test for it, please mind that a little magic is necessary to
31665796c8dcSSimon Schubert properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3167c50c785cSJohn Marino TYPE_NAME == "char". */
31685796c8dcSSimon Schubert if (TYPE_CODE (target) == TYPE_CODE_FUNC
31695796c8dcSSimon Schubert || TYPE_CODE (target) == TYPE_CODE_VOID)
31705796c8dcSSimon Schubert children = 0;
31715796c8dcSSimon Schubert else
31725796c8dcSSimon Schubert children = 1;
31735796c8dcSSimon Schubert break;
31745796c8dcSSimon Schubert
31755796c8dcSSimon Schubert default:
3176c50c785cSJohn Marino /* Other types have no children. */
31775796c8dcSSimon Schubert break;
31785796c8dcSSimon Schubert }
31795796c8dcSSimon Schubert
31805796c8dcSSimon Schubert return children;
31815796c8dcSSimon Schubert }
31825796c8dcSSimon Schubert
31835796c8dcSSimon Schubert static char *
c_name_of_variable(struct varobj * parent)31845796c8dcSSimon Schubert c_name_of_variable (struct varobj *parent)
31855796c8dcSSimon Schubert {
31865796c8dcSSimon Schubert return xstrdup (parent->name);
31875796c8dcSSimon Schubert }
31885796c8dcSSimon Schubert
31895796c8dcSSimon Schubert /* Return the value of element TYPE_INDEX of a structure
31905796c8dcSSimon Schubert value VALUE. VALUE's type should be a structure,
31915796c8dcSSimon Schubert or union, or a typedef to struct/union.
31925796c8dcSSimon Schubert
31935796c8dcSSimon Schubert Returns NULL if getting the value fails. Never throws. */
31945796c8dcSSimon Schubert static struct value *
value_struct_element_index(struct value * value,int type_index)31955796c8dcSSimon Schubert value_struct_element_index (struct value *value, int type_index)
31965796c8dcSSimon Schubert {
31975796c8dcSSimon Schubert struct value *result = NULL;
31985796c8dcSSimon Schubert volatile struct gdb_exception e;
31995796c8dcSSimon Schubert struct type *type = value_type (value);
3200cf7f2e2dSJohn Marino
32015796c8dcSSimon Schubert type = check_typedef (type);
32025796c8dcSSimon Schubert
32035796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
32045796c8dcSSimon Schubert || TYPE_CODE (type) == TYPE_CODE_UNION);
32055796c8dcSSimon Schubert
32065796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ERROR)
32075796c8dcSSimon Schubert {
32085796c8dcSSimon Schubert if (field_is_static (&TYPE_FIELD (type, type_index)))
32095796c8dcSSimon Schubert result = value_static_field (type, type_index);
32105796c8dcSSimon Schubert else
32115796c8dcSSimon Schubert result = value_primitive_field (value, 0, type_index, type);
32125796c8dcSSimon Schubert }
32135796c8dcSSimon Schubert if (e.reason < 0)
32145796c8dcSSimon Schubert {
32155796c8dcSSimon Schubert return NULL;
32165796c8dcSSimon Schubert }
32175796c8dcSSimon Schubert else
32185796c8dcSSimon Schubert {
32195796c8dcSSimon Schubert return result;
32205796c8dcSSimon Schubert }
32215796c8dcSSimon Schubert }
32225796c8dcSSimon Schubert
32235796c8dcSSimon Schubert /* Obtain the information about child INDEX of the variable
32245796c8dcSSimon Schubert object PARENT.
32255796c8dcSSimon Schubert If CNAME is not null, sets *CNAME to the name of the child relative
32265796c8dcSSimon Schubert to the parent.
32275796c8dcSSimon Schubert If CVALUE is not null, sets *CVALUE to the value of the child.
32285796c8dcSSimon Schubert If CTYPE is not null, sets *CTYPE to the type of the child.
32295796c8dcSSimon Schubert
32305796c8dcSSimon Schubert If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
32315796c8dcSSimon Schubert information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
32325796c8dcSSimon Schubert to NULL. */
32335796c8dcSSimon Schubert static void
c_describe_child(struct varobj * parent,int index,char ** cname,struct value ** cvalue,struct type ** ctype,char ** cfull_expression)32345796c8dcSSimon Schubert c_describe_child (struct varobj *parent, int index,
32355796c8dcSSimon Schubert char **cname, struct value **cvalue, struct type **ctype,
32365796c8dcSSimon Schubert char **cfull_expression)
32375796c8dcSSimon Schubert {
32385796c8dcSSimon Schubert struct value *value = parent->value;
32395796c8dcSSimon Schubert struct type *type = get_value_type (parent);
32405796c8dcSSimon Schubert char *parent_expression = NULL;
32415796c8dcSSimon Schubert int was_ptr;
3242*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
32435796c8dcSSimon Schubert
32445796c8dcSSimon Schubert if (cname)
32455796c8dcSSimon Schubert *cname = NULL;
32465796c8dcSSimon Schubert if (cvalue)
32475796c8dcSSimon Schubert *cvalue = NULL;
32485796c8dcSSimon Schubert if (ctype)
32495796c8dcSSimon Schubert *ctype = NULL;
32505796c8dcSSimon Schubert if (cfull_expression)
32515796c8dcSSimon Schubert {
32525796c8dcSSimon Schubert *cfull_expression = NULL;
3253*ef5ccd6cSJohn Marino parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
32545796c8dcSSimon Schubert }
3255*ef5ccd6cSJohn Marino adjust_value_for_child_access (&value, &type, &was_ptr, 0);
32565796c8dcSSimon Schubert
32575796c8dcSSimon Schubert switch (TYPE_CODE (type))
32585796c8dcSSimon Schubert {
32595796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
32605796c8dcSSimon Schubert if (cname)
3261c50c785cSJohn Marino *cname
3262c50c785cSJohn Marino = xstrdup (int_string (index
3263cf7f2e2dSJohn Marino + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3264cf7f2e2dSJohn Marino 10, 1, 0, 0));
32655796c8dcSSimon Schubert
32665796c8dcSSimon Schubert if (cvalue && value)
32675796c8dcSSimon Schubert {
32685796c8dcSSimon Schubert int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3269cf7f2e2dSJohn Marino
3270*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
3271*ef5ccd6cSJohn Marino {
3272*ef5ccd6cSJohn Marino *cvalue = value_subscript (value, real_index);
3273*ef5ccd6cSJohn Marino }
32745796c8dcSSimon Schubert }
32755796c8dcSSimon Schubert
32765796c8dcSSimon Schubert if (ctype)
32775796c8dcSSimon Schubert *ctype = get_target_type (type);
32785796c8dcSSimon Schubert
32795796c8dcSSimon Schubert if (cfull_expression)
3280cf7f2e2dSJohn Marino *cfull_expression =
3281cf7f2e2dSJohn Marino xstrprintf ("(%s)[%s]", parent_expression,
3282cf7f2e2dSJohn Marino int_string (index
3283cf7f2e2dSJohn Marino + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3284cf7f2e2dSJohn Marino 10, 1, 0, 0));
32855796c8dcSSimon Schubert
32865796c8dcSSimon Schubert
32875796c8dcSSimon Schubert break;
32885796c8dcSSimon Schubert
32895796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
32905796c8dcSSimon Schubert case TYPE_CODE_UNION:
3291*ef5ccd6cSJohn Marino {
3292*ef5ccd6cSJohn Marino const char *field_name;
3293*ef5ccd6cSJohn Marino
3294*ef5ccd6cSJohn Marino /* If the type is anonymous and the field has no name,
3295*ef5ccd6cSJohn Marino set an appropriate name. */
3296*ef5ccd6cSJohn Marino field_name = TYPE_FIELD_NAME (type, index);
3297*ef5ccd6cSJohn Marino if (field_name == NULL || *field_name == '\0')
3298*ef5ccd6cSJohn Marino {
32995796c8dcSSimon Schubert if (cname)
3300*ef5ccd6cSJohn Marino {
3301*ef5ccd6cSJohn Marino if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3302*ef5ccd6cSJohn Marino == TYPE_CODE_STRUCT)
3303*ef5ccd6cSJohn Marino *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3304*ef5ccd6cSJohn Marino else
3305*ef5ccd6cSJohn Marino *cname = xstrdup (ANONYMOUS_UNION_NAME);
3306*ef5ccd6cSJohn Marino }
3307*ef5ccd6cSJohn Marino
3308*ef5ccd6cSJohn Marino if (cfull_expression)
3309*ef5ccd6cSJohn Marino *cfull_expression = xstrdup ("");
3310*ef5ccd6cSJohn Marino }
3311*ef5ccd6cSJohn Marino else
3312*ef5ccd6cSJohn Marino {
3313*ef5ccd6cSJohn Marino if (cname)
3314*ef5ccd6cSJohn Marino *cname = xstrdup (field_name);
3315*ef5ccd6cSJohn Marino
3316*ef5ccd6cSJohn Marino if (cfull_expression)
3317*ef5ccd6cSJohn Marino {
3318*ef5ccd6cSJohn Marino char *join = was_ptr ? "->" : ".";
3319*ef5ccd6cSJohn Marino
3320*ef5ccd6cSJohn Marino *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3321*ef5ccd6cSJohn Marino join, field_name);
3322*ef5ccd6cSJohn Marino }
3323*ef5ccd6cSJohn Marino }
33245796c8dcSSimon Schubert
33255796c8dcSSimon Schubert if (cvalue && value)
33265796c8dcSSimon Schubert {
33275796c8dcSSimon Schubert /* For C, varobj index is the same as type index. */
33285796c8dcSSimon Schubert *cvalue = value_struct_element_index (value, index);
33295796c8dcSSimon Schubert }
33305796c8dcSSimon Schubert
33315796c8dcSSimon Schubert if (ctype)
33325796c8dcSSimon Schubert *ctype = TYPE_FIELD_TYPE (type, index);
33335796c8dcSSimon Schubert }
33345796c8dcSSimon Schubert break;
33355796c8dcSSimon Schubert
33365796c8dcSSimon Schubert case TYPE_CODE_PTR:
33375796c8dcSSimon Schubert if (cname)
33385796c8dcSSimon Schubert *cname = xstrprintf ("*%s", parent->name);
33395796c8dcSSimon Schubert
33405796c8dcSSimon Schubert if (cvalue && value)
33415796c8dcSSimon Schubert {
3342*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
3343*ef5ccd6cSJohn Marino {
3344*ef5ccd6cSJohn Marino *cvalue = value_ind (value);
3345*ef5ccd6cSJohn Marino }
3346cf7f2e2dSJohn Marino
3347*ef5ccd6cSJohn Marino if (except.reason < 0)
33485796c8dcSSimon Schubert *cvalue = NULL;
33495796c8dcSSimon Schubert }
33505796c8dcSSimon Schubert
33515796c8dcSSimon Schubert /* Don't use get_target_type because it calls
33525796c8dcSSimon Schubert check_typedef and here, we want to show the true
33535796c8dcSSimon Schubert declared type of the variable. */
33545796c8dcSSimon Schubert if (ctype)
33555796c8dcSSimon Schubert *ctype = TYPE_TARGET_TYPE (type);
33565796c8dcSSimon Schubert
33575796c8dcSSimon Schubert if (cfull_expression)
33585796c8dcSSimon Schubert *cfull_expression = xstrprintf ("*(%s)", parent_expression);
33595796c8dcSSimon Schubert
33605796c8dcSSimon Schubert break;
33615796c8dcSSimon Schubert
33625796c8dcSSimon Schubert default:
3363c50c785cSJohn Marino /* This should not happen. */
33645796c8dcSSimon Schubert if (cname)
33655796c8dcSSimon Schubert *cname = xstrdup ("???");
33665796c8dcSSimon Schubert if (cfull_expression)
33675796c8dcSSimon Schubert *cfull_expression = xstrdup ("???");
33685796c8dcSSimon Schubert /* Don't set value and type, we don't know then. */
33695796c8dcSSimon Schubert }
33705796c8dcSSimon Schubert }
33715796c8dcSSimon Schubert
33725796c8dcSSimon Schubert static char *
c_name_of_child(struct varobj * parent,int index)33735796c8dcSSimon Schubert c_name_of_child (struct varobj *parent, int index)
33745796c8dcSSimon Schubert {
33755796c8dcSSimon Schubert char *name;
3376cf7f2e2dSJohn Marino
33775796c8dcSSimon Schubert c_describe_child (parent, index, &name, NULL, NULL, NULL);
33785796c8dcSSimon Schubert return name;
33795796c8dcSSimon Schubert }
33805796c8dcSSimon Schubert
33815796c8dcSSimon Schubert static char *
c_path_expr_of_child(struct varobj * child)33825796c8dcSSimon Schubert c_path_expr_of_child (struct varobj *child)
33835796c8dcSSimon Schubert {
33845796c8dcSSimon Schubert c_describe_child (child->parent, child->index, NULL, NULL, NULL,
33855796c8dcSSimon Schubert &child->path_expr);
33865796c8dcSSimon Schubert return child->path_expr;
33875796c8dcSSimon Schubert }
33885796c8dcSSimon Schubert
33895796c8dcSSimon Schubert /* If frame associated with VAR can be found, switch
33905796c8dcSSimon Schubert to it and return 1. Otherwise, return 0. */
33915796c8dcSSimon Schubert static int
check_scope(struct varobj * var)33925796c8dcSSimon Schubert check_scope (struct varobj *var)
33935796c8dcSSimon Schubert {
33945796c8dcSSimon Schubert struct frame_info *fi;
33955796c8dcSSimon Schubert int scope;
33965796c8dcSSimon Schubert
33975796c8dcSSimon Schubert fi = frame_find_by_id (var->root->frame);
33985796c8dcSSimon Schubert scope = fi != NULL;
33995796c8dcSSimon Schubert
34005796c8dcSSimon Schubert if (fi)
34015796c8dcSSimon Schubert {
34025796c8dcSSimon Schubert CORE_ADDR pc = get_frame_pc (fi);
3403cf7f2e2dSJohn Marino
34045796c8dcSSimon Schubert if (pc < BLOCK_START (var->root->valid_block) ||
34055796c8dcSSimon Schubert pc >= BLOCK_END (var->root->valid_block))
34065796c8dcSSimon Schubert scope = 0;
34075796c8dcSSimon Schubert else
34085796c8dcSSimon Schubert select_frame (fi);
34095796c8dcSSimon Schubert }
34105796c8dcSSimon Schubert return scope;
34115796c8dcSSimon Schubert }
34125796c8dcSSimon Schubert
34135796c8dcSSimon Schubert static struct value *
c_value_of_root(struct varobj ** var_handle)34145796c8dcSSimon Schubert c_value_of_root (struct varobj **var_handle)
34155796c8dcSSimon Schubert {
34165796c8dcSSimon Schubert struct value *new_val = NULL;
34175796c8dcSSimon Schubert struct varobj *var = *var_handle;
34185796c8dcSSimon Schubert int within_scope = 0;
34195796c8dcSSimon Schubert struct cleanup *back_to;
34205796c8dcSSimon Schubert
34215796c8dcSSimon Schubert /* Only root variables can be updated... */
34225796c8dcSSimon Schubert if (!is_root_p (var))
3423c50c785cSJohn Marino /* Not a root var. */
34245796c8dcSSimon Schubert return NULL;
34255796c8dcSSimon Schubert
34265796c8dcSSimon Schubert back_to = make_cleanup_restore_current_thread ();
34275796c8dcSSimon Schubert
34285796c8dcSSimon Schubert /* Determine whether the variable is still around. */
34295796c8dcSSimon Schubert if (var->root->valid_block == NULL || var->root->floating)
34305796c8dcSSimon Schubert within_scope = 1;
34315796c8dcSSimon Schubert else if (var->root->thread_id == 0)
34325796c8dcSSimon Schubert {
34335796c8dcSSimon Schubert /* The program was single-threaded when the variable object was
34345796c8dcSSimon Schubert created. Technically, it's possible that the program became
34355796c8dcSSimon Schubert multi-threaded since then, but we don't support such
34365796c8dcSSimon Schubert scenario yet. */
34375796c8dcSSimon Schubert within_scope = check_scope (var);
34385796c8dcSSimon Schubert }
34395796c8dcSSimon Schubert else
34405796c8dcSSimon Schubert {
34415796c8dcSSimon Schubert ptid_t ptid = thread_id_to_pid (var->root->thread_id);
34425796c8dcSSimon Schubert if (in_thread_list (ptid))
34435796c8dcSSimon Schubert {
34445796c8dcSSimon Schubert switch_to_thread (ptid);
34455796c8dcSSimon Schubert within_scope = check_scope (var);
34465796c8dcSSimon Schubert }
34475796c8dcSSimon Schubert }
34485796c8dcSSimon Schubert
34495796c8dcSSimon Schubert if (within_scope)
34505796c8dcSSimon Schubert {
3451*ef5ccd6cSJohn Marino volatile struct gdb_exception except;
3452*ef5ccd6cSJohn Marino
34535796c8dcSSimon Schubert /* We need to catch errors here, because if evaluate
34545796c8dcSSimon Schubert expression fails we want to just return NULL. */
3455*ef5ccd6cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
3456*ef5ccd6cSJohn Marino {
3457*ef5ccd6cSJohn Marino new_val = evaluate_expression (var->root->exp);
3458*ef5ccd6cSJohn Marino }
3459*ef5ccd6cSJohn Marino
34605796c8dcSSimon Schubert return new_val;
34615796c8dcSSimon Schubert }
34625796c8dcSSimon Schubert
34635796c8dcSSimon Schubert do_cleanups (back_to);
34645796c8dcSSimon Schubert
34655796c8dcSSimon Schubert return NULL;
34665796c8dcSSimon Schubert }
34675796c8dcSSimon Schubert
34685796c8dcSSimon Schubert static struct value *
c_value_of_child(struct varobj * parent,int index)34695796c8dcSSimon Schubert c_value_of_child (struct varobj *parent, int index)
34705796c8dcSSimon Schubert {
34715796c8dcSSimon Schubert struct value *value = NULL;
34725796c8dcSSimon Schubert
3473cf7f2e2dSJohn Marino c_describe_child (parent, index, NULL, &value, NULL, NULL);
34745796c8dcSSimon Schubert return value;
34755796c8dcSSimon Schubert }
34765796c8dcSSimon Schubert
34775796c8dcSSimon Schubert static struct type *
c_type_of_child(struct varobj * parent,int index)34785796c8dcSSimon Schubert c_type_of_child (struct varobj *parent, int index)
34795796c8dcSSimon Schubert {
34805796c8dcSSimon Schubert struct type *type = NULL;
3481cf7f2e2dSJohn Marino
34825796c8dcSSimon Schubert c_describe_child (parent, index, NULL, NULL, &type, NULL);
34835796c8dcSSimon Schubert return type;
34845796c8dcSSimon Schubert }
34855796c8dcSSimon Schubert
34865796c8dcSSimon Schubert static char *
c_value_of_variable(struct varobj * var,enum varobj_display_formats format)34875796c8dcSSimon Schubert c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
34885796c8dcSSimon Schubert {
34895796c8dcSSimon Schubert /* BOGUS: if val_print sees a struct/class, or a reference to one,
34905796c8dcSSimon Schubert it will print out its children instead of "{...}". So we need to
34915796c8dcSSimon Schubert catch that case explicitly. */
34925796c8dcSSimon Schubert struct type *type = get_type (var);
34935796c8dcSSimon Schubert
34945796c8dcSSimon Schubert /* Strip top-level references. */
34955796c8dcSSimon Schubert while (TYPE_CODE (type) == TYPE_CODE_REF)
34965796c8dcSSimon Schubert type = check_typedef (TYPE_TARGET_TYPE (type));
34975796c8dcSSimon Schubert
34985796c8dcSSimon Schubert switch (TYPE_CODE (type))
34995796c8dcSSimon Schubert {
35005796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
35015796c8dcSSimon Schubert case TYPE_CODE_UNION:
35025796c8dcSSimon Schubert return xstrdup ("{...}");
35035796c8dcSSimon Schubert /* break; */
35045796c8dcSSimon Schubert
35055796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
35065796c8dcSSimon Schubert {
35075796c8dcSSimon Schubert char *number;
3508cf7f2e2dSJohn Marino
35095796c8dcSSimon Schubert number = xstrprintf ("[%d]", var->num_children);
35105796c8dcSSimon Schubert return (number);
35115796c8dcSSimon Schubert }
35125796c8dcSSimon Schubert /* break; */
35135796c8dcSSimon Schubert
35145796c8dcSSimon Schubert default:
35155796c8dcSSimon Schubert {
35165796c8dcSSimon Schubert if (var->value == NULL)
35175796c8dcSSimon Schubert {
35185796c8dcSSimon Schubert /* This can happen if we attempt to get the value of a struct
35195796c8dcSSimon Schubert member when the parent is an invalid pointer. This is an
35205796c8dcSSimon Schubert error condition, so we should tell the caller. */
35215796c8dcSSimon Schubert return NULL;
35225796c8dcSSimon Schubert }
35235796c8dcSSimon Schubert else
35245796c8dcSSimon Schubert {
35255796c8dcSSimon Schubert if (var->not_fetched && value_lazy (var->value))
35265796c8dcSSimon Schubert /* Frozen variable and no value yet. We don't
35275796c8dcSSimon Schubert implicitly fetch the value. MI response will
35285796c8dcSSimon Schubert use empty string for the value, which is OK. */
35295796c8dcSSimon Schubert return NULL;
35305796c8dcSSimon Schubert
35315796c8dcSSimon Schubert gdb_assert (varobj_value_is_changeable_p (var));
35325796c8dcSSimon Schubert gdb_assert (!value_lazy (var->value));
35335796c8dcSSimon Schubert
35345796c8dcSSimon Schubert /* If the specified format is the current one,
3535c50c785cSJohn Marino we can reuse print_value. */
35365796c8dcSSimon Schubert if (format == var->format)
35375796c8dcSSimon Schubert return xstrdup (var->print_value);
35385796c8dcSSimon Schubert else
35395796c8dcSSimon Schubert return value_get_print_value (var->value, format, var);
35405796c8dcSSimon Schubert }
35415796c8dcSSimon Schubert }
35425796c8dcSSimon Schubert }
35435796c8dcSSimon Schubert }
35445796c8dcSSimon Schubert
35455796c8dcSSimon Schubert
35465796c8dcSSimon Schubert /* C++ */
35475796c8dcSSimon Schubert
35485796c8dcSSimon Schubert static int
cplus_number_of_children(struct varobj * var)35495796c8dcSSimon Schubert cplus_number_of_children (struct varobj *var)
35505796c8dcSSimon Schubert {
3551*ef5ccd6cSJohn Marino struct value *value = NULL;
35525796c8dcSSimon Schubert struct type *type;
35535796c8dcSSimon Schubert int children, dont_know;
3554*ef5ccd6cSJohn Marino int lookup_actual_type = 0;
3555*ef5ccd6cSJohn Marino struct value_print_options opts;
35565796c8dcSSimon Schubert
35575796c8dcSSimon Schubert dont_know = 1;
35585796c8dcSSimon Schubert children = 0;
35595796c8dcSSimon Schubert
3560*ef5ccd6cSJohn Marino get_user_print_options (&opts);
3561*ef5ccd6cSJohn Marino
35625796c8dcSSimon Schubert if (!CPLUS_FAKE_CHILD (var))
35635796c8dcSSimon Schubert {
35645796c8dcSSimon Schubert type = get_value_type (var);
3565*ef5ccd6cSJohn Marino
3566*ef5ccd6cSJohn Marino /* It is necessary to access a real type (via RTTI). */
3567*ef5ccd6cSJohn Marino if (opts.objectprint)
3568*ef5ccd6cSJohn Marino {
3569*ef5ccd6cSJohn Marino value = var->value;
3570*ef5ccd6cSJohn Marino lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3571*ef5ccd6cSJohn Marino || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3572*ef5ccd6cSJohn Marino }
3573*ef5ccd6cSJohn Marino adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
35745796c8dcSSimon Schubert
35755796c8dcSSimon Schubert if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
35765796c8dcSSimon Schubert ((TYPE_CODE (type)) == TYPE_CODE_UNION))
35775796c8dcSSimon Schubert {
35785796c8dcSSimon Schubert int kids[3];
35795796c8dcSSimon Schubert
35805796c8dcSSimon Schubert cplus_class_num_children (type, kids);
35815796c8dcSSimon Schubert if (kids[v_public] != 0)
35825796c8dcSSimon Schubert children++;
35835796c8dcSSimon Schubert if (kids[v_private] != 0)
35845796c8dcSSimon Schubert children++;
35855796c8dcSSimon Schubert if (kids[v_protected] != 0)
35865796c8dcSSimon Schubert children++;
35875796c8dcSSimon Schubert
3588c50c785cSJohn Marino /* Add any baseclasses. */
35895796c8dcSSimon Schubert children += TYPE_N_BASECLASSES (type);
35905796c8dcSSimon Schubert dont_know = 0;
35915796c8dcSSimon Schubert
3592c50c785cSJohn Marino /* FIXME: save children in var. */
35935796c8dcSSimon Schubert }
35945796c8dcSSimon Schubert }
35955796c8dcSSimon Schubert else
35965796c8dcSSimon Schubert {
35975796c8dcSSimon Schubert int kids[3];
35985796c8dcSSimon Schubert
35995796c8dcSSimon Schubert type = get_value_type (var->parent);
3600*ef5ccd6cSJohn Marino
3601*ef5ccd6cSJohn Marino /* It is necessary to access a real type (via RTTI). */
3602*ef5ccd6cSJohn Marino if (opts.objectprint)
3603*ef5ccd6cSJohn Marino {
3604*ef5ccd6cSJohn Marino struct varobj *parent = var->parent;
3605*ef5ccd6cSJohn Marino
3606*ef5ccd6cSJohn Marino value = parent->value;
3607*ef5ccd6cSJohn Marino lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
3608*ef5ccd6cSJohn Marino || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
3609*ef5ccd6cSJohn Marino }
3610*ef5ccd6cSJohn Marino adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
36115796c8dcSSimon Schubert
36125796c8dcSSimon Schubert cplus_class_num_children (type, kids);
36135796c8dcSSimon Schubert if (strcmp (var->name, "public") == 0)
36145796c8dcSSimon Schubert children = kids[v_public];
36155796c8dcSSimon Schubert else if (strcmp (var->name, "private") == 0)
36165796c8dcSSimon Schubert children = kids[v_private];
36175796c8dcSSimon Schubert else
36185796c8dcSSimon Schubert children = kids[v_protected];
36195796c8dcSSimon Schubert dont_know = 0;
36205796c8dcSSimon Schubert }
36215796c8dcSSimon Schubert
36225796c8dcSSimon Schubert if (dont_know)
36235796c8dcSSimon Schubert children = c_number_of_children (var);
36245796c8dcSSimon Schubert
36255796c8dcSSimon Schubert return children;
36265796c8dcSSimon Schubert }
36275796c8dcSSimon Schubert
36285796c8dcSSimon Schubert /* Compute # of public, private, and protected variables in this class.
36295796c8dcSSimon Schubert That means we need to descend into all baseclasses and find out
36305796c8dcSSimon Schubert how many are there, too. */
36315796c8dcSSimon Schubert static void
cplus_class_num_children(struct type * type,int children[3])36325796c8dcSSimon Schubert cplus_class_num_children (struct type *type, int children[3])
36335796c8dcSSimon Schubert {
3634cf7f2e2dSJohn Marino int i, vptr_fieldno;
3635cf7f2e2dSJohn Marino struct type *basetype = NULL;
36365796c8dcSSimon Schubert
36375796c8dcSSimon Schubert children[v_public] = 0;
36385796c8dcSSimon Schubert children[v_private] = 0;
36395796c8dcSSimon Schubert children[v_protected] = 0;
36405796c8dcSSimon Schubert
3641cf7f2e2dSJohn Marino vptr_fieldno = get_vptr_fieldno (type, &basetype);
36425796c8dcSSimon Schubert for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
36435796c8dcSSimon Schubert {
3644cf7f2e2dSJohn Marino /* If we have a virtual table pointer, omit it. Even if virtual
3645cf7f2e2dSJohn Marino table pointers are not specifically marked in the debug info,
3646cf7f2e2dSJohn Marino they should be artificial. */
3647cf7f2e2dSJohn Marino if ((type == basetype && i == vptr_fieldno)
3648cf7f2e2dSJohn Marino || TYPE_FIELD_ARTIFICIAL (type, i))
36495796c8dcSSimon Schubert continue;
36505796c8dcSSimon Schubert
36515796c8dcSSimon Schubert if (TYPE_FIELD_PROTECTED (type, i))
36525796c8dcSSimon Schubert children[v_protected]++;
36535796c8dcSSimon Schubert else if (TYPE_FIELD_PRIVATE (type, i))
36545796c8dcSSimon Schubert children[v_private]++;
36555796c8dcSSimon Schubert else
36565796c8dcSSimon Schubert children[v_public]++;
36575796c8dcSSimon Schubert }
36585796c8dcSSimon Schubert }
36595796c8dcSSimon Schubert
36605796c8dcSSimon Schubert static char *
cplus_name_of_variable(struct varobj * parent)36615796c8dcSSimon Schubert cplus_name_of_variable (struct varobj *parent)
36625796c8dcSSimon Schubert {
36635796c8dcSSimon Schubert return c_name_of_variable (parent);
36645796c8dcSSimon Schubert }
36655796c8dcSSimon Schubert
36665796c8dcSSimon Schubert enum accessibility { private_field, protected_field, public_field };
36675796c8dcSSimon Schubert
36685796c8dcSSimon Schubert /* Check if field INDEX of TYPE has the specified accessibility.
36695796c8dcSSimon Schubert Return 0 if so and 1 otherwise. */
36705796c8dcSSimon Schubert static int
match_accessibility(struct type * type,int index,enum accessibility acc)36715796c8dcSSimon Schubert match_accessibility (struct type *type, int index, enum accessibility acc)
36725796c8dcSSimon Schubert {
36735796c8dcSSimon Schubert if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
36745796c8dcSSimon Schubert return 1;
36755796c8dcSSimon Schubert else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
36765796c8dcSSimon Schubert return 1;
36775796c8dcSSimon Schubert else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
36785796c8dcSSimon Schubert && !TYPE_FIELD_PROTECTED (type, index))
36795796c8dcSSimon Schubert return 1;
36805796c8dcSSimon Schubert else
36815796c8dcSSimon Schubert return 0;
36825796c8dcSSimon Schubert }
36835796c8dcSSimon Schubert
36845796c8dcSSimon Schubert static void
cplus_describe_child(struct varobj * parent,int index,char ** cname,struct value ** cvalue,struct type ** ctype,char ** cfull_expression)36855796c8dcSSimon Schubert cplus_describe_child (struct varobj *parent, int index,
36865796c8dcSSimon Schubert char **cname, struct value **cvalue, struct type **ctype,
36875796c8dcSSimon Schubert char **cfull_expression)
36885796c8dcSSimon Schubert {
36895796c8dcSSimon Schubert struct value *value;
36905796c8dcSSimon Schubert struct type *type;
36915796c8dcSSimon Schubert int was_ptr;
3692*ef5ccd6cSJohn Marino int lookup_actual_type = 0;
36935796c8dcSSimon Schubert char *parent_expression = NULL;
3694*ef5ccd6cSJohn Marino struct varobj *var;
3695*ef5ccd6cSJohn Marino struct value_print_options opts;
36965796c8dcSSimon Schubert
36975796c8dcSSimon Schubert if (cname)
36985796c8dcSSimon Schubert *cname = NULL;
36995796c8dcSSimon Schubert if (cvalue)
37005796c8dcSSimon Schubert *cvalue = NULL;
37015796c8dcSSimon Schubert if (ctype)
37025796c8dcSSimon Schubert *ctype = NULL;
37035796c8dcSSimon Schubert if (cfull_expression)
37045796c8dcSSimon Schubert *cfull_expression = NULL;
37055796c8dcSSimon Schubert
3706*ef5ccd6cSJohn Marino get_user_print_options (&opts);
37075796c8dcSSimon Schubert
3708*ef5ccd6cSJohn Marino var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
3709*ef5ccd6cSJohn Marino if (opts.objectprint)
3710*ef5ccd6cSJohn Marino lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3711*ef5ccd6cSJohn Marino || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3712*ef5ccd6cSJohn Marino value = var->value;
3713*ef5ccd6cSJohn Marino type = get_value_type (var);
3714*ef5ccd6cSJohn Marino if (cfull_expression)
3715*ef5ccd6cSJohn Marino parent_expression = varobj_get_path_expr (get_path_expr_parent (var));
3716*ef5ccd6cSJohn Marino
3717*ef5ccd6cSJohn Marino adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
37185796c8dcSSimon Schubert
37195796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_STRUCT
37205796c8dcSSimon Schubert || TYPE_CODE (type) == TYPE_CODE_UNION)
37215796c8dcSSimon Schubert {
37225796c8dcSSimon Schubert char *join = was_ptr ? "->" : ".";
3723cf7f2e2dSJohn Marino
37245796c8dcSSimon Schubert if (CPLUS_FAKE_CHILD (parent))
37255796c8dcSSimon Schubert {
37265796c8dcSSimon Schubert /* The fields of the class type are ordered as they
37275796c8dcSSimon Schubert appear in the class. We are given an index for a
37285796c8dcSSimon Schubert particular access control type ("public","protected",
37295796c8dcSSimon Schubert or "private"). We must skip over fields that don't
37305796c8dcSSimon Schubert have the access control we are looking for to properly
37315796c8dcSSimon Schubert find the indexed field. */
37325796c8dcSSimon Schubert int type_index = TYPE_N_BASECLASSES (type);
37335796c8dcSSimon Schubert enum accessibility acc = public_field;
3734cf7f2e2dSJohn Marino int vptr_fieldno;
3735cf7f2e2dSJohn Marino struct type *basetype = NULL;
3736*ef5ccd6cSJohn Marino const char *field_name;
3737cf7f2e2dSJohn Marino
3738cf7f2e2dSJohn Marino vptr_fieldno = get_vptr_fieldno (type, &basetype);
37395796c8dcSSimon Schubert if (strcmp (parent->name, "private") == 0)
37405796c8dcSSimon Schubert acc = private_field;
37415796c8dcSSimon Schubert else if (strcmp (parent->name, "protected") == 0)
37425796c8dcSSimon Schubert acc = protected_field;
37435796c8dcSSimon Schubert
37445796c8dcSSimon Schubert while (index >= 0)
37455796c8dcSSimon Schubert {
3746cf7f2e2dSJohn Marino if ((type == basetype && type_index == vptr_fieldno)
3747cf7f2e2dSJohn Marino || TYPE_FIELD_ARTIFICIAL (type, type_index))
37485796c8dcSSimon Schubert ; /* ignore vptr */
37495796c8dcSSimon Schubert else if (match_accessibility (type, type_index, acc))
37505796c8dcSSimon Schubert --index;
37515796c8dcSSimon Schubert ++type_index;
37525796c8dcSSimon Schubert }
37535796c8dcSSimon Schubert --type_index;
37545796c8dcSSimon Schubert
3755*ef5ccd6cSJohn Marino /* If the type is anonymous and the field has no name,
3756*ef5ccd6cSJohn Marino set an appopriate name. */
3757*ef5ccd6cSJohn Marino field_name = TYPE_FIELD_NAME (type, type_index);
3758*ef5ccd6cSJohn Marino if (field_name == NULL || *field_name == '\0')
3759*ef5ccd6cSJohn Marino {
3760*ef5ccd6cSJohn Marino if (cname)
3761*ef5ccd6cSJohn Marino {
3762*ef5ccd6cSJohn Marino if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3763*ef5ccd6cSJohn Marino == TYPE_CODE_STRUCT)
3764*ef5ccd6cSJohn Marino *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3765*ef5ccd6cSJohn Marino else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3766*ef5ccd6cSJohn Marino == TYPE_CODE_UNION)
3767*ef5ccd6cSJohn Marino *cname = xstrdup (ANONYMOUS_UNION_NAME);
3768*ef5ccd6cSJohn Marino }
3769*ef5ccd6cSJohn Marino
3770*ef5ccd6cSJohn Marino if (cfull_expression)
3771*ef5ccd6cSJohn Marino *cfull_expression = xstrdup ("");
3772*ef5ccd6cSJohn Marino }
3773*ef5ccd6cSJohn Marino else
3774*ef5ccd6cSJohn Marino {
37755796c8dcSSimon Schubert if (cname)
37765796c8dcSSimon Schubert *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
37775796c8dcSSimon Schubert
3778*ef5ccd6cSJohn Marino if (cfull_expression)
3779*ef5ccd6cSJohn Marino *cfull_expression
3780*ef5ccd6cSJohn Marino = xstrprintf ("((%s)%s%s)", parent_expression, join,
3781*ef5ccd6cSJohn Marino field_name);
3782*ef5ccd6cSJohn Marino }
3783*ef5ccd6cSJohn Marino
37845796c8dcSSimon Schubert if (cvalue && value)
37855796c8dcSSimon Schubert *cvalue = value_struct_element_index (value, type_index);
37865796c8dcSSimon Schubert
37875796c8dcSSimon Schubert if (ctype)
37885796c8dcSSimon Schubert *ctype = TYPE_FIELD_TYPE (type, type_index);
37895796c8dcSSimon Schubert }
37905796c8dcSSimon Schubert else if (index < TYPE_N_BASECLASSES (type))
37915796c8dcSSimon Schubert {
37925796c8dcSSimon Schubert /* This is a baseclass. */
37935796c8dcSSimon Schubert if (cname)
37945796c8dcSSimon Schubert *cname = xstrdup (TYPE_FIELD_NAME (type, index));
37955796c8dcSSimon Schubert
37965796c8dcSSimon Schubert if (cvalue && value)
37975796c8dcSSimon Schubert *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
37985796c8dcSSimon Schubert
37995796c8dcSSimon Schubert if (ctype)
38005796c8dcSSimon Schubert {
38015796c8dcSSimon Schubert *ctype = TYPE_FIELD_TYPE (type, index);
38025796c8dcSSimon Schubert }
38035796c8dcSSimon Schubert
38045796c8dcSSimon Schubert if (cfull_expression)
38055796c8dcSSimon Schubert {
38065796c8dcSSimon Schubert char *ptr = was_ptr ? "*" : "";
3807cf7f2e2dSJohn Marino
38085796c8dcSSimon Schubert /* Cast the parent to the base' type. Note that in gdb,
38095796c8dcSSimon Schubert expression like
38105796c8dcSSimon Schubert (Base1)d
38115796c8dcSSimon Schubert will create an lvalue, for all appearences, so we don't
38125796c8dcSSimon Schubert need to use more fancy:
38135796c8dcSSimon Schubert *(Base1*)(&d)
3814a45ae5f8SJohn Marino construct.
3815a45ae5f8SJohn Marino
3816a45ae5f8SJohn Marino When we are in the scope of the base class or of one
3817a45ae5f8SJohn Marino of its children, the type field name will be interpreted
3818a45ae5f8SJohn Marino as a constructor, if it exists. Therefore, we must
3819a45ae5f8SJohn Marino indicate that the name is a class name by using the
3820a45ae5f8SJohn Marino 'class' keyword. See PR mi/11912 */
3821a45ae5f8SJohn Marino *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
38225796c8dcSSimon Schubert ptr,
38235796c8dcSSimon Schubert TYPE_FIELD_NAME (type, index),
38245796c8dcSSimon Schubert ptr,
38255796c8dcSSimon Schubert parent_expression);
38265796c8dcSSimon Schubert }
38275796c8dcSSimon Schubert }
38285796c8dcSSimon Schubert else
38295796c8dcSSimon Schubert {
38305796c8dcSSimon Schubert char *access = NULL;
38315796c8dcSSimon Schubert int children[3];
3832cf7f2e2dSJohn Marino
38335796c8dcSSimon Schubert cplus_class_num_children (type, children);
38345796c8dcSSimon Schubert
38355796c8dcSSimon Schubert /* Everything beyond the baseclasses can
38365796c8dcSSimon Schubert only be "public", "private", or "protected"
38375796c8dcSSimon Schubert
38385796c8dcSSimon Schubert The special "fake" children are always output by varobj in
38395796c8dcSSimon Schubert this order. So if INDEX == 2, it MUST be "protected". */
38405796c8dcSSimon Schubert index -= TYPE_N_BASECLASSES (type);
38415796c8dcSSimon Schubert switch (index)
38425796c8dcSSimon Schubert {
38435796c8dcSSimon Schubert case 0:
38445796c8dcSSimon Schubert if (children[v_public] > 0)
38455796c8dcSSimon Schubert access = "public";
38465796c8dcSSimon Schubert else if (children[v_private] > 0)
38475796c8dcSSimon Schubert access = "private";
38485796c8dcSSimon Schubert else
38495796c8dcSSimon Schubert access = "protected";
38505796c8dcSSimon Schubert break;
38515796c8dcSSimon Schubert case 1:
38525796c8dcSSimon Schubert if (children[v_public] > 0)
38535796c8dcSSimon Schubert {
38545796c8dcSSimon Schubert if (children[v_private] > 0)
38555796c8dcSSimon Schubert access = "private";
38565796c8dcSSimon Schubert else
38575796c8dcSSimon Schubert access = "protected";
38585796c8dcSSimon Schubert }
38595796c8dcSSimon Schubert else if (children[v_private] > 0)
38605796c8dcSSimon Schubert access = "protected";
38615796c8dcSSimon Schubert break;
38625796c8dcSSimon Schubert case 2:
3863c50c785cSJohn Marino /* Must be protected. */
38645796c8dcSSimon Schubert access = "protected";
38655796c8dcSSimon Schubert break;
38665796c8dcSSimon Schubert default:
38675796c8dcSSimon Schubert /* error! */
38685796c8dcSSimon Schubert break;
38695796c8dcSSimon Schubert }
38705796c8dcSSimon Schubert
38715796c8dcSSimon Schubert gdb_assert (access);
38725796c8dcSSimon Schubert if (cname)
38735796c8dcSSimon Schubert *cname = xstrdup (access);
38745796c8dcSSimon Schubert
38755796c8dcSSimon Schubert /* Value and type and full expression are null here. */
38765796c8dcSSimon Schubert }
38775796c8dcSSimon Schubert }
38785796c8dcSSimon Schubert else
38795796c8dcSSimon Schubert {
38805796c8dcSSimon Schubert c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
38815796c8dcSSimon Schubert }
38825796c8dcSSimon Schubert }
38835796c8dcSSimon Schubert
38845796c8dcSSimon Schubert static char *
cplus_name_of_child(struct varobj * parent,int index)38855796c8dcSSimon Schubert cplus_name_of_child (struct varobj *parent, int index)
38865796c8dcSSimon Schubert {
38875796c8dcSSimon Schubert char *name = NULL;
3888cf7f2e2dSJohn Marino
38895796c8dcSSimon Schubert cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
38905796c8dcSSimon Schubert return name;
38915796c8dcSSimon Schubert }
38925796c8dcSSimon Schubert
38935796c8dcSSimon Schubert static char *
cplus_path_expr_of_child(struct varobj * child)38945796c8dcSSimon Schubert cplus_path_expr_of_child (struct varobj *child)
38955796c8dcSSimon Schubert {
38965796c8dcSSimon Schubert cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
38975796c8dcSSimon Schubert &child->path_expr);
38985796c8dcSSimon Schubert return child->path_expr;
38995796c8dcSSimon Schubert }
39005796c8dcSSimon Schubert
39015796c8dcSSimon Schubert static struct value *
cplus_value_of_root(struct varobj ** var_handle)39025796c8dcSSimon Schubert cplus_value_of_root (struct varobj **var_handle)
39035796c8dcSSimon Schubert {
39045796c8dcSSimon Schubert return c_value_of_root (var_handle);
39055796c8dcSSimon Schubert }
39065796c8dcSSimon Schubert
39075796c8dcSSimon Schubert static struct value *
cplus_value_of_child(struct varobj * parent,int index)39085796c8dcSSimon Schubert cplus_value_of_child (struct varobj *parent, int index)
39095796c8dcSSimon Schubert {
39105796c8dcSSimon Schubert struct value *value = NULL;
3911cf7f2e2dSJohn Marino
39125796c8dcSSimon Schubert cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
39135796c8dcSSimon Schubert return value;
39145796c8dcSSimon Schubert }
39155796c8dcSSimon Schubert
39165796c8dcSSimon Schubert static struct type *
cplus_type_of_child(struct varobj * parent,int index)39175796c8dcSSimon Schubert cplus_type_of_child (struct varobj *parent, int index)
39185796c8dcSSimon Schubert {
39195796c8dcSSimon Schubert struct type *type = NULL;
3920cf7f2e2dSJohn Marino
39215796c8dcSSimon Schubert cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
39225796c8dcSSimon Schubert return type;
39235796c8dcSSimon Schubert }
39245796c8dcSSimon Schubert
39255796c8dcSSimon Schubert static char *
cplus_value_of_variable(struct varobj * var,enum varobj_display_formats format)3926cf7f2e2dSJohn Marino cplus_value_of_variable (struct varobj *var,
3927cf7f2e2dSJohn Marino enum varobj_display_formats format)
39285796c8dcSSimon Schubert {
39295796c8dcSSimon Schubert
39305796c8dcSSimon Schubert /* If we have one of our special types, don't print out
39315796c8dcSSimon Schubert any value. */
39325796c8dcSSimon Schubert if (CPLUS_FAKE_CHILD (var))
39335796c8dcSSimon Schubert return xstrdup ("");
39345796c8dcSSimon Schubert
39355796c8dcSSimon Schubert return c_value_of_variable (var, format);
39365796c8dcSSimon Schubert }
39375796c8dcSSimon Schubert
39385796c8dcSSimon Schubert /* Java */
39395796c8dcSSimon Schubert
39405796c8dcSSimon Schubert static int
java_number_of_children(struct varobj * var)39415796c8dcSSimon Schubert java_number_of_children (struct varobj *var)
39425796c8dcSSimon Schubert {
39435796c8dcSSimon Schubert return cplus_number_of_children (var);
39445796c8dcSSimon Schubert }
39455796c8dcSSimon Schubert
39465796c8dcSSimon Schubert static char *
java_name_of_variable(struct varobj * parent)39475796c8dcSSimon Schubert java_name_of_variable (struct varobj *parent)
39485796c8dcSSimon Schubert {
39495796c8dcSSimon Schubert char *p, *name;
39505796c8dcSSimon Schubert
39515796c8dcSSimon Schubert name = cplus_name_of_variable (parent);
39525796c8dcSSimon Schubert /* If the name has "-" in it, it is because we
39535796c8dcSSimon Schubert needed to escape periods in the name... */
39545796c8dcSSimon Schubert p = name;
39555796c8dcSSimon Schubert
39565796c8dcSSimon Schubert while (*p != '\000')
39575796c8dcSSimon Schubert {
39585796c8dcSSimon Schubert if (*p == '-')
39595796c8dcSSimon Schubert *p = '.';
39605796c8dcSSimon Schubert p++;
39615796c8dcSSimon Schubert }
39625796c8dcSSimon Schubert
39635796c8dcSSimon Schubert return name;
39645796c8dcSSimon Schubert }
39655796c8dcSSimon Schubert
39665796c8dcSSimon Schubert static char *
java_name_of_child(struct varobj * parent,int index)39675796c8dcSSimon Schubert java_name_of_child (struct varobj *parent, int index)
39685796c8dcSSimon Schubert {
39695796c8dcSSimon Schubert char *name, *p;
39705796c8dcSSimon Schubert
39715796c8dcSSimon Schubert name = cplus_name_of_child (parent, index);
39725796c8dcSSimon Schubert /* Escape any periods in the name... */
39735796c8dcSSimon Schubert p = name;
39745796c8dcSSimon Schubert
39755796c8dcSSimon Schubert while (*p != '\000')
39765796c8dcSSimon Schubert {
39775796c8dcSSimon Schubert if (*p == '.')
39785796c8dcSSimon Schubert *p = '-';
39795796c8dcSSimon Schubert p++;
39805796c8dcSSimon Schubert }
39815796c8dcSSimon Schubert
39825796c8dcSSimon Schubert return name;
39835796c8dcSSimon Schubert }
39845796c8dcSSimon Schubert
39855796c8dcSSimon Schubert static char *
java_path_expr_of_child(struct varobj * child)39865796c8dcSSimon Schubert java_path_expr_of_child (struct varobj *child)
39875796c8dcSSimon Schubert {
39885796c8dcSSimon Schubert return NULL;
39895796c8dcSSimon Schubert }
39905796c8dcSSimon Schubert
39915796c8dcSSimon Schubert static struct value *
java_value_of_root(struct varobj ** var_handle)39925796c8dcSSimon Schubert java_value_of_root (struct varobj **var_handle)
39935796c8dcSSimon Schubert {
39945796c8dcSSimon Schubert return cplus_value_of_root (var_handle);
39955796c8dcSSimon Schubert }
39965796c8dcSSimon Schubert
39975796c8dcSSimon Schubert static struct value *
java_value_of_child(struct varobj * parent,int index)39985796c8dcSSimon Schubert java_value_of_child (struct varobj *parent, int index)
39995796c8dcSSimon Schubert {
40005796c8dcSSimon Schubert return cplus_value_of_child (parent, index);
40015796c8dcSSimon Schubert }
40025796c8dcSSimon Schubert
40035796c8dcSSimon Schubert static struct type *
java_type_of_child(struct varobj * parent,int index)40045796c8dcSSimon Schubert java_type_of_child (struct varobj *parent, int index)
40055796c8dcSSimon Schubert {
40065796c8dcSSimon Schubert return cplus_type_of_child (parent, index);
40075796c8dcSSimon Schubert }
40085796c8dcSSimon Schubert
40095796c8dcSSimon Schubert static char *
java_value_of_variable(struct varobj * var,enum varobj_display_formats format)40105796c8dcSSimon Schubert java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
40115796c8dcSSimon Schubert {
40125796c8dcSSimon Schubert return cplus_value_of_variable (var, format);
40135796c8dcSSimon Schubert }
40145796c8dcSSimon Schubert
4015a45ae5f8SJohn Marino /* Ada specific callbacks for VAROBJs. */
4016a45ae5f8SJohn Marino
4017a45ae5f8SJohn Marino static int
ada_number_of_children(struct varobj * var)4018a45ae5f8SJohn Marino ada_number_of_children (struct varobj *var)
4019a45ae5f8SJohn Marino {
4020*ef5ccd6cSJohn Marino return ada_varobj_get_number_of_children (var->value, var->type);
4021a45ae5f8SJohn Marino }
4022a45ae5f8SJohn Marino
4023a45ae5f8SJohn Marino static char *
ada_name_of_variable(struct varobj * parent)4024a45ae5f8SJohn Marino ada_name_of_variable (struct varobj *parent)
4025a45ae5f8SJohn Marino {
4026a45ae5f8SJohn Marino return c_name_of_variable (parent);
4027a45ae5f8SJohn Marino }
4028a45ae5f8SJohn Marino
4029a45ae5f8SJohn Marino static char *
ada_name_of_child(struct varobj * parent,int index)4030a45ae5f8SJohn Marino ada_name_of_child (struct varobj *parent, int index)
4031a45ae5f8SJohn Marino {
4032*ef5ccd6cSJohn Marino return ada_varobj_get_name_of_child (parent->value, parent->type,
4033*ef5ccd6cSJohn Marino parent->name, index);
4034a45ae5f8SJohn Marino }
4035a45ae5f8SJohn Marino
4036a45ae5f8SJohn Marino static char*
ada_path_expr_of_child(struct varobj * child)4037a45ae5f8SJohn Marino ada_path_expr_of_child (struct varobj *child)
4038a45ae5f8SJohn Marino {
4039*ef5ccd6cSJohn Marino struct varobj *parent = child->parent;
4040*ef5ccd6cSJohn Marino const char *parent_path_expr = varobj_get_path_expr (parent);
4041*ef5ccd6cSJohn Marino
4042*ef5ccd6cSJohn Marino return ada_varobj_get_path_expr_of_child (parent->value,
4043*ef5ccd6cSJohn Marino parent->type,
4044*ef5ccd6cSJohn Marino parent->name,
4045*ef5ccd6cSJohn Marino parent_path_expr,
4046*ef5ccd6cSJohn Marino child->index);
4047a45ae5f8SJohn Marino }
4048a45ae5f8SJohn Marino
4049a45ae5f8SJohn Marino static struct value *
ada_value_of_root(struct varobj ** var_handle)4050a45ae5f8SJohn Marino ada_value_of_root (struct varobj **var_handle)
4051a45ae5f8SJohn Marino {
4052a45ae5f8SJohn Marino return c_value_of_root (var_handle);
4053a45ae5f8SJohn Marino }
4054a45ae5f8SJohn Marino
4055a45ae5f8SJohn Marino static struct value *
ada_value_of_child(struct varobj * parent,int index)4056a45ae5f8SJohn Marino ada_value_of_child (struct varobj *parent, int index)
4057a45ae5f8SJohn Marino {
4058*ef5ccd6cSJohn Marino return ada_varobj_get_value_of_child (parent->value, parent->type,
4059*ef5ccd6cSJohn Marino parent->name, index);
4060a45ae5f8SJohn Marino }
4061a45ae5f8SJohn Marino
4062a45ae5f8SJohn Marino static struct type *
ada_type_of_child(struct varobj * parent,int index)4063a45ae5f8SJohn Marino ada_type_of_child (struct varobj *parent, int index)
4064a45ae5f8SJohn Marino {
4065*ef5ccd6cSJohn Marino return ada_varobj_get_type_of_child (parent->value, parent->type,
4066*ef5ccd6cSJohn Marino index);
4067a45ae5f8SJohn Marino }
4068a45ae5f8SJohn Marino
4069a45ae5f8SJohn Marino static char *
ada_value_of_variable(struct varobj * var,enum varobj_display_formats format)4070a45ae5f8SJohn Marino ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
4071a45ae5f8SJohn Marino {
4072*ef5ccd6cSJohn Marino struct value_print_options opts;
4073*ef5ccd6cSJohn Marino
4074*ef5ccd6cSJohn Marino get_formatted_print_options (&opts, format_code[(int) format]);
4075*ef5ccd6cSJohn Marino opts.deref_ref = 0;
4076*ef5ccd6cSJohn Marino opts.raw = 1;
4077*ef5ccd6cSJohn Marino
4078*ef5ccd6cSJohn Marino return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
4079*ef5ccd6cSJohn Marino }
4080*ef5ccd6cSJohn Marino
4081*ef5ccd6cSJohn Marino /* Implement the "value_is_changeable_p" routine for Ada. */
4082*ef5ccd6cSJohn Marino
4083*ef5ccd6cSJohn Marino static int
ada_value_is_changeable_p(struct varobj * var)4084*ef5ccd6cSJohn Marino ada_value_is_changeable_p (struct varobj *var)
4085*ef5ccd6cSJohn Marino {
4086*ef5ccd6cSJohn Marino struct type *type = var->value ? value_type (var->value) : var->type;
4087*ef5ccd6cSJohn Marino
4088*ef5ccd6cSJohn Marino if (ada_is_array_descriptor_type (type)
4089*ef5ccd6cSJohn Marino && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
4090*ef5ccd6cSJohn Marino {
4091*ef5ccd6cSJohn Marino /* This is in reality a pointer to an unconstrained array.
4092*ef5ccd6cSJohn Marino its value is changeable. */
4093*ef5ccd6cSJohn Marino return 1;
4094*ef5ccd6cSJohn Marino }
4095*ef5ccd6cSJohn Marino
4096*ef5ccd6cSJohn Marino if (ada_is_string_type (type))
4097*ef5ccd6cSJohn Marino {
4098*ef5ccd6cSJohn Marino /* We display the contents of the string in the array's
4099*ef5ccd6cSJohn Marino "value" field. The contents can change, so consider
4100*ef5ccd6cSJohn Marino that the array is changeable. */
4101*ef5ccd6cSJohn Marino return 1;
4102*ef5ccd6cSJohn Marino }
4103*ef5ccd6cSJohn Marino
4104*ef5ccd6cSJohn Marino return default_value_is_changeable_p (var);
4105*ef5ccd6cSJohn Marino }
4106*ef5ccd6cSJohn Marino
4107*ef5ccd6cSJohn Marino /* Implement the "value_has_mutated" routine for Ada. */
4108*ef5ccd6cSJohn Marino
4109*ef5ccd6cSJohn Marino static int
ada_value_has_mutated(struct varobj * var,struct value * new_val,struct type * new_type)4110*ef5ccd6cSJohn Marino ada_value_has_mutated (struct varobj *var, struct value *new_val,
4111*ef5ccd6cSJohn Marino struct type *new_type)
4112*ef5ccd6cSJohn Marino {
4113*ef5ccd6cSJohn Marino int i;
4114*ef5ccd6cSJohn Marino int from = -1;
4115*ef5ccd6cSJohn Marino int to = -1;
4116*ef5ccd6cSJohn Marino
4117*ef5ccd6cSJohn Marino /* If the number of fields have changed, then for sure the type
4118*ef5ccd6cSJohn Marino has mutated. */
4119*ef5ccd6cSJohn Marino if (ada_varobj_get_number_of_children (new_val, new_type)
4120*ef5ccd6cSJohn Marino != var->num_children)
4121*ef5ccd6cSJohn Marino return 1;
4122*ef5ccd6cSJohn Marino
4123*ef5ccd6cSJohn Marino /* If the number of fields have remained the same, then we need
4124*ef5ccd6cSJohn Marino to check the name of each field. If they remain the same,
4125*ef5ccd6cSJohn Marino then chances are the type hasn't mutated. This is technically
4126*ef5ccd6cSJohn Marino an incomplete test, as the child's type might have changed
4127*ef5ccd6cSJohn Marino despite the fact that the name remains the same. But we'll
4128*ef5ccd6cSJohn Marino handle this situation by saying that the child has mutated,
4129*ef5ccd6cSJohn Marino not this value.
4130*ef5ccd6cSJohn Marino
4131*ef5ccd6cSJohn Marino If only part (or none!) of the children have been fetched,
4132*ef5ccd6cSJohn Marino then only check the ones we fetched. It does not matter
4133*ef5ccd6cSJohn Marino to the frontend whether a child that it has not fetched yet
4134*ef5ccd6cSJohn Marino has mutated or not. So just assume it hasn't. */
4135*ef5ccd6cSJohn Marino
4136*ef5ccd6cSJohn Marino restrict_range (var->children, &from, &to);
4137*ef5ccd6cSJohn Marino for (i = from; i < to; i++)
4138*ef5ccd6cSJohn Marino if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4139*ef5ccd6cSJohn Marino var->name, i),
4140*ef5ccd6cSJohn Marino VEC_index (varobj_p, var->children, i)->name) != 0)
4141*ef5ccd6cSJohn Marino return 1;
4142*ef5ccd6cSJohn Marino
4143*ef5ccd6cSJohn Marino return 0;
4144a45ae5f8SJohn Marino }
4145a45ae5f8SJohn Marino
41465796c8dcSSimon Schubert /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
41475796c8dcSSimon Schubert with an arbitrary caller supplied DATA pointer. */
41485796c8dcSSimon Schubert
41495796c8dcSSimon Schubert void
all_root_varobjs(void (* func)(struct varobj * var,void * data),void * data)41505796c8dcSSimon Schubert all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
41515796c8dcSSimon Schubert {
41525796c8dcSSimon Schubert struct varobj_root *var_root, *var_root_next;
41535796c8dcSSimon Schubert
41545796c8dcSSimon Schubert /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
41555796c8dcSSimon Schubert
41565796c8dcSSimon Schubert for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
41575796c8dcSSimon Schubert {
41585796c8dcSSimon Schubert var_root_next = var_root->next;
41595796c8dcSSimon Schubert
41605796c8dcSSimon Schubert (*func) (var_root->rootvar, data);
41615796c8dcSSimon Schubert }
41625796c8dcSSimon Schubert }
41635796c8dcSSimon Schubert
41645796c8dcSSimon Schubert extern void _initialize_varobj (void);
41655796c8dcSSimon Schubert void
_initialize_varobj(void)41665796c8dcSSimon Schubert _initialize_varobj (void)
41675796c8dcSSimon Schubert {
41685796c8dcSSimon Schubert int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
41695796c8dcSSimon Schubert
41705796c8dcSSimon Schubert varobj_table = xmalloc (sizeof_table);
41715796c8dcSSimon Schubert memset (varobj_table, 0, sizeof_table);
41725796c8dcSSimon Schubert
4173*ef5ccd6cSJohn Marino add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance,
4174c50c785cSJohn Marino &varobjdebug,
4175c50c785cSJohn Marino _("Set varobj debugging."),
4176c50c785cSJohn Marino _("Show varobj debugging."),
4177c50c785cSJohn Marino _("When non-zero, varobj debugging is enabled."),
4178c50c785cSJohn Marino NULL, show_varobjdebug,
41795796c8dcSSimon Schubert &setlist, &showlist);
41805796c8dcSSimon Schubert }
41815796c8dcSSimon Schubert
41825796c8dcSSimon Schubert /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4183*ef5ccd6cSJohn Marino defined on globals. It is a helper for varobj_invalidate.
4184*ef5ccd6cSJohn Marino
4185*ef5ccd6cSJohn Marino This function is called after changing the symbol file, in this case the
4186*ef5ccd6cSJohn Marino pointers to "struct type" stored by the varobj are no longer valid. All
4187*ef5ccd6cSJohn Marino varobj must be either re-evaluated, or marked as invalid here. */
41885796c8dcSSimon Schubert
41895796c8dcSSimon Schubert static void
varobj_invalidate_iter(struct varobj * var,void * unused)41905796c8dcSSimon Schubert varobj_invalidate_iter (struct varobj *var, void *unused)
41915796c8dcSSimon Schubert {
4192*ef5ccd6cSJohn Marino /* global and floating var must be re-evaluated. */
4193*ef5ccd6cSJohn Marino if (var->root->floating || var->root->valid_block == NULL)
41945796c8dcSSimon Schubert {
41955796c8dcSSimon Schubert struct varobj *tmp_var;
41965796c8dcSSimon Schubert
41975796c8dcSSimon Schubert /* Try to create a varobj with same expression. If we succeed
41985796c8dcSSimon Schubert replace the old varobj, otherwise invalidate it. */
41995796c8dcSSimon Schubert tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
42005796c8dcSSimon Schubert USE_CURRENT_FRAME);
42015796c8dcSSimon Schubert if (tmp_var != NULL)
42025796c8dcSSimon Schubert {
42035796c8dcSSimon Schubert tmp_var->obj_name = xstrdup (var->obj_name);
42045796c8dcSSimon Schubert varobj_delete (var, NULL, 0);
42055796c8dcSSimon Schubert install_variable (tmp_var);
42065796c8dcSSimon Schubert }
42075796c8dcSSimon Schubert else
42085796c8dcSSimon Schubert var->root->is_valid = 0;
42095796c8dcSSimon Schubert }
42105796c8dcSSimon Schubert else /* locals must be invalidated. */
42115796c8dcSSimon Schubert var->root->is_valid = 0;
42125796c8dcSSimon Schubert }
42135796c8dcSSimon Schubert
42145796c8dcSSimon Schubert /* Invalidate the varobjs that are tied to locals and re-create the ones that
42155796c8dcSSimon Schubert are defined on globals.
42165796c8dcSSimon Schubert Invalidated varobjs will be always printed in_scope="invalid". */
42175796c8dcSSimon Schubert
42185796c8dcSSimon Schubert void
varobj_invalidate(void)42195796c8dcSSimon Schubert varobj_invalidate (void)
42205796c8dcSSimon Schubert {
42215796c8dcSSimon Schubert all_root_varobjs (varobj_invalidate_iter, NULL);
42225796c8dcSSimon Schubert }
4223