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