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