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