1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007 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 3 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, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_string.h"
31 const char mi_no_values
[] = "--no-values";
32 const char mi_simple_values
[] = "--simple-values";
33 const char mi_all_values
[] = "--all-values";
35 extern int varobjdebug
; /* defined in varobj.c. */
37 static void varobj_update_one (struct varobj
*var
,
38 enum print_values print_values
,
41 static int mi_print_value_p (struct type
*type
, enum print_values print_values
);
43 /* Print variable object VAR. The PRINT_VALUES parameter controls
44 if the value should be printed. The PRINT_EXPRESSION parameter
45 controls if the expression should be printed. */
47 print_varobj (struct varobj
*var
, enum print_values print_values
,
50 struct type
*gdb_type
;
53 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
55 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
56 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
58 if (mi_print_value_p (varobj_get_gdb_type (var
), print_values
))
59 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
61 type
= varobj_get_type (var
);
64 ui_out_field_string (uiout
, "type", type
);
68 if (varobj_get_frozen (var
))
69 ui_out_field_int (uiout
, "frozen", 1);
72 /* VAROBJ operations */
75 mi_cmd_var_create (char *command
, char **argv
, int argc
)
77 CORE_ADDR frameaddr
= 0;
82 struct cleanup
*old_cleanups
;
83 enum varobj_type var_type
;
87 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
88 ...."); return MI_CMD_ERROR; */
89 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
92 name
= xstrdup (argv
[0]);
93 /* Add cleanup for name. Must be free_current_contents as
94 name can be reallocated */
95 old_cleanups
= make_cleanup (free_current_contents
, &name
);
97 frame
= xstrdup (argv
[1]);
98 make_cleanup (xfree
, frame
);
100 expr
= xstrdup (argv
[2]);
101 make_cleanup (xfree
, expr
);
103 if (strcmp (name
, "-") == 0)
106 name
= varobj_gen_name ();
108 else if (!isalpha (*name
))
109 error (_("mi_cmd_var_create: name of object must begin with a letter"));
111 if (strcmp (frame
, "*") == 0)
112 var_type
= USE_CURRENT_FRAME
;
113 else if (strcmp (frame
, "@") == 0)
114 var_type
= USE_SELECTED_FRAME
;
117 var_type
= USE_SPECIFIED_FRAME
;
118 frameaddr
= string_to_core_addr (frame
);
122 fprintf_unfiltered (gdb_stdlog
,
123 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
124 name
, frame
, paddr (frameaddr
), expr
);
126 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
129 error (_("mi_cmd_var_create: unable to create variable object"));
131 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
133 do_cleanups (old_cleanups
);
138 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
143 int children_only_p
= 0;
144 struct cleanup
*old_cleanups
;
146 if (argc
< 1 || argc
> 2)
147 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
149 name
= xstrdup (argv
[0]);
150 /* Add cleanup for name. Must be free_current_contents as
151 name can be reallocated */
152 old_cleanups
= make_cleanup (free_current_contents
, &name
);
154 /* If we have one single argument it cannot be '-c' or any string
155 starting with '-'. */
158 if (strcmp (name
, "-c") == 0)
159 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
161 error (_("mi_cmd_var_delete: Illegal variable object name"));
164 /* If we have 2 arguments they must be '-c' followed by a string
165 which would be the variable name. */
168 if (strcmp (name
, "-c") != 0)
169 error (_("mi_cmd_var_delete: Invalid option."));
171 do_cleanups (old_cleanups
);
172 name
= xstrdup (argv
[1]);
173 make_cleanup (free_current_contents
, &name
);
176 /* If we didn't error out, now NAME contains the name of the
179 var
= varobj_get_handle (name
);
182 error (_("mi_cmd_var_delete: Variable object not found."));
184 numdel
= varobj_delete (var
, NULL
, children_only_p
);
186 ui_out_field_int (uiout
, "ndeleted", numdel
);
188 do_cleanups (old_cleanups
);
193 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
195 enum varobj_display_formats format
;
201 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
203 /* Get varobj handle, if a valid var obj name was specified */
204 var
= varobj_get_handle (argv
[0]);
207 error (_("mi_cmd_var_set_format: Variable object not found"));
210 if (formspec
== NULL
)
211 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
213 len
= strlen (formspec
);
215 if (strncmp (formspec
, "natural", len
) == 0)
216 format
= FORMAT_NATURAL
;
217 else if (strncmp (formspec
, "binary", len
) == 0)
218 format
= FORMAT_BINARY
;
219 else if (strncmp (formspec
, "decimal", len
) == 0)
220 format
= FORMAT_DECIMAL
;
221 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
222 format
= FORMAT_HEXADECIMAL
;
223 else if (strncmp (formspec
, "octal", len
) == 0)
224 format
= FORMAT_OCTAL
;
226 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
228 /* Set the format of VAR to given format */
229 varobj_set_display_format (var
, format
);
231 /* Report the new current format */
232 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
237 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
243 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
245 var
= varobj_get_handle (argv
[0]);
247 error (_("Variable object not found"));
249 if (strcmp (argv
[1], "0") == 0)
251 else if (strcmp (argv
[1], "1") == 0)
254 error (_("Invalid flag value"));
256 varobj_set_frozen (var
, frozen
);
258 /* We don't automatically return the new value, or what varobjs got new
259 values during unfreezing. If this information is required, client
260 should call -var-update explicitly. */
266 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
268 enum varobj_display_formats format
;
272 error (_("mi_cmd_var_show_format: Usage: NAME."));
274 /* Get varobj handle, if a valid var obj name was specified */
275 var
= varobj_get_handle (argv
[0]);
277 error (_("mi_cmd_var_show_format: Variable object not found"));
279 format
= varobj_get_display_format (var
);
281 /* Report the current format */
282 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
287 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
292 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
294 /* Get varobj handle, if a valid var obj name was specified */
295 var
= varobj_get_handle (argv
[0]);
297 error (_("mi_cmd_var_info_num_children: Variable object not found"));
299 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
303 /* Parse a string argument into a print_values value. */
305 static enum print_values
306 mi_parse_values_option (const char *arg
)
308 if (strcmp (arg
, "0") == 0
309 || strcmp (arg
, mi_no_values
) == 0)
310 return PRINT_NO_VALUES
;
311 else if (strcmp (arg
, "1") == 0
312 || strcmp (arg
, mi_all_values
) == 0)
313 return PRINT_ALL_VALUES
;
314 else if (strcmp (arg
, "2") == 0
315 || strcmp (arg
, mi_simple_values
) == 0)
316 return PRINT_SIMPLE_VALUES
;
318 error (_("Unknown value for PRINT_VALUES\n\
319 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
320 mi_no_values
, mi_simple_values
, mi_all_values
);
323 /* Return 1 if given the argument PRINT_VALUES we should display
324 a value of type TYPE. */
327 mi_print_value_p (struct type
*type
, enum print_values print_values
)
330 if (print_values
== PRINT_NO_VALUES
)
333 if (print_values
== PRINT_ALL_VALUES
)
340 type
= check_typedef (type
);
342 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
343 and that type is not a compound type. */
344 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
345 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
346 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
351 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
354 struct varobj
**childlist
;
356 struct cleanup
*cleanup_children
;
358 enum print_values print_values
;
360 if (argc
!= 1 && argc
!= 2)
361 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
363 /* Get varobj handle, if a valid var obj name was specified */
365 var
= varobj_get_handle (argv
[0]);
367 var
= varobj_get_handle (argv
[1]);
369 error (_("Variable object not found"));
371 numchild
= varobj_list_children (var
, &childlist
);
372 ui_out_field_int (uiout
, "numchild", numchild
);
374 print_values
= mi_parse_values_option (argv
[0]);
376 print_values
= PRINT_NO_VALUES
;
384 if (mi_version (uiout
) == 1)
385 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
387 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
391 struct cleanup
*cleanup_child
;
392 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
393 print_varobj (*cc
, print_values
, 1 /* print expression */);
395 do_cleanups (cleanup_child
);
397 do_cleanups (cleanup_children
);
403 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
408 error (_("mi_cmd_var_info_type: Usage: NAME."));
410 /* Get varobj handle, if a valid var obj name was specified */
411 var
= varobj_get_handle (argv
[0]);
413 error (_("mi_cmd_var_info_type: Variable object not found"));
415 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
420 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
426 error (_("Usage: NAME."));
428 /* Get varobj handle, if a valid var obj name was specified. */
429 var
= varobj_get_handle (argv
[0]);
431 error (_("Variable object not found"));
433 path_expr
= varobj_get_path_expr (var
);
435 ui_out_field_string (uiout
, "path_expr", path_expr
);
441 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
443 enum varobj_languages lang
;
447 error (_("mi_cmd_var_info_expression: Usage: NAME."));
449 /* Get varobj handle, if a valid var obj name was specified */
450 var
= varobj_get_handle (argv
[0]);
452 error (_("mi_cmd_var_info_expression: Variable object not found"));
454 lang
= varobj_get_language (var
);
456 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
457 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
462 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
469 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
471 /* Get varobj handle, if a valid var obj name was specified */
472 var
= varobj_get_handle (argv
[0]);
474 error (_("mi_cmd_var_show_attributes: Variable object not found"));
476 attr
= varobj_get_attributes (var
);
477 /* FIXME: define masks for attributes */
478 if (attr
& 0x00000001)
481 attstr
= "noneditable";
483 ui_out_field_string (uiout
, "attr", attstr
);
488 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
493 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
495 /* Get varobj handle, if a valid var obj name was specified */
496 var
= varobj_get_handle (argv
[0]);
498 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
500 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
505 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
511 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
513 /* Get varobj handle, if a valid var obj name was specified */
514 var
= varobj_get_handle (argv
[0]);
516 error (_("mi_cmd_var_assign: Variable object not found"));
518 if (!varobj_editable_p (var
))
519 error (_("mi_cmd_var_assign: Variable object is not editable"));
521 expression
= xstrdup (argv
[1]);
523 if (!varobj_set_value (var
, expression
))
524 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
526 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
531 mi_cmd_var_update (char *command
, char **argv
, int argc
)
534 struct varobj
**rootlist
;
536 struct cleanup
*cleanup
;
539 enum print_values print_values
;
541 if (argc
!= 1 && argc
!= 2)
542 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
550 print_values
= mi_parse_values_option (argv
[0]);
552 print_values
= PRINT_NO_VALUES
;
554 /* Check if the parameter is a "*" which means that we want
555 to update all variables */
557 if ((*name
== '*') && (*(name
+ 1) == '\0'))
559 nv
= varobj_list (&rootlist
);
560 cleanup
= make_cleanup (xfree
, rootlist
);
561 if (mi_version (uiout
) <= 1)
562 make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
564 make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
567 do_cleanups (cleanup
);
573 varobj_update_one (*cr
, print_values
, 0 /* implicit */);
576 do_cleanups (cleanup
);
580 /* Get varobj handle, if a valid var obj name was specified */
581 var
= varobj_get_handle (name
);
583 error (_("mi_cmd_var_update: Variable object not found"));
585 if (mi_version (uiout
) <= 1)
586 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
588 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
589 varobj_update_one (var
, print_values
, 1 /* explicit */);
590 do_cleanups (cleanup
);
595 /* Helper for mi_cmd_var_update(). */
598 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
601 struct varobj
**changelist
;
603 struct cleanup
*cleanup
= NULL
;
606 nc
= varobj_update (&var
, &changelist
, explicit);
608 /* nc >= 0 represents the number of changes reported into changelist.
609 nc < 0 means that an error occured or the the variable has
610 changed type (TYPE_CHANGED). */
616 if (mi_version (uiout
) > 1)
617 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
618 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
623 ui_out_field_string (uiout
, "in_scope", "false");
626 ui_out_field_string (uiout
, "in_scope", "invalid");
629 ui_out_field_string (uiout
, "in_scope", "true");
630 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
631 ui_out_field_int (uiout
, "new_num_children",
632 varobj_get_num_children(var
));
635 if (mi_version (uiout
) > 1)
636 do_cleanups (cleanup
);
643 if (mi_version (uiout
) > 1)
644 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
645 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
646 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
647 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
648 ui_out_field_string (uiout
, "in_scope", "true");
649 ui_out_field_string (uiout
, "type_changed", "false");
650 if (mi_version (uiout
) > 1)
651 do_cleanups (cleanup
);