1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2015 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)
238 error (_("Must specify the format as: \"natural\", "
239 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
243 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
245 enum varobj_display_formats format
;
248 struct ui_out
*uiout
= current_uiout
;
251 error (_("-var-set-format: Usage: NAME FORMAT."));
253 /* Get varobj handle, if a valid var obj name was specified. */
254 var
= varobj_get_handle (argv
[0]);
256 format
= mi_parse_format (argv
[1]);
258 /* Set the format of VAR to the given format. */
259 varobj_set_display_format (var
, format
);
261 /* Report the new current format. */
262 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
264 /* Report the value in the new format. */
265 val
= varobj_get_value (var
);
266 ui_out_field_string (uiout
, "value", val
);
271 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
276 error (_("Usage: NAME VISUALIZER_FUNCTION."));
278 var
= varobj_get_handle (argv
[0]);
281 error (_("Variable object not found"));
283 varobj_set_visualizer (var
, argv
[1]);
287 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
293 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
295 var
= varobj_get_handle (argv
[0]);
297 if (strcmp (argv
[1], "0") == 0)
299 else if (strcmp (argv
[1], "1") == 0)
302 error (_("Invalid flag value"));
304 varobj_set_frozen (var
, frozen
);
306 /* We don't automatically return the new value, or what varobjs got
307 new values during unfreezing. If this information is required,
308 client should call -var-update explicitly. */
312 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
314 struct ui_out
*uiout
= current_uiout
;
315 enum varobj_display_formats format
;
319 error (_("-var-show-format: Usage: NAME."));
321 /* Get varobj handle, if a valid var obj name was specified. */
322 var
= varobj_get_handle (argv
[0]);
324 format
= varobj_get_display_format (var
);
326 /* Report the current format. */
327 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
331 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
333 struct ui_out
*uiout
= current_uiout
;
337 error (_("-var-info-num-children: Usage: NAME."));
339 /* Get varobj handle, if a valid var obj name was specified. */
340 var
= varobj_get_handle (argv
[0]);
342 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
345 /* Return 1 if given the argument PRINT_VALUES we should display
349 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
353 if (print_values
== PRINT_NO_VALUES
)
356 if (print_values
== PRINT_ALL_VALUES
)
359 if (varobj_is_dynamic_p (var
))
362 type
= varobj_get_gdb_type (var
);
367 type
= check_typedef (type
);
369 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
370 and that type is not a compound type. */
371 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
372 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
373 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
378 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
380 struct ui_out
*uiout
= current_uiout
;
382 VEC(varobj_p
) *children
;
383 struct varobj
*child
;
384 enum print_values print_values
;
389 if (argc
< 1 || argc
> 4)
390 error (_("-var-list-children: Usage: "
391 "[PRINT_VALUES] NAME [FROM TO]"));
393 /* Get varobj handle, if a valid var obj name was specified. */
394 if (argc
== 1 || argc
== 3)
395 var
= varobj_get_handle (argv
[0]);
397 var
= varobj_get_handle (argv
[1]);
401 from
= atoi (argv
[argc
- 2]);
402 to
= atoi (argv
[argc
- 1]);
410 children
= varobj_list_children (var
, &from
, &to
);
411 ui_out_field_int (uiout
, "numchild", to
- from
);
412 if (argc
== 2 || argc
== 4)
413 print_values
= mi_parse_print_values (argv
[0]);
415 print_values
= PRINT_NO_VALUES
;
417 display_hint
= varobj_get_display_hint (var
);
420 ui_out_field_string (uiout
, "displayhint", display_hint
);
421 xfree (display_hint
);
426 struct cleanup
*cleanup_children
;
428 if (mi_version (uiout
) == 1)
430 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
433 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
435 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
438 struct cleanup
*cleanup_child
;
440 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
441 print_varobj (child
, print_values
, 1 /* print expression */);
442 do_cleanups (cleanup_child
);
444 do_cleanups (cleanup_children
);
447 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
451 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
453 struct ui_out
*uiout
= current_uiout
;
458 error (_("-var-info-type: Usage: NAME."));
460 /* Get varobj handle, if a valid var obj name was specified. */
461 var
= varobj_get_handle (argv
[0]);
462 type_name
= varobj_get_type (var
);
464 ui_out_field_string (uiout
, "type", type_name
);
470 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
472 struct ui_out
*uiout
= current_uiout
;
477 error (_("Usage: NAME."));
479 /* Get varobj handle, if a valid var obj name was specified. */
480 var
= varobj_get_handle (argv
[0]);
482 path_expr
= varobj_get_path_expr (var
);
484 ui_out_field_string (uiout
, "path_expr", path_expr
);
488 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
490 struct ui_out
*uiout
= current_uiout
;
491 const struct language_defn
*lang
;
496 error (_("-var-info-expression: Usage: NAME."));
498 /* Get varobj handle, if a valid var obj name was specified. */
499 var
= varobj_get_handle (argv
[0]);
501 lang
= varobj_get_language (var
);
503 ui_out_field_string (uiout
, "lang", lang
->la_natural_name
);
505 exp
= varobj_get_expression (var
);
506 ui_out_field_string (uiout
, "exp", exp
);
511 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
513 struct ui_out
*uiout
= current_uiout
;
519 error (_("-var-show-attributes: Usage: NAME."));
521 /* Get varobj handle, if a valid var obj name was specified */
522 var
= varobj_get_handle (argv
[0]);
524 attr
= varobj_get_attributes (var
);
525 /* FIXME: define masks for attributes */
526 if (attr
& 0x00000001)
529 attstr
= "noneditable";
531 ui_out_field_string (uiout
, "attr", attstr
);
535 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
537 struct ui_out
*uiout
= current_uiout
;
540 enum varobj_display_formats format
;
549 static const struct mi_opt opts
[] =
555 /* Parse arguments. */
556 format
= FORMAT_NATURAL
;
561 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
566 switch ((enum opt
) opt
)
570 error (_("Cannot specify format more than once"));
572 format
= mi_parse_format (oarg
);
579 error (_("Usage: [-f FORMAT] NAME"));
582 error (_("Garbage at end of command"));
584 /* Get varobj handle, if a valid var obj name was specified. */
585 var
= varobj_get_handle (argv
[oind
]);
589 char *val
= varobj_get_formatted_value (var
, format
);
591 ui_out_field_string (uiout
, "value", val
);
596 char *val
= varobj_get_value (var
);
598 ui_out_field_string (uiout
, "value", val
);
604 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
606 struct ui_out
*uiout
= current_uiout
;
608 char *expression
, *val
;
609 struct cleanup
*cleanup
;
612 error (_("-var-assign: Usage: NAME EXPRESSION."));
614 /* Get varobj handle, if a valid var obj name was specified. */
615 var
= varobj_get_handle (argv
[0]);
617 if (!varobj_editable_p (var
))
618 error (_("-var-assign: Variable object is not editable"));
620 expression
= xstrdup (argv
[1]);
622 /* MI command '-var-assign' may write memory, so suppress memory
623 changed notification if it does. */
625 = make_cleanup_restore_integer (&mi_suppress_notification
.memory
);
626 mi_suppress_notification
.memory
= 1;
628 if (!varobj_set_value (var
, expression
))
629 error (_("-var-assign: Could not assign "
630 "expression to variable object"));
632 val
= varobj_get_value (var
);
633 ui_out_field_string (uiout
, "value", val
);
636 do_cleanups (cleanup
);
639 /* Type used for parameters passing to mi_cmd_var_update_iter. */
641 struct mi_cmd_var_update
644 enum print_values print_values
;
647 /* Helper for mi_cmd_var_update - update each VAR. */
650 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
652 struct mi_cmd_var_update
*data
= data_pointer
;
653 int thread_id
, thread_stopped
;
655 thread_id
= varobj_get_thread_id (var
);
658 && (ptid_equal (inferior_ptid
, null_ptid
)
659 || is_stopped (inferior_ptid
)))
663 struct thread_info
*tp
= find_thread_id (thread_id
);
666 thread_stopped
= is_stopped (tp
->ptid
);
672 && (!data
->only_floating
|| varobj_floating_p (var
)))
673 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
677 mi_cmd_var_update (char *command
, char **argv
, int argc
)
679 struct ui_out
*uiout
= current_uiout
;
680 struct cleanup
*cleanup
;
682 enum print_values print_values
;
684 if (argc
!= 1 && argc
!= 2)
685 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
693 print_values
= mi_parse_print_values (argv
[0]);
695 print_values
= PRINT_NO_VALUES
;
697 if (mi_version (uiout
) <= 1)
698 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
700 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
702 /* Check if the parameter is a "*", which means that we want to
703 update all variables. */
705 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
707 struct mi_cmd_var_update data
;
709 data
.only_floating
= (*name
== '@');
710 data
.print_values
= print_values
;
712 /* varobj_update_one automatically updates all the children of
713 VAROBJ. Therefore update each VAROBJ only once by iterating
714 only the root VAROBJs. */
716 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
720 /* Get varobj handle, if a valid var obj name was specified. */
721 struct varobj
*var
= varobj_get_handle (name
);
723 varobj_update_one (var
, print_values
, 1 /* explicit */);
726 do_cleanups (cleanup
);
729 /* Helper for mi_cmd_var_update(). */
732 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
735 struct ui_out
*uiout
= current_uiout
;
736 VEC (varobj_update_result
) *changes
;
737 varobj_update_result
*r
;
740 changes
= varobj_update (&var
, is_explicit
);
742 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
746 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
748 if (mi_version (uiout
) > 1)
749 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
750 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
754 case VAROBJ_IN_SCOPE
:
755 if (mi_print_value_p (r
->varobj
, print_values
))
757 char *val
= varobj_get_value (r
->varobj
);
759 ui_out_field_string (uiout
, "value", val
);
762 ui_out_field_string (uiout
, "in_scope", "true");
764 case VAROBJ_NOT_IN_SCOPE
:
765 ui_out_field_string (uiout
, "in_scope", "false");
768 ui_out_field_string (uiout
, "in_scope", "invalid");
772 if (r
->status
!= VAROBJ_INVALID
)
775 ui_out_field_string (uiout
, "type_changed", "true");
777 ui_out_field_string (uiout
, "type_changed", "false");
782 char *type_name
= varobj_get_type (r
->varobj
);
784 ui_out_field_string (uiout
, "new_type", type_name
);
788 if (r
->type_changed
|| r
->children_changed
)
789 ui_out_field_int (uiout
, "new_num_children",
790 varobj_get_num_children (r
->varobj
));
792 display_hint
= varobj_get_display_hint (r
->varobj
);
795 ui_out_field_string (uiout
, "displayhint", display_hint
);
796 xfree (display_hint
);
799 if (varobj_is_dynamic_p (r
->varobj
))
800 ui_out_field_int (uiout
, "dynamic", 1);
802 varobj_get_child_range (r
->varobj
, &from
, &to
);
803 ui_out_field_int (uiout
, "has_more",
804 varobj_has_more (r
->varobj
, to
));
810 struct cleanup
*cleanup
;
812 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
813 for (j
= 0; VEC_iterate (varobj_p
, r
->newobj
, j
, child
); ++j
)
815 struct cleanup
*cleanup_child
;
818 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
819 print_varobj (child
, print_values
, 1 /* print_expression */);
820 do_cleanups (cleanup_child
);
823 do_cleanups (cleanup
);
824 VEC_free (varobj_p
, r
->newobj
);
825 r
->newobj
= NULL
; /* Paranoia. */
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"));
839 varobj_enable_pretty_printing ();
843 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
849 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
851 var
= varobj_get_handle (argv
[0]);
852 from
= atoi (argv
[1]);
855 varobj_set_child_range (var
, from
, to
);