OSDN Git Service

(varobj_update): Remove unused local. Use gdb_assert
[pf3gnuchains/sourceware.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20
21 #include "defs.h"
22 #include "exceptions.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "frame.h"
26 #include "language.h"
27 #include "wrapper.h"
28 #include "gdbcmd.h"
29 #include "block.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 #include "varobj.h"
35 #include "vec.h"
36
37 /* Non-zero if we want to see trace of varobj level stuff.  */
38
39 int varobjdebug = 0;
40 static void
41 show_varobjdebug (struct ui_file *file, int from_tty,
42                   struct cmd_list_element *c, const char *value)
43 {
44   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
45 }
46
47 /* String representations of gdb's format codes */
48 char *varobj_format_string[] =
49   { "natural", "binary", "decimal", "hexadecimal", "octal" };
50
51 /* String representations of gdb's known languages */
52 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
53
54 /* Data structures */
55
56 /* Every root variable has one of these structures saved in its
57    varobj. Members which must be free'd are noted. */
58 struct varobj_root
59 {
60
61   /* Alloc'd expression for this parent. */
62   struct expression *exp;
63
64   /* Block for which this expression is valid */
65   struct block *valid_block;
66
67   /* The frame for this expression */
68   struct frame_id frame;
69
70   /* If 1, "update" always recomputes the frame & valid block
71      using the currently selected frame. */
72   int use_selected_frame;
73
74   /* Flag that indicates validity: set to 0 when this varobj_root refers 
75      to symbols that do not exist anymore.  */
76   int is_valid;
77
78   /* Language info for this variable and its children */
79   struct language_specific *lang;
80
81   /* The varobj for this root node. */
82   struct varobj *rootvar;
83
84   /* Next root variable */
85   struct varobj_root *next;
86 };
87
88 typedef struct varobj *varobj_p;
89
90 DEF_VEC_P (varobj_p);
91
92 /* Every variable in the system has a structure of this type defined
93    for it. This structure holds all information necessary to manipulate
94    a particular object variable. Members which must be freed are noted. */
95 struct varobj
96 {
97
98   /* Alloc'd name of the variable for this object.. If this variable is a
99      child, then this name will be the child's source name.
100      (bar, not foo.bar) */
101   /* NOTE: This is the "expression" */
102   char *name;
103
104   /* The alloc'd name for this variable's object. This is here for
105      convenience when constructing this object's children. */
106   char *obj_name;
107
108   /* Index of this variable in its parent or -1 */
109   int index;
110
111   /* The type of this variable. This may NEVER be NULL. */
112   struct type *type;
113
114   /* The value of this expression or subexpression.  A NULL value
115      indicates there was an error getting this value.
116      Invariant: if varobj_value_is_changeable_p (this) is non-zero, 
117      the value is either NULL, or not lazy.  */
118   struct value *value;
119
120   /* The number of (immediate) children this variable has */
121   int num_children;
122
123   /* If this object is a child, this points to its immediate parent. */
124   struct varobj *parent;
125
126   /* Children of this object.  */
127   VEC (varobj_p) *children;
128
129   /* Description of the root variable. Points to root variable for children. */
130   struct varobj_root *root;
131
132   /* The format of the output for this object */
133   enum varobj_display_formats format;
134
135   /* Was this variable updated via a varobj_set_value operation */
136   int updated;
137
138   /* Last print value.  */
139   char *print_value;
140 };
141
142 struct cpstack
143 {
144   char *name;
145   struct cpstack *next;
146 };
147
148 /* A list of varobjs */
149
150 struct vlist
151 {
152   struct varobj *var;
153   struct vlist *next;
154 };
155
156 /* Private function prototypes */
157
158 /* Helper functions for the above subcommands. */
159
160 static int delete_variable (struct cpstack **, struct varobj *, int);
161
162 static void delete_variable_1 (struct cpstack **, int *,
163                                struct varobj *, int, int);
164
165 static int install_variable (struct varobj *);
166
167 static void uninstall_variable (struct varobj *);
168
169 static struct varobj *create_child (struct varobj *, int, char *);
170
171 /* Utility routines */
172
173 static struct varobj *new_variable (void);
174
175 static struct varobj *new_root_variable (void);
176
177 static void free_variable (struct varobj *var);
178
179 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
180
181 static struct type *get_type (struct varobj *var);
182
183 static struct type *get_value_type (struct varobj *var);
184
185 static struct type *get_target_type (struct type *);
186
187 static enum varobj_display_formats variable_default_display (struct varobj *);
188
189 static void cppush (struct cpstack **pstack, char *name);
190
191 static char *cppop (struct cpstack **pstack);
192
193 static int install_new_value (struct varobj *var, struct value *value, 
194                               int initial);
195
196 /* Language-specific routines. */
197
198 static enum varobj_languages variable_language (struct varobj *var);
199
200 static int number_of_children (struct varobj *);
201
202 static char *name_of_variable (struct varobj *);
203
204 static char *name_of_child (struct varobj *, int);
205
206 static struct value *value_of_root (struct varobj **var_handle, int *);
207
208 static struct value *value_of_child (struct varobj *parent, int index);
209
210 static int variable_editable (struct varobj *var);
211
212 static char *my_value_of_variable (struct varobj *var);
213
214 static char *value_get_print_value (struct value *value,
215                                     enum varobj_display_formats format);
216
217 static int varobj_value_is_changeable_p (struct varobj *var);
218
219 static int is_root_p (struct varobj *var);
220
221 /* C implementation */
222
223 static int c_number_of_children (struct varobj *var);
224
225 static char *c_name_of_variable (struct varobj *parent);
226
227 static char *c_name_of_child (struct varobj *parent, int index);
228
229 static struct value *c_value_of_root (struct varobj **var_handle);
230
231 static struct value *c_value_of_child (struct varobj *parent, int index);
232
233 static struct type *c_type_of_child (struct varobj *parent, int index);
234
235 static int c_variable_editable (struct varobj *var);
236
237 static char *c_value_of_variable (struct varobj *var);
238
239 /* C++ implementation */
240
241 static int cplus_number_of_children (struct varobj *var);
242
243 static void cplus_class_num_children (struct type *type, int children[3]);
244
245 static char *cplus_name_of_variable (struct varobj *parent);
246
247 static char *cplus_name_of_child (struct varobj *parent, int index);
248
249 static struct value *cplus_value_of_root (struct varobj **var_handle);
250
251 static struct value *cplus_value_of_child (struct varobj *parent, int index);
252
253 static struct type *cplus_type_of_child (struct varobj *parent, int index);
254
255 static int cplus_variable_editable (struct varobj *var);
256
257 static char *cplus_value_of_variable (struct varobj *var);
258
259 /* Java implementation */
260
261 static int java_number_of_children (struct varobj *var);
262
263 static char *java_name_of_variable (struct varobj *parent);
264
265 static char *java_name_of_child (struct varobj *parent, int index);
266
267 static struct value *java_value_of_root (struct varobj **var_handle);
268
269 static struct value *java_value_of_child (struct varobj *parent, int index);
270
271 static struct type *java_type_of_child (struct varobj *parent, int index);
272
273 static int java_variable_editable (struct varobj *var);
274
275 static char *java_value_of_variable (struct varobj *var);
276
277 /* The language specific vector */
278
279 struct language_specific
280 {
281
282   /* The language of this variable */
283   enum varobj_languages language;
284
285   /* The number of children of PARENT. */
286   int (*number_of_children) (struct varobj * parent);
287
288   /* The name (expression) of a root varobj. */
289   char *(*name_of_variable) (struct varobj * parent);
290
291   /* The name of the INDEX'th child of PARENT. */
292   char *(*name_of_child) (struct varobj * parent, int index);
293
294   /* The ``struct value *'' of the root variable ROOT. */
295   struct value *(*value_of_root) (struct varobj ** root_handle);
296
297   /* The ``struct value *'' of the INDEX'th child of PARENT. */
298   struct value *(*value_of_child) (struct varobj * parent, int index);
299
300   /* The type of the INDEX'th child of PARENT. */
301   struct type *(*type_of_child) (struct varobj * parent, int index);
302
303   /* Is VAR editable? */
304   int (*variable_editable) (struct varobj * var);
305
306   /* The current value of VAR. */
307   char *(*value_of_variable) (struct varobj * var);
308 };
309
310 /* Array of known source language routines. */
311 static struct language_specific languages[vlang_end] = {
312   /* Unknown (try treating as C */
313   {
314    vlang_unknown,
315    c_number_of_children,
316    c_name_of_variable,
317    c_name_of_child,
318    c_value_of_root,
319    c_value_of_child,
320    c_type_of_child,
321    c_variable_editable,
322    c_value_of_variable}
323   ,
324   /* C */
325   {
326    vlang_c,
327    c_number_of_children,
328    c_name_of_variable,
329    c_name_of_child,
330    c_value_of_root,
331    c_value_of_child,
332    c_type_of_child,
333    c_variable_editable,
334    c_value_of_variable}
335   ,
336   /* C++ */
337   {
338    vlang_cplus,
339    cplus_number_of_children,
340    cplus_name_of_variable,
341    cplus_name_of_child,
342    cplus_value_of_root,
343    cplus_value_of_child,
344    cplus_type_of_child,
345    cplus_variable_editable,
346    cplus_value_of_variable}
347   ,
348   /* Java */
349   {
350    vlang_java,
351    java_number_of_children,
352    java_name_of_variable,
353    java_name_of_child,
354    java_value_of_root,
355    java_value_of_child,
356    java_type_of_child,
357    java_variable_editable,
358    java_value_of_variable}
359 };
360
361 /* A little convenience enum for dealing with C++/Java */
362 enum vsections
363 {
364   v_public = 0, v_private, v_protected
365 };
366
367 /* Private data */
368
369 /* Mappings of varobj_display_formats enums to gdb's format codes */
370 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
371
372 /* Header of the list of root variable objects */
373 static struct varobj_root *rootlist;
374 static int rootcount = 0;       /* number of root varobjs in the list */
375
376 /* Prime number indicating the number of buckets in the hash table */
377 /* A prime large enough to avoid too many colisions */
378 #define VAROBJ_TABLE_SIZE 227
379
380 /* Pointer to the varobj hash table (built at run time) */
381 static struct vlist **varobj_table;
382
383 /* Is the variable X one of our "fake" children? */
384 #define CPLUS_FAKE_CHILD(x) \
385 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
386 \f
387
388 /* API Implementation */
389 static int
390 is_root_p (struct varobj *var)
391 {
392   return (var->root->rootvar == var);
393 }
394
395 /* Creates a varobj (not its children) */
396
397 /* Return the full FRAME which corresponds to the given CORE_ADDR
398    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
399
400 static struct frame_info *
401 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
402 {
403   struct frame_info *frame = NULL;
404
405   if (frame_addr == (CORE_ADDR) 0)
406     return NULL;
407
408   while (1)
409     {
410       frame = get_prev_frame (frame);
411       if (frame == NULL)
412         return NULL;
413       if (get_frame_base_address (frame) == frame_addr)
414         return frame;
415     }
416 }
417
418 struct varobj *
419 varobj_create (char *objname,
420                char *expression, CORE_ADDR frame, enum varobj_type type)
421 {
422   struct varobj *var;
423   struct frame_info *fi;
424   struct frame_info *old_fi = NULL;
425   struct block *block;
426   struct cleanup *old_chain;
427
428   /* Fill out a varobj structure for the (root) variable being constructed. */
429   var = new_root_variable ();
430   old_chain = make_cleanup_free_variable (var);
431
432   if (expression != NULL)
433     {
434       char *p;
435       enum varobj_languages lang;
436       struct value *value;
437
438       /* Parse and evaluate the expression, filling in as much
439          of the variable's data as possible */
440
441       /* Allow creator to specify context of variable */
442       if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
443         fi = deprecated_safe_get_selected_frame ();
444       else
445         /* FIXME: cagney/2002-11-23: This code should be doing a
446            lookup using the frame ID and not just the frame's
447            ``address''.  This, of course, means an interface change.
448            However, with out that interface change ISAs, such as the
449            ia64 with its two stacks, won't work.  Similar goes for the
450            case where there is a frameless function.  */
451         fi = find_frame_addr_in_frame_chain (frame);
452
453       /* frame = -2 means always use selected frame */
454       if (type == USE_SELECTED_FRAME)
455         var->root->use_selected_frame = 1;
456
457       block = NULL;
458       if (fi != NULL)
459         block = get_frame_block (fi, 0);
460
461       p = expression;
462       innermost_block = NULL;
463       /* Wrap the call to parse expression, so we can 
464          return a sensible error. */
465       if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
466         {
467           return NULL;
468         }
469
470       /* Don't allow variables to be created for types. */
471       if (var->root->exp->elts[0].opcode == OP_TYPE)
472         {
473           do_cleanups (old_chain);
474           fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
475                               " as an expression.\n");
476           return NULL;
477         }
478
479       var->format = variable_default_display (var);
480       var->root->valid_block = innermost_block;
481       var->name = savestring (expression, strlen (expression));
482
483       /* When the frame is different from the current frame, 
484          we must select the appropriate frame before parsing
485          the expression, otherwise the value will not be current.
486          Since select_frame is so benign, just call it for all cases. */
487       if (fi != NULL)
488         {
489           var->root->frame = get_frame_id (fi);
490           old_fi = get_selected_frame (NULL);
491           select_frame (fi);
492         }
493
494       /* We definitively need to catch errors here.
495          If evaluate_expression succeeds we got the value we wanted.
496          But if it fails, we still go on with a call to evaluate_type()  */
497       if (!gdb_evaluate_expression (var->root->exp, &value))
498         /* Error getting the value.  Try to at least get the
499            right type.  */
500         value = evaluate_type (var->root->exp);
501
502       var->type = value_type (value);
503       install_new_value (var, value, 1 /* Initial assignment */);
504
505       /* Set language info */
506       lang = variable_language (var);
507       var->root->lang = &languages[lang];
508
509       /* Set ourselves as our root */
510       var->root->rootvar = var;
511
512       /* Reset the selected frame */
513       if (fi != NULL)
514         select_frame (old_fi);
515     }
516
517   /* If the variable object name is null, that means this
518      is a temporary variable, so don't install it. */
519
520   if ((var != NULL) && (objname != NULL))
521     {
522       var->obj_name = savestring (objname, strlen (objname));
523
524       /* If a varobj name is duplicated, the install will fail so
525          we must clenup */
526       if (!install_variable (var))
527         {
528           do_cleanups (old_chain);
529           return NULL;
530         }
531     }
532
533   discard_cleanups (old_chain);
534   return var;
535 }
536
537 /* Generates an unique name that can be used for a varobj */
538
539 char *
540 varobj_gen_name (void)
541 {
542   static int id = 0;
543   char *obj_name;
544
545   /* generate a name for this object */
546   id++;
547   obj_name = xstrprintf ("var%d", id);
548
549   return obj_name;
550 }
551
552 /* Given an "objname", returns the pointer to the corresponding varobj
553    or NULL if not found */
554
555 struct varobj *
556 varobj_get_handle (char *objname)
557 {
558   struct vlist *cv;
559   const char *chp;
560   unsigned int index = 0;
561   unsigned int i = 1;
562
563   for (chp = objname; *chp; chp++)
564     {
565       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
566     }
567
568   cv = *(varobj_table + index);
569   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
570     cv = cv->next;
571
572   if (cv == NULL)
573     error (_("Variable object not found"));
574
575   return cv->var;
576 }
577
578 /* Given the handle, return the name of the object */
579
580 char *
581 varobj_get_objname (struct varobj *var)
582 {
583   return var->obj_name;
584 }
585
586 /* Given the handle, return the expression represented by the object */
587
588 char *
589 varobj_get_expression (struct varobj *var)
590 {
591   return name_of_variable (var);
592 }
593
594 /* Deletes a varobj and all its children if only_children == 0,
595    otherwise deletes only the children; returns a malloc'ed list of all the 
596    (malloc'ed) names of the variables that have been deleted (NULL terminated) */
597
598 int
599 varobj_delete (struct varobj *var, char ***dellist, int only_children)
600 {
601   int delcount;
602   int mycount;
603   struct cpstack *result = NULL;
604   char **cp;
605
606   /* Initialize a stack for temporary results */
607   cppush (&result, NULL);
608
609   if (only_children)
610     /* Delete only the variable children */
611     delcount = delete_variable (&result, var, 1 /* only the children */ );
612   else
613     /* Delete the variable and all its children */
614     delcount = delete_variable (&result, var, 0 /* parent+children */ );
615
616   /* We may have been asked to return a list of what has been deleted */
617   if (dellist != NULL)
618     {
619       *dellist = xmalloc ((delcount + 1) * sizeof (char *));
620
621       cp = *dellist;
622       mycount = delcount;
623       *cp = cppop (&result);
624       while ((*cp != NULL) && (mycount > 0))
625         {
626           mycount--;
627           cp++;
628           *cp = cppop (&result);
629         }
630
631       if (mycount || (*cp != NULL))
632         warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
633                  mycount);
634     }
635
636   return delcount;
637 }
638
639 /* Set/Get variable object display format */
640
641 enum varobj_display_formats
642 varobj_set_display_format (struct varobj *var,
643                            enum varobj_display_formats format)
644 {
645   switch (format)
646     {
647     case FORMAT_NATURAL:
648     case FORMAT_BINARY:
649     case FORMAT_DECIMAL:
650     case FORMAT_HEXADECIMAL:
651     case FORMAT_OCTAL:
652       var->format = format;
653       break;
654
655     default:
656       var->format = variable_default_display (var);
657     }
658
659   return var->format;
660 }
661
662 enum varobj_display_formats
663 varobj_get_display_format (struct varobj *var)
664 {
665   return var->format;
666 }
667
668 int
669 varobj_get_num_children (struct varobj *var)
670 {
671   if (var->num_children == -1)
672     var->num_children = number_of_children (var);
673
674   return var->num_children;
675 }
676
677 /* Creates a list of the immediate children of a variable object;
678    the return code is the number of such children or -1 on error */
679
680 int
681 varobj_list_children (struct varobj *var, struct varobj ***childlist)
682 {
683   struct varobj *child;
684   char *name;
685   int i;
686
687   /* sanity check: have we been passed a pointer? */
688   if (childlist == NULL)
689     return -1;
690
691   *childlist = NULL;
692
693   if (var->num_children == -1)
694     var->num_children = number_of_children (var);
695
696   /* If that failed, give up.  */
697   if (var->num_children == -1)
698     return -1;
699
700   /* If we're called when the list of children is not yet initialized,
701      allocate enough elements in it.  */
702   while (VEC_length (varobj_p, var->children) < var->num_children)
703     VEC_safe_push (varobj_p, var->children, NULL);
704
705   /* List of children */
706   *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
707
708   for (i = 0; i < var->num_children; i++)
709     {
710       varobj_p existing;
711
712       /* Mark as the end in case we bail out */
713       *((*childlist) + i) = NULL;
714
715       existing = VEC_index (varobj_p, var->children, i);
716
717       if (existing == NULL)
718         {
719           /* Either it's the first call to varobj_list_children for
720              this variable object, and the child was never created,
721              or it was explicitly deleted by the client.  */
722           name = name_of_child (var, i);
723           existing = create_child (var, i, name);
724           VEC_replace (varobj_p, var->children, i, existing);
725         }
726
727       *((*childlist) + i) = existing;
728     }
729
730   /* End of list is marked by a NULL pointer */
731   *((*childlist) + i) = NULL;
732
733   return var->num_children;
734 }
735
736 /* Obtain the type of an object Variable as a string similar to the one gdb
737    prints on the console */
738
739 char *
740 varobj_get_type (struct varobj *var)
741 {
742   struct value *val;
743   struct cleanup *old_chain;
744   struct ui_file *stb;
745   char *thetype;
746   long length;
747
748   /* For the "fake" variables, do not return a type. (It's type is
749      NULL, too.)
750      Do not return a type for invalid variables as well.  */
751   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
752     return NULL;
753
754   stb = mem_fileopen ();
755   old_chain = make_cleanup_ui_file_delete (stb);
756
757   /* To print the type, we simply create a zero ``struct value *'' and
758      cast it to our type. We then typeprint this variable. */
759   val = value_zero (var->type, not_lval);
760   type_print (value_type (val), "", stb, -1);
761
762   thetype = ui_file_xstrdup (stb, &length);
763   do_cleanups (old_chain);
764   return thetype;
765 }
766
767 /* Obtain the type of an object variable.  */
768
769 struct type *
770 varobj_get_gdb_type (struct varobj *var)
771 {
772   return var->type;
773 }
774
775 enum varobj_languages
776 varobj_get_language (struct varobj *var)
777 {
778   return variable_language (var);
779 }
780
781 int
782 varobj_get_attributes (struct varobj *var)
783 {
784   int attributes = 0;
785
786   if (var->root->is_valid && variable_editable (var))
787     /* FIXME: define masks for attributes */
788     attributes |= 0x00000001;   /* Editable */
789
790   return attributes;
791 }
792
793 char *
794 varobj_get_value (struct varobj *var)
795 {
796   return my_value_of_variable (var);
797 }
798
799 /* Set the value of an object variable (if it is editable) to the
800    value of the given expression */
801 /* Note: Invokes functions that can call error() */
802
803 int
804 varobj_set_value (struct varobj *var, char *expression)
805 {
806   struct value *val;
807   int offset = 0;
808   int error = 0;
809
810   /* The argument "expression" contains the variable's new value.
811      We need to first construct a legal expression for this -- ugh! */
812   /* Does this cover all the bases? */
813   struct expression *exp;
814   struct value *value;
815   int saved_input_radix = input_radix;
816
817   if (var->value != NULL && variable_editable (var))
818     {
819       char *s = expression;
820       int i;
821
822       input_radix = 10;         /* ALWAYS reset to decimal temporarily */
823       exp = parse_exp_1 (&s, 0, 0);
824       if (!gdb_evaluate_expression (exp, &value))
825         {
826           /* We cannot proceed without a valid expression. */
827           xfree (exp);
828           return 0;
829         }
830
831       /* All types that are editable must also be changeable.  */
832       gdb_assert (varobj_value_is_changeable_p (var));
833
834       /* The value of a changeable variable object must not be lazy.  */
835       gdb_assert (!value_lazy (var->value));
836
837       /* Need to coerce the input.  We want to check if the
838          value of the variable object will be different
839          after assignment, and the first thing value_assign
840          does is coerce the input.
841          For example, if we are assigning an array to a pointer variable we
842          should compare the pointer with the the array's address, not with the
843          array's content.  */
844       value = coerce_array (value);
845
846       /* The new value may be lazy.  gdb_value_assign, or 
847          rather value_contents, will take care of this.
848          If fetching of the new value will fail, gdb_value_assign
849          with catch the exception.  */
850       if (!gdb_value_assign (var->value, value, &val))
851         return 0;
852      
853       /* If the value has changed, record it, so that next -var-update can
854          report this change.  If a variable had a value of '1', we've set it
855          to '333' and then set again to '1', when -var-update will report this
856          variable as changed -- because the first assignment has set the
857          'updated' flag.  There's no need to optimize that, because return value
858          of -var-update should be considered an approximation.  */
859       var->updated = install_new_value (var, val, 0 /* Compare values. */);
860       input_radix = saved_input_radix;
861       return 1;
862     }
863
864   return 0;
865 }
866
867 /* Returns a malloc'ed list with all root variable objects */
868 int
869 varobj_list (struct varobj ***varlist)
870 {
871   struct varobj **cv;
872   struct varobj_root *croot;
873   int mycount = rootcount;
874
875   /* Alloc (rootcount + 1) entries for the result */
876   *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
877
878   cv = *varlist;
879   croot = rootlist;
880   while ((croot != NULL) && (mycount > 0))
881     {
882       *cv = croot->rootvar;
883       mycount--;
884       cv++;
885       croot = croot->next;
886     }
887   /* Mark the end of the list */
888   *cv = NULL;
889
890   if (mycount || (croot != NULL))
891     warning
892       ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
893        rootcount, mycount);
894
895   return rootcount;
896 }
897
898 /* Assign a new value to a variable object.  If INITIAL is non-zero,
899    this is the first assignement after the variable object was just
900    created, or changed type.  In that case, just assign the value 
901    and return 0.
902    Otherwise, assign the value and if type_changeable returns non-zero,
903    find if the new value is different from the current value.
904    Return 1 if so, and 0 if the values are equal.  
905
906    The VALUE parameter should not be released -- the function will
907    take care of releasing it when needed.  */
908 static int
909 install_new_value (struct varobj *var, struct value *value, int initial)
910
911   int changeable;
912   int need_to_fetch;
913   int changed = 0;
914
915   /* We need to know the varobj's type to decide if the value should
916      be fetched or not.  C++ fake children (public/protected/private) don't have
917      a type. */
918   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
919   changeable = varobj_value_is_changeable_p (var);
920   need_to_fetch = changeable;
921
922   /* We are not interested in the address of references, and given
923      that in C++ a reference is not rebindable, it cannot
924      meaningfully change.  So, get hold of the real value.  */
925   if (value)
926     {
927       value = coerce_ref (value);
928       release_value (value);
929     }
930
931   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
932     /* For unions, we need to fetch the value implicitly because
933        of implementation of union member fetch.  When gdb
934        creates a value for a field and the value of the enclosing
935        structure is not lazy,  it immediately copies the necessary
936        bytes from the enclosing values.  If the enclosing value is
937        lazy, the call to value_fetch_lazy on the field will read
938        the data from memory.  For unions, that means we'll read the
939        same memory more than once, which is not desirable.  So
940        fetch now.  */
941     need_to_fetch = 1;
942
943   /* The new value might be lazy.  If the type is changeable,
944      that is we'll be comparing values of this type, fetch the
945      value now.  Otherwise, on the next update the old value
946      will be lazy, which means we've lost that old value.  */
947   if (need_to_fetch && value && value_lazy (value))
948     {
949       if (!gdb_value_fetch_lazy (value))
950         {
951           /* Set the value to NULL, so that for the next -var-update,
952              we don't try to compare the new value with this value,
953              that we couldn't even read.  */
954           value = NULL;
955         }
956     }
957
958   /* If the type is changeable, compare the old and the new values.
959      If this is the initial assignment, we don't have any old value
960      to compare with.  */
961   if (initial && changeable)
962     var->print_value = value_get_print_value (value, var->format);
963   else if (changeable)
964     {
965       /* If the value of the varobj was changed by -var-set-value, then the 
966          value in the varobj and in the target is the same.  However, that value
967          is different from the value that the varobj had after the previous
968          -var-update. So need to the varobj as changed.  */
969       if (var->updated)
970         {
971           xfree (var->print_value);
972           var->print_value = value_get_print_value (value, var->format);
973           changed = 1;
974         }
975       else 
976         {
977           /* Try to compare the values.  That requires that both
978              values are non-lazy.  */
979           
980           /* Quick comparison of NULL values.  */
981           if (var->value == NULL && value == NULL)
982             /* Equal. */
983             ;
984           else if (var->value == NULL || value == NULL)
985             {
986               xfree (var->print_value);
987               var->print_value = value_get_print_value (value, var->format);
988               changed = 1;
989             }
990           else
991             {
992               char *print_value;
993               gdb_assert (!value_lazy (var->value));
994               gdb_assert (!value_lazy (value));
995               print_value = value_get_print_value (value, var->format);
996
997               gdb_assert (var->print_value != NULL && print_value != NULL);
998               if (strcmp (var->print_value, print_value) != 0)
999                 {
1000                   xfree (var->print_value);
1001                   var->print_value = print_value;
1002                   changed = 1;
1003                 }
1004               else
1005                 xfree (print_value);
1006             }
1007         }
1008     }
1009
1010   /* We must always keep the new value, since children depend on it.  */
1011   if (var->value != NULL)
1012     value_free (var->value);
1013   var->value = value;
1014   var->updated = 0;
1015
1016   gdb_assert (!var->value || value_type (var->value));
1017
1018   return changed;
1019 }
1020
1021 /* Update the values for a variable and its children.  This is a
1022    two-pronged attack.  First, re-parse the value for the root's
1023    expression to see if it's changed.  Then go all the way
1024    through its children, reconstructing them and noting if they've
1025    changed.
1026    Return value: 
1027     < 0 for error values, see varobj.h.
1028     Otherwise it is the number of children + parent changed.
1029
1030    Only root variables can be updated... 
1031
1032    NOTE: This function may delete the caller's varobj. If it
1033    returns TYPE_CHANGED, then it has done this and VARP will be modified
1034    to point to the new varobj.  */
1035
1036 int
1037 varobj_update (struct varobj **varp, struct varobj ***changelist)
1038 {
1039   int changed = 0;
1040   int type_changed;
1041   int i;
1042   int vleft;
1043   struct varobj *v;
1044   struct varobj **cv;
1045   struct varobj **templist = NULL;
1046   struct value *new;
1047   VEC (varobj_p) *stack = NULL;
1048   VEC (varobj_p) *result = NULL;
1049   struct frame_id old_fid;
1050   struct frame_info *fi;
1051
1052   /* sanity check: have we been passed a pointer?  */
1053   gdb_assert (changelist);
1054
1055   if (!is_root_p (*varp))
1056     error (_("Only root variables can be updated"));
1057
1058   if (!(*varp)->root->is_valid)
1059     return INVALID;
1060
1061   /* Save the selected stack frame, since we will need to change it
1062      in order to evaluate expressions.  */
1063   old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
1064
1065   /* Update the root variable. value_of_root can return NULL
1066      if the variable is no longer around, i.e. we stepped out of
1067      the frame in which a local existed. We are letting the 
1068      value_of_root variable dispose of the varobj if the type
1069      has changed.  */
1070   type_changed = 1;
1071   new = value_of_root (varp, &type_changed);
1072
1073   /* Restore selected frame.  */
1074   fi = frame_find_by_id (old_fid);
1075   if (fi)
1076     select_frame (fi);
1077
1078   /* If this is a "use_selected_frame" varobj, and its type has changed,
1079      them note that it's changed.  */
1080   if (type_changed)
1081     VEC_safe_push (varobj_p, result, *varp);
1082
1083   if (install_new_value ((*varp), new, type_changed))
1084     {
1085       /* If type_changed is 1, install_new_value will never return
1086          non-zero, so we'll never report the same variable twice.  */
1087       gdb_assert (!type_changed);
1088       VEC_safe_push (varobj_p, result, *varp);
1089     }
1090
1091   if (new == NULL)
1092     {
1093       /* This means the varobj itself is out of scope.
1094          Report it.  */
1095       VEC_free (varobj_p, result);
1096       return NOT_IN_SCOPE;
1097     }
1098
1099   VEC_safe_push (varobj_p, stack, *varp);
1100
1101   /* Walk through the children, reconstructing them all.  */
1102   while (!VEC_empty (varobj_p, stack))
1103     {
1104       v = VEC_pop (varobj_p, stack);
1105
1106       /* Push any children.  Use reverse order so that the first
1107          child is popped from the work stack first, and so
1108          will be added to result first.  This does not
1109          affect correctness, just "nicer".  */
1110       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1111         {
1112           varobj_p c = VEC_index (varobj_p, v->children, i);
1113           /* Child may be NULL if explicitly deleted by -var-delete.  */
1114           if (c != NULL)
1115             VEC_safe_push (varobj_p, stack, c);
1116         }
1117
1118       /* Update this variable, unless it's a root, which is already
1119          updated.  */
1120       if (v != *varp)
1121         {         
1122           new = value_of_child (v->parent, v->index);
1123           if (install_new_value (v, new, 0 /* type not changed */))
1124             {
1125               /* Note that it's changed */
1126               VEC_safe_push (varobj_p, result, v);
1127               v->updated = 0;
1128             }
1129         }
1130     }
1131
1132   /* Alloc (changed + 1) list entries.  */
1133   changed = VEC_length (varobj_p, result);
1134   *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1135   cv = *changelist;
1136
1137   for (i = 0; i < changed; ++i)
1138     {
1139       *cv = VEC_index (varobj_p, result, i);
1140       gdb_assert (*cv != NULL);
1141       ++cv;
1142     }
1143   *cv = 0;
1144
1145   if (type_changed)
1146     return TYPE_CHANGED;
1147   else
1148     return changed;
1149 }
1150 \f
1151
1152 /* Helper functions */
1153
1154 /*
1155  * Variable object construction/destruction
1156  */
1157
1158 static int
1159 delete_variable (struct cpstack **resultp, struct varobj *var,
1160                  int only_children_p)
1161 {
1162   int delcount = 0;
1163
1164   delete_variable_1 (resultp, &delcount, var,
1165                      only_children_p, 1 /* remove_from_parent_p */ );
1166
1167   return delcount;
1168 }
1169
1170 /* Delete the variable object VAR and its children */
1171 /* IMPORTANT NOTE: If we delete a variable which is a child
1172    and the parent is not removed we dump core.  It must be always
1173    initially called with remove_from_parent_p set */
1174 static void
1175 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1176                    struct varobj *var, int only_children_p,
1177                    int remove_from_parent_p)
1178 {
1179   int i;
1180
1181   /* Delete any children of this variable, too. */
1182   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1183     {   
1184       varobj_p child = VEC_index (varobj_p, var->children, i);
1185       if (!remove_from_parent_p)
1186         child->parent = NULL;
1187       delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1188     }
1189   VEC_free (varobj_p, var->children);
1190
1191   /* if we were called to delete only the children we are done here */
1192   if (only_children_p)
1193     return;
1194
1195   /* Otherwise, add it to the list of deleted ones and proceed to do so */
1196   /* If the name is null, this is a temporary variable, that has not
1197      yet been installed, don't report it, it belongs to the caller... */
1198   if (var->obj_name != NULL)
1199     {
1200       cppush (resultp, xstrdup (var->obj_name));
1201       *delcountp = *delcountp + 1;
1202     }
1203
1204   /* If this variable has a parent, remove it from its parent's list */
1205   /* OPTIMIZATION: if the parent of this variable is also being deleted, 
1206      (as indicated by remove_from_parent_p) we don't bother doing an
1207      expensive list search to find the element to remove when we are
1208      discarding the list afterwards */
1209   if ((remove_from_parent_p) && (var->parent != NULL))
1210     {
1211       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1212     }
1213
1214   if (var->obj_name != NULL)
1215     uninstall_variable (var);
1216
1217   /* Free memory associated with this variable */
1218   free_variable (var);
1219 }
1220
1221 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1222 static int
1223 install_variable (struct varobj *var)
1224 {
1225   struct vlist *cv;
1226   struct vlist *newvl;
1227   const char *chp;
1228   unsigned int index = 0;
1229   unsigned int i = 1;
1230
1231   for (chp = var->obj_name; *chp; chp++)
1232     {
1233       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1234     }
1235
1236   cv = *(varobj_table + index);
1237   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1238     cv = cv->next;
1239
1240   if (cv != NULL)
1241     error (_("Duplicate variable object name"));
1242
1243   /* Add varobj to hash table */
1244   newvl = xmalloc (sizeof (struct vlist));
1245   newvl->next = *(varobj_table + index);
1246   newvl->var = var;
1247   *(varobj_table + index) = newvl;
1248
1249   /* If root, add varobj to root list */
1250   if (is_root_p (var))
1251     {
1252       /* Add to list of root variables */
1253       if (rootlist == NULL)
1254         var->root->next = NULL;
1255       else
1256         var->root->next = rootlist;
1257       rootlist = var->root;
1258       rootcount++;
1259     }
1260
1261   return 1;                     /* OK */
1262 }
1263
1264 /* Unistall the object VAR. */
1265 static void
1266 uninstall_variable (struct varobj *var)
1267 {
1268   struct vlist *cv;
1269   struct vlist *prev;
1270   struct varobj_root *cr;
1271   struct varobj_root *prer;
1272   const char *chp;
1273   unsigned int index = 0;
1274   unsigned int i = 1;
1275
1276   /* Remove varobj from hash table */
1277   for (chp = var->obj_name; *chp; chp++)
1278     {
1279       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1280     }
1281
1282   cv = *(varobj_table + index);
1283   prev = NULL;
1284   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1285     {
1286       prev = cv;
1287       cv = cv->next;
1288     }
1289
1290   if (varobjdebug)
1291     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1292
1293   if (cv == NULL)
1294     {
1295       warning
1296         ("Assertion failed: Could not find variable object \"%s\" to delete",
1297          var->obj_name);
1298       return;
1299     }
1300
1301   if (prev == NULL)
1302     *(varobj_table + index) = cv->next;
1303   else
1304     prev->next = cv->next;
1305
1306   xfree (cv);
1307
1308   /* If root, remove varobj from root list */
1309   if (is_root_p (var))
1310     {
1311       /* Remove from list of root variables */
1312       if (rootlist == var->root)
1313         rootlist = var->root->next;
1314       else
1315         {
1316           prer = NULL;
1317           cr = rootlist;
1318           while ((cr != NULL) && (cr->rootvar != var))
1319             {
1320               prer = cr;
1321               cr = cr->next;
1322             }
1323           if (cr == NULL)
1324             {
1325               warning
1326                 ("Assertion failed: Could not find varobj \"%s\" in root list",
1327                  var->obj_name);
1328               return;
1329             }
1330           if (prer == NULL)
1331             rootlist = NULL;
1332           else
1333             prer->next = cr->next;
1334         }
1335       rootcount--;
1336     }
1337
1338 }
1339
1340 /* Create and install a child of the parent of the given name */
1341 static struct varobj *
1342 create_child (struct varobj *parent, int index, char *name)
1343 {
1344   struct varobj *child;
1345   char *childs_name;
1346   struct value *value;
1347
1348   child = new_variable ();
1349
1350   /* name is allocated by name_of_child */
1351   child->name = name;
1352   child->index = index;
1353   value = value_of_child (parent, index);
1354   child->parent = parent;
1355   child->root = parent->root;
1356   childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1357   child->obj_name = childs_name;
1358   install_variable (child);
1359
1360   /* Compute the type of the child.  Must do this before
1361      calling install_new_value.  */
1362   if (value != NULL)
1363     /* If the child had no evaluation errors, var->value
1364        will be non-NULL and contain a valid type. */
1365     child->type = value_type (value);
1366   else
1367     /* Otherwise, we must compute the type. */
1368     child->type = (*child->root->lang->type_of_child) (child->parent, 
1369                                                        child->index);
1370   install_new_value (child, value, 1);
1371
1372   return child;
1373 }
1374 \f
1375
1376 /*
1377  * Miscellaneous utility functions.
1378  */
1379
1380 /* Allocate memory and initialize a new variable */
1381 static struct varobj *
1382 new_variable (void)
1383 {
1384   struct varobj *var;
1385
1386   var = (struct varobj *) xmalloc (sizeof (struct varobj));
1387   var->name = NULL;
1388   var->obj_name = NULL;
1389   var->index = -1;
1390   var->type = NULL;
1391   var->value = NULL;
1392   var->num_children = -1;
1393   var->parent = NULL;
1394   var->children = NULL;
1395   var->format = 0;
1396   var->root = NULL;
1397   var->updated = 0;
1398   var->print_value = NULL;
1399
1400   return var;
1401 }
1402
1403 /* Allocate memory and initialize a new root variable */
1404 static struct varobj *
1405 new_root_variable (void)
1406 {
1407   struct varobj *var = new_variable ();
1408   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1409   var->root->lang = NULL;
1410   var->root->exp = NULL;
1411   var->root->valid_block = NULL;
1412   var->root->frame = null_frame_id;
1413   var->root->use_selected_frame = 0;
1414   var->root->rootvar = NULL;
1415   var->root->is_valid = 1;
1416
1417   return var;
1418 }
1419
1420 /* Free any allocated memory associated with VAR. */
1421 static void
1422 free_variable (struct varobj *var)
1423 {
1424   /* Free the expression if this is a root variable. */
1425   if (is_root_p (var))
1426     {
1427       free_current_contents (&var->root->exp);
1428       xfree (var->root);
1429     }
1430
1431   xfree (var->name);
1432   xfree (var->obj_name);
1433   xfree (var->print_value);
1434   xfree (var);
1435 }
1436
1437 static void
1438 do_free_variable_cleanup (void *var)
1439 {
1440   free_variable (var);
1441 }
1442
1443 static struct cleanup *
1444 make_cleanup_free_variable (struct varobj *var)
1445 {
1446   return make_cleanup (do_free_variable_cleanup, var);
1447 }
1448
1449 /* This returns the type of the variable. It also skips past typedefs
1450    to return the real type of the variable.
1451
1452    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1453    except within get_target_type and get_type. */
1454 static struct type *
1455 get_type (struct varobj *var)
1456 {
1457   struct type *type;
1458   type = var->type;
1459
1460   if (type != NULL)
1461     type = check_typedef (type);
1462
1463   return type;
1464 }
1465
1466 /* Return the type of the value that's stored in VAR,
1467    or that would have being stored there if the
1468    value were accessible.  
1469
1470    This differs from VAR->type in that VAR->type is always
1471    the true type of the expession in the source language.
1472    The return value of this function is the type we're
1473    actually storing in varobj, and using for displaying
1474    the values and for comparing previous and new values.
1475
1476    For example, top-level references are always stripped.  */
1477 static struct type *
1478 get_value_type (struct varobj *var)
1479 {
1480   struct type *type;
1481
1482   if (var->value)
1483     type = value_type (var->value);
1484   else
1485     type = var->type;
1486
1487   type = check_typedef (type);
1488
1489   if (TYPE_CODE (type) == TYPE_CODE_REF)
1490     type = get_target_type (type);
1491
1492   type = check_typedef (type);
1493
1494   return type;
1495 }
1496
1497 /* This returns the target type (or NULL) of TYPE, also skipping
1498    past typedefs, just like get_type ().
1499
1500    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1501    except within get_target_type and get_type. */
1502 static struct type *
1503 get_target_type (struct type *type)
1504 {
1505   if (type != NULL)
1506     {
1507       type = TYPE_TARGET_TYPE (type);
1508       if (type != NULL)
1509         type = check_typedef (type);
1510     }
1511
1512   return type;
1513 }
1514
1515 /* What is the default display for this variable? We assume that
1516    everything is "natural". Any exceptions? */
1517 static enum varobj_display_formats
1518 variable_default_display (struct varobj *var)
1519 {
1520   return FORMAT_NATURAL;
1521 }
1522
1523 /* FIXME: The following should be generic for any pointer */
1524 static void
1525 cppush (struct cpstack **pstack, char *name)
1526 {
1527   struct cpstack *s;
1528
1529   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1530   s->name = name;
1531   s->next = *pstack;
1532   *pstack = s;
1533 }
1534
1535 /* FIXME: The following should be generic for any pointer */
1536 static char *
1537 cppop (struct cpstack **pstack)
1538 {
1539   struct cpstack *s;
1540   char *v;
1541
1542   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1543     return NULL;
1544
1545   s = *pstack;
1546   v = s->name;
1547   *pstack = (*pstack)->next;
1548   xfree (s);
1549
1550   return v;
1551 }
1552 \f
1553 /*
1554  * Language-dependencies
1555  */
1556
1557 /* Common entry points */
1558
1559 /* Get the language of variable VAR. */
1560 static enum varobj_languages
1561 variable_language (struct varobj *var)
1562 {
1563   enum varobj_languages lang;
1564
1565   switch (var->root->exp->language_defn->la_language)
1566     {
1567     default:
1568     case language_c:
1569       lang = vlang_c;
1570       break;
1571     case language_cplus:
1572       lang = vlang_cplus;
1573       break;
1574     case language_java:
1575       lang = vlang_java;
1576       break;
1577     }
1578
1579   return lang;
1580 }
1581
1582 /* Return the number of children for a given variable.
1583    The result of this function is defined by the language
1584    implementation. The number of children returned by this function
1585    is the number of children that the user will see in the variable
1586    display. */
1587 static int
1588 number_of_children (struct varobj *var)
1589 {
1590   return (*var->root->lang->number_of_children) (var);;
1591 }
1592
1593 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1594 static char *
1595 name_of_variable (struct varobj *var)
1596 {
1597   return (*var->root->lang->name_of_variable) (var);
1598 }
1599
1600 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1601 static char *
1602 name_of_child (struct varobj *var, int index)
1603 {
1604   return (*var->root->lang->name_of_child) (var, index);
1605 }
1606
1607 /* What is the ``struct value *'' of the root variable VAR? 
1608    TYPE_CHANGED controls what to do if the type of a
1609    use_selected_frame = 1 variable changes.  On input,
1610    TYPE_CHANGED = 1 means discard the old varobj, and replace
1611    it with this one.  TYPE_CHANGED = 0 means leave it around.
1612    NB: In both cases, var_handle will point to the new varobj,
1613    so if you use TYPE_CHANGED = 0, you will have to stash the
1614    old varobj pointer away somewhere before calling this.
1615    On return, TYPE_CHANGED will be 1 if the type has changed, and 
1616    0 otherwise. */
1617 static struct value *
1618 value_of_root (struct varobj **var_handle, int *type_changed)
1619 {
1620   struct varobj *var;
1621
1622   if (var_handle == NULL)
1623     return NULL;
1624
1625   var = *var_handle;
1626
1627   /* This should really be an exception, since this should
1628      only get called with a root variable. */
1629
1630   if (!is_root_p (var))
1631     return NULL;
1632
1633   if (var->root->use_selected_frame)
1634     {
1635       struct varobj *tmp_var;
1636       char *old_type, *new_type;
1637       old_type = varobj_get_type (var);
1638       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1639                                USE_SELECTED_FRAME);
1640       if (tmp_var == NULL)
1641         {
1642           return NULL;
1643         }
1644       new_type = varobj_get_type (tmp_var);
1645       if (strcmp (old_type, new_type) == 0)
1646         {
1647           varobj_delete (tmp_var, NULL, 0);
1648           *type_changed = 0;
1649         }
1650       else
1651         {
1652           if (*type_changed)
1653             {
1654               tmp_var->obj_name =
1655                 savestring (var->obj_name, strlen (var->obj_name));
1656               varobj_delete (var, NULL, 0);
1657             }
1658           else
1659             {
1660               tmp_var->obj_name = varobj_gen_name ();
1661             }
1662           install_variable (tmp_var);
1663           *var_handle = tmp_var;
1664           var = *var_handle;
1665           *type_changed = 1;
1666         }
1667     }
1668   else
1669     {
1670       *type_changed = 0;
1671     }
1672
1673   return (*var->root->lang->value_of_root) (var_handle);
1674 }
1675
1676 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1677 static struct value *
1678 value_of_child (struct varobj *parent, int index)
1679 {
1680   struct value *value;
1681
1682   value = (*parent->root->lang->value_of_child) (parent, index);
1683
1684   return value;
1685 }
1686
1687 /* Is this variable editable? Use the variable's type to make
1688    this determination. */
1689 static int
1690 variable_editable (struct varobj *var)
1691 {
1692   return (*var->root->lang->variable_editable) (var);
1693 }
1694
1695 /* GDB already has a command called "value_of_variable". Sigh. */
1696 static char *
1697 my_value_of_variable (struct varobj *var)
1698 {
1699   if (var->root->is_valid)
1700     return (*var->root->lang->value_of_variable) (var);
1701   else
1702     return NULL;
1703 }
1704
1705 static char *
1706 value_get_print_value (struct value *value, enum varobj_display_formats format)
1707 {
1708   long dummy;
1709   struct ui_file *stb;
1710   struct cleanup *old_chain;
1711   char *thevalue;
1712
1713   if (value == NULL)
1714     return NULL;
1715
1716   stb = mem_fileopen ();
1717   old_chain = make_cleanup_ui_file_delete (stb);
1718
1719   common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1720   thevalue = ui_file_xstrdup (stb, &dummy);
1721
1722   do_cleanups (old_chain);
1723   return thevalue;
1724 }
1725
1726 /* Return non-zero if changes in value of VAR
1727    must be detected and reported by -var-update.
1728    Return zero is -var-update should never report
1729    changes of such values.  This makes sense for structures
1730    (since the changes in children values will be reported separately),
1731    or for artifical objects (like 'public' pseudo-field in C++).
1732
1733    Return value of 0 means that gdb need not call value_fetch_lazy
1734    for the value of this variable object.  */
1735 static int
1736 varobj_value_is_changeable_p (struct varobj *var)
1737 {
1738   int r;
1739   struct type *type;
1740
1741   if (CPLUS_FAKE_CHILD (var))
1742     return 0;
1743
1744   type = get_value_type (var);
1745
1746   switch (TYPE_CODE (type))
1747     {
1748     case TYPE_CODE_STRUCT:
1749     case TYPE_CODE_UNION:
1750     case TYPE_CODE_ARRAY:
1751       r = 0;
1752       break;
1753
1754     default:
1755       r = 1;
1756     }
1757
1758   return r;
1759 }
1760
1761 /* Given the value and the type of a variable object,
1762    adjust the value and type to those necessary
1763    for getting children of the variable object.
1764    This includes dereferencing top-level references
1765    to all types and dereferencing pointers to
1766    structures.  
1767
1768    Both TYPE and *TYPE should be non-null. VALUE
1769    can be null if we want to only translate type.
1770    *VALUE can be null as well -- if the parent
1771    value is not known.  */
1772 static void
1773 adjust_value_for_child_access (struct value **value,
1774                                   struct type **type)
1775 {
1776   gdb_assert (type && *type);
1777
1778   *type = check_typedef (*type);
1779   
1780   /* The type of value stored in varobj, that is passed
1781      to us, is already supposed to be
1782      reference-stripped.  */
1783
1784   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1785
1786   /* Pointers to structures are treated just like
1787      structures when accessing children.  Don't
1788      dererences pointers to other types.  */
1789   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1790     {
1791       struct type *target_type = get_target_type (*type);
1792       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1793           || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1794         {
1795           if (value && *value)
1796             gdb_value_ind (*value, value);        
1797           *type = target_type;
1798         }
1799     }
1800
1801   /* The 'get_target_type' function calls check_typedef on
1802      result, so we can immediately check type code.  No
1803      need to call check_typedef here.  */
1804 }
1805
1806 /* C */
1807 static int
1808 c_number_of_children (struct varobj *var)
1809 {
1810   struct type *type = get_value_type (var);
1811   int children = 0;
1812   struct type *target;
1813
1814   adjust_value_for_child_access (NULL, &type);
1815   target = get_target_type (type);
1816
1817   switch (TYPE_CODE (type))
1818     {
1819     case TYPE_CODE_ARRAY:
1820       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1821           && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1822         children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1823       else
1824         /* If we don't know how many elements there are, don't display
1825            any.  */
1826         children = 0;
1827       break;
1828
1829     case TYPE_CODE_STRUCT:
1830     case TYPE_CODE_UNION:
1831       children = TYPE_NFIELDS (type);
1832       break;
1833
1834     case TYPE_CODE_PTR:
1835       /* The type here is a pointer to non-struct. Typically, pointers
1836          have one child, except for function ptrs, which have no children,
1837          and except for void*, as we don't know what to show.
1838
1839          We can show char* so we allow it to be dereferenced.  If you decide
1840          to test for it, please mind that a little magic is necessary to
1841          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and 
1842          TYPE_NAME == "char" */
1843       if (TYPE_CODE (target) == TYPE_CODE_FUNC
1844           || TYPE_CODE (target) == TYPE_CODE_VOID)
1845         children = 0;
1846       else
1847         children = 1;
1848       break;
1849
1850     default:
1851       /* Other types have no children */
1852       break;
1853     }
1854
1855   return children;
1856 }
1857
1858 static char *
1859 c_name_of_variable (struct varobj *parent)
1860 {
1861   return savestring (parent->name, strlen (parent->name));
1862 }
1863
1864 /* Return the value of element TYPE_INDEX of a structure
1865    value VALUE.  VALUE's type should be a structure,
1866    or union, or a typedef to struct/union.  
1867
1868    Returns NULL if getting the value fails.  Never throws.  */
1869 static struct value *
1870 value_struct_element_index (struct value *value, int type_index)
1871 {
1872   struct value *result = NULL;
1873   volatile struct gdb_exception e;
1874
1875   struct type *type = value_type (value);
1876   type = check_typedef (type);
1877
1878   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1879               || TYPE_CODE (type) == TYPE_CODE_UNION);
1880
1881   TRY_CATCH (e, RETURN_MASK_ERROR)
1882     {
1883       if (TYPE_FIELD_STATIC (type, type_index))
1884         result = value_static_field (type, type_index);
1885       else
1886         result = value_primitive_field (value, 0, type_index, type);
1887     }
1888   if (e.reason < 0)
1889     {
1890       return NULL;
1891     }
1892   else
1893     {
1894       return result;
1895     }
1896 }
1897
1898 /* Obtain the information about child INDEX of the variable
1899    object PARENT.  
1900    If CNAME is not null, sets *CNAME to the name of the child relative
1901    to the parent.
1902    If CVALUE is not null, sets *CVALUE to the value of the child.
1903    If CTYPE is not null, sets *CTYPE to the type of the child.
1904
1905    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1906    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1907    to NULL.  */
1908 static void 
1909 c_describe_child (struct varobj *parent, int index,
1910                   char **cname, struct value **cvalue, struct type **ctype)
1911 {
1912   struct value *value = parent->value;
1913   struct type *type = get_value_type (parent);
1914
1915   if (cname)
1916     *cname = NULL;
1917   if (cvalue)
1918     *cvalue = NULL;
1919   if (ctype)
1920     *ctype = NULL;
1921
1922   adjust_value_for_child_access (&value, &type);
1923       
1924   switch (TYPE_CODE (type))
1925     {
1926     case TYPE_CODE_ARRAY:
1927       if (cname)
1928         *cname = xstrprintf ("%d", index
1929                              + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
1930
1931       if (cvalue && value)
1932         {
1933           int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
1934           struct value *indval = 
1935             value_from_longest (builtin_type_int, (LONGEST) real_index);
1936           gdb_value_subscript (value, indval, cvalue);
1937         }
1938
1939       if (ctype)
1940         *ctype = get_target_type (type);
1941
1942       break;
1943
1944     case TYPE_CODE_STRUCT:
1945     case TYPE_CODE_UNION:
1946       if (cname)
1947         {
1948           char *string = TYPE_FIELD_NAME (type, index);
1949           *cname = savestring (string, strlen (string));
1950         }
1951
1952       if (cvalue && value)
1953         {
1954           /* For C, varobj index is the same as type index.  */
1955           *cvalue = value_struct_element_index (value, index);
1956         }
1957
1958       if (ctype)
1959         *ctype = TYPE_FIELD_TYPE (type, index);
1960
1961       break;
1962
1963     case TYPE_CODE_PTR:
1964       if (cname)
1965         *cname = xstrprintf ("*%s", parent->name);
1966
1967       if (cvalue && value)
1968         gdb_value_ind (value, cvalue);
1969
1970       /* Don't use get_target_type because it calls
1971          check_typedef and here, we want to show the true
1972          declared type of the variable.  */
1973       if (ctype)
1974         *ctype = TYPE_TARGET_TYPE (type);
1975       
1976       break;
1977
1978     default:
1979       /* This should not happen */
1980       if (cname)
1981         *cname = xstrdup ("???");
1982       /* Don't set value and type, we don't know then. */
1983     }
1984 }
1985
1986 static char *
1987 c_name_of_child (struct varobj *parent, int index)
1988 {
1989   char *name;
1990   c_describe_child (parent, index, &name, NULL, NULL);
1991   return name;
1992 }
1993
1994 static struct value *
1995 c_value_of_root (struct varobj **var_handle)
1996 {
1997   struct value *new_val = NULL;
1998   struct varobj *var = *var_handle;
1999   struct frame_info *fi;
2000   int within_scope;
2001
2002   /*  Only root variables can be updated... */
2003   if (!is_root_p (var))
2004     /* Not a root var */
2005     return NULL;
2006
2007
2008   /* Determine whether the variable is still around. */
2009   if (var->root->valid_block == NULL || var->root->use_selected_frame)
2010     within_scope = 1;
2011   else
2012     {
2013       fi = frame_find_by_id (var->root->frame);
2014       within_scope = fi != NULL;
2015       /* FIXME: select_frame could fail */
2016       if (fi)
2017         {
2018           CORE_ADDR pc = get_frame_pc (fi);
2019           if (pc <  BLOCK_START (var->root->valid_block) ||
2020               pc >= BLOCK_END (var->root->valid_block))
2021             within_scope = 0;
2022           else
2023             select_frame (fi);
2024         }         
2025     }
2026
2027   if (within_scope)
2028     {
2029       /* We need to catch errors here, because if evaluate
2030          expression fails we want to just return NULL.  */
2031       gdb_evaluate_expression (var->root->exp, &new_val);
2032       return new_val;
2033     }
2034
2035   return NULL;
2036 }
2037
2038 static struct value *
2039 c_value_of_child (struct varobj *parent, int index)
2040 {
2041   struct value *value = NULL;
2042   c_describe_child (parent, index, NULL, &value, NULL);
2043
2044   return value;
2045 }
2046
2047 static struct type *
2048 c_type_of_child (struct varobj *parent, int index)
2049 {
2050   struct type *type = NULL;
2051   c_describe_child (parent, index, NULL, NULL, &type);
2052   return type;
2053 }
2054
2055 static int
2056 c_variable_editable (struct varobj *var)
2057 {
2058   switch (TYPE_CODE (get_value_type (var)))
2059     {
2060     case TYPE_CODE_STRUCT:
2061     case TYPE_CODE_UNION:
2062     case TYPE_CODE_ARRAY:
2063     case TYPE_CODE_FUNC:
2064     case TYPE_CODE_METHOD:
2065       return 0;
2066       break;
2067
2068     default:
2069       return 1;
2070       break;
2071     }
2072 }
2073
2074 static char *
2075 c_value_of_variable (struct varobj *var)
2076 {
2077   /* BOGUS: if val_print sees a struct/class, or a reference to one,
2078      it will print out its children instead of "{...}".  So we need to
2079      catch that case explicitly.  */
2080   struct type *type = get_type (var);
2081
2082   /* Strip top-level references. */
2083   while (TYPE_CODE (type) == TYPE_CODE_REF)
2084     type = check_typedef (TYPE_TARGET_TYPE (type));
2085
2086   switch (TYPE_CODE (type))
2087     {
2088     case TYPE_CODE_STRUCT:
2089     case TYPE_CODE_UNION:
2090       return xstrdup ("{...}");
2091       /* break; */
2092
2093     case TYPE_CODE_ARRAY:
2094       {
2095         char *number;
2096         number = xstrprintf ("[%d]", var->num_children);
2097         return (number);
2098       }
2099       /* break; */
2100
2101     default:
2102       {
2103         if (var->value == NULL)
2104           {
2105             /* This can happen if we attempt to get the value of a struct
2106                member when the parent is an invalid pointer. This is an
2107                error condition, so we should tell the caller. */
2108             return NULL;
2109           }
2110         else
2111           {
2112             gdb_assert (varobj_value_is_changeable_p (var));
2113             gdb_assert (!value_lazy (var->value));
2114             return value_get_print_value (var->value, var->format);
2115           }
2116       }
2117     }
2118 }
2119 \f
2120
2121 /* C++ */
2122
2123 static int
2124 cplus_number_of_children (struct varobj *var)
2125 {
2126   struct type *type;
2127   int children, dont_know;
2128
2129   dont_know = 1;
2130   children = 0;
2131
2132   if (!CPLUS_FAKE_CHILD (var))
2133     {
2134       type = get_value_type (var);
2135       adjust_value_for_child_access (NULL, &type);
2136
2137       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2138           ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2139         {
2140           int kids[3];
2141
2142           cplus_class_num_children (type, kids);
2143           if (kids[v_public] != 0)
2144             children++;
2145           if (kids[v_private] != 0)
2146             children++;
2147           if (kids[v_protected] != 0)
2148             children++;
2149
2150           /* Add any baseclasses */
2151           children += TYPE_N_BASECLASSES (type);
2152           dont_know = 0;
2153
2154           /* FIXME: save children in var */
2155         }
2156     }
2157   else
2158     {
2159       int kids[3];
2160
2161       type = get_value_type (var->parent);
2162       adjust_value_for_child_access (NULL, &type);
2163
2164       cplus_class_num_children (type, kids);
2165       if (strcmp (var->name, "public") == 0)
2166         children = kids[v_public];
2167       else if (strcmp (var->name, "private") == 0)
2168         children = kids[v_private];
2169       else
2170         children = kids[v_protected];
2171       dont_know = 0;
2172     }
2173
2174   if (dont_know)
2175     children = c_number_of_children (var);
2176
2177   return children;
2178 }
2179
2180 /* Compute # of public, private, and protected variables in this class.
2181    That means we need to descend into all baseclasses and find out
2182    how many are there, too. */
2183 static void
2184 cplus_class_num_children (struct type *type, int children[3])
2185 {
2186   int i;
2187
2188   children[v_public] = 0;
2189   children[v_private] = 0;
2190   children[v_protected] = 0;
2191
2192   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2193     {
2194       /* If we have a virtual table pointer, omit it. */
2195       if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2196         continue;
2197
2198       if (TYPE_FIELD_PROTECTED (type, i))
2199         children[v_protected]++;
2200       else if (TYPE_FIELD_PRIVATE (type, i))
2201         children[v_private]++;
2202       else
2203         children[v_public]++;
2204     }
2205 }
2206
2207 static char *
2208 cplus_name_of_variable (struct varobj *parent)
2209 {
2210   return c_name_of_variable (parent);
2211 }
2212
2213 enum accessibility { private_field, protected_field, public_field };
2214
2215 /* Check if field INDEX of TYPE has the specified accessibility.
2216    Return 0 if so and 1 otherwise.  */
2217 static int 
2218 match_accessibility (struct type *type, int index, enum accessibility acc)
2219 {
2220   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2221     return 1;
2222   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2223     return 1;
2224   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2225            && !TYPE_FIELD_PROTECTED (type, index))
2226     return 1;
2227   else
2228     return 0;
2229 }
2230
2231 static void
2232 cplus_describe_child (struct varobj *parent, int index,
2233                       char **cname, struct value **cvalue, struct type **ctype)
2234 {
2235   char *name = 0;
2236   struct value *value;
2237   struct type *type;
2238
2239   if (cname)
2240     *cname = NULL;
2241   if (cvalue)
2242     *cvalue = NULL;
2243   if (ctype)
2244     *ctype = NULL;
2245
2246
2247   if (CPLUS_FAKE_CHILD (parent))
2248     {
2249       value = parent->parent->value;
2250       type = get_value_type (parent->parent);
2251     }
2252   else
2253     {
2254       value = parent->value;
2255       type = get_value_type (parent);
2256     }
2257
2258   adjust_value_for_child_access (&value, &type);
2259
2260   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2261       || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2262     {
2263       if (CPLUS_FAKE_CHILD (parent))
2264         {
2265           /* The fields of the class type are ordered as they
2266              appear in the class.  We are given an index for a
2267              particular access control type ("public","protected",
2268              or "private").  We must skip over fields that don't
2269              have the access control we are looking for to properly
2270              find the indexed field. */
2271           int type_index = TYPE_N_BASECLASSES (type);
2272           enum accessibility acc = public_field;
2273           if (strcmp (parent->name, "private") == 0)
2274             acc = private_field;
2275           else if (strcmp (parent->name, "protected") == 0)
2276             acc = protected_field;
2277
2278           while (index >= 0)
2279             {
2280               if (TYPE_VPTR_BASETYPE (type) == type
2281                   && type_index == TYPE_VPTR_FIELDNO (type))
2282                 ; /* ignore vptr */
2283               else if (match_accessibility (type, type_index, acc))
2284                     --index;
2285                   ++type_index;
2286             }
2287           --type_index;
2288
2289           if (cname)
2290             *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2291
2292           if (cvalue && value)
2293             *cvalue = value_struct_element_index (value, type_index);
2294
2295           if (ctype)
2296             *ctype = TYPE_FIELD_TYPE (type, type_index);
2297         }
2298       else if (index < TYPE_N_BASECLASSES (type))
2299         {
2300           /* This is a baseclass.  */
2301           if (cname)
2302             *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2303
2304           if (cvalue && value)
2305             {
2306               *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2307             }
2308
2309           if (ctype)
2310             {
2311               *ctype = TYPE_FIELD_TYPE (type, index);
2312             }
2313         }
2314       else
2315         {
2316           char *access = 0;
2317           int children[3];
2318           cplus_class_num_children (type, children);
2319
2320           /* Everything beyond the baseclasses can
2321              only be "public", "private", or "protected"
2322
2323              The special "fake" children are always output by varobj in
2324              this order. So if INDEX == 2, it MUST be "protected". */
2325           index -= TYPE_N_BASECLASSES (type);
2326           switch (index)
2327             {
2328             case 0:
2329               if (children[v_public] > 0)
2330                 access = "public";
2331               else if (children[v_private] > 0)
2332                 access = "private";
2333               else 
2334                 access = "protected";
2335               break;
2336             case 1:
2337               if (children[v_public] > 0)
2338                 {
2339                   if (children[v_private] > 0)
2340                     access = "private";
2341                   else
2342                     access = "protected";
2343                 }
2344               else if (children[v_private] > 0)
2345                 access = "protected";
2346               break;
2347             case 2:
2348               /* Must be protected */
2349               access = "protected";
2350               break;
2351             default:
2352               /* error! */
2353               break;
2354             }
2355           
2356           if (cname)
2357             *cname = xstrdup (access);
2358
2359           /* Value and type are null here.  */
2360         }
2361     }
2362   else
2363     {
2364       c_describe_child (parent, index, cname, cvalue, ctype);
2365     }  
2366 }
2367
2368 static char *
2369 cplus_name_of_child (struct varobj *parent, int index)
2370 {
2371   char *name = NULL;
2372   cplus_describe_child (parent, index, &name, NULL, NULL);
2373   return name;
2374 }
2375
2376 static struct value *
2377 cplus_value_of_root (struct varobj **var_handle)
2378 {
2379   return c_value_of_root (var_handle);
2380 }
2381
2382 static struct value *
2383 cplus_value_of_child (struct varobj *parent, int index)
2384 {
2385   struct value *value = NULL;
2386   cplus_describe_child (parent, index, NULL, &value, NULL);
2387   return value;
2388 }
2389
2390 static struct type *
2391 cplus_type_of_child (struct varobj *parent, int index)
2392 {
2393   struct type *type = NULL;
2394   cplus_describe_child (parent, index, NULL, NULL, &type);
2395   return type;
2396 }
2397
2398 static int
2399 cplus_variable_editable (struct varobj *var)
2400 {
2401   if (CPLUS_FAKE_CHILD (var))
2402     return 0;
2403
2404   return c_variable_editable (var);
2405 }
2406
2407 static char *
2408 cplus_value_of_variable (struct varobj *var)
2409 {
2410
2411   /* If we have one of our special types, don't print out
2412      any value. */
2413   if (CPLUS_FAKE_CHILD (var))
2414     return xstrdup ("");
2415
2416   return c_value_of_variable (var);
2417 }
2418 \f
2419 /* Java */
2420
2421 static int
2422 java_number_of_children (struct varobj *var)
2423 {
2424   return cplus_number_of_children (var);
2425 }
2426
2427 static char *
2428 java_name_of_variable (struct varobj *parent)
2429 {
2430   char *p, *name;
2431
2432   name = cplus_name_of_variable (parent);
2433   /* If  the name has "-" in it, it is because we
2434      needed to escape periods in the name... */
2435   p = name;
2436
2437   while (*p != '\000')
2438     {
2439       if (*p == '-')
2440         *p = '.';
2441       p++;
2442     }
2443
2444   return name;
2445 }
2446
2447 static char *
2448 java_name_of_child (struct varobj *parent, int index)
2449 {
2450   char *name, *p;
2451
2452   name = cplus_name_of_child (parent, index);
2453   /* Escape any periods in the name... */
2454   p = name;
2455
2456   while (*p != '\000')
2457     {
2458       if (*p == '.')
2459         *p = '-';
2460       p++;
2461     }
2462
2463   return name;
2464 }
2465
2466 static struct value *
2467 java_value_of_root (struct varobj **var_handle)
2468 {
2469   return cplus_value_of_root (var_handle);
2470 }
2471
2472 static struct value *
2473 java_value_of_child (struct varobj *parent, int index)
2474 {
2475   return cplus_value_of_child (parent, index);
2476 }
2477
2478 static struct type *
2479 java_type_of_child (struct varobj *parent, int index)
2480 {
2481   return cplus_type_of_child (parent, index);
2482 }
2483
2484 static int
2485 java_variable_editable (struct varobj *var)
2486 {
2487   return cplus_variable_editable (var);
2488 }
2489
2490 static char *
2491 java_value_of_variable (struct varobj *var)
2492 {
2493   return cplus_value_of_variable (var);
2494 }
2495 \f
2496 extern void _initialize_varobj (void);
2497 void
2498 _initialize_varobj (void)
2499 {
2500   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2501
2502   varobj_table = xmalloc (sizeof_table);
2503   memset (varobj_table, 0, sizeof_table);
2504
2505   add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2506                             &varobjdebug, _("\
2507 Set varobj debugging."), _("\
2508 Show varobj debugging."), _("\
2509 When non-zero, varobj debugging is enabled."),
2510                             NULL,
2511                             show_varobjdebug,
2512                             &setlist, &showlist);
2513 }
2514
2515 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2516    are defined on globals.
2517    Invalidated varobjs will be always printed in_scope="invalid".  */
2518 void 
2519 varobj_invalidate (void)
2520 {
2521   struct varobj **all_rootvarobj;
2522   struct varobj **varp;
2523
2524   if (varobj_list (&all_rootvarobj) > 0)
2525   {
2526     varp = all_rootvarobj;
2527     while (*varp != NULL)
2528       {
2529         /* global var must be re-evaluated.  */     
2530         if ((*varp)->root->valid_block == NULL)
2531         {
2532           struct varobj *tmp_var;
2533
2534           /* Try to create a varobj with same expression.  If we succeed replace
2535              the old varobj, otherwise invalidate it.  */
2536           tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2537           if (tmp_var != NULL) 
2538             { 
2539               tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2540               varobj_delete (*varp, NULL, 0);
2541               install_variable (tmp_var);
2542             }
2543           else
2544               (*varp)->root->is_valid = 0;
2545         }
2546         else /* locals must be invalidated.  */
2547           (*varp)->root->is_valid = 0;
2548
2549         varp++;
2550       }
2551     xfree (all_rootvarobj);
2552   }
2553   return;
2554 }