arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
blob5e504283fcfc17dbcd6246143c30fd451cb5a0c0
1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "event-top.h"
21 #include "target.h"
22 #include "frame.h"
23 #include "value.h"
24 #include "mi-cmds.h"
25 #include "ui-out.h"
26 #include "symtab.h"
27 #include "block.h"
28 #include "stack.h"
29 #include "dictionary.h"
30 #include "language.h"
31 #include "valprint.h"
32 #include "utils.h"
33 #include "mi-getopt.h"
34 #include "extension.h"
35 #include <ctype.h>
36 #include "mi-parse.h"
37 #include <optional>
38 #include "gdbsupport/gdb-safe-ctype.h"
39 #include "inferior.h"
40 #include "observable.h"
42 enum what_to_list { locals, arguments, all };
44 static void list_args_or_locals (const frame_print_options &fp_opts,
45 enum what_to_list what,
46 enum print_values values,
47 const frame_info_ptr &fi,
48 int skip_unavailable);
50 /* True if we want to allow Python-based frame filters. */
51 static int frame_filters = 0;
53 void
54 mi_cmd_enable_frame_filters (const char *command, const char *const *argv,
55 int argc)
57 if (argc != 0)
58 error (_("-enable-frame-filters: no arguments allowed"));
59 frame_filters = 1;
62 /* Like apply_ext_lang_frame_filter, but take a print_values */
64 static enum ext_lang_bt_status
65 mi_apply_ext_lang_frame_filter (const frame_info_ptr &frame,
66 frame_filter_flags flags,
67 enum print_values print_values,
68 struct ui_out *out,
69 int frame_low, int frame_high)
71 /* ext_lang_frame_args's MI options are compatible with MI print
72 values. */
73 return apply_ext_lang_frame_filter (frame, flags,
74 (enum ext_lang_frame_args) print_values,
75 out,
76 frame_low, frame_high);
79 /* Print a list of the stack frames. Args can be none, in which case
80 we want to print the whole backtrace, or a pair of numbers
81 specifying the frame numbers at which to start and stop the
82 display. If the two numbers are equal, a single frame will be
83 displayed. */
85 void
86 mi_cmd_stack_list_frames (const char *command, const char *const *argv,
87 int argc)
89 int frame_low;
90 int frame_high;
91 int i;
92 frame_info_ptr fi;
93 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
94 int raw_arg = 0;
95 int oind = 0;
96 enum opt
98 NO_FRAME_FILTERS
100 static const struct mi_opt opts[] =
102 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
103 { 0, 0, 0 }
106 /* Parse arguments. In this instance we are just looking for
107 --no-frame-filters. */
108 while (1)
110 const char *oarg;
111 int opt = mi_getopt ("-stack-list-frames", argc, argv,
112 opts, &oind, &oarg);
113 if (opt < 0)
114 break;
115 switch ((enum opt) opt)
117 case NO_FRAME_FILTERS:
118 raw_arg = oind;
119 break;
123 /* After the last option is parsed, there should either be low -
124 high range, or no further arguments. */
125 if ((argc - oind != 0) && (argc - oind != 2))
126 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
128 /* If there is a range, set it. */
129 if (argc - oind == 2)
131 frame_low = atoi (argv[0 + oind]);
132 frame_high = atoi (argv[1 + oind]);
134 else
136 /* Called with no arguments, it means we want the whole
137 backtrace. */
138 frame_low = -1;
139 frame_high = -1;
142 /* Let's position fi on the frame at which to start the
143 display. Could be the innermost frame if the whole stack needs
144 displaying, or if frame_low is 0. */
145 for (i = 0, fi = get_current_frame ();
146 fi && i < frame_low;
147 i++, fi = get_prev_frame (fi));
149 if (fi == NULL)
150 error (_("-stack-list-frames: Not enough frames in stack."));
152 ui_out_emit_list list_emitter (current_uiout, "stack");
154 if (! raw_arg && frame_filters)
156 frame_filter_flags flags = PRINT_LEVEL | PRINT_FRAME_INFO;
157 int py_frame_low = frame_low;
159 /* We cannot pass -1 to frame_low, as that would signify a
160 relative backtrace from the tail of the stack. So, in the case
161 of frame_low == -1, assign and increment it. */
162 if (py_frame_low == -1)
163 py_frame_low++;
165 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
166 NO_VALUES, current_uiout,
167 py_frame_low, frame_high);
170 /* Run the inbuilt backtrace if there are no filters registered, or
171 if "--no-frame-filters" has been specified from the command. */
172 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
174 /* Now let's print the frames up to frame_high, or until there are
175 frames in the stack. */
176 for (;
177 fi && (i <= frame_high || frame_high == -1);
178 i++, fi = get_prev_frame (fi))
180 QUIT;
181 /* Print the location and the address always, even for level 0.
182 If args is 0, don't print the arguments. */
183 print_frame_info (user_frame_print_options,
184 fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
189 void
190 mi_cmd_stack_info_depth (const char *command, const char *const *argv,
191 int argc)
193 int frame_high;
194 int i;
195 frame_info_ptr fi;
197 if (argc > 1)
198 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
200 if (argc == 1)
201 frame_high = atoi (argv[0]);
202 else
203 /* Called with no arguments, it means we want the real depth of
204 the stack. */
205 frame_high = -1;
207 for (i = 0, fi = get_current_frame ();
208 fi && (i < frame_high || frame_high == -1);
209 i++, fi = get_prev_frame (fi))
210 QUIT;
212 current_uiout->field_signed ("depth", i);
215 /* Print a list of the locals for the current frame. With argument of
216 0, print only the names, with argument of 1 print also the
217 values. */
219 void
220 mi_cmd_stack_list_locals (const char *command, const char *const *argv,
221 int argc)
223 frame_info_ptr frame;
224 int raw_arg = 0;
225 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
226 enum print_values print_value;
227 int oind = 0;
228 int skip_unavailable = 0;
230 if (argc > 1)
232 enum opt
234 NO_FRAME_FILTERS,
235 SKIP_UNAVAILABLE,
237 static const struct mi_opt opts[] =
239 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
240 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
241 { 0, 0, 0 }
244 while (1)
246 const char *oarg;
247 /* Don't parse 'print-values' as an option. */
248 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
249 opts, &oind, &oarg);
251 if (opt < 0)
252 break;
253 switch ((enum opt) opt)
255 case NO_FRAME_FILTERS:
256 raw_arg = oind;
257 break;
258 case SKIP_UNAVAILABLE:
259 skip_unavailable = 1;
260 break;
265 /* After the last option is parsed, there should be only
266 'print-values'. */
267 if (argc - oind != 1)
268 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
269 "[--skip-unavailable] PRINT_VALUES"));
271 frame = get_selected_frame (NULL);
272 print_value = mi_parse_print_values (argv[oind]);
274 if (! raw_arg && frame_filters)
276 frame_filter_flags flags = PRINT_LEVEL | PRINT_LOCALS;
278 result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
279 current_uiout, 0, 0);
282 /* Run the inbuilt backtrace if there are no filters registered, or
283 if "--no-frame-filters" has been specified from the command. */
284 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
286 list_args_or_locals (user_frame_print_options,
287 locals, print_value, frame,
288 skip_unavailable);
292 /* Print a list of the arguments for the current frame. With argument
293 of 0, print only the names, with argument of 1 print also the
294 values. */
296 void
297 mi_cmd_stack_list_args (const char *command, const char *const *argv, int argc)
299 int frame_low;
300 int frame_high;
301 int i;
302 frame_info_ptr fi;
303 enum print_values print_values;
304 struct ui_out *uiout = current_uiout;
305 int raw_arg = 0;
306 int oind = 0;
307 int skip_unavailable = 0;
308 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
309 enum opt
311 NO_FRAME_FILTERS,
312 SKIP_UNAVAILABLE,
314 static const struct mi_opt opts[] =
316 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
317 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
318 { 0, 0, 0 }
321 while (1)
323 const char *oarg;
324 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
325 opts, &oind, &oarg);
327 if (opt < 0)
328 break;
329 switch ((enum opt) opt)
331 case NO_FRAME_FILTERS:
332 raw_arg = oind;
333 break;
334 case SKIP_UNAVAILABLE:
335 skip_unavailable = 1;
336 break;
340 if (argc - oind != 1 && argc - oind != 3)
341 error (_("-stack-list-arguments: Usage: " \
342 "[--no-frame-filters] [--skip-unavailable] "
343 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
345 if (argc - oind == 3)
347 frame_low = atoi (argv[1 + oind]);
348 frame_high = atoi (argv[2 + oind]);
350 else
352 /* Called with no arguments, it means we want args for the whole
353 backtrace. */
354 frame_low = -1;
355 frame_high = -1;
358 print_values = mi_parse_print_values (argv[oind]);
360 /* Let's position fi on the frame at which to start the
361 display. Could be the innermost frame if the whole stack needs
362 displaying, or if frame_low is 0. */
363 for (i = 0, fi = get_current_frame ();
364 fi && i < frame_low;
365 i++, fi = get_prev_frame (fi));
367 if (fi == NULL)
368 error (_("-stack-list-arguments: Not enough frames in stack."));
370 ui_out_emit_list list_emitter (uiout, "stack-args");
372 if (! raw_arg && frame_filters)
374 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS;
375 if (user_frame_print_options.print_raw_frame_arguments)
376 flags |= PRINT_RAW_FRAME_ARGUMENTS;
377 int py_frame_low = frame_low;
379 /* We cannot pass -1 to frame_low, as that would signify a
380 relative backtrace from the tail of the stack. So, in the case
381 of frame_low == -1, assign and increment it. */
382 if (py_frame_low == -1)
383 py_frame_low++;
385 result = mi_apply_ext_lang_frame_filter (get_current_frame (), flags,
386 print_values, current_uiout,
387 py_frame_low, frame_high);
390 /* Run the inbuilt backtrace if there are no filters registered, or
391 if "--no-frame-filters" has been specified from the command. */
392 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
394 /* Now let's print the frames up to frame_high, or until there are
395 frames in the stack. */
396 for (;
397 fi && (i <= frame_high || frame_high == -1);
398 i++, fi = get_prev_frame (fi))
400 QUIT;
401 ui_out_emit_tuple tuple_emitter (uiout, "frame");
402 uiout->field_signed ("level", i);
403 list_args_or_locals (user_frame_print_options,
404 arguments, print_values, fi, skip_unavailable);
409 /* Print a list of the local variables (including arguments) for the
410 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
411 or both names and values of the variables must be printed. See
412 parse_print_value for possible values. */
414 void
415 mi_cmd_stack_list_variables (const char *command, const char *const *argv,
416 int argc)
418 frame_info_ptr frame;
419 int raw_arg = 0;
420 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
421 enum print_values print_value;
422 int oind = 0;
423 int skip_unavailable = 0;
425 if (argc > 1)
427 enum opt
429 NO_FRAME_FILTERS,
430 SKIP_UNAVAILABLE,
432 static const struct mi_opt opts[] =
434 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
435 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
436 { 0, 0, 0 }
439 while (1)
441 const char *oarg;
442 /* Don't parse 'print-values' as an option. */
443 int opt = mi_getopt ("-stack-list-variables", argc - 1,
444 argv, opts, &oind, &oarg);
445 if (opt < 0)
446 break;
447 switch ((enum opt) opt)
449 case NO_FRAME_FILTERS:
450 raw_arg = oind;
451 break;
452 case SKIP_UNAVAILABLE:
453 skip_unavailable = 1;
454 break;
459 /* After the last option is parsed, there should be only
460 'print-values'. */
461 if (argc - oind != 1)
462 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
463 "[--skip-unavailable] PRINT_VALUES"));
465 frame = get_selected_frame (NULL);
466 print_value = mi_parse_print_values (argv[oind]);
468 if (! raw_arg && frame_filters)
470 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
471 if (user_frame_print_options.print_raw_frame_arguments)
472 flags |= PRINT_RAW_FRAME_ARGUMENTS;
474 result = mi_apply_ext_lang_frame_filter (frame, flags,
475 print_value,
476 current_uiout, 0, 0);
479 /* Run the inbuilt backtrace if there are no filters registered, or
480 if "--no-frame-filters" has been specified from the command. */
481 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
483 list_args_or_locals (user_frame_print_options,
484 all, print_value, frame,
485 skip_unavailable);
489 /* Print single local or argument. ARG must be already read in. For
490 WHAT and VALUES see list_args_or_locals.
492 Errors are printed as if they would be the parameter value. Use
493 zeroed ARG iff it should not be printed according to VALUES. If
494 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
496 static void
497 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
498 enum print_values values, int skip_unavailable,
499 const frame_print_options &fp_opts)
501 struct ui_out *uiout = current_uiout;
503 gdb_assert (!arg->val || !arg->error);
504 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
505 && arg->error == NULL)
506 || values == PRINT_SIMPLE_VALUES
507 || (values == PRINT_ALL_VALUES
508 && (arg->val != NULL || arg->error != NULL)));
509 gdb_assert (arg->entry_kind == print_entry_values_no
510 || (arg->entry_kind == print_entry_values_only
511 && (arg->val || arg->error)));
513 if (skip_unavailable && arg->val != NULL
514 && (arg->val->entirely_unavailable ()
515 /* A scalar object that does not have all bits available is
516 also considered unavailable, because all bits contribute
517 to its representation. */
518 || (val_print_scalar_type_p (arg->val->type ())
519 && !arg->val->bytes_available (arg->val->embedded_offset (),
520 arg->val->type ()->length ()))))
521 return;
523 std::optional<ui_out_emit_tuple> tuple_emitter;
524 if (values != PRINT_NO_VALUES || what == all)
525 tuple_emitter.emplace (uiout, nullptr);
527 string_file stb;
529 stb.puts (arg->sym->print_name ());
530 if (arg->entry_kind == print_entry_values_only)
531 stb.puts ("@entry");
532 uiout->field_stream ("name", stb);
534 if (what == all && arg->sym->is_argument ())
535 uiout->field_signed ("arg", 1);
537 if (values == PRINT_SIMPLE_VALUES)
539 check_typedef (arg->sym->type ());
540 type_print (arg->sym->type (), "", &stb, -1);
541 uiout->field_stream ("type", stb);
544 if (arg->val || arg->error)
546 if (arg->error)
547 stb.printf (_("<error reading variable: %s>"), arg->error.get ());
548 else
552 struct value_print_options opts;
554 get_no_prettyformat_print_options (&opts);
555 opts.deref_ref = true;
556 if (arg->sym->is_argument ())
557 opts.raw = fp_opts.print_raw_frame_arguments;
558 common_val_print (arg->val, &stb, 0, &opts,
559 language_def (arg->sym->language ()));
561 catch (const gdb_exception_error &except)
563 stb.printf (_("<error reading variable: %s>"),
564 except.what ());
567 uiout->field_stream ("value", stb);
571 /* Print a list of the objects for the frame FI in a certain form,
572 which is determined by VALUES. The objects can be locals,
573 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
574 is true, only print the arguments or local variables whose values
575 are available. */
577 static void
578 list_args_or_locals (const frame_print_options &fp_opts,
579 enum what_to_list what, enum print_values values,
580 const frame_info_ptr &fi, int skip_unavailable)
582 const struct block *block;
583 const char *name_of_result;
584 struct ui_out *uiout = current_uiout;
586 block = get_frame_block (fi, 0);
588 switch (what)
590 case locals:
591 name_of_result = "locals";
592 break;
593 case arguments:
594 name_of_result = "args";
595 break;
596 case all:
597 name_of_result = "variables";
598 break;
599 default:
600 internal_error ("unexpected what_to_list: %d", (int) what);
603 ui_out_emit_list list_emitter (uiout, name_of_result);
605 while (block != 0)
607 for (struct symbol *sym : block_iterator_range (block))
609 int print_me = 0;
611 switch (sym->aclass ())
613 default:
614 case LOC_UNDEF: /* catches errors */
615 case LOC_CONST: /* constant */
616 case LOC_TYPEDEF: /* local typedef */
617 case LOC_LABEL: /* local label */
618 case LOC_BLOCK: /* local function */
619 case LOC_CONST_BYTES: /* loc. byte seq. */
620 case LOC_UNRESOLVED: /* unresolved static */
621 case LOC_OPTIMIZED_OUT: /* optimized out */
622 print_me = 0;
623 break;
625 case LOC_ARG: /* argument */
626 case LOC_REF_ARG: /* reference arg */
627 case LOC_REGPARM_ADDR: /* indirect register arg */
628 case LOC_LOCAL: /* stack local */
629 case LOC_STATIC: /* static */
630 case LOC_REGISTER: /* register */
631 case LOC_COMPUTED: /* computed location */
632 if (what == all)
633 print_me = 1;
634 else if (what == locals)
635 print_me = !sym->is_argument ();
636 else
637 print_me = sym->is_argument ();
638 break;
640 if (print_me)
642 struct symbol *sym2;
643 struct frame_arg arg, entryarg;
645 if (sym->is_argument ())
646 sym2 = (lookup_symbol_search_name
647 (sym->search_name (),
648 block, SEARCH_VAR_DOMAIN).symbol);
649 else
650 sym2 = sym;
651 gdb_assert (sym2 != NULL);
653 arg.sym = sym2;
654 arg.entry_kind = print_entry_values_no;
655 entryarg.sym = sym2;
656 entryarg.entry_kind = print_entry_values_no;
658 switch (values)
660 case PRINT_SIMPLE_VALUES:
661 if (!mi_simple_type_p (sym2->type ()))
662 break;
663 [[fallthrough]];
665 case PRINT_ALL_VALUES:
666 if (sym->is_argument ())
667 read_frame_arg (fp_opts, sym2, fi, &arg, &entryarg);
668 else
669 read_frame_local (sym2, fi, &arg);
670 break;
673 if (arg.entry_kind != print_entry_values_only)
674 list_arg_or_local (&arg, what, values, skip_unavailable,
675 fp_opts);
676 if (entryarg.entry_kind != print_entry_values_no)
677 list_arg_or_local (&entryarg, what, values, skip_unavailable,
678 fp_opts);
682 if (block->function ())
683 break;
684 else
685 block = block->superblock ();
689 /* Read a frame specification from FRAME_EXP and return the selected frame.
690 Call error() if the specification is in any way invalid (so this
691 function never returns NULL).
693 The frame specification is usually an integer level number, however if
694 the number does not match a valid frame level then it will be treated as
695 a frame address. The frame address will then be used to find a matching
696 frame in the stack. If no matching frame is found then a new frame will
697 be created.
699 The use of FRAME_EXP as an address is undocumented in the GDB user
700 manual, this feature is supported here purely for backward
701 compatibility. */
703 static frame_info_ptr
704 parse_frame_specification (const char *frame_exp)
706 gdb_assert (frame_exp != NULL);
708 /* NOTE: Parse and evaluate expression, but do not use
709 functions such as parse_and_eval_long or
710 parse_and_eval_address to also extract the value.
711 Instead value_as_long and value_as_address are used.
712 This avoids problems with expressions that contain
713 side-effects. */
714 struct value *arg = parse_and_eval (frame_exp);
716 /* Assume ARG is an integer, and try using that to select a frame. */
717 frame_info_ptr fid;
718 int level = value_as_long (arg);
720 fid = find_relative_frame (get_current_frame (), &level);
721 if (level == 0)
722 /* find_relative_frame was successful. */
723 return fid;
725 /* Convert the value into a corresponding address. */
726 CORE_ADDR addr = value_as_address (arg);
728 /* Assume that ADDR is an address, use that to identify a frame with a
729 matching ID. */
730 struct frame_id id = frame_id_build_wild (addr);
732 /* If (s)he specifies the frame with an address, he deserves
733 what (s)he gets. Still, give the highest one that matches.
734 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
735 know). */
736 for (fid = get_current_frame ();
737 fid != NULL;
738 fid = get_prev_frame (fid))
740 if (id == get_frame_id (fid))
742 frame_info_ptr prev_frame;
744 while (1)
746 prev_frame = get_prev_frame (fid);
747 if (!prev_frame
748 || id != get_frame_id (prev_frame))
749 break;
750 fid = prev_frame;
752 return fid;
756 /* We couldn't identify the frame as an existing frame, but
757 perhaps we can create one with a single argument. */
758 return create_new_frame (addr, 0);
761 /* Implement the -stack-select-frame MI command. */
763 void
764 mi_cmd_stack_select_frame (const char *command, const char *const *argv,
765 int argc)
767 if (argc == 0 || argc > 1)
768 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
769 select_frame (parse_frame_specification (argv[0]));
772 void
773 mi_cmd_stack_info_frame (const char *command, const char *const *argv,
774 int argc)
776 if (argc > 0)
777 error (_("-stack-info-frame: No arguments allowed"));
779 print_frame_info (user_frame_print_options,
780 get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);