1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000, 2002, 2004-2005, 2007-2012 Free Software
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_string.h"
30 #include "mi-getopt.h"
31 #include "gdbthread.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 unsigned 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 varobj *var,
44 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. */
51 print_varobj (struct varobj *var, enum print_values print_values,
54 struct ui_out *uiout = current_uiout;
59 ui_out_field_string (uiout, "name", varobj_get_objname (var));
61 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
62 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
64 if (mi_print_value_p (var, print_values))
66 char *val = varobj_get_value (var);
68 ui_out_field_string (uiout, "value", val);
72 type = varobj_get_type (var);
75 ui_out_field_string (uiout, "type", type);
79 thread_id = varobj_get_thread_id (var);
81 ui_out_field_int (uiout, "thread-id", thread_id);
83 if (varobj_get_frozen (var))
84 ui_out_field_int (uiout, "frozen", 1);
86 display_hint = varobj_get_display_hint (var);
89 ui_out_field_string (uiout, "displayhint", display_hint);
93 if (varobj_pretty_printed_p (var))
94 ui_out_field_int (uiout, "dynamic", 1);
97 /* VAROBJ operations */
100 mi_cmd_var_create (char *command, char **argv, int argc)
102 struct ui_out *uiout = current_uiout;
103 CORE_ADDR frameaddr = 0;
108 struct cleanup *old_cleanups;
109 enum varobj_type var_type;
112 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
114 name = xstrdup (argv[0]);
115 /* Add cleanup for name. Must be free_current_contents as name can
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 (_("-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 (_("-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;
168 struct ui_out *uiout = current_uiout;
170 if (argc < 1 || argc > 2)
171 error (_("-var-delete: Usage: [-c] EXPRESSION."));
173 name = xstrdup (argv[0]);
174 /* Add cleanup for name. Must be free_current_contents as name can
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 (_("-var-delete: Missing required "
184 "argument after '-c': variable object name"));
186 error (_("-var-delete: Illegal variable object name"));
189 /* If we have 2 arguments they must be '-c' followed by a string
190 which would be the variable name. */
193 if (strcmp (name, "-c") != 0)
194 error (_("-var-delete: Invalid option."));
196 do_cleanups (old_cleanups);
197 name = xstrdup (argv[1]);
198 old_cleanups = make_cleanup (free_current_contents, &name);
201 /* If we didn't error out, now NAME contains the name of the
204 var = varobj_get_handle (name);
206 numdel = varobj_delete (var, NULL, children_only_p);
208 ui_out_field_int (uiout, "ndeleted", numdel);
210 do_cleanups (old_cleanups);
213 /* Parse a string argument into a format value. */
215 static enum varobj_display_formats
216 mi_parse_format (const char *arg)
224 if (strncmp (arg, "natural", len) == 0)
225 return FORMAT_NATURAL;
226 else if (strncmp (arg, "binary", len) == 0)
227 return FORMAT_BINARY;
228 else if (strncmp (arg, "decimal", len) == 0)
229 return FORMAT_DECIMAL;
230 else if (strncmp (arg, "hexadecimal", len) == 0)
231 return FORMAT_HEXADECIMAL;
232 else if (strncmp (arg, "octal", len) == 0)
236 error (_("Must specify the format as: \"natural\", "
237 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
241 mi_cmd_var_set_format (char *command, char **argv, int argc)
243 enum varobj_display_formats format;
246 struct ui_out *uiout = current_uiout;
249 error (_("-var-set-format: Usage: NAME FORMAT."));
251 /* Get varobj handle, if a valid var obj name was specified. */
252 var = varobj_get_handle (argv[0]);
254 format = mi_parse_format (argv[1]);
256 /* Set the format of VAR to the given format. */
257 varobj_set_display_format (var, format);
259 /* Report the new current format. */
260 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
262 /* Report the value in the new format. */
263 val = varobj_get_value (var);
264 ui_out_field_string (uiout, "value", val);
269 mi_cmd_var_set_visualizer (char *command, char **argv, int argc)
274 error (_("Usage: NAME VISUALIZER_FUNCTION."));
276 var = varobj_get_handle (argv[0]);
279 error (_("Variable object not found"));
281 varobj_set_visualizer (var, argv[1]);
285 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
291 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
293 var = varobj_get_handle (argv[0]);
295 if (strcmp (argv[1], "0") == 0)
297 else if (strcmp (argv[1], "1") == 0)
300 error (_("Invalid flag value"));
302 varobj_set_frozen (var, frozen);
304 /* We don't automatically return the new value, or what varobjs got
305 new values during unfreezing. If this information is required,
306 client should call -var-update explicitly. */
310 mi_cmd_var_show_format (char *command, char **argv, int argc)
312 struct ui_out *uiout = current_uiout;
313 enum varobj_display_formats format;
317 error (_("-var-show-format: Usage: NAME."));
319 /* Get varobj handle, if a valid var obj name was specified. */
320 var = varobj_get_handle (argv[0]);
322 format = varobj_get_display_format (var);
324 /* Report the current format. */
325 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
329 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
331 struct ui_out *uiout = current_uiout;
335 error (_("-var-info-num-children: Usage: NAME."));
337 /* Get varobj handle, if a valid var obj name was specified. */
338 var = varobj_get_handle (argv[0]);
340 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
343 /* Parse a string argument into a print_values value. */
345 static enum print_values
346 mi_parse_values_option (const char *arg)
348 if (strcmp (arg, "0") == 0
349 || strcmp (arg, mi_no_values) == 0)
350 return PRINT_NO_VALUES;
351 else if (strcmp (arg, "1") == 0
352 || strcmp (arg, mi_all_values) == 0)
353 return PRINT_ALL_VALUES;
354 else if (strcmp (arg, "2") == 0
355 || strcmp (arg, mi_simple_values) == 0)
356 return PRINT_SIMPLE_VALUES;
358 error (_("Unknown value for PRINT_VALUES\n\
359 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
360 mi_no_values, mi_simple_values, mi_all_values);
363 /* Return 1 if given the argument PRINT_VALUES we should display
367 mi_print_value_p (struct varobj *var, enum print_values print_values)
371 if (print_values == PRINT_NO_VALUES)
374 if (print_values == PRINT_ALL_VALUES)
377 if (varobj_pretty_printed_p (var))
380 type = varobj_get_gdb_type (var);
385 type = check_typedef (type);
387 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
388 and that type is not a compound type. */
389 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
390 && TYPE_CODE (type) != TYPE_CODE_STRUCT
391 && TYPE_CODE (type) != TYPE_CODE_UNION);
396 mi_cmd_var_list_children (char *command, char **argv, int argc)
398 struct ui_out *uiout = current_uiout;
400 VEC(varobj_p) *children;
401 struct varobj *child;
402 enum print_values print_values;
407 if (argc < 1 || argc > 4)
408 error (_("-var-list-children: Usage: "
409 "[PRINT_VALUES] NAME [FROM TO]"));
411 /* Get varobj handle, if a valid var obj name was specified. */
412 if (argc == 1 || argc == 3)
413 var = varobj_get_handle (argv[0]);
415 var = varobj_get_handle (argv[1]);
419 from = atoi (argv[argc - 2]);
420 to = atoi (argv[argc - 1]);
428 children = varobj_list_children (var, &from, &to);
429 ui_out_field_int (uiout, "numchild", to - from);
430 if (argc == 2 || argc == 4)
431 print_values = mi_parse_values_option (argv[0]);
433 print_values = PRINT_NO_VALUES;
435 display_hint = varobj_get_display_hint (var);
438 ui_out_field_string (uiout, "displayhint", display_hint);
439 xfree (display_hint);
444 struct cleanup *cleanup_children;
446 if (mi_version (uiout) == 1)
448 = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
451 = make_cleanup_ui_out_list_begin_end (uiout, "children");
453 ix < to && VEC_iterate (varobj_p, children, ix, child);
456 struct cleanup *cleanup_child;
458 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
459 print_varobj (child, print_values, 1 /* print expression */);
460 do_cleanups (cleanup_child);
462 do_cleanups (cleanup_children);
465 ui_out_field_int (uiout, "has_more", varobj_has_more (var, to));
469 mi_cmd_var_info_type (char *command, char **argv, int argc)
471 struct ui_out *uiout = current_uiout;
475 error (_("-var-info-type: Usage: NAME."));
477 /* Get varobj handle, if a valid var obj name was specified. */
478 var = varobj_get_handle (argv[0]);
480 ui_out_field_string (uiout, "type", varobj_get_type (var));
484 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
486 struct ui_out *uiout = current_uiout;
491 error (_("Usage: NAME."));
493 /* Get varobj handle, if a valid var obj name was specified. */
494 var = varobj_get_handle (argv[0]);
496 path_expr = varobj_get_path_expr (var);
498 ui_out_field_string (uiout, "path_expr", path_expr);
502 mi_cmd_var_info_expression (char *command, char **argv, int argc)
504 struct ui_out *uiout = current_uiout;
505 enum varobj_languages lang;
509 error (_("-var-info-expression: Usage: NAME."));
511 /* Get varobj handle, if a valid var obj name was specified. */
512 var = varobj_get_handle (argv[0]);
514 lang = varobj_get_language (var);
516 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
517 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
521 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
523 struct ui_out *uiout = current_uiout;
529 error (_("-var-show-attributes: Usage: NAME."));
531 /* Get varobj handle, if a valid var obj name was specified */
532 var = varobj_get_handle (argv[0]);
534 attr = varobj_get_attributes (var);
535 /* FIXME: define masks for attributes */
536 if (attr & 0x00000001)
539 attstr = "noneditable";
541 ui_out_field_string (uiout, "attr", attstr);
545 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
547 struct ui_out *uiout = current_uiout;
550 enum varobj_display_formats format;
559 static const struct mi_opt opts[] =
565 /* Parse arguments. */
566 format = FORMAT_NATURAL;
571 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
576 switch ((enum opt) opt)
580 error (_("Cannot specify format more than once"));
582 format = mi_parse_format (oarg);
589 error (_("Usage: [-f FORMAT] NAME"));
592 error (_("Garbage at end of command"));
594 /* Get varobj handle, if a valid var obj name was specified. */
595 var = varobj_get_handle (argv[oind]);
599 char *val = varobj_get_formatted_value (var, format);
601 ui_out_field_string (uiout, "value", val);
606 char *val = varobj_get_value (var);
608 ui_out_field_string (uiout, "value", val);
614 mi_cmd_var_assign (char *command, char **argv, int argc)
616 struct ui_out *uiout = current_uiout;
618 char *expression, *val;
621 error (_("-var-assign: Usage: NAME EXPRESSION."));
623 /* Get varobj handle, if a valid var obj name was specified. */
624 var = varobj_get_handle (argv[0]);
626 if (!varobj_editable_p (var))
627 error (_("-var-assign: Variable object is not editable"));
629 expression = xstrdup (argv[1]);
631 if (!varobj_set_value (var, expression))
632 error (_("-var-assign: Could not assign "
633 "expression to variable object"));
635 val = varobj_get_value (var);
636 ui_out_field_string (uiout, "value", val);
640 /* Type used for parameters passing to mi_cmd_var_update_iter. */
642 struct mi_cmd_var_update
645 enum print_values print_values;
648 /* Helper for mi_cmd_var_update - update each VAR. */
651 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
653 struct mi_cmd_var_update *data = data_pointer;
654 int thread_id, thread_stopped;
656 thread_id = varobj_get_thread_id (var);
658 if (thread_id == -1 && is_stopped (inferior_ptid))
662 struct thread_info *tp = find_thread_id (thread_id);
665 thread_stopped = is_stopped (tp->ptid);
671 && (!data->only_floating || varobj_floating_p (var)))
672 varobj_update_one (var, data->print_values, 0 /* implicit */);
676 mi_cmd_var_update (char *command, char **argv, int argc)
678 struct ui_out *uiout = current_uiout;
679 struct cleanup *cleanup;
681 enum print_values print_values;
683 if (argc != 1 && argc != 2)
684 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
692 print_values = mi_parse_values_option (argv[0]);
694 print_values = PRINT_NO_VALUES;
696 if (mi_version (uiout) <= 1)
697 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
699 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
701 /* Check if the parameter is a "*", which means that we want to
702 update all variables. */
704 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
706 struct mi_cmd_var_update data;
708 data.only_floating = (*name == '@');
709 data.print_values = print_values;
711 /* varobj_update_one automatically updates all the children of
712 VAROBJ. Therefore update each VAROBJ only once by iterating
713 only the root VAROBJs. */
715 all_root_varobjs (mi_cmd_var_update_iter, &data);
719 /* Get varobj handle, if a valid var obj name was specified. */
720 struct varobj *var = varobj_get_handle (name);
722 varobj_update_one (var, print_values, 1 /* explicit */);
725 do_cleanups (cleanup);
728 /* Helper for mi_cmd_var_update(). */
731 varobj_update_one (struct varobj *var, enum print_values print_values,
734 struct ui_out *uiout = current_uiout;
735 struct cleanup *cleanup = NULL;
736 VEC (varobj_update_result) *changes;
737 varobj_update_result *r;
740 changes = varobj_update (&var, explicit);
742 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
747 if (mi_version (uiout) > 1)
748 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
749 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
753 case VAROBJ_IN_SCOPE:
754 if (mi_print_value_p (r->varobj, print_values))
756 char *val = varobj_get_value (r->varobj);
758 ui_out_field_string (uiout, "value", val);
761 ui_out_field_string (uiout, "in_scope", "true");
763 case VAROBJ_NOT_IN_SCOPE:
764 ui_out_field_string (uiout, "in_scope", "false");
767 ui_out_field_string (uiout, "in_scope", "invalid");
771 if (r->status != VAROBJ_INVALID)
774 ui_out_field_string (uiout, "type_changed", "true");
776 ui_out_field_string (uiout, "type_changed", "false");
780 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
782 if (r->type_changed || r->children_changed)
783 ui_out_field_int (uiout, "new_num_children",
784 varobj_get_num_children (r->varobj));
786 display_hint = varobj_get_display_hint (var);
789 ui_out_field_string (uiout, "displayhint", display_hint);
790 xfree (display_hint);
793 if (varobj_pretty_printed_p (var))
794 ui_out_field_int (uiout, "dynamic", 1);
796 varobj_get_child_range (r->varobj, &from, &to);
797 ui_out_field_int (uiout, "has_more",
798 varobj_has_more (r->varobj, to));
804 struct cleanup *cleanup;
806 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
807 for (j = 0; VEC_iterate (varobj_p, r->new, j, child); ++j)
809 struct cleanup *cleanup_child;
812 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
813 print_varobj (child, print_values, 1 /* print_expression */);
814 do_cleanups (cleanup_child);
817 do_cleanups (cleanup);
818 VEC_free (varobj_p, r->new);
819 r->new = NULL; /* Paranoia. */
822 if (mi_version (uiout) > 1)
823 do_cleanups (cleanup);
825 VEC_free (varobj_update_result, changes);
829 mi_cmd_enable_pretty_printing (char *command, char **argv, int argc)
832 error (_("-enable-pretty-printing: no arguments allowed"));
834 varobj_enable_pretty_printing ();
838 mi_cmd_var_set_update_range (char *command, char **argv, int argc)
844 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
846 var = varobj_get_handle (argv[0]);
847 from = atoi (argv[1]);
850 varobj_set_child_range (var, from, to);