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