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
,
52 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
54 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
55 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
57 if (mi_print_value_p (varobj_get_gdb_type (var
), print_values
))
58 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
60 type
= varobj_get_type (var
);
63 ui_out_field_string (uiout
, "type", type
);
67 if (varobj_get_frozen (var
))
68 ui_out_field_int (uiout
, "frozen", 1);
71 /* VAROBJ operations */
74 mi_cmd_var_create (char *command
, char **argv
, int argc
)
76 CORE_ADDR frameaddr
= 0;
81 struct cleanup
*old_cleanups
;
82 enum varobj_type var_type
;
86 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
87 ...."); return MI_CMD_ERROR; */
88 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
91 name
= xstrdup (argv
[0]);
92 /* Add cleanup for name. Must be free_current_contents as
93 name can be reallocated */
94 old_cleanups
= make_cleanup (free_current_contents
, &name
);
96 frame
= xstrdup (argv
[1]);
97 make_cleanup (xfree
, frame
);
99 expr
= xstrdup (argv
[2]);
100 make_cleanup (xfree
, expr
);
102 if (strcmp (name
, "-") == 0)
105 name
= varobj_gen_name ();
107 else if (!isalpha (*name
))
108 error (_("mi_cmd_var_create: name of object must begin with a letter"));
110 if (strcmp (frame
, "*") == 0)
111 var_type
= USE_CURRENT_FRAME
;
112 else if (strcmp (frame
, "@") == 0)
113 var_type
= USE_SELECTED_FRAME
;
116 var_type
= USE_SPECIFIED_FRAME
;
117 frameaddr
= string_to_core_addr (frame
);
121 fprintf_unfiltered (gdb_stdlog
,
122 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
123 name
, frame
, paddr (frameaddr
), expr
);
125 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
128 error (_("mi_cmd_var_create: unable to create variable object"));
130 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
132 do_cleanups (old_cleanups
);
137 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
142 int children_only_p
= 0;
143 struct cleanup
*old_cleanups
;
145 if (argc
< 1 || argc
> 2)
146 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
148 name
= xstrdup (argv
[0]);
149 /* Add cleanup for name. Must be free_current_contents as
150 name can be reallocated */
151 old_cleanups
= make_cleanup (free_current_contents
, &name
);
153 /* If we have one single argument it cannot be '-c' or any string
154 starting with '-'. */
157 if (strcmp (name
, "-c") == 0)
158 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
160 error (_("mi_cmd_var_delete: Illegal variable object name"));
163 /* If we have 2 arguments they must be '-c' followed by a string
164 which would be the variable name. */
167 if (strcmp (name
, "-c") != 0)
168 error (_("mi_cmd_var_delete: Invalid option."));
170 do_cleanups (old_cleanups
);
171 name
= xstrdup (argv
[1]);
172 make_cleanup (free_current_contents
, &name
);
175 /* If we didn't error out, now NAME contains the name of the
178 var
= varobj_get_handle (name
);
181 error (_("mi_cmd_var_delete: Variable object not found."));
183 numdel
= varobj_delete (var
, NULL
, children_only_p
);
185 ui_out_field_int (uiout
, "ndeleted", numdel
);
187 do_cleanups (old_cleanups
);
192 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
194 enum varobj_display_formats format
;
200 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
202 /* Get varobj handle, if a valid var obj name was specified */
203 var
= varobj_get_handle (argv
[0]);
206 error (_("mi_cmd_var_set_format: Variable object not found"));
209 if (formspec
== NULL
)
210 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
212 len
= strlen (formspec
);
214 if (strncmp (formspec
, "natural", len
) == 0)
215 format
= FORMAT_NATURAL
;
216 else if (strncmp (formspec
, "binary", len
) == 0)
217 format
= FORMAT_BINARY
;
218 else if (strncmp (formspec
, "decimal", len
) == 0)
219 format
= FORMAT_DECIMAL
;
220 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
221 format
= FORMAT_HEXADECIMAL
;
222 else if (strncmp (formspec
, "octal", len
) == 0)
223 format
= FORMAT_OCTAL
;
225 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
227 /* Set the format of VAR to given format */
228 varobj_set_display_format (var
, format
);
230 /* Report the new current format */
231 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
236 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
242 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
244 var
= varobj_get_handle (argv
[0]);
246 error (_("Variable object not found"));
248 if (strcmp (argv
[1], "0") == 0)
250 else if (strcmp (argv
[1], "1") == 0)
253 error (_("Invalid flag value"));
255 varobj_set_frozen (var
, frozen
);
257 /* We don't automatically return the new value, or what varobjs got new
258 values during unfreezing. If this information is required, client
259 should call -var-update explicitly. */
265 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
267 enum varobj_display_formats format
;
271 error (_("mi_cmd_var_show_format: Usage: NAME."));
273 /* Get varobj handle, if a valid var obj name was specified */
274 var
= varobj_get_handle (argv
[0]);
276 error (_("mi_cmd_var_show_format: Variable object not found"));
278 format
= varobj_get_display_format (var
);
280 /* Report the current format */
281 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
286 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
291 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
293 /* Get varobj handle, if a valid var obj name was specified */
294 var
= varobj_get_handle (argv
[0]);
296 error (_("mi_cmd_var_info_num_children: Variable object not found"));
298 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
302 /* Parse a string argument into a print_values value. */
304 static enum print_values
305 mi_parse_values_option (const char *arg
)
307 if (strcmp (arg
, "0") == 0
308 || strcmp (arg
, mi_no_values
) == 0)
309 return PRINT_NO_VALUES
;
310 else if (strcmp (arg
, "1") == 0
311 || strcmp (arg
, mi_all_values
) == 0)
312 return PRINT_ALL_VALUES
;
313 else if (strcmp (arg
, "2") == 0
314 || strcmp (arg
, mi_simple_values
) == 0)
315 return PRINT_SIMPLE_VALUES
;
317 error (_("Unknown value for PRINT_VALUES\n\
318 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
319 mi_no_values
, mi_simple_values
, mi_all_values
);
322 /* Return 1 if given the argument PRINT_VALUES we should display
323 a value of type TYPE. */
326 mi_print_value_p (struct type
*type
, enum print_values print_values
)
329 type
= check_typedef (type
);
331 if (print_values
== PRINT_NO_VALUES
)
334 if (print_values
== PRINT_ALL_VALUES
)
337 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
338 and that type is not a compound type. */
340 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
341 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
342 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
346 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
349 struct varobj
**childlist
;
351 struct cleanup
*cleanup_children
;
353 enum print_values print_values
;
355 if (argc
!= 1 && argc
!= 2)
356 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
358 /* Get varobj handle, if a valid var obj name was specified */
360 var
= varobj_get_handle (argv
[0]);
362 var
= varobj_get_handle (argv
[1]);
364 error (_("Variable object not found"));
366 numchild
= varobj_list_children (var
, &childlist
);
367 ui_out_field_int (uiout
, "numchild", numchild
);
369 print_values
= mi_parse_values_option (argv
[0]);
371 print_values
= PRINT_NO_VALUES
;
379 if (mi_version (uiout
) == 1)
380 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
382 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
386 struct cleanup
*cleanup_child
;
387 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
388 print_varobj (*cc
, print_values
, 1 /* print expression */);
390 do_cleanups (cleanup_child
);
392 do_cleanups (cleanup_children
);
398 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
403 error (_("mi_cmd_var_info_type: 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_info_type: Variable object not found"));
410 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
415 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
417 enum varobj_languages lang
;
421 error (_("mi_cmd_var_info_expression: Usage: NAME."));
423 /* Get varobj handle, if a valid var obj name was specified */
424 var
= varobj_get_handle (argv
[0]);
426 error (_("mi_cmd_var_info_expression: Variable object not found"));
428 lang
= varobj_get_language (var
);
430 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
431 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
436 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
443 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
445 /* Get varobj handle, if a valid var obj name was specified */
446 var
= varobj_get_handle (argv
[0]);
448 error (_("mi_cmd_var_show_attributes: Variable object not found"));
450 attr
= varobj_get_attributes (var
);
451 /* FIXME: define masks for attributes */
452 if (attr
& 0x00000001)
455 attstr
= "noneditable";
457 ui_out_field_string (uiout
, "attr", attstr
);
462 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
467 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
469 /* Get varobj handle, if a valid var obj name was specified */
470 var
= varobj_get_handle (argv
[0]);
472 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
474 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
479 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
485 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
487 /* Get varobj handle, if a valid var obj name was specified */
488 var
= varobj_get_handle (argv
[0]);
490 error (_("mi_cmd_var_assign: Variable object not found"));
492 /* FIXME: define masks for attributes */
493 if (!(varobj_get_attributes (var
) & 0x00000001))
494 error (_("mi_cmd_var_assign: Variable object is not editable"));
496 expression
= xstrdup (argv
[1]);
498 if (!varobj_set_value (var
, expression
))
499 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
501 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
506 mi_cmd_var_update (char *command
, char **argv
, int argc
)
509 struct varobj
**rootlist
;
511 struct cleanup
*cleanup
;
514 enum print_values print_values
;
516 if (argc
!= 1 && argc
!= 2)
517 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
525 print_values
= mi_parse_values_option (argv
[0]);
527 print_values
= PRINT_NO_VALUES
;
529 /* Check if the parameter is a "*" which means that we want
530 to update all variables */
532 if ((*name
== '*') && (*(name
+ 1) == '\0'))
534 nv
= varobj_list (&rootlist
);
535 cleanup
= make_cleanup (xfree
, rootlist
);
536 if (mi_version (uiout
) <= 1)
537 make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
539 make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
542 do_cleanups (cleanup
);
548 varobj_update_one (*cr
, print_values
, 0 /* implicit */);
551 do_cleanups (cleanup
);
555 /* Get varobj handle, if a valid var obj name was specified */
556 var
= varobj_get_handle (name
);
558 error (_("mi_cmd_var_update: Variable object not found"));
560 if (mi_version (uiout
) <= 1)
561 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
563 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
564 varobj_update_one (var
, print_values
, 1 /* explicit */);
565 do_cleanups (cleanup
);
570 /* Helper for mi_cmd_var_update(). */
573 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
576 struct varobj
**changelist
;
578 struct cleanup
*cleanup
= NULL
;
581 nc
= varobj_update (&var
, &changelist
, explicit);
583 /* nc >= 0 represents the number of changes reported into changelist.
584 nc < 0 means that an error occured or the the variable has
585 changed type (TYPE_CHANGED). */
591 if (mi_version (uiout
) > 1)
592 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
593 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
598 ui_out_field_string (uiout
, "in_scope", "false");
601 ui_out_field_string (uiout
, "in_scope", "invalid");
604 ui_out_field_string (uiout
, "in_scope", "true");
605 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
606 ui_out_field_int (uiout
, "new_num_children",
607 varobj_get_num_children(var
));
610 if (mi_version (uiout
) > 1)
611 do_cleanups (cleanup
);
618 if (mi_version (uiout
) > 1)
619 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
620 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
621 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
622 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
623 ui_out_field_string (uiout
, "in_scope", "true");
624 ui_out_field_string (uiout
, "type_changed", "false");
625 if (mi_version (uiout
) > 1)
626 do_cleanups (cleanup
);