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