1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
31 #include "mi-getopt.h"
32 #include "gdbthread.h"
34 const char mi_no_values
[] = "--no-values";
35 const char mi_simple_values
[] = "--simple-values";
36 const char mi_all_values
[] = "--all-values";
38 extern int varobjdebug
; /* defined in varobj.c. */
40 static void varobj_update_one (struct varobj
*var
,
41 enum print_values print_values
,
44 static int mi_print_value_p (struct varobj
*var
,
45 enum print_values print_values
);
47 /* Print variable object VAR. The PRINT_VALUES parameter controls
48 if the value should be printed. The PRINT_EXPRESSION parameter
49 controls if the expression should be printed. */
51 print_varobj (struct varobj
*var
, enum print_values print_values
,
54 struct ui_out
*uiout
= current_uiout
;
59 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
61 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
62 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
64 if (mi_print_value_p (var
, print_values
))
66 char *val
= varobj_get_value (var
);
68 ui_out_field_string (uiout
, "value", val
);
72 type
= varobj_get_type (var
);
75 ui_out_field_string (uiout
, "type", type
);
79 thread_id
= varobj_get_thread_id (var
);
81 ui_out_field_int (uiout
, "thread-id", thread_id
);
83 if (varobj_get_frozen (var
))
84 ui_out_field_int (uiout
, "frozen", 1);
86 display_hint
= varobj_get_display_hint (var
);
89 ui_out_field_string (uiout
, "displayhint", display_hint
);
93 if (varobj_pretty_printed_p (var
))
94 ui_out_field_int (uiout
, "dynamic", 1);
97 /* VAROBJ operations */
100 mi_cmd_var_create (char *command
, char **argv
, int argc
)
102 struct ui_out
*uiout
= current_uiout
;
103 CORE_ADDR frameaddr
= 0;
108 struct cleanup
*old_cleanups
;
109 enum varobj_type var_type
;
113 /* mi_error_message = xstrprintf ("-var-create: Usage:
114 ...."); return MI_CMD_ERROR; */
115 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
118 name
= xstrdup (argv
[0]);
119 /* Add cleanup for name. Must be free_current_contents as
120 name can be reallocated */
121 old_cleanups
= make_cleanup (free_current_contents
, &name
);
123 frame
= xstrdup (argv
[1]);
124 make_cleanup (xfree
, frame
);
126 expr
= xstrdup (argv
[2]);
127 make_cleanup (xfree
, expr
);
129 if (strcmp (name
, "-") == 0)
132 name
= varobj_gen_name ();
134 else if (!isalpha (*name
))
135 error (_("-var-create: name of object must begin with a letter"));
137 if (strcmp (frame
, "*") == 0)
138 var_type
= USE_CURRENT_FRAME
;
139 else if (strcmp (frame
, "@") == 0)
140 var_type
= USE_SELECTED_FRAME
;
143 var_type
= USE_SPECIFIED_FRAME
;
144 frameaddr
= string_to_core_addr (frame
);
148 fprintf_unfiltered (gdb_stdlog
,
149 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
150 name
, frame
, hex_string (frameaddr
), expr
);
152 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
155 error (_("-var-create: unable to create variable object"));
157 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
159 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, 0));
161 do_cleanups (old_cleanups
);
165 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
170 int children_only_p
= 0;
171 struct cleanup
*old_cleanups
;
172 struct ui_out
*uiout
= current_uiout
;
174 if (argc
< 1 || argc
> 2)
175 error (_("-var-delete: Usage: [-c] EXPRESSION."));
177 name
= xstrdup (argv
[0]);
178 /* Add cleanup for name. Must be free_current_contents as
179 name can be reallocated */
180 old_cleanups
= make_cleanup (free_current_contents
, &name
);
182 /* If we have one single argument it cannot be '-c' or any string
183 starting with '-'. */
186 if (strcmp (name
, "-c") == 0)
187 error (_("-var-delete: Missing required "
188 "argument after '-c': variable object name"));
190 error (_("-var-delete: Illegal variable object name"));
193 /* If we have 2 arguments they must be '-c' followed by a string
194 which would be the variable name. */
197 if (strcmp (name
, "-c") != 0)
198 error (_("-var-delete: Invalid option."));
200 do_cleanups (old_cleanups
);
201 name
= xstrdup (argv
[1]);
202 old_cleanups
= make_cleanup (free_current_contents
, &name
);
205 /* If we didn't error out, now NAME contains the name of the
208 var
= varobj_get_handle (name
);
210 numdel
= varobj_delete (var
, NULL
, children_only_p
);
212 ui_out_field_int (uiout
, "ndeleted", numdel
);
214 do_cleanups (old_cleanups
);
217 /* Parse a string argument into a format value. */
219 static enum varobj_display_formats
220 mi_parse_format (const char *arg
)
228 if (strncmp (arg
, "natural", len
) == 0)
229 return FORMAT_NATURAL
;
230 else if (strncmp (arg
, "binary", len
) == 0)
231 return FORMAT_BINARY
;
232 else if (strncmp (arg
, "decimal", len
) == 0)
233 return FORMAT_DECIMAL
;
234 else if (strncmp (arg
, "hexadecimal", len
) == 0)
235 return FORMAT_HEXADECIMAL
;
236 else if (strncmp (arg
, "octal", len
) == 0)
240 error (_("Must specify the format as: \"natural\", "
241 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
245 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
247 enum varobj_display_formats format
;
250 struct ui_out
*uiout
= current_uiout
;
253 error (_("-var-set-format: Usage: NAME FORMAT."));
255 /* Get varobj handle, if a valid var obj name was specified */
256 var
= varobj_get_handle (argv
[0]);
258 format
= mi_parse_format (argv
[1]);
260 /* Set the format of VAR to given format */
261 varobj_set_display_format (var
, format
);
263 /* Report the new current format */
264 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
266 /* Report the value in the new format */
267 val
= varobj_get_value (var
);
268 ui_out_field_string (uiout
, "value", val
);
273 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
278 error (_("Usage: NAME VISUALIZER_FUNCTION."));
280 var
= varobj_get_handle (argv
[0]);
283 error (_("Variable object not found"));
285 varobj_set_visualizer (var
, argv
[1]);
289 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
295 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
297 var
= varobj_get_handle (argv
[0]);
299 if (strcmp (argv
[1], "0") == 0)
301 else if (strcmp (argv
[1], "1") == 0)
304 error (_("Invalid flag value"));
306 varobj_set_frozen (var
, frozen
);
308 /* We don't automatically return the new value, or what varobjs got new
309 values during unfreezing. If this information is required, client
310 should call -var-update explicitly. */
315 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
317 struct ui_out
*uiout
= current_uiout
;
318 enum varobj_display_formats format
;
322 error (_("-var-show-format: Usage: NAME."));
324 /* Get varobj handle, if a valid var obj name was specified */
325 var
= varobj_get_handle (argv
[0]);
327 format
= varobj_get_display_format (var
);
329 /* Report the current format */
330 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
334 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
336 struct ui_out
*uiout
= current_uiout
;
340 error (_("-var-info-num-children: Usage: NAME."));
342 /* Get varobj handle, if a valid var obj name was specified */
343 var
= varobj_get_handle (argv
[0]);
345 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
348 /* Parse a string argument into a print_values value. */
350 static enum print_values
351 mi_parse_values_option (const char *arg
)
353 if (strcmp (arg
, "0") == 0
354 || strcmp (arg
, mi_no_values
) == 0)
355 return PRINT_NO_VALUES
;
356 else if (strcmp (arg
, "1") == 0
357 || strcmp (arg
, mi_all_values
) == 0)
358 return PRINT_ALL_VALUES
;
359 else if (strcmp (arg
, "2") == 0
360 || strcmp (arg
, mi_simple_values
) == 0)
361 return PRINT_SIMPLE_VALUES
;
363 error (_("Unknown value for PRINT_VALUES\n\
364 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
365 mi_no_values
, mi_simple_values
, mi_all_values
);
368 /* Return 1 if given the argument PRINT_VALUES we should display
372 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
376 if (print_values
== PRINT_NO_VALUES
)
379 if (print_values
== PRINT_ALL_VALUES
)
382 if (varobj_pretty_printed_p (var
))
385 type
= varobj_get_gdb_type (var
);
390 type
= check_typedef (type
);
392 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
393 and that type is not a compound type. */
394 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
395 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
396 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
401 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
403 struct ui_out
*uiout
= current_uiout
;
405 VEC(varobj_p
) *children
;
406 struct varobj
*child
;
407 enum print_values print_values
;
412 if (argc
< 1 || argc
> 4)
413 error (_("-var-list-children: Usage: "
414 "[PRINT_VALUES] NAME [FROM TO]"));
416 /* Get varobj handle, if a valid var obj name was specified */
417 if (argc
== 1 || argc
== 3)
418 var
= varobj_get_handle (argv
[0]);
420 var
= varobj_get_handle (argv
[1]);
424 from
= atoi (argv
[argc
- 2]);
425 to
= atoi (argv
[argc
- 1]);
433 children
= varobj_list_children (var
, &from
, &to
);
434 ui_out_field_int (uiout
, "numchild", to
- from
);
435 if (argc
== 2 || argc
== 4)
436 print_values
= mi_parse_values_option (argv
[0]);
438 print_values
= PRINT_NO_VALUES
;
440 display_hint
= varobj_get_display_hint (var
);
443 ui_out_field_string (uiout
, "displayhint", display_hint
);
444 xfree (display_hint
);
449 struct cleanup
*cleanup_children
;
451 if (mi_version (uiout
) == 1)
453 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
456 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
458 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
461 struct cleanup
*cleanup_child
;
463 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
464 print_varobj (child
, print_values
, 1 /* print expression */);
465 do_cleanups (cleanup_child
);
467 do_cleanups (cleanup_children
);
470 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
474 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
476 struct ui_out
*uiout
= current_uiout
;
480 error (_("-var-info-type: Usage: NAME."));
482 /* Get varobj handle, if a valid var obj name was specified */
483 var
= varobj_get_handle (argv
[0]);
485 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
489 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
491 struct ui_out
*uiout
= current_uiout
;
496 error (_("Usage: NAME."));
498 /* Get varobj handle, if a valid var obj name was specified. */
499 var
= varobj_get_handle (argv
[0]);
501 path_expr
= varobj_get_path_expr (var
);
503 ui_out_field_string (uiout
, "path_expr", path_expr
);
507 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
509 struct ui_out
*uiout
= current_uiout
;
510 enum varobj_languages lang
;
514 error (_("-var-info-expression: Usage: NAME."));
516 /* Get varobj handle, if a valid var obj name was specified */
517 var
= varobj_get_handle (argv
[0]);
519 lang
= varobj_get_language (var
);
521 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
522 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
526 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
528 struct ui_out
*uiout
= current_uiout
;
534 error (_("-var-show-attributes: Usage: NAME."));
536 /* Get varobj handle, if a valid var obj name was specified */
537 var
= varobj_get_handle (argv
[0]);
539 attr
= varobj_get_attributes (var
);
540 /* FIXME: define masks for attributes */
541 if (attr
& 0x00000001)
544 attstr
= "noneditable";
546 ui_out_field_string (uiout
, "attr", attstr
);
550 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
552 struct ui_out
*uiout
= current_uiout
;
555 enum varobj_display_formats format
;
564 static struct mi_opt opts
[] =
570 /* Parse arguments */
571 format
= FORMAT_NATURAL
;
576 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
577 opts
, &optind
, &optarg
);
581 switch ((enum opt
) opt
)
585 error (_("Cannot specify format more than once"));
587 format
= mi_parse_format (optarg
);
594 error (_("Usage: [-f FORMAT] NAME"));
596 if (optind
< argc
- 1)
597 error (_("Garbage at end of command"));
599 /* Get varobj handle, if a valid var obj name was specified */
600 var
= varobj_get_handle (argv
[optind
]);
604 char *val
= varobj_get_formatted_value (var
, format
);
606 ui_out_field_string (uiout
, "value", val
);
611 char *val
= varobj_get_value (var
);
613 ui_out_field_string (uiout
, "value", val
);
619 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
621 struct ui_out
*uiout
= current_uiout
;
623 char *expression
, *val
;
626 error (_("-var-assign: Usage: NAME EXPRESSION."));
628 /* Get varobj handle, if a valid var obj name was specified */
629 var
= varobj_get_handle (argv
[0]);
631 if (!varobj_editable_p (var
))
632 error (_("-var-assign: Variable object is not editable"));
634 expression
= xstrdup (argv
[1]);
636 if (!varobj_set_value (var
, expression
))
637 error (_("-var-assign: Could not assign "
638 "expression to variable object"));
640 val
= varobj_get_value (var
);
641 ui_out_field_string (uiout
, "value", val
);
645 /* Type used for parameters passing to mi_cmd_var_update_iter. */
647 struct mi_cmd_var_update
650 enum print_values print_values
;
653 /* Helper for mi_cmd_var_update - update each VAR. */
656 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
658 struct mi_cmd_var_update
*data
= data_pointer
;
659 int thread_id
, thread_stopped
;
661 thread_id
= varobj_get_thread_id (var
);
663 if (thread_id
== -1 && is_stopped (inferior_ptid
))
667 struct thread_info
*tp
= find_thread_id (thread_id
);
670 thread_stopped
= is_stopped (tp
->ptid
);
676 if (!data
->only_floating
|| varobj_floating_p (var
))
677 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
681 mi_cmd_var_update (char *command
, char **argv
, int argc
)
683 struct ui_out
*uiout
= current_uiout
;
684 struct cleanup
*cleanup
;
686 enum print_values print_values
;
688 if (argc
!= 1 && argc
!= 2)
689 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
697 print_values
= mi_parse_values_option (argv
[0]);
699 print_values
= PRINT_NO_VALUES
;
701 if (mi_version (uiout
) <= 1)
702 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
704 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
706 /* Check if the parameter is a "*" which means that we want
707 to update all variables */
709 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
711 struct mi_cmd_var_update data
;
713 data
.only_floating
= *name
== '@';
714 data
.print_values
= print_values
;
716 /* varobj_update_one automatically updates all the children of VAROBJ.
717 Therefore update each VAROBJ only once by iterating only the root
720 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
724 /* Get varobj handle, if a valid var obj name was specified */
725 struct varobj
*var
= varobj_get_handle (name
);
727 varobj_update_one (var
, print_values
, 1 /* explicit */);
730 do_cleanups (cleanup
);
733 /* Helper for mi_cmd_var_update(). */
736 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
739 struct ui_out
*uiout
= current_uiout
;
740 struct cleanup
*cleanup
= NULL
;
741 VEC (varobj_update_result
) *changes
;
742 varobj_update_result
*r
;
745 changes
= varobj_update (&var
, explicit);
747 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
752 if (mi_version (uiout
) > 1)
753 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
754 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
758 case VAROBJ_IN_SCOPE
:
759 if (mi_print_value_p (r
->varobj
, print_values
))
761 char *val
= varobj_get_value (r
->varobj
);
763 ui_out_field_string (uiout
, "value", val
);
766 ui_out_field_string (uiout
, "in_scope", "true");
768 case VAROBJ_NOT_IN_SCOPE
:
769 ui_out_field_string (uiout
, "in_scope", "false");
772 ui_out_field_string (uiout
, "in_scope", "invalid");
776 if (r
->status
!= VAROBJ_INVALID
)
779 ui_out_field_string (uiout
, "type_changed", "true");
781 ui_out_field_string (uiout
, "type_changed", "false");
785 ui_out_field_string (uiout
, "new_type", varobj_get_type (r
->varobj
));
787 if (r
->type_changed
|| r
->children_changed
)
788 ui_out_field_int (uiout
, "new_num_children",
789 varobj_get_num_children (r
->varobj
));
791 display_hint
= varobj_get_display_hint (var
);
794 ui_out_field_string (uiout
, "displayhint", display_hint
);
795 xfree (display_hint
);
798 if (varobj_pretty_printed_p (var
))
799 ui_out_field_int (uiout
, "dynamic", 1);
801 varobj_get_child_range (r
->varobj
, &from
, &to
);
802 ui_out_field_int (uiout
, "has_more",
803 varobj_has_more (r
->varobj
, to
));
809 struct cleanup
*cleanup
;
811 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
812 for (j
= 0; VEC_iterate (varobj_p
, r
->new, j
, child
); ++j
)
814 struct cleanup
*cleanup_child
;
817 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
818 print_varobj (child
, print_values
, 1 /* print_expression */);
819 do_cleanups (cleanup_child
);
822 do_cleanups (cleanup
);
823 VEC_free (varobj_p
, r
->new);
824 r
->new = NULL
; /* Paranoia. */
827 if (mi_version (uiout
) > 1)
828 do_cleanups (cleanup
);
830 VEC_free (varobj_update_result
, changes
);
834 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
837 error (_("-enable-pretty-printing: no arguments allowed"));
838 varobj_enable_pretty_printing ();
842 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
848 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
850 var
= varobj_get_handle (argv
[0]);
851 from
= atoi (argv
[1]);
854 varobj_set_child_range (var
, from
, to
);