1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008, 2009
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 type
*type
, enum print_values print_values
);
46 /* Print variable object VAR. The PRINT_VALUES parameter controls
47 if the value should be printed. The PRINT_EXPRESSION parameter
48 controls if the expression should be printed. */
50 print_varobj (struct varobj
*var
, enum print_values print_values
,
53 struct type
*gdb_type
;
57 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
59 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
60 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
62 if (mi_print_value_p (varobj_get_gdb_type (var
), print_values
))
63 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
65 type
= varobj_get_type (var
);
68 ui_out_field_string (uiout
, "type", type
);
72 thread_id
= varobj_get_thread_id (var
);
74 ui_out_field_int (uiout
, "thread-id", thread_id
);
76 if (varobj_get_frozen (var
))
77 ui_out_field_int (uiout
, "frozen", 1);
80 /* VAROBJ operations */
83 mi_cmd_var_create (char *command
, char **argv
, int argc
)
85 CORE_ADDR frameaddr
= 0;
90 struct cleanup
*old_cleanups
;
91 enum varobj_type var_type
;
95 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
96 ...."); return MI_CMD_ERROR; */
97 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
100 name
= xstrdup (argv
[0]);
101 /* Add cleanup for name. Must be free_current_contents as
102 name can be reallocated */
103 old_cleanups
= make_cleanup (free_current_contents
, &name
);
105 frame
= xstrdup (argv
[1]);
106 make_cleanup (xfree
, frame
);
108 expr
= xstrdup (argv
[2]);
109 make_cleanup (xfree
, expr
);
111 if (strcmp (name
, "-") == 0)
114 name
= varobj_gen_name ();
116 else if (!isalpha (*name
))
117 error (_("mi_cmd_var_create: name of object must begin with a letter"));
119 if (strcmp (frame
, "*") == 0)
120 var_type
= USE_CURRENT_FRAME
;
121 else if (strcmp (frame
, "@") == 0)
122 var_type
= USE_SELECTED_FRAME
;
125 var_type
= USE_SPECIFIED_FRAME
;
126 frameaddr
= string_to_core_addr (frame
);
130 fprintf_unfiltered (gdb_stdlog
,
131 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
132 name
, frame
, hex_string (frameaddr
), expr
);
134 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
137 error (_("mi_cmd_var_create: unable to create variable object"));
139 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
141 do_cleanups (old_cleanups
);
145 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
150 int children_only_p
= 0;
151 struct cleanup
*old_cleanups
;
153 if (argc
< 1 || argc
> 2)
154 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
156 name
= xstrdup (argv
[0]);
157 /* Add cleanup for name. Must be free_current_contents as
158 name can be reallocated */
159 old_cleanups
= make_cleanup (free_current_contents
, &name
);
161 /* If we have one single argument it cannot be '-c' or any string
162 starting with '-'. */
165 if (strcmp (name
, "-c") == 0)
166 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
168 error (_("mi_cmd_var_delete: Illegal variable object name"));
171 /* If we have 2 arguments they must be '-c' followed by a string
172 which would be the variable name. */
175 if (strcmp (name
, "-c") != 0)
176 error (_("mi_cmd_var_delete: Invalid option."));
178 do_cleanups (old_cleanups
);
179 name
= xstrdup (argv
[1]);
180 make_cleanup (free_current_contents
, &name
);
183 /* If we didn't error out, now NAME contains the name of the
186 var
= varobj_get_handle (name
);
188 numdel
= varobj_delete (var
, NULL
, children_only_p
);
190 ui_out_field_int (uiout
, "ndeleted", numdel
);
192 do_cleanups (old_cleanups
);
195 /* Parse a string argument into a format value. */
197 static enum varobj_display_formats
198 mi_parse_format (const char *arg
)
206 if (strncmp (arg
, "natural", len
) == 0)
207 return FORMAT_NATURAL
;
208 else if (strncmp (arg
, "binary", len
) == 0)
209 return FORMAT_BINARY
;
210 else if (strncmp (arg
, "decimal", len
) == 0)
211 return FORMAT_DECIMAL
;
212 else if (strncmp (arg
, "hexadecimal", len
) == 0)
213 return FORMAT_HEXADECIMAL
;
214 else if (strncmp (arg
, "octal", len
) == 0)
218 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
222 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
224 enum varobj_display_formats format
;
228 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
230 /* Get varobj handle, if a valid var obj name was specified */
231 var
= varobj_get_handle (argv
[0]);
233 format
= mi_parse_format (argv
[1]);
235 /* Set the format of VAR to given format */
236 varobj_set_display_format (var
, format
);
238 /* Report the new current format */
239 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
241 /* Report the value in the new format */
242 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
246 mi_cmd_var_set_visualizer (char *command
, char **argv
, int argc
)
251 error ("Usage: NAME VISUALIZER_FUNCTION.");
253 var
= varobj_get_handle (argv
[0]);
256 error ("Variable object not found");
258 varobj_set_visualizer (var
, argv
[1]);
262 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
268 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
270 var
= varobj_get_handle (argv
[0]);
272 if (strcmp (argv
[1], "0") == 0)
274 else if (strcmp (argv
[1], "1") == 0)
277 error (_("Invalid flag value"));
279 varobj_set_frozen (var
, frozen
);
281 /* We don't automatically return the new value, or what varobjs got new
282 values during unfreezing. If this information is required, client
283 should call -var-update explicitly. */
288 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
290 enum varobj_display_formats format
;
294 error (_("mi_cmd_var_show_format: Usage: NAME."));
296 /* Get varobj handle, if a valid var obj name was specified */
297 var
= varobj_get_handle (argv
[0]);
299 format
= varobj_get_display_format (var
);
301 /* Report the current format */
302 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
306 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
311 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
313 /* Get varobj handle, if a valid var obj name was specified */
314 var
= varobj_get_handle (argv
[0]);
316 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
319 /* Parse a string argument into a print_values value. */
321 static enum print_values
322 mi_parse_values_option (const char *arg
)
324 if (strcmp (arg
, "0") == 0
325 || strcmp (arg
, mi_no_values
) == 0)
326 return PRINT_NO_VALUES
;
327 else if (strcmp (arg
, "1") == 0
328 || strcmp (arg
, mi_all_values
) == 0)
329 return PRINT_ALL_VALUES
;
330 else if (strcmp (arg
, "2") == 0
331 || strcmp (arg
, mi_simple_values
) == 0)
332 return PRINT_SIMPLE_VALUES
;
334 error (_("Unknown value for PRINT_VALUES\n\
335 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
336 mi_no_values
, mi_simple_values
, mi_all_values
);
339 /* Return 1 if given the argument PRINT_VALUES we should display
340 a value of type TYPE. */
343 mi_print_value_p (struct type
*type
, enum print_values print_values
)
346 if (print_values
== PRINT_NO_VALUES
)
349 if (print_values
== PRINT_ALL_VALUES
)
356 type
= check_typedef (type
);
358 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
359 and that type is not a compound type. */
360 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
361 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
362 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
367 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
370 VEC(varobj_p
) *children
;
371 struct varobj
*child
;
372 struct cleanup
*cleanup_children
;
374 enum print_values print_values
;
378 if (argc
!= 1 && argc
!= 2)
379 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
381 /* Get varobj handle, if a valid var obj name was specified */
383 var
= varobj_get_handle (argv
[0]);
385 var
= varobj_get_handle (argv
[1]);
387 children
= varobj_list_children (var
);
388 ui_out_field_int (uiout
, "numchild", VEC_length (varobj_p
, children
));
390 print_values
= mi_parse_values_option (argv
[0]);
392 print_values
= PRINT_NO_VALUES
;
394 display_hint
= varobj_get_display_hint (var
);
397 ui_out_field_string (uiout
, "displayhint", display_hint
);
398 xfree (display_hint
);
401 if (VEC_length (varobj_p
, children
) == 0)
404 if (mi_version (uiout
) == 1)
405 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
407 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
408 for (ix
= 0; VEC_iterate (varobj_p
, children
, ix
, child
); ++ix
)
410 struct cleanup
*cleanup_child
;
411 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
412 print_varobj (child
, print_values
, 1 /* print expression */);
413 do_cleanups (cleanup_child
);
415 do_cleanups (cleanup_children
);
419 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
424 error (_("mi_cmd_var_info_type: Usage: NAME."));
426 /* Get varobj handle, if a valid var obj name was specified */
427 var
= varobj_get_handle (argv
[0]);
429 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
433 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
439 error (_("Usage: NAME."));
441 /* Get varobj handle, if a valid var obj name was specified. */
442 var
= varobj_get_handle (argv
[0]);
444 path_expr
= varobj_get_path_expr (var
);
446 ui_out_field_string (uiout
, "path_expr", path_expr
);
450 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
452 enum varobj_languages lang
;
456 error (_("mi_cmd_var_info_expression: Usage: NAME."));
458 /* Get varobj handle, if a valid var obj name was specified */
459 var
= varobj_get_handle (argv
[0]);
461 lang
= varobj_get_language (var
);
463 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
464 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
468 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
475 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
477 /* Get varobj handle, if a valid var obj name was specified */
478 var
= varobj_get_handle (argv
[0]);
480 attr
= varobj_get_attributes (var
);
481 /* FIXME: define masks for attributes */
482 if (attr
& 0x00000001)
485 attstr
= "noneditable";
487 ui_out_field_string (uiout
, "attr", attstr
);
491 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
495 enum varobj_display_formats format
;
504 static struct mi_opt opts
[] =
510 /* Parse arguments */
511 format
= FORMAT_NATURAL
;
516 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
, opts
, &optind
, &optarg
);
519 switch ((enum opt
) opt
)
523 error (_("Cannot specify format more than once"));
525 format
= mi_parse_format (optarg
);
532 error (_("Usage: [-f FORMAT] NAME"));
534 if (optind
< argc
- 1)
535 error (_("Garbage at end of command"));
537 /* Get varobj handle, if a valid var obj name was specified */
538 var
= varobj_get_handle (argv
[optind
]);
541 ui_out_field_string (uiout
, "value", varobj_get_formatted_value (var
, format
));
543 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
547 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
553 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
555 /* Get varobj handle, if a valid var obj name was specified */
556 var
= varobj_get_handle (argv
[0]);
558 if (!varobj_editable_p (var
))
559 error (_("mi_cmd_var_assign: Variable object is not editable"));
561 expression
= xstrdup (argv
[1]);
563 if (!varobj_set_value (var
, expression
))
564 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
566 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
569 /* Type used for parameters passing to mi_cmd_var_update_iter. */
571 struct mi_cmd_var_update
574 enum print_values print_values
;
577 /* Helper for mi_cmd_var_update - update each VAR. */
580 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
582 struct mi_cmd_var_update
*data
= data_pointer
;
583 int thread_id
, thread_stopped
;
585 thread_id
= varobj_get_thread_id (var
);
587 if (thread_id
== -1 && is_stopped (inferior_ptid
))
591 struct thread_info
*tp
= find_thread_id (thread_id
);
594 thread_stopped
= is_stopped (tp
->ptid
);
600 if (!data
->only_floating
|| varobj_floating_p (var
))
601 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
605 mi_cmd_var_update (char *command
, char **argv
, int argc
)
607 struct cleanup
*cleanup
;
609 enum print_values print_values
;
611 if (argc
!= 1 && argc
!= 2)
612 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
620 print_values
= mi_parse_values_option (argv
[0]);
622 print_values
= PRINT_NO_VALUES
;
624 if (mi_version (uiout
) <= 1)
625 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
627 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
629 /* Check if the parameter is a "*" which means that we want
630 to update all variables */
632 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
634 struct mi_cmd_var_update data
;
636 data
.only_floating
= *name
== '@';
637 data
.print_values
= print_values
;
639 /* varobj_update_one automatically updates all the children of VAROBJ.
640 Therefore update each VAROBJ only once by iterating only the root
643 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
647 /* Get varobj handle, if a valid var obj name was specified */
648 struct varobj
*var
= varobj_get_handle (name
);
650 varobj_update_one (var
, print_values
, 1 /* explicit */);
653 do_cleanups (cleanup
);
656 /* Helper for mi_cmd_var_update(). */
659 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
663 struct cleanup
*cleanup
= NULL
;
664 VEC (varobj_update_result
) *changes
;
665 varobj_update_result
*r
;
668 changes
= varobj_update (&var
, explicit);
670 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
674 if (mi_version (uiout
) > 1)
675 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
676 ui_out_field_string (uiout
, "name", varobj_get_objname (r
->varobj
));
680 case VAROBJ_IN_SCOPE
:
681 if (mi_print_value_p (varobj_get_gdb_type (r
->varobj
), print_values
))
682 ui_out_field_string (uiout
, "value", varobj_get_value (r
->varobj
));
683 ui_out_field_string (uiout
, "in_scope", "true");
685 case VAROBJ_NOT_IN_SCOPE
:
686 ui_out_field_string (uiout
, "in_scope", "false");
689 ui_out_field_string (uiout
, "in_scope", "invalid");
693 if (r
->status
!= VAROBJ_INVALID
)
696 ui_out_field_string (uiout
, "type_changed", "true");
698 ui_out_field_string (uiout
, "type_changed", "false");
703 ui_out_field_string (uiout
, "new_type", varobj_get_type (r
->varobj
));
704 ui_out_field_int (uiout
, "new_num_children",
705 varobj_get_num_children (r
->varobj
));
708 display_hint
= varobj_get_display_hint (var
);
711 ui_out_field_string (uiout
, "displayhint", display_hint
);
712 xfree (display_hint
);
715 if (r
->children_changed
)
718 struct varobj
*child
;
719 struct cleanup
*cleanup
=
720 make_cleanup_ui_out_list_begin_end (uiout
, "children");
722 VEC (varobj_p
)* children
= varobj_list_children (r
->varobj
);
724 for (ix
= 0; VEC_iterate (varobj_p
, children
, ix
, child
); ++ix
)
726 struct cleanup
*cleanup_child
;
727 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
728 print_varobj (child
, print_values
, 1 /* print expression */);
729 do_cleanups (cleanup_child
);
732 do_cleanups (cleanup
);
735 if (mi_version (uiout
) > 1)
736 do_cleanups (cleanup
);
738 VEC_free (varobj_update_result
, changes
);