1 /* MI Command Set - varobj commands.
3 Copyright 2000, 2002, 2004 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., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
31 #include "gdb_string.h"
33 extern int varobjdebug; /* defined in varobj.c */
35 static int varobj_update_one (struct varobj *var);
37 /* VAROBJ operations */
40 mi_cmd_var_create (char *command, char **argv, int argc)
42 CORE_ADDR frameaddr = 0;
48 struct cleanup *old_cleanups;
49 enum varobj_type var_type;
53 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
54 ...."); return MI_CMD_ERROR; */
55 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
58 name = xstrdup (argv[0]);
59 /* Add cleanup for name. Must be free_current_contents as
60 name can be reallocated */
61 old_cleanups = make_cleanup (free_current_contents, &name);
63 frame = xstrdup (argv[1]);
64 old_cleanups = make_cleanup (xfree, frame);
66 expr = xstrdup (argv[2]);
68 if (strcmp (name, "-") == 0)
71 name = varobj_gen_name ();
73 else if (!isalpha (*name))
74 error (_("mi_cmd_var_create: name of object must begin with a letter"));
76 if (strcmp (frame, "*") == 0)
77 var_type = USE_CURRENT_FRAME;
78 else if (strcmp (frame, "@") == 0)
79 var_type = USE_SELECTED_FRAME;
82 var_type = USE_SPECIFIED_FRAME;
83 frameaddr = string_to_core_addr (frame);
87 fprintf_unfiltered (gdb_stdlog,
88 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
89 name, frame, paddr (frameaddr), expr);
91 var = varobj_create (name, expr, frameaddr, var_type);
94 error (_("mi_cmd_var_create: unable to create variable object"));
96 ui_out_field_string (uiout, "name", name);
97 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
98 type = varobj_get_type (var);
100 ui_out_field_string (uiout, "type", "");
103 ui_out_field_string (uiout, "type", type);
107 do_cleanups (old_cleanups);
112 mi_cmd_var_delete (char *command, char **argv, int argc)
118 int children_only_p = 0;
119 struct cleanup *old_cleanups;
121 if (argc < 1 || argc > 2)
122 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
124 name = xstrdup (argv[0]);
125 /* Add cleanup for name. Must be free_current_contents as
126 name can be reallocated */
127 old_cleanups = make_cleanup (free_current_contents, &name);
129 /* If we have one single argument it cannot be '-c' or any string
130 starting with '-'. */
133 if (strcmp (name, "-c") == 0)
134 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
136 error (_("mi_cmd_var_delete: Illegal variable object name"));
139 /* If we have 2 arguments they must be '-c' followed by a string
140 which would be the variable name. */
143 expr = xstrdup (argv[1]);
144 if (strcmp (name, "-c") != 0)
145 error (_("mi_cmd_var_delete: Invalid option."));
148 name = xstrdup (expr);
152 /* If we didn't error out, now NAME contains the name of the
155 var = varobj_get_handle (name);
158 error (_("mi_cmd_var_delete: Variable object not found."));
160 numdel = varobj_delete (var, NULL, children_only_p);
162 ui_out_field_int (uiout, "ndeleted", numdel);
164 do_cleanups (old_cleanups);
169 mi_cmd_var_set_format (char *command, char **argv, int argc)
171 enum varobj_display_formats format;
177 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
179 /* Get varobj handle, if a valid var obj name was specified */
180 var = varobj_get_handle (argv[0]);
183 error (_("mi_cmd_var_set_format: Variable object not found"));
185 formspec = xstrdup (argv[1]);
186 if (formspec == NULL)
187 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
189 len = strlen (formspec);
191 if (strncmp (formspec, "natural", len) == 0)
192 format = FORMAT_NATURAL;
193 else if (strncmp (formspec, "binary", len) == 0)
194 format = FORMAT_BINARY;
195 else if (strncmp (formspec, "decimal", len) == 0)
196 format = FORMAT_DECIMAL;
197 else if (strncmp (formspec, "hexadecimal", len) == 0)
198 format = FORMAT_HEXADECIMAL;
199 else if (strncmp (formspec, "octal", len) == 0)
200 format = FORMAT_OCTAL;
202 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
204 /* Set the format of VAR to given format */
205 varobj_set_display_format (var, format);
207 /* Report the new current format */
208 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
213 mi_cmd_var_show_format (char *command, char **argv, int argc)
215 enum varobj_display_formats format;
219 error (_("mi_cmd_var_show_format: Usage: NAME."));
221 /* Get varobj handle, if a valid var obj name was specified */
222 var = varobj_get_handle (argv[0]);
224 error (_("mi_cmd_var_show_format: Variable object not found"));
226 format = varobj_get_display_format (var);
228 /* Report the current format */
229 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
234 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
239 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
241 /* Get varobj handle, if a valid var obj name was specified */
242 var = varobj_get_handle (argv[0]);
244 error (_("mi_cmd_var_info_num_children: Variable object not found"));
246 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
251 mi_cmd_var_list_children (char *command, char **argv, int argc)
254 struct varobj **childlist;
256 struct cleanup *cleanup_children;
259 enum print_values print_values;
261 if (argc != 1 && argc != 2)
262 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
264 /* Get varobj handle, if a valid var obj name was specified */
265 if (argc == 1) var = varobj_get_handle (argv[0]);
266 else var = varobj_get_handle (argv[1]);
268 error (_("Variable object not found"));
270 numchild = varobj_list_children (var, &childlist);
271 ui_out_field_int (uiout, "numchild", numchild);
273 if (strcmp (argv[0], "0") == 0
274 || strcmp (argv[0], "--no-values") == 0)
275 print_values = PRINT_NO_VALUES;
276 else if (strcmp (argv[0], "1") == 0
277 || strcmp (argv[0], "--all-values") == 0)
278 print_values = PRINT_ALL_VALUES;
280 error (_("Unknown value for PRINT_VALUES: must be: 0 or \"--no-values\", 1 or \"--all-values\""));
281 else print_values = PRINT_NO_VALUES;
286 if (mi_version (uiout) == 1)
287 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
289 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
293 struct cleanup *cleanup_child;
294 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
295 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
296 ui_out_field_string (uiout, "exp", varobj_get_expression (*cc));
297 ui_out_field_int (uiout, "numchild", varobj_get_num_children (*cc));
299 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
300 type = varobj_get_type (*cc);
301 /* C++ pseudo-variables (public, private, protected) do not have a type */
303 ui_out_field_string (uiout, "type", varobj_get_type (*cc));
304 do_cleanups (cleanup_child);
307 do_cleanups (cleanup_children);
313 mi_cmd_var_info_type (char *command, char **argv, int argc)
318 error (_("mi_cmd_var_info_type: Usage: NAME."));
320 /* Get varobj handle, if a valid var obj name was specified */
321 var = varobj_get_handle (argv[0]);
323 error (_("mi_cmd_var_info_type: Variable object not found"));
325 ui_out_field_string (uiout, "type", varobj_get_type (var));
330 mi_cmd_var_info_expression (char *command, char **argv, int argc)
332 enum varobj_languages lang;
336 error (_("mi_cmd_var_info_expression: Usage: NAME."));
338 /* Get varobj handle, if a valid var obj name was specified */
339 var = varobj_get_handle (argv[0]);
341 error (_("mi_cmd_var_info_expression: Variable object not found"));
343 lang = varobj_get_language (var);
345 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
346 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
351 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
358 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
360 /* Get varobj handle, if a valid var obj name was specified */
361 var = varobj_get_handle (argv[0]);
363 error (_("mi_cmd_var_show_attributes: Variable object not found"));
365 attr = varobj_get_attributes (var);
366 /* FIXME: define masks for attributes */
367 if (attr & 0x00000001)
370 attstr = "noneditable";
372 ui_out_field_string (uiout, "attr", attstr);
377 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
382 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
384 /* Get varobj handle, if a valid var obj name was specified */
385 var = varobj_get_handle (argv[0]);
387 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
389 ui_out_field_string (uiout, "value", varobj_get_value (var));
394 mi_cmd_var_assign (char *command, char **argv, int argc)
400 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
402 /* Get varobj handle, if a valid var obj name was specified */
403 var = varobj_get_handle (argv[0]);
405 error (_("mi_cmd_var_assign: Variable object not found"));
407 /* FIXME: define masks for attributes */
408 if (!(varobj_get_attributes (var) & 0x00000001))
409 error (_("mi_cmd_var_assign: Variable object is not editable"));
411 expression = xstrdup (argv[1]);
413 if (!varobj_set_value (var, expression))
414 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
416 ui_out_field_string (uiout, "value", varobj_get_value (var));
421 mi_cmd_var_update (char *command, char **argv, int argc)
424 struct varobj **rootlist;
426 struct cleanup *cleanup;
431 error (_("mi_cmd_var_update: Usage: NAME."));
435 /* Check if the parameter is a "*" which means that we want
436 to update all variables */
438 if ((*name == '*') && (*(name + 1) == '\0'))
440 nv = varobj_list (&rootlist);
441 if (mi_version (uiout) <= 1)
442 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
444 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
447 do_cleanups (cleanup);
453 varobj_update_one (*cr);
457 do_cleanups (cleanup);
461 /* Get varobj handle, if a valid var obj name was specified */
462 var = varobj_get_handle (name);
464 error (_("mi_cmd_var_update: Variable object not found"));
466 if (mi_version (uiout) <= 1)
467 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
469 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
470 varobj_update_one (var);
471 do_cleanups (cleanup);
476 /* Helper for mi_cmd_var_update() Returns 0 if the update for
477 the variable fails (usually because the variable is out of
478 scope), and 1 if it succeeds. */
481 varobj_update_one (struct varobj *var)
483 struct varobj **changelist;
485 struct cleanup *cleanup = NULL;
488 nc = varobj_update (&var, &changelist);
490 /* nc == 0 means that nothing has changed.
491 nc == -1 means that an error occured in updating the variable.
492 nc == -2 means the variable has changed type. */
498 if (mi_version (uiout) > 1)
499 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
500 ui_out_field_string (uiout, "name", varobj_get_objname(var));
501 ui_out_field_string (uiout, "in_scope", "false");
502 if (mi_version (uiout) > 1)
503 do_cleanups (cleanup);
508 if (mi_version (uiout) > 1)
509 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
510 ui_out_field_string (uiout, "name", varobj_get_objname (var));
511 ui_out_field_string (uiout, "in_scope", "true");
512 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
513 ui_out_field_int (uiout, "new_num_children",
514 varobj_get_num_children(var));
515 if (mi_version (uiout) > 1)
516 do_cleanups (cleanup);
524 if (mi_version (uiout) > 1)
525 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
526 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
527 ui_out_field_string (uiout, "in_scope", "true");
528 ui_out_field_string (uiout, "type_changed", "false");
529 if (mi_version (uiout) > 1)
530 do_cleanups (cleanup);