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 /* VAROBJ operations */
45 mi_cmd_var_create (char *command
, char **argv
, int argc
)
47 CORE_ADDR frameaddr
= 0;
53 struct cleanup
*old_cleanups
;
54 enum varobj_type var_type
;
58 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
59 ...."); return MI_CMD_ERROR; */
60 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
63 name
= xstrdup (argv
[0]);
64 /* Add cleanup for name. Must be free_current_contents as
65 name can be reallocated */
66 old_cleanups
= make_cleanup (free_current_contents
, &name
);
68 frame
= xstrdup (argv
[1]);
69 make_cleanup (xfree
, frame
);
71 expr
= xstrdup (argv
[2]);
72 make_cleanup (xfree
, expr
);
74 if (strcmp (name
, "-") == 0)
77 name
= varobj_gen_name ();
79 else if (!isalpha (*name
))
80 error (_("mi_cmd_var_create: name of object must begin with a letter"));
82 if (strcmp (frame
, "*") == 0)
83 var_type
= USE_CURRENT_FRAME
;
84 else if (strcmp (frame
, "@") == 0)
85 var_type
= USE_SELECTED_FRAME
;
88 var_type
= USE_SPECIFIED_FRAME
;
89 frameaddr
= string_to_core_addr (frame
);
93 fprintf_unfiltered (gdb_stdlog
,
94 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
95 name
, frame
, paddr (frameaddr
), expr
);
97 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
100 error (_("mi_cmd_var_create: unable to create variable object"));
102 ui_out_field_string (uiout
, "name", name
);
103 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
104 type
= varobj_get_type (var
);
106 ui_out_field_string (uiout
, "type", "");
109 ui_out_field_string (uiout
, "type", type
);
113 do_cleanups (old_cleanups
);
118 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
124 int children_only_p
= 0;
125 struct cleanup
*old_cleanups
;
127 if (argc
< 1 || argc
> 2)
128 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
130 name
= xstrdup (argv
[0]);
131 /* Add cleanup for name. Must be free_current_contents as
132 name can be reallocated */
133 old_cleanups
= make_cleanup (free_current_contents
, &name
);
135 /* If we have one single argument it cannot be '-c' or any string
136 starting with '-'. */
139 if (strcmp (name
, "-c") == 0)
140 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
142 error (_("mi_cmd_var_delete: Illegal variable object name"));
145 /* If we have 2 arguments they must be '-c' followed by a string
146 which would be the variable name. */
149 expr
= xstrdup (argv
[1]);
150 if (strcmp (name
, "-c") != 0)
151 error (_("mi_cmd_var_delete: Invalid option."));
154 name
= xstrdup (expr
);
158 /* If we didn't error out, now NAME contains the name of the
161 var
= varobj_get_handle (name
);
164 error (_("mi_cmd_var_delete: Variable object not found."));
166 numdel
= varobj_delete (var
, NULL
, children_only_p
);
168 ui_out_field_int (uiout
, "ndeleted", numdel
);
170 do_cleanups (old_cleanups
);
175 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
177 enum varobj_display_formats format
;
183 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
185 /* Get varobj handle, if a valid var obj name was specified */
186 var
= varobj_get_handle (argv
[0]);
189 error (_("mi_cmd_var_set_format: Variable object not found"));
191 formspec
= xstrdup (argv
[1]);
192 if (formspec
== NULL
)
193 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
195 len
= strlen (formspec
);
197 if (strncmp (formspec
, "natural", len
) == 0)
198 format
= FORMAT_NATURAL
;
199 else if (strncmp (formspec
, "binary", len
) == 0)
200 format
= FORMAT_BINARY
;
201 else if (strncmp (formspec
, "decimal", len
) == 0)
202 format
= FORMAT_DECIMAL
;
203 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
204 format
= FORMAT_HEXADECIMAL
;
205 else if (strncmp (formspec
, "octal", len
) == 0)
206 format
= FORMAT_OCTAL
;
208 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
210 /* Set the format of VAR to given format */
211 varobj_set_display_format (var
, format
);
213 /* Report the new current format */
214 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
219 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
221 enum varobj_display_formats format
;
225 error (_("mi_cmd_var_show_format: Usage: NAME."));
227 /* Get varobj handle, if a valid var obj name was specified */
228 var
= varobj_get_handle (argv
[0]);
230 error (_("mi_cmd_var_show_format: Variable object not found"));
232 format
= varobj_get_display_format (var
);
234 /* Report the current format */
235 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
240 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
245 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
247 /* Get varobj handle, if a valid var obj name was specified */
248 var
= varobj_get_handle (argv
[0]);
250 error (_("mi_cmd_var_info_num_children: Variable object not found"));
252 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
256 /* Parse a string argument into a print_values value. */
258 static enum print_values
259 mi_parse_values_option (const char *arg
)
261 if (strcmp (arg
, "0") == 0
262 || strcmp (arg
, mi_no_values
) == 0)
263 return PRINT_NO_VALUES
;
264 else if (strcmp (arg
, "1") == 0
265 || strcmp (arg
, mi_all_values
) == 0)
266 return PRINT_ALL_VALUES
;
267 else if (strcmp (arg
, "2") == 0
268 || strcmp (arg
, mi_simple_values
) == 0)
269 return PRINT_SIMPLE_VALUES
;
271 error (_("Unknown value for PRINT_VALUES\n\
272 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
273 mi_no_values
, mi_simple_values
, mi_all_values
);
276 /* Return 1 if given the argument PRINT_VALUES we should display
277 a value of type TYPE. */
280 mi_print_value_p (struct type
*type
, enum print_values print_values
)
283 type
= check_typedef (type
);
285 if (print_values
== PRINT_NO_VALUES
)
288 if (print_values
== PRINT_ALL_VALUES
)
291 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
292 and that type is not a compound type. */
294 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
295 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
296 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
300 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
303 struct varobj
**childlist
;
305 struct cleanup
*cleanup_children
;
308 enum print_values print_values
;
310 if (argc
!= 1 && argc
!= 2)
311 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
313 /* Get varobj handle, if a valid var obj name was specified */
315 var
= varobj_get_handle (argv
[0]);
317 var
= varobj_get_handle (argv
[1]);
319 error (_("Variable object not found"));
321 numchild
= varobj_list_children (var
, &childlist
);
322 ui_out_field_int (uiout
, "numchild", numchild
);
324 print_values
= mi_parse_values_option (argv
[0]);
326 print_values
= PRINT_NO_VALUES
;
331 if (mi_version (uiout
) == 1)
332 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
334 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
338 struct cleanup
*cleanup_child
;
339 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
340 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
341 ui_out_field_string (uiout
, "exp", varobj_get_expression (*cc
));
342 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (*cc
));
343 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
344 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
345 type
= varobj_get_type (*cc
);
346 /* C++ pseudo-variables (public, private, protected) do not have a type */
348 ui_out_field_string (uiout
, "type", type
);
349 do_cleanups (cleanup_child
);
352 do_cleanups (cleanup_children
);
358 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
363 error (_("mi_cmd_var_info_type: Usage: NAME."));
365 /* Get varobj handle, if a valid var obj name was specified */
366 var
= varobj_get_handle (argv
[0]);
368 error (_("mi_cmd_var_info_type: Variable object not found"));
370 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
375 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
377 enum varobj_languages lang
;
381 error (_("mi_cmd_var_info_expression: Usage: NAME."));
383 /* Get varobj handle, if a valid var obj name was specified */
384 var
= varobj_get_handle (argv
[0]);
386 error (_("mi_cmd_var_info_expression: Variable object not found"));
388 lang
= varobj_get_language (var
);
390 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
391 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
396 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
403 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
405 /* Get varobj handle, if a valid var obj name was specified */
406 var
= varobj_get_handle (argv
[0]);
408 error (_("mi_cmd_var_show_attributes: Variable object not found"));
410 attr
= varobj_get_attributes (var
);
411 /* FIXME: define masks for attributes */
412 if (attr
& 0x00000001)
415 attstr
= "noneditable";
417 ui_out_field_string (uiout
, "attr", attstr
);
422 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
427 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
429 /* Get varobj handle, if a valid var obj name was specified */
430 var
= varobj_get_handle (argv
[0]);
432 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
434 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
439 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
445 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
447 /* Get varobj handle, if a valid var obj name was specified */
448 var
= varobj_get_handle (argv
[0]);
450 error (_("mi_cmd_var_assign: Variable object not found"));
452 /* FIXME: define masks for attributes */
453 if (!(varobj_get_attributes (var
) & 0x00000001))
454 error (_("mi_cmd_var_assign: Variable object is not editable"));
456 expression
= xstrdup (argv
[1]);
458 if (!varobj_set_value (var
, expression
))
459 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
461 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
466 mi_cmd_var_update (char *command
, char **argv
, int argc
)
469 struct varobj
**rootlist
;
471 struct cleanup
*cleanup
;
474 enum print_values print_values
;
476 if (argc
!= 1 && argc
!= 2)
477 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
485 print_values
= mi_parse_values_option (argv
[0]);
487 print_values
= PRINT_NO_VALUES
;
489 /* Check if the parameter is a "*" which means that we want
490 to update all variables */
492 if ((*name
== '*') && (*(name
+ 1) == '\0'))
494 nv
= varobj_list (&rootlist
);
495 if (mi_version (uiout
) <= 1)
496 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
498 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
501 do_cleanups (cleanup
);
507 varobj_update_one (*cr
, print_values
);
511 do_cleanups (cleanup
);
515 /* Get varobj handle, if a valid var obj name was specified */
516 var
= varobj_get_handle (name
);
518 error (_("mi_cmd_var_update: Variable object not found"));
520 if (mi_version (uiout
) <= 1)
521 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
523 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
524 varobj_update_one (var
, print_values
);
525 do_cleanups (cleanup
);
530 /* Helper for mi_cmd_var_update() Returns 0 if the update for
531 the variable fails (usually because the variable is out of
532 scope), and 1 if it succeeds. */
535 varobj_update_one (struct varobj
*var
, enum print_values print_values
)
537 struct varobj
**changelist
;
539 struct cleanup
*cleanup
= NULL
;
542 nc
= varobj_update (&var
, &changelist
);
544 /* nc == 0 means that nothing has changed.
545 nc == -1 means that an error occured in updating the variable.
546 nc == -2 means the variable has changed type. */
552 if (mi_version (uiout
) > 1)
553 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
554 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
555 ui_out_field_string (uiout
, "in_scope", "false");
556 if (mi_version (uiout
) > 1)
557 do_cleanups (cleanup
);
562 if (mi_version (uiout
) > 1)
563 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
564 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
565 ui_out_field_string (uiout
, "in_scope", "true");
566 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
567 ui_out_field_int (uiout
, "new_num_children",
568 varobj_get_num_children(var
));
569 if (mi_version (uiout
) > 1)
570 do_cleanups (cleanup
);
578 if (mi_version (uiout
) > 1)
579 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
580 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
581 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
582 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
583 ui_out_field_string (uiout
, "in_scope", "true");
584 ui_out_field_string (uiout
, "type_changed", "false");
585 if (mi_version (uiout
) > 1)
586 do_cleanups (cleanup
);