]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-cmd-stack.c
Turn some value offset functions into method
[thirdparty/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
213516ef 2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
ab91fdd5 3 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
19
20#include "defs.h"
21#include "target.h"
22#include "frame.h"
23#include "value.h"
24#include "mi-cmds.h"
25#include "ui-out.h"
e88c90f2 26#include "symtab.h"
fe898f56 27#include "block.h"
b9362cc7 28#include "stack.h"
de4f826b 29#include "dictionary.h"
d8ca156b 30#include "language.h"
79a45b7d 31#include "valprint.h"
1e611234
PM
32#include "utils.h"
33#include "mi-getopt.h"
6dddc817 34#include "extension.h"
1e611234 35#include <ctype.h>
87967e27 36#include "mi-parse.h"
268a13a5 37#include "gdbsupport/gdb_optional.h"
f67ffa6a 38#include "safe-ctype.h"
a9c82bc1
JV
39#include "inferior.h"
40#include "observable.h"
daf3c977
VP
41
42enum what_to_list { locals, arguments, all };
43
d4c16835
PA
44static void list_args_or_locals (const frame_print_options &fp_opts,
45 enum what_to_list what,
bdaf8d4a 46 enum print_values values,
bd2b40ac 47 frame_info_ptr fi,
6211c335 48 int skip_unavailable);
fb40c209 49
1e611234
PM
50/* True if we want to allow Python-based frame filters. */
51static int frame_filters = 0;
52
53void
9f33b8b7 54mi_cmd_enable_frame_filters (const char *command, char **argv, int argc)
1e611234
PM
55{
56 if (argc != 0)
57 error (_("-enable-frame-filters: no arguments allowed"));
58 frame_filters = 1;
59}
60
10367c7c
PA
61/* Like apply_ext_lang_frame_filter, but take a print_values */
62
63static enum ext_lang_bt_status
bd2b40ac 64mi_apply_ext_lang_frame_filter (frame_info_ptr frame,
d4dd3282 65 frame_filter_flags flags,
10367c7c
PA
66 enum print_values print_values,
67 struct ui_out *out,
68 int frame_low, int frame_high)
69{
70 /* ext_lang_frame_args's MI options are compatible with MI print
71 values. */
72 return apply_ext_lang_frame_filter (frame, flags,
73 (enum ext_lang_frame_args) print_values,
74 out,
75 frame_low, frame_high);
76}
77
2b03b41d 78/* Print a list of the stack frames. Args can be none, in which case
fb40c209
AC
79 we want to print the whole backtrace, or a pair of numbers
80 specifying the frame numbers at which to start and stop the
2b03b41d
SS
81 display. If the two numbers are equal, a single frame will be
82 displayed. */
83
ce8f13f8 84void
9f33b8b7 85mi_cmd_stack_list_frames (const char *command, char **argv, int argc)
fb40c209
AC
86{
87 int frame_low;
88 int frame_high;
89 int i;
bd2b40ac 90 frame_info_ptr fi;
6dddc817 91 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
1e611234
PM
92 int raw_arg = 0;
93 int oind = 0;
94 enum opt
95 {
96 NO_FRAME_FILTERS
97 };
98 static const struct mi_opt opts[] =
99 {
100 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
101 { 0, 0, 0 }
102 };
103
104 /* Parse arguments. In this instance we are just looking for
105 --no-frame-filters. */
106 while (1)
107 {
108 char *oarg;
109 int opt = mi_getopt ("-stack-list-frames", argc, argv,
110 opts, &oind, &oarg);
111 if (opt < 0)
112 break;
113 switch ((enum opt) opt)
114 {
115 case NO_FRAME_FILTERS:
116 raw_arg = oind;
117 break;
118 }
119 }
fb40c209 120
1e611234
PM
121 /* After the last option is parsed, there should either be low -
122 high range, or no further arguments. */
123 if ((argc - oind != 0) && (argc - oind != 2))
124 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
fb40c209 125
1e611234
PM
126 /* If there is a range, set it. */
127 if (argc - oind == 2)
fb40c209 128 {
1e611234
PM
129 frame_low = atoi (argv[0 + oind]);
130 frame_high = atoi (argv[1 + oind]);
fb40c209
AC
131 }
132 else
133 {
134 /* Called with no arguments, it means we want the whole
dda83cd7 135 backtrace. */
fb40c209
AC
136 frame_low = -1;
137 frame_high = -1;
138 }
139
140 /* Let's position fi on the frame at which to start the
141 display. Could be the innermost frame if the whole stack needs
2b03b41d 142 displaying, or if frame_low is 0. */
fb40c209
AC
143 for (i = 0, fi = get_current_frame ();
144 fi && i < frame_low;
145 i++, fi = get_prev_frame (fi));
146
147 if (fi == NULL)
1b05df00 148 error (_("-stack-list-frames: Not enough frames in stack."));
fb40c209 149
10f489e5 150 ui_out_emit_list list_emitter (current_uiout, "stack");
fb40c209 151
1e611234 152 if (! raw_arg && frame_filters)
fb40c209 153 {
d4dd3282 154 frame_filter_flags flags = PRINT_LEVEL | PRINT_FRAME_INFO;
1e611234
PM
155 int py_frame_low = frame_low;
156
157 /* We cannot pass -1 to frame_low, as that would signify a
158 relative backtrace from the tail of the stack. So, in the case
159 of frame_low == -1, assign and increment it. */
160 if (py_frame_low == -1)
161 py_frame_low++;
162
6dddc817
DE
163 result = apply_ext_lang_frame_filter (get_current_frame (), flags,
164 NO_VALUES, current_uiout,
165 py_frame_low, frame_high);
1e611234
PM
166 }
167
168 /* Run the inbuilt backtrace if there are no filters registered, or
169 if "--no-frame-filters" has been specified from the command. */
6dddc817 170 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
1e611234
PM
171 {
172 /* Now let's print the frames up to frame_high, or until there are
173 frames in the stack. */
174 for (;
175 fi && (i <= frame_high || frame_high == -1);
176 i++, fi = get_prev_frame (fi))
177 {
178 QUIT;
179 /* Print the location and the address always, even for level 0.
180 If args is 0, don't print the arguments. */
d4c16835
PA
181 print_frame_info (user_frame_print_options,
182 fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
1e611234 183 }
fb40c209 184 }
fb40c209
AC
185}
186
ce8f13f8 187void
9f33b8b7 188mi_cmd_stack_info_depth (const char *command, char **argv, int argc)
fb40c209
AC
189{
190 int frame_high;
191 int i;
bd2b40ac 192 frame_info_ptr fi;
fb40c209 193
fb40c209 194 if (argc > 1)
1b05df00 195 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
196
197 if (argc == 1)
198 frame_high = atoi (argv[0]);
199 else
200 /* Called with no arguments, it means we want the real depth of
2b03b41d 201 the stack. */
fb40c209
AC
202 frame_high = -1;
203
204 for (i = 0, fi = get_current_frame ();
205 fi && (i < frame_high || frame_high == -1);
206 i++, fi = get_prev_frame (fi))
207 QUIT;
208
381befee 209 current_uiout->field_signed ("depth", i);
fb40c209
AC
210}
211
7a93fb82 212/* Print a list of the locals for the current frame. With argument of
fb40c209 213 0, print only the names, with argument of 1 print also the
2b03b41d
SS
214 values. */
215
ce8f13f8 216void
9f33b8b7 217mi_cmd_stack_list_locals (const char *command, char **argv, int argc)
fb40c209 218{
bd2b40ac 219 frame_info_ptr frame;
1e611234 220 int raw_arg = 0;
6dddc817 221 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
f486487f 222 enum print_values print_value;
645eab03 223 int oind = 0;
6211c335 224 int skip_unavailable = 0;
f5ec2042 225
645eab03
YQ
226 if (argc > 1)
227 {
645eab03
YQ
228 enum opt
229 {
6211c335
YQ
230 NO_FRAME_FILTERS,
231 SKIP_UNAVAILABLE,
645eab03
YQ
232 };
233 static const struct mi_opt opts[] =
234 {
235 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
6211c335 236 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
645eab03
YQ
237 { 0, 0, 0 }
238 };
239
240 while (1)
241 {
242 char *oarg;
243 /* Don't parse 'print-values' as an option. */
244 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv,
245 opts, &oind, &oarg);
246
247 if (opt < 0)
248 break;
249 switch ((enum opt) opt)
250 {
251 case NO_FRAME_FILTERS:
252 raw_arg = oind;
15c9ffd6 253 break;
6211c335
YQ
254 case SKIP_UNAVAILABLE:
255 skip_unavailable = 1;
645eab03
YQ
256 break;
257 }
258 }
259 }
fb40c209 260
645eab03
YQ
261 /* After the last option is parsed, there should be only
262 'print-values'. */
263 if (argc - oind != 1)
6211c335
YQ
264 error (_("-stack-list-locals: Usage: [--no-frame-filters] "
265 "[--skip-unavailable] PRINT_VALUES"));
f5ec2042 266
1e611234 267 frame = get_selected_frame (NULL);
645eab03 268 print_value = mi_parse_print_values (argv[oind]);
1e611234
PM
269
270 if (! raw_arg && frame_filters)
271 {
d4dd3282 272 frame_filter_flags flags = PRINT_LEVEL | PRINT_LOCALS;
1e611234 273
10367c7c
PA
274 result = mi_apply_ext_lang_frame_filter (frame, flags, print_value,
275 current_uiout, 0, 0);
1e611234
PM
276 }
277
278 /* Run the inbuilt backtrace if there are no filters registered, or
279 if "--no-frame-filters" has been specified from the command. */
6dddc817 280 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
1e611234 281 {
d4c16835
PA
282 list_args_or_locals (user_frame_print_options,
283 locals, print_value, frame,
6211c335 284 skip_unavailable);
1e611234 285 }
fb40c209
AC
286}
287
7a93fb82 288/* Print a list of the arguments for the current frame. With argument
fb40c209 289 of 0, print only the names, with argument of 1 print also the
2b03b41d
SS
290 values. */
291
ce8f13f8 292void
9f33b8b7 293mi_cmd_stack_list_args (const char *command, char **argv, int argc)
fb40c209
AC
294{
295 int frame_low;
296 int frame_high;
297 int i;
bd2b40ac 298 frame_info_ptr fi;
8b777f02 299 enum print_values print_values;
79a45e25 300 struct ui_out *uiout = current_uiout;
1e611234 301 int raw_arg = 0;
242f1fd7 302 int oind = 0;
6211c335 303 int skip_unavailable = 0;
6dddc817 304 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
242f1fd7
YQ
305 enum opt
306 {
307 NO_FRAME_FILTERS,
6211c335 308 SKIP_UNAVAILABLE,
242f1fd7
YQ
309 };
310 static const struct mi_opt opts[] =
311 {
312 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
6211c335 313 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
242f1fd7
YQ
314 { 0, 0, 0 }
315 };
316
317 while (1)
318 {
319 char *oarg;
320 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
321 opts, &oind, &oarg);
fb40c209 322
242f1fd7
YQ
323 if (opt < 0)
324 break;
325 switch ((enum opt) opt)
326 {
327 case NO_FRAME_FILTERS:
328 raw_arg = oind;
329 break;
6211c335
YQ
330 case SKIP_UNAVAILABLE:
331 skip_unavailable = 1;
332 break;
242f1fd7
YQ
333 }
334 }
fb40c209 335
242f1fd7
YQ
336 if (argc - oind != 1 && argc - oind != 3)
337 error (_("-stack-list-arguments: Usage: " \
6211c335
YQ
338 "[--no-frame-filters] [--skip-unavailable] "
339 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
1e611234 340
242f1fd7 341 if (argc - oind == 3)
fb40c209 342 {
242f1fd7
YQ
343 frame_low = atoi (argv[1 + oind]);
344 frame_high = atoi (argv[2 + oind]);
fb40c209
AC
345 }
346 else
347 {
348 /* Called with no arguments, it means we want args for the whole
dda83cd7 349 backtrace. */
fb40c209
AC
350 frame_low = -1;
351 frame_high = -1;
352 }
353
242f1fd7 354 print_values = mi_parse_print_values (argv[oind]);
8b777f02 355
fb40c209
AC
356 /* Let's position fi on the frame at which to start the
357 display. Could be the innermost frame if the whole stack needs
2b03b41d 358 displaying, or if frame_low is 0. */
fb40c209
AC
359 for (i = 0, fi = get_current_frame ();
360 fi && i < frame_low;
361 i++, fi = get_prev_frame (fi));
362
363 if (fi == NULL)
1b05df00 364 error (_("-stack-list-arguments: Not enough frames in stack."));
fb40c209 365
10f489e5 366 ui_out_emit_list list_emitter (uiout, "stack-args");
fb40c209 367
1e611234 368 if (! raw_arg && frame_filters)
fb40c209 369 {
d4dd3282 370 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS;
1e611234
PM
371 int py_frame_low = frame_low;
372
373 /* We cannot pass -1 to frame_low, as that would signify a
374 relative backtrace from the tail of the stack. So, in the case
375 of frame_low == -1, assign and increment it. */
376 if (py_frame_low == -1)
377 py_frame_low++;
378
10367c7c
PA
379 result = mi_apply_ext_lang_frame_filter (get_current_frame (), flags,
380 print_values, current_uiout,
381 py_frame_low, frame_high);
fb40c209
AC
382 }
383
1e611234
PM
384 /* Run the inbuilt backtrace if there are no filters registered, or
385 if "--no-frame-filters" has been specified from the command. */
6dddc817 386 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
1e611234
PM
387 {
388 /* Now let's print the frames up to frame_high, or until there are
389 frames in the stack. */
390 for (;
391 fi && (i <= frame_high || frame_high == -1);
392 i++, fi = get_prev_frame (fi))
393 {
1e611234 394 QUIT;
2e783024 395 ui_out_emit_tuple tuple_emitter (uiout, "frame");
381befee 396 uiout->field_signed ("level", i);
d4c16835
PA
397 list_args_or_locals (user_frame_print_options,
398 arguments, print_values, fi, skip_unavailable);
1e611234
PM
399 }
400 }
fb40c209
AC
401}
402
daf3c977 403/* Print a list of the local variables (including arguments) for the
7a93fb82
VP
404 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
405 or both names and values of the variables must be printed. See
406 parse_print_value for possible values. */
2b03b41d 407
daf3c977 408void
9f33b8b7 409mi_cmd_stack_list_variables (const char *command, char **argv, int argc)
daf3c977 410{
bd2b40ac 411 frame_info_ptr frame;
1e611234 412 int raw_arg = 0;
6dddc817 413 enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
f486487f 414 enum print_values print_value;
645eab03 415 int oind = 0;
6211c335 416 int skip_unavailable = 0;
daf3c977 417
645eab03
YQ
418 if (argc > 1)
419 {
645eab03
YQ
420 enum opt
421 {
6211c335
YQ
422 NO_FRAME_FILTERS,
423 SKIP_UNAVAILABLE,
645eab03
YQ
424 };
425 static const struct mi_opt opts[] =
426 {
427 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
6211c335 428 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
645eab03
YQ
429 { 0, 0, 0 }
430 };
431
432 while (1)
433 {
434 char *oarg;
435 /* Don't parse 'print-values' as an option. */
436 int opt = mi_getopt ("-stack-list-variables", argc - 1,
437 argv, opts, &oind, &oarg);
438 if (opt < 0)
439 break;
440 switch ((enum opt) opt)
441 {
442 case NO_FRAME_FILTERS:
443 raw_arg = oind;
444 break;
6211c335
YQ
445 case SKIP_UNAVAILABLE:
446 skip_unavailable = 1;
447 break;
645eab03
YQ
448 }
449 }
450 }
daf3c977 451
645eab03
YQ
452 /* After the last option is parsed, there should be only
453 'print-values'. */
454 if (argc - oind != 1)
6211c335
YQ
455 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
456 "[--skip-unavailable] PRINT_VALUES"));
daf3c977 457
1e611234 458 frame = get_selected_frame (NULL);
645eab03 459 print_value = mi_parse_print_values (argv[oind]);
1e611234
PM
460
461 if (! raw_arg && frame_filters)
462 {
d4dd3282 463 frame_filter_flags flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
1e611234 464
10367c7c
PA
465 result = mi_apply_ext_lang_frame_filter (frame, flags,
466 print_value,
467 current_uiout, 0, 0);
1e611234
PM
468 }
469
470 /* Run the inbuilt backtrace if there are no filters registered, or
471 if "--no-frame-filters" has been specified from the command. */
6dddc817 472 if (! frame_filters || raw_arg || result == EXT_LANG_BT_NO_FILTERS)
1e611234 473 {
d4c16835
PA
474 list_args_or_locals (user_frame_print_options,
475 all, print_value, frame,
6211c335 476 skip_unavailable);
1e611234 477 }
daf3c977
VP
478}
479
2b03b41d
SS
480/* Print single local or argument. ARG must be already read in. For
481 WHAT and VALUES see list_args_or_locals.
93d86cef 482
2b03b41d 483 Errors are printed as if they would be the parameter value. Use
6211c335
YQ
484 zeroed ARG iff it should not be printed according to VALUES. If
485 SKIP_UNAVAILABLE is true, only print ARG if it is available. */
93d86cef
JK
486
487static void
488list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
6211c335 489 enum print_values values, int skip_unavailable)
93d86cef 490{
93d86cef 491 struct ui_out *uiout = current_uiout;
f99d8bf4 492
93d86cef
JK
493 gdb_assert (!arg->val || !arg->error);
494 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
495 && arg->error == NULL)
496 || values == PRINT_SIMPLE_VALUES
497 || (values == PRINT_ALL_VALUES
498 && (arg->val != NULL || arg->error != NULL)));
e18b2753
JK
499 gdb_assert (arg->entry_kind == print_entry_values_no
500 || (arg->entry_kind == print_entry_values_only
dda83cd7 501 && (arg->val || arg->error)));
93d86cef 502
6211c335
YQ
503 if (skip_unavailable && arg->val != NULL
504 && (value_entirely_unavailable (arg->val)
505 /* A scalar object that does not have all bits available is
506 also considered unavailable, because all bits contribute
507 to its representation. */
d0c97917 508 || (val_print_scalar_type_p (arg->val->type ())
6211c335 509 && !value_bytes_available (arg->val,
391f8628 510 arg->val->embedded_offset (),
d0c97917 511 arg->val->type ()->length ()))))
6211c335
YQ
512 return;
513
0092b74d 514 gdb::optional<ui_out_emit_tuple> tuple_emitter;
93d86cef 515 if (values != PRINT_NO_VALUES || what == all)
0092b74d 516 tuple_emitter.emplace (uiout, nullptr);
93d86cef 517
d7e74731
PA
518 string_file stb;
519
987012b8 520 stb.puts (arg->sym->print_name ());
e18b2753 521 if (arg->entry_kind == print_entry_values_only)
d7e74731 522 stb.puts ("@entry");
112e8700 523 uiout->field_stream ("name", stb);
93d86cef 524
d9743061 525 if (what == all && arg->sym->is_argument ())
381befee 526 uiout->field_signed ("arg", 1);
93d86cef
JK
527
528 if (values == PRINT_SIMPLE_VALUES)
529 {
5f9c5a63
SM
530 check_typedef (arg->sym->type ());
531 type_print (arg->sym->type (), "", &stb, -1);
112e8700 532 uiout->field_stream ("type", stb);
93d86cef
JK
533 }
534
535 if (arg->val || arg->error)
536 {
93d86cef 537 if (arg->error)
123cd851 538 stb.printf (_("<error reading variable: %s>"), arg->error.get ());
93d86cef
JK
539 else
540 {
a70b8144 541 try
93d86cef
JK
542 {
543 struct value_print_options opts;
544
2a998fc0 545 get_no_prettyformat_print_options (&opts);
dad6b350 546 opts.deref_ref = true;
d7e74731 547 common_val_print (arg->val, &stb, 0, &opts,
c1b5c1eb 548 language_def (arg->sym->language ()));
93d86cef 549 }
230d2906 550 catch (const gdb_exception_error &except)
492d29ea 551 {
3d6e9d23
TT
552 stb.printf (_("<error reading variable: %s>"),
553 except.what ());
492d29ea 554 }
93d86cef 555 }
112e8700 556 uiout->field_stream ("value", stb);
93d86cef 557 }
93d86cef 558}
daf3c977 559
5c4aa40b
YQ
560/* Print a list of the objects for the frame FI in a certain form,
561 which is determined by VALUES. The objects can be locals,
6211c335
YQ
562 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE
563 is true, only print the arguments or local variables whose values
564 are available. */
2b03b41d 565
fb40c209 566static void
d4c16835
PA
567list_args_or_locals (const frame_print_options &fp_opts,
568 enum what_to_list what, enum print_values values,
bd2b40ac 569 frame_info_ptr fi, int skip_unavailable)
fb40c209 570{
3977b71f 571 const struct block *block;
fb40c209 572 struct symbol *sym;
8157b174 573 struct block_iterator iter;
a121b7c1 574 const char *name_of_result;
79a45e25 575 struct ui_out *uiout = current_uiout;
fb40c209 576
ae767bfb 577 block = get_frame_block (fi, 0);
fb40c209 578
daf3c977
VP
579 switch (what)
580 {
581 case locals:
d6fd4674
PA
582 name_of_result = "locals";
583 break;
daf3c977 584 case arguments:
d6fd4674
PA
585 name_of_result = "args";
586 break;
daf3c977 587 case all:
d6fd4674
PA
588 name_of_result = "variables";
589 break;
654e7c1f 590 default:
f34652de 591 internal_error ("unexpected what_to_list: %d", (int) what);
daf3c977
VP
592 }
593
10f489e5 594 ui_out_emit_list list_emitter (uiout, name_of_result);
fb40c209
AC
595
596 while (block != 0)
597 {
de4f826b 598 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 599 {
dda83cd7 600 int print_me = 0;
39bf4652 601
66d7f48f 602 switch (sym->aclass ())
fb40c209
AC
603 {
604 default:
605 case LOC_UNDEF: /* catches errors */
606 case LOC_CONST: /* constant */
607 case LOC_TYPEDEF: /* local typedef */
608 case LOC_LABEL: /* local label */
609 case LOC_BLOCK: /* local function */
610 case LOC_CONST_BYTES: /* loc. byte seq. */
611 case LOC_UNRESOLVED: /* unresolved static */
612 case LOC_OPTIMIZED_OUT: /* optimized out */
613 print_me = 0;
614 break;
615
616 case LOC_ARG: /* argument */
617 case LOC_REF_ARG: /* reference arg */
fb40c209 618 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 619 case LOC_LOCAL: /* stack local */
fb40c209
AC
620 case LOC_STATIC: /* static */
621 case LOC_REGISTER: /* register */
4cf623b6 622 case LOC_COMPUTED: /* computed location */
daf3c977 623 if (what == all)
fb40c209 624 print_me = 1;
daf3c977 625 else if (what == locals)
d9743061 626 print_me = !sym->is_argument ();
daf3c977 627 else
d9743061 628 print_me = sym->is_argument ();
fb40c209
AC
629 break;
630 }
631 if (print_me)
632 {
6bb0384f 633 struct symbol *sym2;
e18b2753 634 struct frame_arg arg, entryarg;
fb40c209 635
d9743061 636 if (sym->is_argument ())
32fa152e
TT
637 sym2 = lookup_symbol_search_name (sym->search_name (),
638 block, VAR_DOMAIN).symbol;
f5ec2042 639 else
2a2d4dc3 640 sym2 = sym;
f7e44f65 641 gdb_assert (sym2 != NULL);
93d86cef 642
93d86cef 643 arg.sym = sym2;
e18b2753 644 arg.entry_kind = print_entry_values_no;
e18b2753
JK
645 entryarg.sym = sym2;
646 entryarg.entry_kind = print_entry_values_no;
93d86cef 647
f5ec2042
NR
648 switch (values)
649 {
650 case PRINT_SIMPLE_VALUES:
6121eeb7
TT
651 {
652 struct type *type = check_typedef (sym2->type ());
653 if (type->code () == TYPE_CODE_ARRAY
654 || type->code () == TYPE_CODE_STRUCT
655 || type->code () == TYPE_CODE_UNION)
656 break;
657 }
658 /* FALLTHROUGH */
659
f5ec2042 660 case PRINT_ALL_VALUES:
d9743061 661 if (sym->is_argument ())
d4c16835 662 read_frame_arg (fp_opts, sym2, fi, &arg, &entryarg);
82a0a75f
YQ
663 else
664 read_frame_local (sym2, fi, &arg);
f5ec2042 665 break;
fb40c209 666 }
7a93fb82 667
e18b2753 668 if (arg.entry_kind != print_entry_values_only)
6211c335 669 list_arg_or_local (&arg, what, values, skip_unavailable);
e18b2753 670 if (entryarg.entry_kind != print_entry_values_no)
6211c335 671 list_arg_or_local (&entryarg, what, values, skip_unavailable);
fb40c209
AC
672 }
673 }
2b03b41d 674
6c00f721 675 if (block->function ())
fb40c209
AC
676 break;
677 else
f135fe72 678 block = block->superblock ();
fb40c209 679 }
fb40c209
AC
680}
681
f67ffa6a
AB
682/* Read a frame specification from FRAME_EXP and return the selected frame.
683 Call error() if the specification is in any way invalid (so this
684 function never returns NULL).
685
686 The frame specification is usually an integer level number, however if
687 the number does not match a valid frame level then it will be treated as
688 a frame address. The frame address will then be used to find a matching
689 frame in the stack. If no matching frame is found then a new frame will
690 be created.
691
692 The use of FRAME_EXP as an address is undocumented in the GDB user
693 manual, this feature is supported here purely for backward
694 compatibility. */
695
bd2b40ac 696static frame_info_ptr
f67ffa6a
AB
697parse_frame_specification (const char *frame_exp)
698{
699 gdb_assert (frame_exp != NULL);
700
701 /* NOTE: Parse and evaluate expression, but do not use
702 functions such as parse_and_eval_long or
703 parse_and_eval_address to also extract the value.
704 Instead value_as_long and value_as_address are used.
705 This avoids problems with expressions that contain
706 side-effects. */
707 struct value *arg = parse_and_eval (frame_exp);
708
709 /* Assume ARG is an integer, and try using that to select a frame. */
bd2b40ac 710 frame_info_ptr fid;
f67ffa6a
AB
711 int level = value_as_long (arg);
712
713 fid = find_relative_frame (get_current_frame (), &level);
714 if (level == 0)
715 /* find_relative_frame was successful. */
716 return fid;
717
718 /* Convert the value into a corresponding address. */
719 CORE_ADDR addr = value_as_address (arg);
720
721 /* Assume that ADDR is an address, use that to identify a frame with a
722 matching ID. */
723 struct frame_id id = frame_id_build_wild (addr);
724
725 /* If (s)he specifies the frame with an address, he deserves
726 what (s)he gets. Still, give the highest one that matches.
727 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
728 know). */
729 for (fid = get_current_frame ();
730 fid != NULL;
731 fid = get_prev_frame (fid))
732 {
a0cbd650 733 if (id == get_frame_id (fid))
f67ffa6a 734 {
bd2b40ac 735 frame_info_ptr prev_frame;
f67ffa6a
AB
736
737 while (1)
738 {
739 prev_frame = get_prev_frame (fid);
740 if (!prev_frame
a0cbd650 741 || id != get_frame_id (prev_frame))
f67ffa6a
AB
742 break;
743 fid = prev_frame;
744 }
745 return fid;
746 }
747 }
748
749 /* We couldn't identify the frame as an existing frame, but
750 perhaps we can create one with a single argument. */
751 return create_new_frame (addr, 0);
752}
753
754/* Implement the -stack-select-frame MI command. */
755
ce8f13f8 756void
9f33b8b7 757mi_cmd_stack_select_frame (const char *command, char **argv, int argc)
fb40c209 758{
fcf43932 759 if (argc == 0 || argc > 1)
1b05df00 760 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
a2757c4e 761 select_frame (parse_frame_specification (argv[0]));
fb40c209 762}
64fd8944 763
ce8f13f8 764void
9f33b8b7 765mi_cmd_stack_info_frame (const char *command, char **argv, int argc)
64fd8944
NR
766{
767 if (argc > 0)
2b03b41d 768 error (_("-stack-info-frame: No arguments allowed"));
ce8f13f8 769
d4c16835
PA
770 print_frame_info (user_frame_print_options,
771 get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1);
64fd8944 772}