Home | History | Annotate | Line # | Download | only in mi
mi-cmd-var.c revision 1.6.4.1
      1      1.1  christos /* MI Command Set - varobj commands.
      2  1.6.4.1  christos    Copyright (C) 2000-2017 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    Contributed by Cygnus Solutions (a Red Hat company).
      5      1.1  christos 
      6      1.1  christos    This file is part of GDB.
      7      1.1  christos 
      8      1.1  christos    This program is free software; you can redistribute it and/or modify
      9      1.1  christos    it under the terms of the GNU General Public License as published by
     10      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11      1.1  christos    (at your option) any later version.
     12      1.1  christos 
     13      1.1  christos    This program is distributed in the hope that it will be useful,
     14      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16      1.1  christos    GNU General Public License for more details.
     17      1.1  christos 
     18      1.1  christos    You should have received a copy of the GNU General Public License
     19      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20      1.1  christos 
     21      1.1  christos #include "defs.h"
     22      1.1  christos #include "mi-cmds.h"
     23      1.1  christos #include "mi-main.h"
     24      1.1  christos #include "ui-out.h"
     25      1.1  christos #include "mi-out.h"
     26      1.1  christos #include "varobj.h"
     27      1.1  christos #include "language.h"
     28      1.1  christos #include "value.h"
     29      1.1  christos #include <ctype.h>
     30      1.1  christos #include "mi-getopt.h"
     31      1.1  christos #include "gdbthread.h"
     32      1.1  christos #include "mi-parse.h"
     33      1.1  christos 
     34      1.1  christos extern unsigned int varobjdebug;		/* defined in varobj.c.  */
     35      1.1  christos 
     36      1.1  christos static void varobj_update_one (struct varobj *var,
     37      1.1  christos 			       enum print_values print_values,
     38      1.5  christos 			       int is_explicit);
     39      1.1  christos 
     40      1.1  christos static int mi_print_value_p (struct varobj *var,
     41      1.1  christos 			     enum print_values print_values);
     42      1.1  christos 
     43      1.1  christos /* Print variable object VAR.  The PRINT_VALUES parameter controls
     44      1.1  christos    if the value should be printed.  The PRINT_EXPRESSION parameter
     45      1.1  christos    controls if the expression should be printed.  */
     46      1.1  christos 
     47      1.1  christos static void
     48      1.1  christos print_varobj (struct varobj *var, enum print_values print_values,
     49      1.1  christos 	      int print_expression)
     50      1.1  christos {
     51      1.1  christos   struct ui_out *uiout = current_uiout;
     52      1.1  christos   int thread_id;
     53      1.1  christos 
     54  1.6.4.1  christos   uiout->field_string ("name", varobj_get_objname (var));
     55      1.1  christos   if (print_expression)
     56      1.5  christos     {
     57  1.6.4.1  christos       std::string exp = varobj_get_expression (var);
     58      1.5  christos 
     59  1.6.4.1  christos       uiout->field_string ("exp", exp.c_str ());
     60      1.5  christos     }
     61  1.6.4.1  christos   uiout->field_int ("numchild", varobj_get_num_children (var));
     62      1.1  christos 
     63      1.1  christos   if (mi_print_value_p (var, print_values))
     64      1.1  christos     {
     65  1.6.4.1  christos       std::string val = varobj_get_value (var);
     66      1.1  christos 
     67  1.6.4.1  christos       uiout->field_string ("value", val.c_str ());
     68      1.1  christos     }
     69      1.1  christos 
     70  1.6.4.1  christos   std::string type = varobj_get_type (var);
     71  1.6.4.1  christos   if (!type.empty ())
     72  1.6.4.1  christos     uiout->field_string ("type", type.c_str ());
     73      1.1  christos 
     74      1.1  christos   thread_id = varobj_get_thread_id (var);
     75      1.1  christos   if (thread_id > 0)
     76  1.6.4.1  christos     uiout->field_int ("thread-id", thread_id);
     77      1.1  christos 
     78      1.1  christos   if (varobj_get_frozen (var))
     79  1.6.4.1  christos     uiout->field_int ("frozen", 1);
     80      1.1  christos 
     81  1.6.4.1  christos   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
     82      1.1  christos   if (display_hint)
     83  1.6.4.1  christos     uiout->field_string ("displayhint", display_hint.get ());
     84      1.1  christos 
     85      1.3  christos   if (varobj_is_dynamic_p (var))
     86  1.6.4.1  christos     uiout->field_int ("dynamic", 1);
     87      1.1  christos }
     88      1.1  christos 
     89      1.1  christos /* VAROBJ operations */
     90      1.1  christos 
     91      1.1  christos void
     92  1.6.4.1  christos mi_cmd_var_create (const char *command, char **argv, int argc)
     93      1.1  christos {
     94      1.1  christos   struct ui_out *uiout = current_uiout;
     95      1.1  christos   CORE_ADDR frameaddr = 0;
     96      1.1  christos   struct varobj *var;
     97      1.1  christos   char *name;
     98      1.1  christos   char *frame;
     99      1.1  christos   char *expr;
    100      1.1  christos   struct cleanup *old_cleanups;
    101      1.1  christos   enum varobj_type var_type;
    102      1.1  christos 
    103      1.1  christos   if (argc != 3)
    104      1.1  christos     error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
    105      1.1  christos 
    106      1.1  christos   name = xstrdup (argv[0]);
    107      1.1  christos   /* Add cleanup for name. Must be free_current_contents as name can
    108      1.1  christos      be reallocated.  */
    109      1.1  christos   old_cleanups = make_cleanup (free_current_contents, &name);
    110      1.1  christos 
    111      1.1  christos   frame = xstrdup (argv[1]);
    112      1.1  christos   make_cleanup (xfree, frame);
    113      1.1  christos 
    114      1.1  christos   expr = xstrdup (argv[2]);
    115      1.1  christos   make_cleanup (xfree, expr);
    116      1.1  christos 
    117      1.1  christos   if (strcmp (name, "-") == 0)
    118      1.1  christos     {
    119      1.1  christos       xfree (name);
    120      1.1  christos       name = varobj_gen_name ();
    121      1.1  christos     }
    122      1.1  christos   else if (!isalpha (*name))
    123      1.1  christos     error (_("-var-create: name of object must begin with a letter"));
    124      1.1  christos 
    125      1.1  christos   if (strcmp (frame, "*") == 0)
    126      1.1  christos     var_type = USE_CURRENT_FRAME;
    127      1.1  christos   else if (strcmp (frame, "@") == 0)
    128      1.1  christos     var_type = USE_SELECTED_FRAME;
    129      1.1  christos   else
    130      1.1  christos     {
    131      1.1  christos       var_type = USE_SPECIFIED_FRAME;
    132      1.1  christos       frameaddr = string_to_core_addr (frame);
    133      1.1  christos     }
    134      1.1  christos 
    135      1.1  christos   if (varobjdebug)
    136      1.1  christos     fprintf_unfiltered (gdb_stdlog,
    137      1.1  christos 		    "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
    138      1.1  christos 			name, frame, hex_string (frameaddr), expr);
    139      1.1  christos 
    140      1.1  christos   var = varobj_create (name, expr, frameaddr, var_type);
    141      1.1  christos 
    142      1.1  christos   if (var == NULL)
    143      1.1  christos     error (_("-var-create: unable to create variable object"));
    144      1.1  christos 
    145      1.1  christos   print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
    146      1.1  christos 
    147  1.6.4.1  christos   uiout->field_int ("has_more", varobj_has_more (var, 0));
    148      1.1  christos 
    149      1.1  christos   do_cleanups (old_cleanups);
    150      1.1  christos }
    151      1.1  christos 
    152      1.1  christos void
    153  1.6.4.1  christos mi_cmd_var_delete (const char *command, char **argv, int argc)
    154      1.1  christos {
    155      1.1  christos   char *name;
    156      1.1  christos   struct varobj *var;
    157      1.1  christos   int numdel;
    158      1.1  christos   int children_only_p = 0;
    159      1.1  christos   struct cleanup *old_cleanups;
    160      1.1  christos   struct ui_out *uiout = current_uiout;
    161      1.1  christos 
    162      1.1  christos   if (argc < 1 || argc > 2)
    163      1.1  christos     error (_("-var-delete: Usage: [-c] EXPRESSION."));
    164      1.1  christos 
    165      1.1  christos   name = xstrdup (argv[0]);
    166      1.1  christos   /* Add cleanup for name. Must be free_current_contents as name can
    167      1.1  christos      be reallocated.  */
    168      1.1  christos   old_cleanups = make_cleanup (free_current_contents, &name);
    169      1.1  christos 
    170      1.1  christos   /* If we have one single argument it cannot be '-c' or any string
    171      1.1  christos      starting with '-'.  */
    172      1.1  christos   if (argc == 1)
    173      1.1  christos     {
    174      1.1  christos       if (strcmp (name, "-c") == 0)
    175      1.1  christos 	error (_("-var-delete: Missing required "
    176      1.1  christos 		 "argument after '-c': variable object name"));
    177      1.1  christos       if (*name == '-')
    178      1.1  christos 	error (_("-var-delete: Illegal variable object name"));
    179      1.1  christos     }
    180      1.1  christos 
    181      1.1  christos   /* If we have 2 arguments they must be '-c' followed by a string
    182      1.1  christos      which would be the variable name.  */
    183      1.1  christos   if (argc == 2)
    184      1.1  christos     {
    185      1.1  christos       if (strcmp (name, "-c") != 0)
    186      1.1  christos 	error (_("-var-delete: Invalid option."));
    187      1.1  christos       children_only_p = 1;
    188      1.1  christos       do_cleanups (old_cleanups);
    189      1.1  christos       name = xstrdup (argv[1]);
    190      1.1  christos       old_cleanups = make_cleanup (free_current_contents, &name);
    191      1.1  christos     }
    192      1.1  christos 
    193      1.1  christos   /* If we didn't error out, now NAME contains the name of the
    194      1.1  christos      variable.  */
    195      1.1  christos 
    196      1.1  christos   var = varobj_get_handle (name);
    197      1.1  christos 
    198      1.6  christos   numdel = varobj_delete (var, children_only_p);
    199      1.1  christos 
    200  1.6.4.1  christos   uiout->field_int ("ndeleted", numdel);
    201      1.1  christos 
    202      1.1  christos   do_cleanups (old_cleanups);
    203      1.1  christos }
    204      1.1  christos 
    205      1.1  christos /* Parse a string argument into a format value.  */
    206      1.1  christos 
    207      1.1  christos static enum varobj_display_formats
    208      1.1  christos mi_parse_format (const char *arg)
    209      1.1  christos {
    210      1.1  christos   if (arg != NULL)
    211      1.1  christos     {
    212      1.1  christos       int len;
    213      1.1  christos 
    214      1.1  christos       len = strlen (arg);
    215      1.1  christos 
    216      1.1  christos       if (strncmp (arg, "natural", len) == 0)
    217      1.1  christos 	return FORMAT_NATURAL;
    218      1.1  christos       else if (strncmp (arg, "binary", len) == 0)
    219      1.1  christos 	return FORMAT_BINARY;
    220      1.1  christos       else if (strncmp (arg, "decimal", len) == 0)
    221      1.1  christos 	return FORMAT_DECIMAL;
    222      1.1  christos       else if (strncmp (arg, "hexadecimal", len) == 0)
    223      1.1  christos 	return FORMAT_HEXADECIMAL;
    224      1.1  christos       else if (strncmp (arg, "octal", len) == 0)
    225      1.1  christos 	return FORMAT_OCTAL;
    226      1.6  christos       else if (strncmp (arg, "zero-hexadecimal", len) == 0)
    227      1.6  christos 	return FORMAT_ZHEXADECIMAL;
    228      1.1  christos     }
    229      1.1  christos 
    230      1.1  christos   error (_("Must specify the format as: \"natural\", "
    231      1.6  christos 	   "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
    232      1.1  christos }
    233      1.1  christos 
    234      1.1  christos void
    235  1.6.4.1  christos mi_cmd_var_set_format (const char *command, char **argv, int argc)
    236      1.1  christos {
    237      1.1  christos   enum varobj_display_formats format;
    238      1.1  christos   struct varobj *var;
    239      1.1  christos   struct ui_out *uiout = current_uiout;
    240      1.1  christos 
    241      1.1  christos   if (argc != 2)
    242      1.1  christos     error (_("-var-set-format: Usage: NAME FORMAT."));
    243      1.1  christos 
    244      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    245      1.1  christos   var = varobj_get_handle (argv[0]);
    246      1.1  christos 
    247      1.1  christos   format = mi_parse_format (argv[1]);
    248      1.1  christos 
    249      1.1  christos   /* Set the format of VAR to the given format.  */
    250      1.1  christos   varobj_set_display_format (var, format);
    251      1.1  christos 
    252      1.1  christos   /* Report the new current format.  */
    253  1.6.4.1  christos   uiout->field_string ("format", varobj_format_string[(int) format]);
    254      1.1  christos 
    255      1.1  christos   /* Report the value in the new format.  */
    256  1.6.4.1  christos   std::string val = varobj_get_value (var);
    257  1.6.4.1  christos   uiout->field_string ("value", val.c_str ());
    258      1.1  christos }
    259      1.1  christos 
    260      1.1  christos void
    261  1.6.4.1  christos mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
    262      1.1  christos {
    263      1.1  christos   struct varobj *var;
    264      1.1  christos 
    265      1.1  christos   if (argc != 2)
    266      1.1  christos     error (_("Usage: NAME VISUALIZER_FUNCTION."));
    267      1.1  christos 
    268      1.1  christos   var = varobj_get_handle (argv[0]);
    269      1.1  christos 
    270      1.1  christos   if (var == NULL)
    271      1.1  christos     error (_("Variable object not found"));
    272      1.1  christos 
    273      1.1  christos   varobj_set_visualizer (var, argv[1]);
    274      1.1  christos }
    275      1.1  christos 
    276      1.1  christos void
    277  1.6.4.1  christos mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
    278      1.1  christos {
    279      1.1  christos   struct varobj *var;
    280      1.1  christos   int frozen;
    281      1.1  christos 
    282      1.1  christos   if (argc != 2)
    283      1.1  christos     error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
    284      1.1  christos 
    285      1.1  christos   var = varobj_get_handle (argv[0]);
    286      1.1  christos 
    287      1.1  christos   if (strcmp (argv[1], "0") == 0)
    288      1.1  christos     frozen = 0;
    289      1.1  christos   else if (strcmp (argv[1], "1") == 0)
    290      1.1  christos     frozen = 1;
    291      1.1  christos   else
    292      1.1  christos     error (_("Invalid flag value"));
    293      1.1  christos 
    294      1.1  christos   varobj_set_frozen (var, frozen);
    295      1.1  christos 
    296      1.1  christos   /* We don't automatically return the new value, or what varobjs got
    297      1.1  christos      new values during unfreezing.  If this information is required,
    298      1.1  christos      client should call -var-update explicitly.  */
    299      1.1  christos }
    300      1.1  christos 
    301      1.1  christos void
    302  1.6.4.1  christos mi_cmd_var_show_format (const char *command, char **argv, int argc)
    303      1.1  christos {
    304      1.1  christos   struct ui_out *uiout = current_uiout;
    305      1.1  christos   enum varobj_display_formats format;
    306      1.1  christos   struct varobj *var;
    307      1.1  christos 
    308      1.1  christos   if (argc != 1)
    309      1.1  christos     error (_("-var-show-format: Usage: NAME."));
    310      1.1  christos 
    311      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    312      1.1  christos   var = varobj_get_handle (argv[0]);
    313      1.1  christos 
    314      1.1  christos   format = varobj_get_display_format (var);
    315      1.1  christos 
    316      1.1  christos   /* Report the current format.  */
    317  1.6.4.1  christos   uiout->field_string ("format", varobj_format_string[(int) format]);
    318      1.1  christos }
    319      1.1  christos 
    320      1.1  christos void
    321  1.6.4.1  christos mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
    322      1.1  christos {
    323      1.1  christos   struct ui_out *uiout = current_uiout;
    324      1.1  christos   struct varobj *var;
    325      1.1  christos 
    326      1.1  christos   if (argc != 1)
    327      1.1  christos     error (_("-var-info-num-children: Usage: NAME."));
    328      1.1  christos 
    329      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    330      1.1  christos   var = varobj_get_handle (argv[0]);
    331      1.1  christos 
    332  1.6.4.1  christos   uiout->field_int ("numchild", varobj_get_num_children (var));
    333      1.1  christos }
    334      1.1  christos 
    335      1.1  christos /* Return 1 if given the argument PRINT_VALUES we should display
    336      1.1  christos    the varobj VAR.  */
    337      1.1  christos 
    338      1.1  christos static int
    339      1.1  christos mi_print_value_p (struct varobj *var, enum print_values print_values)
    340      1.1  christos {
    341      1.1  christos   struct type *type;
    342      1.1  christos 
    343      1.1  christos   if (print_values == PRINT_NO_VALUES)
    344      1.1  christos     return 0;
    345      1.1  christos 
    346      1.1  christos   if (print_values == PRINT_ALL_VALUES)
    347      1.1  christos     return 1;
    348      1.1  christos 
    349      1.3  christos   if (varobj_is_dynamic_p (var))
    350      1.1  christos     return 1;
    351      1.1  christos 
    352      1.1  christos   type = varobj_get_gdb_type (var);
    353      1.1  christos   if (type == NULL)
    354      1.1  christos     return 1;
    355      1.1  christos   else
    356      1.1  christos     {
    357      1.1  christos       type = check_typedef (type);
    358      1.1  christos 
    359      1.1  christos       /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
    360      1.1  christos 	 and that type is not a compound type.  */
    361      1.1  christos       return (TYPE_CODE (type) != TYPE_CODE_ARRAY
    362      1.1  christos 	      && TYPE_CODE (type) != TYPE_CODE_STRUCT
    363      1.1  christos 	      && TYPE_CODE (type) != TYPE_CODE_UNION);
    364      1.1  christos     }
    365      1.1  christos }
    366      1.1  christos 
    367      1.1  christos void
    368  1.6.4.1  christos mi_cmd_var_list_children (const char *command, char **argv, int argc)
    369      1.1  christos {
    370      1.1  christos   struct ui_out *uiout = current_uiout;
    371      1.1  christos   struct varobj *var;
    372      1.1  christos   VEC(varobj_p) *children;
    373      1.1  christos   struct varobj *child;
    374      1.1  christos   enum print_values print_values;
    375      1.1  christos   int ix;
    376      1.1  christos   int from, to;
    377      1.1  christos 
    378      1.1  christos   if (argc < 1 || argc > 4)
    379      1.1  christos     error (_("-var-list-children: Usage: "
    380      1.1  christos 	     "[PRINT_VALUES] NAME [FROM TO]"));
    381      1.1  christos 
    382      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    383      1.1  christos   if (argc == 1 || argc == 3)
    384      1.1  christos     var = varobj_get_handle (argv[0]);
    385      1.1  christos   else
    386      1.1  christos     var = varobj_get_handle (argv[1]);
    387      1.1  christos 
    388      1.1  christos   if (argc > 2)
    389      1.1  christos     {
    390      1.1  christos       from = atoi (argv[argc - 2]);
    391      1.1  christos       to = atoi (argv[argc - 1]);
    392      1.1  christos     }
    393      1.1  christos   else
    394      1.1  christos     {
    395      1.1  christos       from = -1;
    396      1.1  christos       to = -1;
    397      1.1  christos     }
    398      1.1  christos 
    399      1.1  christos   children = varobj_list_children (var, &from, &to);
    400  1.6.4.1  christos   uiout->field_int ("numchild", to - from);
    401      1.1  christos   if (argc == 2 || argc == 4)
    402      1.1  christos     print_values = mi_parse_print_values (argv[0]);
    403      1.1  christos   else
    404      1.1  christos     print_values = PRINT_NO_VALUES;
    405      1.1  christos 
    406  1.6.4.1  christos   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
    407      1.1  christos   if (display_hint)
    408  1.6.4.1  christos     uiout->field_string ("displayhint", display_hint.get ());
    409      1.1  christos 
    410      1.1  christos   if (from < to)
    411      1.1  christos     {
    412      1.1  christos       struct cleanup *cleanup_children;
    413      1.1  christos 
    414      1.1  christos       if (mi_version (uiout) == 1)
    415      1.1  christos 	cleanup_children
    416      1.1  christos 	  = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
    417      1.1  christos       else
    418      1.1  christos 	cleanup_children
    419      1.1  christos 	  = make_cleanup_ui_out_list_begin_end (uiout, "children");
    420      1.1  christos       for (ix = from;
    421      1.1  christos 	   ix < to && VEC_iterate (varobj_p, children, ix, child);
    422      1.1  christos 	   ++ix)
    423      1.1  christos 	{
    424      1.1  christos 	  struct cleanup *cleanup_child;
    425      1.1  christos 
    426      1.1  christos 	  cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
    427      1.1  christos 	  print_varobj (child, print_values, 1 /* print expression */);
    428      1.1  christos 	  do_cleanups (cleanup_child);
    429      1.1  christos 	}
    430      1.1  christos       do_cleanups (cleanup_children);
    431      1.1  christos     }
    432      1.1  christos 
    433  1.6.4.1  christos   uiout->field_int ("has_more", varobj_has_more (var, to));
    434      1.1  christos }
    435      1.1  christos 
    436      1.1  christos void
    437  1.6.4.1  christos mi_cmd_var_info_type (const char *command, char **argv, int argc)
    438      1.1  christos {
    439      1.1  christos   struct ui_out *uiout = current_uiout;
    440      1.1  christos   struct varobj *var;
    441      1.1  christos 
    442      1.1  christos   if (argc != 1)
    443      1.1  christos     error (_("-var-info-type: Usage: NAME."));
    444      1.1  christos 
    445      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    446      1.1  christos   var = varobj_get_handle (argv[0]);
    447      1.5  christos 
    448  1.6.4.1  christos   std::string type_name = varobj_get_type (var);
    449  1.6.4.1  christos   uiout->field_string ("type", type_name.c_str ());
    450      1.1  christos }
    451      1.1  christos 
    452      1.1  christos void
    453  1.6.4.1  christos mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
    454      1.1  christos {
    455      1.1  christos   struct ui_out *uiout = current_uiout;
    456      1.1  christos   struct varobj *var;
    457      1.1  christos 
    458      1.1  christos   if (argc != 1)
    459      1.1  christos     error (_("Usage: NAME."));
    460      1.1  christos 
    461      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    462      1.1  christos   var = varobj_get_handle (argv[0]);
    463      1.1  christos 
    464  1.6.4.1  christos   const char *path_expr = varobj_get_path_expr (var);
    465      1.1  christos 
    466  1.6.4.1  christos   uiout->field_string ("path_expr", path_expr);
    467      1.1  christos }
    468      1.1  christos 
    469      1.1  christos void
    470  1.6.4.1  christos mi_cmd_var_info_expression (const char *command, char **argv, int argc)
    471      1.1  christos {
    472      1.1  christos   struct ui_out *uiout = current_uiout;
    473      1.1  christos   const struct language_defn *lang;
    474      1.1  christos   struct varobj *var;
    475      1.1  christos 
    476      1.1  christos   if (argc != 1)
    477      1.1  christos     error (_("-var-info-expression: Usage: NAME."));
    478      1.1  christos 
    479      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    480      1.1  christos   var = varobj_get_handle (argv[0]);
    481      1.1  christos 
    482      1.1  christos   lang = varobj_get_language (var);
    483      1.1  christos 
    484  1.6.4.1  christos   uiout->field_string ("lang", lang->la_natural_name);
    485      1.5  christos 
    486  1.6.4.1  christos   std::string exp = varobj_get_expression (var);
    487  1.6.4.1  christos   uiout->field_string ("exp", exp.c_str ());
    488      1.1  christos }
    489      1.1  christos 
    490      1.1  christos void
    491  1.6.4.1  christos mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
    492      1.1  christos {
    493      1.1  christos   struct ui_out *uiout = current_uiout;
    494      1.1  christos   int attr;
    495  1.6.4.1  christos   const char *attstr;
    496      1.1  christos   struct varobj *var;
    497      1.1  christos 
    498      1.1  christos   if (argc != 1)
    499      1.1  christos     error (_("-var-show-attributes: Usage: NAME."));
    500      1.1  christos 
    501      1.1  christos   /* Get varobj handle, if a valid var obj name was specified */
    502      1.1  christos   var = varobj_get_handle (argv[0]);
    503      1.1  christos 
    504      1.1  christos   attr = varobj_get_attributes (var);
    505      1.1  christos   /* FIXME: define masks for attributes */
    506      1.1  christos   if (attr & 0x00000001)
    507      1.1  christos     attstr = "editable";
    508      1.1  christos   else
    509      1.1  christos     attstr = "noneditable";
    510      1.1  christos 
    511  1.6.4.1  christos   uiout->field_string ("attr", attstr);
    512      1.1  christos }
    513      1.1  christos 
    514      1.1  christos void
    515  1.6.4.1  christos mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
    516      1.1  christos {
    517      1.1  christos   struct ui_out *uiout = current_uiout;
    518      1.1  christos   struct varobj *var;
    519      1.1  christos 
    520      1.1  christos   enum varobj_display_formats format;
    521      1.1  christos   int formatFound;
    522      1.1  christos   int oind;
    523      1.1  christos   char *oarg;
    524      1.1  christos 
    525      1.1  christos   enum opt
    526      1.1  christos   {
    527      1.1  christos     OP_FORMAT
    528      1.1  christos   };
    529      1.1  christos   static const struct mi_opt opts[] =
    530      1.1  christos     {
    531      1.1  christos       {"f", OP_FORMAT, 1},
    532      1.1  christos       { 0, 0, 0 }
    533      1.1  christos     };
    534      1.1  christos 
    535      1.1  christos   /* Parse arguments.  */
    536      1.1  christos   format = FORMAT_NATURAL;
    537      1.1  christos   formatFound = 0;
    538      1.1  christos   oind = 0;
    539      1.1  christos   while (1)
    540      1.1  christos     {
    541      1.1  christos       int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
    542      1.1  christos 			   opts, &oind, &oarg);
    543      1.1  christos 
    544      1.1  christos       if (opt < 0)
    545      1.1  christos 	break;
    546      1.1  christos       switch ((enum opt) opt)
    547      1.1  christos 	{
    548      1.1  christos 	case OP_FORMAT:
    549      1.1  christos 	  if (formatFound)
    550      1.1  christos 	    error (_("Cannot specify format more than once"));
    551      1.1  christos 
    552      1.1  christos 	  format = mi_parse_format (oarg);
    553      1.1  christos 	  formatFound = 1;
    554      1.1  christos 	  break;
    555      1.1  christos 	}
    556      1.1  christos     }
    557      1.1  christos 
    558      1.1  christos   if (oind >= argc)
    559      1.1  christos     error (_("Usage: [-f FORMAT] NAME"));
    560      1.1  christos 
    561      1.1  christos   if (oind < argc - 1)
    562      1.1  christos     error (_("Garbage at end of command"));
    563      1.1  christos 
    564      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    565      1.1  christos   var = varobj_get_handle (argv[oind]);
    566      1.1  christos 
    567      1.1  christos   if (formatFound)
    568      1.1  christos     {
    569  1.6.4.1  christos       std::string val = varobj_get_formatted_value (var, format);
    570      1.1  christos 
    571  1.6.4.1  christos       uiout->field_string ("value", val.c_str ());
    572      1.1  christos     }
    573      1.1  christos   else
    574      1.1  christos     {
    575  1.6.4.1  christos       std::string val = varobj_get_value (var);
    576      1.1  christos 
    577  1.6.4.1  christos       uiout->field_string ("value", val.c_str ());
    578      1.1  christos     }
    579      1.1  christos }
    580      1.1  christos 
    581      1.1  christos void
    582  1.6.4.1  christos mi_cmd_var_assign (const char *command, char **argv, int argc)
    583      1.1  christos {
    584      1.1  christos   struct ui_out *uiout = current_uiout;
    585      1.1  christos   struct varobj *var;
    586      1.1  christos 
    587      1.1  christos   if (argc != 2)
    588      1.1  christos     error (_("-var-assign: Usage: NAME EXPRESSION."));
    589      1.1  christos 
    590      1.1  christos   /* Get varobj handle, if a valid var obj name was specified.  */
    591      1.1  christos   var = varobj_get_handle (argv[0]);
    592      1.1  christos 
    593      1.1  christos   if (!varobj_editable_p (var))
    594      1.1  christos     error (_("-var-assign: Variable object is not editable"));
    595      1.1  christos 
    596  1.6.4.1  christos   const char *expression = argv[1];
    597      1.1  christos 
    598      1.1  christos   /* MI command '-var-assign' may write memory, so suppress memory
    599      1.1  christos      changed notification if it does.  */
    600  1.6.4.1  christos   scoped_restore save_suppress
    601  1.6.4.1  christos     = make_scoped_restore (&mi_suppress_notification.memory, 1);
    602      1.1  christos 
    603      1.1  christos   if (!varobj_set_value (var, expression))
    604      1.1  christos     error (_("-var-assign: Could not assign "
    605      1.1  christos 	     "expression to variable object"));
    606      1.1  christos 
    607  1.6.4.1  christos   std::string val = varobj_get_value (var);
    608  1.6.4.1  christos   uiout->field_string ("value", val.c_str ());
    609      1.1  christos }
    610      1.1  christos 
    611      1.1  christos /* Type used for parameters passing to mi_cmd_var_update_iter.  */
    612      1.1  christos 
    613      1.1  christos struct mi_cmd_var_update
    614      1.1  christos   {
    615      1.1  christos     int only_floating;
    616      1.1  christos     enum print_values print_values;
    617      1.1  christos   };
    618      1.1  christos 
    619      1.1  christos /* Helper for mi_cmd_var_update - update each VAR.  */
    620      1.1  christos 
    621      1.1  christos static void
    622      1.1  christos mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
    623      1.1  christos {
    624      1.6  christos   struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
    625      1.1  christos   int thread_id, thread_stopped;
    626      1.1  christos 
    627      1.1  christos   thread_id = varobj_get_thread_id (var);
    628      1.1  christos 
    629      1.3  christos   if (thread_id == -1
    630      1.3  christos       && (ptid_equal (inferior_ptid, null_ptid)
    631      1.3  christos 	  || is_stopped (inferior_ptid)))
    632      1.1  christos     thread_stopped = 1;
    633      1.1  christos   else
    634      1.1  christos     {
    635      1.6  christos       struct thread_info *tp = find_thread_global_id (thread_id);
    636      1.1  christos 
    637      1.1  christos       if (tp)
    638      1.1  christos 	thread_stopped = is_stopped (tp->ptid);
    639      1.1  christos       else
    640      1.1  christos 	thread_stopped = 1;
    641      1.1  christos     }
    642      1.1  christos 
    643      1.1  christos   if (thread_stopped
    644      1.1  christos       && (!data->only_floating || varobj_floating_p (var)))
    645      1.1  christos     varobj_update_one (var, data->print_values, 0 /* implicit */);
    646      1.1  christos }
    647      1.1  christos 
    648      1.1  christos void
    649  1.6.4.1  christos mi_cmd_var_update (const char *command, char **argv, int argc)
    650      1.1  christos {
    651      1.1  christos   struct ui_out *uiout = current_uiout;
    652      1.1  christos   struct cleanup *cleanup;
    653      1.1  christos   char *name;
    654      1.1  christos   enum print_values print_values;
    655      1.1  christos 
    656      1.1  christos   if (argc != 1 && argc != 2)
    657      1.1  christos     error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
    658      1.1  christos 
    659      1.1  christos   if (argc == 1)
    660      1.1  christos     name = argv[0];
    661      1.1  christos   else
    662      1.1  christos     name = argv[1];
    663      1.1  christos 
    664      1.1  christos   if (argc == 2)
    665      1.1  christos     print_values = mi_parse_print_values (argv[0]);
    666      1.1  christos   else
    667      1.1  christos     print_values = PRINT_NO_VALUES;
    668      1.1  christos 
    669      1.1  christos   if (mi_version (uiout) <= 1)
    670      1.1  christos     cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
    671      1.1  christos   else
    672      1.1  christos     cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
    673      1.1  christos 
    674      1.1  christos   /* Check if the parameter is a "*", which means that we want to
    675      1.1  christos      update all variables.  */
    676      1.1  christos 
    677      1.1  christos   if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
    678      1.1  christos     {
    679      1.1  christos       struct mi_cmd_var_update data;
    680      1.1  christos 
    681      1.1  christos       data.only_floating = (*name == '@');
    682      1.1  christos       data.print_values = print_values;
    683      1.1  christos 
    684      1.1  christos       /* varobj_update_one automatically updates all the children of
    685      1.1  christos 	 VAROBJ.  Therefore update each VAROBJ only once by iterating
    686      1.1  christos 	 only the root VAROBJs.  */
    687      1.1  christos 
    688      1.1  christos       all_root_varobjs (mi_cmd_var_update_iter, &data);
    689      1.1  christos     }
    690      1.1  christos   else
    691      1.1  christos     {
    692      1.1  christos       /* Get varobj handle, if a valid var obj name was specified.  */
    693      1.1  christos       struct varobj *var = varobj_get_handle (name);
    694      1.1  christos 
    695      1.1  christos       varobj_update_one (var, print_values, 1 /* explicit */);
    696      1.1  christos     }
    697      1.1  christos 
    698      1.1  christos   do_cleanups (cleanup);
    699      1.1  christos }
    700      1.1  christos 
    701      1.1  christos /* Helper for mi_cmd_var_update().  */
    702      1.1  christos 
    703      1.1  christos static void
    704      1.1  christos varobj_update_one (struct varobj *var, enum print_values print_values,
    705      1.5  christos 		   int is_explicit)
    706      1.1  christos {
    707      1.1  christos   struct ui_out *uiout = current_uiout;
    708      1.1  christos   VEC (varobj_update_result) *changes;
    709      1.1  christos   varobj_update_result *r;
    710      1.1  christos   int i;
    711      1.1  christos 
    712      1.5  christos   changes = varobj_update (&var, is_explicit);
    713      1.1  christos 
    714      1.1  christos   for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
    715      1.1  christos     {
    716      1.1  christos       int from, to;
    717      1.1  christos       struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
    718      1.1  christos 
    719      1.1  christos       if (mi_version (uiout) > 1)
    720      1.1  christos 	make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
    721  1.6.4.1  christos       uiout->field_string ("name", varobj_get_objname (r->varobj));
    722      1.1  christos 
    723      1.1  christos       switch (r->status)
    724      1.1  christos 	{
    725      1.1  christos 	case VAROBJ_IN_SCOPE:
    726      1.1  christos 	  if (mi_print_value_p (r->varobj, print_values))
    727      1.1  christos 	    {
    728  1.6.4.1  christos 	      std::string val = varobj_get_value (r->varobj);
    729      1.1  christos 
    730  1.6.4.1  christos 	      uiout->field_string ("value", val.c_str ());
    731      1.1  christos 	    }
    732  1.6.4.1  christos 	  uiout->field_string ("in_scope", "true");
    733      1.1  christos 	  break;
    734      1.1  christos         case VAROBJ_NOT_IN_SCOPE:
    735  1.6.4.1  christos           uiout->field_string ("in_scope", "false");
    736      1.1  christos 	  break;
    737      1.1  christos         case VAROBJ_INVALID:
    738  1.6.4.1  christos           uiout->field_string ("in_scope", "invalid");
    739      1.1  christos  	  break;
    740      1.1  christos 	}
    741      1.1  christos 
    742      1.1  christos       if (r->status != VAROBJ_INVALID)
    743      1.1  christos 	{
    744      1.1  christos 	  if (r->type_changed)
    745  1.6.4.1  christos 	    uiout->field_string ("type_changed", "true");
    746      1.1  christos 	  else
    747  1.6.4.1  christos 	    uiout->field_string ("type_changed", "false");
    748      1.1  christos 	}
    749      1.1  christos 
    750      1.1  christos       if (r->type_changed)
    751      1.5  christos 	{
    752  1.6.4.1  christos 	  std::string type_name = varobj_get_type (r->varobj);
    753      1.5  christos 
    754  1.6.4.1  christos 	  uiout->field_string ("new_type", type_name.c_str ());
    755      1.5  christos 	}
    756      1.1  christos 
    757      1.1  christos       if (r->type_changed || r->children_changed)
    758  1.6.4.1  christos 	uiout->field_int ("new_num_children",
    759      1.1  christos 			  varobj_get_num_children (r->varobj));
    760      1.1  christos 
    761  1.6.4.1  christos       gdb::unique_xmalloc_ptr<char> display_hint
    762  1.6.4.1  christos 	= varobj_get_display_hint (r->varobj);
    763      1.1  christos       if (display_hint)
    764  1.6.4.1  christos 	uiout->field_string ("displayhint", display_hint.get ());
    765      1.1  christos 
    766      1.3  christos       if (varobj_is_dynamic_p (r->varobj))
    767  1.6.4.1  christos 	uiout->field_int ("dynamic", 1);
    768      1.1  christos 
    769      1.1  christos       varobj_get_child_range (r->varobj, &from, &to);
    770  1.6.4.1  christos       uiout->field_int ("has_more", varobj_has_more (r->varobj, to));
    771      1.1  christos 
    772      1.5  christos       if (r->newobj)
    773      1.1  christos 	{
    774      1.1  christos 	  int j;
    775      1.1  christos 	  varobj_p child;
    776      1.1  christos 	  struct cleanup *cleanup;
    777      1.1  christos 
    778      1.1  christos 	  cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
    779      1.5  christos 	  for (j = 0; VEC_iterate (varobj_p, r->newobj, j, child); ++j)
    780      1.1  christos 	    {
    781      1.1  christos 	      struct cleanup *cleanup_child;
    782      1.1  christos 
    783      1.1  christos 	      cleanup_child
    784      1.1  christos 		= make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
    785      1.1  christos 	      print_varobj (child, print_values, 1 /* print_expression */);
    786      1.1  christos 	      do_cleanups (cleanup_child);
    787      1.1  christos 	    }
    788      1.1  christos 
    789      1.1  christos 	  do_cleanups (cleanup);
    790      1.5  christos 	  VEC_free (varobj_p, r->newobj);
    791      1.5  christos 	  r->newobj = NULL;	/* Paranoia.  */
    792      1.1  christos 	}
    793      1.1  christos 
    794      1.1  christos       do_cleanups (cleanup);
    795      1.1  christos     }
    796      1.1  christos   VEC_free (varobj_update_result, changes);
    797      1.1  christos }
    798      1.1  christos 
    799      1.1  christos void
    800  1.6.4.1  christos mi_cmd_enable_pretty_printing (const char *command, char **argv, int argc)
    801      1.1  christos {
    802      1.1  christos   if (argc != 0)
    803      1.1  christos     error (_("-enable-pretty-printing: no arguments allowed"));
    804      1.1  christos 
    805      1.1  christos   varobj_enable_pretty_printing ();
    806      1.1  christos }
    807      1.1  christos 
    808      1.1  christos void
    809  1.6.4.1  christos mi_cmd_var_set_update_range (const char *command, char **argv, int argc)
    810      1.1  christos {
    811      1.1  christos   struct varobj *var;
    812      1.1  christos   int from, to;
    813      1.1  christos 
    814      1.1  christos   if (argc != 3)
    815      1.1  christos     error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
    816      1.1  christos 
    817      1.1  christos   var = varobj_get_handle (argv[0]);
    818      1.1  christos   from = atoi (argv[1]);
    819      1.1  christos   to = atoi (argv[2]);
    820      1.1  christos 
    821      1.1  christos   varobj_set_child_range (var, from, to);
    822      1.1  christos }
    823