gdb/
[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, 2010, 2011
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 #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 void
48 mi_cmd_stack_list_frames (char *command, char **argv, int argc)
49 {
50   int frame_low;
51   int frame_high;
52   int i;
53   struct cleanup *cleanup_stack;
54   struct frame_info *fi;
55
56   if (argc > 2 || argc == 1)
57     error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]"));
58
59   if (argc == 2)
60     {
61       frame_low = atoi (argv[0]);
62       frame_high = atoi (argv[1]);
63     }
64   else
65     {
66       /* Called with no arguments, it means we want the whole
67          backtrace. */
68       frame_low = -1;
69       frame_high = -1;
70     }
71
72   /* Let's position fi on the frame at which to start the
73      display. Could be the innermost frame if the whole stack needs
74      displaying, or if frame_low is 0. */
75   for (i = 0, fi = get_current_frame ();
76        fi && i < frame_low;
77        i++, fi = get_prev_frame (fi));
78
79   if (fi == NULL)
80     error (_("-stack-list-frames: Not enough frames in stack."));
81
82   cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack");
83
84   /* Now let;s print the frames up to frame_high, or until there are
85      frames in the stack. */
86   for (;
87        fi && (i <= frame_high || frame_high == -1);
88        i++, fi = get_prev_frame (fi))
89     {
90       QUIT;
91       /* Print the location and the address always, even for level 0.
92          args == 0: don't print the arguments. */
93       print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ );
94     }
95
96   do_cleanups (cleanup_stack);
97 }
98
99 void
100 mi_cmd_stack_info_depth (char *command, char **argv, int argc)
101 {
102   int frame_high;
103   int i;
104   struct frame_info *fi;
105
106   if (argc > 1)
107     error (_("-stack-info-depth: Usage: [MAX_DEPTH]"));
108
109   if (argc == 1)
110     frame_high = atoi (argv[0]);
111   else
112     /* Called with no arguments, it means we want the real depth of
113        the stack. */
114     frame_high = -1;
115
116   for (i = 0, fi = get_current_frame ();
117        fi && (i < frame_high || frame_high == -1);
118        i++, fi = get_prev_frame (fi))
119     QUIT;
120
121   ui_out_field_int (current_uiout, "depth", i);
122 }
123
124 static enum print_values
125 parse_print_values (char *name)
126 {
127    if (strcmp (name, "0") == 0
128        || strcmp (name, mi_no_values) == 0)
129      return PRINT_NO_VALUES;
130    else if (strcmp (name, "1") == 0
131             || strcmp (name, mi_all_values) == 0)
132      return PRINT_ALL_VALUES;
133    else if (strcmp (name, "2") == 0
134             || strcmp (name, mi_simple_values) == 0)
135      return PRINT_SIMPLE_VALUES;
136    else
137      error (_("Unknown value for PRINT_VALUES: must be: \
138 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
139             mi_no_values, mi_all_values, mi_simple_values);
140 }
141
142 /* Print a list of the locals for the current frame.  With argument of
143    0, print only the names, with argument of 1 print also the
144    values. */
145 void
146 mi_cmd_stack_list_locals (char *command, char **argv, int argc)
147 {
148   struct frame_info *frame;
149
150   if (argc != 1)
151     error (_("-stack-list-locals: Usage: PRINT_VALUES"));
152
153    frame = get_selected_frame (NULL);
154
155    list_args_or_locals (locals, parse_print_values (argv[0]), frame);
156 }
157
158 /* Print a list of the arguments for the current frame.  With argument
159    of 0, print only the names, with argument of 1 print also the
160    values. */
161 void
162 mi_cmd_stack_list_args (char *command, char **argv, int argc)
163 {
164   int frame_low;
165   int frame_high;
166   int i;
167   struct frame_info *fi;
168   struct cleanup *cleanup_stack_args;
169   enum print_values print_values;
170   struct ui_out *uiout = current_uiout;
171
172   if (argc < 1 || argc > 3 || argc == 2)
173     error (_("-stack-list-arguments: Usage: "
174              "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
175
176   if (argc == 3)
177     {
178       frame_low = atoi (argv[1]);
179       frame_high = atoi (argv[2]);
180     }
181   else
182     {
183       /* Called with no arguments, it means we want args for the whole
184          backtrace. */
185       frame_low = -1;
186       frame_high = -1;
187     }
188
189   print_values = parse_print_values (argv[0]);
190
191   /* Let's position fi on the frame at which to start the
192      display. Could be the innermost frame if the whole stack needs
193      displaying, or if frame_low is 0. */
194   for (i = 0, fi = get_current_frame ();
195        fi && i < frame_low;
196        i++, fi = get_prev_frame (fi));
197
198   if (fi == NULL)
199     error (_("-stack-list-arguments: Not enough frames in stack."));
200
201   cleanup_stack_args
202     = make_cleanup_ui_out_list_begin_end (uiout, "stack-args");
203
204   /* Now let's print the frames up to frame_high, or until there are
205      frames in the stack. */
206   for (;
207        fi && (i <= frame_high || frame_high == -1);
208        i++, fi = get_prev_frame (fi))
209     {
210       struct cleanup *cleanup_frame;
211
212       QUIT;
213       cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
214       ui_out_field_int (uiout, "level", i);
215       list_args_or_locals (arguments, print_values, fi);
216       do_cleanups (cleanup_frame);
217     }
218
219   do_cleanups (cleanup_stack_args);
220 }
221
222 /* Print a list of the local variables (including arguments) for the 
223    current frame.  ARGC must be 1 and ARGV[0] specify if only the names,
224    or both names and values of the variables must be printed.  See 
225    parse_print_value for possible values.  */
226 void
227 mi_cmd_stack_list_variables (char *command, char **argv, int argc)
228 {
229   struct frame_info *frame;
230
231   if (argc != 1)
232     error (_("Usage: PRINT_VALUES"));
233
234   frame = get_selected_frame (NULL);
235
236   list_args_or_locals (all, parse_print_values (argv[0]), frame);
237 }
238
239
240 /* Print a list of the locals or the arguments for the currently
241    selected frame.  If the argument passed is 0, printonly the names
242    of the variables, if an argument of 1 is passed, print the values
243    as well. */
244 static void
245 list_args_or_locals (enum what_to_list what, enum print_values values,
246                      struct frame_info *fi)
247 {
248   struct block *block;
249   struct symbol *sym;
250   struct dict_iterator iter;
251   struct cleanup *cleanup_list;
252   struct ui_stream *stb;
253   struct type *type;
254   char *name_of_result;
255   struct ui_out *uiout = current_uiout;
256
257   stb = ui_out_stream_new (uiout);
258
259   block = get_frame_block (fi, 0);
260
261   switch (what)
262     {
263     case locals:
264       name_of_result = "locals";
265       break;
266     case arguments:
267       name_of_result = "args";
268       break;
269     case all:
270       name_of_result = "variables";
271       break;
272     default:
273       internal_error (__FILE__, __LINE__,
274                       "unexpected what_to_list: %d", (int) what);
275     }
276
277   cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result);
278
279   while (block != 0)
280     {
281       ALL_BLOCK_SYMBOLS (block, iter, sym)
282         {
283           int print_me = 0;
284
285           switch (SYMBOL_CLASS (sym))
286             {
287             default:
288             case LOC_UNDEF:     /* catches errors        */
289             case LOC_CONST:     /* constant              */
290             case LOC_TYPEDEF:   /* local typedef         */
291             case LOC_LABEL:     /* local label           */
292             case LOC_BLOCK:     /* local function        */
293             case LOC_CONST_BYTES:       /* loc. byte seq.        */
294             case LOC_UNRESOLVED:        /* unresolved static     */
295             case LOC_OPTIMIZED_OUT:     /* optimized out         */
296               print_me = 0;
297               break;
298
299             case LOC_ARG:       /* argument              */
300             case LOC_REF_ARG:   /* reference arg         */
301             case LOC_REGPARM_ADDR:      /* indirect register arg */
302             case LOC_LOCAL:     /* stack local           */
303             case LOC_STATIC:    /* static                */
304             case LOC_REGISTER:  /* register              */
305             case LOC_COMPUTED:  /* computed location     */
306               if (what == all)
307                 print_me = 1;
308               else if (what == locals)
309                 print_me = !SYMBOL_IS_ARGUMENT (sym);
310               else
311                 print_me = SYMBOL_IS_ARGUMENT (sym);
312               break;
313             }
314           if (print_me)
315             {
316               struct cleanup *cleanup_tuple = NULL;
317               struct symbol *sym2;
318               struct value *val;
319
320               if (values != PRINT_NO_VALUES || what == all)
321                 cleanup_tuple =
322                   make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
323               ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym));
324               if (what == all && SYMBOL_IS_ARGUMENT (sym))
325                 ui_out_field_int (uiout, "arg", 1);
326
327               if (SYMBOL_IS_ARGUMENT (sym))
328                 sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym),
329                                       block, VAR_DOMAIN,
330                                       (int *) NULL);
331               else
332                 sym2 = sym;
333               switch (values)
334                 {
335                 case PRINT_SIMPLE_VALUES:
336                   type = check_typedef (sym2->type);
337                   type_print (sym2->type, "", stb->stream, -1);
338                   ui_out_field_stream (uiout, "type", stb);
339                   if (TYPE_CODE (type) != TYPE_CODE_ARRAY
340                       && TYPE_CODE (type) != TYPE_CODE_STRUCT
341                       && TYPE_CODE (type) != TYPE_CODE_UNION)
342                     {
343                       volatile struct gdb_exception except;
344
345                       TRY_CATCH (except, RETURN_MASK_ERROR)
346                         {
347                           struct value_print_options opts;
348
349                           val = read_var_value (sym2, fi);
350                           get_raw_print_options (&opts);
351                           opts.deref_ref = 1;
352                           common_val_print
353                             (val, stb->stream, 0, &opts,
354                              language_def (SYMBOL_LANGUAGE (sym2)));
355                         }
356                       if (except.reason < 0)
357                         fprintf_filtered (stb->stream,
358                                           _("<error reading variable: %s>"),
359                                           except.message);
360
361                       ui_out_field_stream (uiout, "value", stb);
362                     }
363                   break;
364                 case PRINT_ALL_VALUES:
365                   {
366                     volatile struct gdb_exception except;
367
368                     TRY_CATCH (except, RETURN_MASK_ERROR)
369                       {
370                         struct value_print_options opts;
371
372                         val = read_var_value (sym2, fi);
373                         get_raw_print_options (&opts);
374                         opts.deref_ref = 1;
375                         common_val_print
376                           (val, stb->stream, 0, &opts,
377                            language_def (SYMBOL_LANGUAGE (sym2)));
378                       }
379                     if (except.reason < 0)
380                       fprintf_filtered (stb->stream,
381                                         _("<error reading variable: %s>"),
382                                         except.message);
383
384                     ui_out_field_stream (uiout, "value", stb);
385                   }
386                   break;
387                 }
388
389               if (values != PRINT_NO_VALUES || what == all)
390                 do_cleanups (cleanup_tuple);
391             }
392         }
393       if (BLOCK_FUNCTION (block))
394         break;
395       else
396         block = BLOCK_SUPERBLOCK (block);
397     }
398   do_cleanups (cleanup_list);
399   ui_out_stream_delete (stb);
400 }
401
402 void
403 mi_cmd_stack_select_frame (char *command, char **argv, int argc)
404 {
405   if (argc == 0 || argc > 1)
406     error (_("-stack-select-frame: Usage: FRAME_SPEC"));
407
408   select_frame_command (argv[0], 1 /* not used */ );
409 }
410
411 void
412 mi_cmd_stack_info_frame (char *command, char **argv, int argc)
413 {
414   if (argc > 0)
415     error (_("-stack-info-frame: No arguments required"));
416
417   print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0);
418 }