1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008, 2009, 2010
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"
32 #include "gdbthread.h"
34 const char mi_no_values[] = "--no-values";
35 const char mi_simple_values[] = "--simple-values";
36 const char mi_all_values[] = "--all-values";
38 extern int varobjdebug; /* defined in varobj.c. */
40 static void varobj_update_one (struct varobj *var,
41 enum print_values print_values,
44 static int mi_print_value_p (struct varobj *var, enum print_values print_values);
46 /* Print variable object VAR. The PRINT_VALUES parameter controls
47 if the value should be printed. The PRINT_EXPRESSION parameter
48 controls if the expression should be printed. */
50 print_varobj (struct varobj *var, enum print_values print_values,
57 ui_out_field_string (uiout, "name", varobj_get_objname (var));
59 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
60 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
62 if (mi_print_value_p (var, print_values))
64 char *val = varobj_get_value (var);
65 ui_out_field_string (uiout, "value", val);
69 type = varobj_get_type (var);
72 ui_out_field_string (uiout, "type", type);
76 thread_id = varobj_get_thread_id (var);
78 ui_out_field_int (uiout, "thread-id", thread_id);
80 if (varobj_get_frozen (var))
81 ui_out_field_int (uiout, "frozen", 1);
83 display_hint = varobj_get_display_hint (var);
86 ui_out_field_string (uiout, "displayhint", display_hint);
90 if (varobj_pretty_printed_p (var))
91 ui_out_field_int (uiout, "dynamic", 1);
94 /* VAROBJ operations */
97 mi_cmd_var_create (char *command, char **argv, int argc)
99 CORE_ADDR frameaddr = 0;
104 struct cleanup *old_cleanups;
105 enum varobj_type var_type;
109 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
110 ...."); return MI_CMD_ERROR; */
111 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
114 name = xstrdup (argv[0]);
115 /* Add cleanup for name. Must be free_current_contents as
116 name can be reallocated */
117 old_cleanups = make_cleanup (free_current_contents, &name);
119 frame = xstrdup (argv[1]);
120 make_cleanup (xfree, frame);
122 expr = xstrdup (argv[2]);
123 make_cleanup (xfree, expr);
125 if (strcmp (name, "-") == 0)
128 name = varobj_gen_name ();
130 else if (!isalpha (*name))
131 error (_("mi_cmd_var_create: name of object must begin with a letter"));
133 if (strcmp (frame, "*") == 0)
134 var_type = USE_CURRENT_FRAME;
135 else if (strcmp (frame, "@") == 0)
136 var_type = USE_SELECTED_FRAME;
139 var_type = USE_SPECIFIED_FRAME;
140 frameaddr = string_to_core_addr (frame);
144 fprintf_unfiltered (gdb_stdlog,
145 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
146 name, frame, hex_string (frameaddr), expr);
148 var = varobj_create (name, expr, frameaddr, var_type);
151 error (_("mi_cmd_var_create: unable to create variable object"));
153 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
155 ui_out_field_int (uiout, "has_more", varobj_has_more (var, 0));
157 do_cleanups (old_cleanups);
161 mi_cmd_var_delete (char *command, char **argv, int argc)
166 int children_only_p = 0;
167 struct cleanup *old_cleanups;
169 if (argc < 1 || argc > 2)
170 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
172 name = xstrdup (argv[0]);
173 /* Add cleanup for name. Must be free_current_contents as
174 name can be reallocated */
175 old_cleanups = make_cleanup (free_current_contents, &name);
177 /* If we have one single argument it cannot be '-c' or any string
178 starting with '-'. */
181 if (strcmp (name, "-c") == 0)
182 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
184 error (_("mi_cmd_var_delete: Illegal variable object name"));
187 /* If we have 2 arguments they must be '-c' followed by a string
188 which would be the variable name. */
191 if (strcmp (name, "-c") != 0)
192 error (_("mi_cmd_var_delete: Invalid option."));
194 do_cleanups (old_cleanups);
195 name = xstrdup (argv[1]);
196 make_cleanup (free_current_contents, &name);
199 /* If we didn't error out, now NAME contains the name of the
202 var = varobj_get_handle (name);
204 numdel = varobj_delete (var, NULL, children_only_p);
206 ui_out_field_int (uiout, "ndeleted", numdel);
208 do_cleanups (old_cleanups);
211 /* Parse a string argument into a format value. */
213 static enum varobj_display_formats
214 mi_parse_format (const char *arg)
222 if (strncmp (arg, "natural", len) == 0)
223 return FORMAT_NATURAL;
224 else if (strncmp (arg, "binary", len) == 0)
225 return FORMAT_BINARY;
226 else if (strncmp (arg, "decimal", len) == 0)
227 return FORMAT_DECIMAL;
228 else if (strncmp (arg, "hexadecimal", len) == 0)
229 return FORMAT_HEXADECIMAL;
230 else if (strncmp (arg, "octal", len) == 0)
234 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
238 mi_cmd_var_set_format (char *command, char **argv, int argc)
240 enum varobj_display_formats format;
245 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
247 /* Get varobj handle, if a valid var obj name was specified */
248 var = varobj_get_handle (argv[0]);
250 format = mi_parse_format (argv[1]);
252 /* Set the format of VAR to given format */
253 varobj_set_display_format (var, format);
255 /* Report the new current format */
256 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
258 /* Report the value in the new format */
259 val = varobj_get_value (var);
260 ui_out_field_string (uiout, "value", val);
265 mi_cmd_var_set_visualizer (char *command, char **argv, int argc)
270 error ("Usage: NAME VISUALIZER_FUNCTION.");
272 var = varobj_get_handle (argv[0]);
275 error ("Variable object not found");
277 varobj_set_visualizer (var, argv[1]);
281 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
287 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
289 var = varobj_get_handle (argv[0]);
291 if (strcmp (argv[1], "0") == 0)
293 else if (strcmp (argv[1], "1") == 0)
296 error (_("Invalid flag value"));
298 varobj_set_frozen (var, frozen);
300 /* We don't automatically return the new value, or what varobjs got new
301 values during unfreezing. If this information is required, client
302 should call -var-update explicitly. */
307 mi_cmd_var_show_format (char *command, char **argv, int argc)
309 enum varobj_display_formats format;
313 error (_("mi_cmd_var_show_format: Usage: NAME."));
315 /* Get varobj handle, if a valid var obj name was specified */
316 var = varobj_get_handle (argv[0]);
318 format = varobj_get_display_format (var);
320 /* Report the current format */
321 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
325 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
330 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
332 /* Get varobj handle, if a valid var obj name was specified */
333 var = varobj_get_handle (argv[0]);
335 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
338 /* Parse a string argument into a print_values value. */
340 static enum print_values
341 mi_parse_values_option (const char *arg)
343 if (strcmp (arg, "0") == 0
344 || strcmp (arg, mi_no_values) == 0)
345 return PRINT_NO_VALUES;
346 else if (strcmp (arg, "1") == 0
347 || strcmp (arg, mi_all_values) == 0)
348 return PRINT_ALL_VALUES;
349 else if (strcmp (arg, "2") == 0
350 || strcmp (arg, mi_simple_values) == 0)
351 return PRINT_SIMPLE_VALUES;
353 error (_("Unknown value for PRINT_VALUES\n\
354 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
355 mi_no_values, mi_simple_values, mi_all_values);
358 /* Return 1 if given the argument PRINT_VALUES we should display
362 mi_print_value_p (struct varobj *var, enum print_values print_values)
366 if (print_values == PRINT_NO_VALUES)
369 if (print_values == PRINT_ALL_VALUES)
372 if (varobj_pretty_printed_p (var))
375 type = varobj_get_gdb_type (var);
380 type = check_typedef (type);
382 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
383 and that type is not a compound type. */
384 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
385 && TYPE_CODE (type) != TYPE_CODE_STRUCT
386 && TYPE_CODE (type) != TYPE_CODE_UNION);
391 mi_cmd_var_list_children (char *command, char **argv, int argc)
394 VEC(varobj_p) *children;
395 struct varobj *child;
396 enum print_values print_values;
401 if (argc < 1 || argc > 4)
402 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME [FROM TO]"));
404 /* Get varobj handle, if a valid var obj name was specified */
405 if (argc == 1 || argc == 3)
406 var = varobj_get_handle (argv[0]);
408 var = varobj_get_handle (argv[1]);
412 from = atoi (argv[argc - 2]);
413 to = atoi (argv[argc - 1]);
421 children = varobj_list_children (var, &from, &to);
422 ui_out_field_int (uiout, "numchild", to - from);
423 if (argc == 2 || argc == 4)
424 print_values = mi_parse_values_option (argv[0]);
426 print_values = PRINT_NO_VALUES;
428 display_hint = varobj_get_display_hint (var);
431 ui_out_field_string (uiout, "displayhint", display_hint);
432 xfree (display_hint);
437 struct cleanup *cleanup_children;
438 if (mi_version (uiout) == 1)
440 = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
443 = make_cleanup_ui_out_list_begin_end (uiout, "children");
445 ix < to && VEC_iterate (varobj_p, children, ix, child);
448 struct cleanup *cleanup_child;
449 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
450 print_varobj (child, print_values, 1 /* print expression */);
451 do_cleanups (cleanup_child);
453 do_cleanups (cleanup_children);
456 ui_out_field_int (uiout, "has_more", varobj_has_more (var, to));
460 mi_cmd_var_info_type (char *command, char **argv, int argc)
465 error (_("mi_cmd_var_info_type: Usage: NAME."));
467 /* Get varobj handle, if a valid var obj name was specified */
468 var = varobj_get_handle (argv[0]);
470 ui_out_field_string (uiout, "type", varobj_get_type (var));
474 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
480 error (_("Usage: NAME."));
482 /* Get varobj handle, if a valid var obj name was specified. */
483 var = varobj_get_handle (argv[0]);
485 path_expr = varobj_get_path_expr (var);
487 ui_out_field_string (uiout, "path_expr", path_expr);
491 mi_cmd_var_info_expression (char *command, char **argv, int argc)
493 enum varobj_languages lang;
497 error (_("mi_cmd_var_info_expression: Usage: NAME."));
499 /* Get varobj handle, if a valid var obj name was specified */
500 var = varobj_get_handle (argv[0]);
502 lang = varobj_get_language (var);
504 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
505 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
509 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
516 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
518 /* Get varobj handle, if a valid var obj name was specified */
519 var = varobj_get_handle (argv[0]);
521 attr = varobj_get_attributes (var);
522 /* FIXME: define masks for attributes */
523 if (attr & 0x00000001)
526 attstr = "noneditable";
528 ui_out_field_string (uiout, "attr", attstr);
532 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
536 enum varobj_display_formats format;
545 static struct mi_opt opts[] =
551 /* Parse arguments */
552 format = FORMAT_NATURAL;
557 int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg);
560 switch ((enum opt) opt)
564 error (_("Cannot specify format more than once"));
566 format = mi_parse_format (optarg);
573 error (_("Usage: [-f FORMAT] NAME"));
575 if (optind < argc - 1)
576 error (_("Garbage at end of command"));
578 /* Get varobj handle, if a valid var obj name was specified */
579 var = varobj_get_handle (argv[optind]);
583 char *val = varobj_get_formatted_value (var, format);
584 ui_out_field_string (uiout, "value", val);
589 char *val = varobj_get_value (var);
590 ui_out_field_string (uiout, "value", val);
596 mi_cmd_var_assign (char *command, char **argv, int argc)
599 char *expression, *val;
602 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
604 /* Get varobj handle, if a valid var obj name was specified */
605 var = varobj_get_handle (argv[0]);
607 if (!varobj_editable_p (var))
608 error (_("mi_cmd_var_assign: Variable object is not editable"));
610 expression = xstrdup (argv[1]);
612 if (!varobj_set_value (var, expression))
613 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
615 val = varobj_get_value (var);
616 ui_out_field_string (uiout, "value", val);
620 /* Type used for parameters passing to mi_cmd_var_update_iter. */
622 struct mi_cmd_var_update
625 enum print_values print_values;
628 /* Helper for mi_cmd_var_update - update each VAR. */
631 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
633 struct mi_cmd_var_update *data = data_pointer;
634 int thread_id, thread_stopped;
636 thread_id = varobj_get_thread_id (var);
638 if (thread_id == -1 && is_stopped (inferior_ptid))
642 struct thread_info *tp = find_thread_id (thread_id);
645 thread_stopped = is_stopped (tp->ptid);
651 if (!data->only_floating || varobj_floating_p (var))
652 varobj_update_one (var, data->print_values, 0 /* implicit */);
656 mi_cmd_var_update (char *command, char **argv, int argc)
658 struct cleanup *cleanup;
660 enum print_values print_values;
662 if (argc != 1 && argc != 2)
663 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
671 print_values = mi_parse_values_option (argv[0]);
673 print_values = PRINT_NO_VALUES;
675 if (mi_version (uiout) <= 1)
676 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
678 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
680 /* Check if the parameter is a "*" which means that we want
681 to update all variables */
683 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
685 struct mi_cmd_var_update data;
687 data.only_floating = *name == '@';
688 data.print_values = print_values;
690 /* varobj_update_one automatically updates all the children of VAROBJ.
691 Therefore update each VAROBJ only once by iterating only the root
694 all_root_varobjs (mi_cmd_var_update_iter, &data);
698 /* Get varobj handle, if a valid var obj name was specified */
699 struct varobj *var = varobj_get_handle (name);
701 varobj_update_one (var, print_values, 1 /* explicit */);
704 do_cleanups (cleanup);
707 /* Helper for mi_cmd_var_update(). */
710 varobj_update_one (struct varobj *var, enum print_values print_values,
713 struct cleanup *cleanup = NULL;
714 VEC (varobj_update_result) *changes;
715 varobj_update_result *r;
718 changes = varobj_update (&var, explicit);
720 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
725 if (mi_version (uiout) > 1)
726 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
727 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
731 case VAROBJ_IN_SCOPE:
732 if (mi_print_value_p (r->varobj, print_values))
734 char *val = varobj_get_value (r->varobj);
735 ui_out_field_string (uiout, "value", val);
738 ui_out_field_string (uiout, "in_scope", "true");
740 case VAROBJ_NOT_IN_SCOPE:
741 ui_out_field_string (uiout, "in_scope", "false");
744 ui_out_field_string (uiout, "in_scope", "invalid");
748 if (r->status != VAROBJ_INVALID)
751 ui_out_field_string (uiout, "type_changed", "true");
753 ui_out_field_string (uiout, "type_changed", "false");
757 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
759 if (r->type_changed || r->children_changed)
760 ui_out_field_int (uiout, "new_num_children",
761 varobj_get_num_children (r->varobj));
763 display_hint = varobj_get_display_hint (var);
766 ui_out_field_string (uiout, "displayhint", display_hint);
767 xfree (display_hint);
770 if (varobj_pretty_printed_p (var))
771 ui_out_field_int (uiout, "dynamic", 1);
773 varobj_get_child_range (r->varobj, &from, &to);
774 ui_out_field_int (uiout, "has_more",
775 varobj_has_more (r->varobj, to));
781 struct cleanup *cleanup;
783 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
784 for (j = 0; VEC_iterate (varobj_p, r->new, j, child); ++j)
786 struct cleanup *cleanup_child;
787 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
788 print_varobj (child, print_values, 1 /* print_expression */);
789 do_cleanups (cleanup_child);
792 do_cleanups (cleanup);
793 VEC_free (varobj_p, r->new);
794 r->new = NULL; /* Paranoia. */
797 if (mi_version (uiout) > 1)
798 do_cleanups (cleanup);
800 VEC_free (varobj_update_result, changes);
804 mi_cmd_enable_pretty_printing (char *command, char **argv, int argc)
807 error (_("mi_cmd_enable_pretty_printing: no arguments allowed"));
808 varobj_enable_pretty_printing ();
812 mi_cmd_var_set_update_range (char *command, char **argv, int argc)
818 error (_("mi_cmd_var_set_update_range: Usage: VAROBJ FROM TO"));
820 var = varobj_get_handle (argv[0]);
821 from = atoi (argv[1]);
824 varobj_set_child_range (var, from, to);