Make MI commands const-correct
[binutils-gdb.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2 Copyright (C) 2000-2023 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);
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);
68 }
69
70 std::string type = varobj_get_type (var);
71 if (!type.empty ())
72 uiout->field_string ("type", type);
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, const char *const *argv, int argc)
93 {
94 struct ui_out *uiout = current_uiout;
95 CORE_ADDR frameaddr = 0;
96 struct varobj *var;
97 const char *frame;
98 const 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 gdb_printf (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, const char *const *argv, int argc)
144 {
145 const 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, const char *const *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);
240 }
241
242 void
243 mi_cmd_var_set_visualizer (const char *command, const char *const *argv,
244 int argc)
245 {
246 struct varobj *var;
247
248 if (argc != 2)
249 error (_("Usage: NAME VISUALIZER_FUNCTION."));
250
251 var = varobj_get_handle (argv[0]);
252
253 if (var == NULL)
254 error (_("Variable object not found"));
255
256 varobj_set_visualizer (var, argv[1]);
257 }
258
259 void
260 mi_cmd_var_set_frozen (const char *command, const char *const *argv, int argc)
261 {
262 struct varobj *var;
263 bool frozen;
264
265 if (argc != 2)
266 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
267
268 var = varobj_get_handle (argv[0]);
269
270 if (strcmp (argv[1], "0") == 0)
271 frozen = false;
272 else if (strcmp (argv[1], "1") == 0)
273 frozen = true;
274 else
275 error (_("Invalid flag value"));
276
277 varobj_set_frozen (var, frozen);
278
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. */
282 }
283
284 void
285 mi_cmd_var_show_format (const char *command, const char *const *argv, int argc)
286 {
287 struct ui_out *uiout = current_uiout;
288 enum varobj_display_formats format;
289 struct varobj *var;
290
291 if (argc != 1)
292 error (_("-var-show-format: Usage: NAME."));
293
294 /* Get varobj handle, if a valid var obj name was specified. */
295 var = varobj_get_handle (argv[0]);
296
297 format = varobj_get_display_format (var);
298
299 /* Report the current format. */
300 uiout->field_string ("format", varobj_format_string[(int) format]);
301 }
302
303 void
304 mi_cmd_var_info_num_children (const char *command, const char *const *argv,
305 int argc)
306 {
307 struct ui_out *uiout = current_uiout;
308 struct varobj *var;
309
310 if (argc != 1)
311 error (_("-var-info-num-children: Usage: NAME."));
312
313 /* Get varobj handle, if a valid var obj name was specified. */
314 var = varobj_get_handle (argv[0]);
315
316 uiout->field_signed ("numchild", varobj_get_num_children (var));
317 }
318
319 /* Return 1 if given the argument PRINT_VALUES we should display
320 the varobj VAR. */
321
322 static int
323 mi_print_value_p (struct varobj *var, enum print_values print_values)
324 {
325 struct type *type;
326
327 if (print_values == PRINT_NO_VALUES)
328 return 0;
329
330 if (print_values == PRINT_ALL_VALUES)
331 return 1;
332
333 if (varobj_is_dynamic_p (var))
334 return 1;
335
336 type = varobj_get_gdb_type (var);
337 if (type == NULL)
338 return 1;
339 else
340 return mi_simple_type_p (type);
341 }
342
343 /* See mi-cmds.h. */
344
345 bool
346 mi_simple_type_p (struct type *type)
347 {
348 type = check_typedef (type);
349
350 if (TYPE_IS_REFERENCE (type))
351 type = check_typedef (type->target_type ());
352
353 switch (type->code ())
354 {
355 case TYPE_CODE_ARRAY:
356 case TYPE_CODE_STRUCT:
357 case TYPE_CODE_UNION:
358 return false;
359 default:
360 return true;
361 }
362 }
363
364 void
365 mi_cmd_var_list_children (const char *command, const char *const *argv,
366 int argc)
367 {
368 struct ui_out *uiout = current_uiout;
369 struct varobj *var;
370 enum print_values print_values;
371 int from, to;
372
373 if (argc < 1 || argc > 4)
374 error (_("-var-list-children: Usage: "
375 "[PRINT_VALUES] NAME [FROM TO]"));
376
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]);
380 else
381 var = varobj_get_handle (argv[1]);
382
383 if (argc > 2)
384 {
385 from = atoi (argv[argc - 2]);
386 to = atoi (argv[argc - 1]);
387 }
388 else
389 {
390 from = -1;
391 to = -1;
392 }
393
394 const std::vector<varobj *> &children
395 = varobj_list_children (var, &from, &to);
396
397 uiout->field_signed ("numchild", to - from);
398 if (argc == 2 || argc == 4)
399 print_values = mi_parse_print_values (argv[0]);
400 else
401 print_values = PRINT_NO_VALUES;
402
403 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
404 if (display_hint)
405 uiout->field_string ("displayhint", display_hint.get ());
406
407 if (from < to)
408 {
409 ui_out_emit_list list_emitter (uiout, "children");
410 for (int ix = from; ix < to && ix < children.size (); ix++)
411 {
412 ui_out_emit_tuple child_emitter (uiout, "child");
413
414 print_varobj (children[ix], print_values, 1 /* print expression */);
415 }
416 }
417
418 uiout->field_signed ("has_more", varobj_has_more (var, to));
419 }
420
421 void
422 mi_cmd_var_info_type (const char *command, const char *const *argv, int argc)
423 {
424 struct ui_out *uiout = current_uiout;
425 struct varobj *var;
426
427 if (argc != 1)
428 error (_("-var-info-type: Usage: NAME."));
429
430 /* Get varobj handle, if a valid var obj name was specified. */
431 var = varobj_get_handle (argv[0]);
432
433 std::string type_name = varobj_get_type (var);
434 uiout->field_string ("type", type_name);
435 }
436
437 void
438 mi_cmd_var_info_path_expression (const char *command, const char *const *argv,
439 int argc)
440 {
441 struct ui_out *uiout = current_uiout;
442 struct varobj *var;
443
444 if (argc != 1)
445 error (_("Usage: NAME."));
446
447 /* Get varobj handle, if a valid var obj name was specified. */
448 var = varobj_get_handle (argv[0]);
449
450 const char *path_expr = varobj_get_path_expr (var);
451
452 uiout->field_string ("path_expr", path_expr);
453 }
454
455 void
456 mi_cmd_var_info_expression (const char *command, const char *const *argv,
457 int argc)
458 {
459 struct ui_out *uiout = current_uiout;
460 const struct language_defn *lang;
461 struct varobj *var;
462
463 if (argc != 1)
464 error (_("-var-info-expression: Usage: NAME."));
465
466 /* Get varobj handle, if a valid var obj name was specified. */
467 var = varobj_get_handle (argv[0]);
468
469 lang = varobj_get_language (var);
470
471 uiout->field_string ("lang", lang->natural_name ());
472
473 std::string exp = varobj_get_expression (var);
474 uiout->field_string ("exp", exp);
475 }
476
477 void
478 mi_cmd_var_show_attributes (const char *command, const char *const *argv,
479 int argc)
480 {
481 struct ui_out *uiout = current_uiout;
482 int attr;
483 const char *attstr;
484 struct varobj *var;
485
486 if (argc != 1)
487 error (_("-var-show-attributes: Usage: NAME."));
488
489 /* Get varobj handle, if a valid var obj name was specified */
490 var = varobj_get_handle (argv[0]);
491
492 attr = varobj_get_attributes (var);
493 /* FIXME: define masks for attributes */
494 if (attr & 0x00000001)
495 attstr = "editable";
496 else
497 attstr = "noneditable";
498
499 uiout->field_string ("attr", attstr);
500 }
501
502 void
503 mi_cmd_var_evaluate_expression (const char *command, const char *const *argv,
504 int argc)
505 {
506 struct ui_out *uiout = current_uiout;
507 struct varobj *var;
508
509 enum varobj_display_formats format;
510 int formatFound;
511 int oind;
512 const char *oarg;
513
514 enum opt
515 {
516 OP_FORMAT
517 };
518 static const struct mi_opt opts[] =
519 {
520 {"f", OP_FORMAT, 1},
521 { 0, 0, 0 }
522 };
523
524 /* Parse arguments. */
525 format = FORMAT_NATURAL;
526 formatFound = 0;
527 oind = 0;
528 while (1)
529 {
530 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
531 opts, &oind, &oarg);
532
533 if (opt < 0)
534 break;
535 switch ((enum opt) opt)
536 {
537 case OP_FORMAT:
538 if (formatFound)
539 error (_("Cannot specify format more than once"));
540
541 format = mi_parse_format (oarg);
542 formatFound = 1;
543 break;
544 }
545 }
546
547 if (oind >= argc)
548 error (_("Usage: [-f FORMAT] NAME"));
549
550 if (oind < argc - 1)
551 error (_("Garbage at end of command"));
552
553 /* Get varobj handle, if a valid var obj name was specified. */
554 var = varobj_get_handle (argv[oind]);
555
556 if (formatFound)
557 {
558 std::string val = varobj_get_formatted_value (var, format);
559
560 uiout->field_string ("value", val);
561 }
562 else
563 {
564 std::string val = varobj_get_value (var);
565
566 uiout->field_string ("value", val);
567 }
568 }
569
570 void
571 mi_cmd_var_assign (const char *command, const char *const *argv, int argc)
572 {
573 struct ui_out *uiout = current_uiout;
574 struct varobj *var;
575
576 if (argc != 2)
577 error (_("-var-assign: Usage: NAME EXPRESSION."));
578
579 /* Get varobj handle, if a valid var obj name was specified. */
580 var = varobj_get_handle (argv[0]);
581
582 if (!varobj_editable_p (var))
583 error (_("-var-assign: Variable object is not editable"));
584
585 const char *expression = argv[1];
586
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);
591
592 if (!varobj_set_value (var, expression))
593 error (_("-var-assign: Could not assign "
594 "expression to variable object"));
595
596 std::string val = varobj_get_value (var);
597 uiout->field_string ("value", val);
598 }
599
600 /* Helper for mi_cmd_var_update - update each VAR. */
601
602 static void
603 mi_cmd_var_update_iter (struct varobj *var, bool only_floating,
604 enum print_values print_values)
605 {
606 bool thread_stopped;
607
608 int thread_id = varobj_get_thread_id (var);
609
610 if (thread_id == -1)
611 {
612 thread_stopped = (inferior_ptid == null_ptid
613 || inferior_thread ()->state == THREAD_STOPPED);
614 }
615 else
616 {
617 thread_info *tp = find_thread_global_id (thread_id);
618
619 thread_stopped = (tp == NULL
620 || tp->state == THREAD_STOPPED);
621 }
622
623 if (thread_stopped
624 && (!only_floating || varobj_floating_p (var)))
625 varobj_update_one (var, print_values, false /* implicit */);
626 }
627
628 void
629 mi_cmd_var_update (const char *command, const char *const *argv, int argc)
630 {
631 struct ui_out *uiout = current_uiout;
632 const char *name;
633 enum print_values print_values;
634
635 if (argc != 1 && argc != 2)
636 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
637
638 if (argc == 1)
639 name = argv[0];
640 else
641 name = argv[1];
642
643 if (argc == 2)
644 print_values = mi_parse_print_values (argv[0]);
645 else
646 print_values = PRINT_NO_VALUES;
647
648 ui_out_emit_list list_emitter (uiout, "changelist");
649
650 /* Check if the parameter is a "*", which means that we want to
651 update all variables. */
652
653 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
654 {
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. */
658
659 all_root_varobjs ([=] (varobj *var)
660 { mi_cmd_var_update_iter (var, *name == '0', print_values); });
661 }
662 else
663 {
664 /* Get varobj handle, if a valid var obj name was specified. */
665 struct varobj *var = varobj_get_handle (name);
666
667 varobj_update_one (var, print_values, true /* explicit */);
668 }
669 }
670
671 /* Helper for mi_cmd_var_update(). */
672
673 static void
674 varobj_update_one (struct varobj *var, enum print_values print_values,
675 bool is_explicit)
676 {
677 struct ui_out *uiout = current_uiout;
678
679 std::vector<varobj_update_result> changes = varobj_update (&var, is_explicit);
680
681 for (const varobj_update_result &r : changes)
682 {
683 int from, to;
684
685 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
686 uiout->field_string ("name", varobj_get_objname (r.varobj));
687
688 switch (r.status)
689 {
690 case VAROBJ_IN_SCOPE:
691 if (mi_print_value_p (r.varobj, print_values))
692 {
693 std::string val = varobj_get_value (r.varobj);
694
695 uiout->field_string ("value", val);
696 }
697 uiout->field_string ("in_scope", "true");
698 break;
699 case VAROBJ_NOT_IN_SCOPE:
700 uiout->field_string ("in_scope", "false");
701 break;
702 case VAROBJ_INVALID:
703 uiout->field_string ("in_scope", "invalid");
704 break;
705 }
706
707 if (r.status != VAROBJ_INVALID)
708 {
709 if (r.type_changed)
710 uiout->field_string ("type_changed", "true");
711 else
712 uiout->field_string ("type_changed", "false");
713 }
714
715 if (r.type_changed)
716 {
717 std::string type_name = varobj_get_type (r.varobj);
718
719 uiout->field_string ("new_type", type_name);
720 }
721
722 if (r.type_changed || r.children_changed)
723 uiout->field_signed ("new_num_children",
724 varobj_get_num_children (r.varobj));
725
726 gdb::unique_xmalloc_ptr<char> display_hint
727 = varobj_get_display_hint (r.varobj);
728 if (display_hint)
729 uiout->field_string ("displayhint", display_hint.get ());
730
731 if (varobj_is_dynamic_p (r.varobj))
732 uiout->field_signed ("dynamic", 1);
733
734 varobj_get_child_range (r.varobj, &from, &to);
735 uiout->field_signed ("has_more", varobj_has_more (r.varobj, to));
736
737 if (!r.newobj.empty ())
738 {
739 ui_out_emit_list list_emitter (uiout, "new_children");
740
741 for (varobj *child : r.newobj)
742 {
743 ui_out_emit_tuple inner_tuple_emitter (uiout, NULL);
744 print_varobj (child, print_values, 1 /* print_expression */);
745 }
746 }
747 }
748 }
749
750 void
751 mi_cmd_enable_pretty_printing (const char *command, const char *const *argv,
752 int argc)
753 {
754 if (argc != 0)
755 error (_("-enable-pretty-printing: no arguments allowed"));
756
757 varobj_enable_pretty_printing ();
758 }
759
760 void
761 mi_cmd_var_set_update_range (const char *command, const char *const *argv,
762 int argc)
763 {
764 struct varobj *var;
765 int from, to;
766
767 if (argc != 3)
768 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
769
770 var = varobj_get_handle (argv[0]);
771 from = atoi (argv[1]);
772 to = atoi (argv[2]);
773
774 varobj_set_child_range (var, from, to);
775 }