Updated copyright notices for most files.
[external/binutils.git] / gdb / mi / mi-cmd-stack.c
1 /* MI Command Set - stack commands.
2    Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3    Free Software Foundation, 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
35 static 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. */
42 void
43 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
44 {
45   int frame_low;
46   int frame_high;
47   int i;
48   struct cleanup *cleanup_stack;
49   struct frame_info *fi;
50
51   if (argc > 2 || argc == 1)
52     error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]"));
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)
75     error (_("mi_cmd_stack_list_frames: Not enough frames in stack."));
76
77   cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack");
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;
86       /* Print the location and the address always, even for level 0.
87          args == 0: don't print the arguments. */
88       print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
89     }
90
91   do_cleanups (cleanup_stack);
92 }
93
94 void
95 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
96 {
97   int frame_high;
98   int i;
99   struct frame_info *fi;
100
101   if (argc > 1)
102     error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]"));
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);
117 }
118
119 /* Print a list of the locals for the current frame. With argument of
120    0, print only the names, with argument of 1 print also the
121    values. */
122 void
123 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
124 {
125   struct frame_info *frame;
126   enum print_values print_values;
127
128   if (argc != 1)
129     error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES"));
130
131    frame = get_selected_frame (NULL);
132
133    if (strcmp (argv[0], "0") == 0
134        || strcmp (argv[0], mi_no_values) == 0)
135      print_values = PRINT_NO_VALUES;
136    else if (strcmp (argv[0], "1") == 0
137             || strcmp (argv[0], mi_all_values) == 0)
138      print_values = PRINT_ALL_VALUES;
139    else if (strcmp (argv[0], "2") == 0
140             || strcmp (argv[0], mi_simple_values) == 0)
141      print_values = PRINT_SIMPLE_VALUES;
142    else
143      error (_("Unknown value for PRINT_VALUES: must be: \
144 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
145             mi_no_values, mi_all_values, mi_simple_values);
146   list_args_or_locals (1, print_values, frame);
147 }
148
149 /* Print a list of the arguments for the current frame. With argument
150    of 0, print only the names, with argument of 1 print also the
151    values. */
152 void
153 mi_cmd_stack_list_args (char *command, char **argv, int argc)
154 {
155   int frame_low;
156   int frame_high;
157   int i;
158   struct frame_info *fi;
159   struct cleanup *cleanup_stack_args;
160
161   if (argc < 1 || argc > 3 || argc == 2)
162     error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
163
164   if (argc == 3)
165     {
166       frame_low = atoi (argv[1]);
167       frame_high = atoi (argv[2]);
168     }
169   else
170     {
171       /* Called with no arguments, it means we want args for the whole
172          backtrace. */
173       frame_low = -1;
174       frame_high = -1;
175     }
176
177   /* Let's position fi on the frame at which to start the
178      display. Could be the innermost frame if the whole stack needs
179      displaying, or if frame_low is 0. */
180   for (i = 0, fi = get_current_frame ();
181        fi && i < frame_low;
182        i++, fi = get_prev_frame (fi));
183
184   if (fi == NULL)
185     error (_("mi_cmd_stack_list_args: Not enough frames in stack."));
186
187   cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
188
189   /* Now let's print the frames up to frame_high, or until there are
190      frames in the stack. */
191   for (;
192        fi && (i <= frame_high || frame_high == -1);
193        i++, fi = get_prev_frame (fi))
194     {
195       struct cleanup *cleanup_frame;
196       QUIT;
197       cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
198       ui_out_field_int (uiout, "level", i);
199       list_args_or_locals (0, atoi (argv[0]), fi);
200       do_cleanups (cleanup_frame);
201     }
202
203   do_cleanups (cleanup_stack_args);
204 }
205
206 /* Print a list of the locals or the arguments for the currently
207    selected frame.  If the argument passed is 0, printonly the names
208    of the variables, if an argument of 1 is passed, print the values
209    as well. */
210 static void
211 list_args_or_locals (int locals, int values, struct frame_info *fi)
212 {
213   struct block *block;
214   struct symbol *sym;
215   struct dict_iterator iter;
216   int nsyms;
217   struct cleanup *cleanup_list;
218   static struct ui_stream *stb = NULL;
219   struct type *type;
220
221   stb = ui_out_stream_new (uiout);
222
223   block = get_frame_block (fi, 0);
224
225   cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
226
227   while (block != 0)
228     {
229       ALL_BLOCK_SYMBOLS (block, iter, sym)
230         {
231           int print_me = 0;
232
233           switch (SYMBOL_CLASS (sym))
234             {
235             default:
236             case LOC_UNDEF:     /* catches errors        */
237             case LOC_CONST:     /* constant              */
238             case LOC_TYPEDEF:   /* local typedef         */
239             case LOC_LABEL:     /* local label           */
240             case LOC_BLOCK:     /* local function        */
241             case LOC_CONST_BYTES:       /* loc. byte seq.        */
242             case LOC_UNRESOLVED:        /* unresolved static     */
243             case LOC_OPTIMIZED_OUT:     /* optimized out         */
244               print_me = 0;
245               break;
246
247             case LOC_ARG:       /* argument              */
248             case LOC_REF_ARG:   /* reference arg         */
249             case LOC_REGPARM_ADDR:      /* indirect register arg */
250             case LOC_LOCAL:     /* stack local           */
251             case LOC_STATIC:    /* static                */
252             case LOC_REGISTER:  /* register              */
253             case LOC_COMPUTED:  /* computed location     */
254               if (SYMBOL_IS_ARGUMENT (sym) ? !locals : locals)
255                 print_me = 1;
256               break;
257             }
258           if (print_me)
259             {
260               struct cleanup *cleanup_tuple = NULL;
261               struct symbol *sym2;
262               struct value *val;
263               if (values != PRINT_NO_VALUES)
264                 cleanup_tuple =
265                   make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
266               ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
267
268               if (!locals)
269                 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
270                                       block, VAR_DOMAIN,
271                                       (int *) NULL);
272               else
273                 sym2 = sym;
274               switch (values)
275                 {
276                 case PRINT_SIMPLE_VALUES:
277                   type = check_typedef (sym2->type);
278                   type_print (sym2->type, "", stb->stream, -1);
279                   ui_out_field_stream (uiout, "type", stb);
280                   if (TYPE_CODE (type) != TYPE_CODE_ARRAY
281                       && TYPE_CODE (type) != TYPE_CODE_STRUCT
282                       && TYPE_CODE (type) != TYPE_CODE_UNION)
283                     {
284                       struct value_print_options opts;
285                       val = read_var_value (sym2, fi);
286                       get_raw_print_options (&opts);
287                       opts.deref_ref = 1;
288                       common_val_print
289                         (val, stb->stream, 0, &opts,
290                          language_def (SYMBOL_LANGUAGE (sym2)));
291                       ui_out_field_stream (uiout, "value", stb);
292                     }
293                   do_cleanups (cleanup_tuple);
294                   break;
295                 case PRINT_ALL_VALUES:
296                   {
297                     struct value_print_options opts;
298                     val = read_var_value (sym2, fi);
299                     get_raw_print_options (&opts);
300                     opts.deref_ref = 1;
301                     common_val_print
302                       (val, stb->stream, 0, &opts,
303                        language_def (SYMBOL_LANGUAGE (sym2)));
304                     ui_out_field_stream (uiout, "value", stb);
305                     do_cleanups (cleanup_tuple);
306                   }
307                   break;
308                 }
309             }
310         }
311       if (BLOCK_FUNCTION (block))
312         break;
313       else
314         block = BLOCK_SUPERBLOCK (block);
315     }
316   do_cleanups (cleanup_list);
317   ui_out_stream_delete (stb);
318 }
319
320 void
321 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
322 {
323   if (argc == 0 || argc > 1)
324     error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC"));
325
326   select_frame_command (argv[0], 1 /* not used */ );
327 }
328
329 void
330 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
331 {
332   if (argc > 0)
333     error (_("mi_cmd_stack_info_frame: No arguments required"));
334
335   print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
336 }