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