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 enum print_values print_values;
358 if (argc < 1 || argc > 4)
359 error (_("-var-list-children: Usage: "
360 "[PRINT_VALUES] NAME [FROM TO]"));
362 /* Get varobj handle, if a valid var obj name was specified. */
363 if (argc == 1 || argc == 3)
364 var = varobj_get_handle (argv[0]);
366 var = varobj_get_handle (argv[1]);
370 from = atoi (argv[argc - 2]);
371 to = atoi (argv[argc - 1]);
379 const std::vector<varobj *> &children
380 = varobj_list_children (var, &from, &to);
382 uiout->field_int ("numchild", to - from);
383 if (argc == 2 || argc == 4)
384 print_values = mi_parse_print_values (argv[0]);
386 print_values = PRINT_NO_VALUES;
388 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
390 uiout->field_string ("displayhint", display_hint.get ());
394 /* For historical reasons this might emit a list or a tuple, so
395 we construct one or the other. */
396 gdb::optional<ui_out_emit_tuple> tuple_emitter;
397 gdb::optional<ui_out_emit_list> list_emitter;
399 if (mi_version (uiout) == 1)
400 tuple_emitter.emplace (uiout, "children");
402 list_emitter.emplace (uiout, "children");
403 for (int ix = from; ix < to && ix < children.size (); ix++)
405 ui_out_emit_tuple child_emitter (uiout, "child");
407 print_varobj (children[ix], print_values, 1 /* print expression */);
411 uiout->field_int ("has_more", varobj_has_more (var, to));
415 mi_cmd_var_info_type (const char *command, char **argv, int argc)
417 struct ui_out *uiout = current_uiout;
421 error (_("-var-info-type: Usage: NAME."));
423 /* Get varobj handle, if a valid var obj name was specified. */
424 var = varobj_get_handle (argv[0]);
426 std::string type_name = varobj_get_type (var);
427 uiout->field_string ("type", type_name.c_str ());
431 mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
433 struct ui_out *uiout = current_uiout;
437 error (_("Usage: NAME."));
439 /* Get varobj handle, if a valid var obj name was specified. */
440 var = varobj_get_handle (argv[0]);
442 const char *path_expr = varobj_get_path_expr (var);
444 uiout->field_string ("path_expr", path_expr);
448 mi_cmd_var_info_expression (const char *command, char **argv, int argc)
450 struct ui_out *uiout = current_uiout;
451 const struct language_defn *lang;
455 error (_("-var-info-expression: Usage: NAME."));
457 /* Get varobj handle, if a valid var obj name was specified. */
458 var = varobj_get_handle (argv[0]);
460 lang = varobj_get_language (var);
462 uiout->field_string ("lang", lang->la_natural_name);
464 std::string exp = varobj_get_expression (var);
465 uiout->field_string ("exp", exp.c_str ());
469 mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
471 struct ui_out *uiout = current_uiout;
477 error (_("-var-show-attributes: Usage: NAME."));
479 /* Get varobj handle, if a valid var obj name was specified */
480 var = varobj_get_handle (argv[0]);
482 attr = varobj_get_attributes (var);
483 /* FIXME: define masks for attributes */
484 if (attr & 0x00000001)
487 attstr = "noneditable";
489 uiout->field_string ("attr", attstr);
493 mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
495 struct ui_out *uiout = current_uiout;
498 enum varobj_display_formats format;
507 static const struct mi_opt opts[] =
513 /* Parse arguments. */
514 format = FORMAT_NATURAL;
519 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
524 switch ((enum opt) opt)
528 error (_("Cannot specify format more than once"));
530 format = mi_parse_format (oarg);
537 error (_("Usage: [-f FORMAT] NAME"));
540 error (_("Garbage at end of command"));
542 /* Get varobj handle, if a valid var obj name was specified. */
543 var = varobj_get_handle (argv[oind]);
547 std::string val = varobj_get_formatted_value (var, format);
549 uiout->field_string ("value", val.c_str ());
553 std::string val = varobj_get_value (var);
555 uiout->field_string ("value", val.c_str ());
560 mi_cmd_var_assign (const char *command, char **argv, int argc)
562 struct ui_out *uiout = current_uiout;
566 error (_("-var-assign: Usage: NAME EXPRESSION."));
568 /* Get varobj handle, if a valid var obj name was specified. */
569 var = varobj_get_handle (argv[0]);
571 if (!varobj_editable_p (var))
572 error (_("-var-assign: Variable object is not editable"));
574 const char *expression = argv[1];
576 /* MI command '-var-assign' may write memory, so suppress memory
577 changed notification if it does. */
578 scoped_restore save_suppress
579 = make_scoped_restore (&mi_suppress_notification.memory, 1);
581 if (!varobj_set_value (var, expression))
582 error (_("-var-assign: Could not assign "
583 "expression to variable object"));
585 std::string val = varobj_get_value (var);
586 uiout->field_string ("value", val.c_str ());
589 /* Type used for parameters passing to mi_cmd_var_update_iter. */
591 struct mi_cmd_var_update
594 enum print_values print_values;
597 /* Helper for mi_cmd_var_update - update each VAR. */
600 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
602 struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
603 int thread_id, thread_stopped;
605 thread_id = varobj_get_thread_id (var);
608 && (ptid_equal (inferior_ptid, null_ptid)
609 || is_stopped (inferior_ptid)))
613 struct thread_info *tp = find_thread_global_id (thread_id);
616 thread_stopped = is_stopped (tp->ptid);
622 && (!data->only_floating || varobj_floating_p (var)))
623 varobj_update_one (var, data->print_values, 0 /* 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, 1 /* 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 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);