1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2019 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"
36 extern unsigned int varobjdebug; /* defined in varobj.c. */
38 static void varobj_update_one (struct varobj *var,
39 enum print_values print_values,
42 static int mi_print_value_p (struct varobj *var,
43 enum print_values print_values);
45 /* Print variable object VAR. The PRINT_VALUES parameter controls
46 if the value should be printed. The PRINT_EXPRESSION parameter
47 controls if the expression should be printed. */
50 print_varobj (struct varobj *var, enum print_values print_values,
53 struct ui_out *uiout = current_uiout;
56 uiout->field_string ("name", varobj_get_objname (var));
59 std::string exp = varobj_get_expression (var);
61 uiout->field_string ("exp", exp.c_str ());
63 uiout->field_int ("numchild", varobj_get_num_children (var));
65 if (mi_print_value_p (var, print_values))
67 std::string val = varobj_get_value (var);
69 uiout->field_string ("value", val.c_str ());
72 std::string type = varobj_get_type (var);
74 uiout->field_string ("type", type.c_str ());
76 thread_id = varobj_get_thread_id (var);
78 uiout->field_int ("thread-id", thread_id);
80 if (varobj_get_frozen (var))
81 uiout->field_int ("frozen", 1);
83 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
85 uiout->field_string ("displayhint", display_hint.get ());
87 if (varobj_is_dynamic_p (var))
88 uiout->field_int ("dynamic", 1);
91 /* VAROBJ operations */
94 mi_cmd_var_create (const char *command, char **argv, int argc)
96 struct ui_out *uiout = current_uiout;
97 CORE_ADDR frameaddr = 0;
101 enum varobj_type var_type;
104 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
109 const char *name = argv[0];
110 std::string gen_name;
111 if (strcmp (name, "-") == 0)
113 gen_name = varobj_gen_name ();
114 name = gen_name.c_str ();
116 else if (!isalpha (name[0]))
117 error (_("-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 (_("-var-create: unable to create variable object"));
139 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
141 uiout->field_int ("has_more", varobj_has_more (var, 0));
145 mi_cmd_var_delete (const char *command, char **argv, int argc)
150 int children_only_p = 0;
151 struct ui_out *uiout = current_uiout;
153 if (argc < 1 || argc > 2)
154 error (_("-var-delete: Usage: [-c] EXPRESSION."));
158 /* If we have one single argument it cannot be '-c' or any string
159 starting with '-'. */
162 if (strcmp (name, "-c") == 0)
163 error (_("-var-delete: Missing required "
164 "argument after '-c': variable object name"));
166 error (_("-var-delete: Illegal variable object name"));
169 /* If we have 2 arguments they must be '-c' followed by a string
170 which would be the variable name. */
173 if (strcmp (name, "-c") != 0)
174 error (_("-var-delete: Invalid option."));
179 /* If we didn't error out, now NAME contains the name of the
182 var = varobj_get_handle (name);
184 numdel = varobj_delete (var, children_only_p);
186 uiout->field_int ("ndeleted", numdel);
189 /* Parse a string argument into a format value. */
191 static enum varobj_display_formats
192 mi_parse_format (const char *arg)
200 if (strncmp (arg, "natural", len) == 0)
201 return FORMAT_NATURAL;
202 else if (strncmp (arg, "binary", len) == 0)
203 return FORMAT_BINARY;
204 else if (strncmp (arg, "decimal", len) == 0)
205 return FORMAT_DECIMAL;
206 else if (strncmp (arg, "hexadecimal", len) == 0)
207 return FORMAT_HEXADECIMAL;
208 else if (strncmp (arg, "octal", len) == 0)
210 else if (strncmp (arg, "zero-hexadecimal", len) == 0)
211 return FORMAT_ZHEXADECIMAL;
214 error (_("Must specify the format as: \"natural\", "
215 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
219 mi_cmd_var_set_format (const char *command, char **argv, int argc)
221 enum varobj_display_formats format;
223 struct ui_out *uiout = current_uiout;
226 error (_("-var-set-format: Usage: NAME FORMAT."));
228 /* Get varobj handle, if a valid var obj name was specified. */
229 var = varobj_get_handle (argv[0]);
231 format = mi_parse_format (argv[1]);
233 /* Set the format of VAR to the given format. */
234 varobj_set_display_format (var, format);
236 /* Report the new current format. */
237 uiout->field_string ("format", varobj_format_string[(int) format]);
239 /* Report the value in the new format. */
240 std::string val = varobj_get_value (var);
241 uiout->field_string ("value", val.c_str ());
245 mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
250 error (_("Usage: NAME VISUALIZER_FUNCTION."));
252 var = varobj_get_handle (argv[0]);
255 error (_("Variable object not found"));
257 varobj_set_visualizer (var, argv[1]);
261 mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
267 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
269 var = varobj_get_handle (argv[0]);
271 if (strcmp (argv[1], "0") == 0)
273 else if (strcmp (argv[1], "1") == 0)
276 error (_("Invalid flag value"));
278 varobj_set_frozen (var, frozen);
280 /* We don't automatically return the new value, or what varobjs got
281 new values during unfreezing. If this information is required,
282 client should call -var-update explicitly. */
286 mi_cmd_var_show_format (const char *command, char **argv, int argc)
288 struct ui_out *uiout = current_uiout;
289 enum varobj_display_formats format;
293 error (_("-var-show-format: Usage: NAME."));
295 /* Get varobj handle, if a valid var obj name was specified. */
296 var = varobj_get_handle (argv[0]);
298 format = varobj_get_display_format (var);
300 /* Report the current format. */
301 uiout->field_string ("format", varobj_format_string[(int) format]);
305 mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
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_int ("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);
341 type = check_typedef (type);
343 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
344 and that type is not a compound type. */
345 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
346 && TYPE_CODE (type) != TYPE_CODE_STRUCT
347 && TYPE_CODE (type) != TYPE_CODE_UNION);
352 mi_cmd_var_list_children (const char *command, char **argv, int argc)
354 struct ui_out *uiout = current_uiout;
356 enum print_values print_values;
359 if (argc < 1 || argc > 4)
360 error (_("-var-list-children: Usage: "
361 "[PRINT_VALUES] NAME [FROM TO]"));
363 /* Get varobj handle, if a valid var obj name was specified. */
364 if (argc == 1 || argc == 3)
365 var = varobj_get_handle (argv[0]);
367 var = varobj_get_handle (argv[1]);
371 from = atoi (argv[argc - 2]);
372 to = atoi (argv[argc - 1]);
380 const std::vector<varobj *> &children
381 = 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");
404 for (int ix = from; ix < to && ix < children.size (); ix++)
406 ui_out_emit_tuple child_emitter (uiout, "child");
408 print_varobj (children[ix], print_values, 1 /* print expression */);
412 uiout->field_int ("has_more", varobj_has_more (var, to));
416 mi_cmd_var_info_type (const char *command, char **argv, int argc)
418 struct ui_out *uiout = current_uiout;
422 error (_("-var-info-type: Usage: NAME."));
424 /* Get varobj handle, if a valid var obj name was specified. */
425 var = varobj_get_handle (argv[0]);
427 std::string type_name = varobj_get_type (var);
428 uiout->field_string ("type", type_name.c_str ());
432 mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
434 struct ui_out *uiout = current_uiout;
438 error (_("Usage: NAME."));
440 /* Get varobj handle, if a valid var obj name was specified. */
441 var = varobj_get_handle (argv[0]);
443 const char *path_expr = varobj_get_path_expr (var);
445 uiout->field_string ("path_expr", path_expr);
449 mi_cmd_var_info_expression (const char *command, char **argv, int argc)
451 struct ui_out *uiout = current_uiout;
452 const struct language_defn *lang;
456 error (_("-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 uiout->field_string ("lang", lang->la_natural_name);
465 std::string exp = varobj_get_expression (var);
466 uiout->field_string ("exp", exp.c_str ());
470 mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
472 struct ui_out *uiout = current_uiout;
478 error (_("-var-show-attributes: Usage: NAME."));
480 /* Get varobj handle, if a valid var obj name was specified */
481 var = varobj_get_handle (argv[0]);
483 attr = varobj_get_attributes (var);
484 /* FIXME: define masks for attributes */
485 if (attr & 0x00000001)
488 attstr = "noneditable";
490 uiout->field_string ("attr", attstr);
494 mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
496 struct ui_out *uiout = current_uiout;
499 enum varobj_display_formats format;
508 static const struct mi_opt opts[] =
514 /* Parse arguments. */
515 format = FORMAT_NATURAL;
520 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
525 switch ((enum opt) opt)
529 error (_("Cannot specify format more than once"));
531 format = mi_parse_format (oarg);
538 error (_("Usage: [-f FORMAT] NAME"));
541 error (_("Garbage at end of command"));
543 /* Get varobj handle, if a valid var obj name was specified. */
544 var = varobj_get_handle (argv[oind]);
548 std::string val = varobj_get_formatted_value (var, format);
550 uiout->field_string ("value", val.c_str ());
554 std::string val = varobj_get_value (var);
556 uiout->field_string ("value", val.c_str ());
561 mi_cmd_var_assign (const char *command, char **argv, int argc)
563 struct ui_out *uiout = current_uiout;
567 error (_("-var-assign: Usage: NAME EXPRESSION."));
569 /* Get varobj handle, if a valid var obj name was specified. */
570 var = varobj_get_handle (argv[0]);
572 if (!varobj_editable_p (var))
573 error (_("-var-assign: Variable object is not editable"));
575 const char *expression = argv[1];
577 /* MI command '-var-assign' may write memory, so suppress memory
578 changed notification if it does. */
579 scoped_restore save_suppress
580 = make_scoped_restore (&mi_suppress_notification.memory, 1);
582 if (!varobj_set_value (var, expression))
583 error (_("-var-assign: Could not assign "
584 "expression to variable object"));
586 std::string val = varobj_get_value (var);
587 uiout->field_string ("value", val.c_str ());
590 /* Type used for parameters passing to mi_cmd_var_update_iter. */
592 struct mi_cmd_var_update
595 enum print_values print_values;
598 /* Helper for mi_cmd_var_update - update each VAR. */
601 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
603 struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
606 int thread_id = varobj_get_thread_id (var);
610 thread_stopped = (inferior_ptid == null_ptid
611 || inferior_thread ()->state == THREAD_STOPPED);
615 thread_info *tp = find_thread_global_id (thread_id);
617 thread_stopped = (tp == NULL
618 || tp->state == THREAD_STOPPED);
622 && (!data->only_floating || varobj_floating_p (var)))
623 varobj_update_one (var, data->print_values, false /* implicit */);
627 mi_cmd_var_update (const char *command, char **argv, int argc)
629 struct ui_out *uiout = current_uiout;
631 enum print_values print_values;
633 if (argc != 1 && argc != 2)
634 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
642 print_values = mi_parse_print_values (argv[0]);
644 print_values = PRINT_NO_VALUES;
646 /* For historical reasons this might emit a list or a tuple, so we
647 construct one or the other. */
648 gdb::optional<ui_out_emit_tuple> tuple_emitter;
649 gdb::optional<ui_out_emit_list> list_emitter;
651 if (mi_version (uiout) <= 1)
652 tuple_emitter.emplace (uiout, "changelist");
654 list_emitter.emplace (uiout, "changelist");
656 /* Check if the parameter is a "*", which means that we want to
657 update all variables. */
659 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
661 struct mi_cmd_var_update data;
663 data.only_floating = (*name == '@');
664 data.print_values = print_values;
666 /* varobj_update_one automatically updates all the children of
667 VAROBJ. Therefore update each VAROBJ only once by iterating
668 only the root VAROBJs. */
670 all_root_varobjs (mi_cmd_var_update_iter, &data);
674 /* Get varobj handle, if a valid var obj name was specified. */
675 struct varobj *var = varobj_get_handle (name);
677 varobj_update_one (var, print_values, true /* explicit */);
681 /* Helper for mi_cmd_var_update(). */
684 varobj_update_one (struct varobj *var, enum print_values print_values,
687 struct ui_out *uiout = current_uiout;
689 std::vector<varobj_update_result> changes = varobj_update (&var, is_explicit);
691 for (const varobj_update_result &r : changes)
695 gdb::optional<ui_out_emit_tuple> tuple_emitter;
696 if (mi_version (uiout) > 1)
697 tuple_emitter.emplace (uiout, nullptr);
698 uiout->field_string ("name", varobj_get_objname (r.varobj));
702 case VAROBJ_IN_SCOPE:
703 if (mi_print_value_p (r.varobj, print_values))
705 std::string val = varobj_get_value (r.varobj);
707 uiout->field_string ("value", val.c_str ());
709 uiout->field_string ("in_scope", "true");
711 case VAROBJ_NOT_IN_SCOPE:
712 uiout->field_string ("in_scope", "false");
715 uiout->field_string ("in_scope", "invalid");
719 if (r.status != VAROBJ_INVALID)
722 uiout->field_string ("type_changed", "true");
724 uiout->field_string ("type_changed", "false");
729 std::string type_name = varobj_get_type (r.varobj);
731 uiout->field_string ("new_type", type_name.c_str ());
734 if (r.type_changed || r.children_changed)
735 uiout->field_int ("new_num_children",
736 varobj_get_num_children (r.varobj));
738 gdb::unique_xmalloc_ptr<char> display_hint
739 = varobj_get_display_hint (r.varobj);
741 uiout->field_string ("displayhint", display_hint.get ());
743 if (varobj_is_dynamic_p (r.varobj))
744 uiout->field_int ("dynamic", 1);
746 varobj_get_child_range (r.varobj, &from, &to);
747 uiout->field_int ("has_more", varobj_has_more (r.varobj, to));
749 if (!r.newobj.empty ())
751 ui_out_emit_list list_emitter (uiout, "new_children");
753 for (varobj *child : r.newobj)
755 ui_out_emit_tuple inner_tuple_emitter (uiout, NULL);
756 print_varobj (child, print_values, 1 /* print_expression */);
763 mi_cmd_enable_pretty_printing (const char *command, char **argv, int argc)
766 error (_("-enable-pretty-printing: no arguments allowed"));
768 varobj_enable_pretty_printing ();
772 mi_cmd_var_set_update_range (const char *command, char **argv, int argc)
778 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
780 var = varobj_get_handle (argv[0]);
781 from = atoi (argv[1]);
784 varobj_set_child_range (var, from, to);