1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007 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 void 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_ALL_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;
324 enum print_values print_values;
326 if (argc != 1 && argc != 2)
327 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
329 /* Get varobj handle, if a valid var obj name was specified */
331 var = varobj_get_handle (argv[0]);
333 var = varobj_get_handle (argv[1]);
335 error (_("Variable object not found"));
337 numchild = varobj_list_children (var, &childlist);
338 ui_out_field_int (uiout, "numchild", numchild);
340 print_values = mi_parse_values_option (argv[0]);
342 print_values = PRINT_NO_VALUES;
347 if (mi_version (uiout) == 1)
348 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
350 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
354 struct cleanup *cleanup_child;
355 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
356 print_varobj (*cc, print_values, 1 /* print expression */);
358 do_cleanups (cleanup_child);
360 do_cleanups (cleanup_children);
366 mi_cmd_var_info_type (char *command, char **argv, int argc)
371 error (_("mi_cmd_var_info_type: Usage: NAME."));
373 /* Get varobj handle, if a valid var obj name was specified */
374 var = varobj_get_handle (argv[0]);
376 error (_("mi_cmd_var_info_type: Variable object not found"));
378 ui_out_field_string (uiout, "type", varobj_get_type (var));
383 mi_cmd_var_info_expression (char *command, char **argv, int argc)
385 enum varobj_languages lang;
389 error (_("mi_cmd_var_info_expression: Usage: NAME."));
391 /* Get varobj handle, if a valid var obj name was specified */
392 var = varobj_get_handle (argv[0]);
394 error (_("mi_cmd_var_info_expression: Variable object not found"));
396 lang = varobj_get_language (var);
398 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
399 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
404 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
411 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
413 /* Get varobj handle, if a valid var obj name was specified */
414 var = varobj_get_handle (argv[0]);
416 error (_("mi_cmd_var_show_attributes: Variable object not found"));
418 attr = varobj_get_attributes (var);
419 /* FIXME: define masks for attributes */
420 if (attr & 0x00000001)
423 attstr = "noneditable";
425 ui_out_field_string (uiout, "attr", attstr);
430 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
435 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
437 /* Get varobj handle, if a valid var obj name was specified */
438 var = varobj_get_handle (argv[0]);
440 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
442 ui_out_field_string (uiout, "value", varobj_get_value (var));
447 mi_cmd_var_assign (char *command, char **argv, int argc)
453 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
455 /* Get varobj handle, if a valid var obj name was specified */
456 var = varobj_get_handle (argv[0]);
458 error (_("mi_cmd_var_assign: Variable object not found"));
460 /* FIXME: define masks for attributes */
461 if (!(varobj_get_attributes (var) & 0x00000001))
462 error (_("mi_cmd_var_assign: Variable object is not editable"));
464 expression = xstrdup (argv[1]);
466 if (!varobj_set_value (var, expression))
467 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
469 ui_out_field_string (uiout, "value", varobj_get_value (var));
474 mi_cmd_var_update (char *command, char **argv, int argc)
477 struct varobj **rootlist;
479 struct cleanup *cleanup;
482 enum print_values print_values;
484 if (argc != 1 && argc != 2)
485 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
493 print_values = mi_parse_values_option (argv[0]);
495 print_values = PRINT_NO_VALUES;
497 /* Check if the parameter is a "*" which means that we want
498 to update all variables */
500 if ((*name == '*') && (*(name + 1) == '\0'))
502 nv = varobj_list (&rootlist);
503 cleanup = make_cleanup (xfree, rootlist);
504 if (mi_version (uiout) <= 1)
505 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
507 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
510 do_cleanups (cleanup);
516 varobj_update_one (*cr, print_values);
519 do_cleanups (cleanup);
523 /* Get varobj handle, if a valid var obj name was specified */
524 var = varobj_get_handle (name);
526 error (_("mi_cmd_var_update: Variable object not found"));
528 if (mi_version (uiout) <= 1)
529 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
531 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
532 varobj_update_one (var, print_values);
533 do_cleanups (cleanup);
538 /* Helper for mi_cmd_var_update(). */
541 varobj_update_one (struct varobj *var, enum print_values print_values)
543 struct varobj **changelist;
545 struct cleanup *cleanup = NULL;
548 nc = varobj_update (&var, &changelist);
550 /* nc >= 0 represents the number of changes reported into changelist.
551 nc < 0 means that an error occured or the the variable has
552 changed type (TYPE_CHANGED). */
558 if (mi_version (uiout) > 1)
559 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
560 ui_out_field_string (uiout, "name", varobj_get_objname(var));
566 ui_out_field_string (uiout, "in_scope", "false");
569 ui_out_field_string (uiout, "in_scope", "invalid");
572 ui_out_field_string (uiout, "in_scope", "true");
573 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
574 ui_out_field_int (uiout, "new_num_children",
575 varobj_get_num_children(var));
578 if (mi_version (uiout) > 1)
579 do_cleanups (cleanup);
586 if (mi_version (uiout) > 1)
587 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
588 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
589 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
590 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
591 ui_out_field_string (uiout, "in_scope", "true");
592 ui_out_field_string (uiout, "type_changed", "false");
593 if (mi_version (uiout) > 1)
594 do_cleanups (cleanup);