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);
144 mi_cmd_var_delete (char *command, char **argv, int argc)
149 int children_only_p = 0;
150 struct cleanup *old_cleanups;
152 if (argc < 1 || argc > 2)
153 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
155 name = xstrdup (argv[0]);
156 /* Add cleanup for name. Must be free_current_contents as
157 name can be reallocated */
158 old_cleanups = make_cleanup (free_current_contents, &name);
160 /* If we have one single argument it cannot be '-c' or any string
161 starting with '-'. */
164 if (strcmp (name, "-c") == 0)
165 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
167 error (_("mi_cmd_var_delete: Illegal variable object name"));
170 /* If we have 2 arguments they must be '-c' followed by a string
171 which would be the variable name. */
174 if (strcmp (name, "-c") != 0)
175 error (_("mi_cmd_var_delete: Invalid option."));
177 do_cleanups (old_cleanups);
178 name = xstrdup (argv[1]);
179 make_cleanup (free_current_contents, &name);
182 /* If we didn't error out, now NAME contains the name of the
185 var = varobj_get_handle (name);
188 error (_("mi_cmd_var_delete: Variable object not found."));
190 numdel = varobj_delete (var, NULL, children_only_p);
192 ui_out_field_int (uiout, "ndeleted", numdel);
194 do_cleanups (old_cleanups);
197 /* Parse a string argument into a format value. */
199 static enum varobj_display_formats
200 mi_parse_format (const char *arg)
208 if (strncmp (arg, "natural", len) == 0)
209 return FORMAT_NATURAL;
210 else if (strncmp (arg, "binary", len) == 0)
211 return FORMAT_BINARY;
212 else if (strncmp (arg, "decimal", len) == 0)
213 return FORMAT_DECIMAL;
214 else if (strncmp (arg, "hexadecimal", len) == 0)
215 return FORMAT_HEXADECIMAL;
216 else if (strncmp (arg, "octal", len) == 0)
220 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
224 mi_cmd_var_set_format (char *command, char **argv, int argc)
226 enum varobj_display_formats format;
230 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
232 /* Get varobj handle, if a valid var obj name was specified */
233 var = varobj_get_handle (argv[0]);
236 error (_("mi_cmd_var_set_format: Variable object not found"));
238 format = mi_parse_format (argv[1]);
240 /* Set the format of VAR to given format */
241 varobj_set_display_format (var, format);
243 /* Report the new current format */
244 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
246 /* Report the value in the new format */
247 ui_out_field_string (uiout, "value", varobj_get_value (var));
251 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
257 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
259 var = varobj_get_handle (argv[0]);
261 error (_("Variable object not found"));
263 if (strcmp (argv[1], "0") == 0)
265 else if (strcmp (argv[1], "1") == 0)
268 error (_("Invalid flag value"));
270 varobj_set_frozen (var, frozen);
272 /* We don't automatically return the new value, or what varobjs got new
273 values during unfreezing. If this information is required, client
274 should call -var-update explicitly. */
279 mi_cmd_var_show_format (char *command, char **argv, int argc)
281 enum varobj_display_formats format;
285 error (_("mi_cmd_var_show_format: Usage: NAME."));
287 /* Get varobj handle, if a valid var obj name was specified */
288 var = varobj_get_handle (argv[0]);
290 error (_("mi_cmd_var_show_format: Variable object not found"));
292 format = varobj_get_display_format (var);
294 /* Report the current format */
295 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
299 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
304 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
306 /* Get varobj handle, if a valid var obj name was specified */
307 var = varobj_get_handle (argv[0]);
309 error (_("mi_cmd_var_info_num_children: Variable object not found"));
311 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
314 /* Parse a string argument into a print_values value. */
316 static enum print_values
317 mi_parse_values_option (const char *arg)
319 if (strcmp (arg, "0") == 0
320 || strcmp (arg, mi_no_values) == 0)
321 return PRINT_NO_VALUES;
322 else if (strcmp (arg, "1") == 0
323 || strcmp (arg, mi_all_values) == 0)
324 return PRINT_ALL_VALUES;
325 else if (strcmp (arg, "2") == 0
326 || strcmp (arg, mi_simple_values) == 0)
327 return PRINT_SIMPLE_VALUES;
329 error (_("Unknown value for PRINT_VALUES\n\
330 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
331 mi_no_values, mi_simple_values, mi_all_values);
334 /* Return 1 if given the argument PRINT_VALUES we should display
335 a value of type TYPE. */
338 mi_print_value_p (struct type *type, enum print_values print_values)
341 if (print_values == PRINT_NO_VALUES)
344 if (print_values == PRINT_ALL_VALUES)
351 type = check_typedef (type);
353 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
354 and that type is not a compound type. */
355 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
356 && TYPE_CODE (type) != TYPE_CODE_STRUCT
357 && TYPE_CODE (type) != TYPE_CODE_UNION);
362 mi_cmd_var_list_children (char *command, char **argv, int argc)
365 VEC(varobj_p) *children;
366 struct varobj *child;
367 struct cleanup *cleanup_children;
369 enum print_values print_values;
372 if (argc != 1 && argc != 2)
373 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
375 /* Get varobj handle, if a valid var obj name was specified */
377 var = varobj_get_handle (argv[0]);
379 var = varobj_get_handle (argv[1]);
381 error (_("Variable object not found"));
383 children = varobj_list_children (var);
384 ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children));
386 print_values = mi_parse_values_option (argv[0]);
388 print_values = PRINT_NO_VALUES;
390 if (VEC_length (varobj_p, children) == 0)
393 if (mi_version (uiout) == 1)
394 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
396 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
397 for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix)
399 struct cleanup *cleanup_child;
400 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
401 print_varobj (child, print_values, 1 /* print expression */);
402 do_cleanups (cleanup_child);
404 do_cleanups (cleanup_children);
408 mi_cmd_var_info_type (char *command, char **argv, int argc)
413 error (_("mi_cmd_var_info_type: Usage: NAME."));
415 /* Get varobj handle, if a valid var obj name was specified */
416 var = varobj_get_handle (argv[0]);
418 error (_("mi_cmd_var_info_type: Variable object not found"));
420 ui_out_field_string (uiout, "type", varobj_get_type (var));
424 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
430 error (_("Usage: NAME."));
432 /* Get varobj handle, if a valid var obj name was specified. */
433 var = varobj_get_handle (argv[0]);
435 error (_("Variable object not found"));
437 path_expr = varobj_get_path_expr (var);
439 ui_out_field_string (uiout, "path_expr", path_expr);
443 mi_cmd_var_info_expression (char *command, char **argv, int argc)
445 enum varobj_languages lang;
449 error (_("mi_cmd_var_info_expression: Usage: NAME."));
451 /* Get varobj handle, if a valid var obj name was specified */
452 var = varobj_get_handle (argv[0]);
454 error (_("mi_cmd_var_info_expression: Variable object not found"));
456 lang = varobj_get_language (var);
458 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
459 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
463 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
470 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
472 /* Get varobj handle, if a valid var obj name was specified */
473 var = varobj_get_handle (argv[0]);
475 error (_("mi_cmd_var_show_attributes: Variable object not found"));
477 attr = varobj_get_attributes (var);
478 /* FIXME: define masks for attributes */
479 if (attr & 0x00000001)
482 attstr = "noneditable";
484 ui_out_field_string (uiout, "attr", attstr);
488 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
492 enum varobj_display_formats format;
501 static struct mi_opt opts[] =
507 /* Parse arguments */
508 format = FORMAT_NATURAL;
513 int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg);
516 switch ((enum opt) opt)
520 error (_("Cannot specify format more than once"));
522 format = mi_parse_format (optarg);
529 error (_("Usage: [-f FORMAT] NAME"));
531 if (optind < argc - 1)
532 error (_("Garbage at end of command"));
534 /* Get varobj handle, if a valid var obj name was specified */
535 var = varobj_get_handle (argv[optind]);
537 error (_("Variable object not found"));
540 ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));
542 ui_out_field_string (uiout, "value", varobj_get_value (var));
546 mi_cmd_var_assign (char *command, char **argv, int argc)
552 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
554 /* Get varobj handle, if a valid var obj name was specified */
555 var = varobj_get_handle (argv[0]);
557 error (_("mi_cmd_var_assign: Variable object not found"));
559 if (!varobj_editable_p (var))
560 error (_("mi_cmd_var_assign: Variable object is not editable"));
562 expression = xstrdup (argv[1]);
564 if (!varobj_set_value (var, expression))
565 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
567 ui_out_field_string (uiout, "value", varobj_get_value (var));
571 mi_cmd_var_update (char *command, char **argv, int argc)
574 struct varobj **rootlist;
576 struct cleanup *cleanup;
579 enum print_values print_values;
581 if (argc != 1 && argc != 2)
582 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
590 print_values = mi_parse_values_option (argv[0]);
592 print_values = PRINT_NO_VALUES;
594 /* Check if the parameter is a "*" which means that we want
595 to update all variables */
597 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
599 nv = varobj_list (&rootlist);
600 cleanup = make_cleanup (xfree, rootlist);
601 if (mi_version (uiout) <= 1)
602 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
604 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
607 do_cleanups (cleanup);
613 if (*name == '*' || varobj_floating_p (*cr))
614 varobj_update_one (*cr, print_values, 0 /* implicit */);
617 do_cleanups (cleanup);
621 /* Get varobj handle, if a valid var obj name was specified */
622 var = varobj_get_handle (name);
624 error (_("mi_cmd_var_update: Variable object not found"));
626 if (mi_version (uiout) <= 1)
627 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
629 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
630 varobj_update_one (var, print_values, 1 /* explicit */);
631 do_cleanups (cleanup);
635 /* Helper for mi_cmd_var_update(). */
638 varobj_update_one (struct varobj *var, enum print_values print_values,
642 struct cleanup *cleanup = NULL;
643 VEC (varobj_update_result) *changes;
644 varobj_update_result *r;
647 changes = varobj_update (&var, explicit);
649 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
651 if (mi_version (uiout) > 1)
652 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
653 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
657 case VAROBJ_IN_SCOPE:
658 if (mi_print_value_p (varobj_get_gdb_type (r->varobj), print_values))
659 ui_out_field_string (uiout, "value", varobj_get_value (r->varobj));
660 ui_out_field_string (uiout, "in_scope", "true");
662 case VAROBJ_NOT_IN_SCOPE:
663 ui_out_field_string (uiout, "in_scope", "false");
666 ui_out_field_string (uiout, "in_scope", "invalid");
670 if (r->status != VAROBJ_INVALID)
673 ui_out_field_string (uiout, "type_changed", "true");
675 ui_out_field_string (uiout, "type_changed", "false");
680 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
681 ui_out_field_int (uiout, "new_num_children",
682 varobj_get_num_children (r->varobj));
685 if (mi_version (uiout) > 1)
686 do_cleanups (cleanup);
688 VEC_free (varobj_update_result, changes);