1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
31 #include "mi-getopt.h"
33 const char mi_no_values[] = "--no-values";
34 const char mi_simple_values[] = "--simple-values";
35 const char mi_all_values[] = "--all-values";
37 extern int varobjdebug; /* defined in varobj.c. */
39 static void varobj_update_one (struct varobj *var,
40 enum print_values print_values,
43 static int mi_print_value_p (struct type *type, enum print_values print_values);
45 /* Print variable object VAR. The PRINT_VALUES parameter controls
46 if the value should be printed. The PRINT_EXPRESSION parameter
47 controls if the expression should be printed. */
49 print_varobj (struct varobj *var, enum print_values print_values,
52 struct type *gdb_type;
56 ui_out_field_string (uiout, "name", varobj_get_objname (var));
58 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
59 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
61 if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
62 ui_out_field_string (uiout, "value", varobj_get_value (var));
64 type = varobj_get_type (var);
67 ui_out_field_string (uiout, "type", type);
71 thread_id = varobj_get_thread_id (var);
73 ui_out_field_int (uiout, "thread-id", thread_id);
75 if (varobj_get_frozen (var))
76 ui_out_field_int (uiout, "frozen", 1);
79 /* VAROBJ operations */
82 mi_cmd_var_create (char *command, char **argv, int argc)
84 CORE_ADDR frameaddr = 0;
89 struct cleanup *old_cleanups;
90 enum varobj_type var_type;
94 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
95 ...."); return MI_CMD_ERROR; */
96 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
99 name = xstrdup (argv[0]);
100 /* Add cleanup for name. Must be free_current_contents as
101 name can be reallocated */
102 old_cleanups = make_cleanup (free_current_contents, &name);
104 frame = xstrdup (argv[1]);
105 make_cleanup (xfree, frame);
107 expr = xstrdup (argv[2]);
108 make_cleanup (xfree, expr);
110 if (strcmp (name, "-") == 0)
113 name = varobj_gen_name ();
115 else if (!isalpha (*name))
116 error (_("mi_cmd_var_create: name of object must begin with a letter"));
118 if (strcmp (frame, "*") == 0)
119 var_type = USE_CURRENT_FRAME;
120 else if (strcmp (frame, "@") == 0)
121 var_type = USE_SELECTED_FRAME;
124 var_type = USE_SPECIFIED_FRAME;
125 frameaddr = string_to_core_addr (frame);
129 fprintf_unfiltered (gdb_stdlog,
130 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
131 name, frame, paddr (frameaddr), expr);
133 var = varobj_create (name, expr, frameaddr, var_type);
136 error (_("mi_cmd_var_create: unable to create variable object"));
138 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
140 do_cleanups (old_cleanups);
145 mi_cmd_var_delete (char *command, char **argv, int argc)
150 int children_only_p = 0;
151 struct cleanup *old_cleanups;
153 if (argc < 1 || argc > 2)
154 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
156 name = xstrdup (argv[0]);
157 /* Add cleanup for name. Must be free_current_contents as
158 name can be reallocated */
159 old_cleanups = make_cleanup (free_current_contents, &name);
161 /* If we have one single argument it cannot be '-c' or any string
162 starting with '-'. */
165 if (strcmp (name, "-c") == 0)
166 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
168 error (_("mi_cmd_var_delete: Illegal variable object name"));
171 /* If we have 2 arguments they must be '-c' followed by a string
172 which would be the variable name. */
175 if (strcmp (name, "-c") != 0)
176 error (_("mi_cmd_var_delete: Invalid option."));
178 do_cleanups (old_cleanups);
179 name = xstrdup (argv[1]);
180 make_cleanup (free_current_contents, &name);
183 /* If we didn't error out, now NAME contains the name of the
186 var = varobj_get_handle (name);
189 error (_("mi_cmd_var_delete: Variable object not found."));
191 numdel = varobj_delete (var, NULL, children_only_p);
193 ui_out_field_int (uiout, "ndeleted", numdel);
195 do_cleanups (old_cleanups);
199 /* Parse a string argument into a format value. */
201 static enum varobj_display_formats
202 mi_parse_format (const char *arg)
210 if (strncmp (arg, "natural", len) == 0)
211 return FORMAT_NATURAL;
212 else if (strncmp (arg, "binary", len) == 0)
213 return FORMAT_BINARY;
214 else if (strncmp (arg, "decimal", len) == 0)
215 return FORMAT_DECIMAL;
216 else if (strncmp (arg, "hexadecimal", len) == 0)
217 return FORMAT_HEXADECIMAL;
218 else if (strncmp (arg, "octal", len) == 0)
222 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
226 mi_cmd_var_set_format (char *command, char **argv, int argc)
228 enum varobj_display_formats format;
232 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
234 /* Get varobj handle, if a valid var obj name was specified */
235 var = varobj_get_handle (argv[0]);
238 error (_("mi_cmd_var_set_format: Variable object not found"));
240 format = mi_parse_format (argv[1]);
242 /* Set the format of VAR to given format */
243 varobj_set_display_format (var, format);
245 /* Report the new current format */
246 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
248 /* Report the value in the new format */
249 ui_out_field_string (uiout, "value", varobj_get_value (var));
254 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
260 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
262 var = varobj_get_handle (argv[0]);
264 error (_("Variable object not found"));
266 if (strcmp (argv[1], "0") == 0)
268 else if (strcmp (argv[1], "1") == 0)
271 error (_("Invalid flag value"));
273 varobj_set_frozen (var, frozen);
275 /* We don't automatically return the new value, or what varobjs got new
276 values during unfreezing. If this information is required, client
277 should call -var-update explicitly. */
283 mi_cmd_var_show_format (char *command, char **argv, int argc)
285 enum varobj_display_formats format;
289 error (_("mi_cmd_var_show_format: Usage: NAME."));
291 /* Get varobj handle, if a valid var obj name was specified */
292 var = varobj_get_handle (argv[0]);
294 error (_("mi_cmd_var_show_format: Variable object not found"));
296 format = varobj_get_display_format (var);
298 /* Report the current format */
299 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
304 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
309 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
311 /* Get varobj handle, if a valid var obj name was specified */
312 var = varobj_get_handle (argv[0]);
314 error (_("mi_cmd_var_info_num_children: Variable object not found"));
316 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
320 /* Parse a string argument into a print_values value. */
322 static enum print_values
323 mi_parse_values_option (const char *arg)
325 if (strcmp (arg, "0") == 0
326 || strcmp (arg, mi_no_values) == 0)
327 return PRINT_NO_VALUES;
328 else if (strcmp (arg, "1") == 0
329 || strcmp (arg, mi_all_values) == 0)
330 return PRINT_ALL_VALUES;
331 else if (strcmp (arg, "2") == 0
332 || strcmp (arg, mi_simple_values) == 0)
333 return PRINT_SIMPLE_VALUES;
335 error (_("Unknown value for PRINT_VALUES\n\
336 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
337 mi_no_values, mi_simple_values, mi_all_values);
340 /* Return 1 if given the argument PRINT_VALUES we should display
341 a value of type TYPE. */
344 mi_print_value_p (struct type *type, enum print_values print_values)
347 if (print_values == PRINT_NO_VALUES)
350 if (print_values == PRINT_ALL_VALUES)
357 type = check_typedef (type);
359 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
360 and that type is not a compound type. */
361 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
362 && TYPE_CODE (type) != TYPE_CODE_STRUCT
363 && TYPE_CODE (type) != TYPE_CODE_UNION);
368 mi_cmd_var_list_children (char *command, char **argv, int argc)
371 VEC(varobj_p) *children;
372 struct varobj *child;
373 struct cleanup *cleanup_children;
375 enum print_values print_values;
378 if (argc != 1 && argc != 2)
379 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
381 /* Get varobj handle, if a valid var obj name was specified */
383 var = varobj_get_handle (argv[0]);
385 var = varobj_get_handle (argv[1]);
387 error (_("Variable object not found"));
389 children = varobj_list_children (var);
390 ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children));
392 print_values = mi_parse_values_option (argv[0]);
394 print_values = PRINT_NO_VALUES;
396 if (VEC_length (varobj_p, children) == 0)
399 if (mi_version (uiout) == 1)
400 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
402 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
403 for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix)
405 struct cleanup *cleanup_child;
406 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
407 print_varobj (child, print_values, 1 /* print expression */);
408 do_cleanups (cleanup_child);
410 do_cleanups (cleanup_children);
415 mi_cmd_var_info_type (char *command, char **argv, int argc)
420 error (_("mi_cmd_var_info_type: Usage: NAME."));
422 /* Get varobj handle, if a valid var obj name was specified */
423 var = varobj_get_handle (argv[0]);
425 error (_("mi_cmd_var_info_type: Variable object not found"));
427 ui_out_field_string (uiout, "type", varobj_get_type (var));
432 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
438 error (_("Usage: NAME."));
440 /* Get varobj handle, if a valid var obj name was specified. */
441 var = varobj_get_handle (argv[0]);
443 error (_("Variable object not found"));
445 path_expr = varobj_get_path_expr (var);
447 ui_out_field_string (uiout, "path_expr", path_expr);
453 mi_cmd_var_info_expression (char *command, char **argv, int argc)
455 enum varobj_languages lang;
459 error (_("mi_cmd_var_info_expression: Usage: NAME."));
461 /* Get varobj handle, if a valid var obj name was specified */
462 var = varobj_get_handle (argv[0]);
464 error (_("mi_cmd_var_info_expression: Variable object not found"));
466 lang = varobj_get_language (var);
468 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
469 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
474 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
481 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
483 /* Get varobj handle, if a valid var obj name was specified */
484 var = varobj_get_handle (argv[0]);
486 error (_("mi_cmd_var_show_attributes: Variable object not found"));
488 attr = varobj_get_attributes (var);
489 /* FIXME: define masks for attributes */
490 if (attr & 0x00000001)
493 attstr = "noneditable";
495 ui_out_field_string (uiout, "attr", attstr);
500 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
504 enum varobj_display_formats format;
513 static struct mi_opt opts[] =
519 /* Parse arguments */
520 format = FORMAT_NATURAL;
525 int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg);
528 switch ((enum opt) opt)
532 error (_("Cannot specify format more than once"));
534 format = mi_parse_format (optarg);
541 error (_("Usage: [-f FORMAT] NAME"));
543 if (optind < argc - 1)
544 error (_("Garbage at end of command"));
546 /* Get varobj handle, if a valid var obj name was specified */
547 var = varobj_get_handle (argv[optind]);
549 error (_("Variable object not found"));
552 ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));
554 ui_out_field_string (uiout, "value", varobj_get_value (var));
560 mi_cmd_var_assign (char *command, char **argv, int argc)
566 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
568 /* Get varobj handle, if a valid var obj name was specified */
569 var = varobj_get_handle (argv[0]);
571 error (_("mi_cmd_var_assign: Variable object not found"));
573 if (!varobj_editable_p (var))
574 error (_("mi_cmd_var_assign: Variable object is not editable"));
576 expression = xstrdup (argv[1]);
578 if (!varobj_set_value (var, expression))
579 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
581 ui_out_field_string (uiout, "value", varobj_get_value (var));
586 mi_cmd_var_update (char *command, char **argv, int argc)
589 struct varobj **rootlist;
591 struct cleanup *cleanup;
594 enum print_values print_values;
596 if (argc != 1 && argc != 2)
597 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
605 print_values = mi_parse_values_option (argv[0]);
607 print_values = PRINT_NO_VALUES;
609 /* Check if the parameter is a "*" which means that we want
610 to update all variables */
612 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
614 nv = varobj_list (&rootlist);
615 cleanup = make_cleanup (xfree, rootlist);
616 if (mi_version (uiout) <= 1)
617 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
619 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
622 do_cleanups (cleanup);
628 if (*name == '*' || varobj_floating_p (*cr))
629 varobj_update_one (*cr, print_values, 0 /* implicit */);
632 do_cleanups (cleanup);
636 /* Get varobj handle, if a valid var obj name was specified */
637 var = varobj_get_handle (name);
639 error (_("mi_cmd_var_update: Variable object not found"));
641 if (mi_version (uiout) <= 1)
642 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
644 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
645 varobj_update_one (var, print_values, 1 /* explicit */);
646 do_cleanups (cleanup);
651 /* Helper for mi_cmd_var_update(). */
654 varobj_update_one (struct varobj *var, enum print_values print_values,
658 struct cleanup *cleanup = NULL;
659 VEC (varobj_update_result) *changes;
660 varobj_update_result *r;
663 changes = varobj_update (&var, explicit);
665 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
667 if (mi_version (uiout) > 1)
668 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
669 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
673 case VAROBJ_IN_SCOPE:
674 if (mi_print_value_p (varobj_get_gdb_type (r->varobj), print_values))
675 ui_out_field_string (uiout, "value", varobj_get_value (r->varobj));
676 ui_out_field_string (uiout, "in_scope", "true");
678 case VAROBJ_NOT_IN_SCOPE:
679 ui_out_field_string (uiout, "in_scope", "false");
682 ui_out_field_string (uiout, "in_scope", "invalid");
686 if (r->status != VAROBJ_INVALID)
689 ui_out_field_string (uiout, "type_changed", "true");
691 ui_out_field_string (uiout, "type_changed", "false");
696 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
697 ui_out_field_int (uiout, "new_num_children",
698 varobj_get_num_children (r->varobj));
701 if (mi_version (uiout) > 1)
702 do_cleanups (cleanup);
704 VEC_free (varobj_update_result, changes);