Varobj support for Ada.
[binutils-gdb.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3 Copyright (C) 1999-2012 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "defs.h"
19 #include "exceptions.h"
20 #include "value.h"
21 #include "expression.h"
22 #include "frame.h"
23 #include "language.h"
24 #include "gdbcmd.h"
25 #include "block.h"
26 #include "valprint.h"
27
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdb_regex.h"
31
32 #include "varobj.h"
33 #include "vec.h"
34 #include "gdbthread.h"
35 #include "inferior.h"
36 #include "ada-varobj.h"
37 #include "ada-lang.h"
38
39 #if HAVE_PYTHON
40 #include "python/python.h"
41 #include "python/python-internal.h"
42 #else
43 typedef int PyObject;
44 #endif
45
46 /* The names of varobjs representing anonymous structs or unions. */
47 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
49
50 /* Non-zero if we want to see trace of varobj level stuff. */
51
52 int varobjdebug = 0;
53 static void
54 show_varobjdebug (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
56 {
57 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
58 }
59
60 /* String representations of gdb's format codes. */
61 char *varobj_format_string[] =
62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
63
64 /* String representations of gdb's known languages. */
65 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
66
67 /* True if we want to allow Python-based pretty-printing. */
68 static int pretty_printing = 0;
69
70 void
71 varobj_enable_pretty_printing (void)
72 {
73 pretty_printing = 1;
74 }
75
76 /* Data structures */
77
78 /* Every root variable has one of these structures saved in its
79 varobj. Members which must be free'd are noted. */
80 struct varobj_root
81 {
82
83 /* Alloc'd expression for this parent. */
84 struct expression *exp;
85
86 /* Block for which this expression is valid. */
87 struct block *valid_block;
88
89 /* The frame for this expression. This field is set iff valid_block is
90 not NULL. */
91 struct frame_id frame;
92
93 /* The thread ID that this varobj_root belong to. This field
94 is only valid if valid_block is not NULL.
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
97 was created. */
98 int thread_id;
99
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
102 always updated in the specific scope/thread/frame. */
103 int floating;
104
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
107 int is_valid;
108
109 /* Language info for this variable and its children. */
110 struct language_specific *lang;
111
112 /* The varobj for this root node. */
113 struct varobj *rootvar;
114
115 /* Next root variable */
116 struct varobj_root *next;
117 };
118
119 /* Every variable in the system has a structure of this type defined
120 for it. This structure holds all information necessary to manipulate
121 a particular object variable. Members which must be freed are noted. */
122 struct varobj
123 {
124
125 /* Alloc'd name of the variable for this object. If this variable is a
126 child, then this name will be the child's source name.
127 (bar, not foo.bar). */
128 /* NOTE: This is the "expression". */
129 char *name;
130
131 /* Alloc'd expression for this child. Can be used to create a
132 root variable corresponding to this child. */
133 char *path_expr;
134
135 /* The alloc'd name for this variable's object. This is here for
136 convenience when constructing this object's children. */
137 char *obj_name;
138
139 /* Index of this variable in its parent or -1. */
140 int index;
141
142 /* The type of this variable. This can be NULL
143 for artifial variable objects -- currently, the "accessibility"
144 variable objects in C++. */
145 struct type *type;
146
147 /* The value of this expression or subexpression. A NULL value
148 indicates there was an error getting this value.
149 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
150 the value is either NULL, or not lazy. */
151 struct value *value;
152
153 /* The number of (immediate) children this variable has. */
154 int num_children;
155
156 /* If this object is a child, this points to its immediate parent. */
157 struct varobj *parent;
158
159 /* Children of this object. */
160 VEC (varobj_p) *children;
161
162 /* Whether the children of this varobj were requested. This field is
163 used to decide if dynamic varobj should recompute their children.
164 In the event that the frontend never asked for the children, we
165 can avoid that. */
166 int children_requested;
167
168 /* Description of the root variable. Points to root variable for
169 children. */
170 struct varobj_root *root;
171
172 /* The format of the output for this object. */
173 enum varobj_display_formats format;
174
175 /* Was this variable updated via a varobj_set_value operation. */
176 int updated;
177
178 /* Last print value. */
179 char *print_value;
180
181 /* Is this variable frozen. Frozen variables are never implicitly
182 updated by -var-update *
183 or -var-update <direct-or-indirect-parent>. */
184 int frozen;
185
186 /* Is the value of this variable intentionally not fetched? It is
187 not fetched if either the variable is frozen, or any parents is
188 frozen. */
189 int not_fetched;
190
191 /* Sub-range of children which the MI consumer has requested. If
192 FROM < 0 or TO < 0, means that all children have been
193 requested. */
194 int from;
195 int to;
196
197 /* The pretty-printer constructor. If NULL, then the default
198 pretty-printer will be looked up. If None, then no
199 pretty-printer will be installed. */
200 PyObject *constructor;
201
202 /* The pretty-printer that has been constructed. If NULL, then a
203 new printer object is needed, and one will be constructed. */
204 PyObject *pretty_printer;
205
206 /* The iterator returned by the printer's 'children' method, or NULL
207 if not available. */
208 PyObject *child_iter;
209
210 /* We request one extra item from the iterator, so that we can
211 report to the caller whether there are more items than we have
212 already reported. However, we don't want to install this value
213 when we read it, because that will mess up future updates. So,
214 we stash it here instead. */
215 PyObject *saved_item;
216 };
217
218 struct cpstack
219 {
220 char *name;
221 struct cpstack *next;
222 };
223
224 /* A list of varobjs */
225
226 struct vlist
227 {
228 struct varobj *var;
229 struct vlist *next;
230 };
231
232 /* Private function prototypes */
233
234 /* Helper functions for the above subcommands. */
235
236 static int delete_variable (struct cpstack **, struct varobj *, int);
237
238 static void delete_variable_1 (struct cpstack **, int *,
239 struct varobj *, int, int);
240
241 static int install_variable (struct varobj *);
242
243 static void uninstall_variable (struct varobj *);
244
245 static struct varobj *create_child (struct varobj *, int, char *);
246
247 static struct varobj *
248 create_child_with_value (struct varobj *parent, int index, const char *name,
249 struct value *value);
250
251 /* Utility routines */
252
253 static struct varobj *new_variable (void);
254
255 static struct varobj *new_root_variable (void);
256
257 static void free_variable (struct varobj *var);
258
259 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
260
261 static struct type *get_type (struct varobj *var);
262
263 static struct type *get_value_type (struct varobj *var);
264
265 static struct type *get_target_type (struct type *);
266
267 static enum varobj_display_formats variable_default_display (struct varobj *);
268
269 static void cppush (struct cpstack **pstack, char *name);
270
271 static char *cppop (struct cpstack **pstack);
272
273 static int install_new_value (struct varobj *var, struct value *value,
274 int initial);
275
276 /* Language-specific routines. */
277
278 static enum varobj_languages variable_language (struct varobj *var);
279
280 static int number_of_children (struct varobj *);
281
282 static char *name_of_variable (struct varobj *);
283
284 static char *name_of_child (struct varobj *, int);
285
286 static struct value *value_of_root (struct varobj **var_handle, int *);
287
288 static struct value *value_of_child (struct varobj *parent, int index);
289
290 static char *my_value_of_variable (struct varobj *var,
291 enum varobj_display_formats format);
292
293 static char *value_get_print_value (struct value *value,
294 enum varobj_display_formats format,
295 struct varobj *var);
296
297 static int varobj_value_is_changeable_p (struct varobj *var);
298
299 static int is_root_p (struct varobj *var);
300
301 #if HAVE_PYTHON
302
303 static struct varobj *varobj_add_child (struct varobj *var,
304 const char *name,
305 struct value *value);
306
307 #endif /* HAVE_PYTHON */
308
309 /* C implementation */
310
311 static int c_number_of_children (struct varobj *var);
312
313 static char *c_name_of_variable (struct varobj *parent);
314
315 static char *c_name_of_child (struct varobj *parent, int index);
316
317 static char *c_path_expr_of_child (struct varobj *child);
318
319 static struct value *c_value_of_root (struct varobj **var_handle);
320
321 static struct value *c_value_of_child (struct varobj *parent, int index);
322
323 static struct type *c_type_of_child (struct varobj *parent, int index);
324
325 static char *c_value_of_variable (struct varobj *var,
326 enum varobj_display_formats format);
327
328 /* C++ implementation */
329
330 static int cplus_number_of_children (struct varobj *var);
331
332 static void cplus_class_num_children (struct type *type, int children[3]);
333
334 static char *cplus_name_of_variable (struct varobj *parent);
335
336 static char *cplus_name_of_child (struct varobj *parent, int index);
337
338 static char *cplus_path_expr_of_child (struct varobj *child);
339
340 static struct value *cplus_value_of_root (struct varobj **var_handle);
341
342 static struct value *cplus_value_of_child (struct varobj *parent, int index);
343
344 static struct type *cplus_type_of_child (struct varobj *parent, int index);
345
346 static char *cplus_value_of_variable (struct varobj *var,
347 enum varobj_display_formats format);
348
349 /* Java implementation */
350
351 static int java_number_of_children (struct varobj *var);
352
353 static char *java_name_of_variable (struct varobj *parent);
354
355 static char *java_name_of_child (struct varobj *parent, int index);
356
357 static char *java_path_expr_of_child (struct varobj *child);
358
359 static struct value *java_value_of_root (struct varobj **var_handle);
360
361 static struct value *java_value_of_child (struct varobj *parent, int index);
362
363 static struct type *java_type_of_child (struct varobj *parent, int index);
364
365 static char *java_value_of_variable (struct varobj *var,
366 enum varobj_display_formats format);
367
368 /* Ada implementation */
369
370 static int ada_number_of_children (struct varobj *var);
371
372 static char *ada_name_of_variable (struct varobj *parent);
373
374 static char *ada_name_of_child (struct varobj *parent, int index);
375
376 static char *ada_path_expr_of_child (struct varobj *child);
377
378 static struct value *ada_value_of_root (struct varobj **var_handle);
379
380 static struct value *ada_value_of_child (struct varobj *parent, int index);
381
382 static struct type *ada_type_of_child (struct varobj *parent, int index);
383
384 static char *ada_value_of_variable (struct varobj *var,
385 enum varobj_display_formats format);
386
387 static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
388 struct type *new_type);
389
390 /* The language specific vector */
391
392 struct language_specific
393 {
394
395 /* The language of this variable. */
396 enum varobj_languages language;
397
398 /* The number of children of PARENT. */
399 int (*number_of_children) (struct varobj * parent);
400
401 /* The name (expression) of a root varobj. */
402 char *(*name_of_variable) (struct varobj * parent);
403
404 /* The name of the INDEX'th child of PARENT. */
405 char *(*name_of_child) (struct varobj * parent, int index);
406
407 /* Returns the rooted expression of CHILD, which is a variable
408 obtain that has some parent. */
409 char *(*path_expr_of_child) (struct varobj * child);
410
411 /* The ``struct value *'' of the root variable ROOT. */
412 struct value *(*value_of_root) (struct varobj ** root_handle);
413
414 /* The ``struct value *'' of the INDEX'th child of PARENT. */
415 struct value *(*value_of_child) (struct varobj * parent, int index);
416
417 /* The type of the INDEX'th child of PARENT. */
418 struct type *(*type_of_child) (struct varobj * parent, int index);
419
420 /* The current value of VAR. */
421 char *(*value_of_variable) (struct varobj * var,
422 enum varobj_display_formats format);
423
424 /* Return nonzero if the type of VAR has mutated.
425
426 VAR's value is still the varobj's previous value, while NEW_VALUE
427 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
428 may be NULL indicating that there is no value available (the varobj
429 may be out of scope, of may be the child of a null pointer, for
430 instance). NEW_TYPE, on the other hand, must never be NULL.
431
432 This function should also be able to assume that var's number of
433 children is set (not < 0).
434
435 Languages where types do not mutate can set this to NULL. */
436 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
437 struct type *new_type);
438 };
439
440 /* Array of known source language routines. */
441 static struct language_specific languages[vlang_end] = {
442 /* Unknown (try treating as C). */
443 {
444 vlang_unknown,
445 c_number_of_children,
446 c_name_of_variable,
447 c_name_of_child,
448 c_path_expr_of_child,
449 c_value_of_root,
450 c_value_of_child,
451 c_type_of_child,
452 c_value_of_variable,
453 NULL /* value_has_mutated */}
454 ,
455 /* C */
456 {
457 vlang_c,
458 c_number_of_children,
459 c_name_of_variable,
460 c_name_of_child,
461 c_path_expr_of_child,
462 c_value_of_root,
463 c_value_of_child,
464 c_type_of_child,
465 c_value_of_variable,
466 NULL /* value_has_mutated */}
467 ,
468 /* C++ */
469 {
470 vlang_cplus,
471 cplus_number_of_children,
472 cplus_name_of_variable,
473 cplus_name_of_child,
474 cplus_path_expr_of_child,
475 cplus_value_of_root,
476 cplus_value_of_child,
477 cplus_type_of_child,
478 cplus_value_of_variable,
479 NULL /* value_has_mutated */}
480 ,
481 /* Java */
482 {
483 vlang_java,
484 java_number_of_children,
485 java_name_of_variable,
486 java_name_of_child,
487 java_path_expr_of_child,
488 java_value_of_root,
489 java_value_of_child,
490 java_type_of_child,
491 java_value_of_variable,
492 NULL /* value_has_mutated */},
493 /* Ada */
494 {
495 vlang_ada,
496 ada_number_of_children,
497 ada_name_of_variable,
498 ada_name_of_child,
499 ada_path_expr_of_child,
500 ada_value_of_root,
501 ada_value_of_child,
502 ada_type_of_child,
503 ada_value_of_variable,
504 ada_value_has_mutated}
505 };
506
507 /* A little convenience enum for dealing with C++/Java. */
508 enum vsections
509 {
510 v_public = 0, v_private, v_protected
511 };
512
513 /* Private data */
514
515 /* Mappings of varobj_display_formats enums to gdb's format codes. */
516 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
517
518 /* Header of the list of root variable objects. */
519 static struct varobj_root *rootlist;
520
521 /* Prime number indicating the number of buckets in the hash table. */
522 /* A prime large enough to avoid too many colisions. */
523 #define VAROBJ_TABLE_SIZE 227
524
525 /* Pointer to the varobj hash table (built at run time). */
526 static struct vlist **varobj_table;
527
528 /* Is the variable X one of our "fake" children? */
529 #define CPLUS_FAKE_CHILD(x) \
530 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
531 \f
532
533 /* API Implementation */
534 static int
535 is_root_p (struct varobj *var)
536 {
537 return (var->root->rootvar == var);
538 }
539
540 #ifdef HAVE_PYTHON
541 /* Helper function to install a Python environment suitable for
542 use during operations on VAR. */
543 static struct cleanup *
544 varobj_ensure_python_env (struct varobj *var)
545 {
546 return ensure_python_env (var->root->exp->gdbarch,
547 var->root->exp->language_defn);
548 }
549 #endif
550
551 /* Creates a varobj (not its children). */
552
553 /* Return the full FRAME which corresponds to the given CORE_ADDR
554 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
555
556 static struct frame_info *
557 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
558 {
559 struct frame_info *frame = NULL;
560
561 if (frame_addr == (CORE_ADDR) 0)
562 return NULL;
563
564 for (frame = get_current_frame ();
565 frame != NULL;
566 frame = get_prev_frame (frame))
567 {
568 /* The CORE_ADDR we get as argument was parsed from a string GDB
569 output as $fp. This output got truncated to gdbarch_addr_bit.
570 Truncate the frame base address in the same manner before
571 comparing it against our argument. */
572 CORE_ADDR frame_base = get_frame_base_address (frame);
573 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
574
575 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
576 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
577
578 if (frame_base == frame_addr)
579 return frame;
580 }
581
582 return NULL;
583 }
584
585 struct varobj *
586 varobj_create (char *objname,
587 char *expression, CORE_ADDR frame, enum varobj_type type)
588 {
589 struct varobj *var;
590 struct cleanup *old_chain;
591
592 /* Fill out a varobj structure for the (root) variable being constructed. */
593 var = new_root_variable ();
594 old_chain = make_cleanup_free_variable (var);
595
596 if (expression != NULL)
597 {
598 struct frame_info *fi;
599 struct frame_id old_id = null_frame_id;
600 struct block *block;
601 char *p;
602 enum varobj_languages lang;
603 struct value *value = NULL;
604 volatile struct gdb_exception except;
605
606 /* Parse and evaluate the expression, filling in as much of the
607 variable's data as possible. */
608
609 if (has_stack_frames ())
610 {
611 /* Allow creator to specify context of variable. */
612 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
613 fi = get_selected_frame (NULL);
614 else
615 /* FIXME: cagney/2002-11-23: This code should be doing a
616 lookup using the frame ID and not just the frame's
617 ``address''. This, of course, means an interface
618 change. However, with out that interface change ISAs,
619 such as the ia64 with its two stacks, won't work.
620 Similar goes for the case where there is a frameless
621 function. */
622 fi = find_frame_addr_in_frame_chain (frame);
623 }
624 else
625 fi = NULL;
626
627 /* frame = -2 means always use selected frame. */
628 if (type == USE_SELECTED_FRAME)
629 var->root->floating = 1;
630
631 block = NULL;
632 if (fi != NULL)
633 block = get_frame_block (fi, 0);
634
635 p = expression;
636 innermost_block = NULL;
637 /* Wrap the call to parse expression, so we can
638 return a sensible error. */
639 TRY_CATCH (except, RETURN_MASK_ERROR)
640 {
641 var->root->exp = parse_exp_1 (&p, block, 0);
642 }
643
644 if (except.reason < 0)
645 {
646 do_cleanups (old_chain);
647 return NULL;
648 }
649
650 /* Don't allow variables to be created for types. */
651 if (var->root->exp->elts[0].opcode == OP_TYPE)
652 {
653 do_cleanups (old_chain);
654 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
655 " as an expression.\n");
656 return NULL;
657 }
658
659 var->format = variable_default_display (var);
660 var->root->valid_block = innermost_block;
661 var->name = xstrdup (expression);
662 /* For a root var, the name and the expr are the same. */
663 var->path_expr = xstrdup (expression);
664
665 /* When the frame is different from the current frame,
666 we must select the appropriate frame before parsing
667 the expression, otherwise the value will not be current.
668 Since select_frame is so benign, just call it for all cases. */
669 if (innermost_block)
670 {
671 /* User could specify explicit FRAME-ADDR which was not found but
672 EXPRESSION is frame specific and we would not be able to evaluate
673 it correctly next time. With VALID_BLOCK set we must also set
674 FRAME and THREAD_ID. */
675 if (fi == NULL)
676 error (_("Failed to find the specified frame"));
677
678 var->root->frame = get_frame_id (fi);
679 var->root->thread_id = pid_to_thread_id (inferior_ptid);
680 old_id = get_frame_id (get_selected_frame (NULL));
681 select_frame (fi);
682 }
683
684 /* We definitely need to catch errors here.
685 If evaluate_expression succeeds we got the value we wanted.
686 But if it fails, we still go on with a call to evaluate_type(). */
687 TRY_CATCH (except, RETURN_MASK_ERROR)
688 {
689 value = evaluate_expression (var->root->exp);
690 }
691
692 if (except.reason < 0)
693 {
694 /* Error getting the value. Try to at least get the
695 right type. */
696 struct value *type_only_value = evaluate_type (var->root->exp);
697
698 var->type = value_type (type_only_value);
699 }
700 else
701 var->type = value_type (value);
702
703 install_new_value (var, value, 1 /* Initial assignment */);
704
705 /* Set language info */
706 lang = variable_language (var);
707 var->root->lang = &languages[lang];
708
709 /* Set ourselves as our root. */
710 var->root->rootvar = var;
711
712 /* Reset the selected frame. */
713 if (frame_id_p (old_id))
714 select_frame (frame_find_by_id (old_id));
715 }
716
717 /* If the variable object name is null, that means this
718 is a temporary variable, so don't install it. */
719
720 if ((var != NULL) && (objname != NULL))
721 {
722 var->obj_name = xstrdup (objname);
723
724 /* If a varobj name is duplicated, the install will fail so
725 we must cleanup. */
726 if (!install_variable (var))
727 {
728 do_cleanups (old_chain);
729 return NULL;
730 }
731 }
732
733 discard_cleanups (old_chain);
734 return var;
735 }
736
737 /* Generates an unique name that can be used for a varobj. */
738
739 char *
740 varobj_gen_name (void)
741 {
742 static int id = 0;
743 char *obj_name;
744
745 /* Generate a name for this object. */
746 id++;
747 obj_name = xstrprintf ("var%d", id);
748
749 return obj_name;
750 }
751
752 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
753 error if OBJNAME cannot be found. */
754
755 struct varobj *
756 varobj_get_handle (char *objname)
757 {
758 struct vlist *cv;
759 const char *chp;
760 unsigned int index = 0;
761 unsigned int i = 1;
762
763 for (chp = objname; *chp; chp++)
764 {
765 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
766 }
767
768 cv = *(varobj_table + index);
769 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
770 cv = cv->next;
771
772 if (cv == NULL)
773 error (_("Variable object not found"));
774
775 return cv->var;
776 }
777
778 /* Given the handle, return the name of the object. */
779
780 char *
781 varobj_get_objname (struct varobj *var)
782 {
783 return var->obj_name;
784 }
785
786 /* Given the handle, return the expression represented by the object. */
787
788 char *
789 varobj_get_expression (struct varobj *var)
790 {
791 return name_of_variable (var);
792 }
793
794 /* Deletes a varobj and all its children if only_children == 0,
795 otherwise deletes only the children; returns a malloc'ed list of
796 all the (malloc'ed) names of the variables that have been deleted
797 (NULL terminated). */
798
799 int
800 varobj_delete (struct varobj *var, char ***dellist, int only_children)
801 {
802 int delcount;
803 int mycount;
804 struct cpstack *result = NULL;
805 char **cp;
806
807 /* Initialize a stack for temporary results. */
808 cppush (&result, NULL);
809
810 if (only_children)
811 /* Delete only the variable children. */
812 delcount = delete_variable (&result, var, 1 /* only the children */ );
813 else
814 /* Delete the variable and all its children. */
815 delcount = delete_variable (&result, var, 0 /* parent+children */ );
816
817 /* We may have been asked to return a list of what has been deleted. */
818 if (dellist != NULL)
819 {
820 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
821
822 cp = *dellist;
823 mycount = delcount;
824 *cp = cppop (&result);
825 while ((*cp != NULL) && (mycount > 0))
826 {
827 mycount--;
828 cp++;
829 *cp = cppop (&result);
830 }
831
832 if (mycount || (*cp != NULL))
833 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
834 mycount);
835 }
836
837 return delcount;
838 }
839
840 #if HAVE_PYTHON
841
842 /* Convenience function for varobj_set_visualizer. Instantiate a
843 pretty-printer for a given value. */
844 static PyObject *
845 instantiate_pretty_printer (PyObject *constructor, struct value *value)
846 {
847 PyObject *val_obj = NULL;
848 PyObject *printer;
849
850 val_obj = value_to_value_object (value);
851 if (! val_obj)
852 return NULL;
853
854 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
855 Py_DECREF (val_obj);
856 return printer;
857 }
858
859 #endif
860
861 /* Set/Get variable object display format. */
862
863 enum varobj_display_formats
864 varobj_set_display_format (struct varobj *var,
865 enum varobj_display_formats format)
866 {
867 switch (format)
868 {
869 case FORMAT_NATURAL:
870 case FORMAT_BINARY:
871 case FORMAT_DECIMAL:
872 case FORMAT_HEXADECIMAL:
873 case FORMAT_OCTAL:
874 var->format = format;
875 break;
876
877 default:
878 var->format = variable_default_display (var);
879 }
880
881 if (varobj_value_is_changeable_p (var)
882 && var->value && !value_lazy (var->value))
883 {
884 xfree (var->print_value);
885 var->print_value = value_get_print_value (var->value, var->format, var);
886 }
887
888 return var->format;
889 }
890
891 enum varobj_display_formats
892 varobj_get_display_format (struct varobj *var)
893 {
894 return var->format;
895 }
896
897 char *
898 varobj_get_display_hint (struct varobj *var)
899 {
900 char *result = NULL;
901
902 #if HAVE_PYTHON
903 struct cleanup *back_to = varobj_ensure_python_env (var);
904
905 if (var->pretty_printer)
906 result = gdbpy_get_display_hint (var->pretty_printer);
907
908 do_cleanups (back_to);
909 #endif
910
911 return result;
912 }
913
914 /* Return true if the varobj has items after TO, false otherwise. */
915
916 int
917 varobj_has_more (struct varobj *var, int to)
918 {
919 if (VEC_length (varobj_p, var->children) > to)
920 return 1;
921 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
922 && var->saved_item != NULL);
923 }
924
925 /* If the variable object is bound to a specific thread, that
926 is its evaluation can always be done in context of a frame
927 inside that thread, returns GDB id of the thread -- which
928 is always positive. Otherwise, returns -1. */
929 int
930 varobj_get_thread_id (struct varobj *var)
931 {
932 if (var->root->valid_block && var->root->thread_id > 0)
933 return var->root->thread_id;
934 else
935 return -1;
936 }
937
938 void
939 varobj_set_frozen (struct varobj *var, int frozen)
940 {
941 /* When a variable is unfrozen, we don't fetch its value.
942 The 'not_fetched' flag remains set, so next -var-update
943 won't complain.
944
945 We don't fetch the value, because for structures the client
946 should do -var-update anyway. It would be bad to have different
947 client-size logic for structure and other types. */
948 var->frozen = frozen;
949 }
950
951 int
952 varobj_get_frozen (struct varobj *var)
953 {
954 return var->frozen;
955 }
956
957 /* A helper function that restricts a range to what is actually
958 available in a VEC. This follows the usual rules for the meaning
959 of FROM and TO -- if either is negative, the entire range is
960 used. */
961
962 static void
963 restrict_range (VEC (varobj_p) *children, int *from, int *to)
964 {
965 if (*from < 0 || *to < 0)
966 {
967 *from = 0;
968 *to = VEC_length (varobj_p, children);
969 }
970 else
971 {
972 if (*from > VEC_length (varobj_p, children))
973 *from = VEC_length (varobj_p, children);
974 if (*to > VEC_length (varobj_p, children))
975 *to = VEC_length (varobj_p, children);
976 if (*from > *to)
977 *from = *to;
978 }
979 }
980
981 #if HAVE_PYTHON
982
983 /* A helper for update_dynamic_varobj_children that installs a new
984 child when needed. */
985
986 static void
987 install_dynamic_child (struct varobj *var,
988 VEC (varobj_p) **changed,
989 VEC (varobj_p) **new,
990 VEC (varobj_p) **unchanged,
991 int *cchanged,
992 int index,
993 const char *name,
994 struct value *value)
995 {
996 if (VEC_length (varobj_p, var->children) < index + 1)
997 {
998 /* There's no child yet. */
999 struct varobj *child = varobj_add_child (var, name, value);
1000
1001 if (new)
1002 {
1003 VEC_safe_push (varobj_p, *new, child);
1004 *cchanged = 1;
1005 }
1006 }
1007 else
1008 {
1009 varobj_p existing = VEC_index (varobj_p, var->children, index);
1010
1011 if (install_new_value (existing, value, 0))
1012 {
1013 if (changed)
1014 VEC_safe_push (varobj_p, *changed, existing);
1015 }
1016 else if (unchanged)
1017 VEC_safe_push (varobj_p, *unchanged, existing);
1018 }
1019 }
1020
1021 static int
1022 dynamic_varobj_has_child_method (struct varobj *var)
1023 {
1024 struct cleanup *back_to;
1025 PyObject *printer = var->pretty_printer;
1026 int result;
1027
1028 back_to = varobj_ensure_python_env (var);
1029 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1030 do_cleanups (back_to);
1031 return result;
1032 }
1033
1034 #endif
1035
1036 static int
1037 update_dynamic_varobj_children (struct varobj *var,
1038 VEC (varobj_p) **changed,
1039 VEC (varobj_p) **new,
1040 VEC (varobj_p) **unchanged,
1041 int *cchanged,
1042 int update_children,
1043 int from,
1044 int to)
1045 {
1046 #if HAVE_PYTHON
1047 struct cleanup *back_to;
1048 PyObject *children;
1049 int i;
1050 PyObject *printer = var->pretty_printer;
1051
1052 back_to = varobj_ensure_python_env (var);
1053
1054 *cchanged = 0;
1055 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1056 {
1057 do_cleanups (back_to);
1058 return 0;
1059 }
1060
1061 if (update_children || !var->child_iter)
1062 {
1063 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1064 NULL);
1065
1066 if (!children)
1067 {
1068 gdbpy_print_stack ();
1069 error (_("Null value returned for children"));
1070 }
1071
1072 make_cleanup_py_decref (children);
1073
1074 if (!PyIter_Check (children))
1075 error (_("Returned value is not iterable"));
1076
1077 Py_XDECREF (var->child_iter);
1078 var->child_iter = PyObject_GetIter (children);
1079 if (!var->child_iter)
1080 {
1081 gdbpy_print_stack ();
1082 error (_("Could not get children iterator"));
1083 }
1084
1085 Py_XDECREF (var->saved_item);
1086 var->saved_item = NULL;
1087
1088 i = 0;
1089 }
1090 else
1091 i = VEC_length (varobj_p, var->children);
1092
1093 /* We ask for one extra child, so that MI can report whether there
1094 are more children. */
1095 for (; to < 0 || i < to + 1; ++i)
1096 {
1097 PyObject *item;
1098 int force_done = 0;
1099
1100 /* See if there was a leftover from last time. */
1101 if (var->saved_item)
1102 {
1103 item = var->saved_item;
1104 var->saved_item = NULL;
1105 }
1106 else
1107 item = PyIter_Next (var->child_iter);
1108
1109 if (!item)
1110 {
1111 /* Normal end of iteration. */
1112 if (!PyErr_Occurred ())
1113 break;
1114
1115 /* If we got a memory error, just use the text as the
1116 item. */
1117 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1118 {
1119 PyObject *type, *value, *trace;
1120 char *name_str, *value_str;
1121
1122 PyErr_Fetch (&type, &value, &trace);
1123 value_str = gdbpy_exception_to_string (type, value);
1124 Py_XDECREF (type);
1125 Py_XDECREF (value);
1126 Py_XDECREF (trace);
1127 if (!value_str)
1128 {
1129 gdbpy_print_stack ();
1130 break;
1131 }
1132
1133 name_str = xstrprintf ("<error at %d>", i);
1134 item = Py_BuildValue ("(ss)", name_str, value_str);
1135 xfree (name_str);
1136 xfree (value_str);
1137 if (!item)
1138 {
1139 gdbpy_print_stack ();
1140 break;
1141 }
1142
1143 force_done = 1;
1144 }
1145 else
1146 {
1147 /* Any other kind of error. */
1148 gdbpy_print_stack ();
1149 break;
1150 }
1151 }
1152
1153 /* We don't want to push the extra child on any report list. */
1154 if (to < 0 || i < to)
1155 {
1156 PyObject *py_v;
1157 const char *name;
1158 struct value *v;
1159 struct cleanup *inner;
1160 int can_mention = from < 0 || i >= from;
1161
1162 inner = make_cleanup_py_decref (item);
1163
1164 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1165 {
1166 gdbpy_print_stack ();
1167 error (_("Invalid item from the child list"));
1168 }
1169
1170 v = convert_value_from_python (py_v);
1171 if (v == NULL)
1172 gdbpy_print_stack ();
1173 install_dynamic_child (var, can_mention ? changed : NULL,
1174 can_mention ? new : NULL,
1175 can_mention ? unchanged : NULL,
1176 can_mention ? cchanged : NULL, i, name, v);
1177 do_cleanups (inner);
1178 }
1179 else
1180 {
1181 Py_XDECREF (var->saved_item);
1182 var->saved_item = item;
1183
1184 /* We want to truncate the child list just before this
1185 element. */
1186 break;
1187 }
1188
1189 if (force_done)
1190 break;
1191 }
1192
1193 if (i < VEC_length (varobj_p, var->children))
1194 {
1195 int j;
1196
1197 *cchanged = 1;
1198 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1199 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1200 VEC_truncate (varobj_p, var->children, i);
1201 }
1202
1203 /* If there are fewer children than requested, note that the list of
1204 children changed. */
1205 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1206 *cchanged = 1;
1207
1208 var->num_children = VEC_length (varobj_p, var->children);
1209
1210 do_cleanups (back_to);
1211
1212 return 1;
1213 #else
1214 gdb_assert (0 && "should never be called if Python is not enabled");
1215 #endif
1216 }
1217
1218 int
1219 varobj_get_num_children (struct varobj *var)
1220 {
1221 if (var->num_children == -1)
1222 {
1223 if (var->pretty_printer)
1224 {
1225 int dummy;
1226
1227 /* If we have a dynamic varobj, don't report -1 children.
1228 So, try to fetch some children first. */
1229 update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
1230 0, 0, 0);
1231 }
1232 else
1233 var->num_children = number_of_children (var);
1234 }
1235
1236 return var->num_children >= 0 ? var->num_children : 0;
1237 }
1238
1239 /* Creates a list of the immediate children of a variable object;
1240 the return code is the number of such children or -1 on error. */
1241
1242 VEC (varobj_p)*
1243 varobj_list_children (struct varobj *var, int *from, int *to)
1244 {
1245 char *name;
1246 int i, children_changed;
1247
1248 var->children_requested = 1;
1249
1250 if (var->pretty_printer)
1251 {
1252 /* This, in theory, can result in the number of children changing without
1253 frontend noticing. But well, calling -var-list-children on the same
1254 varobj twice is not something a sane frontend would do. */
1255 update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
1256 0, 0, *to);
1257 restrict_range (var->children, from, to);
1258 return var->children;
1259 }
1260
1261 if (var->num_children == -1)
1262 var->num_children = number_of_children (var);
1263
1264 /* If that failed, give up. */
1265 if (var->num_children == -1)
1266 return var->children;
1267
1268 /* If we're called when the list of children is not yet initialized,
1269 allocate enough elements in it. */
1270 while (VEC_length (varobj_p, var->children) < var->num_children)
1271 VEC_safe_push (varobj_p, var->children, NULL);
1272
1273 for (i = 0; i < var->num_children; i++)
1274 {
1275 varobj_p existing = VEC_index (varobj_p, var->children, i);
1276
1277 if (existing == NULL)
1278 {
1279 /* Either it's the first call to varobj_list_children for
1280 this variable object, and the child was never created,
1281 or it was explicitly deleted by the client. */
1282 name = name_of_child (var, i);
1283 existing = create_child (var, i, name);
1284 VEC_replace (varobj_p, var->children, i, existing);
1285 }
1286 }
1287
1288 restrict_range (var->children, from, to);
1289 return var->children;
1290 }
1291
1292 #if HAVE_PYTHON
1293
1294 static struct varobj *
1295 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1296 {
1297 varobj_p v = create_child_with_value (var,
1298 VEC_length (varobj_p, var->children),
1299 name, value);
1300
1301 VEC_safe_push (varobj_p, var->children, v);
1302 return v;
1303 }
1304
1305 #endif /* HAVE_PYTHON */
1306
1307 /* Obtain the type of an object Variable as a string similar to the one gdb
1308 prints on the console. */
1309
1310 char *
1311 varobj_get_type (struct varobj *var)
1312 {
1313 /* For the "fake" variables, do not return a type. (It's type is
1314 NULL, too.)
1315 Do not return a type for invalid variables as well. */
1316 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1317 return NULL;
1318
1319 return type_to_string (var->type);
1320 }
1321
1322 /* Obtain the type of an object variable. */
1323
1324 struct type *
1325 varobj_get_gdb_type (struct varobj *var)
1326 {
1327 return var->type;
1328 }
1329
1330 /* Is VAR a path expression parent, i.e., can it be used to construct
1331 a valid path expression? */
1332
1333 static int
1334 is_path_expr_parent (struct varobj *var)
1335 {
1336 struct type *type;
1337
1338 /* "Fake" children are not path_expr parents. */
1339 if (CPLUS_FAKE_CHILD (var))
1340 return 0;
1341
1342 type = get_value_type (var);
1343
1344 /* Anonymous unions and structs are also not path_expr parents. */
1345 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1346 || TYPE_CODE (type) == TYPE_CODE_UNION)
1347 && TYPE_NAME (type) == NULL);
1348 }
1349
1350 /* Return the path expression parent for VAR. */
1351
1352 static struct varobj *
1353 get_path_expr_parent (struct varobj *var)
1354 {
1355 struct varobj *parent = var;
1356
1357 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1358 parent = parent->parent;
1359
1360 return parent;
1361 }
1362
1363 /* Return a pointer to the full rooted expression of varobj VAR.
1364 If it has not been computed yet, compute it. */
1365 char *
1366 varobj_get_path_expr (struct varobj *var)
1367 {
1368 if (var->path_expr != NULL)
1369 return var->path_expr;
1370 else
1371 {
1372 /* For root varobjs, we initialize path_expr
1373 when creating varobj, so here it should be
1374 child varobj. */
1375 gdb_assert (!is_root_p (var));
1376 return (*var->root->lang->path_expr_of_child) (var);
1377 }
1378 }
1379
1380 enum varobj_languages
1381 varobj_get_language (struct varobj *var)
1382 {
1383 return variable_language (var);
1384 }
1385
1386 int
1387 varobj_get_attributes (struct varobj *var)
1388 {
1389 int attributes = 0;
1390
1391 if (varobj_editable_p (var))
1392 /* FIXME: define masks for attributes. */
1393 attributes |= 0x00000001; /* Editable */
1394
1395 return attributes;
1396 }
1397
1398 int
1399 varobj_pretty_printed_p (struct varobj *var)
1400 {
1401 return var->pretty_printer != NULL;
1402 }
1403
1404 char *
1405 varobj_get_formatted_value (struct varobj *var,
1406 enum varobj_display_formats format)
1407 {
1408 return my_value_of_variable (var, format);
1409 }
1410
1411 char *
1412 varobj_get_value (struct varobj *var)
1413 {
1414 return my_value_of_variable (var, var->format);
1415 }
1416
1417 /* Set the value of an object variable (if it is editable) to the
1418 value of the given expression. */
1419 /* Note: Invokes functions that can call error(). */
1420
1421 int
1422 varobj_set_value (struct varobj *var, char *expression)
1423 {
1424 struct value *val = NULL; /* Initialize to keep gcc happy. */
1425 /* The argument "expression" contains the variable's new value.
1426 We need to first construct a legal expression for this -- ugh! */
1427 /* Does this cover all the bases? */
1428 struct expression *exp;
1429 struct value *value = NULL; /* Initialize to keep gcc happy. */
1430 int saved_input_radix = input_radix;
1431 char *s = expression;
1432 volatile struct gdb_exception except;
1433
1434 gdb_assert (varobj_editable_p (var));
1435
1436 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1437 exp = parse_exp_1 (&s, 0, 0);
1438 TRY_CATCH (except, RETURN_MASK_ERROR)
1439 {
1440 value = evaluate_expression (exp);
1441 }
1442
1443 if (except.reason < 0)
1444 {
1445 /* We cannot proceed without a valid expression. */
1446 xfree (exp);
1447 return 0;
1448 }
1449
1450 /* All types that are editable must also be changeable. */
1451 gdb_assert (varobj_value_is_changeable_p (var));
1452
1453 /* The value of a changeable variable object must not be lazy. */
1454 gdb_assert (!value_lazy (var->value));
1455
1456 /* Need to coerce the input. We want to check if the
1457 value of the variable object will be different
1458 after assignment, and the first thing value_assign
1459 does is coerce the input.
1460 For example, if we are assigning an array to a pointer variable we
1461 should compare the pointer with the array's address, not with the
1462 array's content. */
1463 value = coerce_array (value);
1464
1465 /* The new value may be lazy. value_assign, or
1466 rather value_contents, will take care of this. */
1467 TRY_CATCH (except, RETURN_MASK_ERROR)
1468 {
1469 val = value_assign (var->value, value);
1470 }
1471
1472 if (except.reason < 0)
1473 return 0;
1474
1475 /* If the value has changed, record it, so that next -var-update can
1476 report this change. If a variable had a value of '1', we've set it
1477 to '333' and then set again to '1', when -var-update will report this
1478 variable as changed -- because the first assignment has set the
1479 'updated' flag. There's no need to optimize that, because return value
1480 of -var-update should be considered an approximation. */
1481 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1482 input_radix = saved_input_radix;
1483 return 1;
1484 }
1485
1486 #if HAVE_PYTHON
1487
1488 /* A helper function to install a constructor function and visualizer
1489 in a varobj. */
1490
1491 static void
1492 install_visualizer (struct varobj *var, PyObject *constructor,
1493 PyObject *visualizer)
1494 {
1495 Py_XDECREF (var->constructor);
1496 var->constructor = constructor;
1497
1498 Py_XDECREF (var->pretty_printer);
1499 var->pretty_printer = visualizer;
1500
1501 Py_XDECREF (var->child_iter);
1502 var->child_iter = NULL;
1503 }
1504
1505 /* Install the default visualizer for VAR. */
1506
1507 static void
1508 install_default_visualizer (struct varobj *var)
1509 {
1510 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1511 if (CPLUS_FAKE_CHILD (var))
1512 return;
1513
1514 if (pretty_printing)
1515 {
1516 PyObject *pretty_printer = NULL;
1517
1518 if (var->value)
1519 {
1520 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1521 if (! pretty_printer)
1522 {
1523 gdbpy_print_stack ();
1524 error (_("Cannot instantiate printer for default visualizer"));
1525 }
1526 }
1527
1528 if (pretty_printer == Py_None)
1529 {
1530 Py_DECREF (pretty_printer);
1531 pretty_printer = NULL;
1532 }
1533
1534 install_visualizer (var, NULL, pretty_printer);
1535 }
1536 }
1537
1538 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1539 make a new object. */
1540
1541 static void
1542 construct_visualizer (struct varobj *var, PyObject *constructor)
1543 {
1544 PyObject *pretty_printer;
1545
1546 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1547 if (CPLUS_FAKE_CHILD (var))
1548 return;
1549
1550 Py_INCREF (constructor);
1551 if (constructor == Py_None)
1552 pretty_printer = NULL;
1553 else
1554 {
1555 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1556 if (! pretty_printer)
1557 {
1558 gdbpy_print_stack ();
1559 Py_DECREF (constructor);
1560 constructor = Py_None;
1561 Py_INCREF (constructor);
1562 }
1563
1564 if (pretty_printer == Py_None)
1565 {
1566 Py_DECREF (pretty_printer);
1567 pretty_printer = NULL;
1568 }
1569 }
1570
1571 install_visualizer (var, constructor, pretty_printer);
1572 }
1573
1574 #endif /* HAVE_PYTHON */
1575
1576 /* A helper function for install_new_value. This creates and installs
1577 a visualizer for VAR, if appropriate. */
1578
1579 static void
1580 install_new_value_visualizer (struct varobj *var)
1581 {
1582 #if HAVE_PYTHON
1583 /* If the constructor is None, then we want the raw value. If VAR
1584 does not have a value, just skip this. */
1585 if (var->constructor != Py_None && var->value)
1586 {
1587 struct cleanup *cleanup;
1588
1589 cleanup = varobj_ensure_python_env (var);
1590
1591 if (!var->constructor)
1592 install_default_visualizer (var);
1593 else
1594 construct_visualizer (var, var->constructor);
1595
1596 do_cleanups (cleanup);
1597 }
1598 #else
1599 /* Do nothing. */
1600 #endif
1601 }
1602
1603 /* Assign a new value to a variable object. If INITIAL is non-zero,
1604 this is the first assignement after the variable object was just
1605 created, or changed type. In that case, just assign the value
1606 and return 0.
1607 Otherwise, assign the new value, and return 1 if the value is
1608 different from the current one, 0 otherwise. The comparison is
1609 done on textual representation of value. Therefore, some types
1610 need not be compared. E.g. for structures the reported value is
1611 always "{...}", so no comparison is necessary here. If the old
1612 value was NULL and new one is not, or vice versa, we always return 1.
1613
1614 The VALUE parameter should not be released -- the function will
1615 take care of releasing it when needed. */
1616 static int
1617 install_new_value (struct varobj *var, struct value *value, int initial)
1618 {
1619 int changeable;
1620 int need_to_fetch;
1621 int changed = 0;
1622 int intentionally_not_fetched = 0;
1623 char *print_value = NULL;
1624
1625 /* We need to know the varobj's type to decide if the value should
1626 be fetched or not. C++ fake children (public/protected/private)
1627 don't have a type. */
1628 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1629 changeable = varobj_value_is_changeable_p (var);
1630
1631 /* If the type has custom visualizer, we consider it to be always
1632 changeable. FIXME: need to make sure this behaviour will not
1633 mess up read-sensitive values. */
1634 if (var->pretty_printer)
1635 changeable = 1;
1636
1637 need_to_fetch = changeable;
1638
1639 /* We are not interested in the address of references, and given
1640 that in C++ a reference is not rebindable, it cannot
1641 meaningfully change. So, get hold of the real value. */
1642 if (value)
1643 value = coerce_ref (value);
1644
1645 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1646 /* For unions, we need to fetch the value implicitly because
1647 of implementation of union member fetch. When gdb
1648 creates a value for a field and the value of the enclosing
1649 structure is not lazy, it immediately copies the necessary
1650 bytes from the enclosing values. If the enclosing value is
1651 lazy, the call to value_fetch_lazy on the field will read
1652 the data from memory. For unions, that means we'll read the
1653 same memory more than once, which is not desirable. So
1654 fetch now. */
1655 need_to_fetch = 1;
1656
1657 /* The new value might be lazy. If the type is changeable,
1658 that is we'll be comparing values of this type, fetch the
1659 value now. Otherwise, on the next update the old value
1660 will be lazy, which means we've lost that old value. */
1661 if (need_to_fetch && value && value_lazy (value))
1662 {
1663 struct varobj *parent = var->parent;
1664 int frozen = var->frozen;
1665
1666 for (; !frozen && parent; parent = parent->parent)
1667 frozen |= parent->frozen;
1668
1669 if (frozen && initial)
1670 {
1671 /* For variables that are frozen, or are children of frozen
1672 variables, we don't do fetch on initial assignment.
1673 For non-initial assignemnt we do the fetch, since it means we're
1674 explicitly asked to compare the new value with the old one. */
1675 intentionally_not_fetched = 1;
1676 }
1677 else
1678 {
1679 volatile struct gdb_exception except;
1680
1681 TRY_CATCH (except, RETURN_MASK_ERROR)
1682 {
1683 value_fetch_lazy (value);
1684 }
1685
1686 if (except.reason < 0)
1687 {
1688 /* Set the value to NULL, so that for the next -var-update,
1689 we don't try to compare the new value with this value,
1690 that we couldn't even read. */
1691 value = NULL;
1692 }
1693 }
1694 }
1695
1696 /* Get a reference now, before possibly passing it to any Python
1697 code that might release it. */
1698 if (value != NULL)
1699 value_incref (value);
1700
1701 /* Below, we'll be comparing string rendering of old and new
1702 values. Don't get string rendering if the value is
1703 lazy -- if it is, the code above has decided that the value
1704 should not be fetched. */
1705 if (value && !value_lazy (value) && !var->pretty_printer)
1706 print_value = value_get_print_value (value, var->format, var);
1707
1708 /* If the type is changeable, compare the old and the new values.
1709 If this is the initial assignment, we don't have any old value
1710 to compare with. */
1711 if (!initial && changeable)
1712 {
1713 /* If the value of the varobj was changed by -var-set-value,
1714 then the value in the varobj and in the target is the same.
1715 However, that value is different from the value that the
1716 varobj had after the previous -var-update. So need to the
1717 varobj as changed. */
1718 if (var->updated)
1719 {
1720 changed = 1;
1721 }
1722 else if (! var->pretty_printer)
1723 {
1724 /* Try to compare the values. That requires that both
1725 values are non-lazy. */
1726 if (var->not_fetched && value_lazy (var->value))
1727 {
1728 /* This is a frozen varobj and the value was never read.
1729 Presumably, UI shows some "never read" indicator.
1730 Now that we've fetched the real value, we need to report
1731 this varobj as changed so that UI can show the real
1732 value. */
1733 changed = 1;
1734 }
1735 else if (var->value == NULL && value == NULL)
1736 /* Equal. */
1737 ;
1738 else if (var->value == NULL || value == NULL)
1739 {
1740 changed = 1;
1741 }
1742 else
1743 {
1744 gdb_assert (!value_lazy (var->value));
1745 gdb_assert (!value_lazy (value));
1746
1747 gdb_assert (var->print_value != NULL && print_value != NULL);
1748 if (strcmp (var->print_value, print_value) != 0)
1749 changed = 1;
1750 }
1751 }
1752 }
1753
1754 if (!initial && !changeable)
1755 {
1756 /* For values that are not changeable, we don't compare the values.
1757 However, we want to notice if a value was not NULL and now is NULL,
1758 or vise versa, so that we report when top-level varobjs come in scope
1759 and leave the scope. */
1760 changed = (var->value != NULL) != (value != NULL);
1761 }
1762
1763 /* We must always keep the new value, since children depend on it. */
1764 if (var->value != NULL && var->value != value)
1765 value_free (var->value);
1766 var->value = value;
1767 if (value && value_lazy (value) && intentionally_not_fetched)
1768 var->not_fetched = 1;
1769 else
1770 var->not_fetched = 0;
1771 var->updated = 0;
1772
1773 install_new_value_visualizer (var);
1774
1775 /* If we installed a pretty-printer, re-compare the printed version
1776 to see if the variable changed. */
1777 if (var->pretty_printer)
1778 {
1779 xfree (print_value);
1780 print_value = value_get_print_value (var->value, var->format, var);
1781 if ((var->print_value == NULL && print_value != NULL)
1782 || (var->print_value != NULL && print_value == NULL)
1783 || (var->print_value != NULL && print_value != NULL
1784 && strcmp (var->print_value, print_value) != 0))
1785 changed = 1;
1786 }
1787 if (var->print_value)
1788 xfree (var->print_value);
1789 var->print_value = print_value;
1790
1791 gdb_assert (!var->value || value_type (var->value));
1792
1793 return changed;
1794 }
1795
1796 /* Return the requested range for a varobj. VAR is the varobj. FROM
1797 and TO are out parameters; *FROM and *TO will be set to the
1798 selected sub-range of VAR. If no range was selected using
1799 -var-set-update-range, then both will be -1. */
1800 void
1801 varobj_get_child_range (struct varobj *var, int *from, int *to)
1802 {
1803 *from = var->from;
1804 *to = var->to;
1805 }
1806
1807 /* Set the selected sub-range of children of VAR to start at index
1808 FROM and end at index TO. If either FROM or TO is less than zero,
1809 this is interpreted as a request for all children. */
1810 void
1811 varobj_set_child_range (struct varobj *var, int from, int to)
1812 {
1813 var->from = from;
1814 var->to = to;
1815 }
1816
1817 void
1818 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1819 {
1820 #if HAVE_PYTHON
1821 PyObject *mainmod, *globals, *constructor;
1822 struct cleanup *back_to;
1823
1824 back_to = varobj_ensure_python_env (var);
1825
1826 mainmod = PyImport_AddModule ("__main__");
1827 globals = PyModule_GetDict (mainmod);
1828 Py_INCREF (globals);
1829 make_cleanup_py_decref (globals);
1830
1831 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1832
1833 if (! constructor)
1834 {
1835 gdbpy_print_stack ();
1836 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1837 }
1838
1839 construct_visualizer (var, constructor);
1840 Py_XDECREF (constructor);
1841
1842 /* If there are any children now, wipe them. */
1843 varobj_delete (var, NULL, 1 /* children only */);
1844 var->num_children = -1;
1845
1846 do_cleanups (back_to);
1847 #else
1848 error (_("Python support required"));
1849 #endif
1850 }
1851
1852 /* If NEW_VALUE is the new value of the given varobj (var), return
1853 non-zero if var has mutated. In other words, if the type of
1854 the new value is different from the type of the varobj's old
1855 value.
1856
1857 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1858
1859 static int
1860 varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1861 struct type *new_type)
1862 {
1863 /* If we haven't previously computed the number of children in var,
1864 it does not matter from the front-end's perspective whether
1865 the type has mutated or not. For all intents and purposes,
1866 it has not mutated. */
1867 if (var->num_children < 0)
1868 return 0;
1869
1870 if (var->root->lang->value_has_mutated)
1871 return var->root->lang->value_has_mutated (var, new_value, new_type);
1872 else
1873 return 0;
1874 }
1875
1876 /* Update the values for a variable and its children. This is a
1877 two-pronged attack. First, re-parse the value for the root's
1878 expression to see if it's changed. Then go all the way
1879 through its children, reconstructing them and noting if they've
1880 changed.
1881
1882 The EXPLICIT parameter specifies if this call is result
1883 of MI request to update this specific variable, or
1884 result of implicit -var-update *. For implicit request, we don't
1885 update frozen variables.
1886
1887 NOTE: This function may delete the caller's varobj. If it
1888 returns TYPE_CHANGED, then it has done this and VARP will be modified
1889 to point to the new varobj. */
1890
1891 VEC(varobj_update_result) *
1892 varobj_update (struct varobj **varp, int explicit)
1893 {
1894 int changed = 0;
1895 int type_changed = 0;
1896 int i;
1897 struct value *new;
1898 VEC (varobj_update_result) *stack = NULL;
1899 VEC (varobj_update_result) *result = NULL;
1900
1901 /* Frozen means frozen -- we don't check for any change in
1902 this varobj, including its going out of scope, or
1903 changing type. One use case for frozen varobjs is
1904 retaining previously evaluated expressions, and we don't
1905 want them to be reevaluated at all. */
1906 if (!explicit && (*varp)->frozen)
1907 return result;
1908
1909 if (!(*varp)->root->is_valid)
1910 {
1911 varobj_update_result r = {0};
1912
1913 r.varobj = *varp;
1914 r.status = VAROBJ_INVALID;
1915 VEC_safe_push (varobj_update_result, result, &r);
1916 return result;
1917 }
1918
1919 if ((*varp)->root->rootvar == *varp)
1920 {
1921 varobj_update_result r = {0};
1922
1923 r.varobj = *varp;
1924 r.status = VAROBJ_IN_SCOPE;
1925
1926 /* Update the root variable. value_of_root can return NULL
1927 if the variable is no longer around, i.e. we stepped out of
1928 the frame in which a local existed. We are letting the
1929 value_of_root variable dispose of the varobj if the type
1930 has changed. */
1931 new = value_of_root (varp, &type_changed);
1932 r.varobj = *varp;
1933
1934 r.type_changed = type_changed;
1935 if (install_new_value ((*varp), new, type_changed))
1936 r.changed = 1;
1937
1938 if (new == NULL)
1939 r.status = VAROBJ_NOT_IN_SCOPE;
1940 r.value_installed = 1;
1941
1942 if (r.status == VAROBJ_NOT_IN_SCOPE)
1943 {
1944 if (r.type_changed || r.changed)
1945 VEC_safe_push (varobj_update_result, result, &r);
1946 return result;
1947 }
1948
1949 VEC_safe_push (varobj_update_result, stack, &r);
1950 }
1951 else
1952 {
1953 varobj_update_result r = {0};
1954
1955 r.varobj = *varp;
1956 VEC_safe_push (varobj_update_result, stack, &r);
1957 }
1958
1959 /* Walk through the children, reconstructing them all. */
1960 while (!VEC_empty (varobj_update_result, stack))
1961 {
1962 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1963 struct varobj *v = r.varobj;
1964
1965 VEC_pop (varobj_update_result, stack);
1966
1967 /* Update this variable, unless it's a root, which is already
1968 updated. */
1969 if (!r.value_installed)
1970 {
1971 struct type *new_type;
1972
1973 new = value_of_child (v->parent, v->index);
1974 if (new)
1975 new_type = value_type (new);
1976 else
1977 new_type = v->root->lang->type_of_child (v->parent, v->index);
1978
1979 if (varobj_value_has_mutated (v, new, new_type))
1980 {
1981 /* The children are no longer valid; delete them now.
1982 Report the fact that its type changed as well. */
1983 varobj_delete (v, NULL, 1 /* only_children */);
1984 v->num_children = -1;
1985 v->to = -1;
1986 v->from = -1;
1987 v->type = new_type;
1988 r.type_changed = 1;
1989 }
1990
1991 if (install_new_value (v, new, r.type_changed))
1992 {
1993 r.changed = 1;
1994 v->updated = 0;
1995 }
1996 }
1997
1998 /* We probably should not get children of a varobj that has a
1999 pretty-printer, but for which -var-list-children was never
2000 invoked. */
2001 if (v->pretty_printer)
2002 {
2003 VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
2004 int i, children_changed = 0;
2005
2006 if (v->frozen)
2007 continue;
2008
2009 if (!v->children_requested)
2010 {
2011 int dummy;
2012
2013 /* If we initially did not have potential children, but
2014 now we do, consider the varobj as changed.
2015 Otherwise, if children were never requested, consider
2016 it as unchanged -- presumably, such varobj is not yet
2017 expanded in the UI, so we need not bother getting
2018 it. */
2019 if (!varobj_has_more (v, 0))
2020 {
2021 update_dynamic_varobj_children (v, NULL, NULL, NULL,
2022 &dummy, 0, 0, 0);
2023 if (varobj_has_more (v, 0))
2024 r.changed = 1;
2025 }
2026
2027 if (r.changed)
2028 VEC_safe_push (varobj_update_result, result, &r);
2029
2030 continue;
2031 }
2032
2033 /* If update_dynamic_varobj_children returns 0, then we have
2034 a non-conforming pretty-printer, so we skip it. */
2035 if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
2036 &children_changed, 1,
2037 v->from, v->to))
2038 {
2039 if (children_changed || new)
2040 {
2041 r.children_changed = 1;
2042 r.new = new;
2043 }
2044 /* Push in reverse order so that the first child is
2045 popped from the work stack first, and so will be
2046 added to result first. This does not affect
2047 correctness, just "nicer". */
2048 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
2049 {
2050 varobj_p tmp = VEC_index (varobj_p, changed, i);
2051 varobj_update_result r = {0};
2052
2053 r.varobj = tmp;
2054 r.changed = 1;
2055 r.value_installed = 1;
2056 VEC_safe_push (varobj_update_result, stack, &r);
2057 }
2058 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2059 {
2060 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2061
2062 if (!tmp->frozen)
2063 {
2064 varobj_update_result r = {0};
2065
2066 r.varobj = tmp;
2067 r.value_installed = 1;
2068 VEC_safe_push (varobj_update_result, stack, &r);
2069 }
2070 }
2071 if (r.changed || r.children_changed)
2072 VEC_safe_push (varobj_update_result, result, &r);
2073
2074 /* Free CHANGED and UNCHANGED, but not NEW, because NEW
2075 has been put into the result vector. */
2076 VEC_free (varobj_p, changed);
2077 VEC_free (varobj_p, unchanged);
2078
2079 continue;
2080 }
2081 }
2082
2083 /* Push any children. Use reverse order so that the first
2084 child is popped from the work stack first, and so
2085 will be added to result first. This does not
2086 affect correctness, just "nicer". */
2087 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2088 {
2089 varobj_p c = VEC_index (varobj_p, v->children, i);
2090
2091 /* Child may be NULL if explicitly deleted by -var-delete. */
2092 if (c != NULL && !c->frozen)
2093 {
2094 varobj_update_result r = {0};
2095
2096 r.varobj = c;
2097 VEC_safe_push (varobj_update_result, stack, &r);
2098 }
2099 }
2100
2101 if (r.changed || r.type_changed)
2102 VEC_safe_push (varobj_update_result, result, &r);
2103 }
2104
2105 VEC_free (varobj_update_result, stack);
2106
2107 return result;
2108 }
2109 \f
2110
2111 /* Helper functions */
2112
2113 /*
2114 * Variable object construction/destruction
2115 */
2116
2117 static int
2118 delete_variable (struct cpstack **resultp, struct varobj *var,
2119 int only_children_p)
2120 {
2121 int delcount = 0;
2122
2123 delete_variable_1 (resultp, &delcount, var,
2124 only_children_p, 1 /* remove_from_parent_p */ );
2125
2126 return delcount;
2127 }
2128
2129 /* Delete the variable object VAR and its children. */
2130 /* IMPORTANT NOTE: If we delete a variable which is a child
2131 and the parent is not removed we dump core. It must be always
2132 initially called with remove_from_parent_p set. */
2133 static void
2134 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2135 struct varobj *var, int only_children_p,
2136 int remove_from_parent_p)
2137 {
2138 int i;
2139
2140 /* Delete any children of this variable, too. */
2141 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2142 {
2143 varobj_p child = VEC_index (varobj_p, var->children, i);
2144
2145 if (!child)
2146 continue;
2147 if (!remove_from_parent_p)
2148 child->parent = NULL;
2149 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2150 }
2151 VEC_free (varobj_p, var->children);
2152
2153 /* if we were called to delete only the children we are done here. */
2154 if (only_children_p)
2155 return;
2156
2157 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2158 /* If the name is null, this is a temporary variable, that has not
2159 yet been installed, don't report it, it belongs to the caller... */
2160 if (var->obj_name != NULL)
2161 {
2162 cppush (resultp, xstrdup (var->obj_name));
2163 *delcountp = *delcountp + 1;
2164 }
2165
2166 /* If this variable has a parent, remove it from its parent's list. */
2167 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2168 (as indicated by remove_from_parent_p) we don't bother doing an
2169 expensive list search to find the element to remove when we are
2170 discarding the list afterwards. */
2171 if ((remove_from_parent_p) && (var->parent != NULL))
2172 {
2173 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2174 }
2175
2176 if (var->obj_name != NULL)
2177 uninstall_variable (var);
2178
2179 /* Free memory associated with this variable. */
2180 free_variable (var);
2181 }
2182
2183 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2184 static int
2185 install_variable (struct varobj *var)
2186 {
2187 struct vlist *cv;
2188 struct vlist *newvl;
2189 const char *chp;
2190 unsigned int index = 0;
2191 unsigned int i = 1;
2192
2193 for (chp = var->obj_name; *chp; chp++)
2194 {
2195 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2196 }
2197
2198 cv = *(varobj_table + index);
2199 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2200 cv = cv->next;
2201
2202 if (cv != NULL)
2203 error (_("Duplicate variable object name"));
2204
2205 /* Add varobj to hash table. */
2206 newvl = xmalloc (sizeof (struct vlist));
2207 newvl->next = *(varobj_table + index);
2208 newvl->var = var;
2209 *(varobj_table + index) = newvl;
2210
2211 /* If root, add varobj to root list. */
2212 if (is_root_p (var))
2213 {
2214 /* Add to list of root variables. */
2215 if (rootlist == NULL)
2216 var->root->next = NULL;
2217 else
2218 var->root->next = rootlist;
2219 rootlist = var->root;
2220 }
2221
2222 return 1; /* OK */
2223 }
2224
2225 /* Unistall the object VAR. */
2226 static void
2227 uninstall_variable (struct varobj *var)
2228 {
2229 struct vlist *cv;
2230 struct vlist *prev;
2231 struct varobj_root *cr;
2232 struct varobj_root *prer;
2233 const char *chp;
2234 unsigned int index = 0;
2235 unsigned int i = 1;
2236
2237 /* Remove varobj from hash table. */
2238 for (chp = var->obj_name; *chp; chp++)
2239 {
2240 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2241 }
2242
2243 cv = *(varobj_table + index);
2244 prev = NULL;
2245 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2246 {
2247 prev = cv;
2248 cv = cv->next;
2249 }
2250
2251 if (varobjdebug)
2252 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2253
2254 if (cv == NULL)
2255 {
2256 warning
2257 ("Assertion failed: Could not find variable object \"%s\" to delete",
2258 var->obj_name);
2259 return;
2260 }
2261
2262 if (prev == NULL)
2263 *(varobj_table + index) = cv->next;
2264 else
2265 prev->next = cv->next;
2266
2267 xfree (cv);
2268
2269 /* If root, remove varobj from root list. */
2270 if (is_root_p (var))
2271 {
2272 /* Remove from list of root variables. */
2273 if (rootlist == var->root)
2274 rootlist = var->root->next;
2275 else
2276 {
2277 prer = NULL;
2278 cr = rootlist;
2279 while ((cr != NULL) && (cr->rootvar != var))
2280 {
2281 prer = cr;
2282 cr = cr->next;
2283 }
2284 if (cr == NULL)
2285 {
2286 warning (_("Assertion failed: Could not find "
2287 "varobj \"%s\" in root list"),
2288 var->obj_name);
2289 return;
2290 }
2291 if (prer == NULL)
2292 rootlist = NULL;
2293 else
2294 prer->next = cr->next;
2295 }
2296 }
2297
2298 }
2299
2300 /* Create and install a child of the parent of the given name. */
2301 static struct varobj *
2302 create_child (struct varobj *parent, int index, char *name)
2303 {
2304 return create_child_with_value (parent, index, name,
2305 value_of_child (parent, index));
2306 }
2307
2308 /* Does CHILD represent a child with no name? This happens when
2309 the child is an anonmous struct or union and it has no field name
2310 in its parent variable.
2311
2312 This has already been determined by *_describe_child. The easiest
2313 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2314
2315 static int
2316 is_anonymous_child (struct varobj *child)
2317 {
2318 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2319 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2320 }
2321
2322 static struct varobj *
2323 create_child_with_value (struct varobj *parent, int index, const char *name,
2324 struct value *value)
2325 {
2326 struct varobj *child;
2327 char *childs_name;
2328
2329 child = new_variable ();
2330
2331 /* Name is allocated by name_of_child. */
2332 /* FIXME: xstrdup should not be here. */
2333 child->name = xstrdup (name);
2334 child->index = index;
2335 child->parent = parent;
2336 child->root = parent->root;
2337
2338 if (is_anonymous_child (child))
2339 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2340 else
2341 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2342 child->obj_name = childs_name;
2343
2344 install_variable (child);
2345
2346 /* Compute the type of the child. Must do this before
2347 calling install_new_value. */
2348 if (value != NULL)
2349 /* If the child had no evaluation errors, var->value
2350 will be non-NULL and contain a valid type. */
2351 child->type = value_type (value);
2352 else
2353 /* Otherwise, we must compute the type. */
2354 child->type = (*child->root->lang->type_of_child) (child->parent,
2355 child->index);
2356 install_new_value (child, value, 1);
2357
2358 return child;
2359 }
2360 \f
2361
2362 /*
2363 * Miscellaneous utility functions.
2364 */
2365
2366 /* Allocate memory and initialize a new variable. */
2367 static struct varobj *
2368 new_variable (void)
2369 {
2370 struct varobj *var;
2371
2372 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2373 var->name = NULL;
2374 var->path_expr = NULL;
2375 var->obj_name = NULL;
2376 var->index = -1;
2377 var->type = NULL;
2378 var->value = NULL;
2379 var->num_children = -1;
2380 var->parent = NULL;
2381 var->children = NULL;
2382 var->format = 0;
2383 var->root = NULL;
2384 var->updated = 0;
2385 var->print_value = NULL;
2386 var->frozen = 0;
2387 var->not_fetched = 0;
2388 var->children_requested = 0;
2389 var->from = -1;
2390 var->to = -1;
2391 var->constructor = 0;
2392 var->pretty_printer = 0;
2393 var->child_iter = 0;
2394 var->saved_item = 0;
2395
2396 return var;
2397 }
2398
2399 /* Allocate memory and initialize a new root variable. */
2400 static struct varobj *
2401 new_root_variable (void)
2402 {
2403 struct varobj *var = new_variable ();
2404
2405 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2406 var->root->lang = NULL;
2407 var->root->exp = NULL;
2408 var->root->valid_block = NULL;
2409 var->root->frame = null_frame_id;
2410 var->root->floating = 0;
2411 var->root->rootvar = NULL;
2412 var->root->is_valid = 1;
2413
2414 return var;
2415 }
2416
2417 /* Free any allocated memory associated with VAR. */
2418 static void
2419 free_variable (struct varobj *var)
2420 {
2421 #if HAVE_PYTHON
2422 if (var->pretty_printer)
2423 {
2424 struct cleanup *cleanup = varobj_ensure_python_env (var);
2425 Py_XDECREF (var->constructor);
2426 Py_XDECREF (var->pretty_printer);
2427 Py_XDECREF (var->child_iter);
2428 Py_XDECREF (var->saved_item);
2429 do_cleanups (cleanup);
2430 }
2431 #endif
2432
2433 value_free (var->value);
2434
2435 /* Free the expression if this is a root variable. */
2436 if (is_root_p (var))
2437 {
2438 xfree (var->root->exp);
2439 xfree (var->root);
2440 }
2441
2442 xfree (var->name);
2443 xfree (var->obj_name);
2444 xfree (var->print_value);
2445 xfree (var->path_expr);
2446 xfree (var);
2447 }
2448
2449 static void
2450 do_free_variable_cleanup (void *var)
2451 {
2452 free_variable (var);
2453 }
2454
2455 static struct cleanup *
2456 make_cleanup_free_variable (struct varobj *var)
2457 {
2458 return make_cleanup (do_free_variable_cleanup, var);
2459 }
2460
2461 /* This returns the type of the variable. It also skips past typedefs
2462 to return the real type of the variable.
2463
2464 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2465 except within get_target_type and get_type. */
2466 static struct type *
2467 get_type (struct varobj *var)
2468 {
2469 struct type *type;
2470
2471 type = var->type;
2472 if (type != NULL)
2473 type = check_typedef (type);
2474
2475 return type;
2476 }
2477
2478 /* Return the type of the value that's stored in VAR,
2479 or that would have being stored there if the
2480 value were accessible.
2481
2482 This differs from VAR->type in that VAR->type is always
2483 the true type of the expession in the source language.
2484 The return value of this function is the type we're
2485 actually storing in varobj, and using for displaying
2486 the values and for comparing previous and new values.
2487
2488 For example, top-level references are always stripped. */
2489 static struct type *
2490 get_value_type (struct varobj *var)
2491 {
2492 struct type *type;
2493
2494 if (var->value)
2495 type = value_type (var->value);
2496 else
2497 type = var->type;
2498
2499 type = check_typedef (type);
2500
2501 if (TYPE_CODE (type) == TYPE_CODE_REF)
2502 type = get_target_type (type);
2503
2504 type = check_typedef (type);
2505
2506 return type;
2507 }
2508
2509 /* This returns the target type (or NULL) of TYPE, also skipping
2510 past typedefs, just like get_type ().
2511
2512 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2513 except within get_target_type and get_type. */
2514 static struct type *
2515 get_target_type (struct type *type)
2516 {
2517 if (type != NULL)
2518 {
2519 type = TYPE_TARGET_TYPE (type);
2520 if (type != NULL)
2521 type = check_typedef (type);
2522 }
2523
2524 return type;
2525 }
2526
2527 /* What is the default display for this variable? We assume that
2528 everything is "natural". Any exceptions? */
2529 static enum varobj_display_formats
2530 variable_default_display (struct varobj *var)
2531 {
2532 return FORMAT_NATURAL;
2533 }
2534
2535 /* FIXME: The following should be generic for any pointer. */
2536 static void
2537 cppush (struct cpstack **pstack, char *name)
2538 {
2539 struct cpstack *s;
2540
2541 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2542 s->name = name;
2543 s->next = *pstack;
2544 *pstack = s;
2545 }
2546
2547 /* FIXME: The following should be generic for any pointer. */
2548 static char *
2549 cppop (struct cpstack **pstack)
2550 {
2551 struct cpstack *s;
2552 char *v;
2553
2554 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2555 return NULL;
2556
2557 s = *pstack;
2558 v = s->name;
2559 *pstack = (*pstack)->next;
2560 xfree (s);
2561
2562 return v;
2563 }
2564 \f
2565 /*
2566 * Language-dependencies
2567 */
2568
2569 /* Common entry points */
2570
2571 /* Get the language of variable VAR. */
2572 static enum varobj_languages
2573 variable_language (struct varobj *var)
2574 {
2575 enum varobj_languages lang;
2576
2577 switch (var->root->exp->language_defn->la_language)
2578 {
2579 default:
2580 case language_c:
2581 lang = vlang_c;
2582 break;
2583 case language_cplus:
2584 lang = vlang_cplus;
2585 break;
2586 case language_java:
2587 lang = vlang_java;
2588 break;
2589 case language_ada:
2590 lang = vlang_ada;
2591 break;
2592 }
2593
2594 return lang;
2595 }
2596
2597 /* Return the number of children for a given variable.
2598 The result of this function is defined by the language
2599 implementation. The number of children returned by this function
2600 is the number of children that the user will see in the variable
2601 display. */
2602 static int
2603 number_of_children (struct varobj *var)
2604 {
2605 return (*var->root->lang->number_of_children) (var);
2606 }
2607
2608 /* What is the expression for the root varobj VAR? Returns a malloc'd
2609 string. */
2610 static char *
2611 name_of_variable (struct varobj *var)
2612 {
2613 return (*var->root->lang->name_of_variable) (var);
2614 }
2615
2616 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2617 string. */
2618 static char *
2619 name_of_child (struct varobj *var, int index)
2620 {
2621 return (*var->root->lang->name_of_child) (var, index);
2622 }
2623
2624 /* What is the ``struct value *'' of the root variable VAR?
2625 For floating variable object, evaluation can get us a value
2626 of different type from what is stored in varobj already. In
2627 that case:
2628 - *type_changed will be set to 1
2629 - old varobj will be freed, and new one will be
2630 created, with the same name.
2631 - *var_handle will be set to the new varobj
2632 Otherwise, *type_changed will be set to 0. */
2633 static struct value *
2634 value_of_root (struct varobj **var_handle, int *type_changed)
2635 {
2636 struct varobj *var;
2637
2638 if (var_handle == NULL)
2639 return NULL;
2640
2641 var = *var_handle;
2642
2643 /* This should really be an exception, since this should
2644 only get called with a root variable. */
2645
2646 if (!is_root_p (var))
2647 return NULL;
2648
2649 if (var->root->floating)
2650 {
2651 struct varobj *tmp_var;
2652 char *old_type, *new_type;
2653
2654 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2655 USE_SELECTED_FRAME);
2656 if (tmp_var == NULL)
2657 {
2658 return NULL;
2659 }
2660 old_type = varobj_get_type (var);
2661 new_type = varobj_get_type (tmp_var);
2662 if (strcmp (old_type, new_type) == 0)
2663 {
2664 /* The expression presently stored inside var->root->exp
2665 remembers the locations of local variables relatively to
2666 the frame where the expression was created (in DWARF location
2667 button, for example). Naturally, those locations are not
2668 correct in other frames, so update the expression. */
2669
2670 struct expression *tmp_exp = var->root->exp;
2671
2672 var->root->exp = tmp_var->root->exp;
2673 tmp_var->root->exp = tmp_exp;
2674
2675 varobj_delete (tmp_var, NULL, 0);
2676 *type_changed = 0;
2677 }
2678 else
2679 {
2680 tmp_var->obj_name = xstrdup (var->obj_name);
2681 tmp_var->from = var->from;
2682 tmp_var->to = var->to;
2683 varobj_delete (var, NULL, 0);
2684
2685 install_variable (tmp_var);
2686 *var_handle = tmp_var;
2687 var = *var_handle;
2688 *type_changed = 1;
2689 }
2690 xfree (old_type);
2691 xfree (new_type);
2692 }
2693 else
2694 {
2695 *type_changed = 0;
2696 }
2697
2698 {
2699 struct value *value;
2700
2701 value = (*var->root->lang->value_of_root) (var_handle);
2702 if (var->value == NULL || value == NULL)
2703 {
2704 /* For root varobj-s, a NULL value indicates a scoping issue.
2705 So, nothing to do in terms of checking for mutations. */
2706 }
2707 else if (varobj_value_has_mutated (var, value, value_type (value)))
2708 {
2709 /* The type has mutated, so the children are no longer valid.
2710 Just delete them, and tell our caller that the type has
2711 changed. */
2712 varobj_delete (var, NULL, 1 /* only_children */);
2713 var->num_children = -1;
2714 var->to = -1;
2715 var->from = -1;
2716 *type_changed = 1;
2717 }
2718 return value;
2719 }
2720 }
2721
2722 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2723 static struct value *
2724 value_of_child (struct varobj *parent, int index)
2725 {
2726 struct value *value;
2727
2728 value = (*parent->root->lang->value_of_child) (parent, index);
2729
2730 return value;
2731 }
2732
2733 /* GDB already has a command called "value_of_variable". Sigh. */
2734 static char *
2735 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2736 {
2737 if (var->root->is_valid)
2738 {
2739 if (var->pretty_printer)
2740 return value_get_print_value (var->value, var->format, var);
2741 return (*var->root->lang->value_of_variable) (var, format);
2742 }
2743 else
2744 return NULL;
2745 }
2746
2747 static char *
2748 value_get_print_value (struct value *value, enum varobj_display_formats format,
2749 struct varobj *var)
2750 {
2751 struct ui_file *stb;
2752 struct cleanup *old_chain;
2753 gdb_byte *thevalue = NULL;
2754 struct value_print_options opts;
2755 struct type *type = NULL;
2756 long len = 0;
2757 char *encoding = NULL;
2758 struct gdbarch *gdbarch = NULL;
2759 /* Initialize it just to avoid a GCC false warning. */
2760 CORE_ADDR str_addr = 0;
2761 int string_print = 0;
2762
2763 if (value == NULL)
2764 return NULL;
2765
2766 stb = mem_fileopen ();
2767 old_chain = make_cleanup_ui_file_delete (stb);
2768
2769 gdbarch = get_type_arch (value_type (value));
2770 #if HAVE_PYTHON
2771 {
2772 PyObject *value_formatter = var->pretty_printer;
2773
2774 varobj_ensure_python_env (var);
2775
2776 if (value_formatter)
2777 {
2778 /* First check to see if we have any children at all. If so,
2779 we simply return {...}. */
2780 if (dynamic_varobj_has_child_method (var))
2781 {
2782 do_cleanups (old_chain);
2783 return xstrdup ("{...}");
2784 }
2785
2786 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2787 {
2788 struct value *replacement;
2789 PyObject *output = NULL;
2790
2791 output = apply_varobj_pretty_printer (value_formatter,
2792 &replacement,
2793 stb);
2794
2795 /* If we have string like output ... */
2796 if (output)
2797 {
2798 make_cleanup_py_decref (output);
2799
2800 /* If this is a lazy string, extract it. For lazy
2801 strings we always print as a string, so set
2802 string_print. */
2803 if (gdbpy_is_lazy_string (output))
2804 {
2805 gdbpy_extract_lazy_string (output, &str_addr, &type,
2806 &len, &encoding);
2807 make_cleanup (free_current_contents, &encoding);
2808 string_print = 1;
2809 }
2810 else
2811 {
2812 /* If it is a regular (non-lazy) string, extract
2813 it and copy the contents into THEVALUE. If the
2814 hint says to print it as a string, set
2815 string_print. Otherwise just return the extracted
2816 string as a value. */
2817
2818 PyObject *py_str
2819 = python_string_to_target_python_string (output);
2820
2821 if (py_str)
2822 {
2823 char *s = PyString_AsString (py_str);
2824 char *hint;
2825
2826 hint = gdbpy_get_display_hint (value_formatter);
2827 if (hint)
2828 {
2829 if (!strcmp (hint, "string"))
2830 string_print = 1;
2831 xfree (hint);
2832 }
2833
2834 len = PyString_Size (py_str);
2835 thevalue = xmemdup (s, len + 1, len + 1);
2836 type = builtin_type (gdbarch)->builtin_char;
2837 Py_DECREF (py_str);
2838
2839 if (!string_print)
2840 {
2841 do_cleanups (old_chain);
2842 return thevalue;
2843 }
2844
2845 make_cleanup (xfree, thevalue);
2846 }
2847 else
2848 gdbpy_print_stack ();
2849 }
2850 }
2851 /* If the printer returned a replacement value, set VALUE
2852 to REPLACEMENT. If there is not a replacement value,
2853 just use the value passed to this function. */
2854 if (replacement)
2855 value = replacement;
2856 }
2857 }
2858 }
2859 #endif
2860
2861 get_formatted_print_options (&opts, format_code[(int) format]);
2862 opts.deref_ref = 0;
2863 opts.raw = 1;
2864
2865 /* If the THEVALUE has contents, it is a regular string. */
2866 if (thevalue)
2867 LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2868 else if (string_print)
2869 /* Otherwise, if string_print is set, and it is not a regular
2870 string, it is a lazy string. */
2871 val_print_string (type, encoding, str_addr, len, stb, &opts);
2872 else
2873 /* All other cases. */
2874 common_val_print (value, stb, 0, &opts, current_language);
2875
2876 thevalue = ui_file_xstrdup (stb, NULL);
2877
2878 do_cleanups (old_chain);
2879 return thevalue;
2880 }
2881
2882 int
2883 varobj_editable_p (struct varobj *var)
2884 {
2885 struct type *type;
2886
2887 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2888 return 0;
2889
2890 type = get_value_type (var);
2891
2892 switch (TYPE_CODE (type))
2893 {
2894 case TYPE_CODE_STRUCT:
2895 case TYPE_CODE_UNION:
2896 case TYPE_CODE_ARRAY:
2897 case TYPE_CODE_FUNC:
2898 case TYPE_CODE_METHOD:
2899 return 0;
2900 break;
2901
2902 default:
2903 return 1;
2904 break;
2905 }
2906 }
2907
2908 /* Return non-zero if changes in value of VAR
2909 must be detected and reported by -var-update.
2910 Return zero is -var-update should never report
2911 changes of such values. This makes sense for structures
2912 (since the changes in children values will be reported separately),
2913 or for artifical objects (like 'public' pseudo-field in C++).
2914
2915 Return value of 0 means that gdb need not call value_fetch_lazy
2916 for the value of this variable object. */
2917 static int
2918 varobj_value_is_changeable_p (struct varobj *var)
2919 {
2920 int r;
2921 struct type *type;
2922
2923 if (CPLUS_FAKE_CHILD (var))
2924 return 0;
2925
2926 /* FIXME: This, and the check above, show that this routine
2927 should be language-specific. */
2928 if (variable_language (var) == vlang_ada)
2929 {
2930 struct type *type = var->value ? value_type (var->value) : var->type;
2931
2932 if (ada_is_array_descriptor_type (type)
2933 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2934 {
2935 /* This is in reality a pointer to an unconstrained array.
2936 its value is changeable. */
2937 return 1;
2938 }
2939
2940 if (ada_is_string_type (type))
2941 {
2942 /* We display the contents of the string in the array's
2943 "value" field. The contents can change, so consider
2944 that the array is changeable. */
2945 return 1;
2946 }
2947 }
2948
2949 type = get_value_type (var);
2950
2951 switch (TYPE_CODE (type))
2952 {
2953 case TYPE_CODE_STRUCT:
2954 case TYPE_CODE_UNION:
2955 case TYPE_CODE_ARRAY:
2956 r = 0;
2957 break;
2958
2959 default:
2960 r = 1;
2961 }
2962
2963 return r;
2964 }
2965
2966 /* Return 1 if that varobj is floating, that is is always evaluated in the
2967 selected frame, and not bound to thread/frame. Such variable objects
2968 are created using '@' as frame specifier to -var-create. */
2969 int
2970 varobj_floating_p (struct varobj *var)
2971 {
2972 return var->root->floating;
2973 }
2974
2975 /* Given the value and the type of a variable object,
2976 adjust the value and type to those necessary
2977 for getting children of the variable object.
2978 This includes dereferencing top-level references
2979 to all types and dereferencing pointers to
2980 structures.
2981
2982 Both TYPE and *TYPE should be non-null. VALUE
2983 can be null if we want to only translate type.
2984 *VALUE can be null as well -- if the parent
2985 value is not known.
2986
2987 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2988 depending on whether pointer was dereferenced
2989 in this function. */
2990 static void
2991 adjust_value_for_child_access (struct value **value,
2992 struct type **type,
2993 int *was_ptr)
2994 {
2995 gdb_assert (type && *type);
2996
2997 if (was_ptr)
2998 *was_ptr = 0;
2999
3000 *type = check_typedef (*type);
3001
3002 /* The type of value stored in varobj, that is passed
3003 to us, is already supposed to be
3004 reference-stripped. */
3005
3006 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
3007
3008 /* Pointers to structures are treated just like
3009 structures when accessing children. Don't
3010 dererences pointers to other types. */
3011 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
3012 {
3013 struct type *target_type = get_target_type (*type);
3014 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
3015 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
3016 {
3017 if (value && *value)
3018 {
3019 volatile struct gdb_exception except;
3020
3021 TRY_CATCH (except, RETURN_MASK_ERROR)
3022 {
3023 *value = value_ind (*value);
3024 }
3025
3026 if (except.reason < 0)
3027 *value = NULL;
3028 }
3029 *type = target_type;
3030 if (was_ptr)
3031 *was_ptr = 1;
3032 }
3033 }
3034
3035 /* The 'get_target_type' function calls check_typedef on
3036 result, so we can immediately check type code. No
3037 need to call check_typedef here. */
3038 }
3039
3040 /* C */
3041 static int
3042 c_number_of_children (struct varobj *var)
3043 {
3044 struct type *type = get_value_type (var);
3045 int children = 0;
3046 struct type *target;
3047
3048 adjust_value_for_child_access (NULL, &type, NULL);
3049 target = get_target_type (type);
3050
3051 switch (TYPE_CODE (type))
3052 {
3053 case TYPE_CODE_ARRAY:
3054 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
3055 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3056 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3057 else
3058 /* If we don't know how many elements there are, don't display
3059 any. */
3060 children = 0;
3061 break;
3062
3063 case TYPE_CODE_STRUCT:
3064 case TYPE_CODE_UNION:
3065 children = TYPE_NFIELDS (type);
3066 break;
3067
3068 case TYPE_CODE_PTR:
3069 /* The type here is a pointer to non-struct. Typically, pointers
3070 have one child, except for function ptrs, which have no children,
3071 and except for void*, as we don't know what to show.
3072
3073 We can show char* so we allow it to be dereferenced. If you decide
3074 to test for it, please mind that a little magic is necessary to
3075 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3076 TYPE_NAME == "char". */
3077 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3078 || TYPE_CODE (target) == TYPE_CODE_VOID)
3079 children = 0;
3080 else
3081 children = 1;
3082 break;
3083
3084 default:
3085 /* Other types have no children. */
3086 break;
3087 }
3088
3089 return children;
3090 }
3091
3092 static char *
3093 c_name_of_variable (struct varobj *parent)
3094 {
3095 return xstrdup (parent->name);
3096 }
3097
3098 /* Return the value of element TYPE_INDEX of a structure
3099 value VALUE. VALUE's type should be a structure,
3100 or union, or a typedef to struct/union.
3101
3102 Returns NULL if getting the value fails. Never throws. */
3103 static struct value *
3104 value_struct_element_index (struct value *value, int type_index)
3105 {
3106 struct value *result = NULL;
3107 volatile struct gdb_exception e;
3108 struct type *type = value_type (value);
3109
3110 type = check_typedef (type);
3111
3112 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3113 || TYPE_CODE (type) == TYPE_CODE_UNION);
3114
3115 TRY_CATCH (e, RETURN_MASK_ERROR)
3116 {
3117 if (field_is_static (&TYPE_FIELD (type, type_index)))
3118 result = value_static_field (type, type_index);
3119 else
3120 result = value_primitive_field (value, 0, type_index, type);
3121 }
3122 if (e.reason < 0)
3123 {
3124 return NULL;
3125 }
3126 else
3127 {
3128 return result;
3129 }
3130 }
3131
3132 /* Obtain the information about child INDEX of the variable
3133 object PARENT.
3134 If CNAME is not null, sets *CNAME to the name of the child relative
3135 to the parent.
3136 If CVALUE is not null, sets *CVALUE to the value of the child.
3137 If CTYPE is not null, sets *CTYPE to the type of the child.
3138
3139 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3140 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3141 to NULL. */
3142 static void
3143 c_describe_child (struct varobj *parent, int index,
3144 char **cname, struct value **cvalue, struct type **ctype,
3145 char **cfull_expression)
3146 {
3147 struct value *value = parent->value;
3148 struct type *type = get_value_type (parent);
3149 char *parent_expression = NULL;
3150 int was_ptr;
3151 volatile struct gdb_exception except;
3152
3153 if (cname)
3154 *cname = NULL;
3155 if (cvalue)
3156 *cvalue = NULL;
3157 if (ctype)
3158 *ctype = NULL;
3159 if (cfull_expression)
3160 {
3161 *cfull_expression = NULL;
3162 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3163 }
3164 adjust_value_for_child_access (&value, &type, &was_ptr);
3165
3166 switch (TYPE_CODE (type))
3167 {
3168 case TYPE_CODE_ARRAY:
3169 if (cname)
3170 *cname
3171 = xstrdup (int_string (index
3172 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3173 10, 1, 0, 0));
3174
3175 if (cvalue && value)
3176 {
3177 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3178
3179 TRY_CATCH (except, RETURN_MASK_ERROR)
3180 {
3181 *cvalue = value_subscript (value, real_index);
3182 }
3183 }
3184
3185 if (ctype)
3186 *ctype = get_target_type (type);
3187
3188 if (cfull_expression)
3189 *cfull_expression =
3190 xstrprintf ("(%s)[%s]", parent_expression,
3191 int_string (index
3192 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3193 10, 1, 0, 0));
3194
3195
3196 break;
3197
3198 case TYPE_CODE_STRUCT:
3199 case TYPE_CODE_UNION:
3200 {
3201 const char *field_name;
3202
3203 /* If the type is anonymous and the field has no name,
3204 set an appropriate name. */
3205 field_name = TYPE_FIELD_NAME (type, index);
3206 if (field_name == NULL || *field_name == '\0')
3207 {
3208 if (cname)
3209 {
3210 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3211 == TYPE_CODE_STRUCT)
3212 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3213 else
3214 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3215 }
3216
3217 if (cfull_expression)
3218 *cfull_expression = xstrdup ("");
3219 }
3220 else
3221 {
3222 if (cname)
3223 *cname = xstrdup (field_name);
3224
3225 if (cfull_expression)
3226 {
3227 char *join = was_ptr ? "->" : ".";
3228
3229 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3230 join, field_name);
3231 }
3232 }
3233
3234 if (cvalue && value)
3235 {
3236 /* For C, varobj index is the same as type index. */
3237 *cvalue = value_struct_element_index (value, index);
3238 }
3239
3240 if (ctype)
3241 *ctype = TYPE_FIELD_TYPE (type, index);
3242 }
3243 break;
3244
3245 case TYPE_CODE_PTR:
3246 if (cname)
3247 *cname = xstrprintf ("*%s", parent->name);
3248
3249 if (cvalue && value)
3250 {
3251 TRY_CATCH (except, RETURN_MASK_ERROR)
3252 {
3253 *cvalue = value_ind (value);
3254 }
3255
3256 if (except.reason < 0)
3257 *cvalue = NULL;
3258 }
3259
3260 /* Don't use get_target_type because it calls
3261 check_typedef and here, we want to show the true
3262 declared type of the variable. */
3263 if (ctype)
3264 *ctype = TYPE_TARGET_TYPE (type);
3265
3266 if (cfull_expression)
3267 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3268
3269 break;
3270
3271 default:
3272 /* This should not happen. */
3273 if (cname)
3274 *cname = xstrdup ("???");
3275 if (cfull_expression)
3276 *cfull_expression = xstrdup ("???");
3277 /* Don't set value and type, we don't know then. */
3278 }
3279 }
3280
3281 static char *
3282 c_name_of_child (struct varobj *parent, int index)
3283 {
3284 char *name;
3285
3286 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3287 return name;
3288 }
3289
3290 static char *
3291 c_path_expr_of_child (struct varobj *child)
3292 {
3293 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3294 &child->path_expr);
3295 return child->path_expr;
3296 }
3297
3298 /* If frame associated with VAR can be found, switch
3299 to it and return 1. Otherwise, return 0. */
3300 static int
3301 check_scope (struct varobj *var)
3302 {
3303 struct frame_info *fi;
3304 int scope;
3305
3306 fi = frame_find_by_id (var->root->frame);
3307 scope = fi != NULL;
3308
3309 if (fi)
3310 {
3311 CORE_ADDR pc = get_frame_pc (fi);
3312
3313 if (pc < BLOCK_START (var->root->valid_block) ||
3314 pc >= BLOCK_END (var->root->valid_block))
3315 scope = 0;
3316 else
3317 select_frame (fi);
3318 }
3319 return scope;
3320 }
3321
3322 static struct value *
3323 c_value_of_root (struct varobj **var_handle)
3324 {
3325 struct value *new_val = NULL;
3326 struct varobj *var = *var_handle;
3327 int within_scope = 0;
3328 struct cleanup *back_to;
3329
3330 /* Only root variables can be updated... */
3331 if (!is_root_p (var))
3332 /* Not a root var. */
3333 return NULL;
3334
3335 back_to = make_cleanup_restore_current_thread ();
3336
3337 /* Determine whether the variable is still around. */
3338 if (var->root->valid_block == NULL || var->root->floating)
3339 within_scope = 1;
3340 else if (var->root->thread_id == 0)
3341 {
3342 /* The program was single-threaded when the variable object was
3343 created. Technically, it's possible that the program became
3344 multi-threaded since then, but we don't support such
3345 scenario yet. */
3346 within_scope = check_scope (var);
3347 }
3348 else
3349 {
3350 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
3351 if (in_thread_list (ptid))
3352 {
3353 switch_to_thread (ptid);
3354 within_scope = check_scope (var);
3355 }
3356 }
3357
3358 if (within_scope)
3359 {
3360 volatile struct gdb_exception except;
3361
3362 /* We need to catch errors here, because if evaluate
3363 expression fails we want to just return NULL. */
3364 TRY_CATCH (except, RETURN_MASK_ERROR)
3365 {
3366 new_val = evaluate_expression (var->root->exp);
3367 }
3368
3369 return new_val;
3370 }
3371
3372 do_cleanups (back_to);
3373
3374 return NULL;
3375 }
3376
3377 static struct value *
3378 c_value_of_child (struct varobj *parent, int index)
3379 {
3380 struct value *value = NULL;
3381
3382 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3383 return value;
3384 }
3385
3386 static struct type *
3387 c_type_of_child (struct varobj *parent, int index)
3388 {
3389 struct type *type = NULL;
3390
3391 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3392 return type;
3393 }
3394
3395 static char *
3396 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3397 {
3398 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3399 it will print out its children instead of "{...}". So we need to
3400 catch that case explicitly. */
3401 struct type *type = get_type (var);
3402
3403 /* Strip top-level references. */
3404 while (TYPE_CODE (type) == TYPE_CODE_REF)
3405 type = check_typedef (TYPE_TARGET_TYPE (type));
3406
3407 switch (TYPE_CODE (type))
3408 {
3409 case TYPE_CODE_STRUCT:
3410 case TYPE_CODE_UNION:
3411 return xstrdup ("{...}");
3412 /* break; */
3413
3414 case TYPE_CODE_ARRAY:
3415 {
3416 char *number;
3417
3418 number = xstrprintf ("[%d]", var->num_children);
3419 return (number);
3420 }
3421 /* break; */
3422
3423 default:
3424 {
3425 if (var->value == NULL)
3426 {
3427 /* This can happen if we attempt to get the value of a struct
3428 member when the parent is an invalid pointer. This is an
3429 error condition, so we should tell the caller. */
3430 return NULL;
3431 }
3432 else
3433 {
3434 if (var->not_fetched && value_lazy (var->value))
3435 /* Frozen variable and no value yet. We don't
3436 implicitly fetch the value. MI response will
3437 use empty string for the value, which is OK. */
3438 return NULL;
3439
3440 gdb_assert (varobj_value_is_changeable_p (var));
3441 gdb_assert (!value_lazy (var->value));
3442
3443 /* If the specified format is the current one,
3444 we can reuse print_value. */
3445 if (format == var->format)
3446 return xstrdup (var->print_value);
3447 else
3448 return value_get_print_value (var->value, format, var);
3449 }
3450 }
3451 }
3452 }
3453 \f
3454
3455 /* C++ */
3456
3457 static int
3458 cplus_number_of_children (struct varobj *var)
3459 {
3460 struct type *type;
3461 int children, dont_know;
3462
3463 dont_know = 1;
3464 children = 0;
3465
3466 if (!CPLUS_FAKE_CHILD (var))
3467 {
3468 type = get_value_type (var);
3469 adjust_value_for_child_access (NULL, &type, NULL);
3470
3471 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3472 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3473 {
3474 int kids[3];
3475
3476 cplus_class_num_children (type, kids);
3477 if (kids[v_public] != 0)
3478 children++;
3479 if (kids[v_private] != 0)
3480 children++;
3481 if (kids[v_protected] != 0)
3482 children++;
3483
3484 /* Add any baseclasses. */
3485 children += TYPE_N_BASECLASSES (type);
3486 dont_know = 0;
3487
3488 /* FIXME: save children in var. */
3489 }
3490 }
3491 else
3492 {
3493 int kids[3];
3494
3495 type = get_value_type (var->parent);
3496 adjust_value_for_child_access (NULL, &type, NULL);
3497
3498 cplus_class_num_children (type, kids);
3499 if (strcmp (var->name, "public") == 0)
3500 children = kids[v_public];
3501 else if (strcmp (var->name, "private") == 0)
3502 children = kids[v_private];
3503 else
3504 children = kids[v_protected];
3505 dont_know = 0;
3506 }
3507
3508 if (dont_know)
3509 children = c_number_of_children (var);
3510
3511 return children;
3512 }
3513
3514 /* Compute # of public, private, and protected variables in this class.
3515 That means we need to descend into all baseclasses and find out
3516 how many are there, too. */
3517 static void
3518 cplus_class_num_children (struct type *type, int children[3])
3519 {
3520 int i, vptr_fieldno;
3521 struct type *basetype = NULL;
3522
3523 children[v_public] = 0;
3524 children[v_private] = 0;
3525 children[v_protected] = 0;
3526
3527 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3528 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3529 {
3530 /* If we have a virtual table pointer, omit it. Even if virtual
3531 table pointers are not specifically marked in the debug info,
3532 they should be artificial. */
3533 if ((type == basetype && i == vptr_fieldno)
3534 || TYPE_FIELD_ARTIFICIAL (type, i))
3535 continue;
3536
3537 if (TYPE_FIELD_PROTECTED (type, i))
3538 children[v_protected]++;
3539 else if (TYPE_FIELD_PRIVATE (type, i))
3540 children[v_private]++;
3541 else
3542 children[v_public]++;
3543 }
3544 }
3545
3546 static char *
3547 cplus_name_of_variable (struct varobj *parent)
3548 {
3549 return c_name_of_variable (parent);
3550 }
3551
3552 enum accessibility { private_field, protected_field, public_field };
3553
3554 /* Check if field INDEX of TYPE has the specified accessibility.
3555 Return 0 if so and 1 otherwise. */
3556 static int
3557 match_accessibility (struct type *type, int index, enum accessibility acc)
3558 {
3559 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3560 return 1;
3561 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3562 return 1;
3563 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3564 && !TYPE_FIELD_PROTECTED (type, index))
3565 return 1;
3566 else
3567 return 0;
3568 }
3569
3570 static void
3571 cplus_describe_child (struct varobj *parent, int index,
3572 char **cname, struct value **cvalue, struct type **ctype,
3573 char **cfull_expression)
3574 {
3575 struct value *value;
3576 struct type *type;
3577 int was_ptr;
3578 char *parent_expression = NULL;
3579
3580 if (cname)
3581 *cname = NULL;
3582 if (cvalue)
3583 *cvalue = NULL;
3584 if (ctype)
3585 *ctype = NULL;
3586 if (cfull_expression)
3587 *cfull_expression = NULL;
3588
3589 if (CPLUS_FAKE_CHILD (parent))
3590 {
3591 value = parent->parent->value;
3592 type = get_value_type (parent->parent);
3593 if (cfull_expression)
3594 parent_expression
3595 = varobj_get_path_expr (get_path_expr_parent (parent->parent));
3596 }
3597 else
3598 {
3599 value = parent->value;
3600 type = get_value_type (parent);
3601 if (cfull_expression)
3602 parent_expression
3603 = varobj_get_path_expr (get_path_expr_parent (parent));
3604 }
3605
3606 adjust_value_for_child_access (&value, &type, &was_ptr);
3607
3608 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3609 || TYPE_CODE (type) == TYPE_CODE_UNION)
3610 {
3611 char *join = was_ptr ? "->" : ".";
3612
3613 if (CPLUS_FAKE_CHILD (parent))
3614 {
3615 /* The fields of the class type are ordered as they
3616 appear in the class. We are given an index for a
3617 particular access control type ("public","protected",
3618 or "private"). We must skip over fields that don't
3619 have the access control we are looking for to properly
3620 find the indexed field. */
3621 int type_index = TYPE_N_BASECLASSES (type);
3622 enum accessibility acc = public_field;
3623 int vptr_fieldno;
3624 struct type *basetype = NULL;
3625 const char *field_name;
3626
3627 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3628 if (strcmp (parent->name, "private") == 0)
3629 acc = private_field;
3630 else if (strcmp (parent->name, "protected") == 0)
3631 acc = protected_field;
3632
3633 while (index >= 0)
3634 {
3635 if ((type == basetype && type_index == vptr_fieldno)
3636 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3637 ; /* ignore vptr */
3638 else if (match_accessibility (type, type_index, acc))
3639 --index;
3640 ++type_index;
3641 }
3642 --type_index;
3643
3644 /* If the type is anonymous and the field has no name,
3645 set an appopriate name. */
3646 field_name = TYPE_FIELD_NAME (type, type_index);
3647 if (field_name == NULL || *field_name == '\0')
3648 {
3649 if (cname)
3650 {
3651 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3652 == TYPE_CODE_STRUCT)
3653 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3654 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3655 == TYPE_CODE_UNION)
3656 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3657 }
3658
3659 if (cfull_expression)
3660 *cfull_expression = xstrdup ("");
3661 }
3662 else
3663 {
3664 if (cname)
3665 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3666
3667 if (cfull_expression)
3668 *cfull_expression
3669 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3670 field_name);
3671 }
3672
3673 if (cvalue && value)
3674 *cvalue = value_struct_element_index (value, type_index);
3675
3676 if (ctype)
3677 *ctype = TYPE_FIELD_TYPE (type, type_index);
3678 }
3679 else if (index < TYPE_N_BASECLASSES (type))
3680 {
3681 /* This is a baseclass. */
3682 if (cname)
3683 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3684
3685 if (cvalue && value)
3686 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3687
3688 if (ctype)
3689 {
3690 *ctype = TYPE_FIELD_TYPE (type, index);
3691 }
3692
3693 if (cfull_expression)
3694 {
3695 char *ptr = was_ptr ? "*" : "";
3696
3697 /* Cast the parent to the base' type. Note that in gdb,
3698 expression like
3699 (Base1)d
3700 will create an lvalue, for all appearences, so we don't
3701 need to use more fancy:
3702 *(Base1*)(&d)
3703 construct.
3704
3705 When we are in the scope of the base class or of one
3706 of its children, the type field name will be interpreted
3707 as a constructor, if it exists. Therefore, we must
3708 indicate that the name is a class name by using the
3709 'class' keyword. See PR mi/11912 */
3710 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3711 ptr,
3712 TYPE_FIELD_NAME (type, index),
3713 ptr,
3714 parent_expression);
3715 }
3716 }
3717 else
3718 {
3719 char *access = NULL;
3720 int children[3];
3721
3722 cplus_class_num_children (type, children);
3723
3724 /* Everything beyond the baseclasses can
3725 only be "public", "private", or "protected"
3726
3727 The special "fake" children are always output by varobj in
3728 this order. So if INDEX == 2, it MUST be "protected". */
3729 index -= TYPE_N_BASECLASSES (type);
3730 switch (index)
3731 {
3732 case 0:
3733 if (children[v_public] > 0)
3734 access = "public";
3735 else if (children[v_private] > 0)
3736 access = "private";
3737 else
3738 access = "protected";
3739 break;
3740 case 1:
3741 if (children[v_public] > 0)
3742 {
3743 if (children[v_private] > 0)
3744 access = "private";
3745 else
3746 access = "protected";
3747 }
3748 else if (children[v_private] > 0)
3749 access = "protected";
3750 break;
3751 case 2:
3752 /* Must be protected. */
3753 access = "protected";
3754 break;
3755 default:
3756 /* error! */
3757 break;
3758 }
3759
3760 gdb_assert (access);
3761 if (cname)
3762 *cname = xstrdup (access);
3763
3764 /* Value and type and full expression are null here. */
3765 }
3766 }
3767 else
3768 {
3769 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3770 }
3771 }
3772
3773 static char *
3774 cplus_name_of_child (struct varobj *parent, int index)
3775 {
3776 char *name = NULL;
3777
3778 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3779 return name;
3780 }
3781
3782 static char *
3783 cplus_path_expr_of_child (struct varobj *child)
3784 {
3785 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3786 &child->path_expr);
3787 return child->path_expr;
3788 }
3789
3790 static struct value *
3791 cplus_value_of_root (struct varobj **var_handle)
3792 {
3793 return c_value_of_root (var_handle);
3794 }
3795
3796 static struct value *
3797 cplus_value_of_child (struct varobj *parent, int index)
3798 {
3799 struct value *value = NULL;
3800
3801 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3802 return value;
3803 }
3804
3805 static struct type *
3806 cplus_type_of_child (struct varobj *parent, int index)
3807 {
3808 struct type *type = NULL;
3809
3810 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3811 return type;
3812 }
3813
3814 static char *
3815 cplus_value_of_variable (struct varobj *var,
3816 enum varobj_display_formats format)
3817 {
3818
3819 /* If we have one of our special types, don't print out
3820 any value. */
3821 if (CPLUS_FAKE_CHILD (var))
3822 return xstrdup ("");
3823
3824 return c_value_of_variable (var, format);
3825 }
3826 \f
3827 /* Java */
3828
3829 static int
3830 java_number_of_children (struct varobj *var)
3831 {
3832 return cplus_number_of_children (var);
3833 }
3834
3835 static char *
3836 java_name_of_variable (struct varobj *parent)
3837 {
3838 char *p, *name;
3839
3840 name = cplus_name_of_variable (parent);
3841 /* If the name has "-" in it, it is because we
3842 needed to escape periods in the name... */
3843 p = name;
3844
3845 while (*p != '\000')
3846 {
3847 if (*p == '-')
3848 *p = '.';
3849 p++;
3850 }
3851
3852 return name;
3853 }
3854
3855 static char *
3856 java_name_of_child (struct varobj *parent, int index)
3857 {
3858 char *name, *p;
3859
3860 name = cplus_name_of_child (parent, index);
3861 /* Escape any periods in the name... */
3862 p = name;
3863
3864 while (*p != '\000')
3865 {
3866 if (*p == '.')
3867 *p = '-';
3868 p++;
3869 }
3870
3871 return name;
3872 }
3873
3874 static char *
3875 java_path_expr_of_child (struct varobj *child)
3876 {
3877 return NULL;
3878 }
3879
3880 static struct value *
3881 java_value_of_root (struct varobj **var_handle)
3882 {
3883 return cplus_value_of_root (var_handle);
3884 }
3885
3886 static struct value *
3887 java_value_of_child (struct varobj *parent, int index)
3888 {
3889 return cplus_value_of_child (parent, index);
3890 }
3891
3892 static struct type *
3893 java_type_of_child (struct varobj *parent, int index)
3894 {
3895 return cplus_type_of_child (parent, index);
3896 }
3897
3898 static char *
3899 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3900 {
3901 return cplus_value_of_variable (var, format);
3902 }
3903
3904 /* Ada specific callbacks for VAROBJs. */
3905
3906 static int
3907 ada_number_of_children (struct varobj *var)
3908 {
3909 return ada_varobj_get_number_of_children (var->value, var->type);
3910 }
3911
3912 static char *
3913 ada_name_of_variable (struct varobj *parent)
3914 {
3915 return c_name_of_variable (parent);
3916 }
3917
3918 static char *
3919 ada_name_of_child (struct varobj *parent, int index)
3920 {
3921 return ada_varobj_get_name_of_child (parent->value, parent->type,
3922 parent->name, index);
3923 }
3924
3925 static char*
3926 ada_path_expr_of_child (struct varobj *child)
3927 {
3928 struct varobj *parent = child->parent;
3929 const char *parent_path_expr = varobj_get_path_expr (parent);
3930
3931 return ada_varobj_get_path_expr_of_child (parent->value,
3932 parent->type,
3933 parent->name,
3934 parent_path_expr,
3935 child->index);
3936 }
3937
3938 static struct value *
3939 ada_value_of_root (struct varobj **var_handle)
3940 {
3941 return c_value_of_root (var_handle);
3942 }
3943
3944 static struct value *
3945 ada_value_of_child (struct varobj *parent, int index)
3946 {
3947 return ada_varobj_get_value_of_child (parent->value, parent->type,
3948 parent->name, index);
3949 }
3950
3951 static struct type *
3952 ada_type_of_child (struct varobj *parent, int index)
3953 {
3954 return ada_varobj_get_type_of_child (parent->value, parent->type,
3955 index);
3956 }
3957
3958 static char *
3959 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3960 {
3961 struct value_print_options opts;
3962
3963 get_formatted_print_options (&opts, format_code[(int) format]);
3964 opts.deref_ref = 0;
3965 opts.raw = 1;
3966
3967 return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
3968 }
3969
3970 /* Implement the "value_has_mutated" routine for Ada. */
3971
3972 static int
3973 ada_value_has_mutated (struct varobj *var, struct value *new_val,
3974 struct type *new_type)
3975 {
3976 int i;
3977 int from = -1;
3978 int to = -1;
3979
3980 /* If the number of fields have changed, then for sure the type
3981 has mutated. */
3982 if (ada_varobj_get_number_of_children (new_val, new_type)
3983 != var->num_children)
3984 return 1;
3985
3986 /* If the number of fields have remained the same, then we need
3987 to check the name of each field. If they remain the same,
3988 then chances are the type hasn't mutated. This is technically
3989 an incomplete test, as the child's type might have changed
3990 despite the fact that the name remains the same. But we'll
3991 handle this situation by saying that the child has mutated,
3992 not this value.
3993
3994 If only part (or none!) of the children have been fetched,
3995 then only check the ones we fetched. It does not matter
3996 to the frontend whether a child that it has not fetched yet
3997 has mutated or not. So just assume it hasn't. */
3998
3999 restrict_range (var->children, &from, &to);
4000 for (i = from; i < to; i++)
4001 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4002 var->name, i),
4003 VEC_index (varobj_p, var->children, i)->name) != 0)
4004 return 1;
4005
4006 return 0;
4007 }
4008
4009 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4010 with an arbitrary caller supplied DATA pointer. */
4011
4012 void
4013 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
4014 {
4015 struct varobj_root *var_root, *var_root_next;
4016
4017 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4018
4019 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
4020 {
4021 var_root_next = var_root->next;
4022
4023 (*func) (var_root->rootvar, data);
4024 }
4025 }
4026 \f
4027 extern void _initialize_varobj (void);
4028 void
4029 _initialize_varobj (void)
4030 {
4031 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
4032
4033 varobj_table = xmalloc (sizeof_table);
4034 memset (varobj_table, 0, sizeof_table);
4035
4036 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
4037 &varobjdebug,
4038 _("Set varobj debugging."),
4039 _("Show varobj debugging."),
4040 _("When non-zero, varobj debugging is enabled."),
4041 NULL, show_varobjdebug,
4042 &setlist, &showlist);
4043 }
4044
4045 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4046 defined on globals. It is a helper for varobj_invalidate. */
4047
4048 static void
4049 varobj_invalidate_iter (struct varobj *var, void *unused)
4050 {
4051 /* Floating varobjs are reparsed on each stop, so we don't care if the
4052 presently parsed expression refers to something that's gone. */
4053 if (var->root->floating)
4054 return;
4055
4056 /* global var must be re-evaluated. */
4057 if (var->root->valid_block == NULL)
4058 {
4059 struct varobj *tmp_var;
4060
4061 /* Try to create a varobj with same expression. If we succeed
4062 replace the old varobj, otherwise invalidate it. */
4063 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
4064 USE_CURRENT_FRAME);
4065 if (tmp_var != NULL)
4066 {
4067 tmp_var->obj_name = xstrdup (var->obj_name);
4068 varobj_delete (var, NULL, 0);
4069 install_variable (tmp_var);
4070 }
4071 else
4072 var->root->is_valid = 0;
4073 }
4074 else /* locals must be invalidated. */
4075 var->root->is_valid = 0;
4076 }
4077
4078 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4079 are defined on globals.
4080 Invalidated varobjs will be always printed in_scope="invalid". */
4081
4082 void
4083 varobj_invalidate (void)
4084 {
4085 all_root_varobjs (varobj_invalidate_iter, NULL);
4086 }