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