1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
31 #include "gdb_string.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 int varobjdebug; /* defined in varobj.c */
39 static int varobj_update_one (struct varobj *var,
40 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,
53 ui_out_field_string (uiout, "name", varobj_get_objname (var));
55 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
56 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
58 if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
59 ui_out_field_string (uiout, "value", varobj_get_value (var));
61 type = varobj_get_type (var);
64 ui_out_field_string (uiout, "type", type);
69 /* VAROBJ operations */
72 mi_cmd_var_create (char *command, char **argv, int argc)
74 CORE_ADDR frameaddr = 0;
79 struct cleanup *old_cleanups;
80 enum varobj_type var_type;
84 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
85 ...."); return MI_CMD_ERROR; */
86 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
89 name = xstrdup (argv[0]);
90 /* Add cleanup for name. Must be free_current_contents as
91 name can be reallocated */
92 old_cleanups = make_cleanup (free_current_contents, &name);
94 frame = xstrdup (argv[1]);
95 make_cleanup (xfree, frame);
97 expr = xstrdup (argv[2]);
98 make_cleanup (xfree, expr);
100 if (strcmp (name, "-") == 0)
103 name = varobj_gen_name ();
105 else if (!isalpha (*name))
106 error (_("mi_cmd_var_create: name of object must begin with a letter"));
108 if (strcmp (frame, "*") == 0)
109 var_type = USE_CURRENT_FRAME;
110 else if (strcmp (frame, "@") == 0)
111 var_type = USE_SELECTED_FRAME;
114 var_type = USE_SPECIFIED_FRAME;
115 frameaddr = string_to_core_addr (frame);
119 fprintf_unfiltered (gdb_stdlog,
120 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
121 name, frame, paddr (frameaddr), expr);
123 var = varobj_create (name, expr, frameaddr, var_type);
126 error (_("mi_cmd_var_create: unable to create variable object"));
128 print_varobj (var, PRINT_NO_VALUES, 0 /* don't print expression */);
130 do_cleanups (old_cleanups);
135 mi_cmd_var_delete (char *command, char **argv, int argc)
141 int children_only_p = 0;
142 struct cleanup *old_cleanups;
144 if (argc < 1 || argc > 2)
145 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
147 name = xstrdup (argv[0]);
148 /* Add cleanup for name. Must be free_current_contents as
149 name can be reallocated */
150 old_cleanups = make_cleanup (free_current_contents, &name);
152 /* If we have one single argument it cannot be '-c' or any string
153 starting with '-'. */
156 if (strcmp (name, "-c") == 0)
157 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
159 error (_("mi_cmd_var_delete: Illegal variable object name"));
162 /* If we have 2 arguments they must be '-c' followed by a string
163 which would be the variable name. */
166 expr = xstrdup (argv[1]);
167 if (strcmp (name, "-c") != 0)
168 error (_("mi_cmd_var_delete: Invalid option."));
171 name = xstrdup (expr);
175 /* If we didn't error out, now NAME contains the name of the
178 var = varobj_get_handle (name);
181 error (_("mi_cmd_var_delete: Variable object not found."));
183 numdel = varobj_delete (var, NULL, children_only_p);
185 ui_out_field_int (uiout, "ndeleted", numdel);
187 do_cleanups (old_cleanups);
192 mi_cmd_var_set_format (char *command, char **argv, int argc)
194 enum varobj_display_formats format;
200 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
202 /* Get varobj handle, if a valid var obj name was specified */
203 var = varobj_get_handle (argv[0]);
206 error (_("mi_cmd_var_set_format: Variable object not found"));
208 formspec = xstrdup (argv[1]);
209 if (formspec == NULL)
210 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
212 len = strlen (formspec);
214 if (strncmp (formspec, "natural", len) == 0)
215 format = FORMAT_NATURAL;
216 else if (strncmp (formspec, "binary", len) == 0)
217 format = FORMAT_BINARY;
218 else if (strncmp (formspec, "decimal", len) == 0)
219 format = FORMAT_DECIMAL;
220 else if (strncmp (formspec, "hexadecimal", len) == 0)
221 format = FORMAT_HEXADECIMAL;
222 else if (strncmp (formspec, "octal", len) == 0)
223 format = FORMAT_OCTAL;
225 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
227 /* Set the format of VAR to given format */
228 varobj_set_display_format (var, format);
230 /* Report the new current format */
231 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
236 mi_cmd_var_show_format (char *command, char **argv, int argc)
238 enum varobj_display_formats format;
242 error (_("mi_cmd_var_show_format: Usage: NAME."));
244 /* Get varobj handle, if a valid var obj name was specified */
245 var = varobj_get_handle (argv[0]);
247 error (_("mi_cmd_var_show_format: Variable object not found"));
249 format = varobj_get_display_format (var);
251 /* Report the current format */
252 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
257 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
262 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
264 /* Get varobj handle, if a valid var obj name was specified */
265 var = varobj_get_handle (argv[0]);
267 error (_("mi_cmd_var_info_num_children: Variable object not found"));
269 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
273 /* Parse a string argument into a print_values value. */
275 static enum print_values
276 mi_parse_values_option (const char *arg)
278 if (strcmp (arg, "0") == 0
279 || strcmp (arg, mi_no_values) == 0)
280 return PRINT_NO_VALUES;
281 else if (strcmp (arg, "1") == 0
282 || strcmp (arg, mi_all_values) == 0)
283 return PRINT_ALL_VALUES;
284 else if (strcmp (arg, "2") == 0
285 || strcmp (arg, mi_simple_values) == 0)
286 return PRINT_SIMPLE_VALUES;
288 error (_("Unknown value for PRINT_VALUES\n\
289 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
290 mi_no_values, mi_simple_values, mi_all_values);
293 /* Return 1 if given the argument PRINT_VALUES we should display
294 a value of type TYPE. */
297 mi_print_value_p (struct type *type, enum print_values print_values)
300 type = check_typedef (type);
302 if (print_values == PRINT_NO_VALUES)
305 if (print_values == PRINT_ALL_VALUES)
308 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
309 and that type is not a compound type. */
311 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
312 && TYPE_CODE (type) != TYPE_CODE_STRUCT
313 && TYPE_CODE (type) != TYPE_CODE_UNION);
317 mi_cmd_var_list_children (char *command, char **argv, int argc)
320 struct varobj **childlist;
322 struct cleanup *cleanup_children;
325 enum print_values print_values;
327 if (argc != 1 && argc != 2)
328 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
330 /* Get varobj handle, if a valid var obj name was specified */
332 var = varobj_get_handle (argv[0]);
334 var = varobj_get_handle (argv[1]);
336 error (_("Variable object not found"));
338 numchild = varobj_list_children (var, &childlist);
339 ui_out_field_int (uiout, "numchild", numchild);
341 print_values = mi_parse_values_option (argv[0]);
343 print_values = PRINT_NO_VALUES;
348 if (mi_version (uiout) == 1)
349 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
351 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
355 struct cleanup *cleanup_child;
356 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
357 print_varobj (*cc, print_values, 1 /* print expression */);
359 do_cleanups (cleanup_child);
361 do_cleanups (cleanup_children);
367 mi_cmd_var_info_type (char *command, char **argv, int argc)
372 error (_("mi_cmd_var_info_type: Usage: NAME."));
374 /* Get varobj handle, if a valid var obj name was specified */
375 var = varobj_get_handle (argv[0]);
377 error (_("mi_cmd_var_info_type: Variable object not found"));
379 ui_out_field_string (uiout, "type", varobj_get_type (var));
384 mi_cmd_var_info_expression (char *command, char **argv, int argc)
386 enum varobj_languages lang;
390 error (_("mi_cmd_var_info_expression: Usage: NAME."));
392 /* Get varobj handle, if a valid var obj name was specified */
393 var = varobj_get_handle (argv[0]);
395 error (_("mi_cmd_var_info_expression: Variable object not found"));
397 lang = varobj_get_language (var);
399 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
400 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
405 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
412 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
414 /* Get varobj handle, if a valid var obj name was specified */
415 var = varobj_get_handle (argv[0]);
417 error (_("mi_cmd_var_show_attributes: Variable object not found"));
419 attr = varobj_get_attributes (var);
420 /* FIXME: define masks for attributes */
421 if (attr & 0x00000001)
424 attstr = "noneditable";
426 ui_out_field_string (uiout, "attr", attstr);
431 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
436 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
438 /* Get varobj handle, if a valid var obj name was specified */
439 var = varobj_get_handle (argv[0]);
441 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
443 ui_out_field_string (uiout, "value", varobj_get_value (var));
448 mi_cmd_var_assign (char *command, char **argv, int argc)
454 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
456 /* Get varobj handle, if a valid var obj name was specified */
457 var = varobj_get_handle (argv[0]);
459 error (_("mi_cmd_var_assign: Variable object not found"));
461 /* FIXME: define masks for attributes */
462 if (!(varobj_get_attributes (var) & 0x00000001))
463 error (_("mi_cmd_var_assign: Variable object is not editable"));
465 expression = xstrdup (argv[1]);
467 if (!varobj_set_value (var, expression))
468 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
470 ui_out_field_string (uiout, "value", varobj_get_value (var));
475 mi_cmd_var_update (char *command, char **argv, int argc)
478 struct varobj **rootlist;
480 struct cleanup *cleanup;
483 enum print_values print_values;
485 if (argc != 1 && argc != 2)
486 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
494 print_values = mi_parse_values_option (argv[0]);
496 print_values = PRINT_NO_VALUES;
498 /* Check if the parameter is a "*" which means that we want
499 to update all variables */
501 if ((*name == '*') && (*(name + 1) == '\0'))
503 nv = varobj_list (&rootlist);
504 if (mi_version (uiout) <= 1)
505 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
507 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
510 do_cleanups (cleanup);
516 varobj_update_one (*cr, print_values);
520 do_cleanups (cleanup);
524 /* Get varobj handle, if a valid var obj name was specified */
525 var = varobj_get_handle (name);
527 error (_("mi_cmd_var_update: Variable object not found"));
529 if (mi_version (uiout) <= 1)
530 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
532 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
533 varobj_update_one (var, print_values);
534 do_cleanups (cleanup);
539 /* Helper for mi_cmd_var_update() Returns 0 if the update for
540 the variable fails (usually because the variable is out of
541 scope), and 1 if it succeeds. */
544 varobj_update_one (struct varobj *var, enum print_values print_values)
546 struct varobj **changelist;
548 struct cleanup *cleanup = NULL;
551 nc = varobj_update (&var, &changelist);
553 /* nc == 0 means that nothing has changed.
554 nc == -1 means that an error occured in updating the variable.
555 nc == -2 means the variable has changed type. */
561 if (mi_version (uiout) > 1)
562 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
563 ui_out_field_string (uiout, "name", varobj_get_objname(var));
564 ui_out_field_string (uiout, "in_scope", "false");
565 if (mi_version (uiout) > 1)
566 do_cleanups (cleanup);
571 if (mi_version (uiout) > 1)
572 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
573 ui_out_field_string (uiout, "name", varobj_get_objname (var));
574 ui_out_field_string (uiout, "in_scope", "true");
575 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
576 ui_out_field_int (uiout, "new_num_children",
577 varobj_get_num_children(var));
578 if (mi_version (uiout) > 1)
579 do_cleanups (cleanup);
587 if (mi_version (uiout) > 1)
588 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
589 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
590 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
591 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
592 ui_out_field_string (uiout, "in_scope", "true");
593 ui_out_field_string (uiout, "type_changed", "false");
594 if (mi_version (uiout) > 1)
595 do_cleanups (cleanup);