1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
31 #include "mi-getopt.h"
32 #include "gdbthread.h"
34 const char mi_no_values
[] = "--no-values";
35 const char mi_simple_values
[] = "--simple-values";
36 const char mi_all_values
[] = "--all-values";
38 extern int varobjdebug
; /* defined in varobj.c. */
40 static void varobj_update_one (struct varobj
*var
,
41 enum print_values print_values
,
44 static int mi_print_value_p (struct varobj
*var
,
45 enum print_values print_values
);
47 /* Print variable object VAR. The PRINT_VALUES parameter controls
48 if the value should be printed. The PRINT_EXPRESSION parameter
49 controls if the expression should be printed. */
51 print_varobj (struct varobj
*var
, enum print_values print_values
,
58 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
60 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
61 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
63 if (mi_print_value_p (var
, print_values
))
65 char *val
= varobj_get_value (var
);
67 ui_out_field_string (uiout
, "value", val
);
71 type
= varobj_get_type (var
);
74 ui_out_field_string (uiout
, "type", type
);
78 thread_id
= varobj_get_thread_id (var
);
80 ui_out_field_int (uiout
, "thread-id", thread_id
);
82 if (varobj_get_frozen (var
))
83 ui_out_field_int (uiout
, "frozen", 1);
85 display_hint
= varobj_get_display_hint (var
);
88 ui_out_field_string (uiout
, "displayhint", display_hint
);
92 if (varobj_pretty_printed_p (var
))
93 ui_out_field_int (uiout
, "dynamic", 1);
96 /* VAROBJ operations */
99 mi_cmd_var_create (char *command
, char **argv
, int argc
)
101 CORE_ADDR frameaddr
= 0;
106 struct cleanup
*old_cleanups
;
107 enum varobj_type var_type
;
111 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
112 ...."); return MI_CMD_ERROR; */
113 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
116 name
= xstrdup (argv
[0]);
117 /* Add cleanup for name. Must be free_current_contents as
118 name can be reallocated */
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 (_("mi_cmd_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 (_("mi_cmd_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
;
171 if (argc
< 1 || argc
> 2)
172 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
174 name
= xstrdup (argv
[0]);
175 /* Add cleanup for name. Must be free_current_contents as
176 name can be reallocated */
177 old_cleanups
= make_cleanup (free_current_contents
, &name
);
179 /* If we have one single argument it cannot be '-c' or any string
180 starting with '-'. */
183 if (strcmp (name
, "-c") == 0)
184 error (_("mi_cmd_var_delete: Missing required "
185 "argument after '-c': variable object name"));
187 error (_("mi_cmd_var_delete: Illegal variable object name"));
190 /* If we have 2 arguments they must be '-c' followed by a string
191 which would be the variable name. */
194 if (strcmp (name
, "-c") != 0)
195 error (_("mi_cmd_var_delete: Invalid option."));
197 do_cleanups (old_cleanups
);
198 name
= xstrdup (argv
[1]);
199 make_cleanup (free_current_contents
, &name
);
202 /* If we didn't error out, now NAME contains the name of the
205 var
= varobj_get_handle (name
);
207 numdel
= varobj_delete (var
, NULL
, children_only_p
);
209 ui_out_field_int (uiout
, "ndeleted", numdel
);
211 do_cleanups (old_cleanups
);
214 /* Parse a string argument into a format value. */
216 static enum varobj_display_formats
217 mi_parse_format (const char *arg
)
225 if (strncmp (arg
, "natural", len
) == 0)
226 return FORMAT_NATURAL
;
227 else if (strncmp (arg
, "binary", len
) == 0)
228 return FORMAT_BINARY
;
229 else if (strncmp (arg
, "decimal", len
) == 0)
230 return FORMAT_DECIMAL
;
231 else if (strncmp (arg
, "hexadecimal", len
) == 0)
232 return FORMAT_HEXADECIMAL
;
233 else if (strncmp (arg
, "octal", len
) == 0)
237 error (_("Must specify the format as: \"natural\", "
238 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
242 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
244 enum varobj_display_formats format
;
249 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
251 /* Get varobj handle, if a valid var obj name was specified */
252 var
= varobj_get_handle (argv
[0]);
254 format
= mi_parse_format (argv
[1]);
256 /* Set the format of VAR to given format */
257 varobj_set_display_format (var
, format
);
259 /* Report the new current format */
260 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
262 /* Report the value in the new format */
263 val
= varobj_get_value (var
);
264 ui_out_field_string (uiout
, "value", val
);
269 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
274 error (_("Usage: NAME VISUALIZER_FUNCTION."));
276 var
= varobj_get_handle (argv
[0]);
279 error (_("Variable object not found"));
281 varobj_set_visualizer (var
, argv
[1]);
285 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
291 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
293 var
= varobj_get_handle (argv
[0]);
295 if (strcmp (argv
[1], "0") == 0)
297 else if (strcmp (argv
[1], "1") == 0)
300 error (_("Invalid flag value"));
302 varobj_set_frozen (var
, frozen
);
304 /* We don't automatically return the new value, or what varobjs got new
305 values during unfreezing. If this information is required, client
306 should call -var-update explicitly. */
311 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
313 enum varobj_display_formats format
;
317 error (_("mi_cmd_var_show_format: Usage: NAME."));
319 /* Get varobj handle, if a valid var obj name was specified */
320 var
= varobj_get_handle (argv
[0]);
322 format
= varobj_get_display_format (var
);
324 /* Report the current format */
325 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
329 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
334 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
336 /* Get varobj handle, if a valid var obj name was specified */
337 var
= varobj_get_handle (argv
[0]);
339 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
342 /* Parse a string argument into a print_values value. */
344 static enum print_values
345 mi_parse_values_option (const char *arg
)
347 if (strcmp (arg
, "0") == 0
348 || strcmp (arg
, mi_no_values
) == 0)
349 return PRINT_NO_VALUES
;
350 else if (strcmp (arg
, "1") == 0
351 || strcmp (arg
, mi_all_values
) == 0)
352 return PRINT_ALL_VALUES
;
353 else if (strcmp (arg
, "2") == 0
354 || strcmp (arg
, mi_simple_values
) == 0)
355 return PRINT_SIMPLE_VALUES
;
357 error (_("Unknown value for PRINT_VALUES\n\
358 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
359 mi_no_values
, mi_simple_values
, mi_all_values
);
362 /* Return 1 if given the argument PRINT_VALUES we should display
366 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
370 if (print_values
== PRINT_NO_VALUES
)
373 if (print_values
== PRINT_ALL_VALUES
)
376 if (varobj_pretty_printed_p (var
))
379 type
= varobj_get_gdb_type (var
);
384 type
= check_typedef (type
);
386 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
387 and that type is not a compound type. */
388 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
389 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
390 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
395 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
398 VEC(varobj_p
) *children
;
399 struct varobj
*child
;
400 enum print_values print_values
;
405 if (argc
< 1 || argc
> 4)
406 error (_("mi_cmd_var_list_children: Usage: "
407 "[PRINT_VALUES] NAME [FROM TO]"));
409 /* Get varobj handle, if a valid var obj name was specified */
410 if (argc
== 1 || argc
== 3)
411 var
= varobj_get_handle (argv
[0]);
413 var
= varobj_get_handle (argv
[1]);
417 from
= atoi (argv
[argc
- 2]);
418 to
= atoi (argv
[argc
- 1]);
426 children
= varobj_list_children (var
, &from
, &to
);
427 ui_out_field_int (uiout
, "numchild", to
- from
);
428 if (argc
== 2 || argc
== 4)
429 print_values
= mi_parse_values_option (argv
[0]);
431 print_values
= PRINT_NO_VALUES
;
433 display_hint
= varobj_get_display_hint (var
);
436 ui_out_field_string (uiout
, "displayhint", display_hint
);
437 xfree (display_hint
);
442 struct cleanup
*cleanup_children
;
444 if (mi_version (uiout
) == 1)
446 = make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
449 = make_cleanup_ui_out_list_begin_end (uiout
, "children");
451 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
454 struct cleanup
*cleanup_child
;
456 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
457 print_varobj (child
, print_values
, 1 /* print expression */);
458 do_cleanups (cleanup_child
);
460 do_cleanups (cleanup_children
);
463 ui_out_field_int (uiout
, "has_more", varobj_has_more (var
, to
));
467 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
472 error (_("mi_cmd_var_info_type: Usage: NAME."));
474 /* Get varobj handle, if a valid var obj name was specified */
475 var
= varobj_get_handle (argv
[0]);
477 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
481 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
487 error (_("Usage: NAME."));
489 /* Get varobj handle, if a valid var obj name was specified. */
490 var
= varobj_get_handle (argv
[0]);
492 path_expr
= varobj_get_path_expr (var
);
494 ui_out_field_string (uiout
, "path_expr", path_expr
);
498 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
500 enum varobj_languages lang
;
504 error (_("mi_cmd_var_info_expression: Usage: NAME."));
506 /* Get varobj handle, if a valid var obj name was specified */
507 var
= varobj_get_handle (argv
[0]);
509 lang
= varobj_get_language (var
);
511 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
512 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
516 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
523 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
525 /* Get varobj handle, if a valid var obj name was specified */
526 var
= varobj_get_handle (argv
[0]);
528 attr
= varobj_get_attributes (var
);
529 /* FIXME: define masks for attributes */
530 if (attr
& 0x00000001)
533 attstr
= "noneditable";
535 ui_out_field_string (uiout
, "attr", attstr
);
539 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
543 enum varobj_display_formats format
;
552 static struct mi_opt opts
[] =
558 /* Parse arguments */
559 format
= FORMAT_NATURAL
;
564 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
565 opts
, &optind
, &optarg
);
569 switch ((enum opt
) opt
)
573 error (_("Cannot specify format more than once"));
575 format
= mi_parse_format (optarg
);
582 error (_("Usage: [-f FORMAT] NAME"));
584 if (optind
< argc
- 1)
585 error (_("Garbage at end of command"));
587 /* Get varobj handle, if a valid var obj name was specified */
588 var
= varobj_get_handle (argv
[optind
]);
592 char *val
= varobj_get_formatted_value (var
, format
);
594 ui_out_field_string (uiout
, "value", val
);
599 char *val
= varobj_get_value (var
);
601 ui_out_field_string (uiout
, "value", val
);
607 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
610 char *expression
, *val
;
613 error (_("mi_cmd_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 (_("mi_cmd_var_assign: Variable object is not editable"));
621 expression
= xstrdup (argv
[1]);
623 if (!varobj_set_value (var
, expression
))
624 error (_("mi_cmd_var_assign: Could not assign "
625 "expression to variable object"));
627 val
= varobj_get_value (var
);
628 ui_out_field_string (uiout
, "value", val
);
632 /* Type used for parameters passing to mi_cmd_var_update_iter. */
634 struct mi_cmd_var_update
637 enum print_values print_values
;
640 /* Helper for mi_cmd_var_update - update each VAR. */
643 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
645 struct mi_cmd_var_update
*data
= data_pointer
;
646 int thread_id
, thread_stopped
;
648 thread_id
= varobj_get_thread_id (var
);
650 if (thread_id
== -1 && is_stopped (inferior_ptid
))
654 struct thread_info
*tp
= find_thread_id (thread_id
);
657 thread_stopped
= is_stopped (tp
->ptid
);
663 if (!data
->only_floating
|| varobj_floating_p (var
))
664 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
668 mi_cmd_var_update (char *command
, char **argv
, int argc
)
670 struct cleanup
*cleanup
;
672 enum print_values print_values
;
674 if (argc
!= 1 && argc
!= 2)
675 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
683 print_values
= mi_parse_values_option (argv
[0]);
685 print_values
= PRINT_NO_VALUES
;
687 if (mi_version (uiout
) <= 1)
688 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
690 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
692 /* Check if the parameter is a "*" which means that we want
693 to update all variables */
695 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
697 struct mi_cmd_var_update data
;
699 data
.only_floating
= *name
== '@';
700 data
.print_values
= print_values
;
702 /* varobj_update_one automatically updates all the children of VAROBJ.
703 Therefore update each VAROBJ only once by iterating only the root
706 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
710 /* Get varobj handle, if a valid var obj name was specified */
711 struct varobj
*var
= varobj_get_handle (name
);
713 varobj_update_one (var
, print_values
, 1 /* explicit */);
716 do_cleanups (cleanup
);
719 /* Helper for mi_cmd_var_update(). */
722 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
725 struct cleanup
*cleanup
= NULL
;
726 VEC (varobj_update_result
) *changes
;
727 varobj_update_result
*r
;
730 changes
= varobj_update (&var
, explicit);
732 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
737 if (mi_version (uiout
) > 1)
738 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
739 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
743 case VAROBJ_IN_SCOPE
:
744 if (mi_print_value_p (r
->varobj
, print_values
))
746 char *val
= varobj_get_value (r
->varobj
);
748 ui_out_field_string (uiout
, "value", val
);
751 ui_out_field_string (uiout
, "in_scope", "true");
753 case VAROBJ_NOT_IN_SCOPE
:
754 ui_out_field_string (uiout
, "in_scope", "false");
757 ui_out_field_string (uiout
, "in_scope", "invalid");
761 if (r
->status
!= VAROBJ_INVALID
)
764 ui_out_field_string (uiout
, "type_changed", "true");
766 ui_out_field_string (uiout
, "type_changed", "false");
770 ui_out_field_string (uiout
, "new_type", varobj_get_type (r
->varobj
));
772 if (r
->type_changed
|| r
->children_changed
)
773 ui_out_field_int (uiout
, "new_num_children",
774 varobj_get_num_children (r
->varobj
));
776 display_hint
= varobj_get_display_hint (var
);
779 ui_out_field_string (uiout
, "displayhint", display_hint
);
780 xfree (display_hint
);
783 if (varobj_pretty_printed_p (var
))
784 ui_out_field_int (uiout
, "dynamic", 1);
786 varobj_get_child_range (r
->varobj
, &from
, &to
);
787 ui_out_field_int (uiout
, "has_more",
788 varobj_has_more (r
->varobj
, to
));
794 struct cleanup
*cleanup
;
796 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "new_children");
797 for (j
= 0; VEC_iterate (varobj_p
, r
->new, j
, child
); ++j
)
799 struct cleanup
*cleanup_child
;
802 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
803 print_varobj (child
, print_values
, 1 /* print_expression */);
804 do_cleanups (cleanup_child
);
807 do_cleanups (cleanup
);
808 VEC_free (varobj_p
, r
->new);
809 r
->new = NULL
; /* Paranoia. */
812 if (mi_version (uiout
) > 1)
813 do_cleanups (cleanup
);
815 VEC_free (varobj_update_result
, changes
);
819 mi_cmd_enable_pretty_printing (char *command
, char **argv
, int argc
)
822 error (_("mi_cmd_enable_pretty_printing: no arguments allowed"));
823 varobj_enable_pretty_printing ();
827 mi_cmd_var_set_update_range (char *command
, char **argv
, int argc
)
833 error (_("mi_cmd_var_set_update_range: Usage: VAROBJ FROM TO"));
835 var
= varobj_get_handle (argv
[0]);
836 from
= atoi (argv
[1]);
839 varobj_set_child_range (var
, from
, to
);