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