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