]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-cmd-stack.c
* psymtab.c (PSYMTAB_TO_SYMTAB): Remove.
[thirdparty/binutils-gdb.git] / gdb / mi / mi-cmd-stack.c
CommitLineData
fb40c209 1/* MI Command Set - stack commands.
0b302171
JB
2 Copyright (C) 2000, 2002-2005, 2007-2012 Free Software Foundation,
3 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"
875b4ff5 34#include "exceptions.h"
daf3c977
VP
35
36enum what_to_list { locals, arguments, all };
37
38static void list_args_or_locals (enum what_to_list what,
bdaf8d4a
JK
39 enum print_values values,
40 struct frame_info *fi);
fb40c209 41
2b03b41d 42/* Print a list of the stack frames. Args can be none, in which case
fb40c209
AC
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
2b03b41d
SS
45 display. If the two numbers are equal, a single frame will be
46 displayed. */
47
ce8f13f8 48void
fb40c209
AC
49mi_cmd_stack_list_frames (char *command, char **argv, int argc)
50{
51 int frame_low;
52 int frame_high;
53 int i;
6ad4a2cf 54 struct cleanup *cleanup_stack;
fb40c209
AC
55 struct frame_info *fi;
56
fb40c209 57 if (argc > 2 || argc == 1)
1b05df00 58 error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
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
2b03b41d 68 backtrace. */
fb40c209
AC
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
2b03b41d 75 displaying, or if frame_low is 0. */
fb40c209
AC
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)
1b05df00 81 error (_("-stack-list-frames: Not enough frames in stack."));
fb40c209 82
79a45e25 83 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
fb40c209 84
2b03b41d
SS
85 /* Now let's print the frames up to frame_high, or until there are
86 frames in the stack. */
fb40c209
AC
87 for (;
88 fi && (i <= frame_high || frame_high == -1);
89 i++, fi = get_prev_frame (fi))
90 {
91 QUIT;
0faf0076 92 /* Print the location and the address always, even for level 0.
2b03b41d 93 If args is 0, don't print the arguments. */
0faf0076 94 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
fb40c209
AC
95 }
96
6ad4a2cf 97 do_cleanups (cleanup_stack);
fb40c209
AC
98}
99
ce8f13f8 100void
fb40c209
AC
101mi_cmd_stack_info_depth (char *command, char **argv, int argc)
102{
103 int frame_high;
104 int i;
105 struct frame_info *fi;
106
fb40c209 107 if (argc > 1)
1b05df00 108 error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
fb40c209
AC
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
2b03b41d 114 the stack. */
fb40c209
AC
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
79a45e25 122 ui_out_field_int (current_uiout, "depth", i);
fb40c209
AC
123}
124
8b777f02
VP
125static enum print_values
126parse_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: \
1390 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
140 mi_no_values, mi_all_values, mi_simple_values);
141}
142
7a93fb82 143/* Print a list of the locals for the current frame. With argument of
fb40c209 144 0, print only the names, with argument of 1 print also the
2b03b41d
SS
145 values. */
146
ce8f13f8 147void
fb40c209
AC
148mi_cmd_stack_list_locals (char *command, char **argv, int argc)
149{
f5ec2042 150 struct frame_info *frame;
f5ec2042 151
fb40c209 152 if (argc != 1)
1b05df00 153 error (_("-stack-list-locals: Usage: PRINT_VALUES"));
fb40c209 154
b04f3ab4 155 frame = get_selected_frame (NULL);
f5ec2042 156
daf3c977 157 list_args_or_locals (locals, parse_print_values (argv[0]), frame);
fb40c209
AC
158}
159
7a93fb82 160/* Print a list of the arguments for the current frame. With argument
fb40c209 161 of 0, print only the names, with argument of 1 print also the
2b03b41d
SS
162 values. */
163
ce8f13f8 164void
fb40c209
AC
165mi_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;
6ad4a2cf 171 struct cleanup *cleanup_stack_args;
8b777f02 172 enum print_values print_values;
79a45e25 173 struct ui_out *uiout = current_uiout;
fb40c209
AC
174
175 if (argc < 1 || argc > 3 || argc == 2)
1b05df00 176 error (_("-stack-list-arguments: Usage: "
9a2b4c1b 177 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
fb40c209
AC
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
2b03b41d 187 backtrace. */
fb40c209
AC
188 frame_low = -1;
189 frame_high = -1;
190 }
191
8b777f02
VP
192 print_values = parse_print_values (argv[0]);
193
fb40c209
AC
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
2b03b41d 196 displaying, or if frame_low is 0. */
fb40c209
AC
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)
1b05df00 202 error (_("-stack-list-arguments: Not enough frames in stack."));
fb40c209 203
9a2b4c1b
MS
204 cleanup_stack_args
205 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
fb40c209
AC
206
207 /* Now let's print the frames up to frame_high, or until there are
2b03b41d 208 frames in the stack. */
fb40c209
AC
209 for (;
210 fi && (i <= frame_high || frame_high == -1);
211 i++, fi = get_prev_frame (fi))
212 {
6ad4a2cf 213 struct cleanup *cleanup_frame;
102040f0 214
fb40c209 215 QUIT;
6ad4a2cf 216 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
fb40c209 217 ui_out_field_int (uiout, "level", i);
daf3c977 218 list_args_or_locals (arguments, print_values, fi);
6ad4a2cf 219 do_cleanups (cleanup_frame);
fb40c209
AC
220 }
221
6ad4a2cf 222 do_cleanups (cleanup_stack_args);
fb40c209
AC
223}
224
daf3c977 225/* Print a list of the local variables (including arguments) for the
7a93fb82
VP
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. */
2b03b41d 229
daf3c977
VP
230void
231mi_cmd_stack_list_variables (char *command, char **argv, int argc)
232{
233 struct frame_info *frame;
daf3c977
VP
234
235 if (argc != 1)
236 error (_("Usage: PRINT_VALUES"));
237
7a93fb82 238 frame = get_selected_frame (NULL);
daf3c977 239
7a93fb82 240 list_args_or_locals (all, parse_print_values (argv[0]), frame);
daf3c977
VP
241}
242
2b03b41d
SS
243/* Print single local or argument. ARG must be already read in. For
244 WHAT and VALUES see list_args_or_locals.
93d86cef 245
2b03b41d
SS
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. */
93d86cef
JK
248
249static void
250list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
251 enum print_values values)
252{
f99d8bf4 253 struct cleanup *old_chain;
93d86cef
JK
254 struct cleanup *cleanup_tuple = NULL;
255 struct ui_out *uiout = current_uiout;
f99d8bf4
PA
256 struct ui_file *stb;
257
258 stb = mem_fileopen ();
259 old_chain = make_cleanup_ui_file_delete (stb);
93d86cef
JK
260
261 gdb_assert (!arg->val || !arg->error);
262 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL
263 && arg->error == NULL)
264 || values == PRINT_SIMPLE_VALUES
265 || (values == PRINT_ALL_VALUES
266 && (arg->val != NULL || arg->error != NULL)));
e18b2753
JK
267 gdb_assert (arg->entry_kind == print_entry_values_no
268 || (arg->entry_kind == print_entry_values_only
269 && (arg->val || arg->error)));
93d86cef
JK
270
271 if (values != PRINT_NO_VALUES || what == all)
272 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
273
f99d8bf4 274 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb);
e18b2753 275 if (arg->entry_kind == print_entry_values_only)
f99d8bf4 276 fputs_filtered ("@entry", stb);
e18b2753 277 ui_out_field_stream (uiout, "name", stb);
93d86cef
JK
278
279 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym))
280 ui_out_field_int (uiout, "arg", 1);
281
282 if (values == PRINT_SIMPLE_VALUES)
283 {
284 check_typedef (arg->sym->type);
f99d8bf4 285 type_print (arg->sym->type, "", stb, -1);
93d86cef
JK
286 ui_out_field_stream (uiout, "type", stb);
287 }
288
289 if (arg->val || arg->error)
290 {
291 volatile struct gdb_exception except;
292
293 if (arg->error)
294 except.message = arg->error;
295 else
296 {
297 /* TRY_CATCH has two statements, wrap it in a block. */
298
299 TRY_CATCH (except, RETURN_MASK_ERROR)
300 {
301 struct value_print_options opts;
302
303 get_raw_print_options (&opts);
304 opts.deref_ref = 1;
f99d8bf4 305 common_val_print (arg->val, stb, 0, &opts,
93d86cef
JK
306 language_def (SYMBOL_LANGUAGE (arg->sym)));
307 }
308 }
309 if (except.message)
f99d8bf4 310 fprintf_filtered (stb, _("<error reading variable: %s>"),
93d86cef
JK
311 except.message);
312 ui_out_field_stream (uiout, "value", stb);
313 }
314
93d86cef
JK
315 if (values != PRINT_NO_VALUES || what == all)
316 do_cleanups (cleanup_tuple);
f99d8bf4 317 do_cleanups (old_chain);
93d86cef 318}
daf3c977 319
fb40c209
AC
320/* Print a list of the locals or the arguments for the currently
321 selected frame. If the argument passed is 0, printonly the names
322 of the variables, if an argument of 1 is passed, print the values
2b03b41d
SS
323 as well. */
324
fb40c209 325static void
bdaf8d4a
JK
326list_args_or_locals (enum what_to_list what, enum print_values values,
327 struct frame_info *fi)
fb40c209
AC
328{
329 struct block *block;
330 struct symbol *sym;
de4f826b 331 struct dict_iterator iter;
6ad4a2cf 332 struct cleanup *cleanup_list;
f5ec2042 333 struct type *type;
daf3c977 334 char *name_of_result;
79a45e25 335 struct ui_out *uiout = current_uiout;
fb40c209 336
ae767bfb 337 block = get_frame_block (fi, 0);
fb40c209 338
daf3c977
VP
339 switch (what)
340 {
341 case locals:
d6fd4674
PA
342 name_of_result = "locals";
343 break;
daf3c977 344 case arguments:
d6fd4674
PA
345 name_of_result = "args";
346 break;
daf3c977 347 case all:
d6fd4674
PA
348 name_of_result = "variables";
349 break;
654e7c1f 350 default:
d6fd4674
PA
351 internal_error (__FILE__, __LINE__,
352 "unexpected what_to_list: %d", (int) what);
daf3c977
VP
353 }
354
355 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
fb40c209
AC
356
357 while (block != 0)
358 {
de4f826b 359 ALL_BLOCK_SYMBOLS (block, iter, sym)
fb40c209 360 {
39bf4652
JB
361 int print_me = 0;
362
fb40c209
AC
363 switch (SYMBOL_CLASS (sym))
364 {
365 default:
366 case LOC_UNDEF: /* catches errors */
367 case LOC_CONST: /* constant */
368 case LOC_TYPEDEF: /* local typedef */
369 case LOC_LABEL: /* local label */
370 case LOC_BLOCK: /* local function */
371 case LOC_CONST_BYTES: /* loc. byte seq. */
372 case LOC_UNRESOLVED: /* unresolved static */
373 case LOC_OPTIMIZED_OUT: /* optimized out */
374 print_me = 0;
375 break;
376
377 case LOC_ARG: /* argument */
378 case LOC_REF_ARG: /* reference arg */
fb40c209 379 case LOC_REGPARM_ADDR: /* indirect register arg */
fb40c209 380 case LOC_LOCAL: /* stack local */
fb40c209
AC
381 case LOC_STATIC: /* static */
382 case LOC_REGISTER: /* register */
4cf623b6 383 case LOC_COMPUTED: /* computed location */
daf3c977 384 if (what == all)
fb40c209 385 print_me = 1;
daf3c977
VP
386 else if (what == locals)
387 print_me = !SYMBOL_IS_ARGUMENT (sym);
388 else
389 print_me = SYMBOL_IS_ARGUMENT (sym);
fb40c209
AC
390 break;
391 }
392 if (print_me)
393 {
6bb0384f 394 struct symbol *sym2;
e18b2753 395 struct frame_arg arg, entryarg;
fb40c209 396
daf3c977 397 if (SYMBOL_IS_ARGUMENT (sym))
f7e44f65 398 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
f5ec2042 399 block, VAR_DOMAIN,
2570f2b7 400 (int *) NULL);
f5ec2042 401 else
2a2d4dc3 402 sym2 = sym;
f7e44f65 403 gdb_assert (sym2 != NULL);
93d86cef
JK
404
405 memset (&arg, 0, sizeof (arg));
406 arg.sym = sym2;
e18b2753
JK
407 arg.entry_kind = print_entry_values_no;
408 memset (&entryarg, 0, sizeof (entryarg));
409 entryarg.sym = sym2;
410 entryarg.entry_kind = print_entry_values_no;
93d86cef 411
f5ec2042
NR
412 switch (values)
413 {
414 case PRINT_SIMPLE_VALUES:
415 type = check_typedef (sym2->type);
f5ec2042
NR
416 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
417 && TYPE_CODE (type) != TYPE_CODE_STRUCT
418 && TYPE_CODE (type) != TYPE_CODE_UNION)
419 {
f5ec2042 420 case PRINT_ALL_VALUES:
e18b2753 421 read_frame_arg (sym2, fi, &arg, &entryarg);
93d86cef 422 }
f5ec2042 423 break;
fb40c209 424 }
7a93fb82 425
e18b2753
JK
426 if (arg.entry_kind != print_entry_values_only)
427 list_arg_or_local (&arg, what, values);
428 if (entryarg.entry_kind != print_entry_values_no)
429 list_arg_or_local (&entryarg, what, values);
93d86cef 430 xfree (arg.error);
e18b2753 431 xfree (entryarg.error);
fb40c209
AC
432 }
433 }
2b03b41d 434
fb40c209
AC
435 if (BLOCK_FUNCTION (block))
436 break;
437 else
438 block = BLOCK_SUPERBLOCK (block);
439 }
6ad4a2cf 440 do_cleanups (cleanup_list);
fb40c209
AC
441}
442
ce8f13f8 443void
fb40c209
AC
444mi_cmd_stack_select_frame (char *command, char **argv, int argc)
445{
fcf43932 446 if (argc == 0 || argc > 1)
1b05df00 447 error (_("-stack-select-frame: Usage: FRAME_SPEC"));
fb40c209 448
fcf43932 449 select_frame_command (argv[0], 1 /* not used */ );
fb40c209 450}
64fd8944 451
ce8f13f8 452void
64fd8944
NR
453mi_cmd_stack_info_frame (char *command, char **argv, int argc)
454{
455 if (argc > 0)
2b03b41d 456 error (_("-stack-info-frame: No arguments allowed"));
ce8f13f8 457
64fd8944 458 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
64fd8944 459}