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