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