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
, 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
;
613 error (_("-var-assign: Usage: NAME EXPRESSION."));
615 /* Get varobj handle, if a valid var obj name was specified. */
616 var
= varobj_get_handle (argv
[0]);
618 if (!varobj_editable_p (var
))
619 error (_("-var-assign: Variable object is not editable"));
621 expression
= xstrdup (argv
[1]);
623 /* MI command '-var-assign' may write memory, so suppress memory
624 changed notification if it does. */
625 scoped_restore save_suppress
626 = make_scoped_restore (&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
);
637 /* Type used for parameters passing to mi_cmd_var_update_iter. */
639 struct mi_cmd_var_update
642 enum print_values print_values
;
645 /* Helper for mi_cmd_var_update - update each VAR. */
648 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
650 struct mi_cmd_var_update
*data
= (struct mi_cmd_var_update
*) data_pointer
;
651 int thread_id
, thread_stopped
;
653 thread_id
= varobj_get_thread_id (var
);
656 && (ptid_equal (inferior_ptid
, null_ptid
)
657 || is_stopped (inferior_ptid
)))
661 struct thread_info
*tp
= find_thread_global_id (thread_id
);
664 thread_stopped
= is_stopped (tp
->ptid
);
670 && (!data
->only_floating
|| varobj_floating_p (var
)))
671 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
675 mi_cmd_var_update (char *command
, char **argv
, int argc
)
677 struct ui_out
*uiout
= current_uiout
;
678 struct cleanup
*cleanup
;
680 enum print_values print_values
;
682 if (argc
!= 1 && argc
!= 2)
683 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
691 print_values
= mi_parse_print_values (argv
[0]);
693 print_values
= PRINT_NO_VALUES
;
695 if (mi_version (uiout
) <= 1)
696 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
698 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
700 /* Check if the parameter is a "*", which means that we want to
701 update all variables. */
703 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
705 struct mi_cmd_var_update data
;
707 data
.only_floating
= (*name
== '@');
708 data
.print_values
= print_values
;
710 /* varobj_update_one automatically updates all the children of
711 VAROBJ. Therefore update each VAROBJ only once by iterating
712 only the root VAROBJs. */
714 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
718 /* Get varobj handle, if a valid var obj name was specified. */
719 struct varobj
*var
= varobj_get_handle (name
);
721 varobj_update_one (var
, print_values
, 1 /* explicit */);
724 do_cleanups (cleanup
);
727 /* Helper for mi_cmd_var_update(). */
730 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
733 struct ui_out
*uiout
= current_uiout
;
734 VEC (varobj_update_result
) *changes
;
735 varobj_update_result
*r
;
738 changes
= varobj_update (&var
, is_explicit
);
740 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
744 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
746 if (mi_version (uiout
) > 1)
747 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
748 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
752 case VAROBJ_IN_SCOPE
:
753 if (mi_print_value_p (r
->varobj
, print_values
))
755 char *val
= varobj_get_value (r
->varobj
);
757 ui_out_field_string (uiout
, "value", val
);
760 ui_out_field_string (uiout
, "in_scope", "true");
762 case VAROBJ_NOT_IN_SCOPE
:
763 ui_out_field_string (uiout
, "in_scope", "false");
766 ui_out_field_string (uiout
, "in_scope", "invalid");
770 if (r
->status
!= VAROBJ_INVALID
)
773 ui_out_field_string (uiout
, "type_changed", "true");
775 ui_out_field_string (uiout
, "type_changed", "false");
780 char *type_name
= varobj_get_type (r
->varobj
);
782 ui_out_field_string (uiout
, "new_type", type_name
);
786 if (r
->type_changed
|| r
->children_changed
)
787 ui_out_field_int (uiout
, "new_num_children",
788 varobj_get_num_children (r
->varobj
));
790 display_hint
= varobj_get_display_hint (r
->varobj
);
793 ui_out_field_string (uiout
, "displayhint", display_hint
);
794 xfree (display_hint
);
797 if (varobj_is_dynamic_p (r
->varobj
))
798 ui_out_field_int (uiout
, "dynamic", 1);
800 varobj_get_child_range (r
->varobj
, &from
, &to
);
801 ui_out_field_int (uiout
, "has_more",
802 varobj_has_more (r
->varobj
, to
));
808 struct cleanup
*cleanup
;
810 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
811 for (j
= 0; VEC_iterate (varobj_p
, r
->newobj
, j
, child
); ++j
)
813 struct cleanup
*cleanup_child
;
816 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
817 print_varobj (child
, print_values
, 1 /* print_expression */);
818 do_cleanups (cleanup_child
);
821 do_cleanups (cleanup
);
822 VEC_free (varobj_p
, r
->newobj
);
823 r
->newobj
= NULL
; /* Paranoia. */
826 do_cleanups (cleanup
);
828 VEC_free (varobj_update_result
, changes
);
832 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
835 error (_("-enable-pretty-printing: no arguments allowed"));
837 varobj_enable_pretty_printing ();
841 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
847 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
849 var
= varobj_get_handle (argv
[0]);
850 from
= atoi (argv
[1]);
853 varobj_set_child_range (var
, from
, to
);