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