1 /* MI Command Set - varobj commands.
3 Copyright 2000, 2002 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 /* xasprintf (&mi_error_message,
54 "mi_cmd_var_create: Usage: .");
55 return MI_CMD_ERROR; */
56 error ("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION.");
59 name = xstrdup (argv[0]);
60 /* Add cleanup for name. Must be free_current_contents as
61 name can be reallocated */
62 old_cleanups = make_cleanup (free_current_contents, &name);
64 frame = xstrdup (argv[1]);
65 old_cleanups = make_cleanup (xfree, frame);
67 expr = xstrdup (argv[2]);
69 if (strcmp (name, "-") == 0)
72 name = varobj_gen_name ();
74 else if (!isalpha (*name))
75 error ("mi_cmd_var_create: name of object must begin with a letter");
77 if (strcmp (frame, "*") == 0)
78 var_type = USE_CURRENT_FRAME;
79 else if (strcmp (frame, "@") == 0)
80 var_type = USE_SELECTED_FRAME;
83 var_type = USE_SPECIFIED_FRAME;
84 frameaddr = parse_and_eval_address (frame);
88 fprintf_unfiltered (gdb_stdlog,
89 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
90 name, frame, paddr (frameaddr), expr);
92 var = varobj_create (name, expr, frameaddr, var_type);
95 error ("mi_cmd_var_create: unable to create variable object");
97 ui_out_field_string (uiout, "name", name);
98 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
99 type = varobj_get_type (var);
101 ui_out_field_string (uiout, "type", "");
104 ui_out_field_string (uiout, "type", type);
108 do_cleanups (old_cleanups);
113 mi_cmd_var_delete (char *command, char **argv, int argc)
119 int children_only_p = 0;
120 struct cleanup *old_cleanups;
122 if (argc < 1 || argc > 2)
123 error ("mi_cmd_var_delete: Usage: [-c] EXPRESSION.");
125 name = xstrdup (argv[0]);
126 /* Add cleanup for name. Must be free_current_contents as
127 name can be reallocated */
128 old_cleanups = make_cleanup (free_current_contents, &name);
130 /* If we have one single argument it cannot be '-c' or any string
131 starting with '-'. */
134 if (strcmp (name, "-c") == 0)
135 error ("mi_cmd_var_delete: Missing required argument after '-c': variable object name");
137 error ("mi_cmd_var_delete: Illegal variable object name");
140 /* If we have 2 arguments they must be '-c' followed by a string
141 which would be the variable name. */
144 expr = xstrdup (argv[1]);
145 if (strcmp (name, "-c") != 0)
146 error ("mi_cmd_var_delete: Invalid option.");
149 name = xstrdup (expr);
153 /* If we didn't error out, now NAME contains the name of the
156 var = varobj_get_handle (name);
159 error ("mi_cmd_var_delete: Variable object not found.");
161 numdel = varobj_delete (var, NULL, children_only_p);
163 ui_out_field_int (uiout, "ndeleted", numdel);
165 do_cleanups (old_cleanups);
170 mi_cmd_var_set_format (char *command, char **argv, int argc)
172 enum varobj_display_formats format;
178 error ("mi_cmd_var_set_format: Usage: NAME FORMAT.");
180 /* Get varobj handle, if a valid var obj name was specified */
181 var = varobj_get_handle (argv[0]);
184 error ("mi_cmd_var_set_format: Variable object not found");
186 formspec = xstrdup (argv[1]);
187 if (formspec == NULL)
188 error ("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\"");
190 len = strlen (formspec);
192 if (STREQN (formspec, "natural", len))
193 format = FORMAT_NATURAL;
194 else if (STREQN (formspec, "binary", len))
195 format = FORMAT_BINARY;
196 else if (STREQN (formspec, "decimal", len))
197 format = FORMAT_DECIMAL;
198 else if (STREQN (formspec, "hexadecimal", len))
199 format = FORMAT_HEXADECIMAL;
200 else if (STREQN (formspec, "octal", len))
201 format = FORMAT_OCTAL;
203 error ("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\"");
205 /* Set the format of VAR to given format */
206 varobj_set_display_format (var, format);
208 /* Report the new current format */
209 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
214 mi_cmd_var_show_format (char *command, char **argv, int argc)
216 enum varobj_display_formats format;
220 error ("mi_cmd_var_show_format: Usage: NAME.");
222 /* Get varobj handle, if a valid var obj name was specified */
223 var = varobj_get_handle (argv[0]);
225 error ("mi_cmd_var_show_format: Variable object not found");
227 format = varobj_get_display_format (var);
229 /* Report the current format */
230 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
235 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
240 error ("mi_cmd_var_info_num_children: Usage: NAME.");
242 /* Get varobj handle, if a valid var obj name was specified */
243 var = varobj_get_handle (argv[0]);
245 error ("mi_cmd_var_info_num_children: Variable object not found");
247 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
252 mi_cmd_var_list_children (char *command, char **argv, int argc)
255 struct varobj **childlist;
261 error ("mi_cmd_var_list_children: Usage: NAME.");
263 /* Get varobj handle, if a valid var obj name was specified */
264 var = varobj_get_handle (argv[0]);
266 error ("mi_cmd_var_list_children: Variable object not found");
268 numchild = varobj_list_children (var, &childlist);
269 ui_out_field_int (uiout, "numchild", numchild);
274 ui_out_tuple_begin (uiout, "children");
278 ui_out_tuple_begin (uiout, "child");
279 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
280 ui_out_field_string (uiout, "exp", varobj_get_expression (*cc));
281 ui_out_field_int (uiout, "numchild", varobj_get_num_children (*cc));
282 type = varobj_get_type (*cc);
283 /* C++ pseudo-variables (public, private, protected) do not have a type */
285 ui_out_field_string (uiout, "type", varobj_get_type (*cc));
286 ui_out_tuple_end (uiout);
289 ui_out_tuple_end (uiout);
295 mi_cmd_var_info_type (char *command, char **argv, int argc)
300 error ("mi_cmd_var_info_type: Usage: NAME.");
302 /* Get varobj handle, if a valid var obj name was specified */
303 var = varobj_get_handle (argv[0]);
305 error ("mi_cmd_var_info_type: Variable object not found");
307 ui_out_field_string (uiout, "type", varobj_get_type (var));
312 mi_cmd_var_info_expression (char *command, char **argv, int argc)
314 enum varobj_languages lang;
318 error ("mi_cmd_var_info_expression: 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_expression: Variable object not found");
325 lang = varobj_get_language (var);
327 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
328 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
333 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
340 error ("mi_cmd_var_show_attributes: Usage: NAME.");
342 /* Get varobj handle, if a valid var obj name was specified */
343 var = varobj_get_handle (argv[0]);
345 error ("mi_cmd_var_show_attributes: Variable object not found");
347 attr = varobj_get_attributes (var);
348 /* FIXME: define masks for attributes */
349 if (attr & 0x00000001)
352 attstr = "noneditable";
354 ui_out_field_string (uiout, "attr", attstr);
359 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
364 error ("mi_cmd_var_evaluate_expression: Usage: NAME.");
366 /* Get varobj handle, if a valid var obj name was specified */
367 var = varobj_get_handle (argv[0]);
369 error ("mi_cmd_var_evaluate_expression: Variable object not found");
371 ui_out_field_string (uiout, "value", varobj_get_value (var));
376 mi_cmd_var_assign (char *command, char **argv, int argc)
382 error ("mi_cmd_var_assign: Usage: NAME EXPRESSION.");
384 /* Get varobj handle, if a valid var obj name was specified */
385 var = varobj_get_handle (argv[0]);
387 error ("mi_cmd_var_assign: Variable object not found");
389 /* FIXME: define masks for attributes */
390 if (!(varobj_get_attributes (var) & 0x00000001))
391 error ("mi_cmd_var_assign: Variable object is not editable");
393 expression = xstrdup (argv[1]);
395 if (!varobj_set_value (var, expression))
396 error ("mi_cmd_var_assign: Could not assign expression to varible object");
398 ui_out_field_string (uiout, "value", varobj_get_value (var));
403 mi_cmd_var_update (char *command, char **argv, int argc)
406 struct varobj **rootlist;
408 struct cleanup *cleanup;
413 error ("mi_cmd_var_update: Usage: NAME.");
417 /* Check if the parameter is a "*" which means that we want
418 to update all variables */
420 if ((*name == '*') && (*(name + 1) == '\0'))
422 nv = varobj_list (&rootlist);
423 if (mi_version (uiout) <= 1)
424 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
426 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
429 do_cleanups (cleanup);
435 varobj_update_one (*cr);
439 do_cleanups (cleanup);
443 /* Get varobj handle, if a valid var obj name was specified */
444 var = varobj_get_handle (name);
446 error ("mi_cmd_var_update: Variable object not found");
448 if (mi_version (uiout) <= 1)
449 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
451 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
452 varobj_update_one (var);
453 do_cleanups (cleanup);
458 /* Helper for mi_cmd_var_update() Returns 0 if the update for
459 the variable fails (usually because the variable is out of
460 scope), and 1 if it succeeds. */
463 varobj_update_one (struct varobj *var)
465 struct varobj **changelist;
467 struct cleanup *cleanup = NULL;
470 nc = varobj_update (&var, &changelist);
472 /* nc == 0 means that nothing has changed.
473 nc == -1 means that an error occured in updating the variable.
474 nc == -2 means the variable has changed type. */
480 if (mi_version (uiout) > 1)
481 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
482 ui_out_field_string (uiout, "name", varobj_get_objname(var));
483 ui_out_field_string (uiout, "in_scope", "false");
484 if (mi_version (uiout) > 1)
485 do_cleanups (cleanup);
490 if (mi_version (uiout) > 1)
491 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
492 ui_out_field_string (uiout, "name", varobj_get_objname (var));
493 ui_out_field_string (uiout, "in_scope", "true");
494 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
495 ui_out_field_int (uiout, "new_num_children",
496 varobj_get_num_children(var));
497 if (mi_version (uiout) > 1)
498 do_cleanups (cleanup);
506 if (mi_version (uiout) > 1)
507 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
508 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
509 ui_out_field_string (uiout, "in_scope", "true");
510 ui_out_field_string (uiout, "type_changed", "false");
511 if (mi_version (uiout) > 1)
512 do_cleanups (cleanup);