gdb/varobj.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Implementation of the GDB variable objects API.

  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 3 of the License, or
  6.    (at your option) any later version.

  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.

  11.    You should have received a copy of the GNU General Public License
  12.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  13. #include "defs.h"
  14. #include "value.h"
  15. #include "expression.h"
  16. #include "frame.h"
  17. #include "language.h"
  18. #include "gdbcmd.h"
  19. #include "block.h"
  20. #include "valprint.h"
  21. #include "gdb_regex.h"

  22. #include "varobj.h"
  23. #include "vec.h"
  24. #include "gdbthread.h"
  25. #include "inferior.h"
  26. #include "varobj-iter.h"

  27. #if HAVE_PYTHON
  28. #include "python/python.h"
  29. #include "python/python-internal.h"
  30. #else
  31. typedef int PyObject;
  32. #endif

  33. /* Non-zero if we want to see trace of varobj level stuff.  */

  34. unsigned int varobjdebug = 0;
  35. static void
  36. show_varobjdebug (struct ui_file *file, int from_tty,
  37.                   struct cmd_list_element *c, const char *value)
  38. {
  39.   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
  40. }

  41. /* String representations of gdb's format codes.  */
  42. char *varobj_format_string[] =
  43.   { "natural", "binary", "decimal", "hexadecimal", "octal" };

  44. /* True if we want to allow Python-based pretty-printing.  */
  45. static int pretty_printing = 0;

  46. void
  47. varobj_enable_pretty_printing (void)
  48. {
  49.   pretty_printing = 1;
  50. }

  51. /* Data structures */

  52. /* Every root variable has one of these structures saved in its
  53.    varobj.  Members which must be free'd are noted.  */
  54. struct varobj_root
  55. {

  56.   /* Alloc'd expression for this parent.  */
  57.   struct expression *exp;

  58.   /* Block for which this expression is valid.  */
  59.   const struct block *valid_block;

  60.   /* The frame for this expression.  This field is set iff valid_block is
  61.      not NULL.  */
  62.   struct frame_id frame;

  63.   /* The thread ID that this varobj_root belong to.  This field
  64.      is only valid if valid_block is not NULL.
  65.      When not 0, indicates which thread 'frame' belongs to.
  66.      When 0, indicates that the thread list was empty when the varobj_root
  67.      was created.  */
  68.   int thread_id;

  69.   /* If 1, the -var-update always recomputes the value in the
  70.      current thread and frame.  Otherwise, variable object is
  71.      always updated in the specific scope/thread/frame.  */
  72.   int floating;

  73.   /* Flag that indicates validity: set to 0 when this varobj_root refers
  74.      to symbols that do not exist anymore.  */
  75.   int is_valid;

  76.   /* Language-related operations for this variable and its
  77.      children.  */
  78.   const struct lang_varobj_ops *lang_ops;

  79.   /* The varobj for this root node.  */
  80.   struct varobj *rootvar;

  81.   /* Next root variable */
  82.   struct varobj_root *next;
  83. };

  84. /* Dynamic part of varobj.  */

  85. struct varobj_dynamic
  86. {
  87.   /* Whether the children of this varobj were requested.  This field is
  88.      used to decide if dynamic varobj should recompute their children.
  89.      In the event that the frontend never asked for the children, we
  90.      can avoid that.  */
  91.   int children_requested;

  92.   /* The pretty-printer constructor.  If NULL, then the default
  93.      pretty-printer will be looked up.  If None, then no
  94.      pretty-printer will be installed.  */
  95.   PyObject *constructor;

  96.   /* The pretty-printer that has been constructed.  If NULL, then a
  97.      new printer object is needed, and one will be constructed.  */
  98.   PyObject *pretty_printer;

  99.   /* The iterator returned by the printer's 'children' method, or NULL
  100.      if not available.  */
  101.   struct varobj_iter *child_iter;

  102.   /* We request one extra item from the iterator, so that we can
  103.      report to the caller whether there are more items than we have
  104.      already reported.  However, we don't want to install this value
  105.      when we read it, because that will mess up future updates.  So,
  106.      we stash it here instead.  */
  107.   varobj_item *saved_item;
  108. };

  109. struct cpstack
  110. {
  111.   char *name;
  112.   struct cpstack *next;
  113. };

  114. /* A list of varobjs */

  115. struct vlist
  116. {
  117.   struct varobj *var;
  118.   struct vlist *next;
  119. };

  120. /* Private function prototypes */

  121. /* Helper functions for the above subcommands.  */

  122. static int delete_variable (struct cpstack **, struct varobj *, int);

  123. static void delete_variable_1 (struct cpstack **, int *,
  124.                                struct varobj *, int, int);

  125. static int install_variable (struct varobj *);

  126. static void uninstall_variable (struct varobj *);

  127. static struct varobj *create_child (struct varobj *, int, char *);

  128. static struct varobj *
  129. create_child_with_value (struct varobj *parent, int index,
  130.                          struct varobj_item *item);

  131. /* Utility routines */

  132. static struct varobj *new_variable (void);

  133. static struct varobj *new_root_variable (void);

  134. static void free_variable (struct varobj *var);

  135. static struct cleanup *make_cleanup_free_variable (struct varobj *var);

  136. static enum varobj_display_formats variable_default_display (struct varobj *);

  137. static void cppush (struct cpstack **pstack, char *name);

  138. static char *cppop (struct cpstack **pstack);

  139. static int update_type_if_necessary (struct varobj *var,
  140.                                      struct value *new_value);

  141. static int install_new_value (struct varobj *var, struct value *value,
  142.                               int initial);

  143. /* Language-specific routines.  */

  144. static int number_of_children (struct varobj *);

  145. static char *name_of_variable (struct varobj *);

  146. static char *name_of_child (struct varobj *, int);

  147. static struct value *value_of_root (struct varobj **var_handle, int *);

  148. static struct value *value_of_child (struct varobj *parent, int index);

  149. static char *my_value_of_variable (struct varobj *var,
  150.                                    enum varobj_display_formats format);

  151. static int is_root_p (struct varobj *var);

  152. static struct varobj *varobj_add_child (struct varobj *var,
  153.                                         struct varobj_item *item);

  154. /* Private data */

  155. /* Mappings of varobj_display_formats enums to gdb's format codes.  */
  156. static int format_code[] = { 0, 't', 'd', 'x', 'o' };

  157. /* Header of the list of root variable objects.  */
  158. static struct varobj_root *rootlist;

  159. /* Prime number indicating the number of buckets in the hash table.  */
  160. /* A prime large enough to avoid too many colisions.  */
  161. #define VAROBJ_TABLE_SIZE 227

  162. /* Pointer to the varobj hash table (built at run time).  */
  163. static struct vlist **varobj_table;



  164. /* API Implementation */
  165. static int
  166. is_root_p (struct varobj *var)
  167. {
  168.   return (var->root->rootvar == var);
  169. }

  170. #ifdef HAVE_PYTHON
  171. /* Helper function to install a Python environment suitable for
  172.    use during operations on VAR.  */
  173. struct cleanup *
  174. varobj_ensure_python_env (struct varobj *var)
  175. {
  176.   return ensure_python_env (var->root->exp->gdbarch,
  177.                             var->root->exp->language_defn);
  178. }
  179. #endif

  180. /* Creates a varobj (not its children).  */

  181. /* Return the full FRAME which corresponds to the given CORE_ADDR
  182.    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */

  183. static struct frame_info *
  184. find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
  185. {
  186.   struct frame_info *frame = NULL;

  187.   if (frame_addr == (CORE_ADDR) 0)
  188.     return NULL;

  189.   for (frame = get_current_frame ();
  190.        frame != NULL;
  191.        frame = get_prev_frame (frame))
  192.     {
  193.       /* The CORE_ADDR we get as argument was parsed from a string GDB
  194.          output as $fp.  This output got truncated to gdbarch_addr_bit.
  195.          Truncate the frame base address in the same manner before
  196.          comparing it against our argument.  */
  197.       CORE_ADDR frame_base = get_frame_base_address (frame);
  198.       int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));

  199.       if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  200.         frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;

  201.       if (frame_base == frame_addr)
  202.         return frame;
  203.     }

  204.   return NULL;
  205. }

  206. struct varobj *
  207. varobj_create (char *objname,
  208.                char *expression, CORE_ADDR frame, enum varobj_type type)
  209. {
  210.   struct varobj *var;
  211.   struct cleanup *old_chain;

  212.   /* Fill out a varobj structure for the (root) variable being constructed.  */
  213.   var = new_root_variable ();
  214.   old_chain = make_cleanup_free_variable (var);

  215.   if (expression != NULL)
  216.     {
  217.       struct frame_info *fi;
  218.       struct frame_id old_id = null_frame_id;
  219.       const struct block *block;
  220.       const char *p;
  221.       struct value *value = NULL;
  222.       volatile struct gdb_exception except;
  223.       CORE_ADDR pc;

  224.       /* Parse and evaluate the expression, filling in as much of the
  225.          variable's data as possible.  */

  226.       if (has_stack_frames ())
  227.         {
  228.           /* Allow creator to specify context of variable.  */
  229.           if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
  230.             fi = get_selected_frame (NULL);
  231.           else
  232.             /* FIXME: cagney/2002-11-23: This code should be doing a
  233.                lookup using the frame ID and not just the frame's
  234.                ``address''.  This, of course, means an interface
  235.                change.  However, with out that interface change ISAs,
  236.                such as the ia64 with its two stacks, won't work.
  237.                Similar goes for the case where there is a frameless
  238.                function.  */
  239.             fi = find_frame_addr_in_frame_chain (frame);
  240.         }
  241.       else
  242.         fi = NULL;

  243.       /* frame = -2 means always use selected frame.  */
  244.       if (type == USE_SELECTED_FRAME)
  245.         var->root->floating = 1;

  246.       pc = 0;
  247.       block = NULL;
  248.       if (fi != NULL)
  249.         {
  250.           block = get_frame_block (fi, 0);
  251.           pc = get_frame_pc (fi);
  252.         }

  253.       p = expression;
  254.       innermost_block = NULL;
  255.       /* Wrap the call to parse expression, so we can
  256.          return a sensible error.  */
  257.       TRY_CATCH (except, RETURN_MASK_ERROR)
  258.         {
  259.           var->root->exp = parse_exp_1 (&p, pc, block, 0);
  260.         }

  261.       if (except.reason < 0)
  262.         {
  263.           do_cleanups (old_chain);
  264.           return NULL;
  265.         }

  266.       /* Don't allow variables to be created for types.  */
  267.       if (var->root->exp->elts[0].opcode == OP_TYPE
  268.           || var->root->exp->elts[0].opcode == OP_TYPEOF
  269.           || var->root->exp->elts[0].opcode == OP_DECLTYPE)
  270.         {
  271.           do_cleanups (old_chain);
  272.           fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
  273.                               " as an expression.\n");
  274.           return NULL;
  275.         }

  276.       var->format = variable_default_display (var);
  277.       var->root->valid_block = innermost_block;
  278.       var->name = xstrdup (expression);
  279.       /* For a root var, the name and the expr are the same.  */
  280.       var->path_expr = xstrdup (expression);

  281.       /* When the frame is different from the current frame,
  282.          we must select the appropriate frame before parsing
  283.          the expression, otherwise the value will not be current.
  284.          Since select_frame is so benign, just call it for all cases.  */
  285.       if (innermost_block)
  286.         {
  287.           /* User could specify explicit FRAME-ADDR which was not found but
  288.              EXPRESSION is frame specific and we would not be able to evaluate
  289.              it correctly next time.  With VALID_BLOCK set we must also set
  290.              FRAME and THREAD_ID.  */
  291.           if (fi == NULL)
  292.             error (_("Failed to find the specified frame"));

  293.           var->root->frame = get_frame_id (fi);
  294.           var->root->thread_id = pid_to_thread_id (inferior_ptid);
  295.           old_id = get_frame_id (get_selected_frame (NULL));
  296.           select_frame (fi);
  297.         }

  298.       /* We definitely need to catch errors here.
  299.          If evaluate_expression succeeds we got the value we wanted.
  300.          But if it fails, we still go on with a call to evaluate_type().  */
  301.       TRY_CATCH (except, RETURN_MASK_ERROR)
  302.         {
  303.           value = evaluate_expression (var->root->exp);
  304.         }

  305.       if (except.reason < 0)
  306.         {
  307.           /* Error getting the value.  Try to at least get the
  308.              right type.  */
  309.           struct value *type_only_value = evaluate_type (var->root->exp);

  310.           var->type = value_type (type_only_value);
  311.         }
  312.         else
  313.           {
  314.             int real_type_found = 0;

  315.             var->type = value_actual_type (value, 0, &real_type_found);
  316.             if (real_type_found)
  317.               value = value_cast (var->type, value);
  318.           }

  319.       /* Set language info */
  320.       var->root->lang_ops = var->root->exp->language_defn->la_varobj_ops;

  321.       install_new_value (var, value, 1 /* Initial assignment */);

  322.       /* Set ourselves as our root.  */
  323.       var->root->rootvar = var;

  324.       /* Reset the selected frame.  */
  325.       if (frame_id_p (old_id))
  326.         select_frame (frame_find_by_id (old_id));
  327.     }

  328.   /* If the variable object name is null, that means this
  329.      is a temporary variable, so don't install it.  */

  330.   if ((var != NULL) && (objname != NULL))
  331.     {
  332.       var->obj_name = xstrdup (objname);

  333.       /* If a varobj name is duplicated, the install will fail so
  334.          we must cleanup.  */
  335.       if (!install_variable (var))
  336.         {
  337.           do_cleanups (old_chain);
  338.           return NULL;
  339.         }
  340.     }

  341.   discard_cleanups (old_chain);
  342.   return var;
  343. }

  344. /* Generates an unique name that can be used for a varobj.  */

  345. char *
  346. varobj_gen_name (void)
  347. {
  348.   static int id = 0;
  349.   char *obj_name;

  350.   /* Generate a name for this object.  */
  351.   id++;
  352.   obj_name = xstrprintf ("var%d", id);

  353.   return obj_name;
  354. }

  355. /* Given an OBJNAME, returns the pointer to the corresponding varobj.  Call
  356.    error if OBJNAME cannot be found.  */

  357. struct varobj *
  358. varobj_get_handle (char *objname)
  359. {
  360.   struct vlist *cv;
  361.   const char *chp;
  362.   unsigned int index = 0;
  363.   unsigned int i = 1;

  364.   for (chp = objname; *chp; chp++)
  365.     {
  366.       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
  367.     }

  368.   cv = *(varobj_table + index);
  369.   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
  370.     cv = cv->next;

  371.   if (cv == NULL)
  372.     error (_("Variable object not found"));

  373.   return cv->var;
  374. }

  375. /* Given the handle, return the name of the object.  */

  376. char *
  377. varobj_get_objname (struct varobj *var)
  378. {
  379.   return var->obj_name;
  380. }

  381. /* Given the handle, return the expression represented by the object.  */

  382. char *
  383. varobj_get_expression (struct varobj *var)
  384. {
  385.   return name_of_variable (var);
  386. }

  387. /* Deletes a varobj and all its children if only_children == 0,
  388.    otherwise deletes only the children; returns a malloc'ed list of
  389.    all the (malloc'ed) names of the variables that have been deleted
  390.    (NULL terminated).  */

  391. int
  392. varobj_delete (struct varobj *var, char ***dellist, int only_children)
  393. {
  394.   int delcount;
  395.   int mycount;
  396.   struct cpstack *result = NULL;
  397.   char **cp;

  398.   /* Initialize a stack for temporary results.  */
  399.   cppush (&result, NULL);

  400.   if (only_children)
  401.     /* Delete only the variable children.  */
  402.     delcount = delete_variable (&result, var, 1 /* only the children */ );
  403.   else
  404.     /* Delete the variable and all its children.  */
  405.     delcount = delete_variable (&result, var, 0 /* parent+children */ );

  406.   /* We may have been asked to return a list of what has been deleted.  */
  407.   if (dellist != NULL)
  408.     {
  409.       *dellist = xmalloc ((delcount + 1) * sizeof (char *));

  410.       cp = *dellist;
  411.       mycount = delcount;
  412.       *cp = cppop (&result);
  413.       while ((*cp != NULL) && (mycount > 0))
  414.         {
  415.           mycount--;
  416.           cp++;
  417.           *cp = cppop (&result);
  418.         }

  419.       if (mycount || (*cp != NULL))
  420.         warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
  421.                  mycount);
  422.     }

  423.   return delcount;
  424. }

  425. #if HAVE_PYTHON

  426. /* Convenience function for varobj_set_visualizer.  Instantiate a
  427.    pretty-printer for a given value.  */
  428. static PyObject *
  429. instantiate_pretty_printer (PyObject *constructor, struct value *value)
  430. {
  431.   PyObject *val_obj = NULL;
  432.   PyObject *printer;

  433.   val_obj = value_to_value_object (value);
  434.   if (! val_obj)
  435.     return NULL;

  436.   printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
  437.   Py_DECREF (val_obj);
  438.   return printer;
  439. }

  440. #endif

  441. /* Set/Get variable object display format.  */

  442. enum varobj_display_formats
  443. varobj_set_display_format (struct varobj *var,
  444.                            enum varobj_display_formats format)
  445. {
  446.   switch (format)
  447.     {
  448.     case FORMAT_NATURAL:
  449.     case FORMAT_BINARY:
  450.     case FORMAT_DECIMAL:
  451.     case FORMAT_HEXADECIMAL:
  452.     case FORMAT_OCTAL:
  453.       var->format = format;
  454.       break;

  455.     default:
  456.       var->format = variable_default_display (var);
  457.     }

  458.   if (varobj_value_is_changeable_p (var)
  459.       && var->value && !value_lazy (var->value))
  460.     {
  461.       xfree (var->print_value);
  462.       var->print_value = varobj_value_get_print_value (var->value,
  463.                                                        var->format, var);
  464.     }

  465.   return var->format;
  466. }

  467. enum varobj_display_formats
  468. varobj_get_display_format (struct varobj *var)
  469. {
  470.   return var->format;
  471. }

  472. char *
  473. varobj_get_display_hint (struct varobj *var)
  474. {
  475.   char *result = NULL;

  476. #if HAVE_PYTHON
  477.   struct cleanup *back_to;

  478.   if (!gdb_python_initialized)
  479.     return NULL;

  480.   back_to = varobj_ensure_python_env (var);

  481.   if (var->dynamic->pretty_printer != NULL)
  482.     result = gdbpy_get_display_hint (var->dynamic->pretty_printer);

  483.   do_cleanups (back_to);
  484. #endif

  485.   return result;
  486. }

  487. /* Return true if the varobj has items after TO, false otherwise.  */

  488. int
  489. varobj_has_more (struct varobj *var, int to)
  490. {
  491.   if (VEC_length (varobj_p, var->children) > to)
  492.     return 1;
  493.   return ((to == -1 || VEC_length (varobj_p, var->children) == to)
  494.           && (var->dynamic->saved_item != NULL));
  495. }

  496. /* If the variable object is bound to a specific thread, that
  497.    is its evaluation can always be done in context of a frame
  498.    inside that thread, returns GDB id of the thread -- which
  499.    is always positive.  Otherwise, returns -1.  */
  500. int
  501. varobj_get_thread_id (struct varobj *var)
  502. {
  503.   if (var->root->valid_block && var->root->thread_id > 0)
  504.     return var->root->thread_id;
  505.   else
  506.     return -1;
  507. }

  508. void
  509. varobj_set_frozen (struct varobj *var, int frozen)
  510. {
  511.   /* When a variable is unfrozen, we don't fetch its value.
  512.      The 'not_fetched' flag remains set, so next -var-update
  513.      won't complain.

  514.      We don't fetch the value, because for structures the client
  515.      should do -var-update anyway.  It would be bad to have different
  516.      client-size logic for structure and other types.  */
  517.   var->frozen = frozen;
  518. }

  519. int
  520. varobj_get_frozen (struct varobj *var)
  521. {
  522.   return var->frozen;
  523. }

  524. /* A helper function that restricts a range to what is actually
  525.    available in a VEC.  This follows the usual rules for the meaning
  526.    of FROM and TO -- if either is negative, the entire range is
  527.    used.  */

  528. void
  529. varobj_restrict_range (VEC (varobj_p) *children, int *from, int *to)
  530. {
  531.   if (*from < 0 || *to < 0)
  532.     {
  533.       *from = 0;
  534.       *to = VEC_length (varobj_p, children);
  535.     }
  536.   else
  537.     {
  538.       if (*from > VEC_length (varobj_p, children))
  539.         *from = VEC_length (varobj_p, children);
  540.       if (*to > VEC_length (varobj_p, children))
  541.         *to = VEC_length (varobj_p, children);
  542.       if (*from > *to)
  543.         *from = *to;
  544.     }
  545. }

  546. /* A helper for update_dynamic_varobj_children that installs a new
  547.    child when needed.  */

  548. static void
  549. install_dynamic_child (struct varobj *var,
  550.                        VEC (varobj_p) **changed,
  551.                        VEC (varobj_p) **type_changed,
  552.                        VEC (varobj_p) **new,
  553.                        VEC (varobj_p) **unchanged,
  554.                        int *cchanged,
  555.                        int index,
  556.                        struct varobj_item *item)
  557. {
  558.   if (VEC_length (varobj_p, var->children) < index + 1)
  559.     {
  560.       /* There's no child yet.  */
  561.       struct varobj *child = varobj_add_child (var, item);

  562.       if (new)
  563.         {
  564.           VEC_safe_push (varobj_p, *new, child);
  565.           *cchanged = 1;
  566.         }
  567.     }
  568.   else
  569.     {
  570.       varobj_p existing = VEC_index (varobj_p, var->children, index);
  571.       int type_updated = update_type_if_necessary (existing, item->value);

  572.       if (type_updated)
  573.         {
  574.           if (type_changed)
  575.             VEC_safe_push (varobj_p, *type_changed, existing);
  576.         }
  577.       if (install_new_value (existing, item->value, 0))
  578.         {
  579.           if (!type_updated && changed)
  580.             VEC_safe_push (varobj_p, *changed, existing);
  581.         }
  582.       else if (!type_updated && unchanged)
  583.         VEC_safe_push (varobj_p, *unchanged, existing);
  584.     }
  585. }

  586. #if HAVE_PYTHON

  587. static int
  588. dynamic_varobj_has_child_method (struct varobj *var)
  589. {
  590.   struct cleanup *back_to;
  591.   PyObject *printer = var->dynamic->pretty_printer;
  592.   int result;

  593.   if (!gdb_python_initialized)
  594.     return 0;

  595.   back_to = varobj_ensure_python_env (var);
  596.   result = PyObject_HasAttr (printer, gdbpy_children_cst);
  597.   do_cleanups (back_to);
  598.   return result;
  599. }
  600. #endif

  601. /* A factory for creating dynamic varobj's iterators.  Returns an
  602.    iterator object suitable for iterating over VAR's children.  */

  603. static struct varobj_iter *
  604. varobj_get_iterator (struct varobj *var)
  605. {
  606. #if HAVE_PYTHON
  607.   if (var->dynamic->pretty_printer)
  608.     return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
  609. #endif

  610.   gdb_assert_not_reached (_("\
  611. requested an iterator from a non-dynamic varobj"));
  612. }

  613. /* Release and clear VAR's saved item, if any.  */

  614. static void
  615. varobj_clear_saved_item (struct varobj_dynamic *var)
  616. {
  617.   if (var->saved_item != NULL)
  618.     {
  619.       value_free (var->saved_item->value);
  620.       xfree (var->saved_item);
  621.       var->saved_item = NULL;
  622.     }
  623. }

  624. static int
  625. update_dynamic_varobj_children (struct varobj *var,
  626.                                 VEC (varobj_p) **changed,
  627.                                 VEC (varobj_p) **type_changed,
  628.                                 VEC (varobj_p) **new,
  629.                                 VEC (varobj_p) **unchanged,
  630.                                 int *cchanged,
  631.                                 int update_children,
  632.                                 int from,
  633.                                 int to)
  634. {
  635.   int i;

  636.   *cchanged = 0;

  637.   if (update_children || var->dynamic->child_iter == NULL)
  638.     {
  639.       varobj_iter_delete (var->dynamic->child_iter);
  640.       var->dynamic->child_iter = varobj_get_iterator (var);

  641.       varobj_clear_saved_item (var->dynamic);

  642.       i = 0;

  643.       if (var->dynamic->child_iter == NULL)
  644.         return 0;
  645.     }
  646.   else
  647.     i = VEC_length (varobj_p, var->children);

  648.   /* We ask for one extra child, so that MI can report whether there
  649.      are more children.  */
  650.   for (; to < 0 || i < to + 1; ++i)
  651.     {
  652.       varobj_item *item;

  653.       /* See if there was a leftover from last time.  */
  654.       if (var->dynamic->saved_item != NULL)
  655.         {
  656.           item = var->dynamic->saved_item;
  657.           var->dynamic->saved_item = NULL;
  658.         }
  659.       else
  660.         {
  661.           item = varobj_iter_next (var->dynamic->child_iter);
  662.           /* Release vitem->value so its lifetime is not bound to the
  663.              execution of a command.  */
  664.           if (item != NULL && item->value != NULL)
  665.             release_value_or_incref (item->value);
  666.         }

  667.       if (item == NULL)
  668.         {
  669.           /* Iteration is done.  Remove iterator from VAR.  */
  670.           varobj_iter_delete (var->dynamic->child_iter);
  671.           var->dynamic->child_iter = NULL;
  672.           break;
  673.         }
  674.       /* We don't want to push the extra child on any report list.  */
  675.       if (to < 0 || i < to)
  676.         {
  677.           int can_mention = from < 0 || i >= from;

  678.           install_dynamic_child (var, can_mention ? changed : NULL,
  679.                                  can_mention ? type_changed : NULL,
  680.                                  can_mention ? new : NULL,
  681.                                  can_mention ? unchanged : NULL,
  682.                                  can_mention ? cchanged : NULL, i,
  683.                                  item);

  684.           xfree (item);
  685.         }
  686.       else
  687.         {
  688.           var->dynamic->saved_item = item;

  689.           /* We want to truncate the child list just before this
  690.              element.  */
  691.           break;
  692.         }
  693.     }

  694.   if (i < VEC_length (varobj_p, var->children))
  695.     {
  696.       int j;

  697.       *cchanged = 1;
  698.       for (j = i; j < VEC_length (varobj_p, var->children); ++j)
  699.         varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
  700.       VEC_truncate (varobj_p, var->children, i);
  701.     }

  702.   /* If there are fewer children than requested, note that the list of
  703.      children changed.  */
  704.   if (to >= 0 && VEC_length (varobj_p, var->children) < to)
  705.     *cchanged = 1;

  706.   var->num_children = VEC_length (varobj_p, var->children);

  707.   return 1;
  708. }

  709. int
  710. varobj_get_num_children (struct varobj *var)
  711. {
  712.   if (var->num_children == -1)
  713.     {
  714.       if (varobj_is_dynamic_p (var))
  715.         {
  716.           int dummy;

  717.           /* If we have a dynamic varobj, don't report -1 children.
  718.              So, try to fetch some children first.  */
  719.           update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
  720.                                           0, 0, 0);
  721.         }
  722.       else
  723.         var->num_children = number_of_children (var);
  724.     }

  725.   return var->num_children >= 0 ? var->num_children : 0;
  726. }

  727. /* Creates a list of the immediate children of a variable object;
  728.    the return code is the number of such children or -1 on error.  */

  729. VEC (varobj_p)*
  730. varobj_list_children (struct varobj *var, int *from, int *to)
  731. {
  732.   char *name;
  733.   int i, children_changed;

  734.   var->dynamic->children_requested = 1;

  735.   if (varobj_is_dynamic_p (var))
  736.     {
  737.       /* This, in theory, can result in the number of children changing without
  738.          frontend noticing.  But well, calling -var-list-children on the same
  739.          varobj twice is not something a sane frontend would do.  */
  740.       update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
  741.                                       &children_changed, 0, 0, *to);
  742.       varobj_restrict_range (var->children, from, to);
  743.       return var->children;
  744.     }

  745.   if (var->num_children == -1)
  746.     var->num_children = number_of_children (var);

  747.   /* If that failed, give up.  */
  748.   if (var->num_children == -1)
  749.     return var->children;

  750.   /* If we're called when the list of children is not yet initialized,
  751.      allocate enough elements in it.  */
  752.   while (VEC_length (varobj_p, var->children) < var->num_children)
  753.     VEC_safe_push (varobj_p, var->children, NULL);

  754.   for (i = 0; i < var->num_children; i++)
  755.     {
  756.       varobj_p existing = VEC_index (varobj_p, var->children, i);

  757.       if (existing == NULL)
  758.         {
  759.           /* Either it's the first call to varobj_list_children for
  760.              this variable object, and the child was never created,
  761.              or it was explicitly deleted by the client.  */
  762.           name = name_of_child (var, i);
  763.           existing = create_child (var, i, name);
  764.           VEC_replace (varobj_p, var->children, i, existing);
  765.         }
  766.     }

  767.   varobj_restrict_range (var->children, from, to);
  768.   return var->children;
  769. }

  770. static struct varobj *
  771. varobj_add_child (struct varobj *var, struct varobj_item *item)
  772. {
  773.   varobj_p v = create_child_with_value (var,
  774.                                         VEC_length (varobj_p, var->children),
  775.                                         item);

  776.   VEC_safe_push (varobj_p, var->children, v);
  777.   return v;
  778. }

  779. /* Obtain the type of an object Variable as a string similar to the one gdb
  780.    prints on the console.  */

  781. char *
  782. varobj_get_type (struct varobj *var)
  783. {
  784.   /* For the "fake" variables, do not return a type.  (Its type is
  785.      NULL, too.)
  786.      Do not return a type for invalid variables as well.  */
  787.   if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
  788.     return NULL;

  789.   return type_to_string (var->type);
  790. }

  791. /* Obtain the type of an object variable.  */

  792. struct type *
  793. varobj_get_gdb_type (struct varobj *var)
  794. {
  795.   return var->type;
  796. }

  797. /* Is VAR a path expression parent, i.e., can it be used to construct
  798.    a valid path expression?  */

  799. static int
  800. is_path_expr_parent (struct varobj *var)
  801. {
  802.   gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
  803.   return var->root->lang_ops->is_path_expr_parent (var);
  804. }

  805. /* Is VAR a path expression parent, i.e., can it be used to construct
  806.    a valid path expression?  By default we assume any VAR can be a path
  807.    parent.  */

  808. int
  809. varobj_default_is_path_expr_parent (struct varobj *var)
  810. {
  811.   return 1;
  812. }

  813. /* Return the path expression parent for VAR.  */

  814. struct varobj *
  815. varobj_get_path_expr_parent (struct varobj *var)
  816. {
  817.   struct varobj *parent = var;

  818.   while (!is_root_p (parent) && !is_path_expr_parent (parent))
  819.     parent = parent->parent;

  820.   return parent;
  821. }

  822. /* Return a pointer to the full rooted expression of varobj VAR.
  823.    If it has not been computed yet, compute it.  */
  824. char *
  825. varobj_get_path_expr (struct varobj *var)
  826. {
  827.   if (var->path_expr != NULL)
  828.     return var->path_expr;
  829.   else
  830.     {
  831.       /* For root varobjs, we initialize path_expr
  832.          when creating varobj, so here it should be
  833.          child varobj.  */
  834.       gdb_assert (!is_root_p (var));
  835.       return (*var->root->lang_ops->path_expr_of_child) (var);
  836.     }
  837. }

  838. const struct language_defn *
  839. varobj_get_language (struct varobj *var)
  840. {
  841.   return var->root->exp->language_defn;
  842. }

  843. int
  844. varobj_get_attributes (struct varobj *var)
  845. {
  846.   int attributes = 0;

  847.   if (varobj_editable_p (var))
  848.     /* FIXME: define masks for attributes.  */
  849.     attributes |= 0x00000001;        /* Editable */

  850.   return attributes;
  851. }

  852. /* Return true if VAR is a dynamic varobj.  */

  853. int
  854. varobj_is_dynamic_p (struct varobj *var)
  855. {
  856.   return var->dynamic->pretty_printer != NULL;
  857. }

  858. char *
  859. varobj_get_formatted_value (struct varobj *var,
  860.                             enum varobj_display_formats format)
  861. {
  862.   return my_value_of_variable (var, format);
  863. }

  864. char *
  865. varobj_get_value (struct varobj *var)
  866. {
  867.   return my_value_of_variable (var, var->format);
  868. }

  869. /* Set the value of an object variable (if it is editable) to the
  870.    value of the given expression.  */
  871. /* Note: Invokes functions that can call error().  */

  872. int
  873. varobj_set_value (struct varobj *var, char *expression)
  874. {
  875.   struct value *val = NULL; /* Initialize to keep gcc happy.  */
  876.   /* The argument "expression" contains the variable's new value.
  877.      We need to first construct a legal expression for this -- ugh!  */
  878.   /* Does this cover all the bases?  */
  879.   struct expression *exp;
  880.   struct value *value = NULL; /* Initialize to keep gcc happy.  */
  881.   int saved_input_radix = input_radix;
  882.   const char *s = expression;
  883.   volatile struct gdb_exception except;

  884.   gdb_assert (varobj_editable_p (var));

  885.   input_radix = 10;                /* ALWAYS reset to decimal temporarily.  */
  886.   exp = parse_exp_1 (&s, 0, 0, 0);
  887.   TRY_CATCH (except, RETURN_MASK_ERROR)
  888.     {
  889.       value = evaluate_expression (exp);
  890.     }

  891.   if (except.reason < 0)
  892.     {
  893.       /* We cannot proceed without a valid expression.  */
  894.       xfree (exp);
  895.       return 0;
  896.     }

  897.   /* All types that are editable must also be changeable.  */
  898.   gdb_assert (varobj_value_is_changeable_p (var));

  899.   /* The value of a changeable variable object must not be lazy.  */
  900.   gdb_assert (!value_lazy (var->value));

  901.   /* Need to coerce the input.  We want to check if the
  902.      value of the variable object will be different
  903.      after assignment, and the first thing value_assign
  904.      does is coerce the input.
  905.      For example, if we are assigning an array to a pointer variable we
  906.      should compare the pointer with the array's address, not with the
  907.      array's content.  */
  908.   value = coerce_array (value);

  909.   /* The new value may be lazy.  value_assign, or
  910.      rather value_contents, will take care of this.  */
  911.   TRY_CATCH (except, RETURN_MASK_ERROR)
  912.     {
  913.       val = value_assign (var->value, value);
  914.     }

  915.   if (except.reason < 0)
  916.     return 0;

  917.   /* If the value has changed, record it, so that next -var-update can
  918.      report this change.  If a variable had a value of '1', we've set it
  919.      to '333' and then set again to '1', when -var-update will report this
  920.      variable as changed -- because the first assignment has set the
  921.      'updated' flag.  There's no need to optimize that, because return value
  922.      of -var-update should be considered an approximation.  */
  923.   var->updated = install_new_value (var, val, 0 /* Compare values.  */);
  924.   input_radix = saved_input_radix;
  925.   return 1;
  926. }

  927. #if HAVE_PYTHON

  928. /* A helper function to install a constructor function and visualizer
  929.    in a varobj_dynamic.  */

  930. static void
  931. install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
  932.                     PyObject *visualizer)
  933. {
  934.   Py_XDECREF (var->constructor);
  935.   var->constructor = constructor;

  936.   Py_XDECREF (var->pretty_printer);
  937.   var->pretty_printer = visualizer;

  938.   varobj_iter_delete (var->child_iter);
  939.   var->child_iter = NULL;
  940. }

  941. /* Install the default visualizer for VAR.  */

  942. static void
  943. install_default_visualizer (struct varobj *var)
  944. {
  945.   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
  946.   if (CPLUS_FAKE_CHILD (var))
  947.     return;

  948.   if (pretty_printing)
  949.     {
  950.       PyObject *pretty_printer = NULL;

  951.       if (var->value)
  952.         {
  953.           pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
  954.           if (! pretty_printer)
  955.             {
  956.               gdbpy_print_stack ();
  957.               error (_("Cannot instantiate printer for default visualizer"));
  958.             }
  959.         }

  960.       if (pretty_printer == Py_None)
  961.         {
  962.           Py_DECREF (pretty_printer);
  963.           pretty_printer = NULL;
  964.         }

  965.       install_visualizer (var->dynamic, NULL, pretty_printer);
  966.     }
  967. }

  968. /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
  969.    make a new object.  */

  970. static void
  971. construct_visualizer (struct varobj *var, PyObject *constructor)
  972. {
  973.   PyObject *pretty_printer;

  974.   /* Do not install a visualizer on a CPLUS_FAKE_CHILD.  */
  975.   if (CPLUS_FAKE_CHILD (var))
  976.     return;

  977.   Py_INCREF (constructor);
  978.   if (constructor == Py_None)
  979.     pretty_printer = NULL;
  980.   else
  981.     {
  982.       pretty_printer = instantiate_pretty_printer (constructor, var->value);
  983.       if (! pretty_printer)
  984.         {
  985.           gdbpy_print_stack ();
  986.           Py_DECREF (constructor);
  987.           constructor = Py_None;
  988.           Py_INCREF (constructor);
  989.         }

  990.       if (pretty_printer == Py_None)
  991.         {
  992.           Py_DECREF (pretty_printer);
  993.           pretty_printer = NULL;
  994.         }
  995.     }

  996.   install_visualizer (var->dynamic, constructor, pretty_printer);
  997. }

  998. #endif /* HAVE_PYTHON */

  999. /* A helper function for install_new_value.  This creates and installs
  1000.    a visualizer for VAR, if appropriate.  */

  1001. static void
  1002. install_new_value_visualizer (struct varobj *var)
  1003. {
  1004. #if HAVE_PYTHON
  1005.   /* If the constructor is None, then we want the raw value.  If VAR
  1006.      does not have a value, just skip this.  */
  1007.   if (!gdb_python_initialized)
  1008.     return;

  1009.   if (var->dynamic->constructor != Py_None && var->value != NULL)
  1010.     {
  1011.       struct cleanup *cleanup;

  1012.       cleanup = varobj_ensure_python_env (var);

  1013.       if (var->dynamic->constructor == NULL)
  1014.         install_default_visualizer (var);
  1015.       else
  1016.         construct_visualizer (var, var->dynamic->constructor);

  1017.       do_cleanups (cleanup);
  1018.     }
  1019. #else
  1020.   /* Do nothing.  */
  1021. #endif
  1022. }

  1023. /* When using RTTI to determine variable type it may be changed in runtime when
  1024.    the variable value is changed.  This function checks whether type of varobj
  1025.    VAR will change when a new value NEW_VALUE is assigned and if it is so
  1026.    updates the type of VAR.  */

  1027. static int
  1028. update_type_if_necessary (struct varobj *var, struct value *new_value)
  1029. {
  1030.   if (new_value)
  1031.     {
  1032.       struct value_print_options opts;

  1033.       get_user_print_options (&opts);
  1034.       if (opts.objectprint)
  1035.         {
  1036.           struct type *new_type;
  1037.           char *curr_type_str, *new_type_str;

  1038.           new_type = value_actual_type (new_value, 0, 0);
  1039.           new_type_str = type_to_string (new_type);
  1040.           curr_type_str = varobj_get_type (var);
  1041.           if (strcmp (curr_type_str, new_type_str) != 0)
  1042.             {
  1043.               var->type = new_type;

  1044.               /* This information may be not valid for a new type.  */
  1045.               varobj_delete (var, NULL, 1);
  1046.               VEC_free (varobj_p, var->children);
  1047.               var->num_children = -1;
  1048.               return 1;
  1049.             }
  1050.         }
  1051.     }

  1052.   return 0;
  1053. }

  1054. /* Assign a new value to a variable object.  If INITIAL is non-zero,
  1055.    this is the first assignement after the variable object was just
  1056.    created, or changed type.  In that case, just assign the value
  1057.    and return 0.
  1058.    Otherwise, assign the new value, and return 1 if the value is
  1059.    different from the current one, 0 otherwise.  The comparison is
  1060.    done on textual representation of value.  Therefore, some types
  1061.    need not be compared.  E.g.  for structures the reported value is
  1062.    always "{...}", so no comparison is necessary here.  If the old
  1063.    value was NULL and new one is not, or vice versa, we always return 1.

  1064.    The VALUE parameter should not be released -- the function will
  1065.    take care of releasing it when needed.  */
  1066. static int
  1067. install_new_value (struct varobj *var, struct value *value, int initial)
  1068. {
  1069.   int changeable;
  1070.   int need_to_fetch;
  1071.   int changed = 0;
  1072.   int intentionally_not_fetched = 0;
  1073.   char *print_value = NULL;

  1074.   /* We need to know the varobj's type to decide if the value should
  1075.      be fetched or not.  C++ fake children (public/protected/private)
  1076.      don't have a type.  */
  1077.   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
  1078.   changeable = varobj_value_is_changeable_p (var);

  1079.   /* If the type has custom visualizer, we consider it to be always
  1080.      changeable.  FIXME: need to make sure this behaviour will not
  1081.      mess up read-sensitive values.  */
  1082.   if (var->dynamic->pretty_printer != NULL)
  1083.     changeable = 1;

  1084.   need_to_fetch = changeable;

  1085.   /* We are not interested in the address of references, and given
  1086.      that in C++ a reference is not rebindable, it cannot
  1087.      meaningfully change.  So, get hold of the real value.  */
  1088.   if (value)
  1089.     value = coerce_ref (value);

  1090.   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
  1091.     /* For unions, we need to fetch the value implicitly because
  1092.        of implementation of union member fetch.  When gdb
  1093.        creates a value for a field and the value of the enclosing
  1094.        structure is not lazy,  it immediately copies the necessary
  1095.        bytes from the enclosing values.  If the enclosing value is
  1096.        lazy, the call to value_fetch_lazy on the field will read
  1097.        the data from memory.  For unions, that means we'll read the
  1098.        same memory more than once, which is not desirable.  So
  1099.        fetch now.  */
  1100.     need_to_fetch = 1;

  1101.   /* The new value might be lazy.  If the type is changeable,
  1102.      that is we'll be comparing values of this type, fetch the
  1103.      value now.  Otherwise, on the next update the old value
  1104.      will be lazy, which means we've lost that old value.  */
  1105.   if (need_to_fetch && value && value_lazy (value))
  1106.     {
  1107.       struct varobj *parent = var->parent;
  1108.       int frozen = var->frozen;

  1109.       for (; !frozen && parent; parent = parent->parent)
  1110.         frozen |= parent->frozen;

  1111.       if (frozen && initial)
  1112.         {
  1113.           /* For variables that are frozen, or are children of frozen
  1114.              variables, we don't do fetch on initial assignment.
  1115.              For non-initial assignemnt we do the fetch, since it means we're
  1116.              explicitly asked to compare the new value with the old one.  */
  1117.           intentionally_not_fetched = 1;
  1118.         }
  1119.       else
  1120.         {
  1121.           volatile struct gdb_exception except;

  1122.           TRY_CATCH (except, RETURN_MASK_ERROR)
  1123.             {
  1124.               value_fetch_lazy (value);
  1125.             }

  1126.           if (except.reason < 0)
  1127.             {
  1128.               /* Set the value to NULL, so that for the next -var-update,
  1129.                  we don't try to compare the new value with this value,
  1130.                  that we couldn't even read.  */
  1131.               value = NULL;
  1132.             }
  1133.         }
  1134.     }

  1135.   /* Get a reference now, before possibly passing it to any Python
  1136.      code that might release it.  */
  1137.   if (value != NULL)
  1138.     value_incref (value);

  1139.   /* Below, we'll be comparing string rendering of old and new
  1140.      values.  Don't get string rendering if the value is
  1141.      lazy -- if it is, the code above has decided that the value
  1142.      should not be fetched.  */
  1143.   if (value != NULL && !value_lazy (value)
  1144.       && var->dynamic->pretty_printer == NULL)
  1145.     print_value = varobj_value_get_print_value (value, var->format, var);

  1146.   /* If the type is changeable, compare the old and the new values.
  1147.      If this is the initial assignment, we don't have any old value
  1148.      to compare with.  */
  1149.   if (!initial && changeable)
  1150.     {
  1151.       /* If the value of the varobj was changed by -var-set-value,
  1152.          then the value in the varobj and in the target is the same.
  1153.          However, that value is different from the value that the
  1154.          varobj had after the previous -var-update.  So need to the
  1155.          varobj as changed.  */
  1156.       if (var->updated)
  1157.         {
  1158.           changed = 1;
  1159.         }
  1160.       else if (var->dynamic->pretty_printer == NULL)
  1161.         {
  1162.           /* Try to compare the values.  That requires that both
  1163.              values are non-lazy.  */
  1164.           if (var->not_fetched && value_lazy (var->value))
  1165.             {
  1166.               /* This is a frozen varobj and the value was never read.
  1167.                  Presumably, UI shows some "never read" indicator.
  1168.                  Now that we've fetched the real value, we need to report
  1169.                  this varobj as changed so that UI can show the real
  1170.                  value.  */
  1171.               changed = 1;
  1172.             }
  1173.           else  if (var->value == NULL && value == NULL)
  1174.             /* Equal.  */
  1175.             ;
  1176.           else if (var->value == NULL || value == NULL)
  1177.             {
  1178.               changed = 1;
  1179.             }
  1180.           else
  1181.             {
  1182.               gdb_assert (!value_lazy (var->value));
  1183.               gdb_assert (!value_lazy (value));

  1184.               gdb_assert (var->print_value != NULL && print_value != NULL);
  1185.               if (strcmp (var->print_value, print_value) != 0)
  1186.                 changed = 1;
  1187.             }
  1188.         }
  1189.     }

  1190.   if (!initial && !changeable)
  1191.     {
  1192.       /* For values that are not changeable, we don't compare the values.
  1193.          However, we want to notice if a value was not NULL and now is NULL,
  1194.          or vise versa, so that we report when top-level varobjs come in scope
  1195.          and leave the scope.  */
  1196.       changed = (var->value != NULL) != (value != NULL);
  1197.     }

  1198.   /* We must always keep the new value, since children depend on it.  */
  1199.   if (var->value != NULL && var->value != value)
  1200.     value_free (var->value);
  1201.   var->value = value;
  1202.   if (value && value_lazy (value) && intentionally_not_fetched)
  1203.     var->not_fetched = 1;
  1204.   else
  1205.     var->not_fetched = 0;
  1206.   var->updated = 0;

  1207.   install_new_value_visualizer (var);

  1208.   /* If we installed a pretty-printer, re-compare the printed version
  1209.      to see if the variable changed.  */
  1210.   if (var->dynamic->pretty_printer != NULL)
  1211.     {
  1212.       xfree (print_value);
  1213.       print_value = varobj_value_get_print_value (var->value, var->format,
  1214.                                                   var);
  1215.       if ((var->print_value == NULL && print_value != NULL)
  1216.           || (var->print_value != NULL && print_value == NULL)
  1217.           || (var->print_value != NULL && print_value != NULL
  1218.               && strcmp (var->print_value, print_value) != 0))
  1219.         changed = 1;
  1220.     }
  1221.   if (var->print_value)
  1222.     xfree (var->print_value);
  1223.   var->print_value = print_value;

  1224.   gdb_assert (!var->value || value_type (var->value));

  1225.   return changed;
  1226. }

  1227. /* Return the requested range for a varobjVAR is the varobj.  FROM
  1228.    and TO are out parameters; *FROM and *TO will be set to the
  1229.    selected sub-range of VAR.  If no range was selected using
  1230.    -var-set-update-range, then both will be -1.  */
  1231. void
  1232. varobj_get_child_range (struct varobj *var, int *from, int *to)
  1233. {
  1234.   *from = var->from;
  1235.   *to = var->to;
  1236. }

  1237. /* Set the selected sub-range of children of VAR to start at index
  1238.    FROM and end at index TO.  If either FROM or TO is less than zero,
  1239.    this is interpreted as a request for all children.  */
  1240. void
  1241. varobj_set_child_range (struct varobj *var, int from, int to)
  1242. {
  1243.   var->from = from;
  1244.   var->to = to;
  1245. }

  1246. void
  1247. varobj_set_visualizer (struct varobj *var, const char *visualizer)
  1248. {
  1249. #if HAVE_PYTHON
  1250.   PyObject *mainmod, *globals, *constructor;
  1251.   struct cleanup *back_to;

  1252.   if (!gdb_python_initialized)
  1253.     return;

  1254.   back_to = varobj_ensure_python_env (var);

  1255.   mainmod = PyImport_AddModule ("__main__");
  1256.   globals = PyModule_GetDict (mainmod);
  1257.   Py_INCREF (globals);
  1258.   make_cleanup_py_decref (globals);

  1259.   constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);

  1260.   if (! constructor)
  1261.     {
  1262.       gdbpy_print_stack ();
  1263.       error (_("Could not evaluate visualizer expression: %s"), visualizer);
  1264.     }

  1265.   construct_visualizer (var, constructor);
  1266.   Py_XDECREF (constructor);

  1267.   /* If there are any children now, wipe them.  */
  1268.   varobj_delete (var, NULL, 1 /* children only */);
  1269.   var->num_children = -1;

  1270.   do_cleanups (back_to);
  1271. #else
  1272.   error (_("Python support required"));
  1273. #endif
  1274. }

  1275. /* If NEW_VALUE is the new value of the given varobj (var), return
  1276.    non-zero if var has mutated.  In other words, if the type of
  1277.    the new value is different from the type of the varobj's old
  1278.    value.

  1279.    NEW_VALUE may be NULL, if the varobj is now out of scope.  */

  1280. static int
  1281. varobj_value_has_mutated (struct varobj *var, struct value *new_value,
  1282.                           struct type *new_type)
  1283. {
  1284.   /* If we haven't previously computed the number of children in var,
  1285.      it does not matter from the front-end's perspective whether
  1286.      the type has mutated or not.  For all intents and purposes,
  1287.      it has not mutated.  */
  1288.   if (var->num_children < 0)
  1289.     return 0;

  1290.   if (var->root->lang_ops->value_has_mutated)
  1291.     {
  1292.       /* The varobj module, when installing new values, explicitly strips
  1293.          references, saying that we're not interested in those addresses.
  1294.          But detection of mutation happens before installing the new
  1295.          value, so our value may be a reference that we need to strip
  1296.          in order to remain consistent.  */
  1297.       if (new_value != NULL)
  1298.         new_value = coerce_ref (new_value);
  1299.       return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
  1300.     }
  1301.   else
  1302.     return 0;
  1303. }

  1304. /* Update the values for a variable and its children.  This is a
  1305.    two-pronged attack.  First, re-parse the value for the root's
  1306.    expression to see if it's changed.  Then go all the way
  1307.    through its children, reconstructing them and noting if they've
  1308.    changed.

  1309.    The EXPLICIT parameter specifies if this call is result
  1310.    of MI request to update this specific variable, or
  1311.    result of implicit -var-update *.  For implicit request, we don't
  1312.    update frozen variables.

  1313.    NOTE: This function may delete the caller's varobj.  If it
  1314.    returns TYPE_CHANGED, then it has done this and VARP will be modified
  1315.    to point to the new varobj.  */

  1316. VEC(varobj_update_result) *
  1317. varobj_update (struct varobj **varp, int explicit)
  1318. {
  1319.   int type_changed = 0;
  1320.   int i;
  1321.   struct value *new;
  1322.   VEC (varobj_update_result) *stack = NULL;
  1323.   VEC (varobj_update_result) *result = NULL;

  1324.   /* Frozen means frozen -- we don't check for any change in
  1325.      this varobj, including its going out of scope, or
  1326.      changing typeOne use case for frozen varobjs is
  1327.      retaining previously evaluated expressions, and we don't
  1328.      want them to be reevaluated at all.  */
  1329.   if (!explicit && (*varp)->frozen)
  1330.     return result;

  1331.   if (!(*varp)->root->is_valid)
  1332.     {
  1333.       varobj_update_result r = {0};

  1334.       r.varobj = *varp;
  1335.       r.status = VAROBJ_INVALID;
  1336.       VEC_safe_push (varobj_update_result, result, &r);
  1337.       return result;
  1338.     }

  1339.   if ((*varp)->root->rootvar == *varp)
  1340.     {
  1341.       varobj_update_result r = {0};

  1342.       r.varobj = *varp;
  1343.       r.status = VAROBJ_IN_SCOPE;

  1344.       /* Update the root variable.  value_of_root can return NULL
  1345.          if the variable is no longer around, i.e. we stepped out of
  1346.          the frame in which a local existed.  We are letting the
  1347.          value_of_root variable dispose of the varobj if the type
  1348.          has changed.  */
  1349.       new = value_of_root (varp, &type_changed);
  1350.       if (update_type_if_necessary(*varp, new))
  1351.           type_changed = 1;
  1352.       r.varobj = *varp;
  1353.       r.type_changed = type_changed;
  1354.       if (install_new_value ((*varp), new, type_changed))
  1355.         r.changed = 1;

  1356.       if (new == NULL)
  1357.         r.status = VAROBJ_NOT_IN_SCOPE;
  1358.       r.value_installed = 1;

  1359.       if (r.status == VAROBJ_NOT_IN_SCOPE)
  1360.         {
  1361.           if (r.type_changed || r.changed)
  1362.             VEC_safe_push (varobj_update_result, result, &r);
  1363.           return result;
  1364.         }

  1365.       VEC_safe_push (varobj_update_result, stack, &r);
  1366.     }
  1367.   else
  1368.     {
  1369.       varobj_update_result r = {0};

  1370.       r.varobj = *varp;
  1371.       VEC_safe_push (varobj_update_result, stack, &r);
  1372.     }

  1373.   /* Walk through the children, reconstructing them all.  */
  1374.   while (!VEC_empty (varobj_update_result, stack))
  1375.     {
  1376.       varobj_update_result r = *(VEC_last (varobj_update_result, stack));
  1377.       struct varobj *v = r.varobj;

  1378.       VEC_pop (varobj_update_result, stack);

  1379.       /* Update this variable, unless it's a root, which is already
  1380.          updated.  */
  1381.       if (!r.value_installed)
  1382.         {
  1383.           struct type *new_type;

  1384.           new = value_of_child (v->parent, v->index);
  1385.           if (update_type_if_necessary(v, new))
  1386.             r.type_changed = 1;
  1387.           if (new)
  1388.             new_type = value_type (new);
  1389.           else
  1390.             new_type = v->root->lang_ops->type_of_child (v->parent, v->index);

  1391.           if (varobj_value_has_mutated (v, new, new_type))
  1392.             {
  1393.               /* The children are no longer valid; delete them now.
  1394.                  Report the fact that its type changed as well.  */
  1395.               varobj_delete (v, NULL, 1 /* only_children */);
  1396.               v->num_children = -1;
  1397.               v->to = -1;
  1398.               v->from = -1;
  1399.               v->type = new_type;
  1400.               r.type_changed = 1;
  1401.             }

  1402.           if (install_new_value (v, new, r.type_changed))
  1403.             {
  1404.               r.changed = 1;
  1405.               v->updated = 0;
  1406.             }
  1407.         }

  1408.       /* We probably should not get children of a dynamic varobj, but
  1409.          for which -var-list-children was never invoked.  */
  1410.       if (varobj_is_dynamic_p (v))
  1411.         {
  1412.           VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0;
  1413.           VEC (varobj_p) *new = 0;
  1414.           int i, children_changed = 0;

  1415.           if (v->frozen)
  1416.             continue;

  1417.           if (!v->dynamic->children_requested)
  1418.             {
  1419.               int dummy;

  1420.               /* If we initially did not have potential children, but
  1421.                  now we do, consider the varobj as changed.
  1422.                  Otherwise, if children were never requested, consider
  1423.                  it as unchanged -- presumably, such varobj is not yet
  1424.                  expanded in the UI, so we need not bother getting
  1425.                  it.  */
  1426.               if (!varobj_has_more (v, 0))
  1427.                 {
  1428.                   update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
  1429.                                                   &dummy, 0, 0, 0);
  1430.                   if (varobj_has_more (v, 0))
  1431.                     r.changed = 1;
  1432.                 }

  1433.               if (r.changed)
  1434.                 VEC_safe_push (varobj_update_result, result, &r);

  1435.               continue;
  1436.             }

  1437.           /* If update_dynamic_varobj_children returns 0, then we have
  1438.              a non-conforming pretty-printer, so we skip it.  */
  1439.           if (update_dynamic_varobj_children (v, &changed, &type_changed, &new,
  1440.                                               &unchanged, &children_changed, 1,
  1441.                                               v->from, v->to))
  1442.             {
  1443.               if (children_changed || new)
  1444.                 {
  1445.                   r.children_changed = 1;
  1446.                   r.new = new;
  1447.                 }
  1448.               /* Push in reverse order so that the first child is
  1449.                  popped from the work stack first, and so will be
  1450.                  added to result first.  This does not affect
  1451.                  correctness, just "nicer".  */
  1452.               for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i)
  1453.                 {
  1454.                   varobj_p tmp = VEC_index (varobj_p, type_changed, i);
  1455.                   varobj_update_result r = {0};

  1456.                   /* Type may change only if value was changed.  */
  1457.                   r.varobj = tmp;
  1458.                   r.changed = 1;
  1459.                   r.type_changed = 1;
  1460.                   r.value_installed = 1;
  1461.                   VEC_safe_push (varobj_update_result, stack, &r);
  1462.                 }
  1463.               for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
  1464.                 {
  1465.                   varobj_p tmp = VEC_index (varobj_p, changed, i);
  1466.                   varobj_update_result r = {0};

  1467.                   r.varobj = tmp;
  1468.                   r.changed = 1;
  1469.                   r.value_installed = 1;
  1470.                   VEC_safe_push (varobj_update_result, stack, &r);
  1471.                 }
  1472.               for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
  1473.                       {
  1474.                   varobj_p tmp = VEC_index (varobj_p, unchanged, i);

  1475.                         if (!tmp->frozen)
  1476.                           {
  1477.                             varobj_update_result r = {0};

  1478.                       r.varobj = tmp;
  1479.                             r.value_installed = 1;
  1480.                             VEC_safe_push (varobj_update_result, stack, &r);
  1481.                           }
  1482.                       }
  1483.               if (r.changed || r.children_changed)
  1484.                 VEC_safe_push (varobj_update_result, result, &r);

  1485.               /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
  1486.                  because NEW has been put into the result vector.  */
  1487.               VEC_free (varobj_p, changed);
  1488.               VEC_free (varobj_p, type_changed);
  1489.               VEC_free (varobj_p, unchanged);

  1490.               continue;
  1491.             }
  1492.         }

  1493.       /* Push any children.  Use reverse order so that the first
  1494.          child is popped from the work stack first, and so
  1495.          will be added to result first.  This does not
  1496.          affect correctness, just "nicer".  */
  1497.       for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
  1498.         {
  1499.           varobj_p c = VEC_index (varobj_p, v->children, i);

  1500.           /* Child may be NULL if explicitly deleted by -var-delete.  */
  1501.           if (c != NULL && !c->frozen)
  1502.             {
  1503.               varobj_update_result r = {0};

  1504.               r.varobj = c;
  1505.               VEC_safe_push (varobj_update_result, stack, &r);
  1506.             }
  1507.         }

  1508.       if (r.changed || r.type_changed)
  1509.         VEC_safe_push (varobj_update_result, result, &r);
  1510.     }

  1511.   VEC_free (varobj_update_result, stack);

  1512.   return result;
  1513. }


  1514. /* Helper functions */

  1515. /*
  1516. * Variable object construction/destruction
  1517. */

  1518. static int
  1519. delete_variable (struct cpstack **resultp, struct varobj *var,
  1520.                  int only_children_p)
  1521. {
  1522.   int delcount = 0;

  1523.   delete_variable_1 (resultp, &delcount, var,
  1524.                      only_children_p, 1 /* remove_from_parent_p */ );

  1525.   return delcount;
  1526. }

  1527. /* Delete the variable object VAR and its children.  */
  1528. /* IMPORTANT NOTE: If we delete a variable which is a child
  1529.    and the parent is not removed we dump core.  It must be always
  1530.    initially called with remove_from_parent_p set.  */
  1531. static void
  1532. delete_variable_1 (struct cpstack **resultp, int *delcountp,
  1533.                    struct varobj *var, int only_children_p,
  1534.                    int remove_from_parent_p)
  1535. {
  1536.   int i;

  1537.   /* Delete any children of this variable, too.  */
  1538.   for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
  1539.     {
  1540.       varobj_p child = VEC_index (varobj_p, var->children, i);

  1541.       if (!child)
  1542.         continue;
  1543.       if (!remove_from_parent_p)
  1544.         child->parent = NULL;
  1545.       delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
  1546.     }
  1547.   VEC_free (varobj_p, var->children);

  1548.   /* if we were called to delete only the children we are done here.  */
  1549.   if (only_children_p)
  1550.     return;

  1551.   /* Otherwise, add it to the list of deleted ones and proceed to do so.  */
  1552.   /* If the name is null, this is a temporary variable, that has not
  1553.      yet been installed, don't report it, it belongs to the caller...  */
  1554.   if (var->obj_name != NULL)
  1555.     {
  1556.       cppush (resultp, xstrdup (var->obj_name));
  1557.       *delcountp = *delcountp + 1;
  1558.     }

  1559.   /* If this variable has a parent, remove it from its parent's list.  */
  1560.   /* OPTIMIZATION: if the parent of this variable is also being deleted,
  1561.      (as indicated by remove_from_parent_p) we don't bother doing an
  1562.      expensive list search to find the element to remove when we are
  1563.      discarding the list afterwards.  */
  1564.   if ((remove_from_parent_p) && (var->parent != NULL))
  1565.     {
  1566.       VEC_replace (varobj_p, var->parent->children, var->index, NULL);
  1567.     }

  1568.   if (var->obj_name != NULL)
  1569.     uninstall_variable (var);

  1570.   /* Free memory associated with this variable.  */
  1571.   free_variable (var);
  1572. }

  1573. /* Install the given variable VAR with the object name VAR->OBJ_NAME.  */
  1574. static int
  1575. install_variable (struct varobj *var)
  1576. {
  1577.   struct vlist *cv;
  1578.   struct vlist *newvl;
  1579.   const char *chp;
  1580.   unsigned int index = 0;
  1581.   unsigned int i = 1;

  1582.   for (chp = var->obj_name; *chp; chp++)
  1583.     {
  1584.       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
  1585.     }

  1586.   cv = *(varobj_table + index);
  1587.   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
  1588.     cv = cv->next;

  1589.   if (cv != NULL)
  1590.     error (_("Duplicate variable object name"));

  1591.   /* Add varobj to hash table.  */
  1592.   newvl = xmalloc (sizeof (struct vlist));
  1593.   newvl->next = *(varobj_table + index);
  1594.   newvl->var = var;
  1595.   *(varobj_table + index) = newvl;

  1596.   /* If root, add varobj to root list.  */
  1597.   if (is_root_p (var))
  1598.     {
  1599.       /* Add to list of root variables.  */
  1600.       if (rootlist == NULL)
  1601.         var->root->next = NULL;
  1602.       else
  1603.         var->root->next = rootlist;
  1604.       rootlist = var->root;
  1605.     }

  1606.   return 1;                        /* OK */
  1607. }

  1608. /* Unistall the object VAR.  */
  1609. static void
  1610. uninstall_variable (struct varobj *var)
  1611. {
  1612.   struct vlist *cv;
  1613.   struct vlist *prev;
  1614.   struct varobj_root *cr;
  1615.   struct varobj_root *prer;
  1616.   const char *chp;
  1617.   unsigned int index = 0;
  1618.   unsigned int i = 1;

  1619.   /* Remove varobj from hash table.  */
  1620.   for (chp = var->obj_name; *chp; chp++)
  1621.     {
  1622.       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
  1623.     }

  1624.   cv = *(varobj_table + index);
  1625.   prev = NULL;
  1626.   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
  1627.     {
  1628.       prev = cv;
  1629.       cv = cv->next;
  1630.     }

  1631.   if (varobjdebug)
  1632.     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);

  1633.   if (cv == NULL)
  1634.     {
  1635.       warning
  1636.         ("Assertion failed: Could not find variable object \"%s\" to delete",
  1637.          var->obj_name);
  1638.       return;
  1639.     }

  1640.   if (prev == NULL)
  1641.     *(varobj_table + index) = cv->next;
  1642.   else
  1643.     prev->next = cv->next;

  1644.   xfree (cv);

  1645.   /* If root, remove varobj from root list.  */
  1646.   if (is_root_p (var))
  1647.     {
  1648.       /* Remove from list of root variables.  */
  1649.       if (rootlist == var->root)
  1650.         rootlist = var->root->next;
  1651.       else
  1652.         {
  1653.           prer = NULL;
  1654.           cr = rootlist;
  1655.           while ((cr != NULL) && (cr->rootvar != var))
  1656.             {
  1657.               prer = cr;
  1658.               cr = cr->next;
  1659.             }
  1660.           if (cr == NULL)
  1661.             {
  1662.               warning (_("Assertion failed: Could not find "
  1663.                          "varobj \"%s\" in root list"),
  1664.                        var->obj_name);
  1665.               return;
  1666.             }
  1667.           if (prer == NULL)
  1668.             rootlist = NULL;
  1669.           else
  1670.             prer->next = cr->next;
  1671.         }
  1672.     }

  1673. }

  1674. /* Create and install a child of the parent of the given name.  */
  1675. static struct varobj *
  1676. create_child (struct varobj *parent, int index, char *name)
  1677. {
  1678.   struct varobj_item item;

  1679.   item.name = name;
  1680.   item.value = value_of_child (parent, index);

  1681.   return create_child_with_value (parent, index, &item);
  1682. }

  1683. static struct varobj *
  1684. create_child_with_value (struct varobj *parent, int index,
  1685.                          struct varobj_item *item)
  1686. {
  1687.   struct varobj *child;
  1688.   char *childs_name;

  1689.   child = new_variable ();

  1690.   /* NAME is allocated by caller.  */
  1691.   child->name = item->name;
  1692.   child->index = index;
  1693.   child->parent = parent;
  1694.   child->root = parent->root;

  1695.   if (varobj_is_anonymous_child (child))
  1696.     childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
  1697.   else
  1698.     childs_name = xstrprintf ("%s.%s", parent->obj_name, item->name);
  1699.   child->obj_name = childs_name;

  1700.   install_variable (child);

  1701.   /* Compute the type of the child.  Must do this before
  1702.      calling install_new_value.  */
  1703.   if (item->value != NULL)
  1704.     /* If the child had no evaluation errors, var->value
  1705.        will be non-NULL and contain a valid type.  */
  1706.     child->type = value_actual_type (item->value, 0, NULL);
  1707.   else
  1708.     /* Otherwise, we must compute the type.  */
  1709.     child->type = (*child->root->lang_ops->type_of_child) (child->parent,
  1710.                                                            child->index);
  1711.   install_new_value (child, item->value, 1);

  1712.   return child;
  1713. }


  1714. /*
  1715. * Miscellaneous utility functions.
  1716. */

  1717. /* Allocate memory and initialize a new variable.  */
  1718. static struct varobj *
  1719. new_variable (void)
  1720. {
  1721.   struct varobj *var;

  1722.   var = (struct varobj *) xmalloc (sizeof (struct varobj));
  1723.   var->name = NULL;
  1724.   var->path_expr = NULL;
  1725.   var->obj_name = NULL;
  1726.   var->index = -1;
  1727.   var->type = NULL;
  1728.   var->value = NULL;
  1729.   var->num_children = -1;
  1730.   var->parent = NULL;
  1731.   var->children = NULL;
  1732.   var->format = 0;
  1733.   var->root = NULL;
  1734.   var->updated = 0;
  1735.   var->print_value = NULL;
  1736.   var->frozen = 0;
  1737.   var->not_fetched = 0;
  1738.   var->dynamic
  1739.     = (struct varobj_dynamic *) xmalloc (sizeof (struct varobj_dynamic));
  1740.   var->dynamic->children_requested = 0;
  1741.   var->from = -1;
  1742.   var->to = -1;
  1743.   var->dynamic->constructor = 0;
  1744.   var->dynamic->pretty_printer = 0;
  1745.   var->dynamic->child_iter = 0;
  1746.   var->dynamic->saved_item = 0;

  1747.   return var;
  1748. }

  1749. /* Allocate memory and initialize a new root variable.  */
  1750. static struct varobj *
  1751. new_root_variable (void)
  1752. {
  1753.   struct varobj *var = new_variable ();

  1754.   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
  1755.   var->root->lang_ops = NULL;
  1756.   var->root->exp = NULL;
  1757.   var->root->valid_block = NULL;
  1758.   var->root->frame = null_frame_id;
  1759.   var->root->floating = 0;
  1760.   var->root->rootvar = NULL;
  1761.   var->root->is_valid = 1;

  1762.   return var;
  1763. }

  1764. /* Free any allocated memory associated with VAR.  */
  1765. static void
  1766. free_variable (struct varobj *var)
  1767. {
  1768. #if HAVE_PYTHON
  1769.   if (var->dynamic->pretty_printer != NULL)
  1770.     {
  1771.       struct cleanup *cleanup = varobj_ensure_python_env (var);

  1772.       Py_XDECREF (var->dynamic->constructor);
  1773.       Py_XDECREF (var->dynamic->pretty_printer);
  1774.       do_cleanups (cleanup);
  1775.     }
  1776. #endif

  1777.   varobj_iter_delete (var->dynamic->child_iter);
  1778.   varobj_clear_saved_item (var->dynamic);
  1779.   value_free (var->value);

  1780.   /* Free the expression if this is a root variable.  */
  1781.   if (is_root_p (var))
  1782.     {
  1783.       xfree (var->root->exp);
  1784.       xfree (var->root);
  1785.     }

  1786.   xfree (var->name);
  1787.   xfree (var->obj_name);
  1788.   xfree (var->print_value);
  1789.   xfree (var->path_expr);
  1790.   xfree (var->dynamic);
  1791.   xfree (var);
  1792. }

  1793. static void
  1794. do_free_variable_cleanup (void *var)
  1795. {
  1796.   free_variable (var);
  1797. }

  1798. static struct cleanup *
  1799. make_cleanup_free_variable (struct varobj *var)
  1800. {
  1801.   return make_cleanup (do_free_variable_cleanup, var);
  1802. }

  1803. /* Return the type of the value that's stored in VAR,
  1804.    or that would have being stored there if the
  1805.    value were accessible.

  1806.    This differs from VAR->type in that VAR->type is always
  1807.    the true type of the expession in the source language.
  1808.    The return value of this function is the type we're
  1809.    actually storing in varobj, and using for displaying
  1810.    the values and for comparing previous and new values.

  1811.    For example, top-level references are always stripped.  */
  1812. struct type *
  1813. varobj_get_value_type (struct varobj *var)
  1814. {
  1815.   struct type *type;

  1816.   if (var->value)
  1817.     type = value_type (var->value);
  1818.   else
  1819.     type = var->type;

  1820.   type = check_typedef (type);

  1821.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  1822.     type = get_target_type (type);

  1823.   type = check_typedef (type);

  1824.   return type;
  1825. }

  1826. /* What is the default display for this variable? We assume that
  1827.    everything is "natural".  Any exceptions?  */
  1828. static enum varobj_display_formats
  1829. variable_default_display (struct varobj *var)
  1830. {
  1831.   return FORMAT_NATURAL;
  1832. }

  1833. /* FIXME: The following should be generic for any pointer.  */
  1834. static void
  1835. cppush (struct cpstack **pstack, char *name)
  1836. {
  1837.   struct cpstack *s;

  1838.   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
  1839.   s->name = name;
  1840.   s->next = *pstack;
  1841.   *pstack = s;
  1842. }

  1843. /* FIXME: The following should be generic for any pointer.  */
  1844. static char *
  1845. cppop (struct cpstack **pstack)
  1846. {
  1847.   struct cpstack *s;
  1848.   char *v;

  1849.   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
  1850.     return NULL;

  1851.   s = *pstack;
  1852.   v = s->name;
  1853.   *pstack = (*pstack)->next;
  1854.   xfree (s);

  1855.   return v;
  1856. }

  1857. /*
  1858. * Language-dependencies
  1859. */

  1860. /* Common entry points */

  1861. /* Return the number of children for a given variable.
  1862.    The result of this function is defined by the language
  1863.    implementation.  The number of children returned by this function
  1864.    is the number of children that the user will see in the variable
  1865.    display.  */
  1866. static int
  1867. number_of_children (struct varobj *var)
  1868. {
  1869.   return (*var->root->lang_ops->number_of_children) (var);
  1870. }

  1871. /* What is the expression for the root varobj VAR? Returns a malloc'd
  1872.    string.  */
  1873. static char *
  1874. name_of_variable (struct varobj *var)
  1875. {
  1876.   return (*var->root->lang_ops->name_of_variable) (var);
  1877. }

  1878. /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
  1879.    string.  */
  1880. static char *
  1881. name_of_child (struct varobj *var, int index)
  1882. {
  1883.   return (*var->root->lang_ops->name_of_child) (var, index);
  1884. }

  1885. /* If frame associated with VAR can be found, switch
  1886.    to it and return 1.  Otherwise, return 0.  */

  1887. static int
  1888. check_scope (struct varobj *var)
  1889. {
  1890.   struct frame_info *fi;
  1891.   int scope;

  1892.   fi = frame_find_by_id (var->root->frame);
  1893.   scope = fi != NULL;

  1894.   if (fi)
  1895.     {
  1896.       CORE_ADDR pc = get_frame_pc (fi);

  1897.       if (pc <  BLOCK_START (var->root->valid_block) ||
  1898.           pc >= BLOCK_END (var->root->valid_block))
  1899.         scope = 0;
  1900.       else
  1901.         select_frame (fi);
  1902.     }
  1903.   return scope;
  1904. }

  1905. /* Helper function to value_of_root.  */

  1906. static struct value *
  1907. value_of_root_1 (struct varobj **var_handle)
  1908. {
  1909.   struct value *new_val = NULL;
  1910.   struct varobj *var = *var_handle;
  1911.   int within_scope = 0;
  1912.   struct cleanup *back_to;

  1913.   /*  Only root variables can be updated...  */
  1914.   if (!is_root_p (var))
  1915.     /* Not a root var.  */
  1916.     return NULL;

  1917.   back_to = make_cleanup_restore_current_thread ();

  1918.   /* Determine whether the variable is still around.  */
  1919.   if (var->root->valid_block == NULL || var->root->floating)
  1920.     within_scope = 1;
  1921.   else if (var->root->thread_id == 0)
  1922.     {
  1923.       /* The program was single-threaded when the variable object was
  1924.          created.  Technically, it's possible that the program became
  1925.          multi-threaded since then, but we don't support such
  1926.          scenario yet.  */
  1927.       within_scope = check_scope (var);
  1928.     }
  1929.   else
  1930.     {
  1931.       ptid_t ptid = thread_id_to_pid (var->root->thread_id);
  1932.       if (in_thread_list (ptid))
  1933.         {
  1934.           switch_to_thread (ptid);
  1935.           within_scope = check_scope (var);
  1936.         }
  1937.     }

  1938.   if (within_scope)
  1939.     {
  1940.       volatile struct gdb_exception except;

  1941.       /* We need to catch errors here, because if evaluate
  1942.          expression fails we want to just return NULL.  */
  1943.       TRY_CATCH (except, RETURN_MASK_ERROR)
  1944.         {
  1945.           new_val = evaluate_expression (var->root->exp);
  1946.         }
  1947.     }

  1948.   do_cleanups (back_to);

  1949.   return new_val;
  1950. }

  1951. /* What is the ``struct value *'' of the root variable VAR?
  1952.    For floating variable object, evaluation can get us a value
  1953.    of different type from what is stored in varobj already.  In
  1954.    that case:
  1955.    - *type_changed will be set to 1
  1956.    - old varobj will be freed, and new one will be
  1957.    created, with the same name.
  1958.    - *var_handle will be set to the new varobj
  1959.    Otherwise, *type_changed will be set to 0.  */
  1960. static struct value *
  1961. value_of_root (struct varobj **var_handle, int *type_changed)
  1962. {
  1963.   struct varobj *var;

  1964.   if (var_handle == NULL)
  1965.     return NULL;

  1966.   var = *var_handle;

  1967.   /* This should really be an exception, since this should
  1968.      only get called with a root variable.  */

  1969.   if (!is_root_p (var))
  1970.     return NULL;

  1971.   if (var->root->floating)
  1972.     {
  1973.       struct varobj *tmp_var;
  1974.       char *old_type, *new_type;

  1975.       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
  1976.                                USE_SELECTED_FRAME);
  1977.       if (tmp_var == NULL)
  1978.         {
  1979.           return NULL;
  1980.         }
  1981.       old_type = varobj_get_type (var);
  1982.       new_type = varobj_get_type (tmp_var);
  1983.       if (strcmp (old_type, new_type) == 0)
  1984.         {
  1985.           /* The expression presently stored inside var->root->exp
  1986.              remembers the locations of local variables relatively to
  1987.              the frame where the expression was created (in DWARF location
  1988.              button, for example).  Naturally, those locations are not
  1989.              correct in other frames, so update the expression.  */

  1990.          struct expression *tmp_exp = var->root->exp;

  1991.          var->root->exp = tmp_var->root->exp;
  1992.          tmp_var->root->exp = tmp_exp;

  1993.           varobj_delete (tmp_var, NULL, 0);
  1994.           *type_changed = 0;
  1995.         }
  1996.       else
  1997.         {
  1998.           tmp_var->obj_name = xstrdup (var->obj_name);
  1999.           tmp_var->from = var->from;
  2000.           tmp_var->to = var->to;
  2001.           varobj_delete (var, NULL, 0);

  2002.           install_variable (tmp_var);
  2003.           *var_handle = tmp_var;
  2004.           var = *var_handle;
  2005.           *type_changed = 1;
  2006.         }
  2007.       xfree (old_type);
  2008.       xfree (new_type);
  2009.     }
  2010.   else
  2011.     {
  2012.       *type_changed = 0;
  2013.     }

  2014.   {
  2015.     struct value *value;

  2016.     value = value_of_root_1 (var_handle);
  2017.     if (var->value == NULL || value == NULL)
  2018.       {
  2019.         /* For root varobj-s, a NULL value indicates a scoping issue.
  2020.            So, nothing to do in terms of checking for mutations.  */
  2021.       }
  2022.     else if (varobj_value_has_mutated (var, value, value_type (value)))
  2023.       {
  2024.         /* The type has mutated, so the children are no longer valid.
  2025.            Just delete them, and tell our caller that the type has
  2026.            changed.  */
  2027.         varobj_delete (var, NULL, 1 /* only_children */);
  2028.         var->num_children = -1;
  2029.         var->to = -1;
  2030.         var->from = -1;
  2031.         *type_changed = 1;
  2032.       }
  2033.     return value;
  2034.   }
  2035. }

  2036. /* What is the ``struct value *'' for the INDEX'th child of PARENT?  */
  2037. static struct value *
  2038. value_of_child (struct varobj *parent, int index)
  2039. {
  2040.   struct value *value;

  2041.   value = (*parent->root->lang_ops->value_of_child) (parent, index);

  2042.   return value;
  2043. }

  2044. /* GDB already has a command called "value_of_variable".  Sigh.  */
  2045. static char *
  2046. my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
  2047. {
  2048.   if (var->root->is_valid)
  2049.     {
  2050.       if (var->dynamic->pretty_printer != NULL)
  2051.         return varobj_value_get_print_value (var->value, var->format, var);
  2052.       return (*var->root->lang_ops->value_of_variable) (var, format);
  2053.     }
  2054.   else
  2055.     return NULL;
  2056. }

  2057. void
  2058. varobj_formatted_print_options (struct value_print_options *opts,
  2059.                                 enum varobj_display_formats format)
  2060. {
  2061.   get_formatted_print_options (opts, format_code[(int) format]);
  2062.   opts->deref_ref = 0;
  2063.   opts->raw = 1;
  2064. }

  2065. char *
  2066. varobj_value_get_print_value (struct value *value,
  2067.                               enum varobj_display_formats format,
  2068.                               struct varobj *var)
  2069. {
  2070.   struct ui_file *stb;
  2071.   struct cleanup *old_chain;
  2072.   char *thevalue = NULL;
  2073.   struct value_print_options opts;
  2074.   struct type *type = NULL;
  2075.   long len = 0;
  2076.   char *encoding = NULL;
  2077.   struct gdbarch *gdbarch = NULL;
  2078.   /* Initialize it just to avoid a GCC false warning.  */
  2079.   CORE_ADDR str_addr = 0;
  2080.   int string_print = 0;

  2081.   if (value == NULL)
  2082.     return NULL;

  2083.   stb = mem_fileopen ();
  2084.   old_chain = make_cleanup_ui_file_delete (stb);

  2085.   gdbarch = get_type_arch (value_type (value));
  2086. #if HAVE_PYTHON
  2087.   if (gdb_python_initialized)
  2088.     {
  2089.       PyObject *value_formatter =  var->dynamic->pretty_printer;

  2090.       varobj_ensure_python_env (var);

  2091.       if (value_formatter)
  2092.         {
  2093.           /* First check to see if we have any children at all.  If so,
  2094.              we simply return {...}.  */
  2095.           if (dynamic_varobj_has_child_method (var))
  2096.             {
  2097.               do_cleanups (old_chain);
  2098.               return xstrdup ("{...}");
  2099.             }

  2100.           if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
  2101.             {
  2102.               struct value *replacement;
  2103.               PyObject *output = NULL;

  2104.               output = apply_varobj_pretty_printer (value_formatter,
  2105.                                                     &replacement,
  2106.                                                     stb);

  2107.               /* If we have string like output ...  */
  2108.               if (output)
  2109.                 {
  2110.                   make_cleanup_py_decref (output);

  2111.                   /* If this is a lazy string, extract it.  For lazy
  2112.                      strings we always print as a string, so set
  2113.                      string_print.  */
  2114.                   if (gdbpy_is_lazy_string (output))
  2115.                     {
  2116.                       gdbpy_extract_lazy_string (output, &str_addr, &type,
  2117.                                                  &len, &encoding);
  2118.                       make_cleanup (free_current_contents, &encoding);
  2119.                       string_print = 1;
  2120.                     }
  2121.                   else
  2122.                     {
  2123.                       /* If it is a regular (non-lazy) string, extract
  2124.                          it and copy the contents into THEVALUE.  If the
  2125.                          hint says to print it as a string, set
  2126.                          string_print.  Otherwise just return the extracted
  2127.                          string as a value.  */

  2128.                       char *s = python_string_to_target_string (output);

  2129.                       if (s)
  2130.                         {
  2131.                           char *hint;

  2132.                           hint = gdbpy_get_display_hint (value_formatter);
  2133.                           if (hint)
  2134.                             {
  2135.                               if (!strcmp (hint, "string"))
  2136.                                 string_print = 1;
  2137.                               xfree (hint);
  2138.                             }

  2139.                           len = strlen (s);
  2140.                           thevalue = xmemdup (s, len + 1, len + 1);
  2141.                           type = builtin_type (gdbarch)->builtin_char;
  2142.                           xfree (s);

  2143.                           if (!string_print)
  2144.                             {
  2145.                               do_cleanups (old_chain);
  2146.                               return thevalue;
  2147.                             }

  2148.                           make_cleanup (xfree, thevalue);
  2149.                         }
  2150.                       else
  2151.                         gdbpy_print_stack ();
  2152.                     }
  2153.                 }
  2154.               /* If the printer returned a replacement value, set VALUE
  2155.                  to REPLACEMENT.  If there is not a replacement value,
  2156.                  just use the value passed to this function.  */
  2157.               if (replacement)
  2158.                 value = replacement;
  2159.             }
  2160.         }
  2161.     }
  2162. #endif

  2163.   varobj_formatted_print_options (&opts, format);

  2164.   /* If the THEVALUE has contents, it is a regular string.  */
  2165.   if (thevalue)
  2166.     LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
  2167.   else if (string_print)
  2168.     /* Otherwise, if string_print is set, and it is not a regular
  2169.        string, it is a lazy string.  */
  2170.     val_print_string (type, encoding, str_addr, len, stb, &opts);
  2171.   else
  2172.     /* All other cases.  */
  2173.     common_val_print (value, stb, 0, &opts, current_language);

  2174.   thevalue = ui_file_xstrdup (stb, NULL);

  2175.   do_cleanups (old_chain);
  2176.   return thevalue;
  2177. }

  2178. int
  2179. varobj_editable_p (struct varobj *var)
  2180. {
  2181.   struct type *type;

  2182.   if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
  2183.     return 0;

  2184.   type = varobj_get_value_type (var);

  2185.   switch (TYPE_CODE (type))
  2186.     {
  2187.     case TYPE_CODE_STRUCT:
  2188.     case TYPE_CODE_UNION:
  2189.     case TYPE_CODE_ARRAY:
  2190.     case TYPE_CODE_FUNC:
  2191.     case TYPE_CODE_METHOD:
  2192.       return 0;
  2193.       break;

  2194.     default:
  2195.       return 1;
  2196.       break;
  2197.     }
  2198. }

  2199. /* Call VAR's value_is_changeable_p language-specific callback.  */

  2200. int
  2201. varobj_value_is_changeable_p (struct varobj *var)
  2202. {
  2203.   return var->root->lang_ops->value_is_changeable_p (var);
  2204. }

  2205. /* Return 1 if that varobj is floating, that is is always evaluated in the
  2206.    selected frame, and not bound to thread/frame.  Such variable objects
  2207.    are created using '@' as frame specifier to -var-create.  */
  2208. int
  2209. varobj_floating_p (struct varobj *var)
  2210. {
  2211.   return var->root->floating;
  2212. }

  2213. /* Implement the "value_is_changeable_p" varobj callback for most
  2214.    languages.  */

  2215. int
  2216. varobj_default_value_is_changeable_p (struct varobj *var)
  2217. {
  2218.   int r;
  2219.   struct type *type;

  2220.   if (CPLUS_FAKE_CHILD (var))
  2221.     return 0;

  2222.   type = varobj_get_value_type (var);

  2223.   switch (TYPE_CODE (type))
  2224.     {
  2225.     case TYPE_CODE_STRUCT:
  2226.     case TYPE_CODE_UNION:
  2227.     case TYPE_CODE_ARRAY:
  2228.       r = 0;
  2229.       break;

  2230.     default:
  2231.       r = 1;
  2232.     }

  2233.   return r;
  2234. }

  2235. /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
  2236.    with an arbitrary caller supplied DATA pointer.  */

  2237. void
  2238. all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
  2239. {
  2240.   struct varobj_root *var_root, *var_root_next;

  2241.   /* Iterate "safely" - handle if the callee deletes its passed VAROBJ.  */

  2242.   for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
  2243.     {
  2244.       var_root_next = var_root->next;

  2245.       (*func) (var_root->rootvar, data);
  2246.     }
  2247. }

  2248. /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
  2249.    defined on globals.  It is a helper for varobj_invalidate.

  2250.    This function is called after changing the symbol file, in this case the
  2251.    pointers to "struct type" stored by the varobj are no longer valid.  All
  2252.    varobj must be either re-evaluated, or marked as invalid here.  */

  2253. static void
  2254. varobj_invalidate_iter (struct varobj *var, void *unused)
  2255. {
  2256.   /* global and floating var must be re-evaluated.  */
  2257.   if (var->root->floating || var->root->valid_block == NULL)
  2258.     {
  2259.       struct varobj *tmp_var;

  2260.       /* Try to create a varobj with same expression.  If we succeed
  2261.          replace the old varobj, otherwise invalidate it.  */
  2262.       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
  2263.                                USE_CURRENT_FRAME);
  2264.       if (tmp_var != NULL)
  2265.         {
  2266.           tmp_var->obj_name = xstrdup (var->obj_name);
  2267.           varobj_delete (var, NULL, 0);
  2268.           install_variable (tmp_var);
  2269.         }
  2270.       else
  2271.         var->root->is_valid = 0;
  2272.     }
  2273.   else /* locals must be invalidated.  */
  2274.     var->root->is_valid = 0;
  2275. }

  2276. /* Invalidate the varobjs that are tied to locals and re-create the ones that
  2277.    are defined on globals.
  2278.    Invalidated varobjs will be always printed in_scope="invalid".  */

  2279. void
  2280. varobj_invalidate (void)
  2281. {
  2282.   all_root_varobjs (varobj_invalidate_iter, NULL);
  2283. }

  2284. extern void _initialize_varobj (void);
  2285. void
  2286. _initialize_varobj (void)
  2287. {
  2288.   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;

  2289.   varobj_table = xmalloc (sizeof_table);
  2290.   memset (varobj_table, 0, sizeof_table);

  2291.   add_setshow_zuinteger_cmd ("varobj", class_maintenance,
  2292.                              &varobjdebug,
  2293.                              _("Set varobj debugging."),
  2294.                              _("Show varobj debugging."),
  2295.                              _("When non-zero, varobj debugging is enabled."),
  2296.                              NULL, show_varobjdebug,
  2297.                              &setdebuglist, &showdebuglist);
  2298. }