Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - stack commands. |
6aba47ca DJ |
2 | Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007 |
3 | Free Software Foundation, 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 | |
10 | the Free Software Foundation; either version 2 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, write to the Free Software | |
1caae165 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
fb40c209 AC |
22 | |
23 | #include "defs.h" | |
24 | #include "target.h" | |
25 | #include "frame.h" | |
26 | #include "value.h" | |
27 | #include "mi-cmds.h" | |
28 | #include "ui-out.h" | |
e88c90f2 | 29 | #include "symtab.h" |
fe898f56 | 30 | #include "block.h" |
b9362cc7 | 31 | #include "stack.h" |
de4f826b | 32 | #include "dictionary.h" |
f5ec2042 | 33 | #include "gdb_string.h" |
fb40c209 AC |
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 | enum mi_cmd_result | |
43 | mi_cmd_stack_list_frames (char *command, char **argv, int argc) | |
44 | { | |
45 | int frame_low; | |
46 | int frame_high; | |
47 | int i; | |
6ad4a2cf | 48 | struct cleanup *cleanup_stack; |
fb40c209 AC |
49 | struct frame_info *fi; |
50 | ||
fb40c209 | 51 | if (argc > 2 || argc == 1) |
8a3fe4f8 | 52 | error (_("mi_cmd_stack_list_frames: Usage: [FRAME_LOW FRAME_HIGH]")); |
fb40c209 AC |
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) | |
8a3fe4f8 | 75 | error (_("mi_cmd_stack_list_frames: Not enough frames in stack.")); |
fb40c209 | 76 | |
6ad4a2cf | 77 | cleanup_stack = make_cleanup_ui_out_list_begin_end (uiout, "stack"); |
fb40c209 AC |
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; | |
0faf0076 | 86 | /* Print the location and the address always, even for level 0. |
fb40c209 | 87 | args == 0: don't print the arguments. */ |
0faf0076 | 88 | print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ ); |
fb40c209 AC |
89 | } |
90 | ||
6ad4a2cf | 91 | do_cleanups (cleanup_stack); |
fb40c209 AC |
92 | |
93 | return MI_CMD_DONE; | |
94 | } | |
95 | ||
96 | enum mi_cmd_result | |
97 | mi_cmd_stack_info_depth (char *command, char **argv, int argc) | |
98 | { | |
99 | int frame_high; | |
100 | int i; | |
101 | struct frame_info *fi; | |
102 | ||
fb40c209 | 103 | if (argc > 1) |
8a3fe4f8 | 104 | error (_("mi_cmd_stack_info_depth: Usage: [MAX_DEPTH]")); |
fb40c209 AC |
105 | |
106 | if (argc == 1) | |
107 | frame_high = atoi (argv[0]); | |
108 | else | |
109 | /* Called with no arguments, it means we want the real depth of | |
110 | the stack. */ | |
111 | frame_high = -1; | |
112 | ||
113 | for (i = 0, fi = get_current_frame (); | |
114 | fi && (i < frame_high || frame_high == -1); | |
115 | i++, fi = get_prev_frame (fi)) | |
116 | QUIT; | |
117 | ||
118 | ui_out_field_int (uiout, "depth", i); | |
119 | ||
120 | return MI_CMD_DONE; | |
121 | } | |
122 | ||
123 | /* Print a list of the locals for the current frame. With argument of | |
124 | 0, print only the names, with argument of 1 print also the | |
125 | values. */ | |
126 | enum mi_cmd_result | |
127 | mi_cmd_stack_list_locals (char *command, char **argv, int argc) | |
128 | { | |
f5ec2042 NR |
129 | struct frame_info *frame; |
130 | enum print_values print_values; | |
131 | ||
fb40c209 | 132 | if (argc != 1) |
8a3fe4f8 | 133 | error (_("mi_cmd_stack_list_locals: Usage: PRINT_VALUES")); |
fb40c209 | 134 | |
b04f3ab4 | 135 | frame = get_selected_frame (NULL); |
f5ec2042 NR |
136 | |
137 | if (strcmp (argv[0], "0") == 0 | |
1ecb4ee0 | 138 | || strcmp (argv[0], mi_no_values) == 0) |
f5ec2042 NR |
139 | print_values = PRINT_NO_VALUES; |
140 | else if (strcmp (argv[0], "1") == 0 | |
1ecb4ee0 | 141 | || strcmp (argv[0], mi_all_values) == 0) |
f5ec2042 NR |
142 | print_values = PRINT_ALL_VALUES; |
143 | else if (strcmp (argv[0], "2") == 0 | |
1ecb4ee0 | 144 | || strcmp (argv[0], mi_simple_values) == 0) |
f5ec2042 NR |
145 | print_values = PRINT_SIMPLE_VALUES; |
146 | else | |
1ecb4ee0 DJ |
147 | error (_("Unknown value for PRINT_VALUES: must be: \ |
148 | 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""), | |
149 | mi_no_values, mi_all_values, mi_simple_values); | |
f5ec2042 | 150 | list_args_or_locals (1, print_values, frame); |
fb40c209 AC |
151 | return MI_CMD_DONE; |
152 | } | |
153 | ||
154 | /* Print a list of the arguments for the current frame. With argument | |
155 | of 0, print only the names, with argument of 1 print also the | |
156 | values. */ | |
157 | enum mi_cmd_result | |
158 | mi_cmd_stack_list_args (char *command, char **argv, int argc) | |
159 | { | |
160 | int frame_low; | |
161 | int frame_high; | |
162 | int i; | |
163 | struct frame_info *fi; | |
6ad4a2cf | 164 | struct cleanup *cleanup_stack_args; |
fb40c209 AC |
165 | |
166 | if (argc < 1 || argc > 3 || argc == 2) | |
8a3fe4f8 | 167 | error (_("mi_cmd_stack_list_args: Usage: PRINT_VALUES [FRAME_LOW FRAME_HIGH]")); |
fb40c209 AC |
168 | |
169 | if (argc == 3) | |
170 | { | |
171 | frame_low = atoi (argv[1]); | |
172 | frame_high = atoi (argv[2]); | |
173 | } | |
174 | else | |
175 | { | |
176 | /* Called with no arguments, it means we want args for the whole | |
177 | backtrace. */ | |
178 | frame_low = -1; | |
179 | frame_high = -1; | |
180 | } | |
181 | ||
182 | /* Let's position fi on the frame at which to start the | |
183 | display. Could be the innermost frame if the whole stack needs | |
184 | displaying, or if frame_low is 0. */ | |
185 | for (i = 0, fi = get_current_frame (); | |
186 | fi && i < frame_low; | |
187 | i++, fi = get_prev_frame (fi)); | |
188 | ||
189 | if (fi == NULL) | |
8a3fe4f8 | 190 | error (_("mi_cmd_stack_list_args: Not enough frames in stack.")); |
fb40c209 | 191 | |
6ad4a2cf | 192 | cleanup_stack_args = make_cleanup_ui_out_list_begin_end (uiout, "stack-args"); |
fb40c209 AC |
193 | |
194 | /* Now let's print the frames up to frame_high, or until there are | |
195 | frames in the stack. */ | |
196 | for (; | |
197 | fi && (i <= frame_high || frame_high == -1); | |
198 | i++, fi = get_prev_frame (fi)) | |
199 | { | |
6ad4a2cf | 200 | struct cleanup *cleanup_frame; |
fb40c209 | 201 | QUIT; |
6ad4a2cf | 202 | cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame"); |
fb40c209 AC |
203 | ui_out_field_int (uiout, "level", i); |
204 | list_args_or_locals (0, atoi (argv[0]), fi); | |
6ad4a2cf | 205 | do_cleanups (cleanup_frame); |
fb40c209 AC |
206 | } |
207 | ||
6ad4a2cf | 208 | do_cleanups (cleanup_stack_args); |
fb40c209 AC |
209 | |
210 | return MI_CMD_DONE; | |
211 | } | |
212 | ||
213 | /* Print a list of the locals or the arguments for the currently | |
214 | selected frame. If the argument passed is 0, printonly the names | |
215 | of the variables, if an argument of 1 is passed, print the values | |
216 | as well. */ | |
217 | static void | |
218 | list_args_or_locals (int locals, int values, struct frame_info *fi) | |
219 | { | |
220 | struct block *block; | |
221 | struct symbol *sym; | |
de4f826b DC |
222 | struct dict_iterator iter; |
223 | int nsyms; | |
6ad4a2cf | 224 | struct cleanup *cleanup_list; |
fb40c209 | 225 | static struct ui_stream *stb = NULL; |
f5ec2042 | 226 | struct type *type; |
fb40c209 AC |
227 | |
228 | stb = ui_out_stream_new (uiout); | |
229 | ||
ae767bfb | 230 | block = get_frame_block (fi, 0); |
fb40c209 | 231 | |
6ad4a2cf | 232 | cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, locals ? "locals" : "args"); |
fb40c209 AC |
233 | |
234 | while (block != 0) | |
235 | { | |
de4f826b | 236 | ALL_BLOCK_SYMBOLS (block, iter, sym) |
fb40c209 | 237 | { |
39bf4652 JB |
238 | int print_me = 0; |
239 | ||
fb40c209 AC |
240 | switch (SYMBOL_CLASS (sym)) |
241 | { | |
242 | default: | |
243 | case LOC_UNDEF: /* catches errors */ | |
244 | case LOC_CONST: /* constant */ | |
245 | case LOC_TYPEDEF: /* local typedef */ | |
246 | case LOC_LABEL: /* local label */ | |
247 | case LOC_BLOCK: /* local function */ | |
248 | case LOC_CONST_BYTES: /* loc. byte seq. */ | |
249 | case LOC_UNRESOLVED: /* unresolved static */ | |
250 | case LOC_OPTIMIZED_OUT: /* optimized out */ | |
251 | print_me = 0; | |
252 | break; | |
253 | ||
254 | case LOC_ARG: /* argument */ | |
255 | case LOC_REF_ARG: /* reference arg */ | |
256 | case LOC_REGPARM: /* register arg */ | |
257 | case LOC_REGPARM_ADDR: /* indirect register arg */ | |
258 | case LOC_LOCAL_ARG: /* stack arg */ | |
259 | case LOC_BASEREG_ARG: /* basereg arg */ | |
4cf623b6 | 260 | case LOC_COMPUTED_ARG: /* arg with computed location */ |
fb40c209 AC |
261 | if (!locals) |
262 | print_me = 1; | |
263 | break; | |
264 | ||
265 | case LOC_LOCAL: /* stack local */ | |
266 | case LOC_BASEREG: /* basereg local */ | |
267 | case LOC_STATIC: /* static */ | |
268 | case LOC_REGISTER: /* register */ | |
4cf623b6 | 269 | case LOC_COMPUTED: /* computed location */ |
fb40c209 AC |
270 | if (locals) |
271 | print_me = 1; | |
272 | break; | |
273 | } | |
274 | if (print_me) | |
275 | { | |
6ad4a2cf | 276 | struct cleanup *cleanup_tuple = NULL; |
6bb0384f | 277 | struct symbol *sym2; |
9fbcbb40 | 278 | struct value *val; |
f5ec2042 NR |
279 | if (values != PRINT_NO_VALUES) |
280 | cleanup_tuple = | |
6ad4a2cf | 281 | make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
f5ec2042 | 282 | ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym)); |
fb40c209 | 283 | |
f5ec2042 NR |
284 | if (!locals) |
285 | sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym), | |
286 | block, VAR_DOMAIN, | |
287 | (int *) NULL, | |
288 | (struct symtab **) NULL); | |
289 | else | |
fb40c209 | 290 | sym2 = sym; |
f5ec2042 NR |
291 | switch (values) |
292 | { | |
293 | case PRINT_SIMPLE_VALUES: | |
294 | type = check_typedef (sym2->type); | |
295 | type_print (sym2->type, "", stb->stream, -1); | |
296 | ui_out_field_stream (uiout, "type", stb); | |
297 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
298 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
299 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
300 | { | |
9fbcbb40 NR |
301 | val = read_var_value (sym2, fi); |
302 | common_val_print | |
303 | (val, stb->stream, 0, 1, 0, Val_no_prettyprint); | |
f5ec2042 NR |
304 | ui_out_field_stream (uiout, "value", stb); |
305 | } | |
306 | do_cleanups (cleanup_tuple); | |
307 | break; | |
308 | case PRINT_ALL_VALUES: | |
9fbcbb40 NR |
309 | val = read_var_value (sym2, fi); |
310 | common_val_print | |
311 | (val, stb->stream, 0, 1, 0, Val_no_prettyprint); | |
fb40c209 | 312 | ui_out_field_stream (uiout, "value", stb); |
6ad4a2cf | 313 | do_cleanups (cleanup_tuple); |
f5ec2042 | 314 | break; |
fb40c209 AC |
315 | } |
316 | } | |
317 | } | |
318 | if (BLOCK_FUNCTION (block)) | |
319 | break; | |
320 | else | |
321 | block = BLOCK_SUPERBLOCK (block); | |
322 | } | |
6ad4a2cf | 323 | do_cleanups (cleanup_list); |
fb40c209 AC |
324 | ui_out_stream_delete (stb); |
325 | } | |
326 | ||
327 | enum mi_cmd_result | |
328 | mi_cmd_stack_select_frame (char *command, char **argv, int argc) | |
329 | { | |
fcf43932 NR |
330 | if (argc == 0 || argc > 1) |
331 | error (_("mi_cmd_stack_select_frame: Usage: FRAME_SPEC")); | |
fb40c209 | 332 | |
fcf43932 | 333 | select_frame_command (argv[0], 1 /* not used */ ); |
fb40c209 AC |
334 | return MI_CMD_DONE; |
335 | } | |
64fd8944 NR |
336 | |
337 | enum mi_cmd_result | |
338 | mi_cmd_stack_info_frame (char *command, char **argv, int argc) | |
339 | { | |
340 | if (argc > 0) | |
341 | error (_("mi_cmd_stack_info_frame: No arguments required")); | |
342 | ||
343 | print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0); | |
344 | return MI_CMD_DONE; | |
345 | } |