1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2013 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/>. */
29 #include "gdb_string.h"
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
));
58 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
59 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
61 if (mi_print_value_p (var
, print_values
))
63 char *val
= varobj_get_value (var
);
65 ui_out_field_string (uiout
, "value", val
);
69 type
= varobj_get_type (var
);
72 ui_out_field_string (uiout
, "type", type
);
76 thread_id
= varobj_get_thread_id (var
);
78 ui_out_field_int (uiout
, "thread-id", thread_id
);
80 if (varobj_get_frozen (var
))
81 ui_out_field_int (uiout
, "frozen", 1);
83 display_hint
= varobj_get_display_hint (var
);
86 ui_out_field_string (uiout
, "displayhint", display_hint
);
90 if (varobj_pretty_printed_p (var
))
91 ui_out_field_int (uiout
, "dynamic", 1);
94 /* VAROBJ operations */
97 mi_cmd_var_create (char *command
, char **argv
, int argc
)
99 struct ui_out
*uiout
= current_uiout
;
100 CORE_ADDR frameaddr
= 0;
105 struct cleanup
*old_cleanups
;
106 enum varobj_type var_type
;
109 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
111 name
= xstrdup (argv
[0]);
112 /* Add cleanup for name. Must be free_current_contents as name can
114 old_cleanups
= make_cleanup (free_current_contents
, &name
);
116 frame
= xstrdup (argv
[1]);
117 make_cleanup (xfree
, frame
);
119 expr
= xstrdup (argv
[2]);
120 make_cleanup (xfree
, expr
);
122 if (strcmp (name
, "-") == 0)
125 name
= varobj_gen_name ();
127 else if (!isalpha (*name
))
128 error (_("-var-create: name of object must begin with a letter"));
130 if (strcmp (frame
, "*") == 0)
131 var_type
= USE_CURRENT_FRAME
;
132 else if (strcmp (frame
, "@") == 0)
133 var_type
= USE_SELECTED_FRAME
;
136 var_type
= USE_SPECIFIED_FRAME
;
137 frameaddr
= string_to_core_addr (frame
);
141 fprintf_unfiltered (gdb_stdlog
,
142 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
143 name
, frame
, hex_string (frameaddr
), expr
);
145 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
148 error (_("-var-create: unable to create variable object"));
150 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
152 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, 0));
154 do_cleanups (old_cleanups
);
158 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
163 int children_only_p
= 0;
164 struct cleanup
*old_cleanups
;
165 struct ui_out
*uiout
= current_uiout
;
167 if (argc
< 1 || argc
> 2)
168 error (_("-var-delete: Usage: [-c] EXPRESSION."));
170 name
= xstrdup (argv
[0]);
171 /* Add cleanup for name. Must be free_current_contents as name can
173 old_cleanups
= make_cleanup (free_current_contents
, &name
);
175 /* If we have one single argument it cannot be '-c' or any string
176 starting with '-'. */
179 if (strcmp (name
, "-c") == 0)
180 error (_("-var-delete: Missing required "
181 "argument after '-c': variable object name"));
183 error (_("-var-delete: Illegal variable object name"));
186 /* If we have 2 arguments they must be '-c' followed by a string
187 which would be the variable name. */
190 if (strcmp (name
, "-c") != 0)
191 error (_("-var-delete: Invalid option."));
193 do_cleanups (old_cleanups
);
194 name
= xstrdup (argv
[1]);
195 old_cleanups
= make_cleanup (free_current_contents
, &name
);
198 /* If we didn't error out, now NAME contains the name of the
201 var
= varobj_get_handle (name
);
203 numdel
= varobj_delete (var
, NULL
, children_only_p
);
205 ui_out_field_int (uiout
, "ndeleted", numdel
);
207 do_cleanups (old_cleanups
);
210 /* Parse a string argument into a format value. */
212 static enum varobj_display_formats
213 mi_parse_format (const char *arg
)
221 if (strncmp (arg
, "natural", len
) == 0)
222 return FORMAT_NATURAL
;
223 else if (strncmp (arg
, "binary", len
) == 0)
224 return FORMAT_BINARY
;
225 else if (strncmp (arg
, "decimal", len
) == 0)
226 return FORMAT_DECIMAL
;
227 else if (strncmp (arg
, "hexadecimal", len
) == 0)
228 return FORMAT_HEXADECIMAL
;
229 else if (strncmp (arg
, "octal", len
) == 0)
233 error (_("Must specify the format as: \"natural\", "
234 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
238 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
240 enum varobj_display_formats format
;
243 struct ui_out
*uiout
= current_uiout
;
246 error (_("-var-set-format: Usage: NAME FORMAT."));
248 /* Get varobj handle, if a valid var obj name was specified. */
249 var
= varobj_get_handle (argv
[0]);
251 format
= mi_parse_format (argv
[1]);
253 /* Set the format of VAR to the given format. */
254 varobj_set_display_format (var
, format
);
256 /* Report the new current format. */
257 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
259 /* Report the value in the new format. */
260 val
= varobj_get_value (var
);
261 ui_out_field_string (uiout
, "value", val
);
266 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
271 error (_("Usage: NAME VISUALIZER_FUNCTION."));
273 var
= varobj_get_handle (argv
[0]);
276 error (_("Variable object not found"));
278 varobj_set_visualizer (var
, argv
[1]);
282 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
288 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
290 var
= varobj_get_handle (argv
[0]);
292 if (strcmp (argv
[1], "0") == 0)
294 else if (strcmp (argv
[1], "1") == 0)
297 error (_("Invalid flag value"));
299 varobj_set_frozen (var
, frozen
);
301 /* We don't automatically return the new value, or what varobjs got
302 new values during unfreezing. If this information is required,
303 client should call -var-update explicitly. */
307 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
309 struct ui_out
*uiout
= current_uiout
;
310 enum varobj_display_formats format
;
314 error (_("-var-show-format: Usage: NAME."));
316 /* Get varobj handle, if a valid var obj name was specified. */
317 var
= varobj_get_handle (argv
[0]);
319 format
= varobj_get_display_format (var
);
321 /* Report the current format. */
322 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
326 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
328 struct ui_out
*uiout
= current_uiout
;
332 error (_("-var-info-num-children: Usage: NAME."));
334 /* Get varobj handle, if a valid var obj name was specified. */
335 var
= varobj_get_handle (argv
[0]);
337 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
340 /* Return 1 if given the argument PRINT_VALUES we should display
344 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
348 if (print_values
== PRINT_NO_VALUES
)
351 if (print_values
== PRINT_ALL_VALUES
)
354 if (varobj_pretty_printed_p (var
))
357 type
= varobj_get_gdb_type (var
);
362 type
= check_typedef (type
);
364 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
365 and that type is not a compound type. */
366 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
367 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
368 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
373 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
375 struct ui_out
*uiout
= current_uiout
;
377 VEC(varobj_p
) *children
;
378 struct varobj
*child
;
379 enum print_values print_values
;
384 if (argc
< 1 || argc
> 4)
385 error (_("-var-list-children: Usage: "
386 "[PRINT_VALUES] NAME [FROM TO]"));
388 /* Get varobj handle, if a valid var obj name was specified. */
389 if (argc
== 1 || argc
== 3)
390 var
= varobj_get_handle (argv
[0]);
392 var
= varobj_get_handle (argv
[1]);
396 from
= atoi (argv
[argc
- 2]);
397 to
= atoi (argv
[argc
- 1]);
405 children
= varobj_list_children (var
, &from
, &to
);
406 ui_out_field_int (uiout
, "numchild", to
- from
);
407 if (argc
== 2 || argc
== 4)
408 print_values
= mi_parse_print_values (argv
[0]);
410 print_values
= PRINT_NO_VALUES
;
412 display_hint
= varobj_get_display_hint (var
);
415 ui_out_field_string (uiout
, "displayhint", display_hint
);
416 xfree (display_hint
);
421 struct cleanup
*cleanup_children
;
423 if (mi_version (uiout
) == 1)
425 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
428 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
430 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
433 struct cleanup
*cleanup_child
;
435 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
436 print_varobj (child
, print_values
, 1 /* print expression */);
437 do_cleanups (cleanup_child
);
439 do_cleanups (cleanup_children
);
442 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
446 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
448 struct ui_out
*uiout
= current_uiout
;
452 error (_("-var-info-type: Usage: NAME."));
454 /* Get varobj handle, if a valid var obj name was specified. */
455 var
= varobj_get_handle (argv
[0]);
457 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
461 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
463 struct ui_out
*uiout
= current_uiout
;
468 error (_("Usage: NAME."));
470 /* Get varobj handle, if a valid var obj name was specified. */
471 var
= varobj_get_handle (argv
[0]);
473 path_expr
= varobj_get_path_expr (var
);
475 ui_out_field_string (uiout
, "path_expr", path_expr
);
479 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
481 struct ui_out
*uiout
= current_uiout
;
482 enum varobj_languages lang
;
486 error (_("-var-info-expression: Usage: NAME."));
488 /* Get varobj handle, if a valid var obj name was specified. */
489 var
= varobj_get_handle (argv
[0]);
491 lang
= varobj_get_language (var
);
493 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
494 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
498 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
500 struct ui_out
*uiout
= current_uiout
;
506 error (_("-var-show-attributes: Usage: NAME."));
508 /* Get varobj handle, if a valid var obj name was specified */
509 var
= varobj_get_handle (argv
[0]);
511 attr
= varobj_get_attributes (var
);
512 /* FIXME: define masks for attributes */
513 if (attr
& 0x00000001)
516 attstr
= "noneditable";
518 ui_out_field_string (uiout
, "attr", attstr
);
522 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
524 struct ui_out
*uiout
= current_uiout
;
527 enum varobj_display_formats format
;
536 static const struct mi_opt opts
[] =
542 /* Parse arguments. */
543 format
= FORMAT_NATURAL
;
548 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
553 switch ((enum opt
) opt
)
557 error (_("Cannot specify format more than once"));
559 format
= mi_parse_format (oarg
);
566 error (_("Usage: [-f FORMAT] NAME"));
569 error (_("Garbage at end of command"));
571 /* Get varobj handle, if a valid var obj name was specified. */
572 var
= varobj_get_handle (argv
[oind
]);
576 char *val
= varobj_get_formatted_value (var
, format
);
578 ui_out_field_string (uiout
, "value", val
);
583 char *val
= varobj_get_value (var
);
585 ui_out_field_string (uiout
, "value", val
);
591 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
593 struct ui_out
*uiout
= current_uiout
;
595 char *expression
, *val
;
596 struct cleanup
*cleanup
;
599 error (_("-var-assign: Usage: NAME EXPRESSION."));
601 /* Get varobj handle, if a valid var obj name was specified. */
602 var
= varobj_get_handle (argv
[0]);
604 if (!varobj_editable_p (var
))
605 error (_("-var-assign: Variable object is not editable"));
607 expression
= xstrdup (argv
[1]);
609 /* MI command '-var-assign' may write memory, so suppress memory
610 changed notification if it does. */
612 = make_cleanup_restore_integer (&mi_suppress_notification
.memory
);
613 mi_suppress_notification
.memory
= 1;
615 if (!varobj_set_value (var
, expression
))
616 error (_("-var-assign: Could not assign "
617 "expression to variable object"));
619 val
= varobj_get_value (var
);
620 ui_out_field_string (uiout
, "value", val
);
623 do_cleanups (cleanup
);
626 /* Type used for parameters passing to mi_cmd_var_update_iter. */
628 struct mi_cmd_var_update
631 enum print_values print_values
;
634 /* Helper for mi_cmd_var_update - update each VAR. */
637 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
639 struct mi_cmd_var_update
*data
= data_pointer
;
640 int thread_id
, thread_stopped
;
642 thread_id
= varobj_get_thread_id (var
);
644 if (thread_id
== -1 && is_stopped (inferior_ptid
))
648 struct thread_info
*tp
= find_thread_id (thread_id
);
651 thread_stopped
= is_stopped (tp
->ptid
);
657 && (!data
->only_floating
|| varobj_floating_p (var
)))
658 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
662 mi_cmd_var_update (char *command
, char **argv
, int argc
)
664 struct ui_out
*uiout
= current_uiout
;
665 struct cleanup
*cleanup
;
667 enum print_values print_values
;
669 if (argc
!= 1 && argc
!= 2)
670 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
678 print_values
= mi_parse_print_values (argv
[0]);
680 print_values
= PRINT_NO_VALUES
;
682 if (mi_version (uiout
) <= 1)
683 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
685 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
687 /* Check if the parameter is a "*", which means that we want to
688 update all variables. */
690 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
692 struct mi_cmd_var_update data
;
694 data
.only_floating
= (*name
== '@');
695 data
.print_values
= print_values
;
697 /* varobj_update_one automatically updates all the children of
698 VAROBJ. Therefore update each VAROBJ only once by iterating
699 only the root VAROBJs. */
701 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
705 /* Get varobj handle, if a valid var obj name was specified. */
706 struct varobj
*var
= varobj_get_handle (name
);
708 varobj_update_one (var
, print_values
, 1 /* explicit */);
711 do_cleanups (cleanup
);
714 /* Helper for mi_cmd_var_update(). */
717 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
720 struct ui_out
*uiout
= current_uiout
;
721 VEC (varobj_update_result
) *changes
;
722 varobj_update_result
*r
;
725 changes
= varobj_update (&var
, explicit);
727 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
731 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
733 if (mi_version (uiout
) > 1)
734 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
735 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
739 case VAROBJ_IN_SCOPE
:
740 if (mi_print_value_p (r
->varobj
, print_values
))
742 char *val
= varobj_get_value (r
->varobj
);
744 ui_out_field_string (uiout
, "value", val
);
747 ui_out_field_string (uiout
, "in_scope", "true");
749 case VAROBJ_NOT_IN_SCOPE
:
750 ui_out_field_string (uiout
, "in_scope", "false");
753 ui_out_field_string (uiout
, "in_scope", "invalid");
757 if (r
->status
!= VAROBJ_INVALID
)
760 ui_out_field_string (uiout
, "type_changed", "true");
762 ui_out_field_string (uiout
, "type_changed", "false");
766 ui_out_field_string (uiout
, "new_type", varobj_get_type (r
->varobj
));
768 if (r
->type_changed
|| r
->children_changed
)
769 ui_out_field_int (uiout
, "new_num_children",
770 varobj_get_num_children (r
->varobj
));
772 display_hint
= varobj_get_display_hint (r
->varobj
);
775 ui_out_field_string (uiout
, "displayhint", display_hint
);
776 xfree (display_hint
);
779 if (varobj_pretty_printed_p (r
->varobj
))
780 ui_out_field_int (uiout
, "dynamic", 1);
782 varobj_get_child_range (r
->varobj
, &from
, &to
);
783 ui_out_field_int (uiout
, "has_more",
784 varobj_has_more (r
->varobj
, to
));
790 struct cleanup
*cleanup
;
792 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
793 for (j
= 0; VEC_iterate (varobj_p
, r
->new, j
, child
); ++j
)
795 struct cleanup
*cleanup_child
;
798 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
799 print_varobj (child
, print_values
, 1 /* print_expression */);
800 do_cleanups (cleanup_child
);
803 do_cleanups (cleanup
);
804 VEC_free (varobj_p
, r
->new);
805 r
->new = NULL
; /* Paranoia. */
808 do_cleanups (cleanup
);
810 VEC_free (varobj_update_result
, changes
);
814 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
817 error (_("-enable-pretty-printing: no arguments allowed"));
819 varobj_enable_pretty_printing ();
823 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
829 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
831 var
= varobj_get_handle (argv
[0]);
832 from
= atoi (argv
[1]);
835 varobj_set_child_range (var
, from
, to
);