* varobj.c (varobj_get_type): Use type_to_string.
[platform/upstream/binutils.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4    2009 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "defs.h"
20 #include "exceptions.h"
21 #include "value.h"
22 #include "expression.h"
23 #include "frame.h"
24 #include "language.h"
25 #include "wrapper.h"
26 #include "gdbcmd.h"
27 #include "block.h"
28 #include "valprint.h"
29
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32
33 #include "varobj.h"
34 #include "vec.h"
35 #include "gdbthread.h"
36 #include "inferior.h"
37 #include "typeprint.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 /* Non-zero if we want to see trace of varobj level stuff.  */
47
48 int varobjdebug = 0;
49 static void
50 show_varobjdebug (struct ui_file *file, int from_tty,
51                   struct cmd_list_element *c, const char *value)
52 {
53   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
54 }
55
56 /* String representations of gdb's format codes */
57 char *varobj_format_string[] =
58   { "natural", "binary", "decimal", "hexadecimal", "octal" };
59
60 /* String representations of gdb's known languages */
61 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
62
63 /* Data structures */
64
65 /* Every root variable has one of these structures saved in its
66    varobj. Members which must be free'd are noted. */
67 struct varobj_root
68 {
69
70   /* Alloc'd expression for this parent. */
71   struct expression *exp;
72
73   /* Block for which this expression is valid */
74   struct block *valid_block;
75
76   /* The frame for this expression.  This field is set iff valid_block is
77      not NULL.  */
78   struct frame_id frame;
79
80   /* The thread ID that this varobj_root belong to.  This field
81      is only valid if valid_block is not NULL.  
82      When not 0, indicates which thread 'frame' belongs to.
83      When 0, indicates that the thread list was empty when the varobj_root
84      was created.  */
85   int thread_id;
86
87   /* If 1, the -var-update always recomputes the value in the
88      current thread and frame.  Otherwise, variable object is
89      always updated in the specific scope/thread/frame  */
90   int floating;
91
92   /* Flag that indicates validity: set to 0 when this varobj_root refers 
93      to symbols that do not exist anymore.  */
94   int is_valid;
95
96   /* Language info for this variable and its children */
97   struct language_specific *lang;
98
99   /* The varobj for this root node. */
100   struct varobj *rootvar;
101
102   /* Next root variable */
103   struct varobj_root *next;
104 };
105
106 /* Every variable in the system has a structure of this type defined
107    for it. This structure holds all information necessary to manipulate
108    a particular object variable. Members which must be freed are noted. */
109 struct varobj
110 {
111
112   /* Alloc'd name of the variable for this object.. If this variable is a
113      child, then this name will be the child's source name.
114      (bar, not foo.bar) */
115   /* NOTE: This is the "expression" */
116   char *name;
117
118   /* Alloc'd expression for this child.  Can be used to create a
119      root variable corresponding to this child.  */
120   char *path_expr;
121
122   /* The alloc'd name for this variable's object. This is here for
123      convenience when constructing this object's children. */
124   char *obj_name;
125
126   /* Index of this variable in its parent or -1 */
127   int index;
128
129   /* The type of this variable.  This can be NULL
130      for artifial variable objects -- currently, the "accessibility" 
131      variable objects in C++.  */
132   struct type *type;
133
134   /* The value of this expression or subexpression.  A NULL value
135      indicates there was an error getting this value.
136      Invariant: if varobj_value_is_changeable_p (this) is non-zero, 
137      the value is either NULL, or not lazy.  */
138   struct value *value;
139
140   /* The number of (immediate) children this variable has */
141   int num_children;
142
143   /* If this object is a child, this points to its immediate parent. */
144   struct varobj *parent;
145
146   /* Children of this object.  */
147   VEC (varobj_p) *children;
148
149   /* Whether the children of this varobj were requested.  This field is
150      used to decide if dynamic varobj should recompute their children.
151      In the event that the frontend never asked for the children, we
152      can avoid that.  */
153   int children_requested;
154
155   /* Description of the root variable. Points to root variable for children. */
156   struct varobj_root *root;
157
158   /* The format of the output for this object */
159   enum varobj_display_formats format;
160
161   /* Was this variable updated via a varobj_set_value operation */
162   int updated;
163
164   /* Last print value.  */
165   char *print_value;
166
167   /* Is this variable frozen.  Frozen variables are never implicitly
168      updated by -var-update * 
169      or -var-update <direct-or-indirect-parent>.  */
170   int frozen;
171
172   /* Is the value of this variable intentionally not fetched?  It is
173      not fetched if either the variable is frozen, or any parents is
174      frozen.  */
175   int not_fetched;
176
177   /* The pretty-printer that has been constructed.  If NULL, then a
178      new printer object is needed, and one will be constructed.  */
179   PyObject *pretty_printer;
180 };
181
182 struct cpstack
183 {
184   char *name;
185   struct cpstack *next;
186 };
187
188 /* A list of varobjs */
189
190 struct vlist
191 {
192   struct varobj *var;
193   struct vlist *next;
194 };
195
196 /* Private function prototypes */
197
198 /* Helper functions for the above subcommands. */
199
200 static int delete_variable (struct cpstack **, struct varobj *, int);
201
202 static void delete_variable_1 (struct cpstack **, int *,
203                                struct varobj *, int, int);
204
205 static int install_variable (struct varobj *);
206
207 static void uninstall_variable (struct varobj *);
208
209 static struct varobj *create_child (struct varobj *, int, char *);
210
211 static struct varobj *
212 create_child_with_value (struct varobj *parent, int index, const char *name,
213                          struct value *value);
214
215 /* Utility routines */
216
217 static struct varobj *new_variable (void);
218
219 static struct varobj *new_root_variable (void);
220
221 static void free_variable (struct varobj *var);
222
223 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
224
225 static struct type *get_type (struct varobj *var);
226
227 static struct type *get_value_type (struct varobj *var);
228
229 static struct type *get_target_type (struct type *);
230
231 static enum varobj_display_formats variable_default_display (struct varobj *);
232
233 static void cppush (struct cpstack **pstack, char *name);
234
235 static char *cppop (struct cpstack **pstack);
236
237 static int install_new_value (struct varobj *var, struct value *value, 
238                               int initial);
239
240 static void install_default_visualizer (struct varobj *var);
241
242 /* Language-specific routines. */
243
244 static enum varobj_languages variable_language (struct varobj *var);
245
246 static int number_of_children (struct varobj *);
247
248 static char *name_of_variable (struct varobj *);
249
250 static char *name_of_child (struct varobj *, int);
251
252 static struct value *value_of_root (struct varobj **var_handle, int *);
253
254 static struct value *value_of_child (struct varobj *parent, int index);
255
256 static char *my_value_of_variable (struct varobj *var,
257                                    enum varobj_display_formats format);
258
259 static char *value_get_print_value (struct value *value,
260                                     enum varobj_display_formats format,
261                                     PyObject *value_formatter);
262
263 static int varobj_value_is_changeable_p (struct varobj *var);
264
265 static int is_root_p (struct varobj *var);
266
267 static struct varobj *
268 varobj_add_child (struct varobj *var, const char *name, struct value *value);
269
270 /* C implementation */
271
272 static int c_number_of_children (struct varobj *var);
273
274 static char *c_name_of_variable (struct varobj *parent);
275
276 static char *c_name_of_child (struct varobj *parent, int index);
277
278 static char *c_path_expr_of_child (struct varobj *child);
279
280 static struct value *c_value_of_root (struct varobj **var_handle);
281
282 static struct value *c_value_of_child (struct varobj *parent, int index);
283
284 static struct type *c_type_of_child (struct varobj *parent, int index);
285
286 static char *c_value_of_variable (struct varobj *var,
287                                   enum varobj_display_formats format);
288
289 /* C++ implementation */
290
291 static int cplus_number_of_children (struct varobj *var);
292
293 static void cplus_class_num_children (struct type *type, int children[3]);
294
295 static char *cplus_name_of_variable (struct varobj *parent);
296
297 static char *cplus_name_of_child (struct varobj *parent, int index);
298
299 static char *cplus_path_expr_of_child (struct varobj *child);
300
301 static struct value *cplus_value_of_root (struct varobj **var_handle);
302
303 static struct value *cplus_value_of_child (struct varobj *parent, int index);
304
305 static struct type *cplus_type_of_child (struct varobj *parent, int index);
306
307 static char *cplus_value_of_variable (struct varobj *var,
308                                       enum varobj_display_formats format);
309
310 /* Java implementation */
311
312 static int java_number_of_children (struct varobj *var);
313
314 static char *java_name_of_variable (struct varobj *parent);
315
316 static char *java_name_of_child (struct varobj *parent, int index);
317
318 static char *java_path_expr_of_child (struct varobj *child);
319
320 static struct value *java_value_of_root (struct varobj **var_handle);
321
322 static struct value *java_value_of_child (struct varobj *parent, int index);
323
324 static struct type *java_type_of_child (struct varobj *parent, int index);
325
326 static char *java_value_of_variable (struct varobj *var,
327                                      enum varobj_display_formats format);
328
329 /* The language specific vector */
330
331 struct language_specific
332 {
333
334   /* The language of this variable */
335   enum varobj_languages language;
336
337   /* The number of children of PARENT. */
338   int (*number_of_children) (struct varobj * parent);
339
340   /* The name (expression) of a root varobj. */
341   char *(*name_of_variable) (struct varobj * parent);
342
343   /* The name of the INDEX'th child of PARENT. */
344   char *(*name_of_child) (struct varobj * parent, int index);
345
346   /* Returns the rooted expression of CHILD, which is a variable
347      obtain that has some parent.  */
348   char *(*path_expr_of_child) (struct varobj * child);
349
350   /* The ``struct value *'' of the root variable ROOT. */
351   struct value *(*value_of_root) (struct varobj ** root_handle);
352
353   /* The ``struct value *'' of the INDEX'th child of PARENT. */
354   struct value *(*value_of_child) (struct varobj * parent, int index);
355
356   /* The type of the INDEX'th child of PARENT. */
357   struct type *(*type_of_child) (struct varobj * parent, int index);
358
359   /* The current value of VAR. */
360   char *(*value_of_variable) (struct varobj * var,
361                               enum varobj_display_formats format);
362 };
363
364 /* Array of known source language routines. */
365 static struct language_specific languages[vlang_end] = {
366   /* Unknown (try treating as C */
367   {
368    vlang_unknown,
369    c_number_of_children,
370    c_name_of_variable,
371    c_name_of_child,
372    c_path_expr_of_child,
373    c_value_of_root,
374    c_value_of_child,
375    c_type_of_child,
376    c_value_of_variable}
377   ,
378   /* C */
379   {
380    vlang_c,
381    c_number_of_children,
382    c_name_of_variable,
383    c_name_of_child,
384    c_path_expr_of_child,
385    c_value_of_root,
386    c_value_of_child,
387    c_type_of_child,
388    c_value_of_variable}
389   ,
390   /* C++ */
391   {
392    vlang_cplus,
393    cplus_number_of_children,
394    cplus_name_of_variable,
395    cplus_name_of_child,
396    cplus_path_expr_of_child,
397    cplus_value_of_root,
398    cplus_value_of_child,
399    cplus_type_of_child,
400    cplus_value_of_variable}
401   ,
402   /* Java */
403   {
404    vlang_java,
405    java_number_of_children,
406    java_name_of_variable,
407    java_name_of_child,
408    java_path_expr_of_child,
409    java_value_of_root,
410    java_value_of_child,
411    java_type_of_child,
412    java_value_of_variable}
413 };
414
415 /* A little convenience enum for dealing with C++/Java */
416 enum vsections
417 {
418   v_public = 0, v_private, v_protected
419 };
420
421 /* Private data */
422
423 /* Mappings of varobj_display_formats enums to gdb's format codes */
424 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
425
426 /* Header of the list of root variable objects */
427 static struct varobj_root *rootlist;
428 static int rootcount = 0;       /* number of root varobjs in the list */
429
430 /* Prime number indicating the number of buckets in the hash table */
431 /* A prime large enough to avoid too many colisions */
432 #define VAROBJ_TABLE_SIZE 227
433
434 /* Pointer to the varobj hash table (built at run time) */
435 static struct vlist **varobj_table;
436
437 /* Is the variable X one of our "fake" children? */
438 #define CPLUS_FAKE_CHILD(x) \
439 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
440 \f
441
442 /* API Implementation */
443 static int
444 is_root_p (struct varobj *var)
445 {
446   return (var->root->rootvar == var);
447 }
448
449 /* Creates a varobj (not its children) */
450
451 /* Return the full FRAME which corresponds to the given CORE_ADDR
452    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
453
454 static struct frame_info *
455 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
456 {
457   struct frame_info *frame = NULL;
458
459   if (frame_addr == (CORE_ADDR) 0)
460     return NULL;
461
462   for (frame = get_current_frame ();
463        frame != NULL;
464        frame = get_prev_frame (frame))
465     {
466       /* The CORE_ADDR we get as argument was parsed from a string GDB
467          output as $fp.  This output got truncated to gdbarch_addr_bit.
468          Truncate the frame base address in the same manner before
469          comparing it against our argument.  */
470       CORE_ADDR frame_base = get_frame_base_address (frame);
471       int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
472       if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
473         frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
474
475       if (frame_base == frame_addr)
476         return frame;
477     }
478
479   return NULL;
480 }
481
482 struct varobj *
483 varobj_create (char *objname,
484                char *expression, CORE_ADDR frame, enum varobj_type type)
485 {
486   struct varobj *var;
487   struct frame_info *fi;
488   struct frame_info *old_fi = NULL;
489   struct block *block;
490   struct cleanup *old_chain;
491
492   /* Fill out a varobj structure for the (root) variable being constructed. */
493   var = new_root_variable ();
494   old_chain = make_cleanup_free_variable (var);
495
496   if (expression != NULL)
497     {
498       char *p;
499       enum varobj_languages lang;
500       struct value *value = NULL;
501
502       /* Parse and evaluate the expression, filling in as much of the
503          variable's data as possible.  */
504
505       if (has_stack_frames ())
506         {
507           /* Allow creator to specify context of variable */
508           if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
509             fi = get_selected_frame (NULL);
510           else
511             /* FIXME: cagney/2002-11-23: This code should be doing a
512                lookup using the frame ID and not just the frame's
513                ``address''.  This, of course, means an interface
514                change.  However, with out that interface change ISAs,
515                such as the ia64 with its two stacks, won't work.
516                Similar goes for the case where there is a frameless
517                function.  */
518             fi = find_frame_addr_in_frame_chain (frame);
519         }
520       else
521         fi = NULL;
522
523       /* frame = -2 means always use selected frame */
524       if (type == USE_SELECTED_FRAME)
525         var->root->floating = 1;
526
527       block = NULL;
528       if (fi != NULL)
529         block = get_frame_block (fi, 0);
530
531       p = expression;
532       innermost_block = NULL;
533       /* Wrap the call to parse expression, so we can 
534          return a sensible error. */
535       if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
536         {
537           return NULL;
538         }
539
540       /* Don't allow variables to be created for types. */
541       if (var->root->exp->elts[0].opcode == OP_TYPE)
542         {
543           do_cleanups (old_chain);
544           fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
545                               " as an expression.\n");
546           return NULL;
547         }
548
549       var->format = variable_default_display (var);
550       var->root->valid_block = innermost_block;
551       var->name = xstrdup (expression);
552       /* For a root var, the name and the expr are the same.  */
553       var->path_expr = xstrdup (expression);
554
555       /* When the frame is different from the current frame, 
556          we must select the appropriate frame before parsing
557          the expression, otherwise the value will not be current.
558          Since select_frame is so benign, just call it for all cases. */
559       if (innermost_block && fi != NULL)
560         {
561           var->root->frame = get_frame_id (fi);
562           var->root->thread_id = pid_to_thread_id (inferior_ptid);
563           old_fi = get_selected_frame (NULL);
564           select_frame (fi);     
565         }
566
567       /* We definitely need to catch errors here.
568          If evaluate_expression succeeds we got the value we wanted.
569          But if it fails, we still go on with a call to evaluate_type()  */
570       if (!gdb_evaluate_expression (var->root->exp, &value))
571         {
572           /* Error getting the value.  Try to at least get the
573              right type.  */
574           struct value *type_only_value = evaluate_type (var->root->exp);
575           var->type = value_type (type_only_value);
576         }
577       else 
578         var->type = value_type (value);
579
580       install_new_value (var, value, 1 /* Initial assignment */);
581
582       /* Set language info */
583       lang = variable_language (var);
584       var->root->lang = &languages[lang];
585
586       /* Set ourselves as our root */
587       var->root->rootvar = var;
588
589       /* Reset the selected frame */
590       if (old_fi != NULL)
591         select_frame (old_fi);
592     }
593
594   /* If the variable object name is null, that means this
595      is a temporary variable, so don't install it. */
596
597   if ((var != NULL) && (objname != NULL))
598     {
599       var->obj_name = xstrdup (objname);
600
601       /* If a varobj name is duplicated, the install will fail so
602          we must clenup */
603       if (!install_variable (var))
604         {
605           do_cleanups (old_chain);
606           return NULL;
607         }
608     }
609
610   install_default_visualizer (var);
611   discard_cleanups (old_chain);
612   return var;
613 }
614
615 /* Generates an unique name that can be used for a varobj */
616
617 char *
618 varobj_gen_name (void)
619 {
620   static int id = 0;
621   char *obj_name;
622
623   /* generate a name for this object */
624   id++;
625   obj_name = xstrprintf ("var%d", id);
626
627   return obj_name;
628 }
629
630 /* Given an OBJNAME, returns the pointer to the corresponding varobj.  Call
631    error if OBJNAME cannot be found.  */
632
633 struct varobj *
634 varobj_get_handle (char *objname)
635 {
636   struct vlist *cv;
637   const char *chp;
638   unsigned int index = 0;
639   unsigned int i = 1;
640
641   for (chp = objname; *chp; chp++)
642     {
643       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
644     }
645
646   cv = *(varobj_table + index);
647   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
648     cv = cv->next;
649
650   if (cv == NULL)
651     error (_("Variable object not found"));
652
653   return cv->var;
654 }
655
656 /* Given the handle, return the name of the object */
657
658 char *
659 varobj_get_objname (struct varobj *var)
660 {
661   return var->obj_name;
662 }
663
664 /* Given the handle, return the expression represented by the object */
665
666 char *
667 varobj_get_expression (struct varobj *var)
668 {
669   return name_of_variable (var);
670 }
671
672 /* Deletes a varobj and all its children if only_children == 0,
673    otherwise deletes only the children; returns a malloc'ed list of all the 
674    (malloc'ed) names of the variables that have been deleted (NULL terminated) */
675
676 int
677 varobj_delete (struct varobj *var, char ***dellist, int only_children)
678 {
679   int delcount;
680   int mycount;
681   struct cpstack *result = NULL;
682   char **cp;
683
684   /* Initialize a stack for temporary results */
685   cppush (&result, NULL);
686
687   if (only_children)
688     /* Delete only the variable children */
689     delcount = delete_variable (&result, var, 1 /* only the children */ );
690   else
691     /* Delete the variable and all its children */
692     delcount = delete_variable (&result, var, 0 /* parent+children */ );
693
694   /* We may have been asked to return a list of what has been deleted */
695   if (dellist != NULL)
696     {
697       *dellist = xmalloc ((delcount + 1) * sizeof (char *));
698
699       cp = *dellist;
700       mycount = delcount;
701       *cp = cppop (&result);
702       while ((*cp != NULL) && (mycount > 0))
703         {
704           mycount--;
705           cp++;
706           *cp = cppop (&result);
707         }
708
709       if (mycount || (*cp != NULL))
710         warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
711                  mycount);
712     }
713
714   return delcount;
715 }
716
717 /* Convenience function for varobj_set_visualizer.  Instantiate a
718    pretty-printer for a given value.  */
719 static PyObject *
720 instantiate_pretty_printer (PyObject *constructor, struct value *value)
721 {
722 #if HAVE_PYTHON
723   PyObject *val_obj = NULL; 
724   PyObject *printer;
725   volatile struct gdb_exception except;
726
727   TRY_CATCH (except, RETURN_MASK_ALL)
728     {
729       value = value_copy (value);
730     }
731   GDB_PY_HANDLE_EXCEPTION (except);
732   val_obj = value_to_value_object (value);
733
734   if (! val_obj)
735     return NULL;
736
737   printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
738   Py_DECREF (val_obj);
739   return printer;
740 #endif
741   return NULL;
742 }
743
744 /* Set/Get variable object display format */
745
746 enum varobj_display_formats
747 varobj_set_display_format (struct varobj *var,
748                            enum varobj_display_formats format)
749 {
750   switch (format)
751     {
752     case FORMAT_NATURAL:
753     case FORMAT_BINARY:
754     case FORMAT_DECIMAL:
755     case FORMAT_HEXADECIMAL:
756     case FORMAT_OCTAL:
757       var->format = format;
758       break;
759
760     default:
761       var->format = variable_default_display (var);
762     }
763
764   if (varobj_value_is_changeable_p (var) 
765       && var->value && !value_lazy (var->value))
766     {
767       xfree (var->print_value);
768       var->print_value = value_get_print_value (var->value, var->format,
769                                                 var->pretty_printer);
770     }
771
772   return var->format;
773 }
774
775 enum varobj_display_formats
776 varobj_get_display_format (struct varobj *var)
777 {
778   return var->format;
779 }
780
781 char *
782 varobj_get_display_hint (struct varobj *var)
783 {
784   char *result = NULL;
785
786 #if HAVE_PYTHON
787   PyGILState_STATE state = PyGILState_Ensure ();
788   if (var->pretty_printer)
789     result = gdbpy_get_display_hint (var->pretty_printer);
790   PyGILState_Release (state);
791 #endif
792
793   return result;
794 }
795
796 /* If the variable object is bound to a specific thread, that
797    is its evaluation can always be done in context of a frame
798    inside that thread, returns GDB id of the thread -- which
799    is always positive.  Otherwise, returns -1. */
800 int
801 varobj_get_thread_id (struct varobj *var)
802 {
803   if (var->root->valid_block && var->root->thread_id > 0)
804     return var->root->thread_id;
805   else
806     return -1;
807 }
808
809 void
810 varobj_set_frozen (struct varobj *var, int frozen)
811 {
812   /* When a variable is unfrozen, we don't fetch its value.
813      The 'not_fetched' flag remains set, so next -var-update
814      won't complain.
815
816      We don't fetch the value, because for structures the client
817      should do -var-update anyway.  It would be bad to have different
818      client-size logic for structure and other types.  */
819   var->frozen = frozen;
820 }
821
822 int
823 varobj_get_frozen (struct varobj *var)
824 {
825   return var->frozen;
826 }
827
828 static int
829 update_dynamic_varobj_children (struct varobj *var,
830                                 VEC (varobj_p) **changed,
831                                 VEC (varobj_p) **new_and_unchanged,
832                                 int *cchanged)
833
834 {
835 #if HAVE_PYTHON
836   /* FIXME: we *might* want to provide this functionality as
837      a standalone function, so that other interested parties
838      than varobj code can benefit for this.  */
839   struct cleanup *back_to;
840   PyObject *children;
841   PyObject *iterator;
842   int i;
843   int children_changed = 0;
844   PyObject *printer = var->pretty_printer;
845   PyGILState_STATE state;
846
847   state = PyGILState_Ensure ();
848   back_to = make_cleanup_py_restore_gil (&state);
849
850   *cchanged = 0;
851   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
852     {
853       do_cleanups (back_to);
854       return 0;
855     }
856
857   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
858                                          NULL);
859
860   if (!children)
861     {
862       gdbpy_print_stack ();
863       error (_("Null value returned for children"));
864     }
865
866   make_cleanup_py_decref (children);
867
868   if (!PyIter_Check (children))
869     error (_("Returned value is not iterable"));
870
871   iterator = PyObject_GetIter (children);
872   if (!iterator)
873     {
874       gdbpy_print_stack ();
875       error (_("Could not get children iterator"));
876     }
877   make_cleanup_py_decref (iterator);
878
879   for (i = 0; ; ++i)
880     {
881       PyObject *item = PyIter_Next (iterator);
882       PyObject *py_v;
883       struct value *v;
884       char *name;
885       struct cleanup *inner;
886       
887       if (!item)
888         break;
889       inner = make_cleanup_py_decref (item);
890
891       if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
892         error (_("Invalid item from the child list"));
893       
894       if (PyObject_TypeCheck (py_v, &value_object_type))
895         {
896           /* If we just call convert_value_from_python for this type,
897              we won't know who owns the result.  For this one case we
898              need to copy the resulting value.  */
899           v = value_object_to_value (py_v);
900           v = value_copy (v);
901         }
902       else
903         v = convert_value_from_python (py_v);
904
905       /* TODO: This assume the name of the i-th child never changes.  */
906
907       /* Now see what to do here.  */
908       if (VEC_length (varobj_p, var->children) < i + 1)
909         {
910           /* There's no child yet.  */
911           struct varobj *child = varobj_add_child (var, name, v);
912           if (new_and_unchanged)
913             VEC_safe_push (varobj_p, *new_and_unchanged, child);
914           children_changed = 1;
915         }
916       else 
917         {
918           varobj_p existing = VEC_index (varobj_p, var->children, i);
919           if (install_new_value (existing, v, 0) && changed)
920             {
921               if (changed)
922                 VEC_safe_push (varobj_p, *changed, existing);
923             }
924           else
925             {
926               if (new_and_unchanged)
927                 VEC_safe_push (varobj_p, *new_and_unchanged, existing);
928             }
929         }
930
931       do_cleanups (inner);
932     }
933
934   if (i < VEC_length (varobj_p, var->children))
935     {
936       int i;
937       children_changed = 1;
938       for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
939         varobj_delete (VEC_index (varobj_p, var->children, i), NULL, 0);
940     }
941   VEC_truncate (varobj_p, var->children, i);
942   var->num_children = VEC_length (varobj_p, var->children);
943  
944   do_cleanups (back_to);
945
946   *cchanged = children_changed;
947   return 1;
948 #else
949   gdb_assert (0 && "should never be called if Python is not enabled");
950 #endif
951 }
952
953 int
954 varobj_get_num_children (struct varobj *var)
955 {
956   if (var->num_children == -1)
957     {
958       int changed;
959       if (!var->pretty_printer
960           || !update_dynamic_varobj_children (var, NULL, NULL, &changed))
961         var->num_children = number_of_children (var);
962     }
963
964   return var->num_children;
965 }
966
967 /* Creates a list of the immediate children of a variable object;
968    the return code is the number of such children or -1 on error */
969
970 VEC (varobj_p)*
971 varobj_list_children (struct varobj *var)
972 {
973   struct varobj *child;
974   char *name;
975   int i, children_changed;
976
977   var->children_requested = 1;
978
979   if (var->pretty_printer
980       /* This, in theory, can result in the number of children changing without
981          frontend noticing.  But well, calling -var-list-children on the same
982          varobj twice is not something a sane frontend would do.  */
983       && update_dynamic_varobj_children (var, NULL, NULL, &children_changed))
984     return var->children;
985
986   if (var->num_children == -1)
987     var->num_children = number_of_children (var);
988
989   /* If that failed, give up.  */
990   if (var->num_children == -1)
991     return var->children;
992
993   /* If we're called when the list of children is not yet initialized,
994      allocate enough elements in it.  */
995   while (VEC_length (varobj_p, var->children) < var->num_children)
996     VEC_safe_push (varobj_p, var->children, NULL);
997
998   for (i = 0; i < var->num_children; i++)
999     {
1000       varobj_p existing = VEC_index (varobj_p, var->children, i);
1001
1002       if (existing == NULL)
1003         {
1004           /* Either it's the first call to varobj_list_children for
1005              this variable object, and the child was never created,
1006              or it was explicitly deleted by the client.  */
1007           name = name_of_child (var, i);
1008           existing = create_child (var, i, name);
1009           VEC_replace (varobj_p, var->children, i, existing);
1010           install_default_visualizer (existing);
1011         }
1012     }
1013
1014   return var->children;
1015 }
1016
1017 static struct varobj *
1018 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1019 {
1020   varobj_p v = create_child_with_value (var, 
1021                                         VEC_length (varobj_p, var->children), 
1022                                         name, value);
1023   VEC_safe_push (varobj_p, var->children, v);
1024   install_default_visualizer (v);
1025   return v;
1026 }
1027
1028 /* Obtain the type of an object Variable as a string similar to the one gdb
1029    prints on the console */
1030
1031 char *
1032 varobj_get_type (struct varobj *var)
1033 {
1034   /* For the "fake" variables, do not return a type. (It's type is
1035      NULL, too.)
1036      Do not return a type for invalid variables as well.  */
1037   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1038     return NULL;
1039
1040   return type_to_string (var->type);
1041 }
1042
1043 /* Obtain the type of an object variable.  */
1044
1045 struct type *
1046 varobj_get_gdb_type (struct varobj *var)
1047 {
1048   return var->type;
1049 }
1050
1051 /* Return a pointer to the full rooted expression of varobj VAR.
1052    If it has not been computed yet, compute it.  */
1053 char *
1054 varobj_get_path_expr (struct varobj *var)
1055 {
1056   if (var->path_expr != NULL)
1057     return var->path_expr;
1058   else 
1059     {
1060       /* For root varobjs, we initialize path_expr
1061          when creating varobj, so here it should be
1062          child varobj.  */
1063       gdb_assert (!is_root_p (var));
1064       return (*var->root->lang->path_expr_of_child) (var);
1065     }
1066 }
1067
1068 enum varobj_languages
1069 varobj_get_language (struct varobj *var)
1070 {
1071   return variable_language (var);
1072 }
1073
1074 int
1075 varobj_get_attributes (struct varobj *var)
1076 {
1077   int attributes = 0;
1078
1079   if (varobj_editable_p (var))
1080     /* FIXME: define masks for attributes */
1081     attributes |= 0x00000001;   /* Editable */
1082
1083   return attributes;
1084 }
1085
1086 char *
1087 varobj_get_formatted_value (struct varobj *var,
1088                             enum varobj_display_formats format)
1089 {
1090   return my_value_of_variable (var, format);
1091 }
1092
1093 char *
1094 varobj_get_value (struct varobj *var)
1095 {
1096   return my_value_of_variable (var, var->format);
1097 }
1098
1099 /* Set the value of an object variable (if it is editable) to the
1100    value of the given expression */
1101 /* Note: Invokes functions that can call error() */
1102
1103 int
1104 varobj_set_value (struct varobj *var, char *expression)
1105 {
1106   struct value *val;
1107   int offset = 0;
1108   int error = 0;
1109
1110   /* The argument "expression" contains the variable's new value.
1111      We need to first construct a legal expression for this -- ugh! */
1112   /* Does this cover all the bases? */
1113   struct expression *exp;
1114   struct value *value;
1115   int saved_input_radix = input_radix;
1116   char *s = expression;
1117   int i;
1118
1119   gdb_assert (varobj_editable_p (var));
1120
1121   input_radix = 10;             /* ALWAYS reset to decimal temporarily */
1122   exp = parse_exp_1 (&s, 0, 0);
1123   if (!gdb_evaluate_expression (exp, &value))
1124     {
1125       /* We cannot proceed without a valid expression. */
1126       xfree (exp);
1127       return 0;
1128     }
1129
1130   /* All types that are editable must also be changeable.  */
1131   gdb_assert (varobj_value_is_changeable_p (var));
1132
1133   /* The value of a changeable variable object must not be lazy.  */
1134   gdb_assert (!value_lazy (var->value));
1135
1136   /* Need to coerce the input.  We want to check if the
1137      value of the variable object will be different
1138      after assignment, and the first thing value_assign
1139      does is coerce the input.
1140      For example, if we are assigning an array to a pointer variable we
1141      should compare the pointer with the the array's address, not with the
1142      array's content.  */
1143   value = coerce_array (value);
1144
1145   /* The new value may be lazy.  gdb_value_assign, or 
1146      rather value_contents, will take care of this.
1147      If fetching of the new value will fail, gdb_value_assign
1148      with catch the exception.  */
1149   if (!gdb_value_assign (var->value, value, &val))
1150     return 0;
1151      
1152   /* If the value has changed, record it, so that next -var-update can
1153      report this change.  If a variable had a value of '1', we've set it
1154      to '333' and then set again to '1', when -var-update will report this
1155      variable as changed -- because the first assignment has set the
1156      'updated' flag.  There's no need to optimize that, because return value
1157      of -var-update should be considered an approximation.  */
1158   var->updated = install_new_value (var, val, 0 /* Compare values. */);
1159   input_radix = saved_input_radix;
1160   return 1;
1161 }
1162
1163 /* Returns a malloc'ed list with all root variable objects */
1164 int
1165 varobj_list (struct varobj ***varlist)
1166 {
1167   struct varobj **cv;
1168   struct varobj_root *croot;
1169   int mycount = rootcount;
1170
1171   /* Alloc (rootcount + 1) entries for the result */
1172   *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
1173
1174   cv = *varlist;
1175   croot = rootlist;
1176   while ((croot != NULL) && (mycount > 0))
1177     {
1178       *cv = croot->rootvar;
1179       mycount--;
1180       cv++;
1181       croot = croot->next;
1182     }
1183   /* Mark the end of the list */
1184   *cv = NULL;
1185
1186   if (mycount || (croot != NULL))
1187     warning
1188       ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
1189        rootcount, mycount);
1190
1191   return rootcount;
1192 }
1193
1194 /* Assign a new value to a variable object.  If INITIAL is non-zero,
1195    this is the first assignement after the variable object was just
1196    created, or changed type.  In that case, just assign the value 
1197    and return 0.
1198    Otherwise, assign the new value, and return 1 if the value is different
1199    from the current one, 0 otherwise. The comparison is done on textual
1200    representation of value. Therefore, some types need not be compared. E.g.
1201    for structures the reported value is always "{...}", so no comparison is
1202    necessary here. If the old value was NULL and new one is not, or vice versa,
1203    we always return 1.
1204
1205    The VALUE parameter should not be released -- the function will
1206    take care of releasing it when needed.  */
1207 static int
1208 install_new_value (struct varobj *var, struct value *value, int initial)
1209
1210   int changeable;
1211   int need_to_fetch;
1212   int changed = 0;
1213   int intentionally_not_fetched = 0;
1214   char *print_value = NULL;
1215
1216   /* We need to know the varobj's type to decide if the value should
1217      be fetched or not.  C++ fake children (public/protected/private) don't have
1218      a type. */
1219   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1220   changeable = varobj_value_is_changeable_p (var);
1221
1222   /* If the type has custom visualizer, we consider it to be always
1223      changeable. FIXME: need to make sure this behaviour will not
1224      mess up read-sensitive values.  */
1225   if (var->pretty_printer)
1226     changeable = 1;
1227
1228   need_to_fetch = changeable;
1229
1230   /* We are not interested in the address of references, and given
1231      that in C++ a reference is not rebindable, it cannot
1232      meaningfully change.  So, get hold of the real value.  */
1233   if (value)
1234     {
1235       value = coerce_ref (value);
1236       release_value (value);
1237     }
1238
1239   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1240     /* For unions, we need to fetch the value implicitly because
1241        of implementation of union member fetch.  When gdb
1242        creates a value for a field and the value of the enclosing
1243        structure is not lazy,  it immediately copies the necessary
1244        bytes from the enclosing values.  If the enclosing value is
1245        lazy, the call to value_fetch_lazy on the field will read
1246        the data from memory.  For unions, that means we'll read the
1247        same memory more than once, which is not desirable.  So
1248        fetch now.  */
1249     need_to_fetch = 1;
1250
1251   /* The new value might be lazy.  If the type is changeable,
1252      that is we'll be comparing values of this type, fetch the
1253      value now.  Otherwise, on the next update the old value
1254      will be lazy, which means we've lost that old value.  */
1255   if (need_to_fetch && value && value_lazy (value))
1256     {
1257       struct varobj *parent = var->parent;
1258       int frozen = var->frozen;
1259       for (; !frozen && parent; parent = parent->parent)
1260         frozen |= parent->frozen;
1261
1262       if (frozen && initial)
1263         {
1264           /* For variables that are frozen, or are children of frozen
1265              variables, we don't do fetch on initial assignment.
1266              For non-initial assignemnt we do the fetch, since it means we're
1267              explicitly asked to compare the new value with the old one.  */
1268           intentionally_not_fetched = 1;
1269         }
1270       else if (!gdb_value_fetch_lazy (value))
1271         {
1272           /* Set the value to NULL, so that for the next -var-update,
1273              we don't try to compare the new value with this value,
1274              that we couldn't even read.  */
1275           value = NULL;
1276         }
1277     }
1278
1279
1280   /* Below, we'll be comparing string rendering of old and new
1281      values.  Don't get string rendering if the value is
1282      lazy -- if it is, the code above has decided that the value
1283      should not be fetched.  */
1284   if (value && !value_lazy (value))
1285     print_value = value_get_print_value (value, var->format, 
1286                                          var->pretty_printer);
1287
1288   /* If the type is changeable, compare the old and the new values.
1289      If this is the initial assignment, we don't have any old value
1290      to compare with.  */
1291   if (!initial && changeable)
1292     {
1293       /* If the value of the varobj was changed by -var-set-value, then the 
1294          value in the varobj and in the target is the same.  However, that value
1295          is different from the value that the varobj had after the previous
1296          -var-update. So need to the varobj as changed.  */
1297       if (var->updated)
1298         {
1299           changed = 1;
1300         }
1301       else 
1302         {
1303           /* Try to compare the values.  That requires that both
1304              values are non-lazy.  */
1305           if (var->not_fetched && value_lazy (var->value))
1306             {
1307               /* This is a frozen varobj and the value was never read.
1308                  Presumably, UI shows some "never read" indicator.
1309                  Now that we've fetched the real value, we need to report
1310                  this varobj as changed so that UI can show the real
1311                  value.  */
1312               changed = 1;
1313             }
1314           else  if (var->value == NULL && value == NULL)
1315             /* Equal. */
1316             ;
1317           else if (var->value == NULL || value == NULL)
1318             {
1319               changed = 1;
1320             }
1321           else
1322             {
1323               gdb_assert (!value_lazy (var->value));
1324               gdb_assert (!value_lazy (value));
1325
1326               gdb_assert (var->print_value != NULL && print_value != NULL);
1327               if (strcmp (var->print_value, print_value) != 0)
1328                 changed = 1;
1329             }
1330         }
1331     }
1332
1333   if (!initial && !changeable)
1334     {
1335       /* For values that are not changeable, we don't compare the values.
1336          However, we want to notice if a value was not NULL and now is NULL,
1337          or vise versa, so that we report when top-level varobjs come in scope
1338          and leave the scope.  */
1339       changed = (var->value != NULL) != (value != NULL);
1340     }
1341
1342   /* We must always keep the new value, since children depend on it.  */
1343   if (var->value != NULL && var->value != value)
1344     value_free (var->value);
1345   var->value = value;
1346   if (var->print_value)
1347     xfree (var->print_value);
1348   var->print_value = print_value;
1349   if (value && value_lazy (value) && intentionally_not_fetched)
1350     var->not_fetched = 1;
1351   else
1352     var->not_fetched = 0;
1353   var->updated = 0;
1354
1355   gdb_assert (!var->value || value_type (var->value));
1356
1357   return changed;
1358 }
1359
1360 static void
1361 install_visualizer (struct varobj *var, PyObject *visualizer)
1362 {
1363 #if HAVE_PYTHON
1364   /* If there are any children now, wipe them.  */
1365   varobj_delete (var, NULL, 1 /* children only */);
1366   var->num_children = -1;
1367
1368   Py_XDECREF (var->pretty_printer);
1369   var->pretty_printer = visualizer;
1370
1371   install_new_value (var, var->value, 1);
1372
1373   /* If we removed the visualizer, and the user ever requested the
1374      object's children, then we must compute the list of children.
1375      Note that we needn't do this when installing a visualizer,
1376      because updating will recompute dynamic children.  */
1377   if (!visualizer && var->children_requested)
1378     varobj_list_children (var);
1379 #else
1380   error (_("Python support required"));
1381 #endif
1382 }
1383
1384 static void
1385 install_default_visualizer (struct varobj *var)
1386 {
1387 #if HAVE_PYTHON
1388   struct cleanup *cleanup;
1389   PyGILState_STATE state;
1390   PyObject *pretty_printer = NULL;
1391
1392   state = PyGILState_Ensure ();
1393   cleanup = make_cleanup_py_restore_gil (&state);
1394
1395   if (var->value)
1396     {
1397       pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1398       if (! pretty_printer)
1399         {
1400           gdbpy_print_stack ();
1401           error (_("Cannot instantiate printer for default visualizer"));
1402         }
1403     }
1404       
1405   if (pretty_printer == Py_None)
1406     {
1407       Py_DECREF (pretty_printer);
1408       pretty_printer = NULL;
1409     }
1410   
1411   install_visualizer (var, pretty_printer);
1412   do_cleanups (cleanup);
1413 #else
1414   /* No error is right as this function is inserted just as a hook.  */
1415 #endif
1416 }
1417
1418 void 
1419 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1420 {
1421 #if HAVE_PYTHON
1422   PyObject *mainmod, *globals, *pretty_printer, *constructor;
1423   struct cleanup *back_to, *value;
1424   PyGILState_STATE state;
1425
1426
1427   state = PyGILState_Ensure ();
1428   back_to = make_cleanup_py_restore_gil (&state);
1429
1430   mainmod = PyImport_AddModule ("__main__");
1431   globals = PyModule_GetDict (mainmod);
1432   Py_INCREF (globals);
1433   make_cleanup_py_decref (globals);
1434
1435   constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1436   
1437   /* Do not instantiate NoneType. */
1438   if (constructor == Py_None)
1439     {
1440       pretty_printer = Py_None;
1441       Py_INCREF (pretty_printer);
1442     }
1443   else
1444     pretty_printer = instantiate_pretty_printer (constructor, var->value);
1445
1446   Py_XDECREF (constructor);
1447
1448   if (! pretty_printer)
1449     {
1450       gdbpy_print_stack ();
1451       error (_("Could not evaluate visualizer expression: %s"), visualizer);
1452     }
1453
1454   if (pretty_printer == Py_None)
1455     {
1456       Py_DECREF (pretty_printer);
1457       pretty_printer = NULL;
1458     }
1459
1460   install_visualizer (var, pretty_printer);
1461
1462   do_cleanups (back_to);
1463 #else
1464   error (_("Python support required"));
1465 #endif
1466 }
1467
1468 /* Update the values for a variable and its children.  This is a
1469    two-pronged attack.  First, re-parse the value for the root's
1470    expression to see if it's changed.  Then go all the way
1471    through its children, reconstructing them and noting if they've
1472    changed.
1473
1474    The EXPLICIT parameter specifies if this call is result
1475    of MI request to update this specific variable, or 
1476    result of implicit -var-update *. For implicit request, we don't
1477    update frozen variables.
1478
1479    NOTE: This function may delete the caller's varobj. If it
1480    returns TYPE_CHANGED, then it has done this and VARP will be modified
1481    to point to the new varobj.  */
1482
1483 VEC(varobj_update_result) *varobj_update (struct varobj **varp, int explicit)
1484 {
1485   int changed = 0;
1486   int type_changed = 0;
1487   int i;
1488   int vleft;
1489   struct varobj *v;
1490   struct varobj **cv;
1491   struct varobj **templist = NULL;
1492   struct value *new;
1493   VEC (varobj_update_result) *stack = NULL;
1494   VEC (varobj_update_result) *result = NULL;
1495   struct frame_info *fi;
1496
1497   /* Frozen means frozen -- we don't check for any change in
1498      this varobj, including its going out of scope, or
1499      changing type.  One use case for frozen varobjs is
1500      retaining previously evaluated expressions, and we don't
1501      want them to be reevaluated at all.  */
1502   if (!explicit && (*varp)->frozen)
1503     return result;
1504
1505   if (!(*varp)->root->is_valid)
1506     {
1507       varobj_update_result r = {*varp};
1508       r.status = VAROBJ_INVALID;
1509       VEC_safe_push (varobj_update_result, result, &r);
1510       return result;
1511     }
1512
1513   if ((*varp)->root->rootvar == *varp)
1514     {
1515       varobj_update_result r = {*varp};
1516       r.status = VAROBJ_IN_SCOPE;
1517
1518       /* Update the root variable. value_of_root can return NULL
1519          if the variable is no longer around, i.e. we stepped out of
1520          the frame in which a local existed. We are letting the 
1521          value_of_root variable dispose of the varobj if the type
1522          has changed.  */
1523       new = value_of_root (varp, &type_changed);
1524       r.varobj = *varp;
1525
1526       r.type_changed = type_changed;
1527       if (install_new_value ((*varp), new, type_changed))
1528         r.changed = 1;
1529       
1530       if (new == NULL)
1531         r.status = VAROBJ_NOT_IN_SCOPE;
1532       r.value_installed = 1;
1533
1534       if (r.status == VAROBJ_NOT_IN_SCOPE)
1535         {
1536           if (r.type_changed || r.changed)
1537             VEC_safe_push (varobj_update_result, result, &r);
1538           return result;
1539         }
1540             
1541       VEC_safe_push (varobj_update_result, stack, &r);
1542     }
1543   else
1544     {
1545       varobj_update_result r = {*varp};
1546       VEC_safe_push (varobj_update_result, stack, &r);
1547     }
1548
1549   /* Walk through the children, reconstructing them all.  */
1550   while (!VEC_empty (varobj_update_result, stack))
1551     {
1552       varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1553       struct varobj *v = r.varobj;
1554
1555       VEC_pop (varobj_update_result, stack);
1556
1557       /* Update this variable, unless it's a root, which is already
1558          updated.  */
1559       if (!r.value_installed)
1560         {         
1561           new = value_of_child (v->parent, v->index);
1562           if (install_new_value (v, new, 0 /* type not changed */))
1563             {
1564               r.changed = 1;
1565               v->updated = 0;
1566             }
1567         }
1568
1569       /* We probably should not get children of a varobj that has a
1570          pretty-printer, but for which -var-list-children was never
1571          invoked.  Presumably, such varobj is not yet expanded in the
1572          UI, so we need not bother getting it.  */
1573       if (v->pretty_printer)
1574         {
1575           VEC (varobj_p) *changed = 0, *new_and_unchanged = 0;
1576           int i, children_changed;
1577           varobj_p tmp;
1578
1579           if (!v->children_requested)
1580             continue;
1581
1582           if (v->frozen)
1583             continue;
1584
1585           /* If update_dynamic_varobj_children returns 0, then we have
1586              a non-conforming pretty-printer, so we skip it.  */
1587           if (update_dynamic_varobj_children (v, &changed, &new_and_unchanged,
1588                                               &children_changed))
1589             {
1590               if (children_changed)
1591                 r.children_changed = 1;
1592               for (i = 0; VEC_iterate (varobj_p, changed, i, tmp); ++i)
1593                 {
1594                   varobj_update_result r = {tmp};
1595                   r.changed = 1;
1596                   r.value_installed = 1;
1597                   VEC_safe_push (varobj_update_result, stack, &r);
1598                 }
1599               for (i = 0;
1600                    VEC_iterate (varobj_p, new_and_unchanged, i, tmp);
1601                    ++i)
1602                 {
1603                   varobj_update_result r = {tmp};
1604                   r.value_installed = 1;
1605                   VEC_safe_push (varobj_update_result, stack, &r);
1606                 }
1607               if (r.changed || r.children_changed)
1608                 VEC_safe_push (varobj_update_result, result, &r);
1609               continue;
1610             }
1611         }
1612
1613       /* Push any children.  Use reverse order so that the first
1614          child is popped from the work stack first, and so
1615          will be added to result first.  This does not
1616          affect correctness, just "nicer".  */
1617       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1618         {
1619           varobj_p c = VEC_index (varobj_p, v->children, i);
1620           /* Child may be NULL if explicitly deleted by -var-delete.  */
1621           if (c != NULL && !c->frozen)
1622             {
1623               varobj_update_result r = {c};
1624               VEC_safe_push (varobj_update_result, stack, &r);
1625             }
1626         }
1627
1628       if (r.changed || r.type_changed)
1629         VEC_safe_push (varobj_update_result, result, &r);
1630     }
1631
1632   VEC_free (varobj_update_result, stack);
1633
1634   return result;
1635 }
1636 \f
1637
1638 /* Helper functions */
1639
1640 /*
1641  * Variable object construction/destruction
1642  */
1643
1644 static int
1645 delete_variable (struct cpstack **resultp, struct varobj *var,
1646                  int only_children_p)
1647 {
1648   int delcount = 0;
1649
1650   delete_variable_1 (resultp, &delcount, var,
1651                      only_children_p, 1 /* remove_from_parent_p */ );
1652
1653   return delcount;
1654 }
1655
1656 /* Delete the variable object VAR and its children */
1657 /* IMPORTANT NOTE: If we delete a variable which is a child
1658    and the parent is not removed we dump core.  It must be always
1659    initially called with remove_from_parent_p set */
1660 static void
1661 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1662                    struct varobj *var, int only_children_p,
1663                    int remove_from_parent_p)
1664 {
1665   int i;
1666
1667   /* Delete any children of this variable, too. */
1668   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1669     {   
1670       varobj_p child = VEC_index (varobj_p, var->children, i);
1671       if (!child)
1672         continue;
1673       if (!remove_from_parent_p)
1674         child->parent = NULL;
1675       delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1676     }
1677   VEC_free (varobj_p, var->children);
1678
1679   /* if we were called to delete only the children we are done here */
1680   if (only_children_p)
1681     return;
1682
1683   /* Otherwise, add it to the list of deleted ones and proceed to do so */
1684   /* If the name is null, this is a temporary variable, that has not
1685      yet been installed, don't report it, it belongs to the caller... */
1686   if (var->obj_name != NULL)
1687     {
1688       cppush (resultp, xstrdup (var->obj_name));
1689       *delcountp = *delcountp + 1;
1690     }
1691
1692   /* If this variable has a parent, remove it from its parent's list */
1693   /* OPTIMIZATION: if the parent of this variable is also being deleted, 
1694      (as indicated by remove_from_parent_p) we don't bother doing an
1695      expensive list search to find the element to remove when we are
1696      discarding the list afterwards */
1697   if ((remove_from_parent_p) && (var->parent != NULL))
1698     {
1699       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1700     }
1701
1702   if (var->obj_name != NULL)
1703     uninstall_variable (var);
1704
1705   /* Free memory associated with this variable */
1706   free_variable (var);
1707 }
1708
1709 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1710 static int
1711 install_variable (struct varobj *var)
1712 {
1713   struct vlist *cv;
1714   struct vlist *newvl;
1715   const char *chp;
1716   unsigned int index = 0;
1717   unsigned int i = 1;
1718
1719   for (chp = var->obj_name; *chp; chp++)
1720     {
1721       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1722     }
1723
1724   cv = *(varobj_table + index);
1725   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1726     cv = cv->next;
1727
1728   if (cv != NULL)
1729     error (_("Duplicate variable object name"));
1730
1731   /* Add varobj to hash table */
1732   newvl = xmalloc (sizeof (struct vlist));
1733   newvl->next = *(varobj_table + index);
1734   newvl->var = var;
1735   *(varobj_table + index) = newvl;
1736
1737   /* If root, add varobj to root list */
1738   if (is_root_p (var))
1739     {
1740       /* Add to list of root variables */
1741       if (rootlist == NULL)
1742         var->root->next = NULL;
1743       else
1744         var->root->next = rootlist;
1745       rootlist = var->root;
1746       rootcount++;
1747     }
1748
1749   return 1;                     /* OK */
1750 }
1751
1752 /* Unistall the object VAR. */
1753 static void
1754 uninstall_variable (struct varobj *var)
1755 {
1756   struct vlist *cv;
1757   struct vlist *prev;
1758   struct varobj_root *cr;
1759   struct varobj_root *prer;
1760   const char *chp;
1761   unsigned int index = 0;
1762   unsigned int i = 1;
1763
1764   /* Remove varobj from hash table */
1765   for (chp = var->obj_name; *chp; chp++)
1766     {
1767       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1768     }
1769
1770   cv = *(varobj_table + index);
1771   prev = NULL;
1772   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1773     {
1774       prev = cv;
1775       cv = cv->next;
1776     }
1777
1778   if (varobjdebug)
1779     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1780
1781   if (cv == NULL)
1782     {
1783       warning
1784         ("Assertion failed: Could not find variable object \"%s\" to delete",
1785          var->obj_name);
1786       return;
1787     }
1788
1789   if (prev == NULL)
1790     *(varobj_table + index) = cv->next;
1791   else
1792     prev->next = cv->next;
1793
1794   xfree (cv);
1795
1796   /* If root, remove varobj from root list */
1797   if (is_root_p (var))
1798     {
1799       /* Remove from list of root variables */
1800       if (rootlist == var->root)
1801         rootlist = var->root->next;
1802       else
1803         {
1804           prer = NULL;
1805           cr = rootlist;
1806           while ((cr != NULL) && (cr->rootvar != var))
1807             {
1808               prer = cr;
1809               cr = cr->next;
1810             }
1811           if (cr == NULL)
1812             {
1813               warning
1814                 ("Assertion failed: Could not find varobj \"%s\" in root list",
1815                  var->obj_name);
1816               return;
1817             }
1818           if (prer == NULL)
1819             rootlist = NULL;
1820           else
1821             prer->next = cr->next;
1822         }
1823       rootcount--;
1824     }
1825
1826 }
1827
1828 /* Create and install a child of the parent of the given name */
1829 static struct varobj *
1830 create_child (struct varobj *parent, int index, char *name)
1831 {
1832   return create_child_with_value (parent, index, name, 
1833                                   value_of_child (parent, index));
1834 }
1835
1836 static struct varobj *
1837 create_child_with_value (struct varobj *parent, int index, const char *name,
1838                          struct value *value)
1839 {
1840   struct varobj *child;
1841   char *childs_name;
1842
1843   child = new_variable ();
1844
1845   /* name is allocated by name_of_child */
1846   /* FIXME: xstrdup should not be here.  */
1847   child->name = xstrdup (name);
1848   child->index = index;
1849   child->parent = parent;
1850   child->root = parent->root;
1851   childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1852   child->obj_name = childs_name;
1853   install_variable (child);
1854
1855   /* Compute the type of the child.  Must do this before
1856      calling install_new_value.  */
1857   if (value != NULL)
1858     /* If the child had no evaluation errors, var->value
1859        will be non-NULL and contain a valid type. */
1860     child->type = value_type (value);
1861   else
1862     /* Otherwise, we must compute the type. */
1863     child->type = (*child->root->lang->type_of_child) (child->parent, 
1864                                                        child->index);
1865   install_new_value (child, value, 1);
1866
1867   return child;
1868 }
1869 \f
1870
1871 /*
1872  * Miscellaneous utility functions.
1873  */
1874
1875 /* Allocate memory and initialize a new variable */
1876 static struct varobj *
1877 new_variable (void)
1878 {
1879   struct varobj *var;
1880
1881   var = (struct varobj *) xmalloc (sizeof (struct varobj));
1882   var->name = NULL;
1883   var->path_expr = NULL;
1884   var->obj_name = NULL;
1885   var->index = -1;
1886   var->type = NULL;
1887   var->value = NULL;
1888   var->num_children = -1;
1889   var->parent = NULL;
1890   var->children = NULL;
1891   var->format = 0;
1892   var->root = NULL;
1893   var->updated = 0;
1894   var->print_value = NULL;
1895   var->frozen = 0;
1896   var->not_fetched = 0;
1897   var->children_requested = 0;
1898   var->pretty_printer = 0;
1899
1900   return var;
1901 }
1902
1903 /* Allocate memory and initialize a new root variable */
1904 static struct varobj *
1905 new_root_variable (void)
1906 {
1907   struct varobj *var = new_variable ();
1908   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1909   var->root->lang = NULL;
1910   var->root->exp = NULL;
1911   var->root->valid_block = NULL;
1912   var->root->frame = null_frame_id;
1913   var->root->floating = 0;
1914   var->root->rootvar = NULL;
1915   var->root->is_valid = 1;
1916
1917   return var;
1918 }
1919
1920 /* Free any allocated memory associated with VAR. */
1921 static void
1922 free_variable (struct varobj *var)
1923 {
1924   value_free (var->value);
1925
1926   /* Free the expression if this is a root variable. */
1927   if (is_root_p (var))
1928     {
1929       xfree (var->root->exp);
1930       xfree (var->root);
1931     }
1932
1933 #if HAVE_PYTHON
1934   {
1935     PyGILState_STATE state = PyGILState_Ensure ();
1936     Py_XDECREF (var->pretty_printer);
1937     PyGILState_Release (state);
1938   }
1939 #endif
1940
1941   xfree (var->name);
1942   xfree (var->obj_name);
1943   xfree (var->print_value);
1944   xfree (var->path_expr);
1945   xfree (var);
1946 }
1947
1948 static void
1949 do_free_variable_cleanup (void *var)
1950 {
1951   free_variable (var);
1952 }
1953
1954 static struct cleanup *
1955 make_cleanup_free_variable (struct varobj *var)
1956 {
1957   return make_cleanup (do_free_variable_cleanup, var);
1958 }
1959
1960 /* This returns the type of the variable. It also skips past typedefs
1961    to return the real type of the variable.
1962
1963    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1964    except within get_target_type and get_type. */
1965 static struct type *
1966 get_type (struct varobj *var)
1967 {
1968   struct type *type;
1969   type = var->type;
1970
1971   if (type != NULL)
1972     type = check_typedef (type);
1973
1974   return type;
1975 }
1976
1977 /* Return the type of the value that's stored in VAR,
1978    or that would have being stored there if the
1979    value were accessible.  
1980
1981    This differs from VAR->type in that VAR->type is always
1982    the true type of the expession in the source language.
1983    The return value of this function is the type we're
1984    actually storing in varobj, and using for displaying
1985    the values and for comparing previous and new values.
1986
1987    For example, top-level references are always stripped.  */
1988 static struct type *
1989 get_value_type (struct varobj *var)
1990 {
1991   struct type *type;
1992
1993   if (var->value)
1994     type = value_type (var->value);
1995   else
1996     type = var->type;
1997
1998   type = check_typedef (type);
1999
2000   if (TYPE_CODE (type) == TYPE_CODE_REF)
2001     type = get_target_type (type);
2002
2003   type = check_typedef (type);
2004
2005   return type;
2006 }
2007
2008 /* This returns the target type (or NULL) of TYPE, also skipping
2009    past typedefs, just like get_type ().
2010
2011    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2012    except within get_target_type and get_type. */
2013 static struct type *
2014 get_target_type (struct type *type)
2015 {
2016   if (type != NULL)
2017     {
2018       type = TYPE_TARGET_TYPE (type);
2019       if (type != NULL)
2020         type = check_typedef (type);
2021     }
2022
2023   return type;
2024 }
2025
2026 /* What is the default display for this variable? We assume that
2027    everything is "natural". Any exceptions? */
2028 static enum varobj_display_formats
2029 variable_default_display (struct varobj *var)
2030 {
2031   return FORMAT_NATURAL;
2032 }
2033
2034 /* FIXME: The following should be generic for any pointer */
2035 static void
2036 cppush (struct cpstack **pstack, char *name)
2037 {
2038   struct cpstack *s;
2039
2040   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2041   s->name = name;
2042   s->next = *pstack;
2043   *pstack = s;
2044 }
2045
2046 /* FIXME: The following should be generic for any pointer */
2047 static char *
2048 cppop (struct cpstack **pstack)
2049 {
2050   struct cpstack *s;
2051   char *v;
2052
2053   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2054     return NULL;
2055
2056   s = *pstack;
2057   v = s->name;
2058   *pstack = (*pstack)->next;
2059   xfree (s);
2060
2061   return v;
2062 }
2063 \f
2064 /*
2065  * Language-dependencies
2066  */
2067
2068 /* Common entry points */
2069
2070 /* Get the language of variable VAR. */
2071 static enum varobj_languages
2072 variable_language (struct varobj *var)
2073 {
2074   enum varobj_languages lang;
2075
2076   switch (var->root->exp->language_defn->la_language)
2077     {
2078     default:
2079     case language_c:
2080       lang = vlang_c;
2081       break;
2082     case language_cplus:
2083       lang = vlang_cplus;
2084       break;
2085     case language_java:
2086       lang = vlang_java;
2087       break;
2088     }
2089
2090   return lang;
2091 }
2092
2093 /* Return the number of children for a given variable.
2094    The result of this function is defined by the language
2095    implementation. The number of children returned by this function
2096    is the number of children that the user will see in the variable
2097    display. */
2098 static int
2099 number_of_children (struct varobj *var)
2100 {
2101   return (*var->root->lang->number_of_children) (var);;
2102 }
2103
2104 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
2105 static char *
2106 name_of_variable (struct varobj *var)
2107 {
2108   return (*var->root->lang->name_of_variable) (var);
2109 }
2110
2111 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
2112 static char *
2113 name_of_child (struct varobj *var, int index)
2114 {
2115   return (*var->root->lang->name_of_child) (var, index);
2116 }
2117
2118 /* What is the ``struct value *'' of the root variable VAR?
2119    For floating variable object, evaluation can get us a value
2120    of different type from what is stored in varobj already.  In
2121    that case:
2122    - *type_changed will be set to 1
2123    - old varobj will be freed, and new one will be
2124    created, with the same name.
2125    - *var_handle will be set to the new varobj 
2126    Otherwise, *type_changed will be set to 0.  */
2127 static struct value *
2128 value_of_root (struct varobj **var_handle, int *type_changed)
2129 {
2130   struct varobj *var;
2131
2132   if (var_handle == NULL)
2133     return NULL;
2134
2135   var = *var_handle;
2136
2137   /* This should really be an exception, since this should
2138      only get called with a root variable. */
2139
2140   if (!is_root_p (var))
2141     return NULL;
2142
2143   if (var->root->floating)
2144     {
2145       struct varobj *tmp_var;
2146       char *old_type, *new_type;
2147
2148       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2149                                USE_SELECTED_FRAME);
2150       if (tmp_var == NULL)
2151         {
2152           return NULL;
2153         }
2154       old_type = varobj_get_type (var);
2155       new_type = varobj_get_type (tmp_var);
2156       if (strcmp (old_type, new_type) == 0)
2157         {
2158           /* The expression presently stored inside var->root->exp
2159              remembers the locations of local variables relatively to
2160              the frame where the expression was created (in DWARF location
2161              button, for example).  Naturally, those locations are not
2162              correct in other frames, so update the expression.  */
2163
2164          struct expression *tmp_exp = var->root->exp;
2165          var->root->exp = tmp_var->root->exp;
2166          tmp_var->root->exp = tmp_exp;
2167
2168           varobj_delete (tmp_var, NULL, 0);
2169           *type_changed = 0;
2170         }
2171       else
2172         {
2173           tmp_var->obj_name = xstrdup (var->obj_name);
2174           varobj_delete (var, NULL, 0);
2175
2176           install_variable (tmp_var);
2177           *var_handle = tmp_var;
2178           var = *var_handle;
2179           *type_changed = 1;
2180         }
2181       xfree (old_type);
2182       xfree (new_type);
2183     }
2184   else
2185     {
2186       *type_changed = 0;
2187     }
2188
2189   return (*var->root->lang->value_of_root) (var_handle);
2190 }
2191
2192 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2193 static struct value *
2194 value_of_child (struct varobj *parent, int index)
2195 {
2196   struct value *value;
2197
2198   value = (*parent->root->lang->value_of_child) (parent, index);
2199
2200   return value;
2201 }
2202
2203 /* GDB already has a command called "value_of_variable". Sigh. */
2204 static char *
2205 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2206 {
2207   if (var->root->is_valid)
2208     return (*var->root->lang->value_of_variable) (var, format);
2209   else
2210     return NULL;
2211 }
2212
2213 static char *
2214 value_get_print_value (struct value *value, enum varobj_display_formats format,
2215                        PyObject *value_formatter)
2216 {
2217   long dummy;
2218   struct ui_file *stb;
2219   struct cleanup *old_chain;
2220   char *thevalue = NULL;
2221   struct value_print_options opts;
2222
2223   if (value == NULL)
2224     return NULL;
2225
2226 #if HAVE_PYTHON
2227   {
2228     PyGILState_STATE state = PyGILState_Ensure ();
2229     if (value_formatter && PyObject_HasAttr (value_formatter,
2230                                              gdbpy_to_string_cst))
2231       {
2232         char *hint;
2233         struct value *replacement;
2234         int string_print = 0;
2235
2236         hint = gdbpy_get_display_hint (value_formatter);
2237         if (hint)
2238           {
2239             if (!strcmp (hint, "string"))
2240               string_print = 1;
2241             xfree (hint);
2242           }
2243
2244         thevalue = apply_varobj_pretty_printer (value_formatter,
2245                                                 &replacement);
2246         if (thevalue && !string_print)
2247           {
2248             PyGILState_Release (state);
2249             return thevalue;
2250           }
2251         if (replacement)
2252           value = replacement;
2253       }
2254     PyGILState_Release (state);
2255   }
2256 #endif
2257
2258   stb = mem_fileopen ();
2259   old_chain = make_cleanup_ui_file_delete (stb);
2260
2261   get_formatted_print_options (&opts, format_code[(int) format]);
2262   opts.deref_ref = 0;
2263   opts.raw = 1;
2264   if (thevalue)
2265     {
2266       make_cleanup (xfree, thevalue);
2267       LA_PRINT_STRING (stb, builtin_type (current_gdbarch)->builtin_char,
2268                        (gdb_byte *) thevalue, strlen (thevalue),
2269                        0, &opts);
2270     }
2271   else
2272     common_val_print (value, stb, 0, &opts, current_language);
2273   thevalue = ui_file_xstrdup (stb, &dummy);
2274
2275   do_cleanups (old_chain);
2276   return thevalue;
2277 }
2278
2279 int
2280 varobj_editable_p (struct varobj *var)
2281 {
2282   struct type *type;
2283   struct value *value;
2284
2285   if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2286     return 0;
2287
2288   type = get_value_type (var);
2289
2290   switch (TYPE_CODE (type))
2291     {
2292     case TYPE_CODE_STRUCT:
2293     case TYPE_CODE_UNION:
2294     case TYPE_CODE_ARRAY:
2295     case TYPE_CODE_FUNC:
2296     case TYPE_CODE_METHOD:
2297       return 0;
2298       break;
2299
2300     default:
2301       return 1;
2302       break;
2303     }
2304 }
2305
2306 /* Return non-zero if changes in value of VAR
2307    must be detected and reported by -var-update.
2308    Return zero is -var-update should never report
2309    changes of such values.  This makes sense for structures
2310    (since the changes in children values will be reported separately),
2311    or for artifical objects (like 'public' pseudo-field in C++).
2312
2313    Return value of 0 means that gdb need not call value_fetch_lazy
2314    for the value of this variable object.  */
2315 static int
2316 varobj_value_is_changeable_p (struct varobj *var)
2317 {
2318   int r;
2319   struct type *type;
2320
2321   if (CPLUS_FAKE_CHILD (var))
2322     return 0;
2323
2324   type = get_value_type (var);
2325
2326   switch (TYPE_CODE (type))
2327     {
2328     case TYPE_CODE_STRUCT:
2329     case TYPE_CODE_UNION:
2330     case TYPE_CODE_ARRAY:
2331       r = 0;
2332       break;
2333
2334     default:
2335       r = 1;
2336     }
2337
2338   return r;
2339 }
2340
2341 /* Return 1 if that varobj is floating, that is is always evaluated in the
2342    selected frame, and not bound to thread/frame.  Such variable objects
2343    are created using '@' as frame specifier to -var-create.  */
2344 int
2345 varobj_floating_p (struct varobj *var)
2346 {
2347   return var->root->floating;
2348 }
2349
2350 /* Given the value and the type of a variable object,
2351    adjust the value and type to those necessary
2352    for getting children of the variable object.
2353    This includes dereferencing top-level references
2354    to all types and dereferencing pointers to
2355    structures.  
2356
2357    Both TYPE and *TYPE should be non-null. VALUE
2358    can be null if we want to only translate type.
2359    *VALUE can be null as well -- if the parent
2360    value is not known.  
2361
2362    If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2363    depending on whether pointer was dereferenced
2364    in this function.  */
2365 static void
2366 adjust_value_for_child_access (struct value **value,
2367                                   struct type **type,
2368                                   int *was_ptr)
2369 {
2370   gdb_assert (type && *type);
2371
2372   if (was_ptr)
2373     *was_ptr = 0;
2374
2375   *type = check_typedef (*type);
2376   
2377   /* The type of value stored in varobj, that is passed
2378      to us, is already supposed to be
2379      reference-stripped.  */
2380
2381   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2382
2383   /* Pointers to structures are treated just like
2384      structures when accessing children.  Don't
2385      dererences pointers to other types.  */
2386   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2387     {
2388       struct type *target_type = get_target_type (*type);
2389       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2390           || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2391         {
2392           if (value && *value)
2393             {
2394               int success = gdb_value_ind (*value, value);        
2395               if (!success)
2396                 *value = NULL;
2397             }
2398           *type = target_type;
2399           if (was_ptr)
2400             *was_ptr = 1;
2401         }
2402     }
2403
2404   /* The 'get_target_type' function calls check_typedef on
2405      result, so we can immediately check type code.  No
2406      need to call check_typedef here.  */
2407 }
2408
2409 /* C */
2410 static int
2411 c_number_of_children (struct varobj *var)
2412 {
2413   struct type *type = get_value_type (var);
2414   int children = 0;
2415   struct type *target;
2416
2417   adjust_value_for_child_access (NULL, &type, NULL);
2418   target = get_target_type (type);
2419
2420   switch (TYPE_CODE (type))
2421     {
2422     case TYPE_CODE_ARRAY:
2423       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
2424           && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
2425         children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
2426       else
2427         /* If we don't know how many elements there are, don't display
2428            any.  */
2429         children = 0;
2430       break;
2431
2432     case TYPE_CODE_STRUCT:
2433     case TYPE_CODE_UNION:
2434       children = TYPE_NFIELDS (type);
2435       break;
2436
2437     case TYPE_CODE_PTR:
2438       /* The type here is a pointer to non-struct. Typically, pointers
2439          have one child, except for function ptrs, which have no children,
2440          and except for void*, as we don't know what to show.
2441
2442          We can show char* so we allow it to be dereferenced.  If you decide
2443          to test for it, please mind that a little magic is necessary to
2444          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and 
2445          TYPE_NAME == "char" */
2446       if (TYPE_CODE (target) == TYPE_CODE_FUNC
2447           || TYPE_CODE (target) == TYPE_CODE_VOID)
2448         children = 0;
2449       else
2450         children = 1;
2451       break;
2452
2453     default:
2454       /* Other types have no children */
2455       break;
2456     }
2457
2458   return children;
2459 }
2460
2461 static char *
2462 c_name_of_variable (struct varobj *parent)
2463 {
2464   return xstrdup (parent->name);
2465 }
2466
2467 /* Return the value of element TYPE_INDEX of a structure
2468    value VALUE.  VALUE's type should be a structure,
2469    or union, or a typedef to struct/union.  
2470
2471    Returns NULL if getting the value fails.  Never throws.  */
2472 static struct value *
2473 value_struct_element_index (struct value *value, int type_index)
2474 {
2475   struct value *result = NULL;
2476   volatile struct gdb_exception e;
2477
2478   struct type *type = value_type (value);
2479   type = check_typedef (type);
2480
2481   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
2482               || TYPE_CODE (type) == TYPE_CODE_UNION);
2483
2484   TRY_CATCH (e, RETURN_MASK_ERROR)
2485     {
2486       if (field_is_static (&TYPE_FIELD (type, type_index)))
2487         result = value_static_field (type, type_index);
2488       else
2489         result = value_primitive_field (value, 0, type_index, type);
2490     }
2491   if (e.reason < 0)
2492     {
2493       return NULL;
2494     }
2495   else
2496     {
2497       return result;
2498     }
2499 }
2500
2501 /* Obtain the information about child INDEX of the variable
2502    object PARENT.  
2503    If CNAME is not null, sets *CNAME to the name of the child relative
2504    to the parent.
2505    If CVALUE is not null, sets *CVALUE to the value of the child.
2506    If CTYPE is not null, sets *CTYPE to the type of the child.
2507
2508    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2509    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2510    to NULL.  */
2511 static void 
2512 c_describe_child (struct varobj *parent, int index,
2513                   char **cname, struct value **cvalue, struct type **ctype,
2514                   char **cfull_expression)
2515 {
2516   struct value *value = parent->value;
2517   struct type *type = get_value_type (parent);
2518   char *parent_expression = NULL;
2519   int was_ptr;
2520
2521   if (cname)
2522     *cname = NULL;
2523   if (cvalue)
2524     *cvalue = NULL;
2525   if (ctype)
2526     *ctype = NULL;
2527   if (cfull_expression)
2528     {
2529       *cfull_expression = NULL;
2530       parent_expression = varobj_get_path_expr (parent);
2531     }
2532   adjust_value_for_child_access (&value, &type, &was_ptr);
2533       
2534   switch (TYPE_CODE (type))
2535     {
2536     case TYPE_CODE_ARRAY:
2537       if (cname)
2538         *cname = xstrprintf ("%d", index
2539                              + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2540
2541       if (cvalue && value)
2542         {
2543           int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2544           gdb_value_subscript (value, real_index, cvalue);
2545         }
2546
2547       if (ctype)
2548         *ctype = get_target_type (type);
2549
2550       if (cfull_expression)
2551         *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression, 
2552                                         index
2553                                         + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2554
2555
2556       break;
2557
2558     case TYPE_CODE_STRUCT:
2559     case TYPE_CODE_UNION:
2560       if (cname)
2561         *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2562
2563       if (cvalue && value)
2564         {
2565           /* For C, varobj index is the same as type index.  */
2566           *cvalue = value_struct_element_index (value, index);
2567         }
2568
2569       if (ctype)
2570         *ctype = TYPE_FIELD_TYPE (type, index);
2571
2572       if (cfull_expression)
2573         {
2574           char *join = was_ptr ? "->" : ".";
2575           *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2576                                           TYPE_FIELD_NAME (type, index));
2577         }
2578
2579       break;
2580
2581     case TYPE_CODE_PTR:
2582       if (cname)
2583         *cname = xstrprintf ("*%s", parent->name);
2584
2585       if (cvalue && value)
2586         {
2587           int success = gdb_value_ind (value, cvalue);
2588           if (!success)
2589             *cvalue = NULL;
2590         }
2591
2592       /* Don't use get_target_type because it calls
2593          check_typedef and here, we want to show the true
2594          declared type of the variable.  */
2595       if (ctype)
2596         *ctype = TYPE_TARGET_TYPE (type);
2597
2598       if (cfull_expression)
2599         *cfull_expression = xstrprintf ("*(%s)", parent_expression);
2600       
2601       break;
2602
2603     default:
2604       /* This should not happen */
2605       if (cname)
2606         *cname = xstrdup ("???");
2607       if (cfull_expression)
2608         *cfull_expression = xstrdup ("???");
2609       /* Don't set value and type, we don't know then. */
2610     }
2611 }
2612
2613 static char *
2614 c_name_of_child (struct varobj *parent, int index)
2615 {
2616   char *name;
2617   c_describe_child (parent, index, &name, NULL, NULL, NULL);
2618   return name;
2619 }
2620
2621 static char *
2622 c_path_expr_of_child (struct varobj *child)
2623 {
2624   c_describe_child (child->parent, child->index, NULL, NULL, NULL, 
2625                     &child->path_expr);
2626   return child->path_expr;
2627 }
2628
2629 /* If frame associated with VAR can be found, switch
2630    to it and return 1.  Otherwise, return 0.  */
2631 static int
2632 check_scope (struct varobj *var)
2633 {
2634   struct frame_info *fi;
2635   int scope;
2636
2637   fi = frame_find_by_id (var->root->frame);
2638   scope = fi != NULL;
2639
2640   if (fi)
2641     {
2642       CORE_ADDR pc = get_frame_pc (fi);
2643       if (pc <  BLOCK_START (var->root->valid_block) ||
2644           pc >= BLOCK_END (var->root->valid_block))
2645         scope = 0;
2646       else
2647         select_frame (fi);
2648     }
2649   return scope;
2650 }
2651
2652 static struct value *
2653 c_value_of_root (struct varobj **var_handle)
2654 {
2655   struct value *new_val = NULL;
2656   struct varobj *var = *var_handle;
2657   struct frame_info *fi;
2658   int within_scope = 0;
2659   struct cleanup *back_to;
2660                                                                  
2661   /*  Only root variables can be updated... */
2662   if (!is_root_p (var))
2663     /* Not a root var */
2664     return NULL;
2665
2666   back_to = make_cleanup_restore_current_thread ();
2667
2668   /* Determine whether the variable is still around. */
2669   if (var->root->valid_block == NULL || var->root->floating)
2670     within_scope = 1;
2671   else if (var->root->thread_id == 0)
2672     {
2673       /* The program was single-threaded when the variable object was
2674          created.  Technically, it's possible that the program became
2675          multi-threaded since then, but we don't support such
2676          scenario yet.  */
2677       within_scope = check_scope (var);   
2678     }
2679   else
2680     {
2681       ptid_t ptid = thread_id_to_pid (var->root->thread_id);
2682       if (in_thread_list (ptid))
2683         {
2684           switch_to_thread (ptid);
2685           within_scope = check_scope (var);
2686         }
2687     }
2688
2689   if (within_scope)
2690     {
2691       /* We need to catch errors here, because if evaluate
2692          expression fails we want to just return NULL.  */
2693       gdb_evaluate_expression (var->root->exp, &new_val);
2694       return new_val;
2695     }
2696
2697   do_cleanups (back_to);
2698
2699   return NULL;
2700 }
2701
2702 static struct value *
2703 c_value_of_child (struct varobj *parent, int index)
2704 {
2705   struct value *value = NULL;
2706   c_describe_child (parent, index, NULL, &value, NULL, NULL);
2707
2708   return value;
2709 }
2710
2711 static struct type *
2712 c_type_of_child (struct varobj *parent, int index)
2713 {
2714   struct type *type = NULL;
2715   c_describe_child (parent, index, NULL, NULL, &type, NULL);
2716   return type;
2717 }
2718
2719 static char *
2720 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2721 {
2722   /* BOGUS: if val_print sees a struct/class, or a reference to one,
2723      it will print out its children instead of "{...}".  So we need to
2724      catch that case explicitly.  */
2725   struct type *type = get_type (var);
2726
2727   /* If we have a custom formatter, return whatever string it has
2728      produced.  */
2729   if (var->pretty_printer && var->print_value)
2730     return xstrdup (var->print_value);
2731   
2732   /* Strip top-level references. */
2733   while (TYPE_CODE (type) == TYPE_CODE_REF)
2734     type = check_typedef (TYPE_TARGET_TYPE (type));
2735
2736   switch (TYPE_CODE (type))
2737     {
2738     case TYPE_CODE_STRUCT:
2739     case TYPE_CODE_UNION:
2740       return xstrdup ("{...}");
2741       /* break; */
2742
2743     case TYPE_CODE_ARRAY:
2744       {
2745         char *number;
2746         number = xstrprintf ("[%d]", var->num_children);
2747         return (number);
2748       }
2749       /* break; */
2750
2751     default:
2752       {
2753         if (var->value == NULL)
2754           {
2755             /* This can happen if we attempt to get the value of a struct
2756                member when the parent is an invalid pointer. This is an
2757                error condition, so we should tell the caller. */
2758             return NULL;
2759           }
2760         else
2761           {
2762             if (var->not_fetched && value_lazy (var->value))
2763               /* Frozen variable and no value yet.  We don't
2764                  implicitly fetch the value.  MI response will
2765                  use empty string for the value, which is OK.  */
2766               return NULL;
2767
2768             gdb_assert (varobj_value_is_changeable_p (var));
2769             gdb_assert (!value_lazy (var->value));
2770             
2771             /* If the specified format is the current one,
2772                we can reuse print_value */
2773             if (format == var->format)
2774               return xstrdup (var->print_value);
2775             else
2776               return value_get_print_value (var->value, format, 
2777                                             var->pretty_printer);
2778           }
2779       }
2780     }
2781 }
2782 \f
2783
2784 /* C++ */
2785
2786 static int
2787 cplus_number_of_children (struct varobj *var)
2788 {
2789   struct type *type;
2790   int children, dont_know;
2791
2792   dont_know = 1;
2793   children = 0;
2794
2795   if (!CPLUS_FAKE_CHILD (var))
2796     {
2797       type = get_value_type (var);
2798       adjust_value_for_child_access (NULL, &type, NULL);
2799
2800       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2801           ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2802         {
2803           int kids[3];
2804
2805           cplus_class_num_children (type, kids);
2806           if (kids[v_public] != 0)
2807             children++;
2808           if (kids[v_private] != 0)
2809             children++;
2810           if (kids[v_protected] != 0)
2811             children++;
2812
2813           /* Add any baseclasses */
2814           children += TYPE_N_BASECLASSES (type);
2815           dont_know = 0;
2816
2817           /* FIXME: save children in var */
2818         }
2819     }
2820   else
2821     {
2822       int kids[3];
2823
2824       type = get_value_type (var->parent);
2825       adjust_value_for_child_access (NULL, &type, NULL);
2826
2827       cplus_class_num_children (type, kids);
2828       if (strcmp (var->name, "public") == 0)
2829         children = kids[v_public];
2830       else if (strcmp (var->name, "private") == 0)
2831         children = kids[v_private];
2832       else
2833         children = kids[v_protected];
2834       dont_know = 0;
2835     }
2836
2837   if (dont_know)
2838     children = c_number_of_children (var);
2839
2840   return children;
2841 }
2842
2843 /* Compute # of public, private, and protected variables in this class.
2844    That means we need to descend into all baseclasses and find out
2845    how many are there, too. */
2846 static void
2847 cplus_class_num_children (struct type *type, int children[3])
2848 {
2849   int i;
2850
2851   children[v_public] = 0;
2852   children[v_private] = 0;
2853   children[v_protected] = 0;
2854
2855   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2856     {
2857       /* If we have a virtual table pointer, omit it. */
2858       if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2859         continue;
2860
2861       if (TYPE_FIELD_PROTECTED (type, i))
2862         children[v_protected]++;
2863       else if (TYPE_FIELD_PRIVATE (type, i))
2864         children[v_private]++;
2865       else
2866         children[v_public]++;
2867     }
2868 }
2869
2870 static char *
2871 cplus_name_of_variable (struct varobj *parent)
2872 {
2873   return c_name_of_variable (parent);
2874 }
2875
2876 enum accessibility { private_field, protected_field, public_field };
2877
2878 /* Check if field INDEX of TYPE has the specified accessibility.
2879    Return 0 if so and 1 otherwise.  */
2880 static int 
2881 match_accessibility (struct type *type, int index, enum accessibility acc)
2882 {
2883   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2884     return 1;
2885   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2886     return 1;
2887   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2888            && !TYPE_FIELD_PROTECTED (type, index))
2889     return 1;
2890   else
2891     return 0;
2892 }
2893
2894 static void
2895 cplus_describe_child (struct varobj *parent, int index,
2896                       char **cname, struct value **cvalue, struct type **ctype,
2897                       char **cfull_expression)
2898 {
2899   char *name = NULL;
2900   struct value *value;
2901   struct type *type;
2902   int was_ptr;
2903   char *parent_expression = NULL;
2904
2905   if (cname)
2906     *cname = NULL;
2907   if (cvalue)
2908     *cvalue = NULL;
2909   if (ctype)
2910     *ctype = NULL;
2911   if (cfull_expression)
2912     *cfull_expression = NULL;
2913
2914   if (CPLUS_FAKE_CHILD (parent))
2915     {
2916       value = parent->parent->value;
2917       type = get_value_type (parent->parent);
2918       if (cfull_expression)
2919         parent_expression = varobj_get_path_expr (parent->parent);
2920     }
2921   else
2922     {
2923       value = parent->value;
2924       type = get_value_type (parent);
2925       if (cfull_expression)
2926         parent_expression = varobj_get_path_expr (parent);
2927     }
2928
2929   adjust_value_for_child_access (&value, &type, &was_ptr);
2930
2931   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2932       || TYPE_CODE (type) == TYPE_CODE_UNION)
2933     {
2934       char *join = was_ptr ? "->" : ".";
2935       if (CPLUS_FAKE_CHILD (parent))
2936         {
2937           /* The fields of the class type are ordered as they
2938              appear in the class.  We are given an index for a
2939              particular access control type ("public","protected",
2940              or "private").  We must skip over fields that don't
2941              have the access control we are looking for to properly
2942              find the indexed field. */
2943           int type_index = TYPE_N_BASECLASSES (type);
2944           enum accessibility acc = public_field;
2945           if (strcmp (parent->name, "private") == 0)
2946             acc = private_field;
2947           else if (strcmp (parent->name, "protected") == 0)
2948             acc = protected_field;
2949
2950           while (index >= 0)
2951             {
2952               if (TYPE_VPTR_BASETYPE (type) == type
2953                   && type_index == TYPE_VPTR_FIELDNO (type))
2954                 ; /* ignore vptr */
2955               else if (match_accessibility (type, type_index, acc))
2956                     --index;
2957                   ++type_index;
2958             }
2959           --type_index;
2960
2961           if (cname)
2962             *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2963
2964           if (cvalue && value)
2965             *cvalue = value_struct_element_index (value, type_index);
2966
2967           if (ctype)
2968             *ctype = TYPE_FIELD_TYPE (type, type_index);
2969
2970           if (cfull_expression)
2971             *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2972                                             join, 
2973                                             TYPE_FIELD_NAME (type, type_index));
2974         }
2975       else if (index < TYPE_N_BASECLASSES (type))
2976         {
2977           /* This is a baseclass.  */
2978           if (cname)
2979             *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2980
2981           if (cvalue && value)
2982             {
2983               *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2984               release_value (*cvalue);
2985             }
2986
2987           if (ctype)
2988             {
2989               *ctype = TYPE_FIELD_TYPE (type, index);
2990             }
2991
2992           if (cfull_expression)
2993             {
2994               char *ptr = was_ptr ? "*" : "";
2995               /* Cast the parent to the base' type. Note that in gdb,
2996                  expression like 
2997                          (Base1)d
2998                  will create an lvalue, for all appearences, so we don't
2999                  need to use more fancy:
3000                          *(Base1*)(&d)
3001                  construct.  */
3002               *cfull_expression = xstrprintf ("(%s(%s%s) %s)", 
3003                                               ptr, 
3004                                               TYPE_FIELD_NAME (type, index),
3005                                               ptr,
3006                                               parent_expression);
3007             }
3008         }
3009       else
3010         {
3011           char *access = NULL;
3012           int children[3];
3013           cplus_class_num_children (type, children);
3014
3015           /* Everything beyond the baseclasses can
3016              only be "public", "private", or "protected"
3017
3018              The special "fake" children are always output by varobj in
3019              this order. So if INDEX == 2, it MUST be "protected". */
3020           index -= TYPE_N_BASECLASSES (type);
3021           switch (index)
3022             {
3023             case 0:
3024               if (children[v_public] > 0)
3025                 access = "public";
3026               else if (children[v_private] > 0)
3027                 access = "private";
3028               else 
3029                 access = "protected";
3030               break;
3031             case 1:
3032               if (children[v_public] > 0)
3033                 {
3034                   if (children[v_private] > 0)
3035                     access = "private";
3036                   else
3037                     access = "protected";
3038                 }
3039               else if (children[v_private] > 0)
3040                 access = "protected";
3041               break;
3042             case 2:
3043               /* Must be protected */
3044               access = "protected";
3045               break;
3046             default:
3047               /* error! */
3048               break;
3049             }
3050
3051           gdb_assert (access);
3052           if (cname)
3053             *cname = xstrdup (access);
3054
3055           /* Value and type and full expression are null here.  */
3056         }
3057     }
3058   else
3059     {
3060       c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3061     }  
3062 }
3063
3064 static char *
3065 cplus_name_of_child (struct varobj *parent, int index)
3066 {
3067   char *name = NULL;
3068   cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3069   return name;
3070 }
3071
3072 static char *
3073 cplus_path_expr_of_child (struct varobj *child)
3074 {
3075   cplus_describe_child (child->parent, child->index, NULL, NULL, NULL, 
3076                         &child->path_expr);
3077   return child->path_expr;
3078 }
3079
3080 static struct value *
3081 cplus_value_of_root (struct varobj **var_handle)
3082 {
3083   return c_value_of_root (var_handle);
3084 }
3085
3086 static struct value *
3087 cplus_value_of_child (struct varobj *parent, int index)
3088 {
3089   struct value *value = NULL;
3090   cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3091   return value;
3092 }
3093
3094 static struct type *
3095 cplus_type_of_child (struct varobj *parent, int index)
3096 {
3097   struct type *type = NULL;
3098   cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3099   return type;
3100 }
3101
3102 static char *
3103 cplus_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3104 {
3105
3106   /* If we have one of our special types, don't print out
3107      any value. */
3108   if (CPLUS_FAKE_CHILD (var))
3109     return xstrdup ("");
3110
3111   return c_value_of_variable (var, format);
3112 }
3113 \f
3114 /* Java */
3115
3116 static int
3117 java_number_of_children (struct varobj *var)
3118 {
3119   return cplus_number_of_children (var);
3120 }
3121
3122 static char *
3123 java_name_of_variable (struct varobj *parent)
3124 {
3125   char *p, *name;
3126
3127   name = cplus_name_of_variable (parent);
3128   /* If  the name has "-" in it, it is because we
3129      needed to escape periods in the name... */
3130   p = name;
3131
3132   while (*p != '\000')
3133     {
3134       if (*p == '-')
3135         *p = '.';
3136       p++;
3137     }
3138
3139   return name;
3140 }
3141
3142 static char *
3143 java_name_of_child (struct varobj *parent, int index)
3144 {
3145   char *name, *p;
3146
3147   name = cplus_name_of_child (parent, index);
3148   /* Escape any periods in the name... */
3149   p = name;
3150
3151   while (*p != '\000')
3152     {
3153       if (*p == '.')
3154         *p = '-';
3155       p++;
3156     }
3157
3158   return name;
3159 }
3160
3161 static char *
3162 java_path_expr_of_child (struct varobj *child)
3163 {
3164   return NULL;
3165 }
3166
3167 static struct value *
3168 java_value_of_root (struct varobj **var_handle)
3169 {
3170   return cplus_value_of_root (var_handle);
3171 }
3172
3173 static struct value *
3174 java_value_of_child (struct varobj *parent, int index)
3175 {
3176   return cplus_value_of_child (parent, index);
3177 }
3178
3179 static struct type *
3180 java_type_of_child (struct varobj *parent, int index)
3181 {
3182   return cplus_type_of_child (parent, index);
3183 }
3184
3185 static char *
3186 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3187 {
3188   return cplus_value_of_variable (var, format);
3189 }
3190 \f
3191 extern void _initialize_varobj (void);
3192 void
3193 _initialize_varobj (void)
3194 {
3195   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
3196
3197   varobj_table = xmalloc (sizeof_table);
3198   memset (varobj_table, 0, sizeof_table);
3199
3200   add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
3201                             &varobjdebug, _("\
3202 Set varobj debugging."), _("\
3203 Show varobj debugging."), _("\
3204 When non-zero, varobj debugging is enabled."),
3205                             NULL,
3206                             show_varobjdebug,
3207                             &setlist, &showlist);
3208 }
3209
3210 /* Invalidate the varobjs that are tied to locals and re-create the ones that
3211    are defined on globals.
3212    Invalidated varobjs will be always printed in_scope="invalid".  */
3213
3214 void 
3215 varobj_invalidate (void)
3216 {
3217   struct varobj **all_rootvarobj;
3218   struct varobj **varp;
3219
3220   if (varobj_list (&all_rootvarobj) > 0)
3221     {
3222       varp = all_rootvarobj;
3223       while (*varp != NULL)
3224         {
3225           /* Floating varobjs are reparsed on each stop, so we don't care if
3226              the presently parsed expression refers to something that's gone.
3227              */
3228           if ((*varp)->root->floating)
3229             continue;
3230
3231           /* global var must be re-evaluated.  */     
3232           if ((*varp)->root->valid_block == NULL)
3233             {
3234               struct varobj *tmp_var;
3235
3236               /* Try to create a varobj with same expression.  If we succeed
3237                  replace the old varobj, otherwise invalidate it.  */
3238               tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0,
3239                                        USE_CURRENT_FRAME);
3240               if (tmp_var != NULL) 
3241                 { 
3242                   tmp_var->obj_name = xstrdup ((*varp)->obj_name);
3243                   varobj_delete (*varp, NULL, 0);
3244                   install_variable (tmp_var);
3245                 }
3246               else
3247                 (*varp)->root->is_valid = 0;
3248             }
3249           else /* locals must be invalidated.  */
3250             (*varp)->root->is_valid = 0;
3251
3252           varp++;
3253         }
3254     }
3255   xfree (all_rootvarobj);
3256   return;
3257 }