1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2024 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/>. */
29 #include "mi-getopt.h"
30 #include "gdbthread.h"
35 static void varobj_update_one (struct varobj
*var
,
36 enum print_values print_values
,
39 static int mi_print_value_p (struct varobj
*var
,
40 enum print_values print_values
);
42 /* Print variable object VAR. The PRINT_VALUES parameter controls
43 if the value should be printed. The PRINT_EXPRESSION parameter
44 controls if the expression should be printed. */
47 print_varobj (struct varobj
*var
, enum print_values print_values
,
50 struct ui_out
*uiout
= current_uiout
;
53 uiout
->field_string ("name", varobj_get_objname (var
));
56 std::string exp
= varobj_get_expression (var
);
58 uiout
->field_string ("exp", exp
);
60 uiout
->field_signed ("numchild", varobj_get_num_children (var
));
62 if (mi_print_value_p (var
, print_values
))
64 std::string val
= varobj_get_value (var
);
66 uiout
->field_string ("value", val
);
69 std::string type
= varobj_get_type (var
);
71 uiout
->field_string ("type", type
);
73 thread_id
= varobj_get_thread_id (var
);
75 uiout
->field_signed ("thread-id", thread_id
);
77 if (varobj_get_frozen (var
))
78 uiout
->field_signed ("frozen", 1);
80 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
82 uiout
->field_string ("displayhint", display_hint
.get ());
84 if (varobj_is_dynamic_p (var
))
85 uiout
->field_signed ("dynamic", 1);
88 /* VAROBJ operations */
91 mi_cmd_var_create (const char *command
, const char *const *argv
, int argc
)
93 struct ui_out
*uiout
= current_uiout
;
94 CORE_ADDR frameaddr
= 0;
98 enum varobj_type var_type
;
101 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
106 const char *name
= argv
[0];
107 std::string gen_name
;
108 if (strcmp (name
, "-") == 0)
110 gen_name
= varobj_gen_name ();
111 name
= gen_name
.c_str ();
113 else if (!isalpha (name
[0]))
114 error (_("-var-create: name of object must begin with a letter"));
116 if (strcmp (frame
, "*") == 0)
117 var_type
= USE_CURRENT_FRAME
;
118 else if (strcmp (frame
, "@") == 0)
119 var_type
= USE_SELECTED_FRAME
;
122 var_type
= USE_SPECIFIED_FRAME
;
123 frameaddr
= string_to_core_addr (frame
);
127 gdb_printf (gdb_stdlog
,
128 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
129 name
, frame
, hex_string (frameaddr
), expr
);
131 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
134 error (_("-var-create: unable to create variable object"));
136 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
138 uiout
->field_signed ("has_more", varobj_has_more (var
, 0));
142 mi_cmd_var_delete (const char *command
, const char *const *argv
, int argc
)
147 int children_only_p
= 0;
148 struct ui_out
*uiout
= current_uiout
;
150 if (argc
< 1 || argc
> 2)
151 error (_("-var-delete: Usage: [-c] EXPRESSION."));
155 /* If we have one single argument it cannot be '-c' or any string
156 starting with '-'. */
159 if (strcmp (name
, "-c") == 0)
160 error (_("-var-delete: Missing required "
161 "argument after '-c': variable object name"));
163 error (_("-var-delete: Illegal variable object name"));
166 /* If we have 2 arguments they must be '-c' followed by a string
167 which would be the variable name. */
170 if (strcmp (name
, "-c") != 0)
171 error (_("-var-delete: Invalid option."));
176 /* If we didn't error out, now NAME contains the name of the
179 var
= varobj_get_handle (name
);
181 numdel
= varobj_delete (var
, children_only_p
);
183 uiout
->field_signed ("ndeleted", numdel
);
186 /* Parse a string argument into a format value. */
188 static enum varobj_display_formats
189 mi_parse_format (const char *arg
)
197 if (strncmp (arg
, "natural", len
) == 0)
198 return FORMAT_NATURAL
;
199 else if (strncmp (arg
, "binary", len
) == 0)
200 return FORMAT_BINARY
;
201 else if (strncmp (arg
, "decimal", len
) == 0)
202 return FORMAT_DECIMAL
;
203 else if (strncmp (arg
, "hexadecimal", len
) == 0)
204 return FORMAT_HEXADECIMAL
;
205 else if (strncmp (arg
, "octal", len
) == 0)
207 else if (strncmp (arg
, "zero-hexadecimal", len
) == 0)
208 return FORMAT_ZHEXADECIMAL
;
211 error (_("Must specify the format as: \"natural\", "
212 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
216 mi_cmd_var_set_format (const char *command
, const char *const *argv
, int argc
)
218 enum varobj_display_formats format
;
220 struct ui_out
*uiout
= current_uiout
;
223 error (_("-var-set-format: Usage: NAME FORMAT."));
225 /* Get varobj handle, if a valid var obj name was specified. */
226 var
= varobj_get_handle (argv
[0]);
228 format
= mi_parse_format (argv
[1]);
230 /* Set the format of VAR to the given format. */
231 varobj_set_display_format (var
, format
);
233 /* Report the new current format. */
234 uiout
->field_string ("format", varobj_format_string
[(int) format
]);
236 /* Report the value in the new format. */
237 std::string val
= varobj_get_value (var
);
238 uiout
->field_string ("value", val
);
242 mi_cmd_var_set_visualizer (const char *command
, const char *const *argv
,
248 error (_("-var-set-visualizer: 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
, const char *const *argv
, int argc
)
265 error (_("-var-set-frozen: 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
, const char *const *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
, const char *const *argv
,
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_signed ("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
);
339 return mi_simple_type_p (type
);
345 mi_simple_type_p (struct type
*type
)
347 type
= check_typedef (type
);
349 if (TYPE_IS_REFERENCE (type
))
350 type
= check_typedef (type
->target_type ());
352 switch (type
->code ())
354 case TYPE_CODE_ARRAY
:
355 case TYPE_CODE_STRUCT
:
356 case TYPE_CODE_UNION
:
364 mi_cmd_var_list_children (const char *command
, const char *const *argv
,
367 struct ui_out
*uiout
= current_uiout
;
369 enum print_values print_values
;
372 if (argc
< 1 || argc
> 4)
373 error (_("-var-list-children: Usage: "
374 "[PRINT_VALUES] NAME [FROM TO]"));
376 /* Get varobj handle, if a valid var obj name was specified. */
377 if (argc
== 1 || argc
== 3)
378 var
= varobj_get_handle (argv
[0]);
380 var
= varobj_get_handle (argv
[1]);
384 from
= atoi (argv
[argc
- 2]);
385 to
= atoi (argv
[argc
- 1]);
393 const std::vector
<varobj
*> &children
394 = varobj_list_children (var
, &from
, &to
);
396 uiout
->field_signed ("numchild", to
- from
);
397 if (argc
== 2 || argc
== 4)
398 print_values
= mi_parse_print_values (argv
[0]);
400 print_values
= PRINT_NO_VALUES
;
402 gdb::unique_xmalloc_ptr
<char> display_hint
= varobj_get_display_hint (var
);
404 uiout
->field_string ("displayhint", display_hint
.get ());
408 ui_out_emit_list
list_emitter (uiout
, "children");
409 for (int ix
= from
; ix
< to
&& ix
< children
.size (); ix
++)
411 ui_out_emit_tuple
child_emitter (uiout
, "child");
413 print_varobj (children
[ix
], print_values
, 1 /* print expression */);
417 uiout
->field_signed ("has_more", varobj_has_more (var
, to
));
421 mi_cmd_var_info_type (const char *command
, const char *const *argv
, int argc
)
423 struct ui_out
*uiout
= current_uiout
;
427 error (_("-var-info-type: Usage: NAME."));
429 /* Get varobj handle, if a valid var obj name was specified. */
430 var
= varobj_get_handle (argv
[0]);
432 std::string type_name
= varobj_get_type (var
);
433 uiout
->field_string ("type", type_name
);
437 mi_cmd_var_info_path_expression (const char *command
, const char *const *argv
,
440 struct ui_out
*uiout
= current_uiout
;
444 error (_("-var-info-path-expression: Usage: NAME."));
446 /* Get varobj handle, if a valid var obj name was specified. */
447 var
= varobj_get_handle (argv
[0]);
449 const char *path_expr
= varobj_get_path_expr (var
);
451 uiout
->field_string ("path_expr", path_expr
);
455 mi_cmd_var_info_expression (const char *command
, const char *const *argv
,
458 struct ui_out
*uiout
= current_uiout
;
459 const struct language_defn
*lang
;
463 error (_("-var-info-expression: Usage: NAME."));
465 /* Get varobj handle, if a valid var obj name was specified. */
466 var
= varobj_get_handle (argv
[0]);
468 lang
= varobj_get_language (var
);
470 uiout
->field_string ("lang", lang
->natural_name ());
472 std::string exp
= varobj_get_expression (var
);
473 uiout
->field_string ("exp", exp
);
477 mi_cmd_var_show_attributes (const char *command
, const char *const *argv
,
480 struct ui_out
*uiout
= current_uiout
;
486 error (_("-var-show-attributes: Usage: NAME."));
488 /* Get varobj handle, if a valid var obj name was specified */
489 var
= varobj_get_handle (argv
[0]);
491 attr
= varobj_get_attributes (var
);
492 /* FIXME: define masks for attributes */
493 if (attr
& 0x00000001)
496 attstr
= "noneditable";
498 uiout
->field_string ("attr", attstr
);
502 mi_cmd_var_evaluate_expression (const char *command
, const char *const *argv
,
505 struct ui_out
*uiout
= current_uiout
;
508 enum varobj_display_formats format
;
517 static const struct mi_opt opts
[] =
523 /* Parse arguments. */
524 format
= FORMAT_NATURAL
;
529 int opt
= mi_getopt ("-var-evaluate-expression", argc
, argv
,
534 switch ((enum opt
) opt
)
538 error (_("Cannot specify format more than once"));
540 format
= mi_parse_format (oarg
);
547 error (_("Usage: [-f FORMAT] NAME"));
550 error (_("Garbage at end of command"));
552 /* Get varobj handle, if a valid var obj name was specified. */
553 var
= varobj_get_handle (argv
[oind
]);
557 std::string val
= varobj_get_formatted_value (var
, format
);
559 uiout
->field_string ("value", val
);
563 std::string val
= varobj_get_value (var
);
565 uiout
->field_string ("value", val
);
570 mi_cmd_var_assign (const char *command
, const char *const *argv
, int argc
)
572 struct ui_out
*uiout
= current_uiout
;
576 error (_("-var-assign: Usage: NAME EXPRESSION."));
578 /* Get varobj handle, if a valid var obj name was specified. */
579 var
= varobj_get_handle (argv
[0]);
581 if (!varobj_editable_p (var
))
582 error (_("-var-assign: Variable object is not editable"));
584 const char *expression
= argv
[1];
586 /* MI command '-var-assign' may write memory, so suppress memory
587 changed notification if it does. */
588 scoped_restore save_suppress
589 = make_scoped_restore (&mi_suppress_notification
.memory
, 1);
591 if (!varobj_set_value (var
, expression
))
592 error (_("-var-assign: Could not assign "
593 "expression to variable object"));
595 std::string val
= varobj_get_value (var
);
596 uiout
->field_string ("value", val
);
599 /* Helper for mi_cmd_var_update - update each VAR. */
602 mi_cmd_var_update_iter (struct varobj
*var
, bool only_floating
,
603 enum print_values print_values
)
607 int thread_id
= varobj_get_thread_id (var
);
611 thread_stopped
= (inferior_ptid
== null_ptid
612 || inferior_thread ()->state
== THREAD_STOPPED
);
616 thread_info
*tp
= find_thread_global_id (thread_id
);
618 thread_stopped
= (tp
== NULL
619 || tp
->state
== THREAD_STOPPED
);
623 && (!only_floating
|| varobj_floating_p (var
)))
624 varobj_update_one (var
, print_values
, false /* implicit */);
628 mi_cmd_var_update (const char *command
, const char *const *argv
, int argc
)
630 struct ui_out
*uiout
= current_uiout
;
632 enum print_values print_values
;
634 if (argc
!= 1 && argc
!= 2)
635 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
643 print_values
= mi_parse_print_values (argv
[0]);
645 print_values
= PRINT_NO_VALUES
;
647 ui_out_emit_list
list_emitter (uiout
, "changelist");
649 /* Check if the parameter is a "*", which means that we want to
650 update all variables. */
652 if ((*name
== '*' || *name
== '@') && (*(name
+ 1) == '\0'))
654 /* varobj_update_one automatically updates all the children of
655 VAROBJ. Therefore update each VAROBJ only once by iterating
656 only the root VAROBJs. */
658 all_root_varobjs ([=] (varobj
*var
)
659 { mi_cmd_var_update_iter (var
, *name
== '0', print_values
); });
663 /* Get varobj handle, if a valid var obj name was specified. */
664 struct varobj
*var
= varobj_get_handle (name
);
666 varobj_update_one (var
, print_values
, true /* explicit */);
670 /* Helper for mi_cmd_var_update(). */
673 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
676 struct ui_out
*uiout
= current_uiout
;
678 std::vector
<varobj_update_result
> changes
= varobj_update (&var
, is_explicit
);
680 for (const varobj_update_result
&r
: changes
)
684 ui_out_emit_tuple
tuple_emitter (uiout
, nullptr);
685 uiout
->field_string ("name", varobj_get_objname (r
.varobj
));
689 case VAROBJ_IN_SCOPE
:
690 if (mi_print_value_p (r
.varobj
, print_values
))
692 std::string val
= varobj_get_value (r
.varobj
);
694 uiout
->field_string ("value", val
);
696 uiout
->field_string ("in_scope", "true");
698 case VAROBJ_NOT_IN_SCOPE
:
699 uiout
->field_string ("in_scope", "false");
702 uiout
->field_string ("in_scope", "invalid");
706 if (r
.status
!= VAROBJ_INVALID
)
709 uiout
->field_string ("type_changed", "true");
711 uiout
->field_string ("type_changed", "false");
716 std::string type_name
= varobj_get_type (r
.varobj
);
718 uiout
->field_string ("new_type", type_name
);
721 if (r
.type_changed
|| r
.children_changed
)
722 uiout
->field_signed ("new_num_children",
723 varobj_get_num_children (r
.varobj
));
725 gdb::unique_xmalloc_ptr
<char> display_hint
726 = varobj_get_display_hint (r
.varobj
);
728 uiout
->field_string ("displayhint", display_hint
.get ());
730 if (varobj_is_dynamic_p (r
.varobj
))
731 uiout
->field_signed ("dynamic", 1);
733 varobj_get_child_range (r
.varobj
, &from
, &to
);
734 uiout
->field_signed ("has_more", varobj_has_more (r
.varobj
, to
));
736 if (!r
.newobj
.empty ())
738 ui_out_emit_list
list_emitter (uiout
, "new_children");
740 for (varobj
*child
: r
.newobj
)
742 ui_out_emit_tuple
inner_tuple_emitter (uiout
, NULL
);
743 print_varobj (child
, print_values
, 1 /* print_expression */);
750 mi_cmd_enable_pretty_printing (const char *command
, const char *const *argv
,
754 error (_("-enable-pretty-printing: no arguments allowed"));
756 varobj_enable_pretty_printing ();
760 mi_cmd_var_set_update_range (const char *command
, const char *const *argv
,
767 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
769 var
= varobj_get_handle (argv
[0]);
770 from
= atoi (argv
[1]);
773 varobj_set_child_range (var
, from
, to
);