Home | History | Annotate | Line # | Download | only in gdb
varobj.c revision 1.6
      1 /* Implementation of the GDB variable objects API.
      2 
      3    Copyright (C) 1999-2016 Free Software Foundation, Inc.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #include "defs.h"
     19 #include "value.h"
     20 #include "expression.h"
     21 #include "frame.h"
     22 #include "language.h"
     23 #include "gdbcmd.h"
     24 #include "block.h"
     25 #include "valprint.h"
     26 #include "gdb_regex.h"
     27 
     28 #include "varobj.h"
     29 #include "vec.h"
     30 #include "gdbthread.h"
     31 #include "inferior.h"
     32 #include "varobj-iter.h"
     33 
     34 #if HAVE_PYTHON
     35 #include "python/python.h"
     36 #include "python/python-internal.h"
     37 #else
     38 typedef int PyObject;
     39 #endif
     40 
     41 /* Non-zero if we want to see trace of varobj level stuff.  */
     42 
     43 unsigned int varobjdebug = 0;
     44 static void
     45 show_varobjdebug (struct ui_file *file, int from_tty,
     46 		  struct cmd_list_element *c, const char *value)
     47 {
     48   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
     49 }
     50 
     51 /* String representations of gdb's format codes.  */
     52 char *varobj_format_string[] =
     53   { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
     54 
     55 /* True if we want to allow Python-based pretty-printing.  */
     56 static int pretty_printing = 0;
     57 
     58 void
     59 varobj_enable_pretty_printing (void)
     60 {
     61   pretty_printing = 1;
     62 }
     63 
     64 /* Data structures */
     65 
     66 /* Every root variable has one of these structures saved in its
     67    varobj.  Members which must be free'd are noted.  */
     68 struct varobj_root
     69 {
     70 
     71   /* Alloc'd expression for this parent.  */
     72   struct expression *exp;
     73 
     74   /* Block for which this expression is valid.  */
     75   const struct block *valid_block;
     76 
     77   /* The frame for this expression.  This field is set iff valid_block is
     78      not NULL.  */
     79   struct frame_id frame;
     80 
     81   /* The global thread ID that this varobj_root belongs to.  This field
     82      is only valid if valid_block is not NULL.
     83      When not 0, indicates which thread 'frame' belongs to.
     84      When 0, indicates that the thread list was empty when the varobj_root
     85      was created.  */
     86   int thread_id;
     87 
     88   /* If 1, the -var-update always recomputes the value in the
     89      current thread and frame.  Otherwise, variable object is
     90      always updated in the specific scope/thread/frame.  */
     91   int floating;
     92 
     93   /* Flag that indicates validity: set to 0 when this varobj_root refers
     94      to symbols that do not exist anymore.  */
     95   int is_valid;
     96 
     97   /* Language-related operations for this variable and its
     98      children.  */
     99   const struct lang_varobj_ops *lang_ops;
    100 
    101   /* The varobj for this root node.  */
    102   struct varobj *rootvar;
    103 
    104   /* Next root variable */
    105   struct varobj_root *next;
    106 };
    107 
    108 /* Dynamic part of varobj.  */
    109 
    110 struct varobj_dynamic
    111 {
    112   /* Whether the children of this varobj were requested.  This field is
    113      used to decide if dynamic varobj should recompute their children.
    114      In the event that the frontend never asked for the children, we
    115      can avoid that.  */
    116   int children_requested;
    117 
    118   /* The pretty-printer constructor.  If NULL, then the default
    119      pretty-printer will be looked up.  If None, then no
    120      pretty-printer will be installed.  */
    121   PyObject *constructor;
    122 
    123   /* The pretty-printer that has been constructed.  If NULL, then a
    124      new printer object is needed, and one will be constructed.  */
    125   PyObject *pretty_printer;
    126 
    127   /* The iterator returned by the printer's 'children' method, or NULL
    128      if not available.  */
    129   struct varobj_iter *child_iter;
    130 
    131   /* We request one extra item from the iterator, so that we can
    132      report to the caller whether there are more items than we have
    133      already reported.  However, we don't want to install this value
    134      when we read it, because that will mess up future updates.  So,
    135      we stash it here instead.  */
    136   varobj_item *saved_item;
    137 };
    138 
    139 /* A list of varobjs */
    140 
    141 struct vlist
    142 {
    143   struct varobj *var;
    144   struct vlist *next;
    145 };
    146 
    147 /* Private function prototypes */
    148 
    149 /* Helper functions for the above subcommands.  */
    150 
    151 static int delete_variable (struct varobj *, int);
    152 
    153 static void delete_variable_1 (int *, struct varobj *, int, int);
    154 
    155 static int install_variable (struct varobj *);
    156 
    157 static void uninstall_variable (struct varobj *);
    158 
    159 static struct varobj *create_child (struct varobj *, int, char *);
    160 
    161 static struct varobj *
    162 create_child_with_value (struct varobj *parent, int index,
    163 			 struct varobj_item *item);
    164 
    165 /* Utility routines */
    166 
    167 static struct varobj *new_variable (void);
    168 
    169 static struct varobj *new_root_variable (void);
    170 
    171 static void free_variable (struct varobj *var);
    172 
    173 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
    174 
    175 static enum varobj_display_formats variable_default_display (struct varobj *);
    176 
    177 static int update_type_if_necessary (struct varobj *var,
    178 				     struct value *new_value);
    179 
    180 static int install_new_value (struct varobj *var, struct value *value,
    181 			      int initial);
    182 
    183 /* Language-specific routines.  */
    184 
    185 static int number_of_children (const struct varobj *);
    186 
    187 static char *name_of_variable (const struct varobj *);
    188 
    189 static char *name_of_child (struct varobj *, int);
    190 
    191 static struct value *value_of_root (struct varobj **var_handle, int *);
    192 
    193 static struct value *value_of_child (const struct varobj *parent, int index);
    194 
    195 static char *my_value_of_variable (struct varobj *var,
    196 				   enum varobj_display_formats format);
    197 
    198 static int is_root_p (const struct varobj *var);
    199 
    200 static struct varobj *varobj_add_child (struct varobj *var,
    201 					struct varobj_item *item);
    202 
    203 /* Private data */
    204 
    205 /* Mappings of varobj_display_formats enums to gdb's format codes.  */
    206 static int format_code[] = { 0, 't', 'd', 'x', 'o', 'z' };
    207 
    208 /* Header of the list of root variable objects.  */
    209 static struct varobj_root *rootlist;
    210 
    211 /* Prime number indicating the number of buckets in the hash table.  */
    212 /* A prime large enough to avoid too many collisions.  */
    213 #define VAROBJ_TABLE_SIZE 227
    214 
    215 /* Pointer to the varobj hash table (built at run time).  */
    216 static struct vlist **varobj_table;
    217 
    218 
    219 
    221 /* API Implementation */
    222 static int
    223 is_root_p (const struct varobj *var)
    224 {
    225   return (var->root->rootvar == var);
    226 }
    227 
    228 #ifdef HAVE_PYTHON
    229 /* Helper function to install a Python environment suitable for
    230    use during operations on VAR.  */
    231 struct cleanup *
    232 varobj_ensure_python_env (const struct varobj *var)
    233 {
    234   return ensure_python_env (var->root->exp->gdbarch,
    235 			    var->root->exp->language_defn);
    236 }
    237 #endif
    238 
    239 /* Return the full FRAME which corresponds to the given CORE_ADDR
    240    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
    241 
    242 static struct frame_info *
    243 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
    244 {
    245   struct frame_info *frame = NULL;
    246 
    247   if (frame_addr == (CORE_ADDR) 0)
    248     return NULL;
    249 
    250   for (frame = get_current_frame ();
    251        frame != NULL;
    252        frame = get_prev_frame (frame))
    253     {
    254       /* The CORE_ADDR we get as argument was parsed from a string GDB
    255 	 output as $fp.  This output got truncated to gdbarch_addr_bit.
    256 	 Truncate the frame base address in the same manner before
    257 	 comparing it against our argument.  */
    258       CORE_ADDR frame_base = get_frame_base_address (frame);
    259       int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
    260 
    261       if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
    262 	frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
    263 
    264       if (frame_base == frame_addr)
    265 	return frame;
    266     }
    267 
    268   return NULL;
    269 }
    270 
    271 /* Creates a varobj (not its children).  */
    272 
    273 struct varobj *
    274 varobj_create (char *objname,
    275 	       char *expression, CORE_ADDR frame, enum varobj_type type)
    276 {
    277   struct varobj *var;
    278   struct cleanup *old_chain;
    279 
    280   /* Fill out a varobj structure for the (root) variable being constructed.  */
    281   var = new_root_variable ();
    282   old_chain = make_cleanup_free_variable (var);
    283 
    284   if (expression != NULL)
    285     {
    286       struct frame_info *fi;
    287       struct frame_id old_id = null_frame_id;
    288       const struct block *block;
    289       const char *p;
    290       struct value *value = NULL;
    291       CORE_ADDR pc;
    292 
    293       /* Parse and evaluate the expression, filling in as much of the
    294          variable's data as possible.  */
    295 
    296       if (has_stack_frames ())
    297 	{
    298 	  /* Allow creator to specify context of variable.  */
    299 	  if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
    300 	    fi = get_selected_frame (NULL);
    301 	  else
    302 	    /* FIXME: cagney/2002-11-23: This code should be doing a
    303 	       lookup using the frame ID and not just the frame's
    304 	       ``address''.  This, of course, means an interface
    305 	       change.  However, with out that interface change ISAs,
    306 	       such as the ia64 with its two stacks, won't work.
    307 	       Similar goes for the case where there is a frameless
    308 	       function.  */
    309 	    fi = find_frame_addr_in_frame_chain (frame);
    310 	}
    311       else
    312 	fi = NULL;
    313 
    314       /* frame = -2 means always use selected frame.  */
    315       if (type == USE_SELECTED_FRAME)
    316 	var->root->floating = 1;
    317 
    318       pc = 0;
    319       block = NULL;
    320       if (fi != NULL)
    321 	{
    322 	  block = get_frame_block (fi, 0);
    323 	  pc = get_frame_pc (fi);
    324 	}
    325 
    326       p = expression;
    327       innermost_block = NULL;
    328       /* Wrap the call to parse expression, so we can
    329          return a sensible error.  */
    330       TRY
    331 	{
    332 	  var->root->exp = parse_exp_1 (&p, pc, block, 0);
    333 	}
    334 
    335       CATCH (except, RETURN_MASK_ERROR)
    336 	{
    337 	  do_cleanups (old_chain);
    338 	  return NULL;
    339 	}
    340       END_CATCH
    341 
    342       /* Don't allow variables to be created for types.  */
    343       if (var->root->exp->elts[0].opcode == OP_TYPE
    344 	  || var->root->exp->elts[0].opcode == OP_TYPEOF
    345 	  || var->root->exp->elts[0].opcode == OP_DECLTYPE)
    346 	{
    347 	  do_cleanups (old_chain);
    348 	  fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
    349 			      " as an expression.\n");
    350 	  return NULL;
    351 	}
    352 
    353       var->format = variable_default_display (var);
    354       var->root->valid_block = innermost_block;
    355       var->name = xstrdup (expression);
    356       /* For a root var, the name and the expr are the same.  */
    357       var->path_expr = xstrdup (expression);
    358 
    359       /* When the frame is different from the current frame,
    360          we must select the appropriate frame before parsing
    361          the expression, otherwise the value will not be current.
    362          Since select_frame is so benign, just call it for all cases.  */
    363       if (innermost_block)
    364 	{
    365 	  /* User could specify explicit FRAME-ADDR which was not found but
    366 	     EXPRESSION is frame specific and we would not be able to evaluate
    367 	     it correctly next time.  With VALID_BLOCK set we must also set
    368 	     FRAME and THREAD_ID.  */
    369 	  if (fi == NULL)
    370 	    error (_("Failed to find the specified frame"));
    371 
    372 	  var->root->frame = get_frame_id (fi);
    373 	  var->root->thread_id = ptid_to_global_thread_id (inferior_ptid);
    374 	  old_id = get_frame_id (get_selected_frame (NULL));
    375 	  select_frame (fi);
    376 	}
    377 
    378       /* We definitely need to catch errors here.
    379          If evaluate_expression succeeds we got the value we wanted.
    380          But if it fails, we still go on with a call to evaluate_type().  */
    381       TRY
    382 	{
    383 	  value = evaluate_expression (var->root->exp);
    384 	}
    385       CATCH (except, RETURN_MASK_ERROR)
    386 	{
    387 	  /* Error getting the value.  Try to at least get the
    388 	     right type.  */
    389 	  struct value *type_only_value = evaluate_type (var->root->exp);
    390 
    391 	  var->type = value_type (type_only_value);
    392 	}
    393       END_CATCH
    394 
    395       if (value != NULL)
    396 	{
    397 	  int real_type_found = 0;
    398 
    399 	  var->type = value_actual_type (value, 0, &real_type_found);
    400 	  if (real_type_found)
    401 	    value = value_cast (var->type, value);
    402 	}
    403 
    404       /* Set language info */
    405       var->root->lang_ops = var->root->exp->language_defn->la_varobj_ops;
    406 
    407       install_new_value (var, value, 1 /* Initial assignment */);
    408 
    409       /* Set ourselves as our root.  */
    410       var->root->rootvar = var;
    411 
    412       /* Reset the selected frame.  */
    413       if (frame_id_p (old_id))
    414 	select_frame (frame_find_by_id (old_id));
    415     }
    416 
    417   /* If the variable object name is null, that means this
    418      is a temporary variable, so don't install it.  */
    419 
    420   if ((var != NULL) && (objname != NULL))
    421     {
    422       var->obj_name = xstrdup (objname);
    423 
    424       /* If a varobj name is duplicated, the install will fail so
    425          we must cleanup.  */
    426       if (!install_variable (var))
    427 	{
    428 	  do_cleanups (old_chain);
    429 	  return NULL;
    430 	}
    431     }
    432 
    433   discard_cleanups (old_chain);
    434   return var;
    435 }
    436 
    437 /* Generates an unique name that can be used for a varobj.  */
    438 
    439 char *
    440 varobj_gen_name (void)
    441 {
    442   static int id = 0;
    443   char *obj_name;
    444 
    445   /* Generate a name for this object.  */
    446   id++;
    447   obj_name = xstrprintf ("var%d", id);
    448 
    449   return obj_name;
    450 }
    451 
    452 /* Given an OBJNAME, returns the pointer to the corresponding varobj.  Call
    453    error if OBJNAME cannot be found.  */
    454 
    455 struct varobj *
    456 varobj_get_handle (char *objname)
    457 {
    458   struct vlist *cv;
    459   const char *chp;
    460   unsigned int index = 0;
    461   unsigned int i = 1;
    462 
    463   for (chp = objname; *chp; chp++)
    464     {
    465       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
    466     }
    467 
    468   cv = *(varobj_table + index);
    469   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
    470     cv = cv->next;
    471 
    472   if (cv == NULL)
    473     error (_("Variable object not found"));
    474 
    475   return cv->var;
    476 }
    477 
    478 /* Given the handle, return the name of the object.  */
    479 
    480 char *
    481 varobj_get_objname (const struct varobj *var)
    482 {
    483   return var->obj_name;
    484 }
    485 
    486 /* Given the handle, return the expression represented by the object.  The
    487    result must be freed by the caller.  */
    488 
    489 char *
    490 varobj_get_expression (const struct varobj *var)
    491 {
    492   return name_of_variable (var);
    493 }
    494 
    495 /* See varobj.h.  */
    496 
    497 int
    498 varobj_delete (struct varobj *var, int only_children)
    499 {
    500   return delete_variable (var, only_children);
    501 }
    502 
    503 #if HAVE_PYTHON
    504 
    505 /* Convenience function for varobj_set_visualizer.  Instantiate a
    506    pretty-printer for a given value.  */
    507 static PyObject *
    508 instantiate_pretty_printer (PyObject *constructor, struct value *value)
    509 {
    510   PyObject *val_obj = NULL;
    511   PyObject *printer;
    512 
    513   val_obj = value_to_value_object (value);
    514   if (! val_obj)
    515     return NULL;
    516 
    517   printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
    518   Py_DECREF (val_obj);
    519   return printer;
    520 }
    521 
    522 #endif
    523 
    524 /* Set/Get variable object display format.  */
    525 
    526 enum varobj_display_formats
    527 varobj_set_display_format (struct varobj *var,
    528 			   enum varobj_display_formats format)
    529 {
    530   switch (format)
    531     {
    532     case FORMAT_NATURAL:
    533     case FORMAT_BINARY:
    534     case FORMAT_DECIMAL:
    535     case FORMAT_HEXADECIMAL:
    536     case FORMAT_OCTAL:
    537     case FORMAT_ZHEXADECIMAL:
    538       var->format = format;
    539       break;
    540 
    541     default:
    542       var->format = variable_default_display (var);
    543     }
    544 
    545   if (varobj_value_is_changeable_p (var)
    546       && var->value && !value_lazy (var->value))
    547     {
    548       xfree (var->print_value);
    549       var->print_value = varobj_value_get_print_value (var->value,
    550 						       var->format, var);
    551     }
    552 
    553   return var->format;
    554 }
    555 
    556 enum varobj_display_formats
    557 varobj_get_display_format (const struct varobj *var)
    558 {
    559   return var->format;
    560 }
    561 
    562 char *
    563 varobj_get_display_hint (const struct varobj *var)
    564 {
    565   char *result = NULL;
    566 
    567 #if HAVE_PYTHON
    568   struct cleanup *back_to;
    569 
    570   if (!gdb_python_initialized)
    571     return NULL;
    572 
    573   back_to = varobj_ensure_python_env (var);
    574 
    575   if (var->dynamic->pretty_printer != NULL)
    576     result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
    577 
    578   do_cleanups (back_to);
    579 #endif
    580 
    581   return result;
    582 }
    583 
    584 /* Return true if the varobj has items after TO, false otherwise.  */
    585 
    586 int
    587 varobj_has_more (const struct varobj *var, int to)
    588 {
    589   if (VEC_length (varobj_p, var->children) > to)
    590     return 1;
    591   return ((to == -1 || VEC_length (varobj_p, var->children) == to)
    592 	  && (var->dynamic->saved_item != NULL));
    593 }
    594 
    595 /* If the variable object is bound to a specific thread, that
    596    is its evaluation can always be done in context of a frame
    597    inside that thread, returns GDB id of the thread -- which
    598    is always positive.  Otherwise, returns -1.  */
    599 int
    600 varobj_get_thread_id (const struct varobj *var)
    601 {
    602   if (var->root->valid_block && var->root->thread_id > 0)
    603     return var->root->thread_id;
    604   else
    605     return -1;
    606 }
    607 
    608 void
    609 varobj_set_frozen (struct varobj *var, int frozen)
    610 {
    611   /* When a variable is unfrozen, we don't fetch its value.
    612      The 'not_fetched' flag remains set, so next -var-update
    613      won't complain.
    614 
    615      We don't fetch the value, because for structures the client
    616      should do -var-update anyway.  It would be bad to have different
    617      client-size logic for structure and other types.  */
    618   var->frozen = frozen;
    619 }
    620 
    621 int
    622 varobj_get_frozen (const struct varobj *var)
    623 {
    624   return var->frozen;
    625 }
    626 
    627 /* A helper function that restricts a range to what is actually
    628    available in a VEC.  This follows the usual rules for the meaning
    629    of FROM and TO -- if either is negative, the entire range is
    630    used.  */
    631 
    632 void
    633 varobj_restrict_range (VEC (varobj_p) *children, int *from, int *to)
    634 {
    635   if (*from < 0 || *to < 0)
    636     {
    637       *from = 0;
    638       *to = VEC_length (varobj_p, children);
    639     }
    640   else
    641     {
    642       if (*from > VEC_length (varobj_p, children))
    643 	*from = VEC_length (varobj_p, children);
    644       if (*to > VEC_length (varobj_p, children))
    645 	*to = VEC_length (varobj_p, children);
    646       if (*from > *to)
    647 	*from = *to;
    648     }
    649 }
    650 
    651 /* A helper for update_dynamic_varobj_children that installs a new
    652    child when needed.  */
    653 
    654 static void
    655 install_dynamic_child (struct varobj *var,
    656 		       VEC (varobj_p) **changed,
    657 		       VEC (varobj_p) **type_changed,
    658 		       VEC (varobj_p) **newobj,
    659 		       VEC (varobj_p) **unchanged,
    660 		       int *cchanged,
    661 		       int index,
    662 		       struct varobj_item *item)
    663 {
    664   if (VEC_length (varobj_p, var->children) < index + 1)
    665     {
    666       /* There's no child yet.  */
    667       struct varobj *child = varobj_add_child (var, item);
    668 
    669       if (newobj)
    670 	{
    671 	  VEC_safe_push (varobj_p, *newobj, child);
    672 	  *cchanged = 1;
    673 	}
    674     }
    675   else
    676     {
    677       varobj_p existing = VEC_index (varobj_p, var->children, index);
    678       int type_updated = update_type_if_necessary (existing, item->value);
    679 
    680       if (type_updated)
    681 	{
    682 	  if (type_changed)
    683 	    VEC_safe_push (varobj_p, *type_changed, existing);
    684 	}
    685       if (install_new_value (existing, item->value, 0))
    686 	{
    687 	  if (!type_updated && changed)
    688 	    VEC_safe_push (varobj_p, *changed, existing);
    689 	}
    690       else if (!type_updated && unchanged)
    691 	VEC_safe_push (varobj_p, *unchanged, existing);
    692     }
    693 }
    694 
    695 #if HAVE_PYTHON
    696 
    697 static int
    698 dynamic_varobj_has_child_method (const struct varobj *var)
    699 {
    700   struct cleanup *back_to;
    701   PyObject *printer = var->dynamic->pretty_printer;
    702   int result;
    703 
    704   if (!gdb_python_initialized)
    705     return 0;
    706 
    707   back_to = varobj_ensure_python_env (var);
    708   result = PyObject_HasAttr (printer, gdbpy_children_cst);
    709   do_cleanups (back_to);
    710   return result;
    711 }
    712 #endif
    713 
    714 /* A factory for creating dynamic varobj's iterators.  Returns an
    715    iterator object suitable for iterating over VAR's children.  */
    716 
    717 static struct varobj_iter *
    718 varobj_get_iterator (struct varobj *var)
    719 {
    720 #if HAVE_PYTHON
    721   if (var->dynamic->pretty_printer)
    722     return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
    723 #endif
    724 
    725   gdb_assert_not_reached (_("\
    726 requested an iterator from a non-dynamic varobj"));
    727 }
    728 
    729 /* Release and clear VAR's saved item, if any.  */
    730 
    731 static void
    732 varobj_clear_saved_item (struct varobj_dynamic *var)
    733 {
    734   if (var->saved_item != NULL)
    735     {
    736       value_free (var->saved_item->value);
    737       xfree (var->saved_item);
    738       var->saved_item = NULL;
    739     }
    740 }
    741 
    742 static int
    743 update_dynamic_varobj_children (struct varobj *var,
    744 				VEC (varobj_p) **changed,
    745 				VEC (varobj_p) **type_changed,
    746 				VEC (varobj_p) **newobj,
    747 				VEC (varobj_p) **unchanged,
    748 				int *cchanged,
    749 				int update_children,
    750 				int from,
    751 				int to)
    752 {
    753   int i;
    754 
    755   *cchanged = 0;
    756 
    757   if (update_children || var->dynamic->child_iter == NULL)
    758     {
    759       varobj_iter_delete (var->dynamic->child_iter);
    760       var->dynamic->child_iter = varobj_get_iterator (var);
    761 
    762       varobj_clear_saved_item (var->dynamic);
    763 
    764       i = 0;
    765 
    766       if (var->dynamic->child_iter == NULL)
    767 	return 0;
    768     }
    769   else
    770     i = VEC_length (varobj_p, var->children);
    771 
    772   /* We ask for one extra child, so that MI can report whether there
    773      are more children.  */
    774   for (; to < 0 || i < to + 1; ++i)
    775     {
    776       varobj_item *item;
    777 
    778       /* See if there was a leftover from last time.  */
    779       if (var->dynamic->saved_item != NULL)
    780 	{
    781 	  item = var->dynamic->saved_item;
    782 	  var->dynamic->saved_item = NULL;
    783 	}
    784       else
    785 	{
    786 	  item = varobj_iter_next (var->dynamic->child_iter);
    787 	  /* Release vitem->value so its lifetime is not bound to the
    788 	     execution of a command.  */
    789 	  if (item != NULL && item->value != NULL)
    790 	    release_value_or_incref (item->value);
    791 	}
    792 
    793       if (item == NULL)
    794 	{
    795 	  /* Iteration is done.  Remove iterator from VAR.  */
    796 	  varobj_iter_delete (var->dynamic->child_iter);
    797 	  var->dynamic->child_iter = NULL;
    798 	  break;
    799 	}
    800       /* We don't want to push the extra child on any report list.  */
    801       if (to < 0 || i < to)
    802 	{
    803 	  int can_mention = from < 0 || i >= from;
    804 
    805 	  install_dynamic_child (var, can_mention ? changed : NULL,
    806 				 can_mention ? type_changed : NULL,
    807 				 can_mention ? newobj : NULL,
    808 				 can_mention ? unchanged : NULL,
    809 				 can_mention ? cchanged : NULL, i,
    810 				 item);
    811 
    812 	  xfree (item);
    813 	}
    814       else
    815 	{
    816 	  var->dynamic->saved_item = item;
    817 
    818 	  /* We want to truncate the child list just before this
    819 	     element.  */
    820 	  break;
    821 	}
    822     }
    823 
    824   if (i < VEC_length (varobj_p, var->children))
    825     {
    826       int j;
    827 
    828       *cchanged = 1;
    829       for (j = i; j < VEC_length (varobj_p, var->children); ++j)
    830 	varobj_delete (VEC_index (varobj_p, var->children, j), 0);
    831       VEC_truncate (varobj_p, var->children, i);
    832     }
    833 
    834   /* If there are fewer children than requested, note that the list of
    835      children changed.  */
    836   if (to >= 0 && VEC_length (varobj_p, var->children) < to)
    837     *cchanged = 1;
    838 
    839   var->num_children = VEC_length (varobj_p, var->children);
    840 
    841   return 1;
    842 }
    843 
    844 int
    845 varobj_get_num_children (struct varobj *var)
    846 {
    847   if (var->num_children == -1)
    848     {
    849       if (varobj_is_dynamic_p (var))
    850 	{
    851 	  int dummy;
    852 
    853 	  /* If we have a dynamic varobj, don't report -1 children.
    854 	     So, try to fetch some children first.  */
    855 	  update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
    856 					  0, 0, 0);
    857 	}
    858       else
    859 	var->num_children = number_of_children (var);
    860     }
    861 
    862   return var->num_children >= 0 ? var->num_children : 0;
    863 }
    864 
    865 /* Creates a list of the immediate children of a variable object;
    866    the return code is the number of such children or -1 on error.  */
    867 
    868 VEC (varobj_p)*
    869 varobj_list_children (struct varobj *var, int *from, int *to)
    870 {
    871   char *name;
    872   int i, children_changed;
    873 
    874   var->dynamic->children_requested = 1;
    875 
    876   if (varobj_is_dynamic_p (var))
    877     {
    878       /* This, in theory, can result in the number of children changing without
    879 	 frontend noticing.  But well, calling -var-list-children on the same
    880 	 varobj twice is not something a sane frontend would do.  */
    881       update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
    882 				      &children_changed, 0, 0, *to);
    883       varobj_restrict_range (var->children, from, to);
    884       return var->children;
    885     }
    886 
    887   if (var->num_children == -1)
    888     var->num_children = number_of_children (var);
    889 
    890   /* If that failed, give up.  */
    891   if (var->num_children == -1)
    892     return var->children;
    893 
    894   /* If we're called when the list of children is not yet initialized,
    895      allocate enough elements in it.  */
    896   while (VEC_length (varobj_p, var->children) < var->num_children)
    897     VEC_safe_push (varobj_p, var->children, NULL);
    898 
    899   for (i = 0; i < var->num_children; i++)
    900     {
    901       varobj_p existing = VEC_index (varobj_p, var->children, i);
    902 
    903       if (existing == NULL)
    904 	{
    905 	  /* Either it's the first call to varobj_list_children for
    906 	     this variable object, and the child was never created,
    907 	     or it was explicitly deleted by the client.  */
    908 	  name = name_of_child (var, i);
    909 	  existing = create_child (var, i, name);
    910 	  VEC_replace (varobj_p, var->children, i, existing);
    911 	}
    912     }
    913 
    914   varobj_restrict_range (var->children, from, to);
    915   return var->children;
    916 }
    917 
    918 static struct varobj *
    919 varobj_add_child (struct varobj *var, struct varobj_item *item)
    920 {
    921   varobj_p v = create_child_with_value (var,
    922 					VEC_length (varobj_p, var->children),
    923 					item);
    924 
    925   VEC_safe_push (varobj_p, var->children, v);
    926   return v;
    927 }
    928 
    929 /* Obtain the type of an object Variable as a string similar to the one gdb
    930    prints on the console.  The caller is responsible for freeing the string.
    931    */
    932 
    933 char *
    934 varobj_get_type (struct varobj *var)
    935 {
    936   /* For the "fake" variables, do not return a type.  (Its type is
    937      NULL, too.)
    938      Do not return a type for invalid variables as well.  */
    939   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
    940     return NULL;
    941 
    942   return type_to_string (var->type);
    943 }
    944 
    945 /* Obtain the type of an object variable.  */
    946 
    947 struct type *
    948 varobj_get_gdb_type (const struct varobj *var)
    949 {
    950   return var->type;
    951 }
    952 
    953 /* Is VAR a path expression parent, i.e., can it be used to construct
    954    a valid path expression?  */
    955 
    956 static int
    957 is_path_expr_parent (const struct varobj *var)
    958 {
    959   gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
    960   return var->root->lang_ops->is_path_expr_parent (var);
    961 }
    962 
    963 /* Is VAR a path expression parent, i.e., can it be used to construct
    964    a valid path expression?  By default we assume any VAR can be a path
    965    parent.  */
    966 
    967 int
    968 varobj_default_is_path_expr_parent (const struct varobj *var)
    969 {
    970   return 1;
    971 }
    972 
    973 /* Return the path expression parent for VAR.  */
    974 
    975 const struct varobj *
    976 varobj_get_path_expr_parent (const struct varobj *var)
    977 {
    978   const struct varobj *parent = var;
    979 
    980   while (!is_root_p (parent) && !is_path_expr_parent (parent))
    981     parent = parent->parent;
    982 
    983   return parent;
    984 }
    985 
    986 /* Return a pointer to the full rooted expression of varobj VAR.
    987    If it has not been computed yet, compute it.  */
    988 char *
    989 varobj_get_path_expr (const struct varobj *var)
    990 {
    991   if (var->path_expr == NULL)
    992     {
    993       /* For root varobjs, we initialize path_expr
    994 	 when creating varobj, so here it should be
    995 	 child varobj.  */
    996       struct varobj *mutable_var = (struct varobj *) var;
    997       gdb_assert (!is_root_p (var));
    998 
    999       mutable_var->path_expr = (*var->root->lang_ops->path_expr_of_child) (var);
   1000     }
   1001 
   1002   return var->path_expr;
   1003 }
   1004 
   1005 const struct language_defn *
   1006 varobj_get_language (const struct varobj *var)
   1007 {
   1008   return var->root->exp->language_defn;
   1009 }
   1010 
   1011 int
   1012 varobj_get_attributes (const struct varobj *var)
   1013 {
   1014   int attributes = 0;
   1015 
   1016   if (varobj_editable_p (var))
   1017     /* FIXME: define masks for attributes.  */
   1018     attributes |= 0x00000001;	/* Editable */
   1019 
   1020   return attributes;
   1021 }
   1022 
   1023 /* Return true if VAR is a dynamic varobj.  */
   1024 
   1025 int
   1026 varobj_is_dynamic_p (const struct varobj *var)
   1027 {
   1028   return var->dynamic->pretty_printer != NULL;
   1029 }
   1030 
   1031 char *
   1032 varobj_get_formatted_value (struct varobj *var,
   1033 			    enum varobj_display_formats format)
   1034 {
   1035   return my_value_of_variable (var, format);
   1036 }
   1037 
   1038 char *
   1039 varobj_get_value (struct varobj *var)
   1040 {
   1041   return my_value_of_variable (var, var->format);
   1042 }
   1043 
   1044 /* Set the value of an object variable (if it is editable) to the
   1045    value of the given expression.  */
   1046 /* Note: Invokes functions that can call error().  */
   1047 
   1048 int
   1049 varobj_set_value (struct varobj *var, char *expression)
   1050 {
   1051   struct value *val = NULL; /* Initialize to keep gcc happy.  */
   1052   /* The argument "expression" contains the variable's new value.
   1053      We need to first construct a legal expression for this -- ugh!  */
   1054   /* Does this cover all the bases?  */
   1055   struct expression *exp;
   1056   struct value *value = NULL; /* Initialize to keep gcc happy.  */
   1057   int saved_input_radix = input_radix;
   1058   const char *s = expression;
   1059 
   1060   gdb_assert (varobj_editable_p (var));
   1061 
   1062   input_radix = 10;		/* ALWAYS reset to decimal temporarily.  */
   1063   exp = parse_exp_1 (&s, 0, 0, 0);
   1064   TRY
   1065     {
   1066       value = evaluate_expression (exp);
   1067     }
   1068 
   1069   CATCH (except, RETURN_MASK_ERROR)
   1070     {
   1071       /* We cannot proceed without a valid expression.  */
   1072       xfree (exp);
   1073       return 0;
   1074     }
   1075   END_CATCH
   1076 
   1077   /* All types that are editable must also be changeable.  */
   1078   gdb_assert (varobj_value_is_changeable_p (var));
   1079 
   1080   /* The value of a changeable variable object must not be lazy.  */
   1081   gdb_assert (!value_lazy (var->value));
   1082 
   1083   /* Need to coerce the input.  We want to check if the
   1084      value of the variable object will be different
   1085      after assignment, and the first thing value_assign
   1086      does is coerce the input.
   1087      For example, if we are assigning an array to a pointer variable we
   1088      should compare the pointer with the array's address, not with the
   1089      array's content.  */
   1090   value = coerce_array (value);
   1091 
   1092   /* The new value may be lazy.  value_assign, or
   1093      rather value_contents, will take care of this.  */
   1094   TRY
   1095     {
   1096       val = value_assign (var->value, value);
   1097     }
   1098 
   1099   CATCH (except, RETURN_MASK_ERROR)
   1100     {
   1101       return 0;
   1102     }
   1103   END_CATCH
   1104 
   1105   /* If the value has changed, record it, so that next -var-update can
   1106      report this change.  If a variable had a value of '1', we've set it
   1107      to '333' and then set again to '1', when -var-update will report this
   1108      variable as changed -- because the first assignment has set the
   1109      'updated' flag.  There's no need to optimize that, because return value
   1110      of -var-update should be considered an approximation.  */
   1111   var->updated = install_new_value (var, val, 0 /* Compare values.  */);
   1112   input_radix = saved_input_radix;
   1113   return 1;
   1114 }
   1115 
   1116 #if HAVE_PYTHON
   1117 
   1118 /* A helper function to install a constructor function and visualizer
   1119    in a varobj_dynamic.  */
   1120 
   1121 static void
   1122 install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
   1123 		    PyObject *visualizer)
   1124 {
   1125   Py_XDECREF (var->constructor);
   1126   var->constructor = constructor;
   1127 
   1128   Py_XDECREF (var->pretty_printer);
   1129   var->pretty_printer = visualizer;
   1130 
   1131   varobj_iter_delete (var->child_iter);
   1132   var->child_iter = NULL;
   1133 }
   1134 
   1135 /* Install the default visualizer for VAR.  */
   1136 
   1137 static void
   1138 install_default_visualizer (struct varobj *var)
   1139 {
   1140   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
   1141   if (CPLUS_FAKE_CHILD (var))
   1142     return;
   1143 
   1144   if (pretty_printing)
   1145     {
   1146       PyObject *pretty_printer = NULL;
   1147 
   1148       if (var->value)
   1149 	{
   1150 	  pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
   1151 	  if (! pretty_printer)
   1152 	    {
   1153 	      gdbpy_print_stack ();
   1154 	      error (_("Cannot instantiate printer for default visualizer"));
   1155 	    }
   1156 	}
   1157 
   1158       if (pretty_printer == Py_None)
   1159 	{
   1160 	  Py_DECREF (pretty_printer);
   1161 	  pretty_printer = NULL;
   1162 	}
   1163 
   1164       install_visualizer (var->dynamic, NULL, pretty_printer);
   1165     }
   1166 }
   1167 
   1168 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
   1169    make a new object.  */
   1170 
   1171 static void
   1172 construct_visualizer (struct varobj *var, PyObject *constructor)
   1173 {
   1174   PyObject *pretty_printer;
   1175 
   1176   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
   1177   if (CPLUS_FAKE_CHILD (var))
   1178     return;
   1179 
   1180   Py_INCREF (constructor);
   1181   if (constructor == Py_None)
   1182     pretty_printer = NULL;
   1183   else
   1184     {
   1185       pretty_printer = instantiate_pretty_printer (constructor, var->value);
   1186       if (! pretty_printer)
   1187 	{
   1188 	  gdbpy_print_stack ();
   1189 	  Py_DECREF (constructor);
   1190 	  constructor = Py_None;
   1191 	  Py_INCREF (constructor);
   1192 	}
   1193 
   1194       if (pretty_printer == Py_None)
   1195 	{
   1196 	  Py_DECREF (pretty_printer);
   1197 	  pretty_printer = NULL;
   1198 	}
   1199     }
   1200 
   1201   install_visualizer (var->dynamic, constructor, pretty_printer);
   1202 }
   1203 
   1204 #endif /* HAVE_PYTHON */
   1205 
   1206 /* A helper function for install_new_value.  This creates and installs
   1207    a visualizer for VAR, if appropriate.  */
   1208 
   1209 static void
   1210 install_new_value_visualizer (struct varobj *var)
   1211 {
   1212 #if HAVE_PYTHON
   1213   /* If the constructor is None, then we want the raw value.  If VAR
   1214      does not have a value, just skip this.  */
   1215   if (!gdb_python_initialized)
   1216     return;
   1217 
   1218   if (var->dynamic->constructor != Py_None && var->value != NULL)
   1219     {
   1220       struct cleanup *cleanup;
   1221 
   1222       cleanup = varobj_ensure_python_env (var);
   1223 
   1224       if (var->dynamic->constructor == NULL)
   1225 	install_default_visualizer (var);
   1226       else
   1227 	construct_visualizer (var, var->dynamic->constructor);
   1228 
   1229       do_cleanups (cleanup);
   1230     }
   1231 #else
   1232   /* Do nothing.  */
   1233 #endif
   1234 }
   1235 
   1236 /* When using RTTI to determine variable type it may be changed in runtime when
   1237    the variable value is changed.  This function checks whether type of varobj
   1238    VAR will change when a new value NEW_VALUE is assigned and if it is so
   1239    updates the type of VAR.  */
   1240 
   1241 static int
   1242 update_type_if_necessary (struct varobj *var, struct value *new_value)
   1243 {
   1244   if (new_value)
   1245     {
   1246       struct value_print_options opts;
   1247 
   1248       get_user_print_options (&opts);
   1249       if (opts.objectprint)
   1250 	{
   1251 	  struct type *new_type;
   1252 	  char *curr_type_str, *new_type_str;
   1253 	  int type_name_changed;
   1254 
   1255 	  new_type = value_actual_type (new_value, 0, 0);
   1256 	  new_type_str = type_to_string (new_type);
   1257 	  curr_type_str = varobj_get_type (var);
   1258 	  type_name_changed = strcmp (curr_type_str, new_type_str) != 0;
   1259 	  xfree (curr_type_str);
   1260 	  xfree (new_type_str);
   1261 
   1262 	  if (type_name_changed)
   1263 	    {
   1264 	      var->type = new_type;
   1265 
   1266 	      /* This information may be not valid for a new type.  */
   1267 	      varobj_delete (var, 1);
   1268 	      VEC_free (varobj_p, var->children);
   1269 	      var->num_children = -1;
   1270 	    }
   1271 	  xfree (new_type_str);
   1272 	  xfree (curr_type_str);
   1273 	  return type_name_changed;
   1274 	}
   1275     }
   1276 
   1277   return 0;
   1278 }
   1279 
   1280 /* Assign a new value to a variable object.  If INITIAL is non-zero,
   1281    this is the first assignement after the variable object was just
   1282    created, or changed type.  In that case, just assign the value
   1283    and return 0.
   1284    Otherwise, assign the new value, and return 1 if the value is
   1285    different from the current one, 0 otherwise.  The comparison is
   1286    done on textual representation of value.  Therefore, some types
   1287    need not be compared.  E.g.  for structures the reported value is
   1288    always "{...}", so no comparison is necessary here.  If the old
   1289    value was NULL and new one is not, or vice versa, we always return 1.
   1290 
   1291    The VALUE parameter should not be released -- the function will
   1292    take care of releasing it when needed.  */
   1293 static int
   1294 install_new_value (struct varobj *var, struct value *value, int initial)
   1295 {
   1296   int changeable;
   1297   int need_to_fetch;
   1298   int changed = 0;
   1299   int intentionally_not_fetched = 0;
   1300   char *print_value = NULL;
   1301 
   1302   /* We need to know the varobj's type to decide if the value should
   1303      be fetched or not.  C++ fake children (public/protected/private)
   1304      don't have a type.  */
   1305   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
   1306   changeable = varobj_value_is_changeable_p (var);
   1307 
   1308   /* If the type has custom visualizer, we consider it to be always
   1309      changeable.  FIXME: need to make sure this behaviour will not
   1310      mess up read-sensitive values.  */
   1311   if (var->dynamic->pretty_printer != NULL)
   1312     changeable = 1;
   1313 
   1314   need_to_fetch = changeable;
   1315 
   1316   /* We are not interested in the address of references, and given
   1317      that in C++ a reference is not rebindable, it cannot
   1318      meaningfully change.  So, get hold of the real value.  */
   1319   if (value)
   1320     value = coerce_ref (value);
   1321 
   1322   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
   1323     /* For unions, we need to fetch the value implicitly because
   1324        of implementation of union member fetch.  When gdb
   1325        creates a value for a field and the value of the enclosing
   1326        structure is not lazy,  it immediately copies the necessary
   1327        bytes from the enclosing values.  If the enclosing value is
   1328        lazy, the call to value_fetch_lazy on the field will read
   1329        the data from memory.  For unions, that means we'll read the
   1330        same memory more than once, which is not desirable.  So
   1331        fetch now.  */
   1332     need_to_fetch = 1;
   1333 
   1334   /* The new value might be lazy.  If the type is changeable,
   1335      that is we'll be comparing values of this type, fetch the
   1336      value now.  Otherwise, on the next update the old value
   1337      will be lazy, which means we've lost that old value.  */
   1338   if (need_to_fetch && value && value_lazy (value))
   1339     {
   1340       const struct varobj *parent = var->parent;
   1341       int frozen = var->frozen;
   1342 
   1343       for (; !frozen && parent; parent = parent->parent)
   1344 	frozen |= parent->frozen;
   1345 
   1346       if (frozen && initial)
   1347 	{
   1348 	  /* For variables that are frozen, or are children of frozen
   1349 	     variables, we don't do fetch on initial assignment.
   1350 	     For non-initial assignemnt we do the fetch, since it means we're
   1351 	     explicitly asked to compare the new value with the old one.  */
   1352 	  intentionally_not_fetched = 1;
   1353 	}
   1354       else
   1355 	{
   1356 
   1357 	  TRY
   1358 	    {
   1359 	      value_fetch_lazy (value);
   1360 	    }
   1361 
   1362 	  CATCH (except, RETURN_MASK_ERROR)
   1363 	    {
   1364 	      /* Set the value to NULL, so that for the next -var-update,
   1365 		 we don't try to compare the new value with this value,
   1366 		 that we couldn't even read.  */
   1367 	      value = NULL;
   1368 	    }
   1369 	  END_CATCH
   1370 	}
   1371     }
   1372 
   1373   /* Get a reference now, before possibly passing it to any Python
   1374      code that might release it.  */
   1375   if (value != NULL)
   1376     value_incref (value);
   1377 
   1378   /* Below, we'll be comparing string rendering of old and new
   1379      values.  Don't get string rendering if the value is
   1380      lazy -- if it is, the code above has decided that the value
   1381      should not be fetched.  */
   1382   if (value != NULL && !value_lazy (value)
   1383       && var->dynamic->pretty_printer == NULL)
   1384     print_value = varobj_value_get_print_value (value, var->format, var);
   1385 
   1386   /* If the type is changeable, compare the old and the new values.
   1387      If this is the initial assignment, we don't have any old value
   1388      to compare with.  */
   1389   if (!initial && changeable)
   1390     {
   1391       /* If the value of the varobj was changed by -var-set-value,
   1392 	 then the value in the varobj and in the target is the same.
   1393 	 However, that value is different from the value that the
   1394 	 varobj had after the previous -var-update.  So need to the
   1395 	 varobj as changed.  */
   1396       if (var->updated)
   1397 	{
   1398 	  changed = 1;
   1399 	}
   1400       else if (var->dynamic->pretty_printer == NULL)
   1401 	{
   1402 	  /* Try to compare the values.  That requires that both
   1403 	     values are non-lazy.  */
   1404 	  if (var->not_fetched && value_lazy (var->value))
   1405 	    {
   1406 	      /* This is a frozen varobj and the value was never read.
   1407 		 Presumably, UI shows some "never read" indicator.
   1408 		 Now that we've fetched the real value, we need to report
   1409 		 this varobj as changed so that UI can show the real
   1410 		 value.  */
   1411 	      changed = 1;
   1412 	    }
   1413           else  if (var->value == NULL && value == NULL)
   1414 	    /* Equal.  */
   1415 	    ;
   1416 	  else if (var->value == NULL || value == NULL)
   1417 	    {
   1418 	      changed = 1;
   1419 	    }
   1420 	  else
   1421 	    {
   1422 	      gdb_assert (!value_lazy (var->value));
   1423 	      gdb_assert (!value_lazy (value));
   1424 
   1425 	      gdb_assert (var->print_value != NULL && print_value != NULL);
   1426 	      if (strcmp (var->print_value, print_value) != 0)
   1427 		changed = 1;
   1428 	    }
   1429 	}
   1430     }
   1431 
   1432   if (!initial && !changeable)
   1433     {
   1434       /* For values that are not changeable, we don't compare the values.
   1435 	 However, we want to notice if a value was not NULL and now is NULL,
   1436 	 or vise versa, so that we report when top-level varobjs come in scope
   1437 	 and leave the scope.  */
   1438       changed = (var->value != NULL) != (value != NULL);
   1439     }
   1440 
   1441   /* We must always keep the new value, since children depend on it.  */
   1442   if (var->value != NULL && var->value != value)
   1443     value_free (var->value);
   1444   var->value = value;
   1445   if (value && value_lazy (value) && intentionally_not_fetched)
   1446     var->not_fetched = 1;
   1447   else
   1448     var->not_fetched = 0;
   1449   var->updated = 0;
   1450 
   1451   install_new_value_visualizer (var);
   1452 
   1453   /* If we installed a pretty-printer, re-compare the printed version
   1454      to see if the variable changed.  */
   1455   if (var->dynamic->pretty_printer != NULL)
   1456     {
   1457       xfree (print_value);
   1458       print_value = varobj_value_get_print_value (var->value, var->format,
   1459 						  var);
   1460       if ((var->print_value == NULL && print_value != NULL)
   1461 	  || (var->print_value != NULL && print_value == NULL)
   1462 	  || (var->print_value != NULL && print_value != NULL
   1463 	      && strcmp (var->print_value, print_value) != 0))
   1464 	changed = 1;
   1465     }
   1466   if (var->print_value)
   1467     xfree (var->print_value);
   1468   var->print_value = print_value;
   1469 
   1470   gdb_assert (!var->value || value_type (var->value));
   1471 
   1472   return changed;
   1473 }
   1474 
   1475 /* Return the requested range for a varobj.  VAR is the varobj.  FROM
   1476    and TO are out parameters; *FROM and *TO will be set to the
   1477    selected sub-range of VAR.  If no range was selected using
   1478    -var-set-update-range, then both will be -1.  */
   1479 void
   1480 varobj_get_child_range (const struct varobj *var, int *from, int *to)
   1481 {
   1482   *from = var->from;
   1483   *to = var->to;
   1484 }
   1485 
   1486 /* Set the selected sub-range of children of VAR to start at index
   1487    FROM and end at index TO.  If either FROM or TO is less than zero,
   1488    this is interpreted as a request for all children.  */
   1489 void
   1490 varobj_set_child_range (struct varobj *var, int from, int to)
   1491 {
   1492   var->from = from;
   1493   var->to = to;
   1494 }
   1495 
   1496 void
   1497 varobj_set_visualizer (struct varobj *var, const char *visualizer)
   1498 {
   1499 #if HAVE_PYTHON
   1500   PyObject *mainmod, *globals, *constructor;
   1501   struct cleanup *back_to;
   1502 
   1503   if (!gdb_python_initialized)
   1504     return;
   1505 
   1506   back_to = varobj_ensure_python_env (var);
   1507 
   1508   mainmod = PyImport_AddModule ("__main__");
   1509   globals = PyModule_GetDict (mainmod);
   1510   Py_INCREF (globals);
   1511   make_cleanup_py_decref (globals);
   1512 
   1513   constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
   1514 
   1515   if (! constructor)
   1516     {
   1517       gdbpy_print_stack ();
   1518       error (_("Could not evaluate visualizer expression: %s"), visualizer);
   1519     }
   1520 
   1521   construct_visualizer (var, constructor);
   1522   Py_XDECREF (constructor);
   1523 
   1524   /* If there are any children now, wipe them.  */
   1525   varobj_delete (var, 1 /* children only */);
   1526   var->num_children = -1;
   1527 
   1528   do_cleanups (back_to);
   1529 #else
   1530   error (_("Python support required"));
   1531 #endif
   1532 }
   1533 
   1534 /* If NEW_VALUE is the new value of the given varobj (var), return
   1535    non-zero if var has mutated.  In other words, if the type of
   1536    the new value is different from the type of the varobj's old
   1537    value.
   1538 
   1539    NEW_VALUE may be NULL, if the varobj is now out of scope.  */
   1540 
   1541 static int
   1542 varobj_value_has_mutated (const struct varobj *var, struct value *new_value,
   1543 			  struct type *new_type)
   1544 {
   1545   /* If we haven't previously computed the number of children in var,
   1546      it does not matter from the front-end's perspective whether
   1547      the type has mutated or not.  For all intents and purposes,
   1548      it has not mutated.  */
   1549   if (var->num_children < 0)
   1550     return 0;
   1551 
   1552   if (var->root->lang_ops->value_has_mutated)
   1553     {
   1554       /* The varobj module, when installing new values, explicitly strips
   1555 	 references, saying that we're not interested in those addresses.
   1556 	 But detection of mutation happens before installing the new
   1557 	 value, so our value may be a reference that we need to strip
   1558 	 in order to remain consistent.  */
   1559       if (new_value != NULL)
   1560 	new_value = coerce_ref (new_value);
   1561       return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
   1562     }
   1563   else
   1564     return 0;
   1565 }
   1566 
   1567 /* Update the values for a variable and its children.  This is a
   1568    two-pronged attack.  First, re-parse the value for the root's
   1569    expression to see if it's changed.  Then go all the way
   1570    through its children, reconstructing them and noting if they've
   1571    changed.
   1572 
   1573    The EXPLICIT parameter specifies if this call is result
   1574    of MI request to update this specific variable, or
   1575    result of implicit -var-update *.  For implicit request, we don't
   1576    update frozen variables.
   1577 
   1578    NOTE: This function may delete the caller's varobj.  If it
   1579    returns TYPE_CHANGED, then it has done this and VARP will be modified
   1580    to point to the new varobj.  */
   1581 
   1582 VEC(varobj_update_result) *
   1583 varobj_update (struct varobj **varp, int is_explicit)
   1584 {
   1585   int type_changed = 0;
   1586   int i;
   1587   struct value *newobj;
   1588   VEC (varobj_update_result) *stack = NULL;
   1589   VEC (varobj_update_result) *result = NULL;
   1590 
   1591   /* Frozen means frozen -- we don't check for any change in
   1592      this varobj, including its going out of scope, or
   1593      changing type.  One use case for frozen varobjs is
   1594      retaining previously evaluated expressions, and we don't
   1595      want them to be reevaluated at all.  */
   1596   if (!is_explicit && (*varp)->frozen)
   1597     return result;
   1598 
   1599   if (!(*varp)->root->is_valid)
   1600     {
   1601       varobj_update_result r = {0};
   1602 
   1603       r.varobj = *varp;
   1604       r.status = VAROBJ_INVALID;
   1605       VEC_safe_push (varobj_update_result, result, &r);
   1606       return result;
   1607     }
   1608 
   1609   if ((*varp)->root->rootvar == *varp)
   1610     {
   1611       varobj_update_result r = {0};
   1612 
   1613       r.varobj = *varp;
   1614       r.status = VAROBJ_IN_SCOPE;
   1615 
   1616       /* Update the root variable.  value_of_root can return NULL
   1617 	 if the variable is no longer around, i.e. we stepped out of
   1618 	 the frame in which a local existed.  We are letting the
   1619 	 value_of_root variable dispose of the varobj if the type
   1620 	 has changed.  */
   1621       newobj = value_of_root (varp, &type_changed);
   1622       if (update_type_if_necessary(*varp, newobj))
   1623 	  type_changed = 1;
   1624       r.varobj = *varp;
   1625       r.type_changed = type_changed;
   1626       if (install_new_value ((*varp), newobj, type_changed))
   1627 	r.changed = 1;
   1628 
   1629       if (newobj == NULL)
   1630 	r.status = VAROBJ_NOT_IN_SCOPE;
   1631       r.value_installed = 1;
   1632 
   1633       if (r.status == VAROBJ_NOT_IN_SCOPE)
   1634 	{
   1635 	  if (r.type_changed || r.changed)
   1636 	    VEC_safe_push (varobj_update_result, result, &r);
   1637 	  return result;
   1638 	}
   1639 
   1640       VEC_safe_push (varobj_update_result, stack, &r);
   1641     }
   1642   else
   1643     {
   1644       varobj_update_result r = {0};
   1645 
   1646       r.varobj = *varp;
   1647       VEC_safe_push (varobj_update_result, stack, &r);
   1648     }
   1649 
   1650   /* Walk through the children, reconstructing them all.  */
   1651   while (!VEC_empty (varobj_update_result, stack))
   1652     {
   1653       varobj_update_result r = *(VEC_last (varobj_update_result, stack));
   1654       struct varobj *v = r.varobj;
   1655 
   1656       VEC_pop (varobj_update_result, stack);
   1657 
   1658       /* Update this variable, unless it's a root, which is already
   1659 	 updated.  */
   1660       if (!r.value_installed)
   1661 	{
   1662 	  struct type *new_type;
   1663 
   1664 	  newobj = value_of_child (v->parent, v->index);
   1665 	  if (update_type_if_necessary(v, newobj))
   1666 	    r.type_changed = 1;
   1667 	  if (newobj)
   1668 	    new_type = value_type (newobj);
   1669 	  else
   1670 	    new_type = v->root->lang_ops->type_of_child (v->parent, v->index);
   1671 
   1672 	  if (varobj_value_has_mutated (v, newobj, new_type))
   1673 	    {
   1674 	      /* The children are no longer valid; delete them now.
   1675 	         Report the fact that its type changed as well.  */
   1676 	      varobj_delete (v, 1 /* only_children */);
   1677 	      v->num_children = -1;
   1678 	      v->to = -1;
   1679 	      v->from = -1;
   1680 	      v->type = new_type;
   1681 	      r.type_changed = 1;
   1682 	    }
   1683 
   1684 	  if (install_new_value (v, newobj, r.type_changed))
   1685 	    {
   1686 	      r.changed = 1;
   1687 	      v->updated = 0;
   1688 	    }
   1689 	}
   1690 
   1691       /* We probably should not get children of a dynamic varobj, but
   1692 	 for which -var-list-children was never invoked.  */
   1693       if (varobj_is_dynamic_p (v))
   1694 	{
   1695 	  VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0;
   1696 	  VEC (varobj_p) *newobj = 0;
   1697 	  int i, children_changed = 0;
   1698 
   1699 	  if (v->frozen)
   1700 	    continue;
   1701 
   1702 	  if (!v->dynamic->children_requested)
   1703 	    {
   1704 	      int dummy;
   1705 
   1706 	      /* If we initially did not have potential children, but
   1707 		 now we do, consider the varobj as changed.
   1708 		 Otherwise, if children were never requested, consider
   1709 		 it as unchanged -- presumably, such varobj is not yet
   1710 		 expanded in the UI, so we need not bother getting
   1711 		 it.  */
   1712 	      if (!varobj_has_more (v, 0))
   1713 		{
   1714 		  update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
   1715 						  &dummy, 0, 0, 0);
   1716 		  if (varobj_has_more (v, 0))
   1717 		    r.changed = 1;
   1718 		}
   1719 
   1720 	      if (r.changed)
   1721 		VEC_safe_push (varobj_update_result, result, &r);
   1722 
   1723 	      continue;
   1724 	    }
   1725 
   1726 	  /* If update_dynamic_varobj_children returns 0, then we have
   1727 	     a non-conforming pretty-printer, so we skip it.  */
   1728 	  if (update_dynamic_varobj_children (v, &changed, &type_changed, &newobj,
   1729 					      &unchanged, &children_changed, 1,
   1730 					      v->from, v->to))
   1731 	    {
   1732 	      if (children_changed || newobj)
   1733 		{
   1734 		  r.children_changed = 1;
   1735 		  r.newobj = newobj;
   1736 		}
   1737 	      /* Push in reverse order so that the first child is
   1738 		 popped from the work stack first, and so will be
   1739 		 added to result first.  This does not affect
   1740 		 correctness, just "nicer".  */
   1741 	      for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i)
   1742 		{
   1743 		  varobj_p tmp = VEC_index (varobj_p, type_changed, i);
   1744 		  varobj_update_result r = {0};
   1745 
   1746 		  /* Type may change only if value was changed.  */
   1747 		  r.varobj = tmp;
   1748 		  r.changed = 1;
   1749 		  r.type_changed = 1;
   1750 		  r.value_installed = 1;
   1751 		  VEC_safe_push (varobj_update_result, stack, &r);
   1752 		}
   1753 	      for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
   1754 		{
   1755 		  varobj_p tmp = VEC_index (varobj_p, changed, i);
   1756 		  varobj_update_result r = {0};
   1757 
   1758 		  r.varobj = tmp;
   1759 		  r.changed = 1;
   1760 		  r.value_installed = 1;
   1761 		  VEC_safe_push (varobj_update_result, stack, &r);
   1762 		}
   1763 	      for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
   1764 	      	{
   1765 		  varobj_p tmp = VEC_index (varobj_p, unchanged, i);
   1766 
   1767 	      	  if (!tmp->frozen)
   1768 	      	    {
   1769 	      	      varobj_update_result r = {0};
   1770 
   1771 		      r.varobj = tmp;
   1772 	      	      r.value_installed = 1;
   1773 	      	      VEC_safe_push (varobj_update_result, stack, &r);
   1774 	      	    }
   1775 	      	}
   1776 	      if (r.changed || r.children_changed)
   1777 		VEC_safe_push (varobj_update_result, result, &r);
   1778 
   1779 	      /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
   1780 		 because NEW has been put into the result vector.  */
   1781 	      VEC_free (varobj_p, changed);
   1782 	      VEC_free (varobj_p, type_changed);
   1783 	      VEC_free (varobj_p, unchanged);
   1784 
   1785 	      continue;
   1786 	    }
   1787 	}
   1788 
   1789       /* Push any children.  Use reverse order so that the first
   1790 	 child is popped from the work stack first, and so
   1791 	 will be added to result first.  This does not
   1792 	 affect correctness, just "nicer".  */
   1793       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
   1794 	{
   1795 	  varobj_p c = VEC_index (varobj_p, v->children, i);
   1796 
   1797 	  /* Child may be NULL if explicitly deleted by -var-delete.  */
   1798 	  if (c != NULL && !c->frozen)
   1799 	    {
   1800 	      varobj_update_result r = {0};
   1801 
   1802 	      r.varobj = c;
   1803 	      VEC_safe_push (varobj_update_result, stack, &r);
   1804 	    }
   1805 	}
   1806 
   1807       if (r.changed || r.type_changed)
   1808 	VEC_safe_push (varobj_update_result, result, &r);
   1809     }
   1810 
   1811   VEC_free (varobj_update_result, stack);
   1812 
   1813   return result;
   1814 }
   1815 
   1816 
   1818 /* Helper functions */
   1819 
   1820 /*
   1821  * Variable object construction/destruction
   1822  */
   1823 
   1824 static int
   1825 delete_variable (struct varobj *var, int only_children_p)
   1826 {
   1827   int delcount = 0;
   1828 
   1829   delete_variable_1 (&delcount, var, only_children_p,
   1830 		     1 /* remove_from_parent_p */ );
   1831 
   1832   return delcount;
   1833 }
   1834 
   1835 /* Delete the variable object VAR and its children.  */
   1836 /* IMPORTANT NOTE: If we delete a variable which is a child
   1837    and the parent is not removed we dump core.  It must be always
   1838    initially called with remove_from_parent_p set.  */
   1839 static void
   1840 delete_variable_1 (int *delcountp, struct varobj *var, int only_children_p,
   1841 		   int remove_from_parent_p)
   1842 {
   1843   int i;
   1844 
   1845   /* Delete any children of this variable, too.  */
   1846   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
   1847     {
   1848       varobj_p child = VEC_index (varobj_p, var->children, i);
   1849 
   1850       if (!child)
   1851 	continue;
   1852       if (!remove_from_parent_p)
   1853 	child->parent = NULL;
   1854       delete_variable_1 (delcountp, child, 0, only_children_p);
   1855     }
   1856   VEC_free (varobj_p, var->children);
   1857 
   1858   /* if we were called to delete only the children we are done here.  */
   1859   if (only_children_p)
   1860     return;
   1861 
   1862   /* Otherwise, add it to the list of deleted ones and proceed to do so.  */
   1863   /* If the name is null, this is a temporary variable, that has not
   1864      yet been installed, don't report it, it belongs to the caller...  */
   1865   if (var->obj_name != NULL)
   1866     {
   1867       *delcountp = *delcountp + 1;
   1868     }
   1869 
   1870   /* If this variable has a parent, remove it from its parent's list.  */
   1871   /* OPTIMIZATION: if the parent of this variable is also being deleted,
   1872      (as indicated by remove_from_parent_p) we don't bother doing an
   1873      expensive list search to find the element to remove when we are
   1874      discarding the list afterwards.  */
   1875   if ((remove_from_parent_p) && (var->parent != NULL))
   1876     {
   1877       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
   1878     }
   1879 
   1880   if (var->obj_name != NULL)
   1881     uninstall_variable (var);
   1882 
   1883   /* Free memory associated with this variable.  */
   1884   free_variable (var);
   1885 }
   1886 
   1887 /* Install the given variable VAR with the object name VAR->OBJ_NAME.  */
   1888 static int
   1889 install_variable (struct varobj *var)
   1890 {
   1891   struct vlist *cv;
   1892   struct vlist *newvl;
   1893   const char *chp;
   1894   unsigned int index = 0;
   1895   unsigned int i = 1;
   1896 
   1897   for (chp = var->obj_name; *chp; chp++)
   1898     {
   1899       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
   1900     }
   1901 
   1902   cv = *(varobj_table + index);
   1903   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
   1904     cv = cv->next;
   1905 
   1906   if (cv != NULL)
   1907     error (_("Duplicate variable object name"));
   1908 
   1909   /* Add varobj to hash table.  */
   1910   newvl = XNEW (struct vlist);
   1911   newvl->next = *(varobj_table + index);
   1912   newvl->var = var;
   1913   *(varobj_table + index) = newvl;
   1914 
   1915   /* If root, add varobj to root list.  */
   1916   if (is_root_p (var))
   1917     {
   1918       /* Add to list of root variables.  */
   1919       if (rootlist == NULL)
   1920 	var->root->next = NULL;
   1921       else
   1922 	var->root->next = rootlist;
   1923       rootlist = var->root;
   1924     }
   1925 
   1926   return 1;			/* OK */
   1927 }
   1928 
   1929 /* Unistall the object VAR.  */
   1930 static void
   1931 uninstall_variable (struct varobj *var)
   1932 {
   1933   struct vlist *cv;
   1934   struct vlist *prev;
   1935   struct varobj_root *cr;
   1936   struct varobj_root *prer;
   1937   const char *chp;
   1938   unsigned int index = 0;
   1939   unsigned int i = 1;
   1940 
   1941   /* Remove varobj from hash table.  */
   1942   for (chp = var->obj_name; *chp; chp++)
   1943     {
   1944       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
   1945     }
   1946 
   1947   cv = *(varobj_table + index);
   1948   prev = NULL;
   1949   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
   1950     {
   1951       prev = cv;
   1952       cv = cv->next;
   1953     }
   1954 
   1955   if (varobjdebug)
   1956     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
   1957 
   1958   if (cv == NULL)
   1959     {
   1960       warning
   1961 	("Assertion failed: Could not find variable object \"%s\" to delete",
   1962 	 var->obj_name);
   1963       return;
   1964     }
   1965 
   1966   if (prev == NULL)
   1967     *(varobj_table + index) = cv->next;
   1968   else
   1969     prev->next = cv->next;
   1970 
   1971   xfree (cv);
   1972 
   1973   /* If root, remove varobj from root list.  */
   1974   if (is_root_p (var))
   1975     {
   1976       /* Remove from list of root variables.  */
   1977       if (rootlist == var->root)
   1978 	rootlist = var->root->next;
   1979       else
   1980 	{
   1981 	  prer = NULL;
   1982 	  cr = rootlist;
   1983 	  while ((cr != NULL) && (cr->rootvar != var))
   1984 	    {
   1985 	      prer = cr;
   1986 	      cr = cr->next;
   1987 	    }
   1988 	  if (cr == NULL)
   1989 	    {
   1990 	      warning (_("Assertion failed: Could not find "
   1991 		         "varobj \"%s\" in root list"),
   1992 		       var->obj_name);
   1993 	      return;
   1994 	    }
   1995 	  if (prer == NULL)
   1996 	    rootlist = NULL;
   1997 	  else
   1998 	    prer->next = cr->next;
   1999 	}
   2000     }
   2001 
   2002 }
   2003 
   2004 /* Create and install a child of the parent of the given name.
   2005 
   2006    The created VAROBJ takes ownership of the allocated NAME.  */
   2007 
   2008 static struct varobj *
   2009 create_child (struct varobj *parent, int index, char *name)
   2010 {
   2011   struct varobj_item item;
   2012 
   2013   item.name = name;
   2014   item.value = value_of_child (parent, index);
   2015 
   2016   return create_child_with_value (parent, index, &item);
   2017 }
   2018 
   2019 static struct varobj *
   2020 create_child_with_value (struct varobj *parent, int index,
   2021 			 struct varobj_item *item)
   2022 {
   2023   struct varobj *child;
   2024   char *childs_name;
   2025 
   2026   child = new_variable ();
   2027 
   2028   /* NAME is allocated by caller.  */
   2029   child->name = item->name;
   2030   child->index = index;
   2031   child->parent = parent;
   2032   child->root = parent->root;
   2033 
   2034   if (varobj_is_anonymous_child (child))
   2035     childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
   2036   else
   2037     childs_name = xstrprintf ("%s.%s", parent->obj_name, item->name);
   2038   child->obj_name = childs_name;
   2039 
   2040   install_variable (child);
   2041 
   2042   /* Compute the type of the child.  Must do this before
   2043      calling install_new_value.  */
   2044   if (item->value != NULL)
   2045     /* If the child had no evaluation errors, var->value
   2046        will be non-NULL and contain a valid type.  */
   2047     child->type = value_actual_type (item->value, 0, NULL);
   2048   else
   2049     /* Otherwise, we must compute the type.  */
   2050     child->type = (*child->root->lang_ops->type_of_child) (child->parent,
   2051 							   child->index);
   2052   install_new_value (child, item->value, 1);
   2053 
   2054   return child;
   2055 }
   2056 
   2057 
   2059 /*
   2060  * Miscellaneous utility functions.
   2061  */
   2062 
   2063 /* Allocate memory and initialize a new variable.  */
   2064 static struct varobj *
   2065 new_variable (void)
   2066 {
   2067   struct varobj *var;
   2068 
   2069   var = XNEW (struct varobj);
   2070   var->name = NULL;
   2071   var->path_expr = NULL;
   2072   var->obj_name = NULL;
   2073   var->index = -1;
   2074   var->type = NULL;
   2075   var->value = NULL;
   2076   var->num_children = -1;
   2077   var->parent = NULL;
   2078   var->children = NULL;
   2079   var->format = FORMAT_NATURAL;
   2080   var->root = NULL;
   2081   var->updated = 0;
   2082   var->print_value = NULL;
   2083   var->frozen = 0;
   2084   var->not_fetched = 0;
   2085   var->dynamic = XNEW (struct varobj_dynamic);
   2086   var->dynamic->children_requested = 0;
   2087   var->from = -1;
   2088   var->to = -1;
   2089   var->dynamic->constructor = 0;
   2090   var->dynamic->pretty_printer = 0;
   2091   var->dynamic->child_iter = 0;
   2092   var->dynamic->saved_item = 0;
   2093 
   2094   return var;
   2095 }
   2096 
   2097 /* Allocate memory and initialize a new root variable.  */
   2098 static struct varobj *
   2099 new_root_variable (void)
   2100 {
   2101   struct varobj *var = new_variable ();
   2102 
   2103   var->root = XNEW (struct varobj_root);
   2104   var->root->lang_ops = NULL;
   2105   var->root->exp = NULL;
   2106   var->root->valid_block = NULL;
   2107   var->root->frame = null_frame_id;
   2108   var->root->floating = 0;
   2109   var->root->rootvar = NULL;
   2110   var->root->is_valid = 1;
   2111 
   2112   return var;
   2113 }
   2114 
   2115 /* Free any allocated memory associated with VAR.  */
   2116 static void
   2117 free_variable (struct varobj *var)
   2118 {
   2119 #if HAVE_PYTHON
   2120   if (var->dynamic->pretty_printer != NULL)
   2121     {
   2122       struct cleanup *cleanup = varobj_ensure_python_env (var);
   2123 
   2124       Py_XDECREF (var->dynamic->constructor);
   2125       Py_XDECREF (var->dynamic->pretty_printer);
   2126       do_cleanups (cleanup);
   2127     }
   2128 #endif
   2129 
   2130   varobj_iter_delete (var->dynamic->child_iter);
   2131   varobj_clear_saved_item (var->dynamic);
   2132   value_free (var->value);
   2133 
   2134   /* Free the expression if this is a root variable.  */
   2135   if (is_root_p (var))
   2136     {
   2137       xfree (var->root->exp);
   2138       xfree (var->root);
   2139     }
   2140 
   2141   xfree (var->name);
   2142   xfree (var->obj_name);
   2143   xfree (var->print_value);
   2144   xfree (var->path_expr);
   2145   xfree (var->dynamic);
   2146   xfree (var);
   2147 }
   2148 
   2149 static void
   2150 do_free_variable_cleanup (void *var)
   2151 {
   2152   free_variable ((struct varobj *) var);
   2153 }
   2154 
   2155 static struct cleanup *
   2156 make_cleanup_free_variable (struct varobj *var)
   2157 {
   2158   return make_cleanup (do_free_variable_cleanup, var);
   2159 }
   2160 
   2161 /* Return the type of the value that's stored in VAR,
   2162    or that would have being stored there if the
   2163    value were accessible.
   2164 
   2165    This differs from VAR->type in that VAR->type is always
   2166    the true type of the expession in the source language.
   2167    The return value of this function is the type we're
   2168    actually storing in varobj, and using for displaying
   2169    the values and for comparing previous and new values.
   2170 
   2171    For example, top-level references are always stripped.  */
   2172 struct type *
   2173 varobj_get_value_type (const struct varobj *var)
   2174 {
   2175   struct type *type;
   2176 
   2177   if (var->value)
   2178     type = value_type (var->value);
   2179   else
   2180     type = var->type;
   2181 
   2182   type = check_typedef (type);
   2183 
   2184   if (TYPE_CODE (type) == TYPE_CODE_REF)
   2185     type = get_target_type (type);
   2186 
   2187   type = check_typedef (type);
   2188 
   2189   return type;
   2190 }
   2191 
   2192 /* What is the default display for this variable? We assume that
   2193    everything is "natural".  Any exceptions?  */
   2194 static enum varobj_display_formats
   2195 variable_default_display (struct varobj *var)
   2196 {
   2197   return FORMAT_NATURAL;
   2198 }
   2199 
   2200 /*
   2201  * Language-dependencies
   2202  */
   2203 
   2204 /* Common entry points */
   2205 
   2206 /* Return the number of children for a given variable.
   2207    The result of this function is defined by the language
   2208    implementation.  The number of children returned by this function
   2209    is the number of children that the user will see in the variable
   2210    display.  */
   2211 static int
   2212 number_of_children (const struct varobj *var)
   2213 {
   2214   return (*var->root->lang_ops->number_of_children) (var);
   2215 }
   2216 
   2217 /* What is the expression for the root varobj VAR? Returns a malloc'd
   2218    string.  */
   2219 static char *
   2220 name_of_variable (const struct varobj *var)
   2221 {
   2222   return (*var->root->lang_ops->name_of_variable) (var);
   2223 }
   2224 
   2225 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
   2226    string.  */
   2227 static char *
   2228 name_of_child (struct varobj *var, int index)
   2229 {
   2230   return (*var->root->lang_ops->name_of_child) (var, index);
   2231 }
   2232 
   2233 /* If frame associated with VAR can be found, switch
   2234    to it and return 1.  Otherwise, return 0.  */
   2235 
   2236 static int
   2237 check_scope (const struct varobj *var)
   2238 {
   2239   struct frame_info *fi;
   2240   int scope;
   2241 
   2242   fi = frame_find_by_id (var->root->frame);
   2243   scope = fi != NULL;
   2244 
   2245   if (fi)
   2246     {
   2247       CORE_ADDR pc = get_frame_pc (fi);
   2248 
   2249       if (pc <  BLOCK_START (var->root->valid_block) ||
   2250 	  pc >= BLOCK_END (var->root->valid_block))
   2251 	scope = 0;
   2252       else
   2253 	select_frame (fi);
   2254     }
   2255   return scope;
   2256 }
   2257 
   2258 /* Helper function to value_of_root.  */
   2259 
   2260 static struct value *
   2261 value_of_root_1 (struct varobj **var_handle)
   2262 {
   2263   struct value *new_val = NULL;
   2264   struct varobj *var = *var_handle;
   2265   int within_scope = 0;
   2266   struct cleanup *back_to;
   2267 
   2268   /*  Only root variables can be updated...  */
   2269   if (!is_root_p (var))
   2270     /* Not a root var.  */
   2271     return NULL;
   2272 
   2273   back_to = make_cleanup_restore_current_thread ();
   2274 
   2275   /* Determine whether the variable is still around.  */
   2276   if (var->root->valid_block == NULL || var->root->floating)
   2277     within_scope = 1;
   2278   else if (var->root->thread_id == 0)
   2279     {
   2280       /* The program was single-threaded when the variable object was
   2281 	 created.  Technically, it's possible that the program became
   2282 	 multi-threaded since then, but we don't support such
   2283 	 scenario yet.  */
   2284       within_scope = check_scope (var);
   2285     }
   2286   else
   2287     {
   2288       ptid_t ptid = global_thread_id_to_ptid (var->root->thread_id);
   2289 
   2290       if (!ptid_equal (minus_one_ptid, ptid))
   2291 	{
   2292 	  switch_to_thread (ptid);
   2293 	  within_scope = check_scope (var);
   2294 	}
   2295     }
   2296 
   2297   if (within_scope)
   2298     {
   2299 
   2300       /* We need to catch errors here, because if evaluate
   2301          expression fails we want to just return NULL.  */
   2302       TRY
   2303 	{
   2304 	  new_val = evaluate_expression (var->root->exp);
   2305 	}
   2306       CATCH (except, RETURN_MASK_ERROR)
   2307 	{
   2308 	}
   2309       END_CATCH
   2310     }
   2311 
   2312   do_cleanups (back_to);
   2313 
   2314   return new_val;
   2315 }
   2316 
   2317 /* What is the ``struct value *'' of the root variable VAR?
   2318    For floating variable object, evaluation can get us a value
   2319    of different type from what is stored in varobj already.  In
   2320    that case:
   2321    - *type_changed will be set to 1
   2322    - old varobj will be freed, and new one will be
   2323    created, with the same name.
   2324    - *var_handle will be set to the new varobj
   2325    Otherwise, *type_changed will be set to 0.  */
   2326 static struct value *
   2327 value_of_root (struct varobj **var_handle, int *type_changed)
   2328 {
   2329   struct varobj *var;
   2330 
   2331   if (var_handle == NULL)
   2332     return NULL;
   2333 
   2334   var = *var_handle;
   2335 
   2336   /* This should really be an exception, since this should
   2337      only get called with a root variable.  */
   2338 
   2339   if (!is_root_p (var))
   2340     return NULL;
   2341 
   2342   if (var->root->floating)
   2343     {
   2344       struct varobj *tmp_var;
   2345       char *old_type, *new_type;
   2346 
   2347       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
   2348 			       USE_SELECTED_FRAME);
   2349       if (tmp_var == NULL)
   2350 	{
   2351 	  return NULL;
   2352 	}
   2353       old_type = varobj_get_type (var);
   2354       new_type = varobj_get_type (tmp_var);
   2355       if (strcmp (old_type, new_type) == 0)
   2356 	{
   2357 	  /* The expression presently stored inside var->root->exp
   2358 	     remembers the locations of local variables relatively to
   2359 	     the frame where the expression was created (in DWARF location
   2360 	     button, for example).  Naturally, those locations are not
   2361 	     correct in other frames, so update the expression.  */
   2362 
   2363          struct expression *tmp_exp = var->root->exp;
   2364 
   2365          var->root->exp = tmp_var->root->exp;
   2366          tmp_var->root->exp = tmp_exp;
   2367 
   2368 	  varobj_delete (tmp_var, 0);
   2369 	  *type_changed = 0;
   2370 	}
   2371       else
   2372 	{
   2373 	  tmp_var->obj_name = xstrdup (var->obj_name);
   2374 	  tmp_var->from = var->from;
   2375 	  tmp_var->to = var->to;
   2376 	  varobj_delete (var, 0);
   2377 
   2378 	  install_variable (tmp_var);
   2379 	  *var_handle = tmp_var;
   2380 	  var = *var_handle;
   2381 	  *type_changed = 1;
   2382 	}
   2383       xfree (old_type);
   2384       xfree (new_type);
   2385     }
   2386   else
   2387     {
   2388       *type_changed = 0;
   2389     }
   2390 
   2391   {
   2392     struct value *value;
   2393 
   2394     value = value_of_root_1 (var_handle);
   2395     if (var->value == NULL || value == NULL)
   2396       {
   2397 	/* For root varobj-s, a NULL value indicates a scoping issue.
   2398 	   So, nothing to do in terms of checking for mutations.  */
   2399       }
   2400     else if (varobj_value_has_mutated (var, value, value_type (value)))
   2401       {
   2402 	/* The type has mutated, so the children are no longer valid.
   2403 	   Just delete them, and tell our caller that the type has
   2404 	   changed.  */
   2405 	varobj_delete (var, 1 /* only_children */);
   2406 	var->num_children = -1;
   2407 	var->to = -1;
   2408 	var->from = -1;
   2409 	*type_changed = 1;
   2410       }
   2411     return value;
   2412   }
   2413 }
   2414 
   2415 /* What is the ``struct value *'' for the INDEX'th child of PARENT?  */
   2416 static struct value *
   2417 value_of_child (const struct varobj *parent, int index)
   2418 {
   2419   struct value *value;
   2420 
   2421   value = (*parent->root->lang_ops->value_of_child) (parent, index);
   2422 
   2423   return value;
   2424 }
   2425 
   2426 /* GDB already has a command called "value_of_variable".  Sigh.  */
   2427 static char *
   2428 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
   2429 {
   2430   if (var->root->is_valid)
   2431     {
   2432       if (var->dynamic->pretty_printer != NULL)
   2433 	return varobj_value_get_print_value (var->value, var->format, var);
   2434       return (*var->root->lang_ops->value_of_variable) (var, format);
   2435     }
   2436   else
   2437     return NULL;
   2438 }
   2439 
   2440 void
   2441 varobj_formatted_print_options (struct value_print_options *opts,
   2442 				enum varobj_display_formats format)
   2443 {
   2444   get_formatted_print_options (opts, format_code[(int) format]);
   2445   opts->deref_ref = 0;
   2446   opts->raw = 1;
   2447 }
   2448 
   2449 char *
   2450 varobj_value_get_print_value (struct value *value,
   2451 			      enum varobj_display_formats format,
   2452 			      const struct varobj *var)
   2453 {
   2454   struct ui_file *stb;
   2455   struct cleanup *old_chain;
   2456   char *thevalue = NULL;
   2457   struct value_print_options opts;
   2458   struct type *type = NULL;
   2459   long len = 0;
   2460   char *encoding = NULL;
   2461   /* Initialize it just to avoid a GCC false warning.  */
   2462   CORE_ADDR str_addr = 0;
   2463   int string_print = 0;
   2464 
   2465   if (value == NULL)
   2466     return NULL;
   2467 
   2468   stb = mem_fileopen ();
   2469   old_chain = make_cleanup_ui_file_delete (stb);
   2470 
   2471 #if HAVE_PYTHON
   2472   if (gdb_python_initialized)
   2473     {
   2474       PyObject *value_formatter =  var->dynamic->pretty_printer;
   2475 
   2476       varobj_ensure_python_env (var);
   2477 
   2478       if (value_formatter)
   2479 	{
   2480 	  /* First check to see if we have any children at all.  If so,
   2481 	     we simply return {...}.  */
   2482 	  if (dynamic_varobj_has_child_method (var))
   2483 	    {
   2484 	      do_cleanups (old_chain);
   2485 	      return xstrdup ("{...}");
   2486 	    }
   2487 
   2488 	  if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
   2489 	    {
   2490 	      struct value *replacement;
   2491 	      PyObject *output = NULL;
   2492 
   2493 	      output = apply_varobj_pretty_printer (value_formatter,
   2494 						    &replacement,
   2495 						    stb);
   2496 
   2497 	      /* If we have string like output ...  */
   2498 	      if (output)
   2499 		{
   2500 		  make_cleanup_py_decref (output);
   2501 
   2502 		  /* If this is a lazy string, extract it.  For lazy
   2503 		     strings we always print as a string, so set
   2504 		     string_print.  */
   2505 		  if (gdbpy_is_lazy_string (output))
   2506 		    {
   2507 		      gdbpy_extract_lazy_string (output, &str_addr, &type,
   2508 						 &len, &encoding);
   2509 		      make_cleanup (free_current_contents, &encoding);
   2510 		      string_print = 1;
   2511 		    }
   2512 		  else
   2513 		    {
   2514 		      /* If it is a regular (non-lazy) string, extract
   2515 			 it and copy the contents into THEVALUE.  If the
   2516 			 hint says to print it as a string, set
   2517 			 string_print.  Otherwise just return the extracted
   2518 			 string as a value.  */
   2519 
   2520 		      char *s = python_string_to_target_string (output);
   2521 
   2522 		      if (s)
   2523 			{
   2524 			  struct gdbarch *gdbarch;
   2525 			  char *hint;
   2526 
   2527 			  hint = gdbpy_get_display_hint (value_formatter);
   2528 			  if (hint)
   2529 			    {
   2530 			      if (!strcmp (hint, "string"))
   2531 				string_print = 1;
   2532 			      xfree (hint);
   2533 			    }
   2534 
   2535 			  len = strlen (s);
   2536 			  thevalue = (char *) xmemdup (s, len + 1, len + 1);
   2537 			  gdbarch = get_type_arch (value_type (value));
   2538 			  type = builtin_type (gdbarch)->builtin_char;
   2539 			  xfree (s);
   2540 
   2541 			  if (!string_print)
   2542 			    {
   2543 			      do_cleanups (old_chain);
   2544 			      return thevalue;
   2545 			    }
   2546 
   2547 			  make_cleanup (xfree, thevalue);
   2548 			}
   2549 		      else
   2550 			gdbpy_print_stack ();
   2551 		    }
   2552 		}
   2553 	      /* If the printer returned a replacement value, set VALUE
   2554 		 to REPLACEMENT.  If there is not a replacement value,
   2555 		 just use the value passed to this function.  */
   2556 	      if (replacement)
   2557 		value = replacement;
   2558 	    }
   2559 	}
   2560     }
   2561 #endif
   2562 
   2563   varobj_formatted_print_options (&opts, format);
   2564 
   2565   /* If the THEVALUE has contents, it is a regular string.  */
   2566   if (thevalue)
   2567     LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
   2568   else if (string_print)
   2569     /* Otherwise, if string_print is set, and it is not a regular
   2570        string, it is a lazy string.  */
   2571     val_print_string (type, encoding, str_addr, len, stb, &opts);
   2572   else
   2573     /* All other cases.  */
   2574     common_val_print (value, stb, 0, &opts, current_language);
   2575 
   2576   thevalue = ui_file_xstrdup (stb, NULL);
   2577 
   2578   do_cleanups (old_chain);
   2579   return thevalue;
   2580 }
   2581 
   2582 int
   2583 varobj_editable_p (const struct varobj *var)
   2584 {
   2585   struct type *type;
   2586 
   2587   if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
   2588     return 0;
   2589 
   2590   type = varobj_get_value_type (var);
   2591 
   2592   switch (TYPE_CODE (type))
   2593     {
   2594     case TYPE_CODE_STRUCT:
   2595     case TYPE_CODE_UNION:
   2596     case TYPE_CODE_ARRAY:
   2597     case TYPE_CODE_FUNC:
   2598     case TYPE_CODE_METHOD:
   2599       return 0;
   2600       break;
   2601 
   2602     default:
   2603       return 1;
   2604       break;
   2605     }
   2606 }
   2607 
   2608 /* Call VAR's value_is_changeable_p language-specific callback.  */
   2609 
   2610 int
   2611 varobj_value_is_changeable_p (const struct varobj *var)
   2612 {
   2613   return var->root->lang_ops->value_is_changeable_p (var);
   2614 }
   2615 
   2616 /* Return 1 if that varobj is floating, that is is always evaluated in the
   2617    selected frame, and not bound to thread/frame.  Such variable objects
   2618    are created using '@' as frame specifier to -var-create.  */
   2619 int
   2620 varobj_floating_p (const struct varobj *var)
   2621 {
   2622   return var->root->floating;
   2623 }
   2624 
   2625 /* Implement the "value_is_changeable_p" varobj callback for most
   2626    languages.  */
   2627 
   2628 int
   2629 varobj_default_value_is_changeable_p (const struct varobj *var)
   2630 {
   2631   int r;
   2632   struct type *type;
   2633 
   2634   if (CPLUS_FAKE_CHILD (var))
   2635     return 0;
   2636 
   2637   type = varobj_get_value_type (var);
   2638 
   2639   switch (TYPE_CODE (type))
   2640     {
   2641     case TYPE_CODE_STRUCT:
   2642     case TYPE_CODE_UNION:
   2643     case TYPE_CODE_ARRAY:
   2644       r = 0;
   2645       break;
   2646 
   2647     default:
   2648       r = 1;
   2649     }
   2650 
   2651   return r;
   2652 }
   2653 
   2654 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
   2655    with an arbitrary caller supplied DATA pointer.  */
   2656 
   2657 void
   2658 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
   2659 {
   2660   struct varobj_root *var_root, *var_root_next;
   2661 
   2662   /* Iterate "safely" - handle if the callee deletes its passed VAROBJ.  */
   2663 
   2664   for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
   2665     {
   2666       var_root_next = var_root->next;
   2667 
   2668       (*func) (var_root->rootvar, data);
   2669     }
   2670 }
   2671 
   2672 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
   2673    defined on globals.  It is a helper for varobj_invalidate.
   2674 
   2675    This function is called after changing the symbol file, in this case the
   2676    pointers to "struct type" stored by the varobj are no longer valid.  All
   2677    varobj must be either re-evaluated, or marked as invalid here.  */
   2678 
   2679 static void
   2680 varobj_invalidate_iter (struct varobj *var, void *unused)
   2681 {
   2682   /* global and floating var must be re-evaluated.  */
   2683   if (var->root->floating || var->root->valid_block == NULL)
   2684     {
   2685       struct varobj *tmp_var;
   2686 
   2687       /* Try to create a varobj with same expression.  If we succeed
   2688 	 replace the old varobj, otherwise invalidate it.  */
   2689       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
   2690 			       USE_CURRENT_FRAME);
   2691       if (tmp_var != NULL)
   2692 	{
   2693 	  tmp_var->obj_name = xstrdup (var->obj_name);
   2694 	  varobj_delete (var, 0);
   2695 	  install_variable (tmp_var);
   2696 	}
   2697       else
   2698 	var->root->is_valid = 0;
   2699     }
   2700   else /* locals must be invalidated.  */
   2701     var->root->is_valid = 0;
   2702 }
   2703 
   2704 /* Invalidate the varobjs that are tied to locals and re-create the ones that
   2705    are defined on globals.
   2706    Invalidated varobjs will be always printed in_scope="invalid".  */
   2707 
   2708 void
   2709 varobj_invalidate (void)
   2710 {
   2711   all_root_varobjs (varobj_invalidate_iter, NULL);
   2712 }
   2713 
   2714 extern void _initialize_varobj (void);
   2716 void
   2717 _initialize_varobj (void)
   2718 {
   2719   varobj_table = XCNEWVEC (struct vlist *, VAROBJ_TABLE_SIZE);
   2720 
   2721   add_setshow_zuinteger_cmd ("varobj", class_maintenance,
   2722 			     &varobjdebug,
   2723 			     _("Set varobj debugging."),
   2724 			     _("Show varobj debugging."),
   2725 			     _("When non-zero, varobj debugging is enabled."),
   2726 			     NULL, show_varobjdebug,
   2727 			     &setdebuglist, &showdebuglist);
   2728 }
   2729