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"
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 type *type, 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,
53 struct type *gdb_type;
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 (varobj_get_gdb_type (var), print_values))
63 ui_out_field_string (uiout, "value", varobj_get_value (var));
65 type = varobj_get_type (var);
68 ui_out_field_string (uiout, "type", type);
72 thread_id = varobj_get_thread_id (var);
74 ui_out_field_int (uiout, "thread-id", thread_id);
76 if (varobj_get_frozen (var))
77 ui_out_field_int (uiout, "frozen", 1);
80 /* VAROBJ operations */
83 mi_cmd_var_create (char *command, char **argv, int argc)
85 CORE_ADDR frameaddr = 0;
90 struct cleanup *old_cleanups;
91 enum varobj_type var_type;
95 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
96 ...."); return MI_CMD_ERROR; */
97 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
100 name = xstrdup (argv[0]);
101 /* Add cleanup for name. Must be free_current_contents as
102 name can be reallocated */
103 old_cleanups = make_cleanup (free_current_contents, &name);
105 frame = xstrdup (argv[1]);
106 make_cleanup (xfree, frame);
108 expr = xstrdup (argv[2]);
109 make_cleanup (xfree, expr);
111 if (strcmp (name, "-") == 0)
114 name = varobj_gen_name ();
116 else if (!isalpha (*name))
117 error (_("mi_cmd_var_create: name of object must begin with a letter"));
119 if (strcmp (frame, "*") == 0)
120 var_type = USE_CURRENT_FRAME;
121 else if (strcmp (frame, "@") == 0)
122 var_type = USE_SELECTED_FRAME;
125 var_type = USE_SPECIFIED_FRAME;
126 frameaddr = string_to_core_addr (frame);
130 fprintf_unfiltered (gdb_stdlog,
131 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
132 name, frame, paddr (frameaddr), expr);
134 var = varobj_create (name, expr, frameaddr, var_type);
137 error (_("mi_cmd_var_create: unable to create variable object"));
139 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
141 do_cleanups (old_cleanups);
145 mi_cmd_var_delete (char *command, char **argv, int argc)
150 int children_only_p = 0;
151 struct cleanup *old_cleanups;
153 if (argc < 1 || argc > 2)
154 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
156 name = xstrdup (argv[0]);
157 /* Add cleanup for name. Must be free_current_contents as
158 name can be reallocated */
159 old_cleanups = make_cleanup (free_current_contents, &name);
161 /* If we have one single argument it cannot be '-c' or any string
162 starting with '-'. */
165 if (strcmp (name, "-c") == 0)
166 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
168 error (_("mi_cmd_var_delete: Illegal variable object name"));
171 /* If we have 2 arguments they must be '-c' followed by a string
172 which would be the variable name. */
175 if (strcmp (name, "-c") != 0)
176 error (_("mi_cmd_var_delete: Invalid option."));
178 do_cleanups (old_cleanups);
179 name = xstrdup (argv[1]);
180 make_cleanup (free_current_contents, &name);
183 /* If we didn't error out, now NAME contains the name of the
186 var = varobj_get_handle (name);
189 error (_("mi_cmd_var_delete: Variable object not found."));
191 numdel = varobj_delete (var, NULL, children_only_p);
193 ui_out_field_int (uiout, "ndeleted", numdel);
195 do_cleanups (old_cleanups);
198 /* Parse a string argument into a format value. */
200 static enum varobj_display_formats
201 mi_parse_format (const char *arg)
209 if (strncmp (arg, "natural", len) == 0)
210 return FORMAT_NATURAL;
211 else if (strncmp (arg, "binary", len) == 0)
212 return FORMAT_BINARY;
213 else if (strncmp (arg, "decimal", len) == 0)
214 return FORMAT_DECIMAL;
215 else if (strncmp (arg, "hexadecimal", len) == 0)
216 return FORMAT_HEXADECIMAL;
217 else if (strncmp (arg, "octal", len) == 0)
221 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
225 mi_cmd_var_set_format (char *command, char **argv, int argc)
227 enum varobj_display_formats format;
231 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
233 /* Get varobj handle, if a valid var obj name was specified */
234 var = varobj_get_handle (argv[0]);
237 error (_("mi_cmd_var_set_format: Variable object not found"));
239 format = mi_parse_format (argv[1]);
241 /* Set the format of VAR to given format */
242 varobj_set_display_format (var, format);
244 /* Report the new current format */
245 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
247 /* Report the value in the new format */
248 ui_out_field_string (uiout, "value", varobj_get_value (var));
252 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
258 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
260 var = varobj_get_handle (argv[0]);
262 error (_("Variable object not found"));
264 if (strcmp (argv[1], "0") == 0)
266 else if (strcmp (argv[1], "1") == 0)
269 error (_("Invalid flag value"));
271 varobj_set_frozen (var, frozen);
273 /* We don't automatically return the new value, or what varobjs got new
274 values during unfreezing. If this information is required, client
275 should call -var-update explicitly. */
280 mi_cmd_var_show_format (char *command, char **argv, int argc)
282 enum varobj_display_formats format;
286 error (_("mi_cmd_var_show_format: Usage: NAME."));
288 /* Get varobj handle, if a valid var obj name was specified */
289 var = varobj_get_handle (argv[0]);
291 error (_("mi_cmd_var_show_format: Variable object not found"));
293 format = varobj_get_display_format (var);
295 /* Report the current format */
296 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
300 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
305 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
307 /* Get varobj handle, if a valid var obj name was specified */
308 var = varobj_get_handle (argv[0]);
310 error (_("mi_cmd_var_info_num_children: Variable object not found"));
312 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
315 /* Parse a string argument into a print_values value. */
317 static enum print_values
318 mi_parse_values_option (const char *arg)
320 if (strcmp (arg, "0") == 0
321 || strcmp (arg, mi_no_values) == 0)
322 return PRINT_NO_VALUES;
323 else if (strcmp (arg, "1") == 0
324 || strcmp (arg, mi_all_values) == 0)
325 return PRINT_ALL_VALUES;
326 else if (strcmp (arg, "2") == 0
327 || strcmp (arg, mi_simple_values) == 0)
328 return PRINT_SIMPLE_VALUES;
330 error (_("Unknown value for PRINT_VALUES\n\
331 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
332 mi_no_values, mi_simple_values, mi_all_values);
335 /* Return 1 if given the argument PRINT_VALUES we should display
336 a value of type TYPE. */
339 mi_print_value_p (struct type *type, enum print_values print_values)
342 if (print_values == PRINT_NO_VALUES)
345 if (print_values == PRINT_ALL_VALUES)
352 type = check_typedef (type);
354 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
355 and that type is not a compound type. */
356 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
357 && TYPE_CODE (type) != TYPE_CODE_STRUCT
358 && TYPE_CODE (type) != TYPE_CODE_UNION);
363 mi_cmd_var_list_children (char *command, char **argv, int argc)
366 VEC(varobj_p) *children;
367 struct varobj *child;
368 struct cleanup *cleanup_children;
370 enum print_values print_values;
373 if (argc != 1 && argc != 2)
374 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
376 /* Get varobj handle, if a valid var obj name was specified */
378 var = varobj_get_handle (argv[0]);
380 var = varobj_get_handle (argv[1]);
382 error (_("Variable object not found"));
384 children = varobj_list_children (var);
385 ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children));
387 print_values = mi_parse_values_option (argv[0]);
389 print_values = PRINT_NO_VALUES;
391 if (VEC_length (varobj_p, children) == 0)
394 if (mi_version (uiout) == 1)
395 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
397 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
398 for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix)
400 struct cleanup *cleanup_child;
401 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
402 print_varobj (child, print_values, 1 /* print expression */);
403 do_cleanups (cleanup_child);
405 do_cleanups (cleanup_children);
409 mi_cmd_var_info_type (char *command, char **argv, int argc)
414 error (_("mi_cmd_var_info_type: Usage: NAME."));
416 /* Get varobj handle, if a valid var obj name was specified */
417 var = varobj_get_handle (argv[0]);
419 error (_("mi_cmd_var_info_type: Variable object not found"));
421 ui_out_field_string (uiout, "type", varobj_get_type (var));
425 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
431 error (_("Usage: NAME."));
433 /* Get varobj handle, if a valid var obj name was specified. */
434 var = varobj_get_handle (argv[0]);
436 error (_("Variable object not found"));
438 path_expr = varobj_get_path_expr (var);
440 ui_out_field_string (uiout, "path_expr", path_expr);
444 mi_cmd_var_info_expression (char *command, char **argv, int argc)
446 enum varobj_languages lang;
450 error (_("mi_cmd_var_info_expression: Usage: NAME."));
452 /* Get varobj handle, if a valid var obj name was specified */
453 var = varobj_get_handle (argv[0]);
455 error (_("mi_cmd_var_info_expression: Variable object not found"));
457 lang = varobj_get_language (var);
459 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
460 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
464 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
471 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
473 /* Get varobj handle, if a valid var obj name was specified */
474 var = varobj_get_handle (argv[0]);
476 error (_("mi_cmd_var_show_attributes: Variable object not found"));
478 attr = varobj_get_attributes (var);
479 /* FIXME: define masks for attributes */
480 if (attr & 0x00000001)
483 attstr = "noneditable";
485 ui_out_field_string (uiout, "attr", attstr);
489 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
493 enum varobj_display_formats format;
502 static struct mi_opt opts[] =
508 /* Parse arguments */
509 format = FORMAT_NATURAL;
514 int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg);
517 switch ((enum opt) opt)
521 error (_("Cannot specify format more than once"));
523 format = mi_parse_format (optarg);
530 error (_("Usage: [-f FORMAT] NAME"));
532 if (optind < argc - 1)
533 error (_("Garbage at end of command"));
535 /* Get varobj handle, if a valid var obj name was specified */
536 var = varobj_get_handle (argv[optind]);
538 error (_("Variable object not found"));
541 ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));
543 ui_out_field_string (uiout, "value", varobj_get_value (var));
547 mi_cmd_var_assign (char *command, char **argv, int argc)
553 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
555 /* Get varobj handle, if a valid var obj name was specified */
556 var = varobj_get_handle (argv[0]);
558 error (_("mi_cmd_var_assign: Variable object not found"));
560 if (!varobj_editable_p (var))
561 error (_("mi_cmd_var_assign: Variable object is not editable"));
563 expression = xstrdup (argv[1]);
565 if (!varobj_set_value (var, expression))
566 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
568 ui_out_field_string (uiout, "value", varobj_get_value (var));
572 mi_cmd_var_update (char *command, char **argv, int argc)
575 struct varobj **rootlist;
577 struct cleanup *cleanup;
580 enum print_values print_values;
582 if (argc != 1 && argc != 2)
583 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
591 print_values = mi_parse_values_option (argv[0]);
593 print_values = PRINT_NO_VALUES;
595 /* Check if the parameter is a "*" which means that we want
596 to update all variables */
598 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
600 nv = varobj_list (&rootlist);
601 cleanup = make_cleanup (xfree, rootlist);
602 if (mi_version (uiout) <= 1)
603 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
605 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
608 do_cleanups (cleanup);
614 int thread_id = varobj_get_thread_id (*cr);
615 int thread_stopped = 0;
616 if (thread_id == -1 && is_stopped (inferior_ptid))
620 struct thread_info *tp = find_thread_id (thread_id);
622 thread_stopped = is_stopped (tp->ptid);
627 if (*name == '*' || varobj_floating_p (*cr))
628 varobj_update_one (*cr, print_values, 0 /* implicit */);
631 do_cleanups (cleanup);
635 /* Get varobj handle, if a valid var obj name was specified */
636 var = varobj_get_handle (name);
638 error (_("mi_cmd_var_update: Variable object not found"));
640 if (mi_version (uiout) <= 1)
641 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
643 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
644 varobj_update_one (var, print_values, 1 /* explicit */);
645 do_cleanups (cleanup);
649 /* Helper for mi_cmd_var_update(). */
652 varobj_update_one (struct varobj *var, enum print_values print_values,
656 struct cleanup *cleanup = NULL;
657 VEC (varobj_update_result) *changes;
658 varobj_update_result *r;
661 changes = varobj_update (&var, explicit);
663 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
665 if (mi_version (uiout) > 1)
666 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
667 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
671 case VAROBJ_IN_SCOPE:
672 if (mi_print_value_p (varobj_get_gdb_type (r->varobj), print_values))
673 ui_out_field_string (uiout, "value", varobj_get_value (r->varobj));
674 ui_out_field_string (uiout, "in_scope", "true");
676 case VAROBJ_NOT_IN_SCOPE:
677 ui_out_field_string (uiout, "in_scope", "false");
680 ui_out_field_string (uiout, "in_scope", "invalid");
684 if (r->status != VAROBJ_INVALID)
687 ui_out_field_string (uiout, "type_changed", "true");
689 ui_out_field_string (uiout, "type_changed", "false");
694 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
695 ui_out_field_int (uiout, "new_num_children",
696 varobj_get_num_children (r->varobj));
699 if (mi_version (uiout) > 1)
700 do_cleanups (cleanup);
702 VEC_free (varobj_update_result, changes);