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