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