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