1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2017 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"
33 #include "common/gdb_optional.h"
35 extern unsigned int varobjdebug
; /* defined in varobj.c. */
37 static void varobj_update_one (struct varobj
*var
,
38 enum print_values print_values
,
41 static int mi_print_value_p (struct varobj
*var
,
42 enum print_values print_values
);
44 /* Print variable object VAR. The PRINT_VALUES parameter controls
45 if the value should be printed. The PRINT_EXPRESSION parameter
46 controls if the expression should be printed. */
49 print_varobj (struct varobj
*var
, enum print_values print_values
,
52 struct ui_out
*uiout
= current_uiout
;
55 uiout
->field_string ("name", varobj_get_objname (var
));
58 std::string exp
= varobj_get_expression (var
);
60 uiout
->field_string ("exp", exp
.c_str ());
62 uiout
->field_int ("numchild", varobj_get_num_children (var
));
64 if (mi_print_value_p (var
, print_values
))
66 std::string val
= varobj_get_value (var
);
68 uiout
->field_string ("value", val
.c_str ());
71 std::string type
= varobj_get_type (var
);
73 uiout
->field_string ("type", type
.c_str ());
75 thread_id
= varobj_get_thread_id (var
);
77 uiout
->field_int ("thread-id", thread_id
);
79 if (varobj_get_frozen (var
))
80 uiout
->field_int ("frozen", 1);
82 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
84 uiout
->field_string ("displayhint", display_hint
.get ());
86 if (varobj_is_dynamic_p (var
))
87 uiout
->field_int ("dynamic", 1);
90 /* VAROBJ operations */
93 mi_cmd_var_create (const char *command
, char **argv
, int argc
)
95 struct ui_out
*uiout
= current_uiout
;
96 CORE_ADDR frameaddr
= 0;
100 enum varobj_type var_type
;
103 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
108 const char *name
= argv
[0];
109 std::string gen_name
;
110 if (strcmp (name
, "-") == 0)
112 gen_name
= varobj_gen_name ();
113 name
= gen_name
.c_str ();
115 else if (!isalpha (name
[0]))
116 error (_("-var-create: name of object must begin with a letter"));
118 if (strcmp (frame
, "*") == 0)
119 var_type
= USE_CURRENT_FRAME
;
120 else if (strcmp (frame
, "@") == 0)
121 var_type
= USE_SELECTED_FRAME
;
124 var_type
= USE_SPECIFIED_FRAME
;
125 frameaddr
= string_to_core_addr (frame
);
129 fprintf_unfiltered (gdb_stdlog
,
130 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
131 name
, frame
, hex_string (frameaddr
), expr
);
133 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
136 error (_("-var-create: unable to create variable object"));
138 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
140 uiout
->field_int ("has_more", varobj_has_more (var
, 0));
144 mi_cmd_var_delete (const char *command
, char **argv
, int argc
)
149 int children_only_p
= 0;
150 struct ui_out
*uiout
= current_uiout
;
152 if (argc
< 1 || argc
> 2)
153 error (_("-var-delete: Usage: [-c] EXPRESSION."));
157 /* If we have one single argument it cannot be '-c' or any string
158 starting with '-'. */
161 if (strcmp (name
, "-c") == 0)
162 error (_("-var-delete: Missing required "
163 "argument after '-c': variable object name"));
165 error (_("-var-delete: Illegal variable object name"));
168 /* If we have 2 arguments they must be '-c' followed by a string
169 which would be the variable name. */
172 if (strcmp (name
, "-c") != 0)
173 error (_("-var-delete: Invalid option."));
178 /* If we didn't error out, now NAME contains the name of the
181 var
= varobj_get_handle (name
);
183 numdel
= varobj_delete (var
, children_only_p
);
185 uiout
->field_int ("ndeleted", numdel
);
188 /* Parse a string argument into a format value. */
190 static enum varobj_display_formats
191 mi_parse_format (const char *arg
)
199 if (strncmp (arg
, "natural", len
) == 0)
200 return FORMAT_NATURAL
;
201 else if (strncmp (arg
, "binary", len
) == 0)
202 return FORMAT_BINARY
;
203 else if (strncmp (arg
, "decimal", len
) == 0)
204 return FORMAT_DECIMAL
;
205 else if (strncmp (arg
, "hexadecimal", len
) == 0)
206 return FORMAT_HEXADECIMAL
;
207 else if (strncmp (arg
, "octal", len
) == 0)
209 else if (strncmp (arg
, "zero-hexadecimal", len
) == 0)
210 return FORMAT_ZHEXADECIMAL
;
213 error (_("Must specify the format as: \"natural\", "
214 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
218 mi_cmd_var_set_format (const char *command
, char **argv
, int argc
)
220 enum varobj_display_formats format
;
222 struct ui_out
*uiout
= current_uiout
;
225 error (_("-var-set-format: Usage: NAME FORMAT."));
227 /* Get varobj handle, if a valid var obj name was specified. */
228 var
= varobj_get_handle (argv
[0]);
230 format
= mi_parse_format (argv
[1]);
232 /* Set the format of VAR to the given format. */
233 varobj_set_display_format (var
, format
);
235 /* Report the new current format. */
236 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
238 /* Report the value in the new format. */
239 std::string val
= varobj_get_value (var
);
240 uiout
->field_string ("value", val
.c_str ());
244 mi_cmd_var_set_visualizer (const char *command
, char **argv
, int argc
)
249 error (_("Usage: NAME VISUALIZER_FUNCTION."));
251 var
= varobj_get_handle (argv
[0]);
254 error (_("Variable object not found"));
256 varobj_set_visualizer (var
, argv
[1]);
260 mi_cmd_var_set_frozen (const char *command
, char **argv
, int argc
)
266 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
268 var
= varobj_get_handle (argv
[0]);
270 if (strcmp (argv
[1], "0") == 0)
272 else if (strcmp (argv
[1], "1") == 0)
275 error (_("Invalid flag value"));
277 varobj_set_frozen (var
, frozen
);
279 /* We don't automatically return the new value, or what varobjs got
280 new values during unfreezing. If this information is required,
281 client should call -var-update explicitly. */
285 mi_cmd_var_show_format (const char *command
, char **argv
, int argc
)
287 struct ui_out
*uiout
= current_uiout
;
288 enum varobj_display_formats format
;
292 error (_("-var-show-format: Usage: NAME."));
294 /* Get varobj handle, if a valid var obj name was specified. */
295 var
= varobj_get_handle (argv
[0]);
297 format
= varobj_get_display_format (var
);
299 /* Report the current format. */
300 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
304 mi_cmd_var_info_num_children (const char *command
, char **argv
, int argc
)
306 struct ui_out
*uiout
= current_uiout
;
310 error (_("-var-info-num-children: Usage: NAME."));
312 /* Get varobj handle, if a valid var obj name was specified. */
313 var
= varobj_get_handle (argv
[0]);
315 uiout
->field_int ("numchild", varobj_get_num_children (var
));
318 /* Return 1 if given the argument PRINT_VALUES we should display
322 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
326 if (print_values
== PRINT_NO_VALUES
)
329 if (print_values
== PRINT_ALL_VALUES
)
332 if (varobj_is_dynamic_p (var
))
335 type
= varobj_get_gdb_type (var
);
340 type
= check_typedef (type
);
342 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
343 and that type is not a compound type. */
344 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
345 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
346 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
351 mi_cmd_var_list_children (const char *command
, char **argv
, int argc
)
353 struct ui_out
*uiout
= current_uiout
;
355 VEC(varobj_p
) *children
;
356 struct varobj
*child
;
357 enum print_values print_values
;
361 if (argc
< 1 || argc
> 4)
362 error (_("-var-list-children: Usage: "
363 "[PRINT_VALUES] NAME [FROM TO]"));
365 /* Get varobj handle, if a valid var obj name was specified. */
366 if (argc
== 1 || argc
== 3)
367 var
= varobj_get_handle (argv
[0]);
369 var
= varobj_get_handle (argv
[1]);
373 from
= atoi (argv
[argc
- 2]);
374 to
= atoi (argv
[argc
- 1]);
382 children
= varobj_list_children (var
, &from
, &to
);
383 uiout
->field_int ("numchild", to
- from
);
384 if (argc
== 2 || argc
== 4)
385 print_values
= mi_parse_print_values (argv
[0]);
387 print_values
= PRINT_NO_VALUES
;
389 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
391 uiout
->field_string ("displayhint", display_hint
.get ());
395 /* For historical reasons this might emit a list or a tuple, so
396 we construct one or the other. */
397 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
398 gdb::optional
<ui_out_emit_list
> list_emitter
;
400 if (mi_version (uiout
) == 1)
401 tuple_emitter
.emplace (uiout
, "children");
403 list_emitter
.emplace (uiout
, "children");
405 ix
< to
&& VEC_iterate (varobj_p
, children
, ix
, child
);
408 ui_out_emit_tuple
child_emitter (uiout
, "child");
410 print_varobj (child
, print_values
, 1 /* print expression */);
414 uiout
->field_int ("has_more", varobj_has_more (var
, to
));
418 mi_cmd_var_info_type (const char *command
, char **argv
, int argc
)
420 struct ui_out
*uiout
= current_uiout
;
424 error (_("-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 std::string type_name
= varobj_get_type (var
);
430 uiout
->field_string ("type", type_name
.c_str ());
434 mi_cmd_var_info_path_expression (const char *command
, char **argv
, int argc
)
436 struct ui_out
*uiout
= current_uiout
;
440 error (_("Usage: NAME."));
442 /* Get varobj handle, if a valid var obj name was specified. */
443 var
= varobj_get_handle (argv
[0]);
445 const char *path_expr
= varobj_get_path_expr (var
);
447 uiout
->field_string ("path_expr", path_expr
);
451 mi_cmd_var_info_expression (const char *command
, char **argv
, int argc
)
453 struct ui_out
*uiout
= current_uiout
;
454 const struct language_defn
*lang
;
458 error (_("-var-info-expression: Usage: NAME."));
460 /* Get varobj handle, if a valid var obj name was specified. */
461 var
= varobj_get_handle (argv
[0]);
463 lang
= varobj_get_language (var
);
465 uiout
->field_string ("lang", lang
->la_natural_name
);
467 std::string exp
= varobj_get_expression (var
);
468 uiout
->field_string ("exp", exp
.c_str ());
472 mi_cmd_var_show_attributes (const char *command
, char **argv
, int argc
)
474 struct ui_out
*uiout
= current_uiout
;
480 error (_("-var-show-attributes: Usage: NAME."));
482 /* Get varobj handle, if a valid var obj name was specified */
483 var
= varobj_get_handle (argv
[0]);
485 attr
= varobj_get_attributes (var
);
486 /* FIXME: define masks for attributes */
487 if (attr
& 0x00000001)
490 attstr
= "noneditable";
492 uiout
->field_string ("attr", attstr
);
496 mi_cmd_var_evaluate_expression (const char *command
, char **argv
, int argc
)
498 struct ui_out
*uiout
= current_uiout
;
501 enum varobj_display_formats format
;
510 static const struct mi_opt opts
[] =
516 /* Parse arguments. */
517 format
= FORMAT_NATURAL
;
522 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
527 switch ((enum opt
) opt
)
531 error (_("Cannot specify format more than once"));
533 format
= mi_parse_format (oarg
);
540 error (_("Usage: [-f FORMAT] NAME"));
543 error (_("Garbage at end of command"));
545 /* Get varobj handle, if a valid var obj name was specified. */
546 var
= varobj_get_handle (argv
[oind
]);
550 std::string val
= varobj_get_formatted_value (var
, format
);
552 uiout
->field_string ("value", val
.c_str ());
556 std::string val
= varobj_get_value (var
);
558 uiout
->field_string ("value", val
.c_str ());
563 mi_cmd_var_assign (const char *command
, char **argv
, int argc
)
565 struct ui_out
*uiout
= current_uiout
;
569 error (_("-var-assign: Usage: NAME EXPRESSION."));
571 /* Get varobj handle, if a valid var obj name was specified. */
572 var
= varobj_get_handle (argv
[0]);
574 if (!varobj_editable_p (var
))
575 error (_("-var-assign: Variable object is not editable"));
577 const char *expression
= argv
[1];
579 /* MI command '-var-assign' may write memory, so suppress memory
580 changed notification if it does. */
581 scoped_restore save_suppress
582 = make_scoped_restore (&mi_suppress_notification
.memory
, 1);
584 if (!varobj_set_value (var
, expression
))
585 error (_("-var-assign: Could not assign "
586 "expression to variable object"));
588 std::string val
= varobj_get_value (var
);
589 uiout
->field_string ("value", val
.c_str ());
592 /* Type used for parameters passing to mi_cmd_var_update_iter. */
594 struct mi_cmd_var_update
597 enum print_values print_values
;
600 /* Helper for mi_cmd_var_update - update each VAR. */
603 mi_cmd_var_update_iter (struct varobj
*var
, void *data_pointer
)
605 struct mi_cmd_var_update
*data
= (struct mi_cmd_var_update
*) data_pointer
;
606 int thread_id
, thread_stopped
;
608 thread_id
= varobj_get_thread_id (var
);
611 && (ptid_equal (inferior_ptid
, null_ptid
)
612 || is_stopped (inferior_ptid
)))
616 struct thread_info
*tp
= find_thread_global_id (thread_id
);
619 thread_stopped
= is_stopped (tp
->ptid
);
625 && (!data
->only_floating
|| varobj_floating_p (var
)))
626 varobj_update_one (var
, data
->print_values
, 0 /* implicit */);
630 mi_cmd_var_update (const char *command
, char **argv
, int argc
)
632 struct ui_out
*uiout
= current_uiout
;
634 enum print_values print_values
;
636 if (argc
!= 1 && argc
!= 2)
637 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
645 print_values
= mi_parse_print_values (argv
[0]);
647 print_values
= PRINT_NO_VALUES
;
649 /* For historical reasons this might emit a list or a tuple, so we
650 construct one or the other. */
651 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
652 gdb::optional
<ui_out_emit_list
> list_emitter
;
654 if (mi_version (uiout
) <= 1)
655 tuple_emitter
.emplace (uiout
, "changelist");
657 list_emitter
.emplace (uiout
, "changelist");
659 /* Check if the parameter is a "*", which means that we want to
660 update all variables. */
662 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
664 struct mi_cmd_var_update data
;
666 data
.only_floating
= (*name
== '@');
667 data
.print_values
= print_values
;
669 /* varobj_update_one automatically updates all the children of
670 VAROBJ. Therefore update each VAROBJ only once by iterating
671 only the root VAROBJs. */
673 all_root_varobjs (mi_cmd_var_update_iter
, &data
);
677 /* Get varobj handle, if a valid var obj name was specified. */
678 struct varobj
*var
= varobj_get_handle (name
);
680 varobj_update_one (var
, print_values
, 1 /* explicit */);
684 /* Helper for mi_cmd_var_update(). */
687 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
690 struct ui_out
*uiout
= current_uiout
;
691 VEC (varobj_update_result
) *changes
;
692 varobj_update_result
*r
;
695 changes
= varobj_update (&var
, is_explicit
);
697 for (i
= 0; VEC_iterate (varobj_update_result
, changes
, i
, r
); ++i
)
701 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
702 if (mi_version (uiout
) > 1)
703 tuple_emitter
.emplace (uiout
, nullptr);
704 uiout
->field_string ("name", varobj_get_objname (r
->varobj
));
708 case VAROBJ_IN_SCOPE
:
709 if (mi_print_value_p (r
->varobj
, print_values
))
711 std::string val
= varobj_get_value (r
->varobj
);
713 uiout
->field_string ("value", val
.c_str ());
715 uiout
->field_string ("in_scope", "true");
717 case VAROBJ_NOT_IN_SCOPE
:
718 uiout
->field_string ("in_scope", "false");
721 uiout
->field_string ("in_scope", "invalid");
725 if (r
->status
!= VAROBJ_INVALID
)
728 uiout
->field_string ("type_changed", "true");
730 uiout
->field_string ("type_changed", "false");
735 std::string type_name
= varobj_get_type (r
->varobj
);
737 uiout
->field_string ("new_type", type_name
.c_str ());
740 if (r
->type_changed
|| r
->children_changed
)
741 uiout
->field_int ("new_num_children",
742 varobj_get_num_children (r
->varobj
));
744 gdb::unique_xmalloc_ptr
<char> display_hint
745 = varobj_get_display_hint (r
->varobj
);
747 uiout
->field_string ("displayhint", display_hint
.get ());
749 if (varobj_is_dynamic_p (r
->varobj
))
750 uiout
->field_int ("dynamic", 1);
752 varobj_get_child_range (r
->varobj
, &from
, &to
);
753 uiout
->field_int ("has_more", varobj_has_more (r
->varobj
, to
));
760 ui_out_emit_list
list_emitter (uiout
, "new_children");
761 for (j
= 0; VEC_iterate (varobj_p
, r
->newobj
, j
, child
); ++j
)
763 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
764 print_varobj (child
, print_values
, 1 /* print_expression */);
767 VEC_free (varobj_p
, r
->newobj
);
768 r
->newobj
= NULL
; /* Paranoia. */
771 VEC_free (varobj_update_result
, changes
);
775 mi_cmd_enable_pretty_printing (const char *command
, char **argv
, int argc
)
778 error (_("-enable-pretty-printing: no arguments allowed"));
780 varobj_enable_pretty_printing ();
784 mi_cmd_var_set_update_range (const char *command
, char **argv
, int argc
)
790 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
792 var
= varobj_get_handle (argv
[0]);
793 from
= atoi (argv
[1]);
796 varobj_set_child_range (var
, from
, to
);