* mi/mi-cmd-break.c: Enforce coding standards, fix comments.
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
1 /* MI Command Set - stack commands.
2 Copyright (C) 2000, 2002-2005, 2007-2012 Free Software Foundation,
3 Inc.
4 Contributed by Cygnus Solutions (a Red Hat company).
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
27 #include "symtab.h"
28 #include "block.h"
29 #include "stack.h"
30 #include "dictionary.h"
31 #include "gdb_string.h"
32 #include "language.h"
33 #include "valprint.h"
34 #include "exceptions.h"
35
36 enum what_to_list { locals, arguments, all };
37
38 static void list_args_or_locals (enum what_to_list what,
39 enum print_values values,
40 struct frame_info *fi);
41
42 /* Print a list of the stack frames. Args can be none, in which case
43 we want to print the whole backtrace, or a pair of numbers
44 specifying the frame numbers at which to start and stop the
45 display. If the two numbers are equal, a single frame will be
46 displayed. */
47
48 void
49 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
50 {
51 int frame_low;
52 int frame_high;
53 int i;
54 struct cleanup *cleanup_stack;
55 struct frame_info *fi;
56
57 if (argc > 2 || argc == 1)
58 error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
59
60 if (argc == 2)
61 {
62 frame_low = atoi (argv[0]);
63 frame_high = atoi (argv[1]);
64 }
65 else
66 {
67 /* Called with no arguments, it means we want the whole
68 backtrace. */
69 frame_low = -1;
70 frame_high = -1;
71 }
72
73 /* Let's position fi on the frame at which to start the
74 display. Could be the innermost frame if the whole stack needs
75 displaying, or if frame_low is 0. */
76 for (i = 0, fi = get_current_frame ();
77 fi && i < frame_low;
78 i++, fi = get_prev_frame (fi));
79
80 if (fi == NULL)
81 error (_("-stack-list-frames: Not enough frames in stack."));
82
83 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
84
85 /* Now let's print the frames up to frame_high, or until there are
86 frames in the stack. */
87 for (;
88 fi && (i <= frame_high || frame_high == -1);
89 i++, fi = get_prev_frame (fi))
90 {
91 QUIT;
92 /* Print the location and the address always, even for level 0.
93 If args is 0, don't print the arguments. */
94 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
95 }
96
97 do_cleanups (cleanup_stack);
98 }
99
100 void
101 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
102 {
103 int frame_high;
104 int i;
105 struct frame_info *fi;
106
107 if (argc > 1)
108 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
109
110 if (argc == 1)
111 frame_high = atoi (argv[0]);
112 else
113 /* Called with no arguments, it means we want the real depth of
114 the stack. */
115 frame_high = -1;
116
117 for (i = 0, fi = get_current_frame ();
118 fi && (i < frame_high || frame_high == -1);
119 i++, fi = get_prev_frame (fi))
120 QUIT;
121
122 ui_out_field_int (current_uiout, "depth", i);
123 }
124
125 static enum print_values
126 parse_print_values (char *name)
127 {
128 if (strcmp (name, "0") == 0
129 || strcmp (name, mi_no_values) == 0)
130 return PRINT_NO_VALUES;
131 else if (strcmp (name, "1") == 0
132 || strcmp (name, mi_all_values) == 0)
133 return PRINT_ALL_VALUES;
134 else if (strcmp (name, "2") == 0
135 || strcmp (name, mi_simple_values) == 0)
136 return PRINT_SIMPLE_VALUES;
137 else
138 error (_("Unknown value for PRINT_VALUES: must be: \
139 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
140 mi_no_values, mi_all_values, mi_simple_values);
141 }
142
143 /* Print a list of the locals for the current frame. With argument of
144 0, print only the names, with argument of 1 print also the
145 values. */
146
147 void
148 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
149 {
150 struct frame_info *frame;
151
152 if (argc != 1)
153 error (_("-stack-list-locals: Usage: PRINT_VALUES"));
154
155 frame = get_selected_frame (NULL);
156
157 list_args_or_locals (locals, parse_print_values (argv[0]), frame);
158 }
159
160 /* Print a list of the arguments for the current frame. With argument
161 of 0, print only the names, with argument of 1 print also the
162 values. */
163
164 void
165 mi_cmd_stack_list_args (char *command, char **argv, int argc)
166 {
167 int frame_low;
168 int frame_high;
169 int i;
170 struct frame_info *fi;
171 struct cleanup *cleanup_stack_args;
172 enum print_values print_values;
173 struct ui_out *uiout = current_uiout;
174
175 if (argc < 1 || argc > 3 || argc == 2)
176 error (_("-stack-list-arguments: Usage: "
177 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
178
179 if (argc == 3)
180 {
181 frame_low = atoi (argv[1]);
182 frame_high = atoi (argv[2]);
183 }
184 else
185 {
186 /* Called with no arguments, it means we want args for the whole
187 backtrace. */
188 frame_low = -1;
189 frame_high = -1;
190 }
191
192 print_values = parse_print_values (argv[0]);
193
194 /* Let's position fi on the frame at which to start the
195 display. Could be the innermost frame if the whole stack needs
196 displaying, or if frame_low is 0. */
197 for (i = 0, fi = get_current_frame ();
198 fi && i < frame_low;
199 i++, fi = get_prev_frame (fi));
200
201 if (fi == NULL)
202 error (_("-stack-list-arguments: Not enough frames in stack."));
203
204 cleanup_stack_args
205 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
206
207 /* Now let's print the frames up to frame_high, or until there are
208 frames in the stack. */
209 for (;
210 fi && (i <= frame_high || frame_high == -1);
211 i++, fi = get_prev_frame (fi))
212 {
213 struct cleanup *cleanup_frame;
214
215 QUIT;
216 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
217 ui_out_field_int (uiout, "level", i);
218 list_args_or_locals (arguments, print_values, fi);
219 do_cleanups (cleanup_frame);
220 }
221
222 do_cleanups (cleanup_stack_args);
223 }
224
225 /* Print a list of the local variables (including arguments) for the
226 current frame. ARGC must be 1 and ARGV[0] specify if only the names,
227 or both names and values of the variables must be printed. See
228 parse_print_value for possible values. */
229
230 void
231 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
232 {
233 struct frame_info *frame;
234
235 if (argc != 1)
236 error (_("Usage: PRINT_VALUES"));
237
238 frame = get_selected_frame (NULL);
239
240 list_args_or_locals (all, parse_print_values (argv[0]), frame);
241 }
242
243 /* Print single local or argument. ARG must be already read in. For
244 WHAT and VALUES see list_args_or_locals.
245
246 Errors are printed as if they would be the parameter value. Use
247 zeroed ARG iff it should not be printed according to VALUES. */
248
249 static void
250 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
251 enum print_values values)
252 {
253 struct cleanup *cleanup_tuple = NULL;
254 struct ui_out *uiout = current_uiout;
255 struct ui_stream *stb = ui_out_stream_new (uiout);
256
257 gdb_assert (!arg->val || !arg->error);
258 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
259 && arg->error == NULL)
260 || values == PRINT_SIMPLE_VALUES
261 || (values == PRINT_ALL_VALUES
262 && (arg->val != NULL || arg->error != NULL)));
263 gdb_assert (arg->entry_kind == print_entry_values_no
264 || (arg->entry_kind == print_entry_values_only
265 && (arg->val || arg->error)));
266
267 if (values != PRINT_NO_VALUES || what == all)
268 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
269
270 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb->stream);
271 if (arg->entry_kind == print_entry_values_only)
272 fputs_filtered ("@entry", stb->stream);
273 ui_out_field_stream (uiout, "name", stb);
274
275 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
276 ui_out_field_int (uiout, "arg", 1);
277
278 if (values == PRINT_SIMPLE_VALUES)
279 {
280 check_typedef (arg->sym->type);
281 type_print (arg->sym->type, "", stb->stream, -1);
282 ui_out_field_stream (uiout, "type", stb);
283 }
284
285 if (arg->val || arg->error)
286 {
287 volatile struct gdb_exception except;
288
289 if (arg->error)
290 except.message = arg->error;
291 else
292 {
293 /* TRY_CATCH has two statements, wrap it in a block. */
294
295 TRY_CATCH (except, RETURN_MASK_ERROR)
296 {
297 struct value_print_options opts;
298
299 get_raw_print_options (&opts);
300 opts.deref_ref = 1;
301 common_val_print (arg->val, stb->stream, 0, &opts,
302 language_def (SYMBOL_LANGUAGE (arg->sym)));
303 }
304 }
305 if (except.message)
306 fprintf_filtered (stb->stream, _("<error reading variable: %s>"),
307 except.message);
308 ui_out_field_stream (uiout, "value", stb);
309 }
310
311 ui_out_stream_delete (stb);
312 if (values != PRINT_NO_VALUES || what == all)
313 do_cleanups (cleanup_tuple);
314 }
315
316 /* Print a list of the locals or the arguments for the currently
317 selected frame. If the argument passed is 0, printonly the names
318 of the variables, if an argument of 1 is passed, print the values
319 as well. */
320
321 static void
322 list_args_or_locals (enum what_to_list what, enum print_values values,
323 struct frame_info *fi)
324 {
325 struct block *block;
326 struct symbol *sym;
327 struct dict_iterator iter;
328 struct cleanup *cleanup_list;
329 struct ui_stream *stb;
330 struct type *type;
331 char *name_of_result;
332 struct ui_out *uiout = current_uiout;
333
334 stb = ui_out_stream_new (uiout);
335
336 block = get_frame_block (fi, 0);
337
338 switch (what)
339 {
340 case locals:
341 name_of_result = "locals";
342 break;
343 case arguments:
344 name_of_result = "args";
345 break;
346 case all:
347 name_of_result = "variables";
348 break;
349 default:
350 internal_error (__FILE__, __LINE__,
351 "unexpected what_to_list: %d", (int) what);
352 }
353
354 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
355
356 while (block != 0)
357 {
358 ALL_BLOCK_SYMBOLS (block, iter, sym)
359 {
360 int print_me = 0;
361
362 switch (SYMBOL_CLASS (sym))
363 {
364 default:
365 case LOC_UNDEF: /* catches errors */
366 case LOC_CONST: /* constant */
367 case LOC_TYPEDEF: /* local typedef */
368 case LOC_LABEL: /* local label */
369 case LOC_BLOCK: /* local function */
370 case LOC_CONST_BYTES: /* loc. byte seq. */
371 case LOC_UNRESOLVED: /* unresolved static */
372 case LOC_OPTIMIZED_OUT: /* optimized out */
373 print_me = 0;
374 break;
375
376 case LOC_ARG: /* argument */
377 case LOC_REF_ARG: /* reference arg */
378 case LOC_REGPARM_ADDR: /* indirect register arg */
379 case LOC_LOCAL: /* stack local */
380 case LOC_STATIC: /* static */
381 case LOC_REGISTER: /* register */
382 case LOC_COMPUTED: /* computed location */
383 if (what == all)
384 print_me = 1;
385 else if (what == locals)
386 print_me = !SYMBOL_IS_ARGUMENT (sym);
387 else
388 print_me = SYMBOL_IS_ARGUMENT (sym);
389 break;
390 }
391 if (print_me)
392 {
393 struct symbol *sym2;
394 struct frame_arg arg, entryarg;
395
396 if (SYMBOL_IS_ARGUMENT (sym))
397 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
398 block, VAR_DOMAIN,
399 (int *) NULL);
400 else
401 sym2 = sym;
402 gdb_assert (sym2 != NULL);
403
404 memset (&arg, 0, sizeof (arg));
405 arg.sym = sym2;
406 arg.entry_kind = print_entry_values_no;
407 memset (&entryarg, 0, sizeof (entryarg));
408 entryarg.sym = sym2;
409 entryarg.entry_kind = print_entry_values_no;
410
411 switch (values)
412 {
413 case PRINT_SIMPLE_VALUES:
414 type = check_typedef (sym2->type);
415 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
416 && TYPE_CODE (type) != TYPE_CODE_STRUCT
417 && TYPE_CODE (type) != TYPE_CODE_UNION)
418 {
419 case PRINT_ALL_VALUES:
420 read_frame_arg (sym2, fi, &arg, &entryarg);
421 }
422 break;
423 }
424
425 if (arg.entry_kind != print_entry_values_only)
426 list_arg_or_local (&arg, what, values);
427 if (entryarg.entry_kind != print_entry_values_no)
428 list_arg_or_local (&entryarg, what, values);
429 xfree (arg.error);
430 xfree (entryarg.error);
431 }
432 }
433
434 if (BLOCK_FUNCTION (block))
435 break;
436 else
437 block = BLOCK_SUPERBLOCK (block);
438 }
439 do_cleanups (cleanup_list);
440 ui_out_stream_delete (stb);
441 }
442
443 void
444 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
445 {
446 if (argc == 0 || argc > 1)
447 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
448
449 select_frame_command (argv[0], 1 /* not used */ );
450 }
451
452 void
453 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
454 {
455 if (argc > 0)
456 error (_("-stack-info-frame: No arguments allowed"));
457
458 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
459 }
This page took 0.041567 seconds and 5 git commands to generate.