Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - stack commands. |
7b6bb8da | 2 | Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 |
6aba47ca | 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 | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
fb40c209 AC |
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 | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fb40c209 AC |
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" | |
e88c90f2 | 27 | #include "symtab.h" |
fe898f56 | 28 | #include "block.h" |
b9362cc7 | 29 | #include "stack.h" |
de4f826b | 30 | #include "dictionary.h" |
f5ec2042 | 31 | #include "gdb_string.h" |
d8ca156b | 32 | #include "language.h" |
79a45b7d | 33 | #include "valprint.h" |
875b4ff5 | 34 | #include "exceptions.h" |
daf3c977 VP |
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); | |
fb40c209 AC |
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. */ | |
ce8f13f8 | 46 | void |
fb40c209 AC |
47 | mi_cmd_stack_list_frames (char *command, char **argv, int argc) |
48 | { | |
49 | int frame_low; | |
50 | int frame_high; | |
51 | int i; | |
6ad4a2cf | 52 | struct cleanup *cleanup_stack; |
fb40c209 AC |
53 | struct frame_info *fi; |
54 | ||
fb40c209 | 55 | if (argc > 2 || argc == 1) |
1b05df00 | 56 | error (_("-stack-list-frames: Usage: [FRAME_LOW FRAME_HIGH]")); |
fb40c209 AC |
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) | |
1b05df00 | 79 | error (_("-stack-list-frames: Not enough frames in stack.")); |
fb40c209 | 80 | |
79a45e25 | 81 | cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack"); |
fb40c209 AC |
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; | |
0faf0076 | 90 | /* Print the location and the address always, even for level 0. |
fb40c209 | 91 | args == 0: don't print the arguments. */ |
0faf0076 | 92 | print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */ ); |
fb40c209 AC |
93 | } |
94 | ||
6ad4a2cf | 95 | do_cleanups (cleanup_stack); |
fb40c209 AC |
96 | } |
97 | ||
ce8f13f8 | 98 | void |
fb40c209 AC |
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 | ||
fb40c209 | 105 | if (argc > 1) |
1b05df00 | 106 | error (_("-stack-info-depth: Usage: [MAX_DEPTH]")); |
fb40c209 AC |
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 | ||
79a45e25 | 120 | ui_out_field_int (current_uiout, "depth", i); |
fb40c209 AC |
121 | } |
122 | ||
8b777f02 VP |
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 | ||
7a93fb82 | 141 | /* Print a list of the locals for the current frame. With argument of |
fb40c209 AC |
142 | 0, print only the names, with argument of 1 print also the |
143 | values. */ | |
ce8f13f8 | 144 | void |
fb40c209 AC |
145 | mi_cmd_stack_list_locals (char *command, char **argv, int argc) |
146 | { | |
f5ec2042 | 147 | struct frame_info *frame; |
f5ec2042 | 148 | |
fb40c209 | 149 | if (argc != 1) |
1b05df00 | 150 | error (_("-stack-list-locals: Usage: PRINT_VALUES")); |
fb40c209 | 151 | |
b04f3ab4 | 152 | frame = get_selected_frame (NULL); |
f5ec2042 | 153 | |
daf3c977 | 154 | list_args_or_locals (locals, parse_print_values (argv[0]), frame); |
fb40c209 AC |
155 | } |
156 | ||
7a93fb82 | 157 | /* Print a list of the arguments for the current frame. With argument |
fb40c209 AC |
158 | of 0, print only the names, with argument of 1 print also the |
159 | values. */ | |
ce8f13f8 | 160 | void |
fb40c209 AC |
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; | |
6ad4a2cf | 167 | struct cleanup *cleanup_stack_args; |
8b777f02 | 168 | enum print_values print_values; |
79a45e25 | 169 | struct ui_out *uiout = current_uiout; |
fb40c209 AC |
170 | |
171 | if (argc < 1 || argc > 3 || argc == 2) | |
1b05df00 | 172 | error (_("-stack-list-arguments: Usage: " |
9a2b4c1b | 173 | "PRINT_VALUES [FRAME_LOW FRAME_HIGH]")); |
fb40c209 AC |
174 | |
175 | if (argc == 3) | |
176 | { | |
177 | frame_low = atoi (argv[1]); | |
178 | frame_high = atoi (argv[2]); | |
179 | } | |
180 | else | |
181 | { | |
182 | /* Called with no arguments, it means we want args for the whole | |
183 | backtrace. */ | |
184 | frame_low = -1; | |
185 | frame_high = -1; | |
186 | } | |
187 | ||
8b777f02 VP |
188 | print_values = parse_print_values (argv[0]); |
189 | ||
fb40c209 AC |
190 | /* Let's position fi on the frame at which to start the |
191 | display. Could be the innermost frame if the whole stack needs | |
192 | displaying, or if frame_low is 0. */ | |
193 | for (i = 0, fi = get_current_frame (); | |
194 | fi && i < frame_low; | |
195 | i++, fi = get_prev_frame (fi)); | |
196 | ||
197 | if (fi == NULL) | |
1b05df00 | 198 | error (_("-stack-list-arguments: Not enough frames in stack.")); |
fb40c209 | 199 | |
9a2b4c1b MS |
200 | cleanup_stack_args |
201 | = make_cleanup_ui_out_list_begin_end (uiout, "stack-args"); | |
fb40c209 AC |
202 | |
203 | /* Now let's print the frames up to frame_high, or until there are | |
204 | frames in the stack. */ | |
205 | for (; | |
206 | fi && (i <= frame_high || frame_high == -1); | |
207 | i++, fi = get_prev_frame (fi)) | |
208 | { | |
6ad4a2cf | 209 | struct cleanup *cleanup_frame; |
102040f0 | 210 | |
fb40c209 | 211 | QUIT; |
6ad4a2cf | 212 | cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame"); |
fb40c209 | 213 | ui_out_field_int (uiout, "level", i); |
daf3c977 | 214 | list_args_or_locals (arguments, print_values, fi); |
6ad4a2cf | 215 | do_cleanups (cleanup_frame); |
fb40c209 AC |
216 | } |
217 | ||
6ad4a2cf | 218 | do_cleanups (cleanup_stack_args); |
fb40c209 AC |
219 | } |
220 | ||
daf3c977 | 221 | /* Print a list of the local variables (including arguments) for the |
7a93fb82 VP |
222 | current frame. ARGC must be 1 and ARGV[0] specify if only the names, |
223 | or both names and values of the variables must be printed. See | |
224 | parse_print_value for possible values. */ | |
daf3c977 VP |
225 | void |
226 | mi_cmd_stack_list_variables (char *command, char **argv, int argc) | |
227 | { | |
228 | struct frame_info *frame; | |
daf3c977 VP |
229 | |
230 | if (argc != 1) | |
231 | error (_("Usage: PRINT_VALUES")); | |
232 | ||
7a93fb82 | 233 | frame = get_selected_frame (NULL); |
daf3c977 | 234 | |
7a93fb82 | 235 | list_args_or_locals (all, parse_print_values (argv[0]), frame); |
daf3c977 VP |
236 | } |
237 | ||
238 | ||
fb40c209 AC |
239 | /* Print a list of the locals or the arguments for the currently |
240 | selected frame. If the argument passed is 0, printonly the names | |
241 | of the variables, if an argument of 1 is passed, print the values | |
242 | as well. */ | |
243 | static void | |
daf3c977 | 244 | list_args_or_locals (enum what_to_list what, int values, struct frame_info *fi) |
fb40c209 AC |
245 | { |
246 | struct block *block; | |
247 | struct symbol *sym; | |
de4f826b | 248 | struct dict_iterator iter; |
6ad4a2cf | 249 | struct cleanup *cleanup_list; |
cb0fd152 | 250 | struct ui_stream *stb; |
f5ec2042 | 251 | struct type *type; |
daf3c977 | 252 | char *name_of_result; |
79a45e25 | 253 | struct ui_out *uiout = current_uiout; |
fb40c209 AC |
254 | |
255 | stb = ui_out_stream_new (uiout); | |
256 | ||
ae767bfb | 257 | block = get_frame_block (fi, 0); |
fb40c209 | 258 | |
daf3c977 VP |
259 | switch (what) |
260 | { | |
261 | case locals: | |
d6fd4674 PA |
262 | name_of_result = "locals"; |
263 | break; | |
daf3c977 | 264 | case arguments: |
d6fd4674 PA |
265 | name_of_result = "args"; |
266 | break; | |
daf3c977 | 267 | case all: |
d6fd4674 PA |
268 | name_of_result = "variables"; |
269 | break; | |
654e7c1f | 270 | default: |
d6fd4674 PA |
271 | internal_error (__FILE__, __LINE__, |
272 | "unexpected what_to_list: %d", (int) what); | |
daf3c977 VP |
273 | } |
274 | ||
275 | cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result); | |
fb40c209 AC |
276 | |
277 | while (block != 0) | |
278 | { | |
de4f826b | 279 | ALL_BLOCK_SYMBOLS (block, iter, sym) |
fb40c209 | 280 | { |
39bf4652 JB |
281 | int print_me = 0; |
282 | ||
fb40c209 AC |
283 | switch (SYMBOL_CLASS (sym)) |
284 | { | |
285 | default: | |
286 | case LOC_UNDEF: /* catches errors */ | |
287 | case LOC_CONST: /* constant */ | |
288 | case LOC_TYPEDEF: /* local typedef */ | |
289 | case LOC_LABEL: /* local label */ | |
290 | case LOC_BLOCK: /* local function */ | |
291 | case LOC_CONST_BYTES: /* loc. byte seq. */ | |
292 | case LOC_UNRESOLVED: /* unresolved static */ | |
293 | case LOC_OPTIMIZED_OUT: /* optimized out */ | |
294 | print_me = 0; | |
295 | break; | |
296 | ||
297 | case LOC_ARG: /* argument */ | |
298 | case LOC_REF_ARG: /* reference arg */ | |
fb40c209 | 299 | case LOC_REGPARM_ADDR: /* indirect register arg */ |
fb40c209 | 300 | case LOC_LOCAL: /* stack local */ |
fb40c209 AC |
301 | case LOC_STATIC: /* static */ |
302 | case LOC_REGISTER: /* register */ | |
4cf623b6 | 303 | case LOC_COMPUTED: /* computed location */ |
daf3c977 | 304 | if (what == all) |
fb40c209 | 305 | print_me = 1; |
daf3c977 VP |
306 | else if (what == locals) |
307 | print_me = !SYMBOL_IS_ARGUMENT (sym); | |
308 | else | |
309 | print_me = SYMBOL_IS_ARGUMENT (sym); | |
fb40c209 AC |
310 | break; |
311 | } | |
312 | if (print_me) | |
313 | { | |
6ad4a2cf | 314 | struct cleanup *cleanup_tuple = NULL; |
6bb0384f | 315 | struct symbol *sym2; |
9fbcbb40 | 316 | struct value *val; |
102040f0 | 317 | |
7a93fb82 | 318 | if (values != PRINT_NO_VALUES || what == all) |
f5ec2042 | 319 | cleanup_tuple = |
6ad4a2cf | 320 | make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
f5ec2042 | 321 | ui_out_field_string (uiout, "name", SYMBOL_PRINT_NAME (sym)); |
7a93fb82 VP |
322 | if (what == all && SYMBOL_IS_ARGUMENT (sym)) |
323 | ui_out_field_int (uiout, "arg", 1); | |
fb40c209 | 324 | |
daf3c977 | 325 | if (SYMBOL_IS_ARGUMENT (sym)) |
f5ec2042 NR |
326 | sym2 = lookup_symbol (SYMBOL_NATURAL_NAME (sym), |
327 | block, VAR_DOMAIN, | |
2570f2b7 | 328 | (int *) NULL); |
f5ec2042 | 329 | else |
2a2d4dc3 | 330 | sym2 = sym; |
f5ec2042 NR |
331 | switch (values) |
332 | { | |
333 | case PRINT_SIMPLE_VALUES: | |
334 | type = check_typedef (sym2->type); | |
335 | type_print (sym2->type, "", stb->stream, -1); | |
336 | ui_out_field_stream (uiout, "type", stb); | |
337 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
338 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
339 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
340 | { | |
875b4ff5 PM |
341 | volatile struct gdb_exception except; |
342 | ||
343 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
344 | { | |
345 | struct value_print_options opts; | |
346 | ||
347 | val = read_var_value (sym2, fi); | |
348 | get_raw_print_options (&opts); | |
349 | opts.deref_ref = 1; | |
350 | common_val_print | |
351 | (val, stb->stream, 0, &opts, | |
352 | language_def (SYMBOL_LANGUAGE (sym2))); | |
353 | } | |
354 | if (except.reason < 0) | |
355 | fprintf_filtered (stb->stream, | |
356 | _("<error reading variable: %s>"), | |
357 | except.message); | |
358 | ||
f5ec2042 NR |
359 | ui_out_field_stream (uiout, "value", stb); |
360 | } | |
f5ec2042 NR |
361 | break; |
362 | case PRINT_ALL_VALUES: | |
79a45b7d | 363 | { |
875b4ff5 PM |
364 | volatile struct gdb_exception except; |
365 | ||
366 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
367 | { | |
368 | struct value_print_options opts; | |
369 | ||
370 | val = read_var_value (sym2, fi); | |
371 | get_raw_print_options (&opts); | |
372 | opts.deref_ref = 1; | |
373 | common_val_print | |
374 | (val, stb->stream, 0, &opts, | |
375 | language_def (SYMBOL_LANGUAGE (sym2))); | |
376 | } | |
377 | if (except.reason < 0) | |
378 | fprintf_filtered (stb->stream, | |
379 | _("<error reading variable: %s>"), | |
380 | except.message); | |
381 | ||
79a45b7d | 382 | ui_out_field_stream (uiout, "value", stb); |
79a45b7d | 383 | } |
f5ec2042 | 384 | break; |
fb40c209 | 385 | } |
7a93fb82 VP |
386 | |
387 | if (values != PRINT_NO_VALUES || what == all) | |
388 | do_cleanups (cleanup_tuple); | |
fb40c209 AC |
389 | } |
390 | } | |
391 | if (BLOCK_FUNCTION (block)) | |
392 | break; | |
393 | else | |
394 | block = BLOCK_SUPERBLOCK (block); | |
395 | } | |
6ad4a2cf | 396 | do_cleanups (cleanup_list); |
fb40c209 AC |
397 | ui_out_stream_delete (stb); |
398 | } | |
399 | ||
ce8f13f8 | 400 | void |
fb40c209 AC |
401 | mi_cmd_stack_select_frame (char *command, char **argv, int argc) |
402 | { | |
fcf43932 | 403 | if (argc == 0 || argc > 1) |
1b05df00 | 404 | error (_("-stack-select-frame: Usage: FRAME_SPEC")); |
fb40c209 | 405 | |
fcf43932 | 406 | select_frame_command (argv[0], 1 /* not used */ ); |
fb40c209 | 407 | } |
64fd8944 | 408 | |
ce8f13f8 | 409 | void |
64fd8944 NR |
410 | mi_cmd_stack_info_frame (char *command, char **argv, int argc) |
411 | { | |
412 | if (argc > 0) | |
1b05df00 | 413 | error (_("-stack-info-frame: No arguments required")); |
ce8f13f8 | 414 | |
64fd8944 | 415 | print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0); |
64fd8944 | 416 | } |