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