OSDN Git Service

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