1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2021 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 "gdbsupport/gdb_optional.h"
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
;
54 uiout
->field_string ("name", varobj_get_objname (var
));
57 std::string exp
= varobj_get_expression (var
);
59 uiout
->field_string ("exp", exp
.c_str ());
61 uiout
->field_signed ("numchild", varobj_get_num_children (var
));
63 if (mi_print_value_p (var
, print_values
))
65 std::string val
= varobj_get_value (var
);
67 uiout
->field_string ("value", val
.c_str ());
70 std::string type
= varobj_get_type (var
);
72 uiout
->field_string ("type", type
.c_str ());
74 thread_id
= varobj_get_thread_id (var
);
76 uiout
->field_signed ("thread-id", thread_id
);
78 if (varobj_get_frozen (var
))
79 uiout
->field_signed ("frozen", 1);
81 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
83 uiout
->field_string ("displayhint", display_hint
.get ());
85 if (varobj_is_dynamic_p (var
))
86 uiout
->field_signed ("dynamic", 1);
89 /* VAROBJ operations */
92 mi_cmd_var_create (const char *command
, char **argv
, int argc
)
94 struct ui_out
*uiout
= current_uiout
;
95 CORE_ADDR frameaddr
= 0;
99 enum varobj_type var_type
;
102 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
107 const char *name
= argv
[0];
108 std::string gen_name
;
109 if (strcmp (name
, "-") == 0)
111 gen_name
= varobj_gen_name ();
112 name
= gen_name
.c_str ();
114 else if (!isalpha (name
[0]))
115 error (_("-var-create: name of object must begin with a letter"));
117 if (strcmp (frame
, "*") == 0)
118 var_type
= USE_CURRENT_FRAME
;
119 else if (strcmp (frame
, "@") == 0)
120 var_type
= USE_SELECTED_FRAME
;
123 var_type
= USE_SPECIFIED_FRAME
;
124 frameaddr
= string_to_core_addr (frame
);
128 fprintf_unfiltered (gdb_stdlog
,
129 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
130 name
, frame
, hex_string (frameaddr
), expr
);
132 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
135 error (_("-var-create: unable to create variable object"));
137 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
139 uiout
->field_signed ("has_more", varobj_has_more (var
, 0));
143 mi_cmd_var_delete (const char *command
, char **argv
, int argc
)
148 int children_only_p
= 0;
149 struct ui_out
*uiout
= current_uiout
;
151 if (argc
< 1 || argc
> 2)
152 error (_("-var-delete: Usage: [-c] EXPRESSION."));
156 /* If we have one single argument it cannot be '-c' or any string
157 starting with '-'. */
160 if (strcmp (name
, "-c") == 0)
161 error (_("-var-delete: Missing required "
162 "argument after '-c': variable object name"));
164 error (_("-var-delete: Illegal variable object name"));
167 /* If we have 2 arguments they must be '-c' followed by a string
168 which would be the variable name. */
171 if (strcmp (name
, "-c") != 0)
172 error (_("-var-delete: Invalid option."));
177 /* If we didn't error out, now NAME contains the name of the
180 var
= varobj_get_handle (name
);
182 numdel
= varobj_delete (var
, children_only_p
);
184 uiout
->field_signed ("ndeleted", numdel
);
187 /* Parse a string argument into a format value. */
189 static enum varobj_display_formats
190 mi_parse_format (const char *arg
)
198 if (strncmp (arg
, "natural", len
) == 0)
199 return FORMAT_NATURAL
;
200 else if (strncmp (arg
, "binary", len
) == 0)
201 return FORMAT_BINARY
;
202 else if (strncmp (arg
, "decimal", len
) == 0)
203 return FORMAT_DECIMAL
;
204 else if (strncmp (arg
, "hexadecimal", len
) == 0)
205 return FORMAT_HEXADECIMAL
;
206 else if (strncmp (arg
, "octal", len
) == 0)
208 else if (strncmp (arg
, "zero-hexadecimal", len
) == 0)
209 return FORMAT_ZHEXADECIMAL
;
212 error (_("Must specify the format as: \"natural\", "
213 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
217 mi_cmd_var_set_format (const char *command
, char **argv
, int argc
)
219 enum varobj_display_formats format
;
221 struct ui_out
*uiout
= current_uiout
;
224 error (_("-var-set-format: Usage: NAME FORMAT."));
226 /* Get varobj handle, if a valid var obj name was specified. */
227 var
= varobj_get_handle (argv
[0]);
229 format
= mi_parse_format (argv
[1]);
231 /* Set the format of VAR to the given format. */
232 varobj_set_display_format (var
, format
);
234 /* Report the new current format. */
235 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
237 /* Report the value in the new format. */
238 std::string val
= varobj_get_value (var
);
239 uiout
->field_string ("value", val
.c_str ());
243 mi_cmd_var_set_visualizer (const char *command
, char **argv
, int argc
)
248 error (_("Usage: NAME VISUALIZER_FUNCTION."));
250 var
= varobj_get_handle (argv
[0]);
253 error (_("Variable object not found"));
255 varobj_set_visualizer (var
, argv
[1]);
259 mi_cmd_var_set_frozen (const char *command
, char **argv
, int argc
)
265 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
267 var
= varobj_get_handle (argv
[0]);
269 if (strcmp (argv
[1], "0") == 0)
271 else if (strcmp (argv
[1], "1") == 0)
274 error (_("Invalid flag value"));
276 varobj_set_frozen (var
, frozen
);
278 /* We don't automatically return the new value, or what varobjs got
279 new values during unfreezing. If this information is required,
280 client should call -var-update explicitly. */
284 mi_cmd_var_show_format (const char *command
, char **argv
, int argc
)
286 struct ui_out
*uiout
= current_uiout
;
287 enum varobj_display_formats format
;
291 error (_("-var-show-format: Usage: NAME."));
293 /* Get varobj handle, if a valid var obj name was specified. */
294 var
= varobj_get_handle (argv
[0]);
296 format
= varobj_get_display_format (var
);
298 /* Report the current format. */
299 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
303 mi_cmd_var_info_num_children (const char *command
, char **argv
, int argc
)
305 struct ui_out
*uiout
= current_uiout
;
309 error (_("-var-info-num-children: Usage: NAME."));
311 /* Get varobj handle, if a valid var obj name was specified. */
312 var
= varobj_get_handle (argv
[0]);
314 uiout
->field_signed ("numchild", varobj_get_num_children (var
));
317 /* Return 1 if given the argument PRINT_VALUES we should display
321 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
325 if (print_values
== PRINT_NO_VALUES
)
328 if (print_values
== PRINT_ALL_VALUES
)
331 if (varobj_is_dynamic_p (var
))
334 type
= varobj_get_gdb_type (var
);
339 type
= check_typedef (type
);
341 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
342 and that type is not a compound type. */
343 return (type
->code () != TYPE_CODE_ARRAY
344 && type
->code () != TYPE_CODE_STRUCT
345 && type
->code () != TYPE_CODE_UNION
);
350 mi_cmd_var_list_children (const char *command
, char **argv
, int argc
)
352 struct ui_out
*uiout
= current_uiout
;
354 enum print_values print_values
;
357 if (argc
< 1 || argc
> 4)
358 error (_("-var-list-children: Usage: "
359 "[PRINT_VALUES] NAME [FROM TO]"));
361 /* Get varobj handle, if a valid var obj name was specified. */
362 if (argc
== 1 || argc
== 3)
363 var
= varobj_get_handle (argv
[0]);
365 var
= varobj_get_handle (argv
[1]);
369 from
= atoi (argv
[argc
- 2]);
370 to
= atoi (argv
[argc
- 1]);
378 const std::vector
<varobj
*> &children
379 = varobj_list_children (var
, &from
, &to
);
381 uiout
->field_signed ("numchild", to
- from
);
382 if (argc
== 2 || argc
== 4)
383 print_values
= mi_parse_print_values (argv
[0]);
385 print_values
= PRINT_NO_VALUES
;
387 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
389 uiout
->field_string ("displayhint", display_hint
.get ());
393 /* For historical reasons this might emit a list or a tuple, so
394 we construct one or the other. */
395 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
396 gdb::optional
<ui_out_emit_list
> list_emitter
;
398 if (mi_version (uiout
) == 1)
399 tuple_emitter
.emplace (uiout
, "children");
401 list_emitter
.emplace (uiout
, "children");
402 for (int ix
= from
; ix
< to
&& ix
< children
.size (); ix
++)
404 ui_out_emit_tuple
child_emitter (uiout
, "child");
406 print_varobj (children
[ix
], print_values
, 1 /* print expression */);
410 uiout
->field_signed ("has_more", varobj_has_more (var
, to
));
414 mi_cmd_var_info_type (const char *command
, char **argv
, int argc
)
416 struct ui_out
*uiout
= current_uiout
;
420 error (_("-var-info-type: Usage: NAME."));
422 /* Get varobj handle, if a valid var obj name was specified. */
423 var
= varobj_get_handle (argv
[0]);
425 std::string type_name
= varobj_get_type (var
);
426 uiout
->field_string ("type", type_name
.c_str ());
430 mi_cmd_var_info_path_expression (const char *command
, char **argv
, int argc
)
432 struct ui_out
*uiout
= current_uiout
;
436 error (_("Usage: NAME."));
438 /* Get varobj handle, if a valid var obj name was specified. */
439 var
= varobj_get_handle (argv
[0]);
441 const char *path_expr
= varobj_get_path_expr (var
);
443 uiout
->field_string ("path_expr", path_expr
);
447 mi_cmd_var_info_expression (const char *command
, char **argv
, int argc
)
449 struct ui_out
*uiout
= current_uiout
;
450 const struct language_defn
*lang
;
454 error (_("-var-info-expression: Usage: NAME."));
456 /* Get varobj handle, if a valid var obj name was specified. */
457 var
= varobj_get_handle (argv
[0]);
459 lang
= varobj_get_language (var
);
461 uiout
->field_string ("lang", lang
->natural_name ());
463 std::string exp
= varobj_get_expression (var
);
464 uiout
->field_string ("exp", exp
.c_str ());
468 mi_cmd_var_show_attributes (const char *command
, char **argv
, int argc
)
470 struct ui_out
*uiout
= current_uiout
;
476 error (_("-var-show-attributes: Usage: NAME."));
478 /* Get varobj handle, if a valid var obj name was specified */
479 var
= varobj_get_handle (argv
[0]);
481 attr
= varobj_get_attributes (var
);
482 /* FIXME: define masks for attributes */
483 if (attr
& 0x00000001)
486 attstr
= "noneditable";
488 uiout
->field_string ("attr", attstr
);
492 mi_cmd_var_evaluate_expression (const char *command
, char **argv
, int argc
)
494 struct ui_out
*uiout
= current_uiout
;
497 enum varobj_display_formats format
;
506 static const struct mi_opt opts
[] =
512 /* Parse arguments. */
513 format
= FORMAT_NATURAL
;
518 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
523 switch ((enum opt
) opt
)
527 error (_("Cannot specify format more than once"));
529 format
= mi_parse_format (oarg
);
536 error (_("Usage: [-f FORMAT] NAME"));
539 error (_("Garbage at end of command"));
541 /* Get varobj handle, if a valid var obj name was specified. */
542 var
= varobj_get_handle (argv
[oind
]);
546 std::string val
= varobj_get_formatted_value (var
, format
);
548 uiout
->field_string ("value", val
.c_str ());
552 std::string val
= varobj_get_value (var
);
554 uiout
->field_string ("value", val
.c_str ());
559 mi_cmd_var_assign (const char *command
, char **argv
, int argc
)
561 struct ui_out
*uiout
= current_uiout
;
565 error (_("-var-assign: Usage: NAME EXPRESSION."));
567 /* Get varobj handle, if a valid var obj name was specified. */
568 var
= varobj_get_handle (argv
[0]);
570 if (!varobj_editable_p (var
))
571 error (_("-var-assign: Variable object is not editable"));
573 const char *expression
= argv
[1];
575 /* MI command '-var-assign' may write memory, so suppress memory
576 changed notification if it does. */
577 scoped_restore save_suppress
578 = make_scoped_restore (&mi_suppress_notification
.memory
, 1);
580 if (!varobj_set_value (var
, expression
))
581 error (_("-var-assign: Could not assign "
582 "expression to variable object"));
584 std::string val
= varobj_get_value (var
);
585 uiout
->field_string ("value", val
.c_str ());
588 /* Helper for mi_cmd_var_update - update each VAR. */
591 mi_cmd_var_update_iter (struct varobj
*var
, bool only_floating
,
592 enum print_values print_values
)
596 int thread_id
= varobj_get_thread_id (var
);
600 thread_stopped
= (inferior_ptid
== null_ptid
601 || inferior_thread ()->state
== THREAD_STOPPED
);
605 thread_info
*tp
= find_thread_global_id (thread_id
);
607 thread_stopped
= (tp
== NULL
608 || tp
->state
== THREAD_STOPPED
);
612 && (!only_floating
|| varobj_floating_p (var
)))
613 varobj_update_one (var
, print_values
, false /* implicit */);
617 mi_cmd_var_update (const char *command
, char **argv
, int argc
)
619 struct ui_out
*uiout
= current_uiout
;
621 enum print_values print_values
;
623 if (argc
!= 1 && argc
!= 2)
624 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
632 print_values
= mi_parse_print_values (argv
[0]);
634 print_values
= PRINT_NO_VALUES
;
636 /* For historical reasons this might emit a list or a tuple, so we
637 construct one or the other. */
638 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
639 gdb::optional
<ui_out_emit_list
> list_emitter
;
641 if (mi_version (uiout
) <= 1)
642 tuple_emitter
.emplace (uiout
, "changelist");
644 list_emitter
.emplace (uiout
, "changelist");
646 /* Check if the parameter is a "*", which means that we want to
647 update all variables. */
649 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
651 /* varobj_update_one automatically updates all the children of
652 VAROBJ. Therefore update each VAROBJ only once by iterating
653 only the root VAROBJs. */
655 all_root_varobjs ([=] (varobj
*var
)
657 mi_cmd_var_update_iter (var
, *name
== '0', print_values
);
662 /* Get varobj handle, if a valid var obj name was specified. */
663 struct varobj
*var
= varobj_get_handle (name
);
665 varobj_update_one (var
, print_values
, true /* explicit */);
669 /* Helper for mi_cmd_var_update(). */
672 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
675 struct ui_out
*uiout
= current_uiout
;
677 std::vector
<varobj_update_result
> changes
= varobj_update (&var
, is_explicit
);
679 for (const varobj_update_result
&r
: changes
)
683 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
684 if (mi_version (uiout
) > 1)
685 tuple_emitter
.emplace (uiout
, nullptr);
686 uiout
->field_string ("name", varobj_get_objname (r
.varobj
));
690 case VAROBJ_IN_SCOPE
:
691 if (mi_print_value_p (r
.varobj
, print_values
))
693 std::string val
= varobj_get_value (r
.varobj
);
695 uiout
->field_string ("value", val
.c_str ());
697 uiout
->field_string ("in_scope", "true");
699 case VAROBJ_NOT_IN_SCOPE
:
700 uiout
->field_string ("in_scope", "false");
703 uiout
->field_string ("in_scope", "invalid");
707 if (r
.status
!= VAROBJ_INVALID
)
710 uiout
->field_string ("type_changed", "true");
712 uiout
->field_string ("type_changed", "false");
717 std::string type_name
= varobj_get_type (r
.varobj
);
719 uiout
->field_string ("new_type", type_name
.c_str ());
722 if (r
.type_changed
|| r
.children_changed
)
723 uiout
->field_signed ("new_num_children",
724 varobj_get_num_children (r
.varobj
));
726 gdb::unique_xmalloc_ptr
<char> display_hint
727 = varobj_get_display_hint (r
.varobj
);
729 uiout
->field_string ("displayhint", display_hint
.get ());
731 if (varobj_is_dynamic_p (r
.varobj
))
732 uiout
->field_signed ("dynamic", 1);
734 varobj_get_child_range (r
.varobj
, &from
, &to
);
735 uiout
->field_signed ("has_more", varobj_has_more (r
.varobj
, to
));
737 if (!r
.newobj
.empty ())
739 ui_out_emit_list
list_emitter (uiout
, "new_children");
741 for (varobj
*child
: r
.newobj
)
743 ui_out_emit_tuple
inner_tuple_emitter (uiout
, NULL
);
744 print_varobj (child
, print_values
, 1 /* print_expression */);
751 mi_cmd_enable_pretty_printing (const char *command
, char **argv
, int argc
)
754 error (_("-enable-pretty-printing: no arguments allowed"));
756 varobj_enable_pretty_printing ();
760 mi_cmd_var_set_update_range (const char *command
, char **argv
, int argc
)
766 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
768 var
= varobj_get_handle (argv
[0]);
769 from
= atoi (argv
[1]);
772 varobj_set_child_range (var
, from
, to
);