2003-06-08 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / mi / mi-cmd-stack.c
1 /* MI Command Set - stack commands.
2    Copyright 2000, 2002, 2003 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "target.h"
24 #include "frame.h"
25 #include "value.h"
26 #include "mi-cmds.h"
27 #include "ui-out.h"
28 #include "symtab.h"
29 #include "block.h"
30 #include "stack.h"
31
32 static void list_args_or_locals (int locals, int values, struct frame_info *fi);
33
34 /* Print a list of the stack frames. Args can be none, in which case
35    we want to print the whole backtrace, or a pair of numbers
36    specifying the frame numbers at which to start and stop the
37    display. If the two numbers are equal, a single frame will be
38    displayed. */
39 enum mi_cmd_result
40 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
41 {
42   int frame_low;
43   int frame_high;
44   int i;
45   struct cleanup *cleanup_stack;
46   struct frame_info *fi;
47
48   if (!target_has_stack)
49     error ("mi_cmd_stack_list_frames: No stack.");
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       /* level == i: always print the level 'i'
87          source == LOC_AND_ADDRESS: print the location and the address 
88          always, even for level 0.
89          args == 0: don't print the arguments. */
90       print_frame_info (fi /* frame info */ ,
91                         i /* level */ ,
92                         LOC_AND_ADDRESS /* source */ ,
93                         0 /* args */ );
94     }
95
96   do_cleanups (cleanup_stack);
97   if (i < frame_high)
98     error ("mi_cmd_stack_list_frames: Not enough frames in stack.");
99
100   return MI_CMD_DONE;
101 }
102
103 enum mi_cmd_result
104 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
105 {
106   int frame_high;
107   int i;
108   struct frame_info *fi;
109
110   if (!target_has_stack)
111     error ("mi_cmd_stack_info_depth: No stack.");
112
113   if (argc > 1)
114     error ("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]");
115
116   if (argc == 1)
117     frame_high = atoi (argv[0]);
118   else
119     /* Called with no arguments, it means we want the real depth of
120        the stack. */
121     frame_high = -1;
122
123   for (i = 0, fi = get_current_frame ();
124        fi && (i < frame_high || frame_high == -1);
125        i++, fi = get_prev_frame (fi))
126     QUIT;
127
128   ui_out_field_int (uiout, "depth", i);
129
130   return MI_CMD_DONE;
131 }
132
133 /* Print a list of the locals for the current frame. With argument of
134    0, print only the names, with argument of 1 print also the
135    values. */
136 enum mi_cmd_result
137 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
138 {
139   if (argc != 1)
140     error ("mi_cmd_stack_list_locals: Usage: PRINT_VALUES");
141
142   list_args_or_locals (1, atoi (argv[0]), deprecated_selected_frame);
143   return MI_CMD_DONE;
144 }
145
146 /* Print a list of the arguments for the current frame. With argument
147    of 0, print only the names, with argument of 1 print also the
148    values. */
149 enum mi_cmd_result
150 mi_cmd_stack_list_args (char *command, char **argv, int argc)
151 {
152   int frame_low;
153   int frame_high;
154   int i;
155   struct frame_info *fi;
156   struct cleanup *cleanup_stack_args;
157
158   if (argc < 1 || argc > 3 || argc == 2)
159     error ("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]");
160
161   if (argc == 3)
162     {
163       frame_low = atoi (argv[1]);
164       frame_high = atoi (argv[2]);
165     }
166   else
167     {
168       /* Called with no arguments, it means we want args for the whole
169          backtrace. */
170       frame_low = -1;
171       frame_high = -1;
172     }
173
174   /* Let's position fi on the frame at which to start the
175      display. Could be the innermost frame if the whole stack needs
176      displaying, or if frame_low is 0. */
177   for (i = 0, fi = get_current_frame ();
178        fi && i < frame_low;
179        i++, fi = get_prev_frame (fi));
180
181   if (fi == NULL)
182     error ("mi_cmd_stack_list_args: Not enough frames in stack.");
183
184   cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
185
186   /* Now let's print the frames up to frame_high, or until there are
187      frames in the stack. */
188   for (;
189        fi && (i <= frame_high || frame_high == -1);
190        i++, fi = get_prev_frame (fi))
191     {
192       struct cleanup *cleanup_frame;
193       QUIT;
194       cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
195       ui_out_field_int (uiout, "level", i);
196       list_args_or_locals (0, atoi (argv[0]), fi);
197       do_cleanups (cleanup_frame);
198     }
199
200   do_cleanups (cleanup_stack_args);
201   if (i < frame_high)
202     error ("mi_cmd_stack_list_args: Not enough frames in stack.");
203
204   return MI_CMD_DONE;
205 }
206
207 /* Print a list of the locals or the arguments for the currently
208    selected frame.  If the argument passed is 0, printonly the names
209    of the variables, if an argument of 1 is passed, print the values
210    as well. */
211 static void
212 list_args_or_locals (int locals, int values, struct frame_info *fi)
213 {
214   struct block *block;
215   struct symbol *sym;
216   int i, nsyms;
217   struct cleanup *cleanup_list;
218   static struct ui_stream *stb = NULL;
219
220   stb = ui_out_stream_new (uiout);
221
222   block = get_frame_block (fi, 0);
223
224   cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args");
225
226   while (block != 0)
227     {
228       ALL_BLOCK_SYMBOLS (block, i, sym)
229         {
230           int print_me = 0;
231
232           switch (SYMBOL_CLASS (sym))
233             {
234             default:
235             case LOC_UNDEF:     /* catches errors        */
236             case LOC_CONST:     /* constant              */
237             case LOC_TYPEDEF:   /* local typedef         */
238             case LOC_LABEL:     /* local label           */
239             case LOC_BLOCK:     /* local function        */
240             case LOC_CONST_BYTES:       /* loc. byte seq.        */
241             case LOC_UNRESOLVED:        /* unresolved static     */
242             case LOC_OPTIMIZED_OUT:     /* optimized out         */
243               print_me = 0;
244               break;
245
246             case LOC_ARG:       /* argument              */
247             case LOC_REF_ARG:   /* reference arg         */
248             case LOC_REGPARM:   /* register arg          */
249             case LOC_REGPARM_ADDR:      /* indirect register arg */
250             case LOC_LOCAL_ARG: /* stack arg             */
251             case LOC_BASEREG_ARG:       /* basereg arg           */
252             case LOC_COMPUTED_ARG:      /* arg with computed location */
253               if (!locals)
254                 print_me = 1;
255               break;
256
257             case LOC_LOCAL:     /* stack local           */
258             case LOC_BASEREG:   /* basereg local         */
259             case LOC_STATIC:    /* static                */
260             case LOC_REGISTER:  /* register              */
261             case LOC_COMPUTED:  /* computed location     */
262               if (locals)
263                 print_me = 1;
264               break;
265             }
266           if (print_me)
267             {
268               struct cleanup *cleanup_tuple = NULL;
269               if (values)
270                 cleanup_tuple = 
271                   make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
272               ui_out_field_string (uiout, "name", DEPRECATED_SYMBOL_NAME (sym));
273
274               if (values)
275                 {
276                   struct symbol *sym2;
277                   if (!locals)
278                     sym2 = lookup_symbol (DEPRECATED_SYMBOL_NAME (sym),
279                                           block, VAR_DOMAIN,
280                                           (int *) NULL,
281                                           (struct symtab **) NULL);
282                   else
283                     sym2 = sym;
284                   print_variable_value (sym2, fi, stb->stream);
285                   ui_out_field_stream (uiout, "value", stb);
286                   do_cleanups (cleanup_tuple);
287                 }
288             }
289         }
290       if (BLOCK_FUNCTION (block))
291         break;
292       else
293         block = BLOCK_SUPERBLOCK (block);
294     }
295   do_cleanups (cleanup_list);
296   ui_out_stream_delete (stb);
297 }
298
299 enum mi_cmd_result
300 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
301 {
302   if (!target_has_stack)
303     error ("mi_cmd_stack_select_frame: No stack.");
304
305   if (argc > 1)
306     error ("mi_cmd_stack_select_frame: Usage: [FRAME_SPEC]");
307
308   /* with no args, don't change frame */
309   if (argc == 0)
310     select_frame_command (0, 1 /* not used */ );
311   else
312     select_frame_command (argv[0], 1 /* not used */ );
313   return MI_CMD_DONE;
314 }