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"
32 const char mi_no_values[] = "--no-values";
33 const char mi_simple_values[] = "--simple-values";
34 const char mi_all_values[] = "--all-values";
36 extern int varobjdebug; /* defined in varobj.c. */
38 static void varobj_update_one (struct varobj *var,
39 enum print_values print_values,
42 static int mi_print_value_p (struct type *type, 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. */
48 print_varobj (struct varobj *var, enum print_values print_values,
51 struct type *gdb_type;
54 ui_out_field_string (uiout, "name", varobj_get_objname (var));
56 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
57 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
59 if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
60 ui_out_field_string (uiout, "value", varobj_get_value (var));
62 type = varobj_get_type (var);
65 ui_out_field_string (uiout, "type", type);
69 if (varobj_get_frozen (var))
70 ui_out_field_int (uiout, "frozen", 1);
73 /* VAROBJ operations */
76 mi_cmd_var_create (char *command, char **argv, int argc)
78 CORE_ADDR frameaddr = 0;
83 struct cleanup *old_cleanups;
84 enum varobj_type var_type;
88 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
89 ...."); return MI_CMD_ERROR; */
90 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
93 name = xstrdup (argv[0]);
94 /* Add cleanup for name. Must be free_current_contents as
95 name can be reallocated */
96 old_cleanups = make_cleanup (free_current_contents, &name);
98 frame = xstrdup (argv[1]);
99 make_cleanup (xfree, frame);
101 expr = xstrdup (argv[2]);
102 make_cleanup (xfree, expr);
104 if (strcmp (name, "-") == 0)
107 name = varobj_gen_name ();
109 else if (!isalpha (*name))
110 error (_("mi_cmd_var_create: name of object must begin with a letter"));
112 if (strcmp (frame, "*") == 0)
113 var_type = USE_CURRENT_FRAME;
114 else if (strcmp (frame, "@") == 0)
115 var_type = USE_SELECTED_FRAME;
118 var_type = USE_SPECIFIED_FRAME;
119 frameaddr = string_to_core_addr (frame);
123 fprintf_unfiltered (gdb_stdlog,
124 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
125 name, frame, paddr (frameaddr), expr);
127 var = varobj_create (name, expr, frameaddr, var_type);
130 error (_("mi_cmd_var_create: unable to create variable object"));
132 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
134 do_cleanups (old_cleanups);
139 mi_cmd_var_delete (char *command, char **argv, int argc)
144 int children_only_p = 0;
145 struct cleanup *old_cleanups;
147 if (argc < 1 || argc > 2)
148 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
150 name = xstrdup (argv[0]);
151 /* Add cleanup for name. Must be free_current_contents as
152 name can be reallocated */
153 old_cleanups = make_cleanup (free_current_contents, &name);
155 /* If we have one single argument it cannot be '-c' or any string
156 starting with '-'. */
159 if (strcmp (name, "-c") == 0)
160 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
162 error (_("mi_cmd_var_delete: Illegal variable object name"));
165 /* If we have 2 arguments they must be '-c' followed by a string
166 which would be the variable name. */
169 if (strcmp (name, "-c") != 0)
170 error (_("mi_cmd_var_delete: Invalid option."));
172 do_cleanups (old_cleanups);
173 name = xstrdup (argv[1]);
174 make_cleanup (free_current_contents, &name);
177 /* If we didn't error out, now NAME contains the name of the
180 var = varobj_get_handle (name);
183 error (_("mi_cmd_var_delete: Variable object not found."));
185 numdel = varobj_delete (var, NULL, children_only_p);
187 ui_out_field_int (uiout, "ndeleted", numdel);
189 do_cleanups (old_cleanups);
194 mi_cmd_var_set_format (char *command, char **argv, int argc)
196 enum varobj_display_formats format;
202 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
204 /* Get varobj handle, if a valid var obj name was specified */
205 var = varobj_get_handle (argv[0]);
208 error (_("mi_cmd_var_set_format: Variable object not found"));
211 if (formspec == NULL)
212 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
214 len = strlen (formspec);
216 if (strncmp (formspec, "natural", len) == 0)
217 format = FORMAT_NATURAL;
218 else if (strncmp (formspec, "binary", len) == 0)
219 format = FORMAT_BINARY;
220 else if (strncmp (formspec, "decimal", len) == 0)
221 format = FORMAT_DECIMAL;
222 else if (strncmp (formspec, "hexadecimal", len) == 0)
223 format = FORMAT_HEXADECIMAL;
224 else if (strncmp (formspec, "octal", len) == 0)
225 format = FORMAT_OCTAL;
227 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
229 /* Set the format of VAR to given format */
230 varobj_set_display_format (var, format);
232 /* Report the new current format */
233 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
238 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
244 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
246 var = varobj_get_handle (argv[0]);
248 error (_("Variable object not found"));
250 if (strcmp (argv[1], "0") == 0)
252 else if (strcmp (argv[1], "1") == 0)
255 error (_("Invalid flag value"));
257 varobj_set_frozen (var, frozen);
259 /* We don't automatically return the new value, or what varobjs got new
260 values during unfreezing. If this information is required, client
261 should call -var-update explicitly. */
267 mi_cmd_var_show_format (char *command, char **argv, int argc)
269 enum varobj_display_formats format;
273 error (_("mi_cmd_var_show_format: Usage: NAME."));
275 /* Get varobj handle, if a valid var obj name was specified */
276 var = varobj_get_handle (argv[0]);
278 error (_("mi_cmd_var_show_format: Variable object not found"));
280 format = varobj_get_display_format (var);
282 /* Report the current format */
283 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
288 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
293 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
295 /* Get varobj handle, if a valid var obj name was specified */
296 var = varobj_get_handle (argv[0]);
298 error (_("mi_cmd_var_info_num_children: Variable object not found"));
300 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
304 /* Parse a string argument into a print_values value. */
306 static enum print_values
307 mi_parse_values_option (const char *arg)
309 if (strcmp (arg, "0") == 0
310 || strcmp (arg, mi_no_values) == 0)
311 return PRINT_NO_VALUES;
312 else if (strcmp (arg, "1") == 0
313 || strcmp (arg, mi_all_values) == 0)
314 return PRINT_ALL_VALUES;
315 else if (strcmp (arg, "2") == 0
316 || strcmp (arg, mi_simple_values) == 0)
317 return PRINT_SIMPLE_VALUES;
319 error (_("Unknown value for PRINT_VALUES\n\
320 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
321 mi_no_values, mi_simple_values, mi_all_values);
324 /* Return 1 if given the argument PRINT_VALUES we should display
325 a value of type TYPE. */
328 mi_print_value_p (struct type *type, enum print_values print_values)
331 if (print_values == PRINT_NO_VALUES)
334 if (print_values == PRINT_ALL_VALUES)
341 type = check_typedef (type);
343 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
344 and that type is not a compound type. */
345 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
346 && TYPE_CODE (type) != TYPE_CODE_STRUCT
347 && TYPE_CODE (type) != TYPE_CODE_UNION);
352 mi_cmd_var_list_children (char *command, char **argv, int argc)
355 struct varobj **childlist;
357 struct cleanup *cleanup_children;
359 enum print_values print_values;
361 if (argc != 1 && argc != 2)
362 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
364 /* Get varobj handle, if a valid var obj name was specified */
366 var = varobj_get_handle (argv[0]);
368 var = varobj_get_handle (argv[1]);
370 error (_("Variable object not found"));
372 numchild = varobj_list_children (var, &childlist);
373 ui_out_field_int (uiout, "numchild", numchild);
375 print_values = mi_parse_values_option (argv[0]);
377 print_values = PRINT_NO_VALUES;
385 if (mi_version (uiout) == 1)
386 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
388 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
392 struct cleanup *cleanup_child;
393 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
394 print_varobj (*cc, print_values, 1 /* print expression */);
396 do_cleanups (cleanup_child);
398 do_cleanups (cleanup_children);
404 mi_cmd_var_info_type (char *command, char **argv, int argc)
409 error (_("mi_cmd_var_info_type: Usage: NAME."));
411 /* Get varobj handle, if a valid var obj name was specified */
412 var = varobj_get_handle (argv[0]);
414 error (_("mi_cmd_var_info_type: Variable object not found"));
416 ui_out_field_string (uiout, "type", varobj_get_type (var));
421 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
427 error (_("Usage: NAME."));
429 /* Get varobj handle, if a valid var obj name was specified. */
430 var = varobj_get_handle (argv[0]);
432 error (_("Variable object not found"));
434 path_expr = varobj_get_path_expr (var);
436 ui_out_field_string (uiout, "path_expr", path_expr);
442 mi_cmd_var_info_expression (char *command, char **argv, int argc)
444 enum varobj_languages lang;
448 error (_("mi_cmd_var_info_expression: Usage: NAME."));
450 /* Get varobj handle, if a valid var obj name was specified */
451 var = varobj_get_handle (argv[0]);
453 error (_("mi_cmd_var_info_expression: Variable object not found"));
455 lang = varobj_get_language (var);
457 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
458 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
463 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
470 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
472 /* Get varobj handle, if a valid var obj name was specified */
473 var = varobj_get_handle (argv[0]);
475 error (_("mi_cmd_var_show_attributes: Variable object not found"));
477 attr = varobj_get_attributes (var);
478 /* FIXME: define masks for attributes */
479 if (attr & 0x00000001)
482 attstr = "noneditable";
484 ui_out_field_string (uiout, "attr", attstr);
489 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
494 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
496 /* Get varobj handle, if a valid var obj name was specified */
497 var = varobj_get_handle (argv[0]);
499 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
501 ui_out_field_string (uiout, "value", varobj_get_value (var));
506 mi_cmd_var_assign (char *command, char **argv, int argc)
512 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
514 /* Get varobj handle, if a valid var obj name was specified */
515 var = varobj_get_handle (argv[0]);
517 error (_("mi_cmd_var_assign: Variable object not found"));
519 if (!varobj_editable_p (var))
520 error (_("mi_cmd_var_assign: Variable object is not editable"));
522 expression = xstrdup (argv[1]);
524 if (!varobj_set_value (var, expression))
525 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
527 ui_out_field_string (uiout, "value", varobj_get_value (var));
532 mi_cmd_var_update (char *command, char **argv, int argc)
535 struct varobj **rootlist;
537 struct cleanup *cleanup;
540 enum print_values print_values;
542 if (argc != 1 && argc != 2)
543 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
551 print_values = mi_parse_values_option (argv[0]);
553 print_values = PRINT_NO_VALUES;
555 /* Check if the parameter is a "*" which means that we want
556 to update all variables */
558 if ((*name == '*') && (*(name + 1) == '\0'))
560 nv = varobj_list (&rootlist);
561 cleanup = make_cleanup (xfree, rootlist);
562 if (mi_version (uiout) <= 1)
563 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
565 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
568 do_cleanups (cleanup);
574 varobj_update_one (*cr, print_values, 0 /* implicit */);
577 do_cleanups (cleanup);
581 /* Get varobj handle, if a valid var obj name was specified */
582 var = varobj_get_handle (name);
584 error (_("mi_cmd_var_update: Variable object not found"));
586 if (mi_version (uiout) <= 1)
587 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
589 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
590 varobj_update_one (var, print_values, 1 /* explicit */);
591 do_cleanups (cleanup);
596 /* Helper for mi_cmd_var_update(). */
599 varobj_update_one (struct varobj *var, enum print_values print_values,
602 struct varobj **changelist;
604 struct cleanup *cleanup = NULL;
607 nc = varobj_update (&var, &changelist, explicit);
609 /* nc >= 0 represents the number of changes reported into changelist.
610 nc < 0 means that an error occured or the the variable has
611 changed type (TYPE_CHANGED). */
617 if (mi_version (uiout) > 1)
618 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
619 ui_out_field_string (uiout, "name", varobj_get_objname(var));
624 ui_out_field_string (uiout, "in_scope", "false");
627 ui_out_field_string (uiout, "in_scope", "invalid");
630 ui_out_field_string (uiout, "in_scope", "true");
631 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
632 ui_out_field_int (uiout, "new_num_children",
633 varobj_get_num_children(var));
636 if (mi_version (uiout) > 1)
637 do_cleanups (cleanup);
644 if (mi_version (uiout) > 1)
645 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
646 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
647 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
648 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
649 ui_out_field_string (uiout, "in_scope", "true");
650 ui_out_field_string (uiout, "type_changed", "false");
651 if (mi_version (uiout) > 1)
652 do_cleanups (cleanup);