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