1 /* MI Command Set - varobj commands.
2 Copyright 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 /* Convenience macro for allocting typesafe memory. */
33 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
35 extern int varobjdebug; /* defined in varobj.c */
37 static int varobj_update_one (struct varobj *var);
39 /* VAROBJ operations */
42 mi_cmd_var_create (char *command, char **argv, int argc)
44 CORE_ADDR frameaddr = 0;
50 struct cleanup *old_cleanups;
51 enum varobj_type var_type;
55 /* xasprintf (&mi_error_message,
56 "mi_cmd_var_create: Usage: .");
57 return MI_CMD_ERROR; */
58 error ("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION.");
61 name = xstrdup (argv[0]);
62 /* Add cleanup for name. Must be free_current_contents as
63 name can be reallocated */
64 old_cleanups = make_cleanup (free_current_contents, &name);
66 frame = xstrdup (argv[1]);
67 old_cleanups = make_cleanup (xfree, frame);
69 expr = xstrdup (argv[2]);
71 if (strcmp (name, "-") == 0)
74 name = varobj_gen_name ();
76 else if (!isalpha (*name))
77 error ("mi_cmd_var_create: name of object must begin with a letter");
79 if (strcmp (frame, "*") == 0)
80 var_type = USE_CURRENT_FRAME;
81 else if (strcmp (frame, "@") == 0)
82 var_type = USE_SELECTED_FRAME;
85 var_type = USE_SPECIFIED_FRAME;
86 frameaddr = parse_and_eval_address (frame);
90 fprintf_unfiltered (gdb_stdlog,
91 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
92 name, frame, paddr (frameaddr), expr);
94 var = varobj_create (name, expr, frameaddr, var_type);
97 error ("mi_cmd_var_create: unable to create variable object");
99 ui_out_field_string (uiout, "name", name);
100 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
101 type = varobj_get_type (var);
103 ui_out_field_string (uiout, "type", "");
106 ui_out_field_string (uiout, "type", type);
110 do_cleanups (old_cleanups);
115 mi_cmd_var_delete (char *command, char **argv, int argc)
121 int children_only_p = 0;
122 struct cleanup *old_cleanups;
124 if (argc < 1 || argc > 2)
125 error ("mi_cmd_var_delete: Usage: [-c] EXPRESSION.");
127 name = xstrdup (argv[0]);
128 /* Add cleanup for name. Must be free_current_contents as
129 name can be reallocated */
130 old_cleanups = make_cleanup (free_current_contents, &name);
132 /* If we have one single argument it cannot be '-c' or any string
133 starting with '-'. */
136 if (strcmp (name, "-c") == 0)
137 error ("mi_cmd_var_delete: Missing required argument after '-c': variable object name");
139 error ("mi_cmd_var_delete: Illegal variable object name");
142 /* If we have 2 arguments they must be '-c' followed by a string
143 which would be the variable name. */
146 expr = xstrdup (argv[1]);
147 if (strcmp (name, "-c") != 0)
148 error ("mi_cmd_var_delete: Invalid option.");
151 name = xstrdup (expr);
155 /* If we didn't error out, now NAME contains the name of the
158 var = varobj_get_handle (name);
161 error ("mi_cmd_var_delete: Variable object not found.");
163 numdel = varobj_delete (var, NULL, children_only_p);
165 ui_out_field_int (uiout, "ndeleted", numdel);
167 do_cleanups (old_cleanups);
172 mi_cmd_var_set_format (char *command, char **argv, int argc)
174 enum varobj_display_formats format;
180 error ("mi_cmd_var_set_format: Usage: NAME FORMAT.");
182 /* Get varobj handle, if a valid var obj name was specified */
183 var = varobj_get_handle (argv[0]);
186 error ("mi_cmd_var_set_format: Variable object not found");
188 formspec = xstrdup (argv[1]);
189 if (formspec == NULL)
190 error ("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\"");
192 len = strlen (formspec);
194 if (STREQN (formspec, "natural", len))
195 format = FORMAT_NATURAL;
196 else if (STREQN (formspec, "binary", len))
197 format = FORMAT_BINARY;
198 else if (STREQN (formspec, "decimal", len))
199 format = FORMAT_DECIMAL;
200 else if (STREQN (formspec, "hexadecimal", len))
201 format = FORMAT_HEXADECIMAL;
202 else if (STREQN (formspec, "octal", len))
203 format = FORMAT_OCTAL;
205 error ("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\"");
207 /* Set the format of VAR to given format */
208 varobj_set_display_format (var, format);
210 /* Report the new current format */
211 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
216 mi_cmd_var_show_format (char *command, char **argv, int argc)
218 enum varobj_display_formats format;
222 error ("mi_cmd_var_show_format: Usage: NAME.");
224 /* Get varobj handle, if a valid var obj name was specified */
225 var = varobj_get_handle (argv[0]);
227 error ("mi_cmd_var_show_format: Variable object not found");
229 format = varobj_get_display_format (var);
231 /* Report the current format */
232 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
237 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
242 error ("mi_cmd_var_info_num_children: 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_info_num_children: Variable object not found");
249 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
254 mi_cmd_var_list_children (char *command, char **argv, int argc)
257 struct varobj **childlist;
263 error ("mi_cmd_var_list_children: Usage: NAME.");
265 /* Get varobj handle, if a valid var obj name was specified */
266 var = varobj_get_handle (argv[0]);
268 error ("mi_cmd_var_list_children: Variable object not found");
270 numchild = varobj_list_children (var, &childlist);
271 ui_out_field_int (uiout, "numchild", numchild);
276 ui_out_tuple_begin (uiout, "children");
280 ui_out_tuple_begin (uiout, "child");
281 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
282 ui_out_field_string (uiout, "exp", varobj_get_expression (*cc));
283 ui_out_field_int (uiout, "numchild", varobj_get_num_children (*cc));
284 type = varobj_get_type (*cc);
285 /* C++ pseudo-variables (public, private, protected) do not have a type */
287 ui_out_field_string (uiout, "type", varobj_get_type (*cc));
288 ui_out_tuple_end (uiout);
291 ui_out_tuple_end (uiout);
297 mi_cmd_var_info_type (char *command, char **argv, int argc)
302 error ("mi_cmd_var_info_type: Usage: NAME.");
304 /* Get varobj handle, if a valid var obj name was specified */
305 var = varobj_get_handle (argv[0]);
307 error ("mi_cmd_var_info_type: Variable object not found");
309 ui_out_field_string (uiout, "type", varobj_get_type (var));
314 mi_cmd_var_info_expression (char *command, char **argv, int argc)
316 enum varobj_languages lang;
320 error ("mi_cmd_var_info_expression: Usage: NAME.");
322 /* Get varobj handle, if a valid var obj name was specified */
323 var = varobj_get_handle (argv[0]);
325 error ("mi_cmd_var_info_expression: Variable object not found");
327 lang = varobj_get_language (var);
329 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
330 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
335 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
342 error ("mi_cmd_var_show_attributes: Usage: NAME.");
344 /* Get varobj handle, if a valid var obj name was specified */
345 var = varobj_get_handle (argv[0]);
347 error ("mi_cmd_var_show_attributes: Variable object not found");
349 attr = varobj_get_attributes (var);
350 /* FIXME: define masks for attributes */
351 if (attr & 0x00000001)
354 attstr = "noneditable";
356 ui_out_field_string (uiout, "attr", attstr);
361 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
366 error ("mi_cmd_var_evaluate_expression: Usage: NAME.");
368 /* Get varobj handle, if a valid var obj name was specified */
369 var = varobj_get_handle (argv[0]);
371 error ("mi_cmd_var_evaluate_expression: Variable object not found");
373 ui_out_field_string (uiout, "value", varobj_get_value (var));
378 mi_cmd_var_assign (char *command, char **argv, int argc)
384 error ("mi_cmd_var_assign: Usage: NAME EXPRESSION.");
386 /* Get varobj handle, if a valid var obj name was specified */
387 var = varobj_get_handle (argv[0]);
389 error ("mi_cmd_var_assign: Variable object not found");
391 /* FIXME: define masks for attributes */
392 if (!(varobj_get_attributes (var) & 0x00000001))
393 error ("mi_cmd_var_assign: Variable object is not editable");
395 expression = xstrdup (argv[1]);
397 if (!varobj_set_value (var, expression))
398 error ("mi_cmd_var_assign: Could not assign expression to varible object");
400 ui_out_field_string (uiout, "value", varobj_get_value (var));
405 mi_cmd_var_update (char *command, char **argv, int argc)
408 struct varobj **rootlist;
414 error ("mi_cmd_var_update: Usage: NAME.");
418 /* Check if the parameter is a "*" which means that we want
419 to update all variables */
421 if ((*name == '*') && (*(name + 1) == '\0'))
423 nv = varobj_list (&rootlist);
424 ui_out_tuple_begin (uiout, "changelist");
427 ui_out_tuple_end (uiout);
433 varobj_update_one (*cr);
437 ui_out_tuple_end (uiout);
441 /* Get varobj handle, if a valid var obj name was specified */
442 var = varobj_get_handle (name);
444 error ("mi_cmd_var_update: Variable object not found");
446 ui_out_tuple_begin (uiout, "changelist");
447 varobj_update_one (var);
448 ui_out_tuple_end (uiout);
453 /* Helper for mi_cmd_var_update() Returns 0 if the update for
454 the variable fails (usually because the variable is out of
455 scope), and 1 if it succeeds. */
458 varobj_update_one (struct varobj *var)
460 struct varobj **changelist;
464 nc = varobj_update (var, &changelist);
466 /* nc == 0 means that nothing has changed.
467 nc == -1 means that an error occured in updating the variable.
468 nc == -2 means the variable has changed type. */
474 ui_out_field_string (uiout, "name", varobj_get_objname(var));
475 ui_out_field_string (uiout, "in_scope", "false");
480 ui_out_field_string (uiout, "name", varobj_get_objname (var));
481 ui_out_field_string (uiout, "in_scope", "true");
482 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
483 ui_out_field_int (uiout, "new_num_children",
484 varobj_get_num_children(var));
492 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
493 ui_out_field_string (uiout, "in_scope", "true");
494 ui_out_field_string (uiout, "type_changed", "false");