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