1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions (a Red Hat company).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "mi-getopt.h"
31 #include "gdbthread.h"
34 extern unsigned int varobjdebug
; /* defined in varobj.c. */
36 static void varobj_update_one (struct varobj
*var
,
37 enum print_values print_values
,
40 static int mi_print_value_p (struct varobj
*var
,
41 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. */
48 print_varobj (struct varobj
*var
, enum print_values print_values
,
51 struct ui_out
*uiout
= current_uiout
;
56 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
59 char *exp
= varobj_get_expression (var
);
61 ui_out_field_string (uiout
, "exp", exp
);
64 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
66 if (mi_print_value_p (var
, print_values
))
68 char *val
= varobj_get_value (var
);
70 ui_out_field_string (uiout
, "value", val
);
74 type
= varobj_get_type (var
);
77 ui_out_field_string (uiout
, "type", type
);
81 thread_id
= varobj_get_thread_id (var
);
83 ui_out_field_int (uiout
, "thread-id", thread_id
);
85 if (varobj_get_frozen (var
))
86 ui_out_field_int (uiout
, "frozen", 1);
88 display_hint
= varobj_get_display_hint (var
);
91 ui_out_field_string (uiout
, "displayhint", display_hint
);
95 if (varobj_is_dynamic_p (var
))
96 ui_out_field_int (uiout
, "dynamic", 1);
99 /* VAROBJ operations */
102 mi_cmd_var_create (char *command
, char **argv
, int argc
)
104 struct ui_out
*uiout
= current_uiout
;
105 CORE_ADDR frameaddr
= 0;
110 struct cleanup
*old_cleanups
;
111 enum varobj_type var_type
;
114 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
116 name
= xstrdup (argv
[0]);
117 /* Add cleanup for name. Must be free_current_contents as name can
119 old_cleanups
= make_cleanup (free_current_contents
, &name
);
121 frame
= xstrdup (argv
[1]);
122 make_cleanup (xfree
, frame
);
124 expr
= xstrdup (argv
[2]);
125 make_cleanup (xfree
, expr
);
127 if (strcmp (name
, "-") == 0)
130 name
= varobj_gen_name ();
132 else if (!isalpha (*name
))
133 error (_("-var-create: name of object must begin with a letter"));
135 if (strcmp (frame
, "*") == 0)
136 var_type
= USE_CURRENT_FRAME
;
137 else if (strcmp (frame
, "@") == 0)
138 var_type
= USE_SELECTED_FRAME
;
141 var_type
= USE_SPECIFIED_FRAME
;
142 frameaddr
= string_to_core_addr (frame
);
146 fprintf_unfiltered (gdb_stdlog
,
147 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
148 name
, frame
, hex_string (frameaddr
), expr
);
150 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
153 error (_("-var-create: unable to create variable object"));
155 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
157 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, 0));
159 do_cleanups (old_cleanups
);
163 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
168 int children_only_p
= 0;
169 struct cleanup
*old_cleanups
;
170 struct ui_out
*uiout
= current_uiout
;
172 if (argc
< 1 || argc
> 2)
173 error (_("-var-delete: Usage: [-c] EXPRESSION."));
175 name
= xstrdup (argv
[0]);
176 /* Add cleanup for name. Must be free_current_contents as name can
178 old_cleanups
= make_cleanup (free_current_contents
, &name
);
180 /* If we have one single argument it cannot be '-c' or any string
181 starting with '-'. */
184 if (strcmp (name
, "-c") == 0)
185 error (_("-var-delete: Missing required "
186 "argument after '-c': variable object name"));
188 error (_("-var-delete: Illegal variable object name"));
191 /* If we have 2 arguments they must be '-c' followed by a string
192 which would be the variable name. */
195 if (strcmp (name
, "-c") != 0)
196 error (_("-var-delete: Invalid option."));
198 do_cleanups (old_cleanups
);
199 name
= xstrdup (argv
[1]);
200 old_cleanups
= make_cleanup (free_current_contents
, &name
);
203 /* If we didn't error out, now NAME contains the name of the
206 var
= varobj_get_handle (name
);
208 numdel
= varobj_delete (var
, NULL
, children_only_p
);
210 ui_out_field_int (uiout
, "ndeleted", numdel
);
212 do_cleanups (old_cleanups
);
215 /* Parse a string argument into a format value. */
217 static enum varobj_display_formats
218 mi_parse_format (const char *arg
)
226 if (strncmp (arg
, "natural", len
) == 0)
227 return FORMAT_NATURAL
;
228 else if (strncmp (arg
, "binary", len
) == 0)
229 return FORMAT_BINARY
;
230 else if (strncmp (arg
, "decimal", len
) == 0)
231 return FORMAT_DECIMAL
;
232 else if (strncmp (arg
, "hexadecimal", len
) == 0)
233 return FORMAT_HEXADECIMAL
;
234 else if (strncmp (arg
, "octal", len
) == 0)
236 else if (strncmp (arg
, "zero-hexadecimal", len
) == 0)
237 return FORMAT_ZHEXADECIMAL
;
240 error (_("Must specify the format as: \"natural\", "
241 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
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 the 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
309 new values during unfreezing. If this information is required,
310 client should call -var-update explicitly. */
314 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
316 struct ui_out
*uiout
= current_uiout
;
317 enum varobj_display_formats format
;
321 error (_("-var-show-format: Usage: NAME."));
323 /* Get varobj handle, if a valid var obj name was specified. */
324 var
= varobj_get_handle (argv
[0]);
326 format
= varobj_get_display_format (var
);
328 /* Report the current format. */
329 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
333 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
335 struct ui_out
*uiout
= current_uiout
;
339 error (_("-var-info-num-children: Usage: NAME."));
341 /* Get varobj handle, if a valid var obj name was specified. */
342 var
= varobj_get_handle (argv
[0]);
344 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
347 /* Return 1 if given the argument PRINT_VALUES we should display
351 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
355 if (print_values
== PRINT_NO_VALUES
)
358 if (print_values
== PRINT_ALL_VALUES
)
361 if (varobj_is_dynamic_p (var
))
364 type
= varobj_get_gdb_type (var
);
369 type
= check_typedef (type
);
371 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
372 and that type is not a compound type. */
373 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
374 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
375 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
380 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
382 struct ui_out
*uiout
= current_uiout
;
384 VEC(varobj_p
) *children
;
385 struct varobj
*child
;
386 enum print_values print_values
;
391 if (argc
< 1 || argc
> 4)
392 error (_("-var-list-children: Usage: "
393 "[PRINT_VALUES] NAME [FROM TO]"));
395 /* Get varobj handle, if a valid var obj name was specified. */
396 if (argc
== 1 || argc
== 3)
397 var
= varobj_get_handle (argv
[0]);
399 var
= varobj_get_handle (argv
[1]);
403 from
= atoi (argv
[argc
- 2]);
404 to
= atoi (argv
[argc
- 1]);
412 children
= varobj_list_children (var
, &from
, &to
);
413 ui_out_field_int (uiout
, "numchild", to
- from
);
414 if (argc
== 2 || argc
== 4)
415 print_values
= mi_parse_print_values (argv
[0]);
417 print_values
= PRINT_NO_VALUES
;
419 display_hint
= varobj_get_display_hint (var
);
422 ui_out_field_string (uiout
, "displayhint", display_hint
);
423 xfree (display_hint
);
428 struct cleanup
*cleanup_children
;
430 if (mi_version (uiout
) == 1)
432 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
435 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
437 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
440 struct cleanup
*cleanup_child
;
442 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
443 print_varobj (child
, print_values
, 1 /* print expression */);
444 do_cleanups (cleanup_child
);
446 do_cleanups (cleanup_children
);
449 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
453 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
455 struct ui_out
*uiout
= current_uiout
;
460 error (_("-var-info-type: Usage: NAME."));
462 /* Get varobj handle, if a valid var obj name was specified. */
463 var
= varobj_get_handle (argv
[0]);
464 type_name
= varobj_get_type (var
);
466 ui_out_field_string (uiout
, "type", type_name
);
472 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
474 struct ui_out
*uiout
= current_uiout
;
479 error (_("Usage: NAME."));
481 /* Get varobj handle, if a valid var obj name was specified. */
482 var
= varobj_get_handle (argv
[0]);
484 path_expr
= varobj_get_path_expr (var
);
486 ui_out_field_string (uiout
, "path_expr", path_expr
);
490 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
492 struct ui_out
*uiout
= current_uiout
;
493 const struct language_defn
*lang
;
498 error (_("-var-info-expression: Usage: NAME."));
500 /* Get varobj handle, if a valid var obj name was specified. */
501 var
= varobj_get_handle (argv
[0]);
503 lang
= varobj_get_language (var
);
505 ui_out_field_string (uiout
, "lang", lang
->la_natural_name
);
507 exp
= varobj_get_expression (var
);
508 ui_out_field_string (uiout
, "exp", exp
);
513 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
515 struct ui_out
*uiout
= current_uiout
;
521 error (_("-var-show-attributes: Usage: NAME."));
523 /* Get varobj handle, if a valid var obj name was specified */
524 var
= varobj_get_handle (argv
[0]);
526 attr
= varobj_get_attributes (var
);
527 /* FIXME: define masks for attributes */
528 if (attr
& 0x00000001)
531 attstr
= "noneditable";
533 ui_out_field_string (uiout
, "attr", attstr
);
537 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
539 struct ui_out
*uiout
= current_uiout
;
542 enum varobj_display_formats format
;
551 static const struct mi_opt opts
[] =
557 /* Parse arguments. */
558 format
= FORMAT_NATURAL
;
563 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
568 switch ((enum opt
) opt
)
572 error (_("Cannot specify format more than once"));
574 format
= mi_parse_format (oarg
);
581 error (_("Usage: [-f FORMAT] NAME"));
584 error (_("Garbage at end of command"));
586 /* Get varobj handle, if a valid var obj name was specified. */
587 var
= varobj_get_handle (argv
[oind
]);
591 char *val
= varobj_get_formatted_value (var
, format
);
593 ui_out_field_string (uiout
, "value", val
);
598 char *val
= varobj_get_value (var
);
600 ui_out_field_string (uiout
, "value", val
);
606 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
608 struct ui_out
*uiout
= current_uiout
;
610 char *expression
, *val
;
611 struct cleanup
*cleanup
;
614 error (_("-var-assign: Usage: NAME EXPRESSION."));
616 /* Get varobj handle, if a valid var obj name was specified. */
617 var
= varobj_get_handle (argv
[0]);
619 if (!varobj_editable_p (var
))
620 error (_("-var-assign: Variable object is not editable"));
622 expression
= xstrdup (argv
[1]);
624 /* MI command '-var-assign' may write memory, so suppress memory
625 changed notification if it does. */
627 = make_cleanup_restore_integer (&mi_suppress_notification
.memory
);
628 mi_suppress_notification
.memory
= 1;
630 if (!varobj_set_value (var
, expression
))
631 error (_("-var-assign: Could not assign "
632 "expression to variable object"));
634 val
= varobj_get_value (var
);
635 ui_out_field_string (uiout
, "value", val
);
638 do_cleanups (cleanup
);
641 /* Type used for parameters passing to mi_cmd_var_update_iter. */
643 struct mi_cmd_var_update
646 enum print_values print_values
;
649 /* Helper for mi_cmd_var_update - update each VAR. */
652 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
654 struct mi_cmd_var_update
*data
= (struct mi_cmd_var_update
*) data_pointer
;
655 int thread_id
, thread_stopped
;
657 thread_id
= varobj_get_thread_id (var
);
660 && (ptid_equal (inferior_ptid
, null_ptid
)
661 || is_stopped (inferior_ptid
)))
665 struct thread_info
*tp
= find_thread_id (thread_id
);
668 thread_stopped
= is_stopped (tp
->ptid
);
674 && (!data
->only_floating
|| varobj_floating_p (var
)))
675 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
679 mi_cmd_var_update (char *command
, char **argv
, int argc
)
681 struct ui_out
*uiout
= current_uiout
;
682 struct cleanup
*cleanup
;
684 enum print_values print_values
;
686 if (argc
!= 1 && argc
!= 2)
687 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
695 print_values
= mi_parse_print_values (argv
[0]);
697 print_values
= PRINT_NO_VALUES
;
699 if (mi_version (uiout
) <= 1)
700 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
702 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
704 /* Check if the parameter is a "*", which means that we want to
705 update all variables. */
707 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
709 struct mi_cmd_var_update data
;
711 data
.only_floating
= (*name
== '@');
712 data
.print_values
= print_values
;
714 /* varobj_update_one automatically updates all the children of
715 VAROBJ. Therefore update each VAROBJ only once by iterating
716 only the root VAROBJs. */
718 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
722 /* Get varobj handle, if a valid var obj name was specified. */
723 struct varobj
*var
= varobj_get_handle (name
);
725 varobj_update_one (var
, print_values
, 1 /* explicit */);
728 do_cleanups (cleanup
);
731 /* Helper for mi_cmd_var_update(). */
734 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
737 struct ui_out
*uiout
= current_uiout
;
738 VEC (varobj_update_result
) *changes
;
739 varobj_update_result
*r
;
742 changes
= varobj_update (&var
, is_explicit
);
744 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
748 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
750 if (mi_version (uiout
) > 1)
751 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
752 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
756 case VAROBJ_IN_SCOPE
:
757 if (mi_print_value_p (r
->varobj
, print_values
))
759 char *val
= varobj_get_value (r
->varobj
);
761 ui_out_field_string (uiout
, "value", val
);
764 ui_out_field_string (uiout
, "in_scope", "true");
766 case VAROBJ_NOT_IN_SCOPE
:
767 ui_out_field_string (uiout
, "in_scope", "false");
770 ui_out_field_string (uiout
, "in_scope", "invalid");
774 if (r
->status
!= VAROBJ_INVALID
)
777 ui_out_field_string (uiout
, "type_changed", "true");
779 ui_out_field_string (uiout
, "type_changed", "false");
784 char *type_name
= varobj_get_type (r
->varobj
);
786 ui_out_field_string (uiout
, "new_type", type_name
);
790 if (r
->type_changed
|| r
->children_changed
)
791 ui_out_field_int (uiout
, "new_num_children",
792 varobj_get_num_children (r
->varobj
));
794 display_hint
= varobj_get_display_hint (r
->varobj
);
797 ui_out_field_string (uiout
, "displayhint", display_hint
);
798 xfree (display_hint
);
801 if (varobj_is_dynamic_p (r
->varobj
))
802 ui_out_field_int (uiout
, "dynamic", 1);
804 varobj_get_child_range (r
->varobj
, &from
, &to
);
805 ui_out_field_int (uiout
, "has_more",
806 varobj_has_more (r
->varobj
, to
));
812 struct cleanup
*cleanup
;
814 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
815 for (j
= 0; VEC_iterate (varobj_p
, r
->newobj
, j
, child
); ++j
)
817 struct cleanup
*cleanup_child
;
820 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
821 print_varobj (child
, print_values
, 1 /* print_expression */);
822 do_cleanups (cleanup_child
);
825 do_cleanups (cleanup
);
826 VEC_free (varobj_p
, r
->newobj
);
827 r
->newobj
= NULL
; /* Paranoia. */
830 do_cleanups (cleanup
);
832 VEC_free (varobj_update_result
, changes
);
836 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
839 error (_("-enable-pretty-printing: no arguments allowed"));
841 varobj_enable_pretty_printing ();
845 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
851 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
853 var
= varobj_get_handle (argv
[0]);
854 from
= atoi (argv
[1]);
857 varobj_set_child_range (var
, from
, to
);