gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3
4 Contributed by Cygnus Solutions (a Red Hat company).
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #include "defs.h"
22 #include "mi-cmds.h"
23 #include "mi-main.h"
24 #include "ui-out.h"
25 #include "mi-out.h"
26 #include "varobj.h"
27 #include "language.h"
28 #include "value.h"
29 #include <ctype.h>
30 #include "mi-getopt.h"
31 #include "gdbthread.h"
32 #include "mi-parse.h"
33 #include "gdbsupport/gdb_optional.h"
34 #include "inferior.h"
35
36 static void varobj_update_one (struct varobj *var,
37 enum print_values print_values,
38 bool is_explicit);
39
40 static int mi_print_value_p (struct varobj *var,
41 enum print_values print_values);
42
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. */
46
47 static void
48 print_varobj (struct varobj *var, enum print_values print_values,
49 int print_expression)
50 {
51 struct ui_out *uiout = current_uiout;
52 int thread_id;
53
54 uiout->field_string ("name", varobj_get_objname (var));
55 if (print_expression)
56 {
57 std::string exp = varobj_get_expression (var);
58
59 uiout->field_string ("exp", exp.c_str ());
60 }
61 uiout->field_signed ("numchild", varobj_get_num_children (var));
62
63 if (mi_print_value_p (var, print_values))
64 {
65 std::string val = varobj_get_value (var);
66
67 uiout->field_string ("value", val.c_str ());
68 }
69
70 std::string type = varobj_get_type (var);
71 if (!type.empty ())
72 uiout->field_string ("type", type.c_str ());
73
74 thread_id = varobj_get_thread_id (var);
75 if (thread_id > 0)
76 uiout->field_signed ("thread-id", thread_id);
77
78 if (varobj_get_frozen (var))
79 uiout->field_signed ("frozen", 1);
80
81 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
82 if (display_hint)
83 uiout->field_string ("displayhint", display_hint.get ());
84
85 if (varobj_is_dynamic_p (var))
86 uiout->field_signed ("dynamic", 1);
87 }
88
89 /* VAROBJ operations */
90
91 void
92 mi_cmd_var_create (const char *command, char **argv, int argc)
93 {
94 struct ui_out *uiout = current_uiout;
95 CORE_ADDR frameaddr = 0;
96 struct varobj *var;
97 char *frame;
98 char *expr;
99 enum varobj_type var_type;
100
101 if (argc != 3)
102 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
103
104 frame = argv[1];
105 expr = argv[2];
106
107 const char *name = argv[0];
108 std::string gen_name;
109 if (strcmp (name, "-") == 0)
110 {
111 gen_name = varobj_gen_name ();
112 name = gen_name.c_str ();
113 }
114 else if (!isalpha (name[0]))
115 error (_("-var-create: name of object must begin with a letter"));
116
117 if (strcmp (frame, "*") == 0)
118 var_type = USE_CURRENT_FRAME;
119 else if (strcmp (frame, "@") == 0)
120 var_type = USE_SELECTED_FRAME;
121 else
122 {
123 var_type = USE_SPECIFIED_FRAME;
124 frameaddr = string_to_core_addr (frame);
125 }
126
127 if (varobjdebug)
128 fprintf_unfiltered (gdb_stdlog,
129 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
130 name, frame, hex_string (frameaddr), expr);
131
132 var = varobj_create (name, expr, frameaddr, var_type);
133
134 if (var == NULL)
135 error (_("-var-create: unable to create variable object"));
136
137 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
138
139 uiout->field_signed ("has_more", varobj_has_more (var, 0));
140 }
141
142 void
143 mi_cmd_var_delete (const char *command, char **argv, int argc)
144 {
145 char *name;
146 struct varobj *var;
147 int numdel;
148 int children_only_p = 0;
149 struct ui_out *uiout = current_uiout;
150
151 if (argc < 1 || argc > 2)
152 error (_("-var-delete: Usage: [-c] EXPRESSION."));
153
154 name = argv[0];
155
156 /* If we have one single argument it cannot be '-c' or any string
157 starting with '-'. */
158 if (argc == 1)
159 {
160 if (strcmp (name, "-c") == 0)
161 error (_("-var-delete: Missing required "
162 "argument after '-c': variable object name"));
163 if (*name == '-')
164 error (_("-var-delete: Illegal variable object name"));
165 }
166
167 /* If we have 2 arguments they must be '-c' followed by a string
168 which would be the variable name. */
169 if (argc == 2)
170 {
171 if (strcmp (name, "-c") != 0)
172 error (_("-var-delete: Invalid option."));
173 children_only_p = 1;
174 name = argv[1];
175 }
176
177 /* If we didn't error out, now NAME contains the name of the
178 variable. */
179
180 var = varobj_get_handle (name);
181
182 numdel = varobj_delete (var, children_only_p);
183
184 uiout->field_signed ("ndeleted", numdel);
185 }
186
187 /* Parse a string argument into a format value. */
188
189 static enum varobj_display_formats
190 mi_parse_format (const char *arg)
191 {
192 if (arg != NULL)
193 {
194 int len;
195
196 len = strlen (arg);
197
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)
207 return FORMAT_OCTAL;
208 else if (strncmp (arg, "zero-hexadecimal", len) == 0)
209 return FORMAT_ZHEXADECIMAL;
210 }
211
212 error (_("Must specify the format as: \"natural\", "
213 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
214 }
215
216 void
217 mi_cmd_var_set_format (const char *command, char **argv, int argc)
218 {
219 enum varobj_display_formats format;
220 struct varobj *var;
221 struct ui_out *uiout = current_uiout;
222
223 if (argc != 2)
224 error (_("-var-set-format: Usage: NAME FORMAT."));
225
226 /* Get varobj handle, if a valid var obj name was specified. */
227 var = varobj_get_handle (argv[0]);
228
229 format = mi_parse_format (argv[1]);
230
231 /* Set the format of VAR to the given format. */
232 varobj_set_display_format (var, format);
233
234 /* Report the new current format. */
235 uiout->field_string ("format", varobj_format_string[(int) format]);
236
237 /* Report the value in the new format. */
238 std::string val = varobj_get_value (var);
239 uiout->field_string ("value", val.c_str ());
240 }
241
242 void
243 mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
244 {
245 struct varobj *var;
246
247 if (argc != 2)
248 error (_("Usage: NAME VISUALIZER_FUNCTION."));
249
250 var = varobj_get_handle (argv[0]);
251
252 if (var == NULL)
253 error (_("Variable object not found"));
254
255 varobj_set_visualizer (var, argv[1]);
256 }
257
258 void
259 mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
260 {
261 struct varobj *var;
262 bool frozen;
263
264 if (argc != 2)
265 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
266
267 var = varobj_get_handle (argv[0]);
268
269 if (strcmp (argv[1], "0") == 0)
270 frozen = false;
271 else if (strcmp (argv[1], "1") == 0)
272 frozen = true;
273 else
274 error (_("Invalid flag value"));
275
276 varobj_set_frozen (var, frozen);
277
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. */
281 }
282
283 void
284 mi_cmd_var_show_format (const char *command, char **argv, int argc)
285 {
286 struct ui_out *uiout = current_uiout;
287 enum varobj_display_formats format;
288 struct varobj *var;
289
290 if (argc != 1)
291 error (_("-var-show-format: Usage: NAME."));
292
293 /* Get varobj handle, if a valid var obj name was specified. */
294 var = varobj_get_handle (argv[0]);
295
296 format = varobj_get_display_format (var);
297
298 /* Report the current format. */
299 uiout->field_string ("format", varobj_format_string[(int) format]);
300 }
301
302 void
303 mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
304 {
305 struct ui_out *uiout = current_uiout;
306 struct varobj *var;
307
308 if (argc != 1)
309 error (_("-var-info-num-children: Usage: NAME."));
310
311 /* Get varobj handle, if a valid var obj name was specified. */
312 var = varobj_get_handle (argv[0]);
313
314 uiout->field_signed ("numchild", varobj_get_num_children (var));
315 }
316
317 /* Return 1 if given the argument PRINT_VALUES we should display
318 the varobj VAR. */
319
320 static int
321 mi_print_value_p (struct varobj *var, enum print_values print_values)
322 {
323 struct type *type;
324
325 if (print_values == PRINT_NO_VALUES)
326 return 0;
327
328 if (print_values == PRINT_ALL_VALUES)
329 return 1;
330
331 if (varobj_is_dynamic_p (var))
332 return 1;
333
334 type = varobj_get_gdb_type (var);
335 if (type == NULL)
336 return 1;
337 else
338 {
339 type = check_typedef (type);
340
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);
346 }
347 }
348
349 void
350 mi_cmd_var_list_children (const char *command, char **argv, int argc)
351 {
352 struct ui_out *uiout = current_uiout;
353 struct varobj *var;
354 enum print_values print_values;
355 int from, to;
356
357 if (argc < 1 || argc > 4)
358 error (_("-var-list-children: Usage: "
359 "[PRINT_VALUES] NAME [FROM TO]"));
360
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]);
364 else
365 var = varobj_get_handle (argv[1]);
366
367 if (argc > 2)
368 {
369 from = atoi (argv[argc - 2]);
370 to = atoi (argv[argc - 1]);
371 }
372 else
373 {
374 from = -1;
375 to = -1;
376 }
377
378 const std::vector<varobj *> &children
379 = varobj_list_children (var, &from, &to);
380
381 uiout->field_signed ("numchild", to - from);
382 if (argc == 2 || argc == 4)
383 print_values = mi_parse_print_values (argv[0]);
384 else
385 print_values = PRINT_NO_VALUES;
386
387 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
388 if (display_hint)
389 uiout->field_string ("displayhint", display_hint.get ());
390
391 if (from < to)
392 {
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;
397
398 if (mi_version (uiout) == 1)
399 tuple_emitter.emplace (uiout, "children");
400 else
401 list_emitter.emplace (uiout, "children");
402 for (int ix = from; ix < to && ix < children.size (); ix++)
403 {
404 ui_out_emit_tuple child_emitter (uiout, "child");
405
406 print_varobj (children[ix], print_values, 1 /* print expression */);
407 }
408 }
409
410 uiout->field_signed ("has_more", varobj_has_more (var, to));
411 }
412
413 void
414 mi_cmd_var_info_type (const char *command, char **argv, int argc)
415 {
416 struct ui_out *uiout = current_uiout;
417 struct varobj *var;
418
419 if (argc != 1)
420 error (_("-var-info-type: Usage: NAME."));
421
422 /* Get varobj handle, if a valid var obj name was specified. */
423 var = varobj_get_handle (argv[0]);
424
425 std::string type_name = varobj_get_type (var);
426 uiout->field_string ("type", type_name.c_str ());
427 }
428
429 void
430 mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
431 {
432 struct ui_out *uiout = current_uiout;
433 struct varobj *var;
434
435 if (argc != 1)
436 error (_("Usage: NAME."));
437
438 /* Get varobj handle, if a valid var obj name was specified. */
439 var = varobj_get_handle (argv[0]);
440
441 const char *path_expr = varobj_get_path_expr (var);
442
443 uiout->field_string ("path_expr", path_expr);
444 }
445
446 void
447 mi_cmd_var_info_expression (const char *command, char **argv, int argc)
448 {
449 struct ui_out *uiout = current_uiout;
450 const struct language_defn *lang;
451 struct varobj *var;
452
453 if (argc != 1)
454 error (_("-var-info-expression: Usage: NAME."));
455
456 /* Get varobj handle, if a valid var obj name was specified. */
457 var = varobj_get_handle (argv[0]);
458
459 lang = varobj_get_language (var);
460
461 uiout->field_string ("lang", lang->la_natural_name);
462
463 std::string exp = varobj_get_expression (var);
464 uiout->field_string ("exp", exp.c_str ());
465 }
466
467 void
468 mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
469 {
470 struct ui_out *uiout = current_uiout;
471 int attr;
472 const char *attstr;
473 struct varobj *var;
474
475 if (argc != 1)
476 error (_("-var-show-attributes: Usage: NAME."));
477
478 /* Get varobj handle, if a valid var obj name was specified */
479 var = varobj_get_handle (argv[0]);
480
481 attr = varobj_get_attributes (var);
482 /* FIXME: define masks for attributes */
483 if (attr & 0x00000001)
484 attstr = "editable";
485 else
486 attstr = "noneditable";
487
488 uiout->field_string ("attr", attstr);
489 }
490
491 void
492 mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
493 {
494 struct ui_out *uiout = current_uiout;
495 struct varobj *var;
496
497 enum varobj_display_formats format;
498 int formatFound;
499 int oind;
500 char *oarg;
501
502 enum opt
503 {
504 OP_FORMAT
505 };
506 static const struct mi_opt opts[] =
507 {
508 {"f", OP_FORMAT, 1},
509 { 0, 0, 0 }
510 };
511
512 /* Parse arguments. */
513 format = FORMAT_NATURAL;
514 formatFound = 0;
515 oind = 0;
516 while (1)
517 {
518 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
519 opts, &oind, &oarg);
520
521 if (opt < 0)
522 break;
523 switch ((enum opt) opt)
524 {
525 case OP_FORMAT:
526 if (formatFound)
527 error (_("Cannot specify format more than once"));
528
529 format = mi_parse_format (oarg);
530 formatFound = 1;
531 break;
532 }
533 }
534
535 if (oind >= argc)
536 error (_("Usage: [-f FORMAT] NAME"));
537
538 if (oind < argc - 1)
539 error (_("Garbage at end of command"));
540
541 /* Get varobj handle, if a valid var obj name was specified. */
542 var = varobj_get_handle (argv[oind]);
543
544 if (formatFound)
545 {
546 std::string val = varobj_get_formatted_value (var, format);
547
548 uiout->field_string ("value", val.c_str ());
549 }
550 else
551 {
552 std::string val = varobj_get_value (var);
553
554 uiout->field_string ("value", val.c_str ());
555 }
556 }
557
558 void
559 mi_cmd_var_assign (const char *command, char **argv, int argc)
560 {
561 struct ui_out *uiout = current_uiout;
562 struct varobj *var;
563
564 if (argc != 2)
565 error (_("-var-assign: Usage: NAME EXPRESSION."));
566
567 /* Get varobj handle, if a valid var obj name was specified. */
568 var = varobj_get_handle (argv[0]);
569
570 if (!varobj_editable_p (var))
571 error (_("-var-assign: Variable object is not editable"));
572
573 const char *expression = argv[1];
574
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);
579
580 if (!varobj_set_value (var, expression))
581 error (_("-var-assign: Could not assign "
582 "expression to variable object"));
583
584 std::string val = varobj_get_value (var);
585 uiout->field_string ("value", val.c_str ());
586 }
587
588 /* Type used for parameters passing to mi_cmd_var_update_iter. */
589
590 struct mi_cmd_var_update
591 {
592 int only_floating;
593 enum print_values print_values;
594 };
595
596 /* Helper for mi_cmd_var_update - update each VAR. */
597
598 static void
599 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
600 {
601 struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
602 bool thread_stopped;
603
604 int thread_id = varobj_get_thread_id (var);
605
606 if (thread_id == -1)
607 {
608 thread_stopped = (inferior_ptid == null_ptid
609 || inferior_thread ()->state == THREAD_STOPPED);
610 }
611 else
612 {
613 thread_info *tp = find_thread_global_id (thread_id);
614
615 thread_stopped = (tp == NULL
616 || tp->state == THREAD_STOPPED);
617 }
618
619 if (thread_stopped
620 && (!data->only_floating || varobj_floating_p (var)))
621 varobj_update_one (var, data->print_values, false /* implicit */);
622 }
623
624 void
625 mi_cmd_var_update (const char *command, char **argv, int argc)
626 {
627 struct ui_out *uiout = current_uiout;
628 char *name;
629 enum print_values print_values;
630
631 if (argc != 1 && argc != 2)
632 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
633
634 if (argc == 1)
635 name = argv[0];
636 else
637 name = argv[1];
638
639 if (argc == 2)
640 print_values = mi_parse_print_values (argv[0]);
641 else
642 print_values = PRINT_NO_VALUES;
643
644 /* For historical reasons this might emit a list or a tuple, so we
645 construct one or the other. */
646 gdb::optional<ui_out_emit_tuple> tuple_emitter;
647 gdb::optional<ui_out_emit_list> list_emitter;
648
649 if (mi_version (uiout) <= 1)
650 tuple_emitter.emplace (uiout, "changelist");
651 else
652 list_emitter.emplace (uiout, "changelist");
653
654 /* Check if the parameter is a "*", which means that we want to
655 update all variables. */
656
657 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
658 {
659 struct mi_cmd_var_update data;
660
661 data.only_floating = (*name == '@');
662 data.print_values = print_values;
663
664 /* varobj_update_one automatically updates all the children of
665 VAROBJ. Therefore update each VAROBJ only once by iterating
666 only the root VAROBJs. */
667
668 all_root_varobjs (mi_cmd_var_update_iter, &data);
669 }
670 else
671 {
672 /* Get varobj handle, if a valid var obj name was specified. */
673 struct varobj *var = varobj_get_handle (name);
674
675 varobj_update_one (var, print_values, true /* explicit */);
676 }
677 }
678
679 /* Helper for mi_cmd_var_update(). */
680
681 static void
682 varobj_update_one (struct varobj *var, enum print_values print_values,
683 bool is_explicit)
684 {
685 struct ui_out *uiout = current_uiout;
686
687 std::vector<varobj_update_result> changes = varobj_update (&var, is_explicit);
688
689 for (const varobj_update_result &r : changes)
690 {
691 int from, to;
692
693 gdb::optional<ui_out_emit_tuple> tuple_emitter;
694 if (mi_version (uiout) > 1)
695 tuple_emitter.emplace (uiout, nullptr);
696 uiout->field_string ("name", varobj_get_objname (r.varobj));
697
698 switch (r.status)
699 {
700 case VAROBJ_IN_SCOPE:
701 if (mi_print_value_p (r.varobj, print_values))
702 {
703 std::string val = varobj_get_value (r.varobj);
704
705 uiout->field_string ("value", val.c_str ());
706 }
707 uiout->field_string ("in_scope", "true");
708 break;
709 case VAROBJ_NOT_IN_SCOPE:
710 uiout->field_string ("in_scope", "false");
711 break;
712 case VAROBJ_INVALID:
713 uiout->field_string ("in_scope", "invalid");
714 break;
715 }
716
717 if (r.status != VAROBJ_INVALID)
718 {
719 if (r.type_changed)
720 uiout->field_string ("type_changed", "true");
721 else
722 uiout->field_string ("type_changed", "false");
723 }
724
725 if (r.type_changed)
726 {
727 std::string type_name = varobj_get_type (r.varobj);
728
729 uiout->field_string ("new_type", type_name.c_str ());
730 }
731
732 if (r.type_changed || r.children_changed)
733 uiout->field_signed ("new_num_children",
734 varobj_get_num_children (r.varobj));
735
736 gdb::unique_xmalloc_ptr<char> display_hint
737 = varobj_get_display_hint (r.varobj);
738 if (display_hint)
739 uiout->field_string ("displayhint", display_hint.get ());
740
741 if (varobj_is_dynamic_p (r.varobj))
742 uiout->field_signed ("dynamic", 1);
743
744 varobj_get_child_range (r.varobj, &from, &to);
745 uiout->field_signed ("has_more", varobj_has_more (r.varobj, to));
746
747 if (!r.newobj.empty ())
748 {
749 ui_out_emit_list list_emitter (uiout, "new_children");
750
751 for (varobj *child : r.newobj)
752 {
753 ui_out_emit_tuple inner_tuple_emitter (uiout, NULL);
754 print_varobj (child, print_values, 1 /* print_expression */);
755 }
756 }
757 }
758 }
759
760 void
761 mi_cmd_enable_pretty_printing (const char *command, char **argv, int argc)
762 {
763 if (argc != 0)
764 error (_("-enable-pretty-printing: no arguments allowed"));
765
766 varobj_enable_pretty_printing ();
767 }
768
769 void
770 mi_cmd_var_set_update_range (const char *command, char **argv, int argc)
771 {
772 struct varobj *var;
773 int from, to;
774
775 if (argc != 3)
776 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
777
778 var = varobj_get_handle (argv[0]);
779 from = atoi (argv[1]);
780 to = atoi (argv[2]);
781
782 varobj_set_child_range (var, from, to);
783 }
This page took 0.059451 seconds and 4 git commands to generate.