1ebf21c17e72419e9fba6a9f342c79ab4db8ff05
[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 if (!gdb_value_assign (temp, value, &val))
822 return 0;
823 VALUE_ADDRESS (val) -= offset;
824 value_free (var->value);
825 release_value (val);
826 var->value = val;
827 input_radix = saved_input_radix;
828 return 1;
829 }
830
831 return 0;
832 }
833
834 /* Returns a malloc'ed list with all root variable objects */
835 int
836 varobj_list (struct varobj ***varlist)
837 {
838 struct varobj **cv;
839 struct varobj_root *croot;
840 int mycount = rootcount;
841
842 /* Alloc (rootcount + 1) entries for the result */
843 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
844
845 cv = *varlist;
846 croot = rootlist;
847 while ((croot != NULL) && (mycount > 0))
848 {
849 *cv = croot->rootvar;
850 mycount--;
851 cv++;
852 croot = croot->next;
853 }
854 /* Mark the end of the list */
855 *cv = NULL;
856
857 if (mycount || (croot != NULL))
858 warning ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
859 rootcount, mycount);
860
861 return rootcount;
862 }
863
864 /* Update the values for a variable and its children. This is a
865 two-pronged attack. First, re-parse the value for the root's
866 expression to see if it's changed. Then go all the way
867 through its children, reconstructing them and noting if they've
868 changed.
869 Return value:
870 -1 if there was an error updating the varobj
871 -2 if the type changed
872 Otherwise it is the number of children + parent changed
873
874 Only root variables can be updated... */
875
876 int
877 varobj_update (struct varobj *var, struct varobj ***changelist)
878 {
879 int changed = 0;
880 int type_changed;
881 int i;
882 int vleft;
883 int error2;
884 struct varobj *v;
885 struct varobj **cv;
886 struct varobj **templist;
887 value_ptr new;
888 struct vstack *stack = NULL;
889 struct vstack *result = NULL;
890 struct frame_info *old_fi;
891
892 /* sanity check: have we been passed a pointer? */
893 if (changelist == NULL)
894 return -1;
895
896 /* Only root variables can be updated... */
897 if (var->root->rootvar != var)
898 /* Not a root var */
899 return -1;
900
901 /* Save the selected stack frame, since we will need to change it
902 in order to evaluate expressions. */
903 old_fi = selected_frame;
904
905 /* Update the root variable. value_of_root can return NULL
906 if the variable is no longer around, i.e. we stepped out of
907 the frame in which a local existed. We are letting the
908 value_of_root variable dispose of the varobj if the type
909 has changed. */
910 type_changed = 1;
911 new = value_of_root (&var, &type_changed);
912 if (new == NULL)
913 {
914 var->error = 1;
915 return -1;
916 }
917
918 /* Initialize a stack for temporary results */
919 vpush (&result, NULL);
920
921 /* If this is a "use_selected_frame" varobj, and its type has changed,
922 them note that it's changed. */
923 if (type_changed)
924 {
925 vpush (&result, var);
926 changed++;
927 }
928 /* If values are not equal, note that it's changed.
929 There a couple of exceptions here, though.
930 We don't want some types to be reported as "changed". */
931 else if (type_changeable (var) && !my_value_equal (var->value, new, &error2))
932 {
933 vpush (&result, var);
934 changed++;
935 /* error2 replaces var->error since this new value
936 WILL replace the old one. */
937 var->error = error2;
938 }
939
940 /* We must always keep around the new value for this root
941 variable expression, or we lose the updated children! */
942 value_free (var->value);
943 var->value = new;
944
945 /* Initialize a stack */
946 vpush (&stack, NULL);
947
948 /* Push the root's children */
949 if (var->children != NULL)
950 {
951 struct varobj_child *c;
952 for (c = var->children; c != NULL; c = c->next)
953 vpush (&stack, c->child);
954 }
955
956 /* Walk through the children, reconstructing them all. */
957 v = vpop (&stack);
958 while (v != NULL)
959 {
960 /* Push any children */
961 if (v->children != NULL)
962 {
963 struct varobj_child *c;
964 for (c = v->children; c != NULL; c = c->next)
965 vpush (&stack, c->child);
966 }
967
968 /* Update this variable */
969 new = value_of_child (v->parent, v->index);
970 if (type_changeable (v) && !my_value_equal (v->value, new, &error2))
971 {
972 /* Note that it's changed */
973 vpush (&result, v);
974 changed++;
975 }
976 /* error2 replaces v->error since this new value
977 WILL replace the old one. */
978 v->error = error2;
979
980 /* We must always keep new values, since children depend on it. */
981 if (v->value != NULL)
982 value_free (v->value);
983 v->value = new;
984
985 /* Get next child */
986 v = vpop (&stack);
987 }
988
989 /* Alloc (changed + 1) list entries */
990 /* FIXME: add a cleanup for the allocated list(s)
991 because one day the select_frame called below can longjump */
992 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
993 if (changed > 1)
994 {
995 templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
996 cv = templist;
997 }
998 else
999 cv = *changelist;
1000
1001 /* Copy from result stack to list */
1002 vleft = changed;
1003 *cv = vpop (&result);
1004 while ((*cv != NULL) && (vleft > 0))
1005 {
1006 vleft--;
1007 cv++;
1008 *cv = vpop (&result);
1009 }
1010 if (vleft)
1011 warning ("varobj_update: assertion failed - vleft <> 0");
1012
1013 if (changed > 1)
1014 {
1015 /* Now we revert the order. */
1016 for (i=0; i < changed; i++)
1017 *(*changelist + i) = *(templist + changed -1 - i);
1018 *(*changelist + changed) = NULL;
1019 }
1020
1021 /* Restore selected frame */
1022 select_frame (old_fi, -1);
1023
1024 if (type_changed)
1025 return -2;
1026 else
1027 return changed;
1028 }
1029 \f
1030
1031 /* Helper functions */
1032
1033 /*
1034 * Variable object construction/destruction
1035 */
1036
1037 static int
1038 delete_variable (struct cpstack **resultp, struct varobj *var,
1039 int only_children_p)
1040 {
1041 int delcount = 0;
1042
1043 delete_variable_1 (resultp, &delcount, var,
1044 only_children_p, 1 /* remove_from_parent_p */ );
1045
1046 return delcount;
1047 }
1048
1049 /* Delete the variable object VAR and its children */
1050 /* IMPORTANT NOTE: If we delete a variable which is a child
1051 and the parent is not removed we dump core. It must be always
1052 initially called with remove_from_parent_p set */
1053 static void
1054 delete_variable_1 (struct cpstack **resultp, int *delcountp, struct varobj *var,
1055 int only_children_p, int remove_from_parent_p)
1056 {
1057 struct varobj_child *vc;
1058 struct varobj_child *next;
1059
1060 /* Delete any children of this variable, too. */
1061 for (vc = var->children; vc != NULL; vc = next)
1062 {
1063 if (!remove_from_parent_p)
1064 vc->child->parent = NULL;
1065 delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
1066 next = vc->next;
1067 free (vc);
1068 }
1069
1070 /* if we were called to delete only the children we are done here */
1071 if (only_children_p)
1072 return;
1073
1074 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1075 /* If the name is null, this is a temporary variable, that has not
1076 yet been installed, don't report it, it belongs to the caller... */
1077 if (var->obj_name != NULL)
1078 {
1079 cppush (resultp, strdup (var->obj_name));
1080 *delcountp = *delcountp + 1;
1081 }
1082
1083 /* If this variable has a parent, remove it from its parent's list */
1084 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1085 (as indicated by remove_from_parent_p) we don't bother doing an
1086 expensive list search to find the element to remove when we are
1087 discarding the list afterwards */
1088 if ((remove_from_parent_p) &&
1089 (var->parent != NULL))
1090 {
1091 remove_child_from_parent (var->parent, var);
1092 }
1093
1094 if (var->obj_name != NULL)
1095 uninstall_variable (var);
1096
1097 /* Free memory associated with this variable */
1098 free_variable (var);
1099 }
1100
1101 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1102 static int
1103 install_variable (struct varobj *var)
1104 {
1105 struct vlist *cv;
1106 struct vlist *newvl;
1107 const char *chp;
1108 unsigned int index = 0;
1109 unsigned int i = 1;
1110
1111 for (chp = var->obj_name; *chp; chp++)
1112 {
1113 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1114 }
1115
1116 cv = *(varobj_table + index);
1117 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1118 cv = cv->next;
1119
1120 if (cv != NULL)
1121 error ("Duplicate variable object name");
1122
1123 /* Add varobj to hash table */
1124 newvl = xmalloc (sizeof (struct vlist));
1125 newvl->next = *(varobj_table + index);
1126 newvl->var = var;
1127 *(varobj_table + index) = newvl;
1128
1129 /* If root, add varobj to root list */
1130 if (var->root->rootvar == var)
1131 {
1132 /* Add to list of root variables */
1133 if (rootlist == NULL)
1134 var->root->next = NULL;
1135 else
1136 var->root->next = rootlist;
1137 rootlist = var->root;
1138 rootcount++;
1139 }
1140
1141 return 1; /* OK */
1142 }
1143
1144 /* Unistall the object VAR. */
1145 static void
1146 uninstall_variable (struct varobj *var)
1147 {
1148 struct vlist *cv;
1149 struct vlist *prev;
1150 struct varobj_root *cr;
1151 struct varobj_root *prer;
1152 const char *chp;
1153 unsigned int index = 0;
1154 unsigned int i = 1;
1155
1156 /* Remove varobj from hash table */
1157 for (chp = var->obj_name; *chp; chp++)
1158 {
1159 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1160 }
1161
1162 cv = *(varobj_table + index);
1163 prev = NULL;
1164 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1165 {
1166 prev = cv;
1167 cv = cv->next;
1168 }
1169
1170 if (varobjdebug)
1171 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1172
1173 if (cv == NULL)
1174 {
1175 warning ("Assertion failed: Could not find variable object \"%s\" to delete", var->obj_name);
1176 return;
1177 }
1178
1179 if (prev == NULL)
1180 *(varobj_table + index) = cv->next;
1181 else
1182 prev->next = cv->next;
1183
1184 free (cv);
1185
1186 /* If root, remove varobj from root list */
1187 if (var->root->rootvar == var)
1188 {
1189 /* Remove from list of root variables */
1190 if (rootlist == var->root)
1191 rootlist = var->root->next;
1192 else
1193 {
1194 prer = NULL;
1195 cr = rootlist;
1196 while ((cr != NULL) && (cr->rootvar != var))
1197 {
1198 prer = cr;
1199 cr = cr->next;
1200 }
1201 if (cr == NULL)
1202 {
1203 warning ("Assertion failed: Could not find varobj \"%s\" in root list", var->obj_name);
1204 return;
1205 }
1206 if (prer == NULL)
1207 rootlist = NULL;
1208 else
1209 prer->next = cr->next;
1210 }
1211 rootcount--;
1212 }
1213
1214 }
1215
1216 /* Does a child with the name NAME exist in VAR? If so, return its data.
1217 If not, return NULL. */
1218 static struct varobj *
1219 child_exists (var, name)
1220 struct varobj *var; /* Parent */
1221 char *name; /* name of child */
1222 {
1223 struct varobj_child *vc;
1224
1225 for (vc = var->children; vc != NULL; vc = vc->next)
1226 {
1227 if (STREQ (vc->child->name, name))
1228 return vc->child;
1229 }
1230
1231 return NULL;
1232 }
1233
1234 /* Create and install a child of the parent of the given name */
1235 static struct varobj *
1236 create_child (struct varobj *parent, int index, char *name)
1237 {
1238 struct varobj *child;
1239 char *childs_name;
1240
1241 child = new_variable ();
1242
1243 /* name is allocated by name_of_child */
1244 child->name = name;
1245 child->index = index;
1246 child->value = value_of_child (parent, index);
1247 if (child->value == NULL || parent->error)
1248 child->error = 1;
1249 child->parent = parent;
1250 child->root = parent->root;
1251 childs_name = (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2)
1252 * sizeof (char));
1253 sprintf (childs_name, "%s.%s", parent->obj_name, name);
1254 child->obj_name = childs_name;
1255 install_variable (child);
1256
1257 /* Save a pointer to this child in the parent */
1258 save_child_in_parent (parent, child);
1259
1260 /* Note the type of this child */
1261 child->type = type_of_child (child);
1262
1263 return child;
1264 }
1265
1266 /* FIXME: This should be a generic add to list */
1267 /* Save CHILD in the PARENT's data. */
1268 static void
1269 save_child_in_parent (struct varobj *parent, struct varobj *child)
1270 {
1271 struct varobj_child *vc;
1272
1273 /* Insert the child at the top */
1274 vc = parent->children;
1275 parent->children =
1276 (struct varobj_child *) xmalloc (sizeof (struct varobj_child));
1277
1278 parent->children->next = vc;
1279 parent->children->child = child;
1280 }
1281
1282 /* FIXME: This should be a generic remove from list */
1283 /* Remove the CHILD from the PARENT's list of children. */
1284 static void
1285 remove_child_from_parent (struct varobj *parent, struct varobj *child)
1286 {
1287 struct varobj_child *vc, *prev;
1288
1289 /* Find the child in the parent's list */
1290 prev = NULL;
1291 for (vc = parent->children; vc != NULL;)
1292 {
1293 if (vc->child == child)
1294 break;
1295 prev = vc;
1296 vc = vc->next;
1297 }
1298
1299 if (prev == NULL)
1300 parent->children = vc->next;
1301 else
1302 prev->next = vc->next;
1303
1304 }
1305 \f
1306
1307 /*
1308 * Miscellaneous utility functions.
1309 */
1310
1311 /* Allocate memory and initialize a new variable */
1312 static struct varobj *
1313 new_variable (void)
1314 {
1315 struct varobj *var;
1316
1317 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1318 var->name = NULL;
1319 var->obj_name = NULL;
1320 var->index = -1;
1321 var->type = NULL;
1322 var->value = NULL;
1323 var->error = 0;
1324 var->num_children = -1;
1325 var->parent = NULL;
1326 var->children = NULL;
1327 var->format = 0;
1328 var->root = NULL;
1329
1330 return var;
1331 }
1332
1333 /* Allocate memory and initialize a new root variable */
1334 static struct varobj *
1335 new_root_variable (void)
1336 {
1337 struct varobj *var = new_variable ();
1338 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1339 var->root->lang = NULL;
1340 var->root->exp = NULL;
1341 var->root->valid_block = NULL;
1342 var->root->frame = (CORE_ADDR) -1;
1343 var->root->use_selected_frame = 0;
1344 var->root->rootvar = NULL;
1345
1346 return var;
1347 }
1348
1349 /* Free any allocated memory associated with VAR. */
1350 static void
1351 free_variable (struct varobj *var)
1352 {
1353 /* Free the expression if this is a root variable. */
1354 if (var->root->rootvar == var)
1355 {
1356 free_current_contents ((char **) &var->root->exp);
1357 FREEIF (var->root);
1358 }
1359
1360 FREEIF (var->name);
1361 FREEIF (var->obj_name);
1362 FREEIF (var);
1363 }
1364
1365 static void
1366 do_free_variable_cleanup (void *var)
1367 {
1368 free_variable (var);
1369 }
1370
1371 static struct cleanup *
1372 make_cleanup_free_variable (struct varobj *var)
1373 {
1374 return make_cleanup (do_free_variable_cleanup, var);
1375 }
1376
1377 /* This returns the type of the variable. This skips past typedefs
1378 and returns the real type of the variable. It also dereferences
1379 pointers and references. */
1380 static struct type *
1381 get_type (struct varobj *var)
1382 {
1383 struct type *type;
1384 type = var->type;
1385
1386 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1387 type = TYPE_TARGET_TYPE (type);
1388
1389 return type;
1390 }
1391
1392 /* This returns the type of the variable, dereferencing pointers, too. */
1393 static struct type *
1394 get_type_deref (struct varobj *var)
1395 {
1396 struct type *type;
1397
1398 type = get_type (var);
1399
1400 if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR
1401 || TYPE_CODE (type) == TYPE_CODE_REF))
1402 type = get_target_type (type);
1403
1404 return type;
1405 }
1406
1407 /* This returns the target type (or NULL) of TYPE, also skipping
1408 past typedefs, just like get_type (). */
1409 static struct type *
1410 get_target_type (struct type *type)
1411 {
1412 if (type != NULL)
1413 {
1414 type = TYPE_TARGET_TYPE (type);
1415 while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1416 type = TYPE_TARGET_TYPE (type);
1417 }
1418
1419 return type;
1420 }
1421
1422 /* What is the default display for this variable? We assume that
1423 everything is "natural". Any exceptions? */
1424 static enum varobj_display_formats
1425 variable_default_display (struct varobj *var)
1426 {
1427 return FORMAT_NATURAL;
1428 }
1429
1430 /* This function is similar to gdb's value_equal, except that this
1431 one is "safe" -- it NEVER longjmps. It determines if the VAR's
1432 value is the same as VAL2. */
1433 static int
1434 my_value_equal (value_ptr val1, value_ptr val2, int *error2)
1435 {
1436 int r, err1, err2;
1437
1438 *error2 = 0;
1439 /* Special case: NULL values. If both are null, say
1440 they're equal. */
1441 if (val1 == NULL && val2 == NULL)
1442 return 1;
1443 else if (val1 == NULL || val2 == NULL)
1444 return 0;
1445
1446 /* This is bogus, but unfortunately necessary. We must know
1447 exactly what caused an error -- reading val1 or val2 -- so
1448 that we can really determine if we think that something has changed. */
1449 err1 = 0;
1450 err2 = 0;
1451 /* We do need to catch errors here because the whole purpose
1452 is to test if value_equal() has errored */
1453 if (!gdb_value_equal (val1, val1, &r))
1454 err1 = 1;
1455
1456 if (!gdb_value_equal (val2, val2, &r))
1457 *error2 = err2 = 1;
1458
1459 if (err1 != err2)
1460 return 0;
1461
1462 if (!gdb_value_equal (val1, val2, &r))
1463 {
1464 /* An error occurred, this could have happened if
1465 either val1 or val2 errored. ERR1 and ERR2 tell
1466 us which of these it is. If both errored, then
1467 we assume nothing has changed. If one of them is
1468 valid, though, then something has changed. */
1469 if (err1 == err2)
1470 {
1471 /* both the old and new values caused errors, so
1472 we say the value did not change */
1473 /* This is indeterminate, though. Perhaps we should
1474 be safe and say, yes, it changed anyway?? */
1475 return 1;
1476 }
1477 else
1478 {
1479 return 0;
1480 }
1481 }
1482
1483 return r;
1484 }
1485
1486 /* FIXME: The following should be generic for any pointer */
1487 static void
1488 vpush (struct vstack **pstack, struct varobj *var)
1489 {
1490 struct vstack *s;
1491
1492 s = (struct vstack *) xmalloc (sizeof (struct vstack));
1493 s->var = var;
1494 s->next = *pstack;
1495 *pstack = s;
1496 }
1497
1498 /* FIXME: The following should be generic for any pointer */
1499 static struct varobj *
1500 vpop (struct vstack **pstack)
1501 {
1502 struct vstack *s;
1503 struct varobj *v;
1504
1505 if ((*pstack)->var == NULL && (*pstack)->next == NULL)
1506 return NULL;
1507
1508 s = *pstack;
1509 v = s->var;
1510 *pstack = (*pstack)->next;
1511 free (s);
1512
1513 return v;
1514 }
1515
1516 /* FIXME: The following should be generic for any pointer */
1517 static void
1518 cppush (struct cpstack **pstack, char *name)
1519 {
1520 struct cpstack *s;
1521
1522 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1523 s->name = name;
1524 s->next = *pstack;
1525 *pstack = s;
1526 }
1527
1528 /* FIXME: The following should be generic for any pointer */
1529 static char *
1530 cppop (struct cpstack **pstack)
1531 {
1532 struct cpstack *s;
1533 char *v;
1534
1535 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1536 return NULL;
1537
1538 s = *pstack;
1539 v = s->name;
1540 *pstack = (*pstack)->next;
1541 free (s);
1542
1543 return v;
1544 }
1545 \f
1546 /*
1547 * Language-dependencies
1548 */
1549
1550 /* Common entry points */
1551
1552 /* Get the language of variable VAR. */
1553 static enum varobj_languages
1554 variable_language (struct varobj *var)
1555 {
1556 enum varobj_languages lang;
1557
1558 switch (var->root->exp->language_defn->la_language)
1559 {
1560 default:
1561 case language_c:
1562 lang = vlang_c;
1563 break;
1564 case language_cplus:
1565 lang = vlang_cplus;
1566 break;
1567 case language_java:
1568 lang = vlang_java;
1569 break;
1570 }
1571
1572 return lang;
1573 }
1574
1575 /* Return the number of children for a given variable.
1576 The result of this function is defined by the language
1577 implementation. The number of children returned by this function
1578 is the number of children that the user will see in the variable
1579 display. */
1580 static int
1581 number_of_children (struct varobj *var)
1582 {
1583 return (*var->root->lang->number_of_children) (var);;
1584 }
1585
1586 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1587 static char *
1588 name_of_variable (struct varobj *var)
1589 {
1590 return (*var->root->lang->name_of_variable) (var);
1591 }
1592
1593 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1594 static char *
1595 name_of_child (struct varobj *var, int index)
1596 {
1597 return (*var->root->lang->name_of_child) (var, index);
1598 }
1599
1600 /* What is the value_ptr of the root variable VAR?
1601 TYPE_CHANGED controls what to do if the type of a
1602 use_selected_frame = 1 variable changes. On input,
1603 TYPE_CHANGED = 1 means discard the old varobj, and replace
1604 it with this one. TYPE_CHANGED = 0 means leave it around.
1605 NB: In both cases, var_handle will point to the new varobj,
1606 so if you use TYPE_CHANGED = 0, you will have to stash the
1607 old varobj pointer away somewhere before calling this.
1608 On return, TYPE_CHANGED will be 1 if the type has changed, and
1609 0 otherwise. */
1610 static value_ptr
1611 value_of_root (struct varobj **var_handle, int *type_changed)
1612 {
1613 struct varobj *var;
1614
1615 if (var_handle == NULL)
1616 return NULL;
1617
1618 var = *var_handle;
1619
1620 /* This should really be an exception, since this should
1621 only get called with a root variable. */
1622
1623 if (var->root->rootvar != var)
1624 return NULL;
1625
1626 if (var->root->use_selected_frame)
1627 {
1628 struct varobj *tmp_var;
1629 char *old_type, *new_type;
1630 old_type = varobj_get_type (var);
1631 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1632 USE_SELECTED_FRAME);
1633 if (tmp_var == NULL)
1634 {
1635 return NULL;
1636 }
1637 new_type = varobj_get_type (tmp_var);
1638 if (strcmp(old_type, new_type) == 0)
1639 {
1640 varobj_delete (tmp_var, NULL, 0);
1641 *type_changed = 0;
1642 }
1643 else
1644 {
1645 if (*type_changed)
1646 {
1647 tmp_var->obj_name =
1648 savestring (var->obj_name, strlen (var->obj_name));
1649 uninstall_variable (var);
1650 }
1651 else
1652 {
1653 tmp_var->obj_name = varobj_gen_name ();
1654 }
1655 install_variable (tmp_var);
1656 *var_handle = tmp_var;
1657 *type_changed = 1;
1658 }
1659 }
1660 else
1661 {
1662 *type_changed = 0;
1663 }
1664
1665 return (*var->root->lang->value_of_root) (var_handle);
1666 }
1667
1668 /* What is the value_ptr for the INDEX'th child of PARENT? */
1669 static value_ptr
1670 value_of_child (struct varobj *parent, int index)
1671 {
1672 value_ptr value;
1673
1674 value = (*parent->root->lang->value_of_child) (parent, index);
1675
1676 /* If we're being lazy, fetch the real value of the variable. */
1677 if (value != NULL && VALUE_LAZY (value))
1678 gdb_value_fetch_lazy (value);
1679
1680 return value;
1681 }
1682
1683 /* What is the type of VAR? */
1684 static struct type *
1685 type_of_child (struct varobj *var)
1686 {
1687
1688 /* If the child had no evaluation errors, var->value
1689 will be non-NULL and contain a valid type. */
1690 if (var->value != NULL)
1691 return VALUE_TYPE (var->value);
1692
1693 /* Otherwise, we must compute the type. */
1694 return (*var->root->lang->type_of_child) (var->parent, var->index);
1695 }
1696
1697 /* Is this variable editable? Use the variable's type to make
1698 this determination. */
1699 static int
1700 variable_editable (struct varobj *var)
1701 {
1702 return (*var->root->lang->variable_editable) (var);
1703 }
1704
1705 /* GDB already has a command called "value_of_variable". Sigh. */
1706 static char *
1707 my_value_of_variable (struct varobj *var)
1708 {
1709 return (*var->root->lang->value_of_variable) (var);
1710 }
1711
1712 /* Is VAR something that can change? Depending on language,
1713 some variable's values never change. For example,
1714 struct and unions never change values. */
1715 static int
1716 type_changeable (struct varobj *var)
1717 {
1718 int r;
1719 struct type *type;
1720
1721 if (CPLUS_FAKE_CHILD (var))
1722 return 0;
1723
1724 type = get_type (var);
1725
1726 switch (TYPE_CODE (type))
1727 {
1728 case TYPE_CODE_STRUCT:
1729 case TYPE_CODE_UNION:
1730 case TYPE_CODE_ARRAY:
1731 r = 0;
1732 break;
1733
1734 default:
1735 r = 1;
1736 }
1737
1738 return r;
1739 }
1740
1741 /* C */
1742 static int
1743 c_number_of_children (struct varobj *var)
1744 {
1745 struct type *type;
1746 struct type *target;
1747 int children;
1748
1749 type = get_type (var);
1750 target = get_target_type (type);
1751 children = 0;
1752
1753 switch (TYPE_CODE (type))
1754 {
1755 case TYPE_CODE_ARRAY:
1756 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1757 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1758 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1759 else
1760 children = -1;
1761 break;
1762
1763 case TYPE_CODE_STRUCT:
1764 case TYPE_CODE_UNION:
1765 children = TYPE_NFIELDS (type);
1766 break;
1767
1768 case TYPE_CODE_PTR:
1769 /* This is where things get compilcated. All pointers have one child.
1770 Except, of course, for struct and union ptr, which we automagically
1771 dereference for the user and function ptrs, which have no children. */
1772 switch (TYPE_CODE (target))
1773 {
1774 case TYPE_CODE_STRUCT:
1775 case TYPE_CODE_UNION:
1776 children = TYPE_NFIELDS (target);
1777 break;
1778
1779 case TYPE_CODE_FUNC:
1780 children = 0;
1781 break;
1782
1783 default:
1784 /* Don't dereference char* or void*. */
1785 if (TYPE_NAME (target) != NULL
1786 && (STREQ (TYPE_NAME (target), "char")
1787 || STREQ (TYPE_NAME (target), "void")))
1788 children = 0;
1789 else
1790 children = 1;
1791 }
1792 break;
1793
1794 default:
1795 /* Other types have no children */
1796 break;
1797 }
1798
1799 return children;
1800 }
1801
1802 static char *
1803 c_name_of_variable (struct varobj *parent)
1804 {
1805 return savestring (parent->name, strlen (parent->name));
1806 }
1807
1808 static char *
1809 c_name_of_child (struct varobj *parent, int index)
1810 {
1811 struct type *type;
1812 struct type *target;
1813 char *name;
1814 char *string;
1815
1816 type = get_type (parent);
1817 target = get_target_type (type);
1818
1819 switch (TYPE_CODE (type))
1820 {
1821 case TYPE_CODE_ARRAY:
1822 {
1823 /* We never get here unless parent->num_children is greater than 0... */
1824 int len = 1;
1825 while ((int) pow ((double) 10, (double) len) < index)
1826 len++;
1827 name = (char *) xmalloc (1 + len * sizeof (char));
1828 sprintf (name, "%d", index);
1829 }
1830 break;
1831
1832 case TYPE_CODE_STRUCT:
1833 case TYPE_CODE_UNION:
1834 string = TYPE_FIELD_NAME (type, index);
1835 name = savestring (string, strlen (string));
1836 break;
1837
1838 case TYPE_CODE_PTR:
1839 switch (TYPE_CODE (target))
1840 {
1841 case TYPE_CODE_STRUCT:
1842 case TYPE_CODE_UNION:
1843 string = TYPE_FIELD_NAME (target, index);
1844 name = savestring (string, strlen (string));
1845 break;
1846
1847 default:
1848 name = (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char));
1849 sprintf (name, "*%s", parent->name);
1850 break;
1851 }
1852 break;
1853
1854 default:
1855 /* This should not happen */
1856 name = xstrdup ("???");
1857 }
1858
1859 return name;
1860 }
1861
1862 static value_ptr
1863 c_value_of_root (struct varobj **var_handle)
1864 {
1865 value_ptr new_val;
1866 struct varobj *var = *var_handle;
1867 struct frame_info *fi;
1868 int within_scope;
1869
1870 /* Only root variables can be updated... */
1871 if (var->root->rootvar != var)
1872 /* Not a root var */
1873 return NULL;
1874
1875
1876 /* Determine whether the variable is still around. */
1877 if (var->root->valid_block == NULL)
1878 within_scope = 1;
1879 else
1880 {
1881 reinit_frame_cache ();
1882
1883
1884 fi = find_frame_addr_in_frame_chain (var->root->frame);
1885
1886 within_scope = fi != NULL;
1887 /* FIXME: select_frame could fail */
1888 if (within_scope)
1889 select_frame (fi, -1);
1890 }
1891
1892 if (within_scope)
1893 {
1894 /* We need to catch errors here, because if evaluate
1895 expression fails we just want to make val->error = 1 and
1896 go on */
1897 if (gdb_evaluate_expression (var->root->exp, &new_val))
1898 {
1899 if (VALUE_LAZY (new_val))
1900 {
1901 /* We need to catch errors because if
1902 value_fetch_lazy fails we still want to continue
1903 (after making val->error = 1) */
1904 /* FIXME: Shouldn't be using VALUE_CONTENTS? The
1905 comment on value_fetch_lazy() says it is only
1906 called from the macro... */
1907 if (!gdb_value_fetch_lazy (new_val))
1908 var->error = 1;
1909 else
1910 var->error = 0;
1911 }
1912 }
1913 else
1914 var->error = 1;
1915
1916 release_value (new_val);
1917 return new_val;
1918 }
1919
1920 return NULL;
1921 }
1922
1923 static value_ptr
1924 c_value_of_child (struct varobj *parent, int index)
1925 {
1926 value_ptr value, temp, indval;
1927 struct type *type, *target;
1928 char *name;
1929
1930 type = get_type (parent);
1931 target = get_target_type (type);
1932 name = name_of_child (parent, index);
1933 temp = parent->value;
1934 value = NULL;
1935
1936 if (temp != NULL)
1937 {
1938 switch (TYPE_CODE (type))
1939 {
1940 case TYPE_CODE_ARRAY:
1941 #if 0
1942 /* This breaks if the array lives in a (vector) register. */
1943 value = value_slice (temp, index, 1);
1944 temp = value_coerce_array (value);
1945 gdb_value_ind (temp, &value);
1946 #else
1947 indval = value_from_longest (builtin_type_int, (LONGEST) index);
1948 gdb_value_subscript (temp, indval, &value);
1949 #endif
1950 break;
1951
1952 case TYPE_CODE_STRUCT:
1953 case TYPE_CODE_UNION:
1954 value = value_struct_elt (&temp, NULL, name, NULL, "vstructure");
1955 break;
1956
1957 case TYPE_CODE_PTR:
1958 switch (TYPE_CODE (target))
1959 {
1960 case TYPE_CODE_STRUCT:
1961 case TYPE_CODE_UNION:
1962 value = value_struct_elt (&temp, NULL, name, NULL, "vstructure");
1963 break;
1964
1965 default:
1966 gdb_value_ind (temp, &value);
1967 break;
1968 }
1969 break;
1970
1971 default:
1972 break;
1973 }
1974 }
1975
1976 if (value != NULL)
1977 release_value (value);
1978
1979 return value;
1980 }
1981
1982 static struct type *
1983 c_type_of_child (struct varobj *parent, int index)
1984 {
1985 struct type *type;
1986 char *name = name_of_child (parent, index);
1987
1988 switch (TYPE_CODE (parent->type))
1989 {
1990 case TYPE_CODE_ARRAY:
1991 type = TYPE_TARGET_TYPE (parent->type);
1992 break;
1993
1994 case TYPE_CODE_STRUCT:
1995 case TYPE_CODE_UNION:
1996 type = lookup_struct_elt_type (parent->type, name, 0);
1997 break;
1998
1999 case TYPE_CODE_PTR:
2000 switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type)))
2001 {
2002 case TYPE_CODE_STRUCT:
2003 case TYPE_CODE_UNION:
2004 type = lookup_struct_elt_type (parent->type, name, 0);
2005 break;
2006
2007 default:
2008 type = TYPE_TARGET_TYPE (parent->type);
2009 break;
2010 }
2011 break;
2012
2013 default:
2014 /* This should not happen as only the above types have children */
2015 warning ("Child of parent whose type does not allow children");
2016 /* FIXME: Can we still go on? */
2017 type = NULL;
2018 break;
2019 }
2020
2021 return type;
2022 }
2023
2024 static int
2025 c_variable_editable (struct varobj *var)
2026 {
2027 switch (TYPE_CODE (get_type (var)))
2028 {
2029 case TYPE_CODE_STRUCT:
2030 case TYPE_CODE_UNION:
2031 case TYPE_CODE_ARRAY:
2032 case TYPE_CODE_FUNC:
2033 case TYPE_CODE_MEMBER:
2034 case TYPE_CODE_METHOD:
2035 return 0;
2036 break;
2037
2038 default:
2039 return 1;
2040 break;
2041 }
2042 }
2043
2044 static char *
2045 c_value_of_variable (struct varobj *var)
2046 {
2047 struct type *type;
2048 value_ptr val;
2049
2050 if (var->value != NULL)
2051 val = var->value;
2052 else
2053 {
2054 /* This can happen if we attempt to get the value of a struct
2055 member when the parent is an invalid pointer. */
2056 return xstrdup ("???");
2057 }
2058
2059 /* BOGUS: if val_print sees a struct/class, it will print out its
2060 children instead of "{...}" */
2061 type = get_type (var);
2062 switch (TYPE_CODE (type))
2063 {
2064 case TYPE_CODE_STRUCT:
2065 case TYPE_CODE_UNION:
2066 return xstrdup ("{...}");
2067 /* break; */
2068
2069 case TYPE_CODE_ARRAY:
2070 {
2071 char number[18];
2072 sprintf (number, "[%d]", var->num_children);
2073 return xstrdup (number);
2074 }
2075 /* break; */
2076
2077 default:
2078 {
2079 long dummy;
2080 struct ui_file *stb = mem_fileopen ();
2081 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
2082 char *thevalue;
2083
2084 if (VALUE_LAZY (val))
2085 gdb_value_fetch_lazy (val);
2086 val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0,
2087 VALUE_ADDRESS (val),
2088 stb, format_code[(int) var->format], 1, 0, 0);
2089 thevalue = ui_file_xstrdup (stb, &dummy);
2090 do_cleanups (old_chain);
2091 return thevalue;
2092 }
2093 /* break; */
2094 }
2095 }
2096 \f
2097
2098 /* C++ */
2099
2100 static int
2101 cplus_number_of_children (struct varobj *var)
2102 {
2103 struct type *type;
2104 int children, dont_know;
2105
2106 dont_know = 1;
2107 children = 0;
2108
2109 if (!CPLUS_FAKE_CHILD (var))
2110 {
2111 type = get_type_deref (var);
2112
2113 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2114 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2115 {
2116 int kids[3];
2117
2118 cplus_class_num_children (type, kids);
2119 if (kids[v_public] != 0)
2120 children++;
2121 if (kids[v_private] != 0)
2122 children++;
2123 if (kids[v_protected] != 0)
2124 children++;
2125
2126 /* Add any baseclasses */
2127 children += TYPE_N_BASECLASSES (type);
2128 dont_know = 0;
2129
2130 /* FIXME: save children in var */
2131 }
2132 }
2133 else
2134 {
2135 int kids[3];
2136
2137 type = get_type_deref (var->parent);
2138
2139 cplus_class_num_children (type, kids);
2140 if (STREQ (var->name, "public"))
2141 children = kids[v_public];
2142 else if (STREQ (var->name, "private"))
2143 children = kids[v_private];
2144 else
2145 children = kids[v_protected];
2146 dont_know = 0;
2147 }
2148
2149 if (dont_know)
2150 children = c_number_of_children (var);
2151
2152 return children;
2153 }
2154
2155 /* Compute # of public, private, and protected variables in this class.
2156 That means we need to descend into all baseclasses and find out
2157 how many are there, too. */
2158 static void
2159 cplus_class_num_children (type, children)
2160 struct type *type;
2161 int children[3];
2162 {
2163 int i;
2164
2165 children[v_public] = 0;
2166 children[v_private] = 0;
2167 children[v_protected] = 0;
2168
2169 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2170 {
2171 /* If we have a virtual table pointer, omit it. */
2172 if (TYPE_VPTR_BASETYPE (type) == type
2173 && TYPE_VPTR_FIELDNO (type) == i)
2174 continue;
2175
2176 if (TYPE_FIELD_PROTECTED (type, i))
2177 children[v_protected]++;
2178 else if (TYPE_FIELD_PRIVATE (type, i))
2179 children[v_private]++;
2180 else
2181 children[v_public]++;
2182 }
2183 }
2184
2185 static char *
2186 cplus_name_of_variable (struct varobj *parent)
2187 {
2188 return c_name_of_variable (parent);
2189 }
2190
2191 static char *
2192 cplus_name_of_child (struct varobj *parent, int index)
2193 {
2194 char *name;
2195 struct type *type;
2196 int children[3];
2197
2198 if (CPLUS_FAKE_CHILD (parent))
2199 {
2200 /* Looking for children of public, private, or protected. */
2201 type = get_type_deref (parent->parent);
2202 }
2203 else
2204 type = get_type_deref (parent);
2205
2206 name = NULL;
2207 switch (TYPE_CODE (type))
2208 {
2209 case TYPE_CODE_STRUCT:
2210 case TYPE_CODE_UNION:
2211 cplus_class_num_children (type, children);
2212
2213 if (CPLUS_FAKE_CHILD (parent))
2214 {
2215 /* FIXME: This assumes that type orders
2216 inherited, public, private, protected */
2217 int i = index + TYPE_N_BASECLASSES (type);
2218 if (STREQ (parent->name, "private") || STREQ (parent->name, "protected"))
2219 i += children[v_public];
2220 if (STREQ (parent->name, "protected"))
2221 i += children[v_private];
2222
2223 name = TYPE_FIELD_NAME (type, i);
2224 }
2225 else if (index < TYPE_N_BASECLASSES (type))
2226 name = TYPE_FIELD_NAME (type, index);
2227 else
2228 {
2229 /* Everything beyond the baseclasses can
2230 only be "public", "private", or "protected" */
2231 index -= TYPE_N_BASECLASSES (type);
2232 switch (index)
2233 {
2234 case 0:
2235 if (children[v_public] != 0)
2236 {
2237 name = "public";
2238 break;
2239 }
2240 case 1:
2241 if (children[v_private] != 0)
2242 {
2243 name = "private";
2244 break;
2245 }
2246 case 2:
2247 if (children[v_protected] != 0)
2248 {
2249 name = "protected";
2250 break;
2251 }
2252 default:
2253 /* error! */
2254 break;
2255 }
2256 }
2257 break;
2258
2259 default:
2260 break;
2261 }
2262
2263 if (name == NULL)
2264 return c_name_of_child (parent, index);
2265 else
2266 {
2267 if (name != NULL)
2268 name = savestring (name, strlen (name));
2269 }
2270
2271 return name;
2272 }
2273
2274 static value_ptr
2275 cplus_value_of_root (struct varobj **var_handle)
2276 {
2277 return c_value_of_root (var_handle);
2278 }
2279
2280 static value_ptr
2281 cplus_value_of_child (struct varobj *parent, int index)
2282 {
2283 struct type *type;
2284 value_ptr value;
2285 char *name;
2286
2287 if (CPLUS_FAKE_CHILD (parent))
2288 type = get_type_deref (parent->parent);
2289 else
2290 type = get_type_deref (parent);
2291
2292 value = NULL;
2293 name = name_of_child (parent, index);
2294
2295 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2296 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2297 {
2298 if (CPLUS_FAKE_CHILD (parent))
2299 {
2300 value_ptr temp = parent->parent->value;
2301 value = value_struct_elt (&temp, NULL, name,
2302 NULL, "cplus_structure");
2303 release_value (value);
2304 }
2305 else if (index >= TYPE_N_BASECLASSES (type))
2306 {
2307 /* public, private, or protected */
2308 return NULL;
2309 }
2310 else
2311 {
2312 /* Baseclass */
2313 if (parent->value != NULL)
2314 {
2315 value_ptr temp;
2316
2317 if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
2318 || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
2319 gdb_value_ind (parent->value, &temp);
2320 else
2321 temp = parent->value;
2322
2323 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
2324 release_value (value);
2325 }
2326 }
2327 }
2328
2329 if (value == NULL)
2330 return c_value_of_child (parent, index);
2331
2332 return value;
2333 }
2334
2335 static struct type *
2336 cplus_type_of_child (struct varobj *parent, int index)
2337 {
2338 struct type *type, *t;
2339
2340 t = get_type_deref (parent);
2341 type = NULL;
2342 switch (TYPE_CODE (t))
2343 {
2344 case TYPE_CODE_STRUCT:
2345 case TYPE_CODE_UNION:
2346 if (index >= TYPE_N_BASECLASSES (t))
2347 {
2348 /* special */
2349 return NULL;
2350 }
2351 else
2352 {
2353 /* Baseclass */
2354 type = TYPE_FIELD_TYPE (t, index);
2355 }
2356 break;
2357
2358 default:
2359 break;
2360 }
2361
2362 if (type == NULL)
2363 return c_type_of_child (parent, index);
2364
2365 return type;
2366 }
2367
2368 static int
2369 cplus_variable_editable (struct varobj *var)
2370 {
2371 if (CPLUS_FAKE_CHILD (var))
2372 return 0;
2373
2374 return c_variable_editable (var);
2375 }
2376
2377 static char *
2378 cplus_value_of_variable (struct varobj *var)
2379 {
2380
2381 /* If we have one of our special types, don't print out
2382 any value. */
2383 if (CPLUS_FAKE_CHILD (var))
2384 return xstrdup ("");
2385
2386 return c_value_of_variable (var);
2387 }
2388 \f
2389 /* Java */
2390
2391 static int
2392 java_number_of_children (struct varobj *var)
2393 {
2394 return cplus_number_of_children (var);
2395 }
2396
2397 static char *
2398 java_name_of_variable (struct varobj *parent)
2399 {
2400 char *p, *name;
2401
2402 name = cplus_name_of_variable (parent);
2403 /* If the name has "-" in it, it is because we
2404 needed to escape periods in the name... */
2405 p = name;
2406
2407 while (*p != '\000')
2408 {
2409 if (*p == '-')
2410 *p = '.';
2411 p++;
2412 }
2413
2414 return name;
2415 }
2416
2417 static char *
2418 java_name_of_child (struct varobj *parent, int index)
2419 {
2420 char *name, *p;
2421
2422 name = cplus_name_of_child (parent, index);
2423 /* Escape any periods in the name... */
2424 p = name;
2425
2426 while (*p != '\000')
2427 {
2428 if (*p == '.')
2429 *p = '-';
2430 p++;
2431 }
2432
2433 return name;
2434 }
2435
2436 static value_ptr
2437 java_value_of_root (struct varobj **var_handle)
2438 {
2439 return cplus_value_of_root (var_handle);
2440 }
2441
2442 static value_ptr
2443 java_value_of_child (struct varobj *parent, int index)
2444 {
2445 return cplus_value_of_child (parent, index);
2446 }
2447
2448 static struct type *
2449 java_type_of_child (struct varobj *parent, int index)
2450 {
2451 return cplus_type_of_child (parent, index);
2452 }
2453
2454 static int
2455 java_variable_editable (struct varobj *var)
2456 {
2457 return cplus_variable_editable (var);
2458 }
2459
2460 static char *
2461 java_value_of_variable (struct varobj *var)
2462 {
2463 return cplus_value_of_variable (var);
2464 }
2465 \f
2466 extern void _initialize_varobj (void);
2467 void
2468 _initialize_varobj (void)
2469 {
2470 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2471
2472 varobj_table = xmalloc (sizeof_table);
2473 memset (varobj_table, 0, sizeof_table);
2474
2475 add_show_from_set (
2476 add_set_cmd ("debugvarobj", class_maintenance, var_zinteger,
2477 (char *) &varobjdebug,
2478 "Set varobj debugging.\n\
2479 When non-zero, varobj debugging is enabled.", &setlist),
2480 &showlist);
2481 }
This page took 0.079483 seconds and 4 git commands to generate.