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