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