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);
66 ui_out_field_string (uiout, "value", val);
70 type = varobj_get_type (var);
73 ui_out_field_string (uiout, "type", type);
77 thread_id = varobj_get_thread_id (var);
79 ui_out_field_int (uiout, "thread-id", thread_id);
81 if (varobj_get_frozen (var))
82 ui_out_field_int (uiout, "frozen", 1);
84 display_hint = varobj_get_display_hint (var);
87 ui_out_field_string (uiout, "displayhint", display_hint);
91 if (varobj_pretty_printed_p (var))
92 ui_out_field_int (uiout, "dynamic", 1);
95 /* VAROBJ operations */
98 mi_cmd_var_create (char *command, char **argv, int argc)
100 CORE_ADDR frameaddr = 0;
105 struct cleanup *old_cleanups;
106 enum varobj_type var_type;
110 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
111 ...."); return MI_CMD_ERROR; */
112 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
115 name = xstrdup (argv[0]);
116 /* Add cleanup for name. Must be free_current_contents as
117 name can be reallocated */
118 old_cleanups = make_cleanup (free_current_contents, &name);
120 frame = xstrdup (argv[1]);
121 make_cleanup (xfree, frame);
123 expr = xstrdup (argv[2]);
124 make_cleanup (xfree, expr);
126 if (strcmp (name, "-") == 0)
129 name = varobj_gen_name ();
131 else if (!isalpha (*name))
132 error (_("mi_cmd_var_create: name of object must begin with a letter"));
134 if (strcmp (frame, "*") == 0)
135 var_type = USE_CURRENT_FRAME;
136 else if (strcmp (frame, "@") == 0)
137 var_type = USE_SELECTED_FRAME;
140 var_type = USE_SPECIFIED_FRAME;
141 frameaddr = string_to_core_addr (frame);
145 fprintf_unfiltered (gdb_stdlog,
146 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
147 name, frame, hex_string (frameaddr), expr);
149 var = varobj_create (name, expr, frameaddr, var_type);
152 error (_("mi_cmd_var_create: unable to create variable object"));
154 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
156 ui_out_field_int (uiout, "has_more", varobj_has_more (var, 0));
158 do_cleanups (old_cleanups);
162 mi_cmd_var_delete (char *command, char **argv, int argc)
167 int children_only_p = 0;
168 struct cleanup *old_cleanups;
170 if (argc < 1 || argc > 2)
171 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
173 name = xstrdup (argv[0]);
174 /* Add cleanup for name. Must be free_current_contents as
175 name can be reallocated */
176 old_cleanups = make_cleanup (free_current_contents, &name);
178 /* If we have one single argument it cannot be '-c' or any string
179 starting with '-'. */
182 if (strcmp (name, "-c") == 0)
183 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
185 error (_("mi_cmd_var_delete: Illegal variable object name"));
188 /* If we have 2 arguments they must be '-c' followed by a string
189 which would be the variable name. */
192 if (strcmp (name, "-c") != 0)
193 error (_("mi_cmd_var_delete: Invalid option."));
195 do_cleanups (old_cleanups);
196 name = xstrdup (argv[1]);
197 make_cleanup (free_current_contents, &name);
200 /* If we didn't error out, now NAME contains the name of the
203 var = varobj_get_handle (name);
205 numdel = varobj_delete (var, NULL, children_only_p);
207 ui_out_field_int (uiout, "ndeleted", numdel);
209 do_cleanups (old_cleanups);
212 /* Parse a string argument into a format value. */
214 static enum varobj_display_formats
215 mi_parse_format (const char *arg)
223 if (strncmp (arg, "natural", len) == 0)
224 return FORMAT_NATURAL;
225 else if (strncmp (arg, "binary", len) == 0)
226 return FORMAT_BINARY;
227 else if (strncmp (arg, "decimal", len) == 0)
228 return FORMAT_DECIMAL;
229 else if (strncmp (arg, "hexadecimal", len) == 0)
230 return FORMAT_HEXADECIMAL;
231 else if (strncmp (arg, "octal", len) == 0)
235 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
239 mi_cmd_var_set_format (char *command, char **argv, int argc)
241 enum varobj_display_formats format;
246 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
248 /* Get varobj handle, if a valid var obj name was specified */
249 var = varobj_get_handle (argv[0]);
251 format = mi_parse_format (argv[1]);
253 /* Set the format of VAR to given format */
254 varobj_set_display_format (var, format);
256 /* Report the new current format */
257 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
259 /* Report the value in the new format */
260 val = varobj_get_value (var);
261 ui_out_field_string (uiout, "value", val);
266 mi_cmd_var_set_visualizer (char *command, char **argv, int argc)
271 error ("Usage: NAME VISUALIZER_FUNCTION.");
273 var = varobj_get_handle (argv[0]);
276 error ("Variable object not found");
278 varobj_set_visualizer (var, argv[1]);
282 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
288 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
290 var = varobj_get_handle (argv[0]);
292 if (strcmp (argv[1], "0") == 0)
294 else if (strcmp (argv[1], "1") == 0)
297 error (_("Invalid flag value"));
299 varobj_set_frozen (var, frozen);
301 /* We don't automatically return the new value, or what varobjs got new
302 values during unfreezing. If this information is required, client
303 should call -var-update explicitly. */
308 mi_cmd_var_show_format (char *command, char **argv, int argc)
310 enum varobj_display_formats format;
314 error (_("mi_cmd_var_show_format: Usage: NAME."));
316 /* Get varobj handle, if a valid var obj name was specified */
317 var = varobj_get_handle (argv[0]);
319 format = varobj_get_display_format (var);
321 /* Report the current format */
322 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
326 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
331 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
333 /* Get varobj handle, if a valid var obj name was specified */
334 var = varobj_get_handle (argv[0]);
336 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
339 /* Parse a string argument into a print_values value. */
341 static enum print_values
342 mi_parse_values_option (const char *arg)
344 if (strcmp (arg, "0") == 0
345 || strcmp (arg, mi_no_values) == 0)
346 return PRINT_NO_VALUES;
347 else if (strcmp (arg, "1") == 0
348 || strcmp (arg, mi_all_values) == 0)
349 return PRINT_ALL_VALUES;
350 else if (strcmp (arg, "2") == 0
351 || strcmp (arg, mi_simple_values) == 0)
352 return PRINT_SIMPLE_VALUES;
354 error (_("Unknown value for PRINT_VALUES\n\
355 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
356 mi_no_values, mi_simple_values, mi_all_values);
359 /* Return 1 if given the argument PRINT_VALUES we should display
363 mi_print_value_p (struct varobj *var, enum print_values print_values)
367 if (print_values == PRINT_NO_VALUES)
370 if (print_values == PRINT_ALL_VALUES)
373 if (varobj_pretty_printed_p (var))
376 type = varobj_get_gdb_type (var);
381 type = check_typedef (type);
383 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
384 and that type is not a compound type. */
385 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
386 && TYPE_CODE (type) != TYPE_CODE_STRUCT
387 && TYPE_CODE (type) != TYPE_CODE_UNION);
392 mi_cmd_var_list_children (char *command, char **argv, int argc)
395 VEC(varobj_p) *children;
396 struct varobj *child;
397 enum print_values print_values;
402 if (argc < 1 || argc > 4)
403 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME [FROM TO]"));
405 /* Get varobj handle, if a valid var obj name was specified */
406 if (argc == 1 || argc == 3)
407 var = varobj_get_handle (argv[0]);
409 var = varobj_get_handle (argv[1]);
413 from = atoi (argv[argc - 2]);
414 to = atoi (argv[argc - 1]);
422 children = varobj_list_children (var, &from, &to);
423 ui_out_field_int (uiout, "numchild", to - from);
424 if (argc == 2 || argc == 4)
425 print_values = mi_parse_values_option (argv[0]);
427 print_values = PRINT_NO_VALUES;
429 display_hint = varobj_get_display_hint (var);
432 ui_out_field_string (uiout, "displayhint", display_hint);
433 xfree (display_hint);
438 struct cleanup *cleanup_children;
440 if (mi_version (uiout) == 1)
442 = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
445 = make_cleanup_ui_out_list_begin_end (uiout, "children");
447 ix < to && VEC_iterate (varobj_p, children, ix, child);
450 struct cleanup *cleanup_child;
452 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
453 print_varobj (child, print_values, 1 /* print expression */);
454 do_cleanups (cleanup_child);
456 do_cleanups (cleanup_children);
459 ui_out_field_int (uiout, "has_more", varobj_has_more (var, to));
463 mi_cmd_var_info_type (char *command, char **argv, int argc)
468 error (_("mi_cmd_var_info_type: Usage: NAME."));
470 /* Get varobj handle, if a valid var obj name was specified */
471 var = varobj_get_handle (argv[0]);
473 ui_out_field_string (uiout, "type", varobj_get_type (var));
477 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
483 error (_("Usage: NAME."));
485 /* Get varobj handle, if a valid var obj name was specified. */
486 var = varobj_get_handle (argv[0]);
488 path_expr = varobj_get_path_expr (var);
490 ui_out_field_string (uiout, "path_expr", path_expr);
494 mi_cmd_var_info_expression (char *command, char **argv, int argc)
496 enum varobj_languages lang;
500 error (_("mi_cmd_var_info_expression: Usage: NAME."));
502 /* Get varobj handle, if a valid var obj name was specified */
503 var = varobj_get_handle (argv[0]);
505 lang = varobj_get_language (var);
507 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
508 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
512 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
519 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
521 /* Get varobj handle, if a valid var obj name was specified */
522 var = varobj_get_handle (argv[0]);
524 attr = varobj_get_attributes (var);
525 /* FIXME: define masks for attributes */
526 if (attr & 0x00000001)
529 attstr = "noneditable";
531 ui_out_field_string (uiout, "attr", attstr);
535 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
539 enum varobj_display_formats format;
548 static struct mi_opt opts[] =
554 /* Parse arguments */
555 format = FORMAT_NATURAL;
560 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
561 opts, &optind, &optarg);
565 switch ((enum opt) opt)
569 error (_("Cannot specify format more than once"));
571 format = mi_parse_format (optarg);
578 error (_("Usage: [-f FORMAT] NAME"));
580 if (optind < argc - 1)
581 error (_("Garbage at end of command"));
583 /* Get varobj handle, if a valid var obj name was specified */
584 var = varobj_get_handle (argv[optind]);
588 char *val = varobj_get_formatted_value (var, format);
590 ui_out_field_string (uiout, "value", val);
595 char *val = varobj_get_value (var);
597 ui_out_field_string (uiout, "value", val);
603 mi_cmd_var_assign (char *command, char **argv, int argc)
606 char *expression, *val;
609 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
611 /* Get varobj handle, if a valid var obj name was specified */
612 var = varobj_get_handle (argv[0]);
614 if (!varobj_editable_p (var))
615 error (_("mi_cmd_var_assign: Variable object is not editable"));
617 expression = xstrdup (argv[1]);
619 if (!varobj_set_value (var, expression))
620 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
622 val = varobj_get_value (var);
623 ui_out_field_string (uiout, "value", val);
627 /* Type used for parameters passing to mi_cmd_var_update_iter. */
629 struct mi_cmd_var_update
632 enum print_values print_values;
635 /* Helper for mi_cmd_var_update - update each VAR. */
638 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
640 struct mi_cmd_var_update *data = data_pointer;
641 int thread_id, thread_stopped;
643 thread_id = varobj_get_thread_id (var);
645 if (thread_id == -1 && is_stopped (inferior_ptid))
649 struct thread_info *tp = find_thread_id (thread_id);
652 thread_stopped = is_stopped (tp->ptid);
658 if (!data->only_floating || varobj_floating_p (var))
659 varobj_update_one (var, data->print_values, 0 /* implicit */);
663 mi_cmd_var_update (char *command, char **argv, int argc)
665 struct cleanup *cleanup;
667 enum print_values print_values;
669 if (argc != 1 && argc != 2)
670 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
678 print_values = mi_parse_values_option (argv[0]);
680 print_values = PRINT_NO_VALUES;
682 if (mi_version (uiout) <= 1)
683 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
685 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
687 /* Check if the parameter is a "*" which means that we want
688 to update all variables */
690 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
692 struct mi_cmd_var_update data;
694 data.only_floating = *name == '@';
695 data.print_values = print_values;
697 /* varobj_update_one automatically updates all the children of VAROBJ.
698 Therefore update each VAROBJ only once by iterating only the root
701 all_root_varobjs (mi_cmd_var_update_iter, &data);
705 /* Get varobj handle, if a valid var obj name was specified */
706 struct varobj *var = varobj_get_handle (name);
708 varobj_update_one (var, print_values, 1 /* explicit */);
711 do_cleanups (cleanup);
714 /* Helper for mi_cmd_var_update(). */
717 varobj_update_one (struct varobj *var, enum print_values print_values,
720 struct cleanup *cleanup = NULL;
721 VEC (varobj_update_result) *changes;
722 varobj_update_result *r;
725 changes = varobj_update (&var, explicit);
727 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
732 if (mi_version (uiout) > 1)
733 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
734 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
738 case VAROBJ_IN_SCOPE:
739 if (mi_print_value_p (r->varobj, print_values))
741 char *val = varobj_get_value (r->varobj);
743 ui_out_field_string (uiout, "value", val);
746 ui_out_field_string (uiout, "in_scope", "true");
748 case VAROBJ_NOT_IN_SCOPE:
749 ui_out_field_string (uiout, "in_scope", "false");
752 ui_out_field_string (uiout, "in_scope", "invalid");
756 if (r->status != VAROBJ_INVALID)
759 ui_out_field_string (uiout, "type_changed", "true");
761 ui_out_field_string (uiout, "type_changed", "false");
765 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
767 if (r->type_changed || r->children_changed)
768 ui_out_field_int (uiout, "new_num_children",
769 varobj_get_num_children (r->varobj));
771 display_hint = varobj_get_display_hint (var);
774 ui_out_field_string (uiout, "displayhint", display_hint);
775 xfree (display_hint);
778 if (varobj_pretty_printed_p (var))
779 ui_out_field_int (uiout, "dynamic", 1);
781 varobj_get_child_range (r->varobj, &from, &to);
782 ui_out_field_int (uiout, "has_more",
783 varobj_has_more (r->varobj, to));
789 struct cleanup *cleanup;
791 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
792 for (j = 0; VEC_iterate (varobj_p, r->new, j, child); ++j)
794 struct cleanup *cleanup_child;
796 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
797 print_varobj (child, print_values, 1 /* print_expression */);
798 do_cleanups (cleanup_child);
801 do_cleanups (cleanup);
802 VEC_free (varobj_p, r->new);
803 r->new = NULL; /* Paranoia. */
806 if (mi_version (uiout) > 1)
807 do_cleanups (cleanup);
809 VEC_free (varobj_update_result, changes);
813 mi_cmd_enable_pretty_printing (char *command, char **argv, int argc)
816 error (_("mi_cmd_enable_pretty_printing: no arguments allowed"));
817 varobj_enable_pretty_printing ();
821 mi_cmd_var_set_update_range (char *command, char **argv, int argc)
827 error (_("mi_cmd_var_set_update_range: Usage: VAROBJ FROM TO"));
829 var = varobj_get_handle (argv[0]);
830 from = atoi (argv[1]);
833 varobj_set_child_range (var, from, to);