*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
1 /* MI Command Set - stack commands.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
26 #include "symtab.h"
27 #include "block.h"
28 #include "stack.h"
29 #include "dictionary.h"
30 #include "gdb_string.h"
31 #include "language.h"
32 #include "valprint.h"
33 #include "exceptions.h"
34 #include "utils.h"
35 #include "mi-getopt.h"
36 #include "python/python.h"
37 #include <ctype.h>
38
39 enum what_to_list { locals, arguments, all };
40
41 static void list_args_or_locals (enum what_to_list what,
42 enum print_values values,
43 struct frame_info *fi);
44
45 /* True if we want to allow Python-based frame filters. */
46 static int frame_filters = 0;
47
48 void
49 mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
50 {
51 if (argc != 0)
52 error (_("-enable-frame-filters: no arguments allowed"));
53 frame_filters = 1;
54 }
55
56 /* Parse the --no-frame-filters option in commands where we cannot use
57 mi_getopt. */
58 static int
59 parse_no_frames_option (const char *arg)
60 {
61 if (arg && (strcmp (arg, "--no-frame-filters") == 0))
62 return 1;
63
64 return 0;
65 }
66
67 /* Print a list of the stack frames. Args can be none, in which case
68 we want to print the whole backtrace, or a pair of numbers
69 specifying the frame numbers at which to start and stop the
70 display. If the two numbers are equal, a single frame will be
71 displayed. */
72
73 void
74 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
75 {
76 int frame_low;
77 int frame_high;
78 int i;
79 struct cleanup *cleanup_stack;
80 struct frame_info *fi;
81 enum py_bt_status result = PY_BT_ERROR;
82 int raw_arg = 0;
83 int oind = 0;
84 enum opt
85 {
86 NO_FRAME_FILTERS
87 };
88 static const struct mi_opt opts[] =
89 {
90 {"-no-frame-filters", NO_FRAME_FILTERS, 0},
91 { 0, 0, 0 }
92 };
93
94 /* Parse arguments. In this instance we are just looking for
95 --no-frame-filters. */
96 while (1)
97 {
98 char *oarg;
99 int opt = mi_getopt ("-stack-list-frames", argc, argv,
100 opts, &oind, &oarg);
101 if (opt < 0)
102 break;
103 switch ((enum opt) opt)
104 {
105 case NO_FRAME_FILTERS:
106 raw_arg = oind;
107 break;
108 }
109 }
110
111 /* After the last option is parsed, there should either be low -
112 high range, or no further arguments. */
113 if ((argc - oind != 0) && (argc - oind != 2))
114 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]"));
115
116 /* If there is a range, set it. */
117 if (argc - oind == 2)
118 {
119 frame_low = atoi (argv[0 + oind]);
120 frame_high = atoi (argv[1 + oind]);
121 }
122 else
123 {
124 /* Called with no arguments, it means we want the whole
125 backtrace. */
126 frame_low = -1;
127 frame_high = -1;
128 }
129
130 /* Let's position fi on the frame at which to start the
131 display. Could be the innermost frame if the whole stack needs
132 displaying, or if frame_low is 0. */
133 for (i = 0, fi = get_current_frame ();
134 fi && i < frame_low;
135 i++, fi = get_prev_frame (fi));
136
137 if (fi == NULL)
138 error (_("-stack-list-frames: Not enough frames in stack."));
139
140 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
141
142 if (! raw_arg && frame_filters)
143 {
144 int flags = PRINT_LEVEL | PRINT_FRAME_INFO;
145 int py_frame_low = frame_low;
146
147 /* We cannot pass -1 to frame_low, as that would signify a
148 relative backtrace from the tail of the stack. So, in the case
149 of frame_low == -1, assign and increment it. */
150 if (py_frame_low == -1)
151 py_frame_low++;
152
153 result = apply_frame_filter (get_current_frame (), flags,
154 NO_VALUES, current_uiout,
155 py_frame_low, frame_high);
156 }
157
158 /* Run the inbuilt backtrace if there are no filters registered, or
159 if "--no-frame-filters" has been specified from the command. */
160 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
161 {
162 /* Now let's print the frames up to frame_high, or until there are
163 frames in the stack. */
164 for (;
165 fi && (i <= frame_high || frame_high == -1);
166 i++, fi = get_prev_frame (fi))
167 {
168 QUIT;
169 /* Print the location and the address always, even for level 0.
170 If args is 0, don't print the arguments. */
171 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
172 }
173 }
174
175 do_cleanups (cleanup_stack);
176 }
177
178 void
179 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
180 {
181 int frame_high;
182 int i;
183 struct frame_info *fi;
184
185 if (argc > 1)
186 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
187
188 if (argc == 1)
189 frame_high = atoi (argv[0]);
190 else
191 /* Called with no arguments, it means we want the real depth of
192 the stack. */
193 frame_high = -1;
194
195 for (i = 0, fi = get_current_frame ();
196 fi && (i < frame_high || frame_high == -1);
197 i++, fi = get_prev_frame (fi))
198 QUIT;
199
200 ui_out_field_int (current_uiout, "depth", i);
201 }
202
203 static enum print_values
204 parse_print_values (char *name)
205 {
206 if (strcmp (name, "0") == 0
207 || strcmp (name, mi_no_values) == 0)
208 return PRINT_NO_VALUES;
209 else if (strcmp (name, "1") == 0
210 || strcmp (name, mi_all_values) == 0)
211 return PRINT_ALL_VALUES;
212 else if (strcmp (name, "2") == 0
213 || strcmp (name, mi_simple_values) == 0)
214 return PRINT_SIMPLE_VALUES;
215 else
216 error (_("Unknown value for PRINT_VALUES: must be: \
217 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
218 mi_no_values, mi_all_values, mi_simple_values);
219 }
220
221 /* Print a list of the locals for the current frame. With argument of
222 0, print only the names, with argument of 1 print also the
223 values. */
224
225 void
226 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
227 {
228 struct frame_info *frame;
229 int raw_arg = 0;
230 enum py_bt_status result = PY_BT_ERROR;
231 int print_value;
232
233 if (argc > 0)
234 raw_arg = parse_no_frames_option (argv[0]);
235
236 if (argc < 1 || argc > 2 || (argc == 2 && ! raw_arg)
237 || (argc == 1 && raw_arg))
238 error (_("-stack-list-locals: Usage: [--no-frame-filters] PRINT_VALUES"));
239
240 frame = get_selected_frame (NULL);
241 print_value = parse_print_values (argv[raw_arg]);
242
243 if (! raw_arg && frame_filters)
244 {
245 int flags = PRINT_LEVEL | PRINT_LOCALS;
246
247 result = apply_frame_filter (frame, flags, print_value,
248 current_uiout, 0, 0);
249 }
250
251 /* Run the inbuilt backtrace if there are no filters registered, or
252 if "--no-frame-filters" has been specified from the command. */
253 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
254 {
255 list_args_or_locals (locals, print_value, frame);
256 }
257 }
258
259 /* Print a list of the arguments for the current frame. With argument
260 of 0, print only the names, with argument of 1 print also the
261 values. */
262
263 void
264 mi_cmd_stack_list_args (char *command, char **argv, int argc)
265 {
266 int frame_low;
267 int frame_high;
268 int i;
269 struct frame_info *fi;
270 struct cleanup *cleanup_stack_args;
271 enum print_values print_values;
272 struct ui_out *uiout = current_uiout;
273 int raw_arg = 0;
274 enum py_bt_status result = PY_BT_ERROR;
275
276 if (argc > 0)
277 raw_arg = parse_no_frames_option (argv[0]);
278
279 if (argc < 1 || (argc > 3 && ! raw_arg) || (argc == 2 && ! raw_arg))
280 error (_("-stack-list-arguments: Usage: " \
281 "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
282
283 if (argc >= 3)
284 {
285 frame_low = atoi (argv[1 + raw_arg]);
286 frame_high = atoi (argv[2 + raw_arg]);
287 }
288 else
289 {
290 /* Called with no arguments, it means we want args for the whole
291 backtrace. */
292 frame_low = -1;
293 frame_high = -1;
294 }
295
296 print_values = parse_print_values (argv[raw_arg]);
297
298 /* Let's position fi on the frame at which to start the
299 display. Could be the innermost frame if the whole stack needs
300 displaying, or if frame_low is 0. */
301 for (i = 0, fi = get_current_frame ();
302 fi && i < frame_low;
303 i++, fi = get_prev_frame (fi));
304
305 if (fi == NULL)
306 error (_("-stack-list-arguments: Not enough frames in stack."));
307
308 cleanup_stack_args
309 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
310
311 if (! raw_arg && frame_filters)
312 {
313 int flags = PRINT_LEVEL | PRINT_ARGS;
314 int py_frame_low = frame_low;
315
316 /* We cannot pass -1 to frame_low, as that would signify a
317 relative backtrace from the tail of the stack. So, in the case
318 of frame_low == -1, assign and increment it. */
319 if (py_frame_low == -1)
320 py_frame_low++;
321
322 result = apply_frame_filter (get_current_frame (), flags,
323 print_values, current_uiout,
324 py_frame_low, frame_high);
325 }
326
327 /* Run the inbuilt backtrace if there are no filters registered, or
328 if "--no-frame-filters" has been specified from the command. */
329 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
330 {
331 /* Now let's print the frames up to frame_high, or until there are
332 frames in the stack. */
333 for (;
334 fi && (i <= frame_high || frame_high == -1);
335 i++, fi = get_prev_frame (fi))
336 {
337 struct cleanup *cleanup_frame;
338
339 QUIT;
340 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
341 ui_out_field_int (uiout, "level", i);
342 list_args_or_locals (arguments, print_values, fi);
343 do_cleanups (cleanup_frame);
344 }
345 }
346 do_cleanups (cleanup_stack_args);
347 }
348
349 /* Print a list of the local variables (including arguments) for the
350 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
351 or both names and values of the variables must be printed. See
352 parse_print_value for possible values. */
353
354 void
355 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
356 {
357 struct frame_info *frame;
358 int raw_arg = 0;
359 enum py_bt_status result = PY_BT_ERROR;
360 int print_value;
361
362 if (argc > 0)
363 raw_arg = parse_no_frames_option (argv[0]);
364
365 if (argc < 1 || argc > 2 || (argc == 2 && ! raw_arg)
366 || (argc == 1 && raw_arg))
367 error (_("-stack-list-variables: Usage: " \
368 "[--no-frame-filters] PRINT_VALUES"));
369
370 frame = get_selected_frame (NULL);
371 print_value = parse_print_values (argv[raw_arg]);
372
373 if (! raw_arg && frame_filters)
374 {
375 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS;
376
377 result = apply_frame_filter (frame, flags, print_value,
378 current_uiout, 0, 0);
379 }
380
381 /* Run the inbuilt backtrace if there are no filters registered, or
382 if "--no-frame-filters" has been specified from the command. */
383 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
384 {
385 list_args_or_locals (all, print_value, frame);
386 }
387 }
388
389 /* Print single local or argument. ARG must be already read in. For
390 WHAT and VALUES see list_args_or_locals.
391
392 Errors are printed as if they would be the parameter value. Use
393 zeroed ARG iff it should not be printed according to VALUES. */
394
395 static void
396 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
397 enum print_values values)
398 {
399 struct cleanup *old_chain;
400 struct ui_out *uiout = current_uiout;
401 struct ui_file *stb;
402
403 stb = mem_fileopen ();
404 old_chain = make_cleanup_ui_file_delete (stb);
405
406 gdb_assert (!arg->val || !arg->error);
407 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
408 && arg->error == NULL)
409 || values == PRINT_SIMPLE_VALUES
410 || (values == PRINT_ALL_VALUES
411 && (arg->val != NULL || arg->error != NULL)));
412 gdb_assert (arg->entry_kind == print_entry_values_no
413 || (arg->entry_kind == print_entry_values_only
414 && (arg->val || arg->error)));
415
416 if (values != PRINT_NO_VALUES || what == all)
417 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
418
419 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
420 if (arg->entry_kind == print_entry_values_only)
421 fputs_filtered ("@entry", stb);
422 ui_out_field_stream (uiout, "name", stb);
423
424 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
425 ui_out_field_int (uiout, "arg", 1);
426
427 if (values == PRINT_SIMPLE_VALUES)
428 {
429 check_typedef (arg->sym->type);
430 type_print (arg->sym->type, "", stb, -1);
431 ui_out_field_stream (uiout, "type", stb);
432 }
433
434 if (arg->val || arg->error)
435 {
436 volatile struct gdb_exception except;
437
438 if (arg->error)
439 except.message = arg->error;
440 else
441 {
442 /* TRY_CATCH has two statements, wrap it in a block. */
443
444 TRY_CATCH (except, RETURN_MASK_ERROR)
445 {
446 struct value_print_options opts;
447
448 get_raw_print_options (&opts);
449 opts.deref_ref = 1;
450 common_val_print (arg->val, stb, 0, &opts,
451 language_def (SYMBOL_LANGUAGE (arg->sym)));
452 }
453 }
454 if (except.message)
455 fprintf_filtered (stb, _("<error reading variable: %s>"),
456 except.message);
457 ui_out_field_stream (uiout, "value", stb);
458 }
459
460 do_cleanups (old_chain);
461 }
462
463 /* Print a list of the locals or the arguments for the currently
464 selected frame. If the argument passed is 0, printonly the names
465 of the variables, if an argument of 1 is passed, print the values
466 as well. */
467
468 static void
469 list_args_or_locals (enum what_to_list what, enum print_values values,
470 struct frame_info *fi)
471 {
472 struct block *block;
473 struct symbol *sym;
474 struct block_iterator iter;
475 struct cleanup *cleanup_list;
476 struct type *type;
477 char *name_of_result;
478 struct ui_out *uiout = current_uiout;
479
480 block = get_frame_block (fi, 0);
481
482 switch (what)
483 {
484 case locals:
485 name_of_result = "locals";
486 break;
487 case arguments:
488 name_of_result = "args";
489 break;
490 case all:
491 name_of_result = "variables";
492 break;
493 default:
494 internal_error (__FILE__, __LINE__,
495 "unexpected what_to_list: %d", (int) what);
496 }
497
498 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
499
500 while (block != 0)
501 {
502 ALL_BLOCK_SYMBOLS (block, iter, sym)
503 {
504 int print_me = 0;
505
506 switch (SYMBOL_CLASS (sym))
507 {
508 default:
509 case LOC_UNDEF: /* catches errors */
510 case LOC_CONST: /* constant */
511 case LOC_TYPEDEF: /* local typedef */
512 case LOC_LABEL: /* local label */
513 case LOC_BLOCK: /* local function */
514 case LOC_CONST_BYTES: /* loc. byte seq. */
515 case LOC_UNRESOLVED: /* unresolved static */
516 case LOC_OPTIMIZED_OUT: /* optimized out */
517 print_me = 0;
518 break;
519
520 case LOC_ARG: /* argument */
521 case LOC_REF_ARG: /* reference arg */
522 case LOC_REGPARM_ADDR: /* indirect register arg */
523 case LOC_LOCAL: /* stack local */
524 case LOC_STATIC: /* static */
525 case LOC_REGISTER: /* register */
526 case LOC_COMPUTED: /* computed location */
527 if (what == all)
528 print_me = 1;
529 else if (what == locals)
530 print_me = !SYMBOL_IS_ARGUMENT (sym);
531 else
532 print_me = SYMBOL_IS_ARGUMENT (sym);
533 break;
534 }
535 if (print_me)
536 {
537 struct symbol *sym2;
538 struct frame_arg arg, entryarg;
539
540 if (SYMBOL_IS_ARGUMENT (sym))
541 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
542 block, VAR_DOMAIN,
543 NULL);
544 else
545 sym2 = sym;
546 gdb_assert (sym2 != NULL);
547
548 memset (&arg, 0, sizeof (arg));
549 arg.sym = sym2;
550 arg.entry_kind = print_entry_values_no;
551 memset (&entryarg, 0, sizeof (entryarg));
552 entryarg.sym = sym2;
553 entryarg.entry_kind = print_entry_values_no;
554
555 switch (values)
556 {
557 case PRINT_SIMPLE_VALUES:
558 type = check_typedef (sym2->type);
559 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
560 && TYPE_CODE (type) != TYPE_CODE_STRUCT
561 && TYPE_CODE (type) != TYPE_CODE_UNION)
562 {
563 case PRINT_ALL_VALUES:
564 read_frame_arg (sym2, fi, &arg, &entryarg);
565 }
566 break;
567 }
568
569 if (arg.entry_kind != print_entry_values_only)
570 list_arg_or_local (&arg, what, values);
571 if (entryarg.entry_kind != print_entry_values_no)
572 list_arg_or_local (&entryarg, what, values);
573 xfree (arg.error);
574 xfree (entryarg.error);
575 }
576 }
577
578 if (BLOCK_FUNCTION (block))
579 break;
580 else
581 block = BLOCK_SUPERBLOCK (block);
582 }
583 do_cleanups (cleanup_list);
584 }
585
586 void
587 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
588 {
589 if (argc == 0 || argc > 1)
590 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
591
592 select_frame_command (argv[0], 1 /* not used */ );
593 }
594
595 void
596 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
597 {
598 if (argc > 0)
599 error (_("-stack-info-frame: No arguments allowed"));
600
601 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
602 }
This page took 0.061385 seconds and 4 git commands to generate.