2013-05-10 Phil Muldoon <pmuldoon@redhat.com>
[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 cleanup *cleanup_tuple = NULL;
401 struct ui_out *uiout = current_uiout;
402 struct ui_file *stb;
403
404 stb = mem_fileopen ();
405 old_chain = make_cleanup_ui_file_delete (stb);
406
407 gdb_assert (!arg->val || !arg->error);
408 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
409 && arg->error == NULL)
410 || values == PRINT_SIMPLE_VALUES
411 || (values == PRINT_ALL_VALUES
412 && (arg->val != NULL || arg->error != NULL)));
413 gdb_assert (arg->entry_kind == print_entry_values_no
414 || (arg->entry_kind == print_entry_values_only
415 && (arg->val || arg->error)));
416
417 if (values != PRINT_NO_VALUES || what == all)
418 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
419
420 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
421 if (arg->entry_kind == print_entry_values_only)
422 fputs_filtered ("@entry", stb);
423 ui_out_field_stream (uiout, "name", stb);
424
425 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
426 ui_out_field_int (uiout, "arg", 1);
427
428 if (values == PRINT_SIMPLE_VALUES)
429 {
430 check_typedef (arg->sym->type);
431 type_print (arg->sym->type, "", stb, -1);
432 ui_out_field_stream (uiout, "type", stb);
433 }
434
435 if (arg->val || arg->error)
436 {
437 volatile struct gdb_exception except;
438
439 if (arg->error)
440 except.message = arg->error;
441 else
442 {
443 /* TRY_CATCH has two statements, wrap it in a block. */
444
445 TRY_CATCH (except, RETURN_MASK_ERROR)
446 {
447 struct value_print_options opts;
448
449 get_raw_print_options (&opts);
450 opts.deref_ref = 1;
451 common_val_print (arg->val, stb, 0, &opts,
452 language_def (SYMBOL_LANGUAGE (arg->sym)));
453 }
454 }
455 if (except.message)
456 fprintf_filtered (stb, _("<error reading variable: %s>"),
457 except.message);
458 ui_out_field_stream (uiout, "value", stb);
459 }
460
461 if (values != PRINT_NO_VALUES || what == all)
462 do_cleanups (cleanup_tuple);
463 do_cleanups (old_chain);
464 }
465
466 /* Print a list of the locals or the arguments for the currently
467 selected frame. If the argument passed is 0, printonly the names
468 of the variables, if an argument of 1 is passed, print the values
469 as well. */
470
471 static void
472 list_args_or_locals (enum what_to_list what, enum print_values values,
473 struct frame_info *fi)
474 {
475 struct block *block;
476 struct symbol *sym;
477 struct block_iterator iter;
478 struct cleanup *cleanup_list;
479 struct type *type;
480 char *name_of_result;
481 struct ui_out *uiout = current_uiout;
482
483 block = get_frame_block (fi, 0);
484
485 switch (what)
486 {
487 case locals:
488 name_of_result = "locals";
489 break;
490 case arguments:
491 name_of_result = "args";
492 break;
493 case all:
494 name_of_result = "variables";
495 break;
496 default:
497 internal_error (__FILE__, __LINE__,
498 "unexpected what_to_list: %d", (int) what);
499 }
500
501 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
502
503 while (block != 0)
504 {
505 ALL_BLOCK_SYMBOLS (block, iter, sym)
506 {
507 int print_me = 0;
508
509 switch (SYMBOL_CLASS (sym))
510 {
511 default:
512 case LOC_UNDEF: /* catches errors */
513 case LOC_CONST: /* constant */
514 case LOC_TYPEDEF: /* local typedef */
515 case LOC_LABEL: /* local label */
516 case LOC_BLOCK: /* local function */
517 case LOC_CONST_BYTES: /* loc. byte seq. */
518 case LOC_UNRESOLVED: /* unresolved static */
519 case LOC_OPTIMIZED_OUT: /* optimized out */
520 print_me = 0;
521 break;
522
523 case LOC_ARG: /* argument */
524 case LOC_REF_ARG: /* reference arg */
525 case LOC_REGPARM_ADDR: /* indirect register arg */
526 case LOC_LOCAL: /* stack local */
527 case LOC_STATIC: /* static */
528 case LOC_REGISTER: /* register */
529 case LOC_COMPUTED: /* computed location */
530 if (what == all)
531 print_me = 1;
532 else if (what == locals)
533 print_me = !SYMBOL_IS_ARGUMENT (sym);
534 else
535 print_me = SYMBOL_IS_ARGUMENT (sym);
536 break;
537 }
538 if (print_me)
539 {
540 struct symbol *sym2;
541 struct frame_arg arg, entryarg;
542
543 if (SYMBOL_IS_ARGUMENT (sym))
544 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
545 block, VAR_DOMAIN,
546 NULL);
547 else
548 sym2 = sym;
549 gdb_assert (sym2 != NULL);
550
551 memset (&arg, 0, sizeof (arg));
552 arg.sym = sym2;
553 arg.entry_kind = print_entry_values_no;
554 memset (&entryarg, 0, sizeof (entryarg));
555 entryarg.sym = sym2;
556 entryarg.entry_kind = print_entry_values_no;
557
558 switch (values)
559 {
560 case PRINT_SIMPLE_VALUES:
561 type = check_typedef (sym2->type);
562 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
563 && TYPE_CODE (type) != TYPE_CODE_STRUCT
564 && TYPE_CODE (type) != TYPE_CODE_UNION)
565 {
566 case PRINT_ALL_VALUES:
567 read_frame_arg (sym2, fi, &arg, &entryarg);
568 }
569 break;
570 }
571
572 if (arg.entry_kind != print_entry_values_only)
573 list_arg_or_local (&arg, what, values);
574 if (entryarg.entry_kind != print_entry_values_no)
575 list_arg_or_local (&entryarg, what, values);
576 xfree (arg.error);
577 xfree (entryarg.error);
578 }
579 }
580
581 if (BLOCK_FUNCTION (block))
582 break;
583 else
584 block = BLOCK_SUPERBLOCK (block);
585 }
586 do_cleanups (cleanup_list);
587 }
588
589 void
590 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
591 {
592 if (argc == 0 || argc > 1)
593 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
594
595 select_frame_command (argv[0], 1 /* not used */ );
596 }
597
598 void
599 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
600 {
601 if (argc > 0)
602 error (_("-stack-info-frame: No arguments allowed"));
603
604 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
605 }
This page took 0.050593 seconds and 5 git commands to generate.