1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000, 2002, 2004-2005, 2007-2012 Free Software
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/>. */
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 unsigned 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. */
52 print_varobj (struct varobj
*var
, enum print_values print_values
,
55 struct ui_out
*uiout
= current_uiout
;
60 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
62 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
63 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
65 if (mi_print_value_p (var
, print_values
))
67 char *val
= varobj_get_value (var
);
69 ui_out_field_string (uiout
, "value", val
);
73 type
= varobj_get_type (var
);
76 ui_out_field_string (uiout
, "type", type
);
80 thread_id
= varobj_get_thread_id (var
);
82 ui_out_field_int (uiout
, "thread-id", thread_id
);
84 if (varobj_get_frozen (var
))
85 ui_out_field_int (uiout
, "frozen", 1);
87 display_hint
= varobj_get_display_hint (var
);
90 ui_out_field_string (uiout
, "displayhint", display_hint
);
94 if (varobj_pretty_printed_p (var
))
95 ui_out_field_int (uiout
, "dynamic", 1);
98 /* VAROBJ operations */
101 mi_cmd_var_create (char *command
, char **argv
, int argc
)
103 struct ui_out
*uiout
= current_uiout
;
104 CORE_ADDR frameaddr
= 0;
109 struct cleanup
*old_cleanups
;
110 enum varobj_type var_type
;
113 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
115 name
= xstrdup (argv
[0]);
116 /* Add cleanup for name. Must be free_current_contents as name can
118 old_cleanups
= make_cleanup (free_current_contents
, &name
);
120 frame
= xstrdup (argv
[1]);
121 make_cleanup (xfree
, frame
);
123 expr
= xstrdup (argv
[2]);
124 make_cleanup (xfree
, expr
);
126 if (strcmp (name
, "-") == 0)
129 name
= varobj_gen_name ();
131 else if (!isalpha (*name
))
132 error (_("-var-create: name of object must begin with a letter"));
134 if (strcmp (frame
, "*") == 0)
135 var_type
= USE_CURRENT_FRAME
;
136 else if (strcmp (frame
, "@") == 0)
137 var_type
= USE_SELECTED_FRAME
;
140 var_type
= USE_SPECIFIED_FRAME
;
141 frameaddr
= string_to_core_addr (frame
);
145 fprintf_unfiltered (gdb_stdlog
,
146 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
147 name
, frame
, hex_string (frameaddr
), expr
);
149 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
152 error (_("-var-create: unable to create variable object"));
154 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
156 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, 0));
158 do_cleanups (old_cleanups
);
162 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
167 int children_only_p
= 0;
168 struct cleanup
*old_cleanups
;
169 struct ui_out
*uiout
= current_uiout
;
171 if (argc
< 1 || argc
> 2)
172 error (_("-var-delete: Usage: [-c] EXPRESSION."));
174 name
= xstrdup (argv
[0]);
175 /* Add cleanup for name. Must be free_current_contents as name can
177 old_cleanups
= make_cleanup (free_current_contents
, &name
);
179 /* If we have one single argument it cannot be '-c' or any string
180 starting with '-'. */
183 if (strcmp (name
, "-c") == 0)
184 error (_("-var-delete: Missing required "
185 "argument after '-c': variable object name"));
187 error (_("-var-delete: Illegal variable object name"));
190 /* If we have 2 arguments they must be '-c' followed by a string
191 which would be the variable name. */
194 if (strcmp (name
, "-c") != 0)
195 error (_("-var-delete: Invalid option."));
197 do_cleanups (old_cleanups
);
198 name
= xstrdup (argv
[1]);
199 old_cleanups
= make_cleanup (free_current_contents
, &name
);
202 /* If we didn't error out, now NAME contains the name of the
205 var
= varobj_get_handle (name
);
207 numdel
= varobj_delete (var
, NULL
, children_only_p
);
209 ui_out_field_int (uiout
, "ndeleted", numdel
);
211 do_cleanups (old_cleanups
);
214 /* Parse a string argument into a format value. */
216 static enum varobj_display_formats
217 mi_parse_format (const char *arg
)
225 if (strncmp (arg
, "natural", len
) == 0)
226 return FORMAT_NATURAL
;
227 else if (strncmp (arg
, "binary", len
) == 0)
228 return FORMAT_BINARY
;
229 else if (strncmp (arg
, "decimal", len
) == 0)
230 return FORMAT_DECIMAL
;
231 else if (strncmp (arg
, "hexadecimal", len
) == 0)
232 return FORMAT_HEXADECIMAL
;
233 else if (strncmp (arg
, "octal", len
) == 0)
237 error (_("Must specify the format as: \"natural\", "
238 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
242 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
244 enum varobj_display_formats format
;
247 struct ui_out
*uiout
= current_uiout
;
250 error (_("-var-set-format: Usage: NAME FORMAT."));
252 /* Get varobj handle, if a valid var obj name was specified. */
253 var
= varobj_get_handle (argv
[0]);
255 format
= mi_parse_format (argv
[1]);
257 /* Set the format of VAR to the given format. */
258 varobj_set_display_format (var
, format
);
260 /* Report the new current format. */
261 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
263 /* Report the value in the new format. */
264 val
= varobj_get_value (var
);
265 ui_out_field_string (uiout
, "value", val
);
270 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
275 error (_("Usage: NAME VISUALIZER_FUNCTION."));
277 var
= varobj_get_handle (argv
[0]);
280 error (_("Variable object not found"));
282 varobj_set_visualizer (var
, argv
[1]);
286 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
292 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
294 var
= varobj_get_handle (argv
[0]);
296 if (strcmp (argv
[1], "0") == 0)
298 else if (strcmp (argv
[1], "1") == 0)
301 error (_("Invalid flag value"));
303 varobj_set_frozen (var
, frozen
);
305 /* We don't automatically return the new value, or what varobjs got
306 new values during unfreezing. If this information is required,
307 client should call -var-update explicitly. */
311 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
313 struct ui_out
*uiout
= current_uiout
;
314 enum varobj_display_formats format
;
318 error (_("-var-show-format: Usage: NAME."));
320 /* Get varobj handle, if a valid var obj name was specified. */
321 var
= varobj_get_handle (argv
[0]);
323 format
= varobj_get_display_format (var
);
325 /* Report the current format. */
326 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
330 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
332 struct ui_out
*uiout
= current_uiout
;
336 error (_("-var-info-num-children: Usage: NAME."));
338 /* Get varobj handle, if a valid var obj name was specified. */
339 var
= varobj_get_handle (argv
[0]);
341 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
344 /* Parse a string argument into a print_values value. */
346 static enum print_values
347 mi_parse_values_option (const char *arg
)
349 if (strcmp (arg
, "0") == 0
350 || strcmp (arg
, mi_no_values
) == 0)
351 return PRINT_NO_VALUES
;
352 else if (strcmp (arg
, "1") == 0
353 || strcmp (arg
, mi_all_values
) == 0)
354 return PRINT_ALL_VALUES
;
355 else if (strcmp (arg
, "2") == 0
356 || strcmp (arg
, mi_simple_values
) == 0)
357 return PRINT_SIMPLE_VALUES
;
359 error (_("Unknown value for PRINT_VALUES\n\
360 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
361 mi_no_values
, mi_simple_values
, mi_all_values
);
364 /* Return 1 if given the argument PRINT_VALUES we should display
368 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
372 if (print_values
== PRINT_NO_VALUES
)
375 if (print_values
== PRINT_ALL_VALUES
)
378 if (varobj_pretty_printed_p (var
))
381 type
= varobj_get_gdb_type (var
);
386 type
= check_typedef (type
);
388 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
389 and that type is not a compound type. */
390 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
391 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
392 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
397 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
399 struct ui_out
*uiout
= current_uiout
;
401 VEC(varobj_p
) *children
;
402 struct varobj
*child
;
403 enum print_values print_values
;
408 if (argc
< 1 || argc
> 4)
409 error (_("-var-list-children: Usage: "
410 "[PRINT_VALUES] NAME [FROM TO]"));
412 /* Get varobj handle, if a valid var obj name was specified. */
413 if (argc
== 1 || argc
== 3)
414 var
= varobj_get_handle (argv
[0]);
416 var
= varobj_get_handle (argv
[1]);
420 from
= atoi (argv
[argc
- 2]);
421 to
= atoi (argv
[argc
- 1]);
429 children
= varobj_list_children (var
, &from
, &to
);
430 ui_out_field_int (uiout
, "numchild", to
- from
);
431 if (argc
== 2 || argc
== 4)
432 print_values
= mi_parse_values_option (argv
[0]);
434 print_values
= PRINT_NO_VALUES
;
436 display_hint
= varobj_get_display_hint (var
);
439 ui_out_field_string (uiout
, "displayhint", display_hint
);
440 xfree (display_hint
);
445 struct cleanup
*cleanup_children
;
447 if (mi_version (uiout
) == 1)
449 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
452 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
454 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
457 struct cleanup
*cleanup_child
;
459 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
460 print_varobj (child
, print_values
, 1 /* print expression */);
461 do_cleanups (cleanup_child
);
463 do_cleanups (cleanup_children
);
466 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
470 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
472 struct ui_out
*uiout
= current_uiout
;
476 error (_("-var-info-type: Usage: NAME."));
478 /* Get varobj handle, if a valid var obj name was specified. */
479 var
= varobj_get_handle (argv
[0]);
481 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
485 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
487 struct ui_out
*uiout
= current_uiout
;
492 error (_("Usage: NAME."));
494 /* Get varobj handle, if a valid var obj name was specified. */
495 var
= varobj_get_handle (argv
[0]);
497 path_expr
= varobj_get_path_expr (var
);
499 ui_out_field_string (uiout
, "path_expr", path_expr
);
503 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
505 struct ui_out
*uiout
= current_uiout
;
506 enum varobj_languages lang
;
510 error (_("-var-info-expression: Usage: NAME."));
512 /* Get varobj handle, if a valid var obj name was specified. */
513 var
= varobj_get_handle (argv
[0]);
515 lang
= varobj_get_language (var
);
517 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
518 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
522 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
524 struct ui_out
*uiout
= current_uiout
;
530 error (_("-var-show-attributes: Usage: NAME."));
532 /* Get varobj handle, if a valid var obj name was specified */
533 var
= varobj_get_handle (argv
[0]);
535 attr
= varobj_get_attributes (var
);
536 /* FIXME: define masks for attributes */
537 if (attr
& 0x00000001)
540 attstr
= "noneditable";
542 ui_out_field_string (uiout
, "attr", attstr
);
546 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
548 struct ui_out
*uiout
= current_uiout
;
551 enum varobj_display_formats format
;
560 static const struct mi_opt opts
[] =
566 /* Parse arguments. */
567 format
= FORMAT_NATURAL
;
572 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
577 switch ((enum opt
) opt
)
581 error (_("Cannot specify format more than once"));
583 format
= mi_parse_format (oarg
);
590 error (_("Usage: [-f FORMAT] NAME"));
593 error (_("Garbage at end of command"));
595 /* Get varobj handle, if a valid var obj name was specified. */
596 var
= varobj_get_handle (argv
[oind
]);
600 char *val
= varobj_get_formatted_value (var
, format
);
602 ui_out_field_string (uiout
, "value", val
);
607 char *val
= varobj_get_value (var
);
609 ui_out_field_string (uiout
, "value", val
);
615 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
617 struct ui_out
*uiout
= current_uiout
;
619 char *expression
, *val
;
620 struct cleanup
*cleanup
;
623 error (_("-var-assign: Usage: NAME EXPRESSION."));
625 /* Get varobj handle, if a valid var obj name was specified. */
626 var
= varobj_get_handle (argv
[0]);
628 if (!varobj_editable_p (var
))
629 error (_("-var-assign: Variable object is not editable"));
631 expression
= xstrdup (argv
[1]);
633 /* MI command '-var-assign' may write memory, so suppress memory
634 changed notification if it does. */
636 = make_cleanup_restore_integer (&mi_suppress_notification
.memory
);
637 mi_suppress_notification
.memory
= 1;
639 if (!varobj_set_value (var
, expression
))
640 error (_("-var-assign: Could not assign "
641 "expression to variable object"));
643 val
= varobj_get_value (var
);
644 ui_out_field_string (uiout
, "value", val
);
647 do_cleanups (cleanup
);
650 /* Type used for parameters passing to mi_cmd_var_update_iter. */
652 struct mi_cmd_var_update
655 enum print_values print_values
;
658 /* Helper for mi_cmd_var_update - update each VAR. */
661 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
663 struct mi_cmd_var_update
*data
= data_pointer
;
664 int thread_id
, thread_stopped
;
666 thread_id
= varobj_get_thread_id (var
);
668 if (thread_id
== -1 && is_stopped (inferior_ptid
))
672 struct thread_info
*tp
= find_thread_id (thread_id
);
675 thread_stopped
= is_stopped (tp
->ptid
);
681 && (!data
->only_floating
|| varobj_floating_p (var
)))
682 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
686 mi_cmd_var_update (char *command
, char **argv
, int argc
)
688 struct ui_out
*uiout
= current_uiout
;
689 struct cleanup
*cleanup
;
691 enum print_values print_values
;
693 if (argc
!= 1 && argc
!= 2)
694 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
702 print_values
= mi_parse_values_option (argv
[0]);
704 print_values
= PRINT_NO_VALUES
;
706 if (mi_version (uiout
) <= 1)
707 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
709 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
711 /* Check if the parameter is a "*", which means that we want to
712 update all variables. */
714 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
716 struct mi_cmd_var_update data
;
718 data
.only_floating
= (*name
== '@');
719 data
.print_values
= print_values
;
721 /* varobj_update_one automatically updates all the children of
722 VAROBJ. Therefore update each VAROBJ only once by iterating
723 only the root VAROBJs. */
725 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
729 /* Get varobj handle, if a valid var obj name was specified. */
730 struct varobj
*var
= varobj_get_handle (name
);
732 varobj_update_one (var
, print_values
, 1 /* explicit */);
735 do_cleanups (cleanup
);
738 /* Helper for mi_cmd_var_update(). */
741 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
744 struct ui_out
*uiout
= current_uiout
;
745 struct cleanup
*cleanup
= NULL
;
746 VEC (varobj_update_result
) *changes
;
747 varobj_update_result
*r
;
750 changes
= varobj_update (&var
, explicit);
752 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
757 if (mi_version (uiout
) > 1)
758 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
759 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
763 case VAROBJ_IN_SCOPE
:
764 if (mi_print_value_p (r
->varobj
, print_values
))
766 char *val
= varobj_get_value (r
->varobj
);
768 ui_out_field_string (uiout
, "value", val
);
771 ui_out_field_string (uiout
, "in_scope", "true");
773 case VAROBJ_NOT_IN_SCOPE
:
774 ui_out_field_string (uiout
, "in_scope", "false");
777 ui_out_field_string (uiout
, "in_scope", "invalid");
781 if (r
->status
!= VAROBJ_INVALID
)
784 ui_out_field_string (uiout
, "type_changed", "true");
786 ui_out_field_string (uiout
, "type_changed", "false");
790 ui_out_field_string (uiout
, "new_type", varobj_get_type (r
->varobj
));
792 if (r
->type_changed
|| r
->children_changed
)
793 ui_out_field_int (uiout
, "new_num_children",
794 varobj_get_num_children (r
->varobj
));
796 display_hint
= varobj_get_display_hint (r
->varobj
);
799 ui_out_field_string (uiout
, "displayhint", display_hint
);
800 xfree (display_hint
);
803 if (varobj_pretty_printed_p (r
->varobj
))
804 ui_out_field_int (uiout
, "dynamic", 1);
806 varobj_get_child_range (r
->varobj
, &from
, &to
);
807 ui_out_field_int (uiout
, "has_more",
808 varobj_has_more (r
->varobj
, to
));
814 struct cleanup
*cleanup
;
816 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
817 for (j
= 0; VEC_iterate (varobj_p
, r
->new, j
, child
); ++j
)
819 struct cleanup
*cleanup_child
;
822 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
823 print_varobj (child
, print_values
, 1 /* print_expression */);
824 do_cleanups (cleanup_child
);
827 do_cleanups (cleanup
);
828 VEC_free (varobj_p
, r
->new);
829 r
->new = NULL
; /* Paranoia. */
832 if (mi_version (uiout
) > 1)
833 do_cleanups (cleanup
);
835 VEC_free (varobj_update_result
, changes
);
839 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
842 error (_("-enable-pretty-printing: no arguments allowed"));
844 varobj_enable_pretty_printing ();
848 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
854 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
856 var
= varobj_get_handle (argv
[0]);
857 from
= atoi (argv
[1]);
860 varobj_set_child_range (var
, from
, to
);