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