1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions (a Red Hat company).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "mi-getopt.h"
32 #include "gdbthread.h"
35 extern unsigned int varobjdebug; /* defined in varobj.c. */
37 static void varobj_update_one (struct varobj *var,
38 enum print_values print_values,
41 static int mi_print_value_p (struct varobj *var,
42 enum print_values print_values);
44 /* Print variable object VAR. The PRINT_VALUES parameter controls
45 if the value should be printed. The PRINT_EXPRESSION parameter
46 controls if the expression should be printed. */
49 print_varobj (struct varobj *var, enum print_values print_values,
52 struct ui_out *uiout = current_uiout;
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 struct ui_out *uiout = current_uiout;
101 CORE_ADDR frameaddr = 0;
106 struct cleanup *old_cleanups;
107 enum varobj_type var_type;
110 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
112 name = xstrdup (argv[0]);
113 /* Add cleanup for name. Must be free_current_contents as name can
115 old_cleanups = make_cleanup (free_current_contents, &name);
117 frame = xstrdup (argv[1]);
118 make_cleanup (xfree, frame);
120 expr = xstrdup (argv[2]);
121 make_cleanup (xfree, expr);
123 if (strcmp (name, "-") == 0)
126 name = varobj_gen_name ();
128 else if (!isalpha (*name))
129 error (_("-var-create: name of object must begin with a letter"));
131 if (strcmp (frame, "*") == 0)
132 var_type = USE_CURRENT_FRAME;
133 else if (strcmp (frame, "@") == 0)
134 var_type = USE_SELECTED_FRAME;
137 var_type = USE_SPECIFIED_FRAME;
138 frameaddr = string_to_core_addr (frame);
142 fprintf_unfiltered (gdb_stdlog,
143 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
144 name, frame, hex_string (frameaddr), expr);
146 var = varobj_create (name, expr, frameaddr, var_type);
149 error (_("-var-create: unable to create variable object"));
151 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
153 ui_out_field_int (uiout, "has_more", varobj_has_more (var, 0));
155 do_cleanups (old_cleanups);
159 mi_cmd_var_delete (char *command, char **argv, int argc)
164 int children_only_p = 0;
165 struct cleanup *old_cleanups;
166 struct ui_out *uiout = current_uiout;
168 if (argc < 1 || argc > 2)
169 error (_("-var-delete: Usage: [-c] EXPRESSION."));
171 name = xstrdup (argv[0]);
172 /* Add cleanup for name. Must be free_current_contents as name can
174 old_cleanups = make_cleanup (free_current_contents, &name);
176 /* If we have one single argument it cannot be '-c' or any string
177 starting with '-'. */
180 if (strcmp (name, "-c") == 0)
181 error (_("-var-delete: Missing required "
182 "argument after '-c': variable object name"));
184 error (_("-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 (_("-var-delete: Invalid option."));
194 do_cleanups (old_cleanups);
195 name = xstrdup (argv[1]);
196 old_cleanups = 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\", "
235 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
239 mi_cmd_var_set_format (char *command, char **argv, int argc)
241 enum varobj_display_formats format;
244 struct ui_out *uiout = current_uiout;
247 error (_("-var-set-format: Usage: NAME FORMAT."));
249 /* Get varobj handle, if a valid var obj name was specified. */
250 var = varobj_get_handle (argv[0]);
252 format = mi_parse_format (argv[1]);
254 /* Set the format of VAR to the given format. */
255 varobj_set_display_format (var, format);
257 /* Report the new current format. */
258 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
260 /* Report the value in the new format. */
261 val = varobj_get_value (var);
262 ui_out_field_string (uiout, "value", val);
267 mi_cmd_var_set_visualizer (char *command, char **argv, int argc)
272 error (_("Usage: NAME VISUALIZER_FUNCTION."));
274 var = varobj_get_handle (argv[0]);
277 error (_("Variable object not found"));
279 varobj_set_visualizer (var, argv[1]);
283 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
289 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
291 var = varobj_get_handle (argv[0]);
293 if (strcmp (argv[1], "0") == 0)
295 else if (strcmp (argv[1], "1") == 0)
298 error (_("Invalid flag value"));
300 varobj_set_frozen (var, frozen);
302 /* We don't automatically return the new value, or what varobjs got
303 new values during unfreezing. If this information is required,
304 client should call -var-update explicitly. */
308 mi_cmd_var_show_format (char *command, char **argv, int argc)
310 struct ui_out *uiout = current_uiout;
311 enum varobj_display_formats format;
315 error (_("-var-show-format: Usage: NAME."));
317 /* Get varobj handle, if a valid var obj name was specified. */
318 var = varobj_get_handle (argv[0]);
320 format = varobj_get_display_format (var);
322 /* Report the current format. */
323 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
327 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
329 struct ui_out *uiout = current_uiout;
333 error (_("-var-info-num-children: Usage: NAME."));
335 /* Get varobj handle, if a valid var obj name was specified. */
336 var = varobj_get_handle (argv[0]);
338 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
341 /* Return 1 if given the argument PRINT_VALUES we should display
345 mi_print_value_p (struct varobj *var, enum print_values print_values)
349 if (print_values == PRINT_NO_VALUES)
352 if (print_values == PRINT_ALL_VALUES)
355 if (varobj_pretty_printed_p (var))
358 type = varobj_get_gdb_type (var);
363 type = check_typedef (type);
365 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
366 and that type is not a compound type. */
367 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
368 && TYPE_CODE (type) != TYPE_CODE_STRUCT
369 && TYPE_CODE (type) != TYPE_CODE_UNION);
374 mi_cmd_var_list_children (char *command, char **argv, int argc)
376 struct ui_out *uiout = current_uiout;
378 VEC(varobj_p) *children;
379 struct varobj *child;
380 enum print_values print_values;
385 if (argc < 1 || argc > 4)
386 error (_("-var-list-children: Usage: "
387 "[PRINT_VALUES] NAME [FROM TO]"));
389 /* Get varobj handle, if a valid var obj name was specified. */
390 if (argc == 1 || argc == 3)
391 var = varobj_get_handle (argv[0]);
393 var = varobj_get_handle (argv[1]);
397 from = atoi (argv[argc - 2]);
398 to = atoi (argv[argc - 1]);
406 children = varobj_list_children (var, &from, &to);
407 ui_out_field_int (uiout, "numchild", to - from);
408 if (argc == 2 || argc == 4)
409 print_values = mi_parse_print_values (argv[0]);
411 print_values = PRINT_NO_VALUES;
413 display_hint = varobj_get_display_hint (var);
416 ui_out_field_string (uiout, "displayhint", display_hint);
417 xfree (display_hint);
422 struct cleanup *cleanup_children;
424 if (mi_version (uiout) == 1)
426 = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
429 = make_cleanup_ui_out_list_begin_end (uiout, "children");
431 ix < to && VEC_iterate (varobj_p, children, ix, child);
434 struct cleanup *cleanup_child;
436 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
437 print_varobj (child, print_values, 1 /* print expression */);
438 do_cleanups (cleanup_child);
440 do_cleanups (cleanup_children);
443 ui_out_field_int (uiout, "has_more", varobj_has_more (var, to));
447 mi_cmd_var_info_type (char *command, char **argv, int argc)
449 struct ui_out *uiout = current_uiout;
453 error (_("-var-info-type: Usage: NAME."));
455 /* Get varobj handle, if a valid var obj name was specified. */
456 var = varobj_get_handle (argv[0]);
458 ui_out_field_string (uiout, "type", varobj_get_type (var));
462 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
464 struct ui_out *uiout = current_uiout;
469 error (_("Usage: NAME."));
471 /* Get varobj handle, if a valid var obj name was specified. */
472 var = varobj_get_handle (argv[0]);
474 path_expr = varobj_get_path_expr (var);
476 ui_out_field_string (uiout, "path_expr", path_expr);
480 mi_cmd_var_info_expression (char *command, char **argv, int argc)
482 struct ui_out *uiout = current_uiout;
483 const struct language_defn *lang;
487 error (_("-var-info-expression: Usage: NAME."));
489 /* Get varobj handle, if a valid var obj name was specified. */
490 var = varobj_get_handle (argv[0]);
492 lang = varobj_get_language (var);
494 ui_out_field_string (uiout, "lang", lang->la_natural_name);
495 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
499 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
501 struct ui_out *uiout = current_uiout;
507 error (_("-var-show-attributes: Usage: NAME."));
509 /* Get varobj handle, if a valid var obj name was specified */
510 var = varobj_get_handle (argv[0]);
512 attr = varobj_get_attributes (var);
513 /* FIXME: define masks for attributes */
514 if (attr & 0x00000001)
517 attstr = "noneditable";
519 ui_out_field_string (uiout, "attr", attstr);
523 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
525 struct ui_out *uiout = current_uiout;
528 enum varobj_display_formats format;
537 static const struct mi_opt opts[] =
543 /* Parse arguments. */
544 format = FORMAT_NATURAL;
549 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
554 switch ((enum opt) opt)
558 error (_("Cannot specify format more than once"));
560 format = mi_parse_format (oarg);
567 error (_("Usage: [-f FORMAT] NAME"));
570 error (_("Garbage at end of command"));
572 /* Get varobj handle, if a valid var obj name was specified. */
573 var = varobj_get_handle (argv[oind]);
577 char *val = varobj_get_formatted_value (var, format);
579 ui_out_field_string (uiout, "value", val);
584 char *val = varobj_get_value (var);
586 ui_out_field_string (uiout, "value", val);
592 mi_cmd_var_assign (char *command, char **argv, int argc)
594 struct ui_out *uiout = current_uiout;
596 char *expression, *val;
597 struct cleanup *cleanup;
600 error (_("-var-assign: Usage: NAME EXPRESSION."));
602 /* Get varobj handle, if a valid var obj name was specified. */
603 var = varobj_get_handle (argv[0]);
605 if (!varobj_editable_p (var))
606 error (_("-var-assign: Variable object is not editable"));
608 expression = xstrdup (argv[1]);
610 /* MI command '-var-assign' may write memory, so suppress memory
611 changed notification if it does. */
613 = make_cleanup_restore_integer (&mi_suppress_notification.memory);
614 mi_suppress_notification.memory = 1;
616 if (!varobj_set_value (var, expression))
617 error (_("-var-assign: Could not assign "
618 "expression to variable object"));
620 val = varobj_get_value (var);
621 ui_out_field_string (uiout, "value", val);
624 do_cleanups (cleanup);
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 && (!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 ui_out *uiout = current_uiout;
666 struct cleanup *cleanup;
668 enum print_values print_values;
670 if (argc != 1 && argc != 2)
671 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
679 print_values = mi_parse_print_values (argv[0]);
681 print_values = PRINT_NO_VALUES;
683 if (mi_version (uiout) <= 1)
684 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
686 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
688 /* Check if the parameter is a "*", which means that we want to
689 update all variables. */
691 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
693 struct mi_cmd_var_update data;
695 data.only_floating = (*name == '@');
696 data.print_values = print_values;
698 /* varobj_update_one automatically updates all the children of
699 VAROBJ. Therefore update each VAROBJ only once by iterating
700 only the root VAROBJs. */
702 all_root_varobjs (mi_cmd_var_update_iter, &data);
706 /* Get varobj handle, if a valid var obj name was specified. */
707 struct varobj *var = varobj_get_handle (name);
709 varobj_update_one (var, print_values, 1 /* explicit */);
712 do_cleanups (cleanup);
715 /* Helper for mi_cmd_var_update(). */
718 varobj_update_one (struct varobj *var, enum print_values print_values,
721 struct ui_out *uiout = current_uiout;
722 VEC (varobj_update_result) *changes;
723 varobj_update_result *r;
726 changes = varobj_update (&var, explicit);
728 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
732 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
734 if (mi_version (uiout) > 1)
735 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
736 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
740 case VAROBJ_IN_SCOPE:
741 if (mi_print_value_p (r->varobj, print_values))
743 char *val = varobj_get_value (r->varobj);
745 ui_out_field_string (uiout, "value", val);
748 ui_out_field_string (uiout, "in_scope", "true");
750 case VAROBJ_NOT_IN_SCOPE:
751 ui_out_field_string (uiout, "in_scope", "false");
754 ui_out_field_string (uiout, "in_scope", "invalid");
758 if (r->status != VAROBJ_INVALID)
761 ui_out_field_string (uiout, "type_changed", "true");
763 ui_out_field_string (uiout, "type_changed", "false");
767 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
769 if (r->type_changed || r->children_changed)
770 ui_out_field_int (uiout, "new_num_children",
771 varobj_get_num_children (r->varobj));
773 display_hint = varobj_get_display_hint (r->varobj);
776 ui_out_field_string (uiout, "displayhint", display_hint);
777 xfree (display_hint);
780 if (varobj_pretty_printed_p (r->varobj))
781 ui_out_field_int (uiout, "dynamic", 1);
783 varobj_get_child_range (r->varobj, &from, &to);
784 ui_out_field_int (uiout, "has_more",
785 varobj_has_more (r->varobj, to));
791 struct cleanup *cleanup;
793 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
794 for (j = 0; VEC_iterate (varobj_p, r->new, j, child); ++j)
796 struct cleanup *cleanup_child;
799 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
800 print_varobj (child, print_values, 1 /* print_expression */);
801 do_cleanups (cleanup_child);
804 do_cleanups (cleanup);
805 VEC_free (varobj_p, r->new);
806 r->new = NULL; /* Paranoia. */
809 do_cleanups (cleanup);
811 VEC_free (varobj_update_result, changes);
815 mi_cmd_enable_pretty_printing (char *command, char **argv, int argc)
818 error (_("-enable-pretty-printing: no arguments allowed"));
820 varobj_enable_pretty_printing ();
824 mi_cmd_var_set_update_range (char *command, char **argv, int argc)
830 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
832 var = varobj_get_handle (argv[0]);
833 from = atoi (argv[1]);
836 varobj_set_child_range (var, from, to);