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