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