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