2000-12-17 Elena Zannoni <ezannoni@kwikemart.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;
415 struct frame_info *old_fi = NULL;
416 struct block *block;
417 struct cleanup *old_chain;
418
419 /* Fill out a varobj structure for the (root) variable being constructed. */
420 var = new_root_variable ();
421 old_chain = make_cleanup_free_variable (var);
422
423 if (expression != NULL)
424 {
425 char *p;
426 enum varobj_languages lang;
427
428 /* Parse and evaluate the expression, filling in as much
429 of the variable's data as possible */
430
431 /* Allow creator to specify context of variable */
432 if ((type == USE_CURRENT_FRAME)
433 || (type == USE_SELECTED_FRAME))
434 fi = selected_frame;
435 else
436 fi = find_frame_addr_in_frame_chain (frame);
437
438 /* frame = -2 means always use selected frame */
439 if (type == USE_SELECTED_FRAME)
440 var->root->use_selected_frame = 1;
441
442 block = NULL;
443 if (fi != NULL)
444 block = get_frame_block (fi);
445
446 p = expression;
447 innermost_block = NULL;
448 /* Wrap the call to parse expression, so we can
449 return a sensible error. */
450 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
451 {
452 return NULL;
453 }
454
455 /* Don't allow variables to be created for types. */
456 if (var->root->exp->elts[0].opcode == OP_TYPE)
457 {
458 do_cleanups (old_chain);
459 fprintf_unfiltered (gdb_stderr,
460 "Attempt to use a type name as an expression.");
461 return NULL;
462 }
463
464 var->format = variable_default_display (var);
465 var->root->valid_block = innermost_block;
466 var->name = savestring (expression, strlen (expression));
467
468 /* When the frame is different from the current frame,
469 we must select the appropriate frame before parsing
470 the expression, otherwise the value will not be current.
471 Since select_frame is so benign, just call it for all cases. */
472 if (fi != NULL)
473 {
474 var->root->frame = FRAME_FP (fi);
475 old_fi = selected_frame;
476 select_frame (fi, -1);
477 }
478
479 /* We definitively need to catch errors here.
480 If evaluate_expression succeeds we got the value we wanted.
481 But if it fails, we still go on with a call to evaluate_type() */
482 if (gdb_evaluate_expression (var->root->exp, &var->value))
483 {
484 /* no error */
485 release_value (var->value);
486 if (VALUE_LAZY (var->value))
487 gdb_value_fetch_lazy (var->value);
488 }
489 else
490 var->value = evaluate_type (var->root->exp);
491
492 var->type = VALUE_TYPE (var->value);
493
494 /* Set language info */
495 lang = variable_language (var);
496 var->root->lang = languages[lang];
497
498 /* Set ourselves as our root */
499 var->root->rootvar = var;
500
501 /* Reset the selected frame */
502 if (fi != NULL)
503 select_frame (old_fi, -1);
504 }
505
506 /* If the variable object name is null, that means this
507 is a temporary variable, so don't install it. */
508
509 if ((var != NULL) && (objname != NULL))
510 {
511 var->obj_name = savestring (objname, strlen (objname));
512
513 /* If a varobj name is duplicated, the install will fail so
514 we must clenup */
515 if (!install_variable (var))
516 {
517 do_cleanups (old_chain);
518 return NULL;
519 }
520 }
521
522 discard_cleanups (old_chain);
523 return var;
524 }
525
526 /* Generates an unique name that can be used for a varobj */
527
528 char *
529 varobj_gen_name (void)
530 {
531 static int id = 0;
532 char obj_name[31];
533
534 /* generate a name for this object */
535 id++;
536 sprintf (obj_name, "var%d", id);
537
538 return xstrdup (obj_name);
539 }
540
541 /* Given an "objname", returns the pointer to the corresponding varobj
542 or NULL if not found */
543
544 struct varobj *
545 varobj_get_handle (char *objname)
546 {
547 struct vlist *cv;
548 const char *chp;
549 unsigned int index = 0;
550 unsigned int i = 1;
551
552 for (chp = objname; *chp; chp++)
553 {
554 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
555 }
556
557 cv = *(varobj_table + index);
558 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
559 cv = cv->next;
560
561 if (cv == NULL)
562 error ("Variable object not found");
563
564 return cv->var;
565 }
566
567 /* Given the handle, return the name of the object */
568
569 char *
570 varobj_get_objname (struct varobj *var)
571 {
572 return var->obj_name;
573 }
574
575 /* Given the handle, return the expression represented by the object */
576
577 char *
578 varobj_get_expression (struct varobj *var)
579 {
580 return name_of_variable (var);
581 }
582
583 /* Deletes a varobj and all its children if only_children == 0,
584 otherwise deletes only the children; returns a malloc'ed list of all the
585 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
586
587 int
588 varobj_delete (struct varobj *var, char ***dellist, int only_children)
589 {
590 int delcount;
591 int mycount;
592 struct cpstack *result = NULL;
593 char **cp;
594
595 /* Initialize a stack for temporary results */
596 cppush (&result, NULL);
597
598 if (only_children)
599 /* Delete only the variable children */
600 delcount = delete_variable (&result, var, 1 /* only the children */ );
601 else
602 /* Delete the variable and all its children */
603 delcount = delete_variable (&result, var, 0 /* parent+children */ );
604
605 /* We may have been asked to return a list of what has been deleted */
606 if (dellist != NULL)
607 {
608 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
609
610 cp = *dellist;
611 mycount = delcount;
612 *cp = cppop (&result);
613 while ((*cp != NULL) && (mycount > 0))
614 {
615 mycount--;
616 cp++;
617 *cp = cppop (&result);
618 }
619
620 if (mycount || (*cp != NULL))
621 warning ("varobj_delete: assertion failed - mycount(=%d) <> 0", mycount);
622 }
623
624 return delcount;
625 }
626
627 /* Set/Get variable object display format */
628
629 enum varobj_display_formats
630 varobj_set_display_format (struct varobj *var,
631 enum varobj_display_formats format)
632 {
633 switch (format)
634 {
635 case FORMAT_NATURAL:
636 case FORMAT_BINARY:
637 case FORMAT_DECIMAL:
638 case FORMAT_HEXADECIMAL:
639 case FORMAT_OCTAL:
640 var->format = format;
641 break;
642
643 default:
644 var->format = variable_default_display (var);
645 }
646
647 return var->format;
648 }
649
650 enum varobj_display_formats
651 varobj_get_display_format (struct varobj *var)
652 {
653 return var->format;
654 }
655
656 int
657 varobj_get_num_children (struct varobj *var)
658 {
659 if (var->num_children == -1)
660 var->num_children = number_of_children (var);
661
662 return var->num_children;
663 }
664
665 /* Creates a list of the immediate children of a variable object;
666 the return code is the number of such children or -1 on error */
667
668 int
669 varobj_list_children (struct varobj *var, struct varobj ***childlist)
670 {
671 struct varobj *child;
672 char *name;
673 int i;
674
675 /* sanity check: have we been passed a pointer? */
676 if (childlist == NULL)
677 return -1;
678
679 *childlist = NULL;
680
681 if (var->num_children == -1)
682 var->num_children = number_of_children (var);
683
684 /* List of children */
685 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
686
687 for (i = 0; i < var->num_children; i++)
688 {
689 /* Mark as the end in case we bail out */
690 *((*childlist) + i) = NULL;
691
692 /* check if child exists, if not create */
693 name = name_of_child (var, i);
694 child = child_exists (var, name);
695 if (child == NULL)
696 child = create_child (var, i, name);
697
698 *((*childlist) + i) = child;
699 }
700
701 /* End of list is marked by a NULL pointer */
702 *((*childlist) + i) = NULL;
703
704 return var->num_children;
705 }
706
707 /* Obtain the type of an object Variable as a string similar to the one gdb
708 prints on the console */
709
710 char *
711 varobj_get_type (struct varobj *var)
712 {
713 value_ptr val;
714 struct cleanup *old_chain;
715 struct ui_file *stb;
716 char *thetype;
717 long length;
718
719 /* For the "fake" variables, do not return a type. (It's type is
720 NULL, too.) */
721 if (CPLUS_FAKE_CHILD (var))
722 return NULL;
723
724 stb = mem_fileopen ();
725 old_chain = make_cleanup_ui_file_delete (stb);
726
727 /* To print the type, we simply create a zero value_ptr and
728 cast it to our type. We then typeprint this variable. */
729 val = value_zero (var->type, not_lval);
730 type_print (VALUE_TYPE (val), "", stb, -1);
731
732 thetype = ui_file_xstrdup (stb, &length);
733 do_cleanups (old_chain);
734 return thetype;
735 }
736
737 enum varobj_languages
738 varobj_get_language (struct varobj *var)
739 {
740 return variable_language (var);
741 }
742
743 int
744 varobj_get_attributes (struct varobj *var)
745 {
746 int attributes = 0;
747
748 if (variable_editable (var))
749 /* FIXME: define masks for attributes */
750 attributes |= 0x00000001; /* Editable */
751
752 return attributes;
753 }
754
755 char *
756 varobj_get_value (struct varobj *var)
757 {
758 return my_value_of_variable (var);
759 }
760
761 /* Set the value of an object variable (if it is editable) to the
762 value of the given expression */
763 /* Note: Invokes functions that can call error() */
764
765 int
766 varobj_set_value (struct varobj *var, char *expression)
767 {
768 value_ptr val;
769 int offset = 0;
770
771 /* The argument "expression" contains the variable's new value.
772 We need to first construct a legal expression for this -- ugh! */
773 /* Does this cover all the bases? */
774 struct expression *exp;
775 value_ptr value;
776 int saved_input_radix = input_radix;
777
778 if (variable_editable (var) && !var->error)
779 {
780 char *s = expression;
781 int i;
782 value_ptr temp;
783
784 input_radix = 10; /* ALWAYS reset to decimal temporarily */
785 if (!gdb_parse_exp_1 (&s, 0, 0, &exp))
786 /* We cannot proceed without a well-formed expression. */
787 return 0;
788 if (!gdb_evaluate_expression (exp, &value))
789 {
790 /* We cannot proceed without a valid expression. */
791 FREEIF (exp);
792 return 0;
793 }
794
795 /* If our parent is "public", "private", or "protected", we could
796 be asking to modify the value of a baseclass. If so, we need to
797 adjust our address by the offset of our baseclass in the subclass,
798 since VALUE_ADDRESS (var->value) points at the start of the subclass.
799 For some reason, value_cast doesn't take care of this properly. */
800 temp = var->value;
801 if (var->parent != NULL && CPLUS_FAKE_CHILD (var->parent))
802 {
803 struct varobj *super, *sub;
804 struct type *type;
805 super = var->parent->parent;
806 sub = super->parent;
807 if (sub != NULL)
808 {
809 /* Yes, it is a baseclass */
810 type = get_type_deref (sub);
811
812 if (super->index < TYPE_N_BASECLASSES (type))
813 {
814 temp = value_copy (var->value);
815 for (i = 0; i < super->index; i++)
816 offset += TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
817 }
818 }
819 }
820
821 VALUE_ADDRESS (temp) += offset;
822 if (!gdb_value_assign (temp, value, &val))
823 return 0;
824 VALUE_ADDRESS (val) -= offset;
825 value_free (var->value);
826 release_value (val);
827 var->value = val;
828 input_radix = saved_input_radix;
829 return 1;
830 }
831
832 return 0;
833 }
834
835 /* Returns a malloc'ed list with all root variable objects */
836 int
837 varobj_list (struct varobj ***varlist)
838 {
839 struct varobj **cv;
840 struct varobj_root *croot;
841 int mycount = rootcount;
842
843 /* Alloc (rootcount + 1) entries for the result */
844 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
845
846 cv = *varlist;
847 croot = rootlist;
848 while ((croot != NULL) && (mycount > 0))
849 {
850 *cv = croot->rootvar;
851 mycount--;
852 cv++;
853 croot = croot->next;
854 }
855 /* Mark the end of the list */
856 *cv = NULL;
857
858 if (mycount || (croot != NULL))
859 warning ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
860 rootcount, mycount);
861
862 return rootcount;
863 }
864
865 /* Update the values for a variable and its children. This is a
866 two-pronged attack. First, re-parse the value for the root's
867 expression to see if it's changed. Then go all the way
868 through its children, reconstructing them and noting if they've
869 changed.
870 Return value:
871 -1 if there was an error updating the varobj
872 -2 if the type changed
873 Otherwise it is the number of children + parent changed
874
875 Only root variables can be updated... */
876
877 int
878 varobj_update (struct varobj *var, struct varobj ***changelist)
879 {
880 int changed = 0;
881 int type_changed;
882 int i;
883 int vleft;
884 int error2;
885 struct varobj *v;
886 struct varobj **cv;
887 struct varobj **templist = NULL;
888 value_ptr new;
889 struct vstack *stack = NULL;
890 struct vstack *result = NULL;
891 struct frame_info *old_fi;
892
893 /* sanity check: have we been passed a pointer? */
894 if (changelist == NULL)
895 return -1;
896
897 /* Only root variables can be updated... */
898 if (var->root->rootvar != var)
899 /* Not a root var */
900 return -1;
901
902 /* Save the selected stack frame, since we will need to change it
903 in order to evaluate expressions. */
904 old_fi = selected_frame;
905
906 /* Update the root variable. value_of_root can return NULL
907 if the variable is no longer around, i.e. we stepped out of
908 the frame in which a local existed. We are letting the
909 value_of_root variable dispose of the varobj if the type
910 has changed. */
911 type_changed = 1;
912 new = value_of_root (&var, &type_changed);
913 if (new == NULL)
914 {
915 var->error = 1;
916 return -1;
917 }
918
919 /* Initialize a stack for temporary results */
920 vpush (&result, NULL);
921
922 /* If this is a "use_selected_frame" varobj, and its type has changed,
923 them note that it's changed. */
924 if (type_changed)
925 {
926 vpush (&result, var);
927 changed++;
928 }
929 /* If values are not equal, note that it's changed.
930 There a couple of exceptions here, though.
931 We don't want some types to be reported as "changed". */
932 else if (type_changeable (var) && !my_value_equal (var->value, new, &error2))
933 {
934 vpush (&result, var);
935 changed++;
936 /* error2 replaces var->error since this new value
937 WILL replace the old one. */
938 var->error = error2;
939 }
940
941 /* We must always keep around the new value for this root
942 variable expression, or we lose the updated children! */
943 value_free (var->value);
944 var->value = new;
945
946 /* Initialize a stack */
947 vpush (&stack, NULL);
948
949 /* Push the root's children */
950 if (var->children != NULL)
951 {
952 struct varobj_child *c;
953 for (c = var->children; c != NULL; c = c->next)
954 vpush (&stack, c->child);
955 }
956
957 /* Walk through the children, reconstructing them all. */
958 v = vpop (&stack);
959 while (v != NULL)
960 {
961 /* Push any children */
962 if (v->children != NULL)
963 {
964 struct varobj_child *c;
965 for (c = v->children; c != NULL; c = c->next)
966 vpush (&stack, c->child);
967 }
968
969 /* Update this variable */
970 new = value_of_child (v->parent, v->index);
971 if (type_changeable (v) && !my_value_equal (v->value, new, &error2))
972 {
973 /* Note that it's changed */
974 vpush (&result, v);
975 changed++;
976 }
977 /* error2 replaces v->error since this new value
978 WILL replace the old one. */
979 v->error = error2;
980
981 /* We must always keep new values, since children depend on it. */
982 if (v->value != NULL)
983 value_free (v->value);
984 v->value = new;
985
986 /* Get next child */
987 v = vpop (&stack);
988 }
989
990 /* Alloc (changed + 1) list entries */
991 /* FIXME: add a cleanup for the allocated list(s)
992 because one day the select_frame called below can longjump */
993 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
994 if (changed > 1)
995 {
996 templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
997 cv = templist;
998 }
999 else
1000 cv = *changelist;
1001
1002 /* Copy from result stack to list */
1003 vleft = changed;
1004 *cv = vpop (&result);
1005 while ((*cv != NULL) && (vleft > 0))
1006 {
1007 vleft--;
1008 cv++;
1009 *cv = vpop (&result);
1010 }
1011 if (vleft)
1012 warning ("varobj_update: assertion failed - vleft <> 0");
1013
1014 if (changed > 1)
1015 {
1016 /* Now we revert the order. */
1017 for (i=0; i < changed; i++)
1018 *(*changelist + i) = *(templist + changed -1 - i);
1019 *(*changelist + changed) = NULL;
1020 }
1021
1022 /* Restore selected frame */
1023 select_frame (old_fi, -1);
1024
1025 if (type_changed)
1026 return -2;
1027 else
1028 return changed;
1029 }
1030 \f
1031
1032 /* Helper functions */
1033
1034 /*
1035 * Variable object construction/destruction
1036 */
1037
1038 static int
1039 delete_variable (struct cpstack **resultp, struct varobj *var,
1040 int only_children_p)
1041 {
1042 int delcount = 0;
1043
1044 delete_variable_1 (resultp, &delcount, var,
1045 only_children_p, 1 /* remove_from_parent_p */ );
1046
1047 return delcount;
1048 }
1049
1050 /* Delete the variable object VAR and its children */
1051 /* IMPORTANT NOTE: If we delete a variable which is a child
1052 and the parent is not removed we dump core. It must be always
1053 initially called with remove_from_parent_p set */
1054 static void
1055 delete_variable_1 (struct cpstack **resultp, int *delcountp, struct varobj *var,
1056 int only_children_p, int remove_from_parent_p)
1057 {
1058 struct varobj_child *vc;
1059 struct varobj_child *next;
1060
1061 /* Delete any children of this variable, too. */
1062 for (vc = var->children; vc != NULL; vc = next)
1063 {
1064 if (!remove_from_parent_p)
1065 vc->child->parent = NULL;
1066 delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
1067 next = vc->next;
1068 xfree (vc);
1069 }
1070
1071 /* if we were called to delete only the children we are done here */
1072 if (only_children_p)
1073 return;
1074
1075 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1076 /* If the name is null, this is a temporary variable, that has not
1077 yet been installed, don't report it, it belongs to the caller... */
1078 if (var->obj_name != NULL)
1079 {
1080 cppush (resultp, strdup (var->obj_name));
1081 *delcountp = *delcountp + 1;
1082 }
1083
1084 /* If this variable has a parent, remove it from its parent's list */
1085 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1086 (as indicated by remove_from_parent_p) we don't bother doing an
1087 expensive list search to find the element to remove when we are
1088 discarding the list afterwards */
1089 if ((remove_from_parent_p) &&
1090 (var->parent != NULL))
1091 {
1092 remove_child_from_parent (var->parent, var);
1093 }
1094
1095 if (var->obj_name != NULL)
1096 uninstall_variable (var);
1097
1098 /* Free memory associated with this variable */
1099 free_variable (var);
1100 }
1101
1102 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1103 static int
1104 install_variable (struct varobj *var)
1105 {
1106 struct vlist *cv;
1107 struct vlist *newvl;
1108 const char *chp;
1109 unsigned int index = 0;
1110 unsigned int i = 1;
1111
1112 for (chp = var->obj_name; *chp; chp++)
1113 {
1114 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1115 }
1116
1117 cv = *(varobj_table + index);
1118 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1119 cv = cv->next;
1120
1121 if (cv != NULL)
1122 error ("Duplicate variable object name");
1123
1124 /* Add varobj to hash table */
1125 newvl = xmalloc (sizeof (struct vlist));
1126 newvl->next = *(varobj_table + index);
1127 newvl->var = var;
1128 *(varobj_table + index) = newvl;
1129
1130 /* If root, add varobj to root list */
1131 if (var->root->rootvar == var)
1132 {
1133 /* Add to list of root variables */
1134 if (rootlist == NULL)
1135 var->root->next = NULL;
1136 else
1137 var->root->next = rootlist;
1138 rootlist = var->root;
1139 rootcount++;
1140 }
1141
1142 return 1; /* OK */
1143 }
1144
1145 /* Unistall the object VAR. */
1146 static void
1147 uninstall_variable (struct varobj *var)
1148 {
1149 struct vlist *cv;
1150 struct vlist *prev;
1151 struct varobj_root *cr;
1152 struct varobj_root *prer;
1153 const char *chp;
1154 unsigned int index = 0;
1155 unsigned int i = 1;
1156
1157 /* Remove varobj from hash table */
1158 for (chp = var->obj_name; *chp; chp++)
1159 {
1160 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1161 }
1162
1163 cv = *(varobj_table + index);
1164 prev = NULL;
1165 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1166 {
1167 prev = cv;
1168 cv = cv->next;
1169 }
1170
1171 if (varobjdebug)
1172 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1173
1174 if (cv == NULL)
1175 {
1176 warning ("Assertion failed: Could not find variable object \"%s\" to delete", var->obj_name);
1177 return;
1178 }
1179
1180 if (prev == NULL)
1181 *(varobj_table + index) = cv->next;
1182 else
1183 prev->next = cv->next;
1184
1185 xfree (cv);
1186
1187 /* If root, remove varobj from root list */
1188 if (var->root->rootvar == var)
1189 {
1190 /* Remove from list of root variables */
1191 if (rootlist == var->root)
1192 rootlist = var->root->next;
1193 else
1194 {
1195 prer = NULL;
1196 cr = rootlist;
1197 while ((cr != NULL) && (cr->rootvar != var))
1198 {
1199 prer = cr;
1200 cr = cr->next;
1201 }
1202 if (cr == NULL)
1203 {
1204 warning ("Assertion failed: Could not find varobj \"%s\" in root list", var->obj_name);
1205 return;
1206 }
1207 if (prer == NULL)
1208 rootlist = NULL;
1209 else
1210 prer->next = cr->next;
1211 }
1212 rootcount--;
1213 }
1214
1215 }
1216
1217 /* Does a child with the name NAME exist in VAR? If so, return its data.
1218 If not, return NULL. */
1219 static struct varobj *
1220 child_exists (struct varobj *var, char *name)
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 xfree (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 xfree (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 case TYPE_CODE_ARRAY:
1730 r = 0;
1731 break;
1732
1733 default:
1734 r = 1;
1735 }
1736
1737 return r;
1738 }
1739
1740 /* C */
1741 static int
1742 c_number_of_children (struct varobj *var)
1743 {
1744 struct type *type;
1745 struct type *target;
1746 int children;
1747
1748 type = get_type (var);
1749 target = get_target_type (type);
1750 children = 0;
1751
1752 switch (TYPE_CODE (type))
1753 {
1754 case TYPE_CODE_ARRAY:
1755 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1756 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1757 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1758 else
1759 children = -1;
1760 break;
1761
1762 case TYPE_CODE_STRUCT:
1763 case TYPE_CODE_UNION:
1764 children = TYPE_NFIELDS (type);
1765 break;
1766
1767 case TYPE_CODE_PTR:
1768 /* This is where things get compilcated. All pointers have one child.
1769 Except, of course, for struct and union ptr, which we automagically
1770 dereference for the user and function ptrs, which have no children. */
1771 switch (TYPE_CODE (target))
1772 {
1773 case TYPE_CODE_STRUCT:
1774 case TYPE_CODE_UNION:
1775 children = TYPE_NFIELDS (target);
1776 break;
1777
1778 case TYPE_CODE_FUNC:
1779 children = 0;
1780 break;
1781
1782 default:
1783 /* Don't dereference char* or void*. */
1784 if (TYPE_NAME (target) != NULL
1785 && (STREQ (TYPE_NAME (target), "char")
1786 || STREQ (TYPE_NAME (target), "void")))
1787 children = 0;
1788 else
1789 children = 1;
1790 }
1791 break;
1792
1793 default:
1794 /* Other types have no children */
1795 break;
1796 }
1797
1798 return children;
1799 }
1800
1801 static char *
1802 c_name_of_variable (struct varobj *parent)
1803 {
1804 return savestring (parent->name, strlen (parent->name));
1805 }
1806
1807 static char *
1808 c_name_of_child (struct varobj *parent, int index)
1809 {
1810 struct type *type;
1811 struct type *target;
1812 char *name;
1813 char *string;
1814
1815 type = get_type (parent);
1816 target = get_target_type (type);
1817
1818 switch (TYPE_CODE (type))
1819 {
1820 case TYPE_CODE_ARRAY:
1821 {
1822 /* We never get here unless parent->num_children is greater than 0... */
1823 int len = 1;
1824 while ((int) pow ((double) 10, (double) len) < index)
1825 len++;
1826 name = (char *) xmalloc (1 + len * sizeof (char));
1827 sprintf (name, "%d", index);
1828 }
1829 break;
1830
1831 case TYPE_CODE_STRUCT:
1832 case TYPE_CODE_UNION:
1833 string = TYPE_FIELD_NAME (type, index);
1834 name = savestring (string, strlen (string));
1835 break;
1836
1837 case TYPE_CODE_PTR:
1838 switch (TYPE_CODE (target))
1839 {
1840 case TYPE_CODE_STRUCT:
1841 case TYPE_CODE_UNION:
1842 string = TYPE_FIELD_NAME (target, index);
1843 name = savestring (string, strlen (string));
1844 break;
1845
1846 default:
1847 name = (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char));
1848 sprintf (name, "*%s", parent->name);
1849 break;
1850 }
1851 break;
1852
1853 default:
1854 /* This should not happen */
1855 name = xstrdup ("???");
1856 }
1857
1858 return name;
1859 }
1860
1861 static value_ptr
1862 c_value_of_root (struct varobj **var_handle)
1863 {
1864 value_ptr new_val;
1865 struct varobj *var = *var_handle;
1866 struct frame_info *fi;
1867 int within_scope;
1868
1869 /* Only root variables can be updated... */
1870 if (var->root->rootvar != var)
1871 /* Not a root var */
1872 return NULL;
1873
1874
1875 /* Determine whether the variable is still around. */
1876 if (var->root->valid_block == NULL)
1877 within_scope = 1;
1878 else
1879 {
1880 reinit_frame_cache ();
1881
1882
1883 fi = find_frame_addr_in_frame_chain (var->root->frame);
1884
1885 within_scope = fi != NULL;
1886 /* FIXME: select_frame could fail */
1887 if (within_scope)
1888 select_frame (fi, -1);
1889 }
1890
1891 if (within_scope)
1892 {
1893 /* We need to catch errors here, because if evaluate
1894 expression fails we just want to make val->error = 1 and
1895 go on */
1896 if (gdb_evaluate_expression (var->root->exp, &new_val))
1897 {
1898 if (VALUE_LAZY (new_val))
1899 {
1900 /* We need to catch errors because if
1901 value_fetch_lazy fails we still want to continue
1902 (after making val->error = 1) */
1903 /* FIXME: Shouldn't be using VALUE_CONTENTS? The
1904 comment on value_fetch_lazy() says it is only
1905 called from the macro... */
1906 if (!gdb_value_fetch_lazy (new_val))
1907 var->error = 1;
1908 else
1909 var->error = 0;
1910 }
1911 }
1912 else
1913 var->error = 1;
1914
1915 release_value (new_val);
1916 return new_val;
1917 }
1918
1919 return NULL;
1920 }
1921
1922 static value_ptr
1923 c_value_of_child (struct varobj *parent, int index)
1924 {
1925 value_ptr value, temp, indval;
1926 struct type *type, *target;
1927 char *name;
1928
1929 type = get_type (parent);
1930 target = get_target_type (type);
1931 name = name_of_child (parent, index);
1932 temp = parent->value;
1933 value = NULL;
1934
1935 if (temp != NULL)
1936 {
1937 switch (TYPE_CODE (type))
1938 {
1939 case TYPE_CODE_ARRAY:
1940 #if 0
1941 /* This breaks if the array lives in a (vector) register. */
1942 value = value_slice (temp, index, 1);
1943 temp = value_coerce_array (value);
1944 gdb_value_ind (temp, &value);
1945 #else
1946 indval = value_from_longest (builtin_type_int, (LONGEST) index);
1947 gdb_value_subscript (temp, indval, &value);
1948 #endif
1949 break;
1950
1951 case TYPE_CODE_STRUCT:
1952 case TYPE_CODE_UNION:
1953 value = value_struct_elt (&temp, NULL, name, NULL, "vstructure");
1954 break;
1955
1956 case TYPE_CODE_PTR:
1957 switch (TYPE_CODE (target))
1958 {
1959 case TYPE_CODE_STRUCT:
1960 case TYPE_CODE_UNION:
1961 value = value_struct_elt (&temp, NULL, name, NULL, "vstructure");
1962 break;
1963
1964 default:
1965 gdb_value_ind (temp, &value);
1966 break;
1967 }
1968 break;
1969
1970 default:
1971 break;
1972 }
1973 }
1974
1975 if (value != NULL)
1976 release_value (value);
1977
1978 return value;
1979 }
1980
1981 static struct type *
1982 c_type_of_child (struct varobj *parent, int index)
1983 {
1984 struct type *type;
1985 char *name = name_of_child (parent, index);
1986
1987 switch (TYPE_CODE (parent->type))
1988 {
1989 case TYPE_CODE_ARRAY:
1990 type = TYPE_TARGET_TYPE (parent->type);
1991 break;
1992
1993 case TYPE_CODE_STRUCT:
1994 case TYPE_CODE_UNION:
1995 type = lookup_struct_elt_type (parent->type, name, 0);
1996 break;
1997
1998 case TYPE_CODE_PTR:
1999 switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type)))
2000 {
2001 case TYPE_CODE_STRUCT:
2002 case TYPE_CODE_UNION:
2003 type = lookup_struct_elt_type (parent->type, name, 0);
2004 break;
2005
2006 default:
2007 type = TYPE_TARGET_TYPE (parent->type);
2008 break;
2009 }
2010 break;
2011
2012 default:
2013 /* This should not happen as only the above types have children */
2014 warning ("Child of parent whose type does not allow children");
2015 /* FIXME: Can we still go on? */
2016 type = NULL;
2017 break;
2018 }
2019
2020 return type;
2021 }
2022
2023 static int
2024 c_variable_editable (struct varobj *var)
2025 {
2026 switch (TYPE_CODE (get_type (var)))
2027 {
2028 case TYPE_CODE_STRUCT:
2029 case TYPE_CODE_UNION:
2030 case TYPE_CODE_ARRAY:
2031 case TYPE_CODE_FUNC:
2032 case TYPE_CODE_MEMBER:
2033 case TYPE_CODE_METHOD:
2034 return 0;
2035 break;
2036
2037 default:
2038 return 1;
2039 break;
2040 }
2041 }
2042
2043 static char *
2044 c_value_of_variable (struct varobj *var)
2045 {
2046 struct type *type;
2047 value_ptr val;
2048
2049 if (var->value != NULL)
2050 val = var->value;
2051 else
2052 {
2053 /* This can happen if we attempt to get the value of a struct
2054 member when the parent is an invalid pointer. */
2055 return xstrdup ("???");
2056 }
2057
2058 /* BOGUS: if val_print sees a struct/class, it will print out its
2059 children instead of "{...}" */
2060 type = get_type (var);
2061 switch (TYPE_CODE (type))
2062 {
2063 case TYPE_CODE_STRUCT:
2064 case TYPE_CODE_UNION:
2065 return xstrdup ("{...}");
2066 /* break; */
2067
2068 case TYPE_CODE_ARRAY:
2069 {
2070 char number[18];
2071 sprintf (number, "[%d]", var->num_children);
2072 return xstrdup (number);
2073 }
2074 /* break; */
2075
2076 default:
2077 {
2078 long dummy;
2079 struct ui_file *stb = mem_fileopen ();
2080 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
2081 char *thevalue;
2082
2083 if (VALUE_LAZY (val))
2084 gdb_value_fetch_lazy (val);
2085 val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0,
2086 VALUE_ADDRESS (val),
2087 stb, format_code[(int) var->format], 1, 0, 0);
2088 thevalue = ui_file_xstrdup (stb, &dummy);
2089 do_cleanups (old_chain);
2090 return thevalue;
2091 }
2092 /* break; */
2093 }
2094 }
2095 \f
2096
2097 /* C++ */
2098
2099 static int
2100 cplus_number_of_children (struct varobj *var)
2101 {
2102 struct type *type;
2103 int children, dont_know;
2104
2105 dont_know = 1;
2106 children = 0;
2107
2108 if (!CPLUS_FAKE_CHILD (var))
2109 {
2110 type = get_type_deref (var);
2111
2112 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2113 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2114 {
2115 int kids[3];
2116
2117 cplus_class_num_children (type, kids);
2118 if (kids[v_public] != 0)
2119 children++;
2120 if (kids[v_private] != 0)
2121 children++;
2122 if (kids[v_protected] != 0)
2123 children++;
2124
2125 /* Add any baseclasses */
2126 children += TYPE_N_BASECLASSES (type);
2127 dont_know = 0;
2128
2129 /* FIXME: save children in var */
2130 }
2131 }
2132 else
2133 {
2134 int kids[3];
2135
2136 type = get_type_deref (var->parent);
2137
2138 cplus_class_num_children (type, kids);
2139 if (STREQ (var->name, "public"))
2140 children = kids[v_public];
2141 else if (STREQ (var->name, "private"))
2142 children = kids[v_private];
2143 else
2144 children = kids[v_protected];
2145 dont_know = 0;
2146 }
2147
2148 if (dont_know)
2149 children = c_number_of_children (var);
2150
2151 return children;
2152 }
2153
2154 /* Compute # of public, private, and protected variables in this class.
2155 That means we need to descend into all baseclasses and find out
2156 how many are there, too. */
2157 static void
2158 cplus_class_num_children (struct type *type, int children[3])
2159 {
2160 int i;
2161
2162 children[v_public] = 0;
2163 children[v_private] = 0;
2164 children[v_protected] = 0;
2165
2166 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2167 {
2168 /* If we have a virtual table pointer, omit it. */
2169 if (TYPE_VPTR_BASETYPE (type) == type
2170 && TYPE_VPTR_FIELDNO (type) == i)
2171 continue;
2172
2173 if (TYPE_FIELD_PROTECTED (type, i))
2174 children[v_protected]++;
2175 else if (TYPE_FIELD_PRIVATE (type, i))
2176 children[v_private]++;
2177 else
2178 children[v_public]++;
2179 }
2180 }
2181
2182 static char *
2183 cplus_name_of_variable (struct varobj *parent)
2184 {
2185 return c_name_of_variable (parent);
2186 }
2187
2188 static char *
2189 cplus_name_of_child (struct varobj *parent, int index)
2190 {
2191 char *name;
2192 struct type *type;
2193 int children[3];
2194
2195 if (CPLUS_FAKE_CHILD (parent))
2196 {
2197 /* Looking for children of public, private, or protected. */
2198 type = get_type_deref (parent->parent);
2199 }
2200 else
2201 type = get_type_deref (parent);
2202
2203 name = NULL;
2204 switch (TYPE_CODE (type))
2205 {
2206 case TYPE_CODE_STRUCT:
2207 case TYPE_CODE_UNION:
2208 cplus_class_num_children (type, children);
2209
2210 if (CPLUS_FAKE_CHILD (parent))
2211 {
2212 /* FIXME: This assumes that type orders
2213 inherited, public, private, protected */
2214 int i = index + TYPE_N_BASECLASSES (type);
2215 if (STREQ (parent->name, "private") || STREQ (parent->name, "protected"))
2216 i += children[v_public];
2217 if (STREQ (parent->name, "protected"))
2218 i += children[v_private];
2219
2220 name = TYPE_FIELD_NAME (type, i);
2221 }
2222 else if (index < TYPE_N_BASECLASSES (type))
2223 name = TYPE_FIELD_NAME (type, index);
2224 else
2225 {
2226 /* Everything beyond the baseclasses can
2227 only be "public", "private", or "protected" */
2228 index -= TYPE_N_BASECLASSES (type);
2229 switch (index)
2230 {
2231 case 0:
2232 if (children[v_public] != 0)
2233 {
2234 name = "public";
2235 break;
2236 }
2237 case 1:
2238 if (children[v_private] != 0)
2239 {
2240 name = "private";
2241 break;
2242 }
2243 case 2:
2244 if (children[v_protected] != 0)
2245 {
2246 name = "protected";
2247 break;
2248 }
2249 default:
2250 /* error! */
2251 break;
2252 }
2253 }
2254 break;
2255
2256 default:
2257 break;
2258 }
2259
2260 if (name == NULL)
2261 return c_name_of_child (parent, index);
2262 else
2263 {
2264 if (name != NULL)
2265 name = savestring (name, strlen (name));
2266 }
2267
2268 return name;
2269 }
2270
2271 static value_ptr
2272 cplus_value_of_root (struct varobj **var_handle)
2273 {
2274 return c_value_of_root (var_handle);
2275 }
2276
2277 static value_ptr
2278 cplus_value_of_child (struct varobj *parent, int index)
2279 {
2280 struct type *type;
2281 value_ptr value;
2282 char *name;
2283
2284 if (CPLUS_FAKE_CHILD (parent))
2285 type = get_type_deref (parent->parent);
2286 else
2287 type = get_type_deref (parent);
2288
2289 value = NULL;
2290 name = name_of_child (parent, index);
2291
2292 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2293 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2294 {
2295 if (CPLUS_FAKE_CHILD (parent))
2296 {
2297 value_ptr temp = parent->parent->value;
2298 value = value_struct_elt (&temp, NULL, name,
2299 NULL, "cplus_structure");
2300 release_value (value);
2301 }
2302 else if (index >= TYPE_N_BASECLASSES (type))
2303 {
2304 /* public, private, or protected */
2305 return NULL;
2306 }
2307 else
2308 {
2309 /* Baseclass */
2310 if (parent->value != NULL)
2311 {
2312 value_ptr temp;
2313
2314 if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
2315 || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
2316 gdb_value_ind (parent->value, &temp);
2317 else
2318 temp = parent->value;
2319
2320 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
2321 release_value (value);
2322 }
2323 }
2324 }
2325
2326 if (value == NULL)
2327 return c_value_of_child (parent, index);
2328
2329 return value;
2330 }
2331
2332 static struct type *
2333 cplus_type_of_child (struct varobj *parent, int index)
2334 {
2335 struct type *type, *t;
2336
2337 t = get_type_deref (parent);
2338 type = NULL;
2339 switch (TYPE_CODE (t))
2340 {
2341 case TYPE_CODE_STRUCT:
2342 case TYPE_CODE_UNION:
2343 if (index >= TYPE_N_BASECLASSES (t))
2344 {
2345 /* special */
2346 return NULL;
2347 }
2348 else
2349 {
2350 /* Baseclass */
2351 type = TYPE_FIELD_TYPE (t, index);
2352 }
2353 break;
2354
2355 default:
2356 break;
2357 }
2358
2359 if (type == NULL)
2360 return c_type_of_child (parent, index);
2361
2362 return type;
2363 }
2364
2365 static int
2366 cplus_variable_editable (struct varobj *var)
2367 {
2368 if (CPLUS_FAKE_CHILD (var))
2369 return 0;
2370
2371 return c_variable_editable (var);
2372 }
2373
2374 static char *
2375 cplus_value_of_variable (struct varobj *var)
2376 {
2377
2378 /* If we have one of our special types, don't print out
2379 any value. */
2380 if (CPLUS_FAKE_CHILD (var))
2381 return xstrdup ("");
2382
2383 return c_value_of_variable (var);
2384 }
2385 \f
2386 /* Java */
2387
2388 static int
2389 java_number_of_children (struct varobj *var)
2390 {
2391 return cplus_number_of_children (var);
2392 }
2393
2394 static char *
2395 java_name_of_variable (struct varobj *parent)
2396 {
2397 char *p, *name;
2398
2399 name = cplus_name_of_variable (parent);
2400 /* If the name has "-" in it, it is because we
2401 needed to escape periods in the name... */
2402 p = name;
2403
2404 while (*p != '\000')
2405 {
2406 if (*p == '-')
2407 *p = '.';
2408 p++;
2409 }
2410
2411 return name;
2412 }
2413
2414 static char *
2415 java_name_of_child (struct varobj *parent, int index)
2416 {
2417 char *name, *p;
2418
2419 name = cplus_name_of_child (parent, index);
2420 /* Escape any periods in the name... */
2421 p = name;
2422
2423 while (*p != '\000')
2424 {
2425 if (*p == '.')
2426 *p = '-';
2427 p++;
2428 }
2429
2430 return name;
2431 }
2432
2433 static value_ptr
2434 java_value_of_root (struct varobj **var_handle)
2435 {
2436 return cplus_value_of_root (var_handle);
2437 }
2438
2439 static value_ptr
2440 java_value_of_child (struct varobj *parent, int index)
2441 {
2442 return cplus_value_of_child (parent, index);
2443 }
2444
2445 static struct type *
2446 java_type_of_child (struct varobj *parent, int index)
2447 {
2448 return cplus_type_of_child (parent, index);
2449 }
2450
2451 static int
2452 java_variable_editable (struct varobj *var)
2453 {
2454 return cplus_variable_editable (var);
2455 }
2456
2457 static char *
2458 java_value_of_variable (struct varobj *var)
2459 {
2460 return cplus_value_of_variable (var);
2461 }
2462 \f
2463 extern void _initialize_varobj (void);
2464 void
2465 _initialize_varobj (void)
2466 {
2467 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2468
2469 varobj_table = xmalloc (sizeof_table);
2470 memset (varobj_table, 0, sizeof_table);
2471
2472 add_show_from_set (
2473 add_set_cmd ("debugvarobj", class_maintenance, var_zinteger,
2474 (char *) &varobjdebug,
2475 "Set varobj debugging.\n\
2476 When non-zero, varobj debugging is enabled.", &setlist),
2477 &showlist);
2478 }
This page took 0.081054 seconds and 4 git commands to generate.