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