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