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
;
54 uiout
->field_string ("name", varobj_get_objname (var
));
57 std::string exp
= varobj_get_expression (var
);
59 uiout
->field_string ("exp", exp
.c_str ());
61 uiout
->field_int ("numchild", varobj_get_num_children (var
));
63 if (mi_print_value_p (var
, print_values
))
65 std::string val
= varobj_get_value (var
);
67 uiout
->field_string ("value", val
.c_str ());
70 std::string type
= varobj_get_type (var
);
72 uiout
->field_string ("type", type
.c_str ());
74 thread_id
= varobj_get_thread_id (var
);
76 uiout
->field_int ("thread-id", thread_id
);
78 if (varobj_get_frozen (var
))
79 uiout
->field_int ("frozen", 1);
81 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
83 uiout
->field_string ("displayhint", display_hint
.get ());
85 if (varobj_is_dynamic_p (var
))
86 uiout
->field_int ("dynamic", 1);
89 /* VAROBJ operations */
92 mi_cmd_var_create (char *command
, char **argv
, int argc
)
94 struct ui_out
*uiout
= current_uiout
;
95 CORE_ADDR frameaddr
= 0;
100 struct cleanup
*old_cleanups
;
101 enum varobj_type var_type
;
104 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
106 name
= xstrdup (argv
[0]);
107 /* Add cleanup for name. Must be free_current_contents as name can
109 old_cleanups
= make_cleanup (free_current_contents
, &name
);
111 frame
= xstrdup (argv
[1]);
112 make_cleanup (xfree
, frame
);
114 expr
= xstrdup (argv
[2]);
115 make_cleanup (xfree
, expr
);
117 if (strcmp (name
, "-") == 0)
120 name
= varobj_gen_name ();
122 else if (!isalpha (*name
))
123 error (_("-var-create: name of object must begin with a letter"));
125 if (strcmp (frame
, "*") == 0)
126 var_type
= USE_CURRENT_FRAME
;
127 else if (strcmp (frame
, "@") == 0)
128 var_type
= USE_SELECTED_FRAME
;
131 var_type
= USE_SPECIFIED_FRAME
;
132 frameaddr
= string_to_core_addr (frame
);
136 fprintf_unfiltered (gdb_stdlog
,
137 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
138 name
, frame
, hex_string (frameaddr
), expr
);
140 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
143 error (_("-var-create: unable to create variable object"));
145 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
147 uiout
->field_int ("has_more", varobj_has_more (var
, 0));
149 do_cleanups (old_cleanups
);
153 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
158 int children_only_p
= 0;
159 struct cleanup
*old_cleanups
;
160 struct ui_out
*uiout
= current_uiout
;
162 if (argc
< 1 || argc
> 2)
163 error (_("-var-delete: Usage: [-c] EXPRESSION."));
165 name
= xstrdup (argv
[0]);
166 /* Add cleanup for name. Must be free_current_contents as name can
168 old_cleanups
= make_cleanup (free_current_contents
, &name
);
170 /* If we have one single argument it cannot be '-c' or any string
171 starting with '-'. */
174 if (strcmp (name
, "-c") == 0)
175 error (_("-var-delete: Missing required "
176 "argument after '-c': variable object name"));
178 error (_("-var-delete: Illegal variable object name"));
181 /* If we have 2 arguments they must be '-c' followed by a string
182 which would be the variable name. */
185 if (strcmp (name
, "-c") != 0)
186 error (_("-var-delete: Invalid option."));
188 do_cleanups (old_cleanups
);
189 name
= xstrdup (argv
[1]);
190 old_cleanups
= make_cleanup (free_current_contents
, &name
);
193 /* If we didn't error out, now NAME contains the name of the
196 var
= varobj_get_handle (name
);
198 numdel
= varobj_delete (var
, children_only_p
);
200 uiout
->field_int ("ndeleted", numdel
);
202 do_cleanups (old_cleanups
);
205 /* Parse a string argument into a format value. */
207 static enum varobj_display_formats
208 mi_parse_format (const char *arg
)
216 if (strncmp (arg
, "natural", len
) == 0)
217 return FORMAT_NATURAL
;
218 else if (strncmp (arg
, "binary", len
) == 0)
219 return FORMAT_BINARY
;
220 else if (strncmp (arg
, "decimal", len
) == 0)
221 return FORMAT_DECIMAL
;
222 else if (strncmp (arg
, "hexadecimal", len
) == 0)
223 return FORMAT_HEXADECIMAL
;
224 else if (strncmp (arg
, "octal", len
) == 0)
226 else if (strncmp (arg
, "zero-hexadecimal", len
) == 0)
227 return FORMAT_ZHEXADECIMAL
;
230 error (_("Must specify the format as: \"natural\", "
231 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
235 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
237 enum varobj_display_formats format
;
239 struct ui_out
*uiout
= current_uiout
;
242 error (_("-var-set-format: Usage: NAME FORMAT."));
244 /* Get varobj handle, if a valid var obj name was specified. */
245 var
= varobj_get_handle (argv
[0]);
247 format
= mi_parse_format (argv
[1]);
249 /* Set the format of VAR to the given format. */
250 varobj_set_display_format (var
, format
);
252 /* Report the new current format. */
253 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
255 /* Report the value in the new format. */
256 std::string val
= varobj_get_value (var
);
257 uiout
->field_string ("value", val
.c_str ());
261 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
266 error (_("Usage: NAME VISUALIZER_FUNCTION."));
268 var
= varobj_get_handle (argv
[0]);
271 error (_("Variable object not found"));
273 varobj_set_visualizer (var
, argv
[1]);
277 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
283 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
285 var
= varobj_get_handle (argv
[0]);
287 if (strcmp (argv
[1], "0") == 0)
289 else if (strcmp (argv
[1], "1") == 0)
292 error (_("Invalid flag value"));
294 varobj_set_frozen (var
, frozen
);
296 /* We don't automatically return the new value, or what varobjs got
297 new values during unfreezing. If this information is required,
298 client should call -var-update explicitly. */
302 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
304 struct ui_out
*uiout
= current_uiout
;
305 enum varobj_display_formats format
;
309 error (_("-var-show-format: Usage: NAME."));
311 /* Get varobj handle, if a valid var obj name was specified. */
312 var
= varobj_get_handle (argv
[0]);
314 format
= varobj_get_display_format (var
);
316 /* Report the current format. */
317 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
321 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
323 struct ui_out
*uiout
= current_uiout
;
327 error (_("-var-info-num-children: Usage: NAME."));
329 /* Get varobj handle, if a valid var obj name was specified. */
330 var
= varobj_get_handle (argv
[0]);
332 uiout
->field_int ("numchild", varobj_get_num_children (var
));
335 /* Return 1 if given the argument PRINT_VALUES we should display
339 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
343 if (print_values
== PRINT_NO_VALUES
)
346 if (print_values
== PRINT_ALL_VALUES
)
349 if (varobj_is_dynamic_p (var
))
352 type
= varobj_get_gdb_type (var
);
357 type
= check_typedef (type
);
359 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
360 and that type is not a compound type. */
361 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
362 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
363 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
368 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
370 struct ui_out
*uiout
= current_uiout
;
372 VEC(varobj_p
) *children
;
373 struct varobj
*child
;
374 enum print_values print_values
;
378 if (argc
< 1 || argc
> 4)
379 error (_("-var-list-children: Usage: "
380 "[PRINT_VALUES] NAME [FROM TO]"));
382 /* Get varobj handle, if a valid var obj name was specified. */
383 if (argc
== 1 || argc
== 3)
384 var
= varobj_get_handle (argv
[0]);
386 var
= varobj_get_handle (argv
[1]);
390 from
= atoi (argv
[argc
- 2]);
391 to
= atoi (argv
[argc
- 1]);
399 children
= varobj_list_children (var
, &from
, &to
);
400 uiout
->field_int ("numchild", to
- from
);
401 if (argc
== 2 || argc
== 4)
402 print_values
= mi_parse_print_values (argv
[0]);
404 print_values
= PRINT_NO_VALUES
;
406 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
408 uiout
->field_string ("displayhint", display_hint
.get ());
412 struct cleanup
*cleanup_children
;
414 if (mi_version (uiout
) == 1)
416 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
419 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
421 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
424 struct cleanup
*cleanup_child
;
426 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
427 print_varobj (child
, print_values
, 1 /* print expression */);
428 do_cleanups (cleanup_child
);
430 do_cleanups (cleanup_children
);
433 uiout
->field_int ("has_more", varobj_has_more (var
, to
));
437 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
439 struct ui_out
*uiout
= current_uiout
;
443 error (_("-var-info-type: Usage: NAME."));
445 /* Get varobj handle, if a valid var obj name was specified. */
446 var
= varobj_get_handle (argv
[0]);
448 std::string type_name
= varobj_get_type (var
);
449 uiout
->field_string ("type", type_name
.c_str ());
453 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
455 struct ui_out
*uiout
= current_uiout
;
459 error (_("Usage: NAME."));
461 /* Get varobj handle, if a valid var obj name was specified. */
462 var
= varobj_get_handle (argv
[0]);
464 const char *path_expr
= varobj_get_path_expr (var
);
466 uiout
->field_string ("path_expr", path_expr
);
470 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
472 struct ui_out
*uiout
= current_uiout
;
473 const struct language_defn
*lang
;
477 error (_("-var-info-expression: Usage: NAME."));
479 /* Get varobj handle, if a valid var obj name was specified. */
480 var
= varobj_get_handle (argv
[0]);
482 lang
= varobj_get_language (var
);
484 uiout
->field_string ("lang", lang
->la_natural_name
);
486 std::string exp
= varobj_get_expression (var
);
487 uiout
->field_string ("exp", exp
.c_str ());
491 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
493 struct ui_out
*uiout
= current_uiout
;
499 error (_("-var-show-attributes: Usage: NAME."));
501 /* Get varobj handle, if a valid var obj name was specified */
502 var
= varobj_get_handle (argv
[0]);
504 attr
= varobj_get_attributes (var
);
505 /* FIXME: define masks for attributes */
506 if (attr
& 0x00000001)
509 attstr
= "noneditable";
511 uiout
->field_string ("attr", attstr
);
515 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
517 struct ui_out
*uiout
= current_uiout
;
520 enum varobj_display_formats format
;
529 static const struct mi_opt opts
[] =
535 /* Parse arguments. */
536 format
= FORMAT_NATURAL
;
541 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
546 switch ((enum opt
) opt
)
550 error (_("Cannot specify format more than once"));
552 format
= mi_parse_format (oarg
);
559 error (_("Usage: [-f FORMAT] NAME"));
562 error (_("Garbage at end of command"));
564 /* Get varobj handle, if a valid var obj name was specified. */
565 var
= varobj_get_handle (argv
[oind
]);
569 std::string val
= varobj_get_formatted_value (var
, format
);
571 uiout
->field_string ("value", val
.c_str ());
575 std::string val
= varobj_get_value (var
);
577 uiout
->field_string ("value", val
.c_str ());
582 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
584 struct ui_out
*uiout
= current_uiout
;
588 error (_("-var-assign: Usage: NAME EXPRESSION."));
590 /* Get varobj handle, if a valid var obj name was specified. */
591 var
= varobj_get_handle (argv
[0]);
593 if (!varobj_editable_p (var
))
594 error (_("-var-assign: Variable object is not editable"));
596 const char *expression
= argv
[1];
598 /* MI command '-var-assign' may write memory, so suppress memory
599 changed notification if it does. */
600 scoped_restore save_suppress
601 = make_scoped_restore (&mi_suppress_notification
.memory
, 1);
603 if (!varobj_set_value (var
, expression
))
604 error (_("-var-assign: Could not assign "
605 "expression to variable object"));
607 std::string val
= varobj_get_value (var
);
608 uiout
->field_string ("value", val
.c_str ());
611 /* Type used for parameters passing to mi_cmd_var_update_iter. */
613 struct mi_cmd_var_update
616 enum print_values print_values
;
619 /* Helper for mi_cmd_var_update - update each VAR. */
622 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
624 struct mi_cmd_var_update
*data
= (struct mi_cmd_var_update
*) data_pointer
;
625 int thread_id
, thread_stopped
;
627 thread_id
= varobj_get_thread_id (var
);
630 && (ptid_equal (inferior_ptid
, null_ptid
)
631 || is_stopped (inferior_ptid
)))
635 struct thread_info
*tp
= find_thread_global_id (thread_id
);
638 thread_stopped
= is_stopped (tp
->ptid
);
644 && (!data
->only_floating
|| varobj_floating_p (var
)))
645 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
649 mi_cmd_var_update (char *command
, char **argv
, int argc
)
651 struct ui_out
*uiout
= current_uiout
;
652 struct cleanup
*cleanup
;
654 enum print_values print_values
;
656 if (argc
!= 1 && argc
!= 2)
657 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
665 print_values
= mi_parse_print_values (argv
[0]);
667 print_values
= PRINT_NO_VALUES
;
669 if (mi_version (uiout
) <= 1)
670 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
672 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
674 /* Check if the parameter is a "*", which means that we want to
675 update all variables. */
677 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
679 struct mi_cmd_var_update data
;
681 data
.only_floating
= (*name
== '@');
682 data
.print_values
= print_values
;
684 /* varobj_update_one automatically updates all the children of
685 VAROBJ. Therefore update each VAROBJ only once by iterating
686 only the root VAROBJs. */
688 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
692 /* Get varobj handle, if a valid var obj name was specified. */
693 struct varobj
*var
= varobj_get_handle (name
);
695 varobj_update_one (var
, print_values
, 1 /* explicit */);
698 do_cleanups (cleanup
);
701 /* Helper for mi_cmd_var_update(). */
704 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
707 struct ui_out
*uiout
= current_uiout
;
708 VEC (varobj_update_result
) *changes
;
709 varobj_update_result
*r
;
712 changes
= varobj_update (&var
, is_explicit
);
714 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
717 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
719 if (mi_version (uiout
) > 1)
720 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
721 uiout
->field_string ("name", varobj_get_objname (r
->varobj
));
725 case VAROBJ_IN_SCOPE
:
726 if (mi_print_value_p (r
->varobj
, print_values
))
728 std::string val
= varobj_get_value (r
->varobj
);
730 uiout
->field_string ("value", val
.c_str ());
732 uiout
->field_string ("in_scope", "true");
734 case VAROBJ_NOT_IN_SCOPE
:
735 uiout
->field_string ("in_scope", "false");
738 uiout
->field_string ("in_scope", "invalid");
742 if (r
->status
!= VAROBJ_INVALID
)
745 uiout
->field_string ("type_changed", "true");
747 uiout
->field_string ("type_changed", "false");
752 std::string type_name
= varobj_get_type (r
->varobj
);
754 uiout
->field_string ("new_type", type_name
.c_str ());
757 if (r
->type_changed
|| r
->children_changed
)
758 uiout
->field_int ("new_num_children",
759 varobj_get_num_children (r
->varobj
));
761 gdb::unique_xmalloc_ptr
<char> display_hint
762 = varobj_get_display_hint (r
->varobj
);
764 uiout
->field_string ("displayhint", display_hint
.get ());
766 if (varobj_is_dynamic_p (r
->varobj
))
767 uiout
->field_int ("dynamic", 1);
769 varobj_get_child_range (r
->varobj
, &from
, &to
);
770 uiout
->field_int ("has_more", varobj_has_more (r
->varobj
, to
));
776 struct cleanup
*cleanup
;
778 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
779 for (j
= 0; VEC_iterate (varobj_p
, r
->newobj
, j
, child
); ++j
)
781 struct cleanup
*cleanup_child
;
784 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
785 print_varobj (child
, print_values
, 1 /* print_expression */);
786 do_cleanups (cleanup_child
);
789 do_cleanups (cleanup
);
790 VEC_free (varobj_p
, r
->newobj
);
791 r
->newobj
= NULL
; /* Paranoia. */
794 do_cleanups (cleanup
);
796 VEC_free (varobj_update_result
, changes
);
800 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
803 error (_("-enable-pretty-printing: no arguments allowed"));
805 varobj_enable_pretty_printing ();
809 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
815 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
817 var
= varobj_get_handle (argv
[0]);
818 from
= atoi (argv
[1]);
821 varobj_set_child_range (var
, from
, to
);