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