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