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 old_cleanups
= make_cleanup (xfree
, frame
);
71 expr
= xstrdup (argv
[2]);
73 if (strcmp (name
, "-") == 0)
76 name
= varobj_gen_name ();
78 else if (!isalpha (*name
))
79 error (_("mi_cmd_var_create: name of object must begin with a letter"));
81 if (strcmp (frame
, "*") == 0)
82 var_type
= USE_CURRENT_FRAME
;
83 else if (strcmp (frame
, "@") == 0)
84 var_type
= USE_SELECTED_FRAME
;
87 var_type
= USE_SPECIFIED_FRAME
;
88 frameaddr
= string_to_core_addr (frame
);
92 fprintf_unfiltered (gdb_stdlog
,
93 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
94 name
, frame
, paddr (frameaddr
), expr
);
96 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
99 error (_("mi_cmd_var_create: unable to create variable object"));
101 ui_out_field_string (uiout
, "name", name
);
102 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
103 type
= varobj_get_type (var
);
105 ui_out_field_string (uiout
, "type", "");
108 ui_out_field_string (uiout
, "type", type
);
112 do_cleanups (old_cleanups
);
117 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
123 int children_only_p
= 0;
124 struct cleanup
*old_cleanups
;
126 if (argc
< 1 || argc
> 2)
127 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
129 name
= xstrdup (argv
[0]);
130 /* Add cleanup for name. Must be free_current_contents as
131 name can be reallocated */
132 old_cleanups
= make_cleanup (free_current_contents
, &name
);
134 /* If we have one single argument it cannot be '-c' or any string
135 starting with '-'. */
138 if (strcmp (name
, "-c") == 0)
139 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
141 error (_("mi_cmd_var_delete: Illegal variable object name"));
144 /* If we have 2 arguments they must be '-c' followed by a string
145 which would be the variable name. */
148 expr
= xstrdup (argv
[1]);
149 if (strcmp (name
, "-c") != 0)
150 error (_("mi_cmd_var_delete: Invalid option."));
153 name
= xstrdup (expr
);
157 /* If we didn't error out, now NAME contains the name of the
160 var
= varobj_get_handle (name
);
163 error (_("mi_cmd_var_delete: Variable object not found."));
165 numdel
= varobj_delete (var
, NULL
, children_only_p
);
167 ui_out_field_int (uiout
, "ndeleted", numdel
);
169 do_cleanups (old_cleanups
);
174 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
176 enum varobj_display_formats format
;
182 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
184 /* Get varobj handle, if a valid var obj name was specified */
185 var
= varobj_get_handle (argv
[0]);
188 error (_("mi_cmd_var_set_format: Variable object not found"));
190 formspec
= xstrdup (argv
[1]);
191 if (formspec
== NULL
)
192 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
194 len
= strlen (formspec
);
196 if (strncmp (formspec
, "natural", len
) == 0)
197 format
= FORMAT_NATURAL
;
198 else if (strncmp (formspec
, "binary", len
) == 0)
199 format
= FORMAT_BINARY
;
200 else if (strncmp (formspec
, "decimal", len
) == 0)
201 format
= FORMAT_DECIMAL
;
202 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
203 format
= FORMAT_HEXADECIMAL
;
204 else if (strncmp (formspec
, "octal", len
) == 0)
205 format
= FORMAT_OCTAL
;
207 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
209 /* Set the format of VAR to given format */
210 varobj_set_display_format (var
, format
);
212 /* Report the new current format */
213 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
218 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
220 enum varobj_display_formats format
;
224 error (_("mi_cmd_var_show_format: Usage: NAME."));
226 /* Get varobj handle, if a valid var obj name was specified */
227 var
= varobj_get_handle (argv
[0]);
229 error (_("mi_cmd_var_show_format: Variable object not found"));
231 format
= varobj_get_display_format (var
);
233 /* Report the current format */
234 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
239 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
244 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
246 /* Get varobj handle, if a valid var obj name was specified */
247 var
= varobj_get_handle (argv
[0]);
249 error (_("mi_cmd_var_info_num_children: Variable object not found"));
251 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
255 /* Parse a string argument into a print_values value. */
257 static enum print_values
258 mi_parse_values_option (const char *arg
)
260 if (strcmp (arg
, "0") == 0
261 || strcmp (arg
, mi_no_values
) == 0)
262 return PRINT_NO_VALUES
;
263 else if (strcmp (arg
, "1") == 0
264 || strcmp (arg
, mi_all_values
) == 0)
265 return PRINT_ALL_VALUES
;
266 else if (strcmp (arg
, "2") == 0
267 || strcmp (arg
, mi_simple_values
) == 0)
268 return PRINT_SIMPLE_VALUES
;
270 error (_("Unknown value for PRINT_VALUES\n\
271 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
272 mi_no_values
, mi_simple_values
, mi_all_values
);
275 /* Return 1 if given the argument PRINT_VALUES we should display
276 a value of type TYPE. */
279 mi_print_value_p (struct type
*type
, enum print_values print_values
)
282 type
= check_typedef (type
);
284 if (print_values
== PRINT_NO_VALUES
)
287 if (print_values
== PRINT_ALL_VALUES
)
290 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
291 and that type is not a compound type. */
293 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
294 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
295 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
299 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
302 struct varobj
**childlist
;
304 struct cleanup
*cleanup_children
;
307 enum print_values print_values
;
309 if (argc
!= 1 && argc
!= 2)
310 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
312 /* Get varobj handle, if a valid var obj name was specified */
314 var
= varobj_get_handle (argv
[0]);
316 var
= varobj_get_handle (argv
[1]);
318 error (_("Variable object not found"));
320 numchild
= varobj_list_children (var
, &childlist
);
321 ui_out_field_int (uiout
, "numchild", numchild
);
323 print_values
= mi_parse_values_option (argv
[0]);
325 print_values
= PRINT_NO_VALUES
;
330 if (mi_version (uiout
) == 1)
331 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
333 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
337 struct cleanup
*cleanup_child
;
338 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
339 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
340 ui_out_field_string (uiout
, "exp", varobj_get_expression (*cc
));
341 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (*cc
));
342 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
343 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
344 type
= varobj_get_type (*cc
);
345 /* C++ pseudo-variables (public, private, protected) do not have a type */
347 ui_out_field_string (uiout
, "type", type
);
348 do_cleanups (cleanup_child
);
351 do_cleanups (cleanup_children
);
357 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
362 error (_("mi_cmd_var_info_type: Usage: NAME."));
364 /* Get varobj handle, if a valid var obj name was specified */
365 var
= varobj_get_handle (argv
[0]);
367 error (_("mi_cmd_var_info_type: Variable object not found"));
369 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
374 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
376 enum varobj_languages lang
;
380 error (_("mi_cmd_var_info_expression: Usage: NAME."));
382 /* Get varobj handle, if a valid var obj name was specified */
383 var
= varobj_get_handle (argv
[0]);
385 error (_("mi_cmd_var_info_expression: Variable object not found"));
387 lang
= varobj_get_language (var
);
389 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
390 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
395 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
402 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
404 /* Get varobj handle, if a valid var obj name was specified */
405 var
= varobj_get_handle (argv
[0]);
407 error (_("mi_cmd_var_show_attributes: Variable object not found"));
409 attr
= varobj_get_attributes (var
);
410 /* FIXME: define masks for attributes */
411 if (attr
& 0x00000001)
414 attstr
= "noneditable";
416 ui_out_field_string (uiout
, "attr", attstr
);
421 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
426 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
428 /* Get varobj handle, if a valid var obj name was specified */
429 var
= varobj_get_handle (argv
[0]);
431 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
433 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
438 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
444 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
446 /* Get varobj handle, if a valid var obj name was specified */
447 var
= varobj_get_handle (argv
[0]);
449 error (_("mi_cmd_var_assign: Variable object not found"));
451 /* FIXME: define masks for attributes */
452 if (!(varobj_get_attributes (var
) & 0x00000001))
453 error (_("mi_cmd_var_assign: Variable object is not editable"));
455 expression
= xstrdup (argv
[1]);
457 if (!varobj_set_value (var
, expression
))
458 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
460 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
465 mi_cmd_var_update (char *command
, char **argv
, int argc
)
468 struct varobj
**rootlist
;
470 struct cleanup
*cleanup
;
473 enum print_values print_values
;
475 if (argc
!= 1 && argc
!= 2)
476 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
484 print_values
= mi_parse_values_option (argv
[0]);
486 print_values
= PRINT_NO_VALUES
;
488 /* Check if the parameter is a "*" which means that we want
489 to update all variables */
491 if ((*name
== '*') && (*(name
+ 1) == '\0'))
493 nv
= varobj_list (&rootlist
);
494 if (mi_version (uiout
) <= 1)
495 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
497 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
500 do_cleanups (cleanup
);
506 varobj_update_one (*cr
, print_values
);
510 do_cleanups (cleanup
);
514 /* Get varobj handle, if a valid var obj name was specified */
515 var
= varobj_get_handle (name
);
517 error (_("mi_cmd_var_update: Variable object not found"));
519 if (mi_version (uiout
) <= 1)
520 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
522 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
523 varobj_update_one (var
, print_values
);
524 do_cleanups (cleanup
);
529 /* Helper for mi_cmd_var_update() Returns 0 if the update for
530 the variable fails (usually because the variable is out of
531 scope), and 1 if it succeeds. */
534 varobj_update_one (struct varobj
*var
, enum print_values print_values
)
536 struct varobj
**changelist
;
538 struct cleanup
*cleanup
= NULL
;
541 nc
= varobj_update (&var
, &changelist
);
543 /* nc == 0 means that nothing has changed.
544 nc == -1 means that an error occured in updating the variable.
545 nc == -2 means the variable has changed type. */
551 if (mi_version (uiout
) > 1)
552 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
553 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
554 ui_out_field_string (uiout
, "in_scope", "false");
555 if (mi_version (uiout
) > 1)
556 do_cleanups (cleanup
);
561 if (mi_version (uiout
) > 1)
562 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
563 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
564 ui_out_field_string (uiout
, "in_scope", "true");
565 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
566 ui_out_field_int (uiout
, "new_num_children",
567 varobj_get_num_children(var
));
568 if (mi_version (uiout
) > 1)
569 do_cleanups (cleanup
);
577 if (mi_version (uiout
) > 1)
578 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
579 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
580 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
581 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
582 ui_out_field_string (uiout
, "in_scope", "true");
583 ui_out_field_string (uiout
, "type_changed", "false");
584 if (mi_version (uiout
) > 1)
585 do_cleanups (cleanup
);