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