1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2023 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
);
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
);
70 std::string type
= varobj_get_type (var
);
72 uiout
->field_string ("type", type
);
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
, const char *const *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 gdb_printf (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
, const char *const *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
, const char *const *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
);
243 mi_cmd_var_set_visualizer (const char *command
, const char *const *argv
,
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
, const char *const *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
, const char *const *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
, const char *const *argv
,
307 struct ui_out
*uiout
= current_uiout
;
311 error (_("-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 uiout
->field_signed ("numchild", varobj_get_num_children (var
));
319 /* Return 1 if given the argument PRINT_VALUES we should display
323 mi_print_value_p (struct varobj
*var
, enum print_values print_values
)
327 if (print_values
== PRINT_NO_VALUES
)
330 if (print_values
== PRINT_ALL_VALUES
)
333 if (varobj_is_dynamic_p (var
))
336 type
= varobj_get_gdb_type (var
);
340 return mi_simple_type_p (type
);
346 mi_simple_type_p (struct type
*type
)
348 type
= check_typedef (type
);
350 if (TYPE_IS_REFERENCE (type
))
351 type
= check_typedef (type
->target_type ());
353 switch (type
->code ())
355 case TYPE_CODE_ARRAY
:
356 case TYPE_CODE_STRUCT
:
357 case TYPE_CODE_UNION
:
365 mi_cmd_var_list_children (const char *command
, const char *const *argv
,
368 struct ui_out
*uiout
= current_uiout
;
370 enum print_values print_values
;
373 if (argc
< 1 || argc
> 4)
374 error (_("-var-list-children: Usage: "
375 "[PRINT_VALUES] NAME [FROM TO]"));
377 /* Get varobj handle, if a valid var obj name was specified. */
378 if (argc
== 1 || argc
== 3)
379 var
= varobj_get_handle (argv
[0]);
381 var
= varobj_get_handle (argv
[1]);
385 from
= atoi (argv
[argc
- 2]);
386 to
= atoi (argv
[argc
- 1]);
394 const std::vector
<varobj
*> &children
395 = varobj_list_children (var
, &from
, &to
);
397 uiout
->field_signed ("numchild", to
- from
);
398 if (argc
== 2 || argc
== 4)
399 print_values
= mi_parse_print_values (argv
[0]);
401 print_values
= PRINT_NO_VALUES
;
403 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
405 uiout
->field_string ("displayhint", display_hint
.get ());
409 ui_out_emit_list
list_emitter (uiout
, "children");
410 for (int ix
= from
; ix
< to
&& ix
< children
.size (); ix
++)
412 ui_out_emit_tuple
child_emitter (uiout
, "child");
414 print_varobj (children
[ix
], print_values
, 1 /* print expression */);
418 uiout
->field_signed ("has_more", varobj_has_more (var
, to
));
422 mi_cmd_var_info_type (const char *command
, const char *const *argv
, int argc
)
424 struct ui_out
*uiout
= current_uiout
;
428 error (_("-var-info-type: Usage: NAME."));
430 /* Get varobj handle, if a valid var obj name was specified. */
431 var
= varobj_get_handle (argv
[0]);
433 std::string type_name
= varobj_get_type (var
);
434 uiout
->field_string ("type", type_name
);
438 mi_cmd_var_info_path_expression (const char *command
, const char *const *argv
,
441 struct ui_out
*uiout
= current_uiout
;
445 error (_("Usage: NAME."));
447 /* Get varobj handle, if a valid var obj name was specified. */
448 var
= varobj_get_handle (argv
[0]);
450 const char *path_expr
= varobj_get_path_expr (var
);
452 uiout
->field_string ("path_expr", path_expr
);
456 mi_cmd_var_info_expression (const char *command
, const char *const *argv
,
459 struct ui_out
*uiout
= current_uiout
;
460 const struct language_defn
*lang
;
464 error (_("-var-info-expression: Usage: NAME."));
466 /* Get varobj handle, if a valid var obj name was specified. */
467 var
= varobj_get_handle (argv
[0]);
469 lang
= varobj_get_language (var
);
471 uiout
->field_string ("lang", lang
->natural_name ());
473 std::string exp
= varobj_get_expression (var
);
474 uiout
->field_string ("exp", exp
);
478 mi_cmd_var_show_attributes (const char *command
, const char *const *argv
,
481 struct ui_out
*uiout
= current_uiout
;
487 error (_("-var-show-attributes: Usage: NAME."));
489 /* Get varobj handle, if a valid var obj name was specified */
490 var
= varobj_get_handle (argv
[0]);
492 attr
= varobj_get_attributes (var
);
493 /* FIXME: define masks for attributes */
494 if (attr
& 0x00000001)
497 attstr
= "noneditable";
499 uiout
->field_string ("attr", attstr
);
503 mi_cmd_var_evaluate_expression (const char *command
, const char *const *argv
,
506 struct ui_out
*uiout
= current_uiout
;
509 enum varobj_display_formats format
;
518 static const struct mi_opt opts
[] =
524 /* Parse arguments. */
525 format
= FORMAT_NATURAL
;
530 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
535 switch ((enum opt
) opt
)
539 error (_("Cannot specify format more than once"));
541 format
= mi_parse_format (oarg
);
548 error (_("Usage: [-f FORMAT] NAME"));
551 error (_("Garbage at end of command"));
553 /* Get varobj handle, if a valid var obj name was specified. */
554 var
= varobj_get_handle (argv
[oind
]);
558 std::string val
= varobj_get_formatted_value (var
, format
);
560 uiout
->field_string ("value", val
);
564 std::string val
= varobj_get_value (var
);
566 uiout
->field_string ("value", val
);
571 mi_cmd_var_assign (const char *command
, const char *const *argv
, int argc
)
573 struct ui_out
*uiout
= current_uiout
;
577 error (_("-var-assign: Usage: NAME EXPRESSION."));
579 /* Get varobj handle, if a valid var obj name was specified. */
580 var
= varobj_get_handle (argv
[0]);
582 if (!varobj_editable_p (var
))
583 error (_("-var-assign: Variable object is not editable"));
585 const char *expression
= argv
[1];
587 /* MI command '-var-assign' may write memory, so suppress memory
588 changed notification if it does. */
589 scoped_restore save_suppress
590 = make_scoped_restore (&mi_suppress_notification
.memory
, 1);
592 if (!varobj_set_value (var
, expression
))
593 error (_("-var-assign: Could not assign "
594 "expression to variable object"));
596 std::string val
= varobj_get_value (var
);
597 uiout
->field_string ("value", val
);
600 /* Helper for mi_cmd_var_update - update each VAR. */
603 mi_cmd_var_update_iter (struct varobj
*var
, bool only_floating
,
604 enum print_values print_values
)
608 int thread_id
= varobj_get_thread_id (var
);
612 thread_stopped
= (inferior_ptid
== null_ptid
613 || inferior_thread ()->state
== THREAD_STOPPED
);
617 thread_info
*tp
= find_thread_global_id (thread_id
);
619 thread_stopped
= (tp
== NULL
620 || tp
->state
== THREAD_STOPPED
);
624 && (!only_floating
|| varobj_floating_p (var
)))
625 varobj_update_one (var
, print_values
, false /* implicit */);
629 mi_cmd_var_update (const char *command
, const char *const *argv
, int argc
)
631 struct ui_out
*uiout
= current_uiout
;
633 enum print_values print_values
;
635 if (argc
!= 1 && argc
!= 2)
636 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
644 print_values
= mi_parse_print_values (argv
[0]);
646 print_values
= PRINT_NO_VALUES
;
648 ui_out_emit_list
list_emitter (uiout
, "changelist");
650 /* Check if the parameter is a "*", which means that we want to
651 update all variables. */
653 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
655 /* varobj_update_one automatically updates all the children of
656 VAROBJ. Therefore update each VAROBJ only once by iterating
657 only the root VAROBJs. */
659 all_root_varobjs ([=] (varobj
*var
)
660 { mi_cmd_var_update_iter (var
, *name
== '0', print_values
); });
664 /* Get varobj handle, if a valid var obj name was specified. */
665 struct varobj
*var
= varobj_get_handle (name
);
667 varobj_update_one (var
, print_values
, true /* explicit */);
671 /* Helper for mi_cmd_var_update(). */
674 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
677 struct ui_out
*uiout
= current_uiout
;
679 std::vector
<varobj_update_result
> changes
= varobj_update (&var
, is_explicit
);
681 for (const varobj_update_result
&r
: changes
)
685 ui_out_emit_tuple
tuple_emitter (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
);
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
);
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
, const char *const *argv
,
755 error (_("-enable-pretty-printing: no arguments allowed"));
757 varobj_enable_pretty_printing ();
761 mi_cmd_var_set_update_range (const char *command
, const char *const *argv
,
768 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
770 var
= varobj_get_handle (argv
[0]);
771 from
= atoi (argv
[1]);
774 varobj_set_child_range (var
, from
, to
);