* symfile.c (find_separate_debug_file): Remove double free of
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
0fb0cc75 2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
6aba47ca 3 Free Software Foundation, Inc.
ab91fdd5 4 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
20
21#include "defs.h"
22#include "target.h"
23#include "frame.h"
24#include "value.h"
25#include "mi-cmds.h"
26#include "ui-out.h"
e88c90f2 27#include "symtab.h"
fe898f56 28#include "block.h"
b9362cc7 29#include "stack.h"
de4f826b 30#include "dictionary.h"
f5ec2042 31#include "gdb_string.h"
d8ca156b 32#include "language.h"
79a45b7d 33#include "valprint.h"
fb40c209
AC
34
35static void list_args_or_locals (int locals, int values, struct frame_info *fi);
36
37/* Print a list of the stack frames. Args can be none, in which case
38 we want to print the whole backtrace, or a pair of numbers
39 specifying the frame numbers at which to start and stop the
40 display. If the two numbers are equal, a single frame will be
41 displayed. */
ce8f13f8 42void
fb40c209
AC
43mi_cmd_stack_list_frames (char *command, char **argv, int argc)
44{
45 int frame_low;
46 int frame_high;
47 int i;
6ad4a2cf 48 struct cleanup *cleanup_stack;
fb40c209
AC
49 struct frame_info *fi;
50
fb40c209 51 if (argc > 2 || argc == 1)
8a3fe4f8 52 error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
53
54 if (argc == 2)
55 {
56 frame_low = atoi (argv[0]);
57 frame_high = atoi (argv[1]);
58 }
59 else
60 {
61 /* Called with no arguments, it means we want the whole
62 backtrace. */
63 frame_low = -1;
64 frame_high = -1;
65 }
66
67 /* Let's position fi on the frame at which to start the
68 display. Could be the innermost frame if the whole stack needs
69 displaying, or if frame_low is 0. */
70 for (i = 0, fi = get_current_frame ();
71 fi && i < frame_low;
72 i++, fi = get_prev_frame (fi));
73
74 if (fi == NULL)
8a3fe4f8 75 error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
fb40c209 76
6ad4a2cf 77 cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
fb40c209
AC
78
79 /* Now let;s print the frames up to frame_high, or until there are
80 frames in the stack. */
81 for (;
82 fi && (i <= frame_high || frame_high == -1);
83 i++, fi = get_prev_frame (fi))
84 {
85 QUIT;
0faf0076 86 /* Print the location and the address always, even for level 0.
fb40c209 87 args == 0: don't print the arguments. */
0faf0076 88 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
89 }
90
6ad4a2cf 91 do_cleanups (cleanup_stack);
fb40c209
AC
92}
93
ce8f13f8 94void
fb40c209
AC
95mi_cmd_stack_info_depth (char *command, char **argv, int argc)
96{
97 int frame_high;
98 int i;
99 struct frame_info *fi;
100
fb40c209 101 if (argc > 1)
8a3fe4f8 102 error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
103
104 if (argc == 1)
105 frame_high = atoi (argv[0]);
106 else
107 /* Called with no arguments, it means we want the real depth of
108 the stack. */
109 frame_high = -1;
110
111 for (i = 0, fi = get_current_frame ();
112 fi && (i < frame_high || frame_high == -1);
113 i++, fi = get_prev_frame (fi))
114 QUIT;
115
116 ui_out_field_int (uiout, "depth", i);
fb40c209
AC
117}
118
8b777f02
VP
119static enum print_values
120parse_print_values (char *name)
121{
122 if (strcmp (name, "0") == 0
123 || strcmp (name, mi_no_values) == 0)
124 return PRINT_NO_VALUES;
125 else if (strcmp (name, "1") == 0
126 || strcmp (name, mi_all_values) == 0)
127 return PRINT_ALL_VALUES;
128 else if (strcmp (name, "2") == 0
129 || strcmp (name, mi_simple_values) == 0)
130 return PRINT_SIMPLE_VALUES;
131 else
132 error (_("Unknown value for PRINT_VALUES: must be: \
1330 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
134 mi_no_values, mi_all_values, mi_simple_values);
135}
136
fb40c209
AC
137/* Print a list of the locals for the current frame. With argument of
138 0, print only the names, with argument of 1 print also the
139 values. */
ce8f13f8 140void
fb40c209
AC
141mi_cmd_stack_list_locals (char *command, char **argv, int argc)
142{
f5ec2042
NR
143 struct frame_info *frame;
144 enum print_values print_values;
145
fb40c209 146 if (argc != 1)
8a3fe4f8 147 error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
fb40c209 148
b04f3ab4 149 frame = get_selected_frame (NULL);
f5ec2042 150
8b777f02 151 list_args_or_locals (1, parse_print_values (argv[0]), frame);
fb40c209
AC
152}
153
154/* Print a list of the arguments for the current frame. With argument
155 of 0, print only the names, with argument of 1 print also the
156 values. */
ce8f13f8 157void
fb40c209
AC
158mi_cmd_stack_list_args (char *command, char **argv, int argc)
159{
160 int frame_low;
161 int frame_high;
162 int i;
163 struct frame_info *fi;
6ad4a2cf 164 struct cleanup *cleanup_stack_args;
8b777f02 165 enum print_values print_values;
fb40c209
AC
166
167 if (argc < 1 || argc > 3 || argc == 2)
8a3fe4f8 168 error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
169
170 if (argc == 3)
171 {
172 frame_low = atoi (argv[1]);
173 frame_high = atoi (argv[2]);
174 }
175 else
176 {
177 /* Called with no arguments, it means we want args for the whole
178 backtrace. */
179 frame_low = -1;
180 frame_high = -1;
181 }
182
8b777f02
VP
183 print_values = parse_print_values (argv[0]);
184
fb40c209
AC
185 /* Let's position fi on the frame at which to start the
186 display. Could be the innermost frame if the whole stack needs
187 displaying, or if frame_low is 0. */
188 for (i = 0, fi = get_current_frame ();
189 fi && i < frame_low;
190 i++, fi = get_prev_frame (fi));
191
192 if (fi == NULL)
8a3fe4f8 193 error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
fb40c209 194
6ad4a2cf 195 cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
196
197 /* Now let's print the frames up to frame_high, or until there are
198 frames in the stack. */
199 for (;
200 fi && (i <= frame_high || frame_high == -1);
201 i++, fi = get_prev_frame (fi))
202 {
6ad4a2cf 203 struct cleanup *cleanup_frame;
fb40c209 204 QUIT;
6ad4a2cf 205 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209 206 ui_out_field_int (uiout, "level", i);
8b777f02 207 list_args_or_locals (0, print_values, fi);
6ad4a2cf 208 do_cleanups (cleanup_frame);
fb40c209
AC
209 }
210
6ad4a2cf 211 do_cleanups (cleanup_stack_args);
fb40c209
AC
212}
213
214/* Print a list of the locals or the arguments for the currently
215 selected frame. If the argument passed is 0, printonly the names
216 of the variables, if an argument of 1 is passed, print the values
217 as well. */
218static void
219list_args_or_locals (int locals, int values, struct frame_info *fi)
220{
221 struct block *block;
222 struct symbol *sym;
de4f826b
DC
223 struct dict_iterator iter;
224 int nsyms;
6ad4a2cf 225 struct cleanup *cleanup_list;
fb40c209 226 static struct ui_stream *stb = NULL;
f5ec2042 227 struct type *type;
fb40c209
AC
228
229 stb = ui_out_stream_new (uiout);
230
ae767bfb 231 block = get_frame_block (fi, 0);
fb40c209 232
6ad4a2cf 233 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
fb40c209
AC
234
235 while (block != 0)
236 {
de4f826b 237 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 238 {
39bf4652
JB
239 int print_me = 0;
240
fb40c209
AC
241 switch (SYMBOL_CLASS (sym))
242 {
243 default:
244 case LOC_UNDEF: /* catches errors */
245 case LOC_CONST: /* constant */
246 case LOC_TYPEDEF: /* local typedef */
247 case LOC_LABEL: /* local label */
248 case LOC_BLOCK: /* local function */
249 case LOC_CONST_BYTES: /* loc. byte seq. */
250 case LOC_UNRESOLVED: /* unresolved static */
251 case LOC_OPTIMIZED_OUT: /* optimized out */
252 print_me = 0;
253 break;
254
255 case LOC_ARG: /* argument */
256 case LOC_REF_ARG: /* reference arg */
fb40c209 257 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 258 case LOC_LOCAL: /* stack local */
fb40c209
AC
259 case LOC_STATIC: /* static */
260 case LOC_REGISTER: /* register */
4cf623b6 261 case LOC_COMPUTED: /* computed location */
2a2d4dc3 262 if (SYMBOL_IS_ARGUMENT (sym) ? !locals : locals)
fb40c209
AC
263 print_me = 1;
264 break;
265 }
266 if (print_me)
267 {
6ad4a2cf 268 struct cleanup *cleanup_tuple = NULL;
6bb0384f 269 struct symbol *sym2;
9fbcbb40 270 struct value *val;
f5ec2042
NR
271 if (values != PRINT_NO_VALUES)
272 cleanup_tuple =
6ad4a2cf 273 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
f5ec2042 274 ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
fb40c209 275
f5ec2042
NR
276 if (!locals)
277 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
278 block, VAR_DOMAIN,
2570f2b7 279 (int *) NULL);
f5ec2042 280 else
2a2d4dc3 281 sym2 = sym;
f5ec2042
NR
282 switch (values)
283 {
284 case PRINT_SIMPLE_VALUES:
285 type = check_typedef (sym2->type);
286 type_print (sym2->type, "", stb->stream, -1);
287 ui_out_field_stream (uiout, "type", stb);
288 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
289 && TYPE_CODE (type) != TYPE_CODE_STRUCT
290 && TYPE_CODE (type) != TYPE_CODE_UNION)
291 {
79a45b7d 292 struct value_print_options opts;
9fbcbb40 293 val = read_var_value (sym2, fi);
79a45b7d
TT
294 get_raw_print_options (&opts);
295 opts.deref_ref = 1;
9fbcbb40 296 common_val_print
79a45b7d 297 (val, stb->stream, 0, &opts,
d8ca156b 298 language_def (SYMBOL_LANGUAGE (sym2)));
f5ec2042
NR
299 ui_out_field_stream (uiout, "value", stb);
300 }
301 do_cleanups (cleanup_tuple);
302 break;
303 case PRINT_ALL_VALUES:
79a45b7d
TT
304 {
305 struct value_print_options opts;
306 val = read_var_value (sym2, fi);
307 get_raw_print_options (&opts);
308 opts.deref_ref = 1;
309 common_val_print
310 (val, stb->stream, 0, &opts,
311 language_def (SYMBOL_LANGUAGE (sym2)));
312 ui_out_field_stream (uiout, "value", stb);
313 do_cleanups (cleanup_tuple);
314 }
f5ec2042 315 break;
fb40c209
AC
316 }
317 }
318 }
319 if (BLOCK_FUNCTION (block))
320 break;
321 else
322 block = BLOCK_SUPERBLOCK (block);
323 }
6ad4a2cf 324 do_cleanups (cleanup_list);
fb40c209
AC
325 ui_out_stream_delete (stb);
326}
327
ce8f13f8 328void
fb40c209
AC
329mi_cmd_stack_select_frame (char *command, char **argv, int argc)
330{
fcf43932
NR
331 if (argc == 0 || argc > 1)
332 error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
fb40c209 333
fcf43932 334 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 335}
64fd8944 336
ce8f13f8 337void
64fd8944
NR
338mi_cmd_stack_info_frame (char *command, char **argv, int argc)
339{
340 if (argc > 0)
341 error (_("mi_cmd_stack_info_frame: No arguments required"));
ce8f13f8 342
64fd8944 343 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 344}
This page took 0.700469 seconds and 4 git commands to generate.