GDB (API)
|
00001 /* MI Command Set - stack commands. 00002 Copyright (C) 2000-2013 Free Software Foundation, Inc. 00003 Contributed by Cygnus Solutions (a Red Hat company). 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "target.h" 00022 #include "frame.h" 00023 #include "value.h" 00024 #include "mi-cmds.h" 00025 #include "ui-out.h" 00026 #include "symtab.h" 00027 #include "block.h" 00028 #include "stack.h" 00029 #include "dictionary.h" 00030 #include "gdb_string.h" 00031 #include "language.h" 00032 #include "valprint.h" 00033 #include "exceptions.h" 00034 #include "utils.h" 00035 #include "mi-getopt.h" 00036 #include "python/python.h" 00037 #include <ctype.h> 00038 #include "mi-parse.h" 00039 00040 enum what_to_list { locals, arguments, all }; 00041 00042 static void list_args_or_locals (enum what_to_list what, 00043 enum print_values values, 00044 struct frame_info *fi, 00045 int skip_unavailable); 00046 00047 /* True if we want to allow Python-based frame filters. */ 00048 static int frame_filters = 0; 00049 00050 void 00051 mi_cmd_enable_frame_filters (char *command, char **argv, int argc) 00052 { 00053 if (argc != 0) 00054 error (_("-enable-frame-filters: no arguments allowed")); 00055 frame_filters = 1; 00056 } 00057 00058 /* Print a list of the stack frames. Args can be none, in which case 00059 we want to print the whole backtrace, or a pair of numbers 00060 specifying the frame numbers at which to start and stop the 00061 display. If the two numbers are equal, a single frame will be 00062 displayed. */ 00063 00064 void 00065 mi_cmd_stack_list_frames (char *command, char **argv, int argc) 00066 { 00067 int frame_low; 00068 int frame_high; 00069 int i; 00070 struct cleanup *cleanup_stack; 00071 struct frame_info *fi; 00072 enum py_bt_status result = PY_BT_ERROR; 00073 int raw_arg = 0; 00074 int oind = 0; 00075 enum opt 00076 { 00077 NO_FRAME_FILTERS 00078 }; 00079 static const struct mi_opt opts[] = 00080 { 00081 {"-no-frame-filters", NO_FRAME_FILTERS, 0}, 00082 { 0, 0, 0 } 00083 }; 00084 00085 /* Parse arguments. In this instance we are just looking for 00086 --no-frame-filters. */ 00087 while (1) 00088 { 00089 char *oarg; 00090 int opt = mi_getopt ("-stack-list-frames", argc, argv, 00091 opts, &oind, &oarg); 00092 if (opt < 0) 00093 break; 00094 switch ((enum opt) opt) 00095 { 00096 case NO_FRAME_FILTERS: 00097 raw_arg = oind; 00098 break; 00099 } 00100 } 00101 00102 /* After the last option is parsed, there should either be low - 00103 high range, or no further arguments. */ 00104 if ((argc - oind != 0) && (argc - oind != 2)) 00105 error (_("-stack-list-frames: Usage: [--no-frame-filters] [FRAME_LOW FRAME_HIGH]")); 00106 00107 /* If there is a range, set it. */ 00108 if (argc - oind == 2) 00109 { 00110 frame_low = atoi (argv[0 + oind]); 00111 frame_high = atoi (argv[1 + oind]); 00112 } 00113 else 00114 { 00115 /* Called with no arguments, it means we want the whole 00116 backtrace. */ 00117 frame_low = -1; 00118 frame_high = -1; 00119 } 00120 00121 /* Let's position fi on the frame at which to start the 00122 display. Could be the innermost frame if the whole stack needs 00123 displaying, or if frame_low is 0. */ 00124 for (i = 0, fi = get_current_frame (); 00125 fi && i < frame_low; 00126 i++, fi = get_prev_frame (fi)); 00127 00128 if (fi == NULL) 00129 error (_("-stack-list-frames: Not enough frames in stack.")); 00130 00131 cleanup_stack = make_cleanup_ui_out_list_begin_end (current_uiout, "stack"); 00132 00133 if (! raw_arg && frame_filters) 00134 { 00135 int flags = PRINT_LEVEL | PRINT_FRAME_INFO; 00136 int py_frame_low = frame_low; 00137 00138 /* We cannot pass -1 to frame_low, as that would signify a 00139 relative backtrace from the tail of the stack. So, in the case 00140 of frame_low == -1, assign and increment it. */ 00141 if (py_frame_low == -1) 00142 py_frame_low++; 00143 00144 result = apply_frame_filter (get_current_frame (), flags, 00145 NO_VALUES, current_uiout, 00146 py_frame_low, frame_high); 00147 } 00148 00149 /* Run the inbuilt backtrace if there are no filters registered, or 00150 if "--no-frame-filters" has been specified from the command. */ 00151 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS) 00152 { 00153 /* Now let's print the frames up to frame_high, or until there are 00154 frames in the stack. */ 00155 for (; 00156 fi && (i <= frame_high || frame_high == -1); 00157 i++, fi = get_prev_frame (fi)) 00158 { 00159 QUIT; 00160 /* Print the location and the address always, even for level 0. 00161 If args is 0, don't print the arguments. */ 00162 print_frame_info (fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0); 00163 } 00164 } 00165 00166 do_cleanups (cleanup_stack); 00167 } 00168 00169 void 00170 mi_cmd_stack_info_depth (char *command, char **argv, int argc) 00171 { 00172 int frame_high; 00173 int i; 00174 struct frame_info *fi; 00175 00176 if (argc > 1) 00177 error (_("-stack-info-depth: Usage: [MAX_DEPTH]")); 00178 00179 if (argc == 1) 00180 frame_high = atoi (argv[0]); 00181 else 00182 /* Called with no arguments, it means we want the real depth of 00183 the stack. */ 00184 frame_high = -1; 00185 00186 for (i = 0, fi = get_current_frame (); 00187 fi && (i < frame_high || frame_high == -1); 00188 i++, fi = get_prev_frame (fi)) 00189 QUIT; 00190 00191 ui_out_field_int (current_uiout, "depth", i); 00192 } 00193 00194 /* Print a list of the locals for the current frame. With argument of 00195 0, print only the names, with argument of 1 print also the 00196 values. */ 00197 00198 void 00199 mi_cmd_stack_list_locals (char *command, char **argv, int argc) 00200 { 00201 struct frame_info *frame; 00202 int raw_arg = 0; 00203 enum py_bt_status result = PY_BT_ERROR; 00204 int print_value; 00205 int oind = 0; 00206 int skip_unavailable = 0; 00207 int i; 00208 00209 if (argc > 1) 00210 { 00211 int i; 00212 enum opt 00213 { 00214 NO_FRAME_FILTERS, 00215 SKIP_UNAVAILABLE, 00216 }; 00217 static const struct mi_opt opts[] = 00218 { 00219 {"-no-frame-filters", NO_FRAME_FILTERS, 0}, 00220 {"-skip-unavailable", SKIP_UNAVAILABLE, 0}, 00221 { 0, 0, 0 } 00222 }; 00223 00224 while (1) 00225 { 00226 char *oarg; 00227 /* Don't parse 'print-values' as an option. */ 00228 int opt = mi_getopt ("-stack-list-locals", argc - 1, argv, 00229 opts, &oind, &oarg); 00230 00231 if (opt < 0) 00232 break; 00233 switch ((enum opt) opt) 00234 { 00235 case NO_FRAME_FILTERS: 00236 raw_arg = oind; 00237 case SKIP_UNAVAILABLE: 00238 skip_unavailable = 1; 00239 break; 00240 } 00241 } 00242 } 00243 00244 /* After the last option is parsed, there should be only 00245 'print-values'. */ 00246 if (argc - oind != 1) 00247 error (_("-stack-list-locals: Usage: [--no-frame-filters] " 00248 "[--skip-unavailable] PRINT_VALUES")); 00249 00250 frame = get_selected_frame (NULL); 00251 print_value = mi_parse_print_values (argv[oind]); 00252 00253 if (! raw_arg && frame_filters) 00254 { 00255 int flags = PRINT_LEVEL | PRINT_LOCALS; 00256 00257 result = apply_frame_filter (frame, flags, print_value, 00258 current_uiout, 0, 0); 00259 } 00260 00261 /* Run the inbuilt backtrace if there are no filters registered, or 00262 if "--no-frame-filters" has been specified from the command. */ 00263 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS) 00264 { 00265 list_args_or_locals (locals, print_value, frame, 00266 skip_unavailable); 00267 } 00268 } 00269 00270 /* Print a list of the arguments for the current frame. With argument 00271 of 0, print only the names, with argument of 1 print also the 00272 values. */ 00273 00274 void 00275 mi_cmd_stack_list_args (char *command, char **argv, int argc) 00276 { 00277 int frame_low; 00278 int frame_high; 00279 int i; 00280 struct frame_info *fi; 00281 struct cleanup *cleanup_stack_args; 00282 enum print_values print_values; 00283 struct ui_out *uiout = current_uiout; 00284 int raw_arg = 0; 00285 int oind = 0; 00286 int skip_unavailable = 0; 00287 enum py_bt_status result = PY_BT_ERROR; 00288 enum opt 00289 { 00290 NO_FRAME_FILTERS, 00291 SKIP_UNAVAILABLE, 00292 }; 00293 static const struct mi_opt opts[] = 00294 { 00295 {"-no-frame-filters", NO_FRAME_FILTERS, 0}, 00296 {"-skip-unavailable", SKIP_UNAVAILABLE, 0}, 00297 { 0, 0, 0 } 00298 }; 00299 00300 while (1) 00301 { 00302 char *oarg; 00303 int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv, 00304 opts, &oind, &oarg); 00305 00306 if (opt < 0) 00307 break; 00308 switch ((enum opt) opt) 00309 { 00310 case NO_FRAME_FILTERS: 00311 raw_arg = oind; 00312 break; 00313 case SKIP_UNAVAILABLE: 00314 skip_unavailable = 1; 00315 break; 00316 } 00317 } 00318 00319 if (argc - oind != 1 && argc - oind != 3) 00320 error (_("-stack-list-arguments: Usage: " \ 00321 "[--no-frame-filters] [--skip-unavailable] " 00322 "PRINT_VALUES [FRAME_LOW FRAME_HIGH]")); 00323 00324 if (argc - oind == 3) 00325 { 00326 frame_low = atoi (argv[1 + oind]); 00327 frame_high = atoi (argv[2 + oind]); 00328 } 00329 else 00330 { 00331 /* Called with no arguments, it means we want args for the whole 00332 backtrace. */ 00333 frame_low = -1; 00334 frame_high = -1; 00335 } 00336 00337 print_values = mi_parse_print_values (argv[oind]); 00338 00339 /* Let's position fi on the frame at which to start the 00340 display. Could be the innermost frame if the whole stack needs 00341 displaying, or if frame_low is 0. */ 00342 for (i = 0, fi = get_current_frame (); 00343 fi && i < frame_low; 00344 i++, fi = get_prev_frame (fi)); 00345 00346 if (fi == NULL) 00347 error (_("-stack-list-arguments: Not enough frames in stack.")); 00348 00349 cleanup_stack_args 00350 = make_cleanup_ui_out_list_begin_end (uiout, "stack-args"); 00351 00352 if (! raw_arg && frame_filters) 00353 { 00354 int flags = PRINT_LEVEL | PRINT_ARGS; 00355 int py_frame_low = frame_low; 00356 00357 /* We cannot pass -1 to frame_low, as that would signify a 00358 relative backtrace from the tail of the stack. So, in the case 00359 of frame_low == -1, assign and increment it. */ 00360 if (py_frame_low == -1) 00361 py_frame_low++; 00362 00363 result = apply_frame_filter (get_current_frame (), flags, 00364 print_values, current_uiout, 00365 py_frame_low, frame_high); 00366 } 00367 00368 /* Run the inbuilt backtrace if there are no filters registered, or 00369 if "--no-frame-filters" has been specified from the command. */ 00370 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS) 00371 { 00372 /* Now let's print the frames up to frame_high, or until there are 00373 frames in the stack. */ 00374 for (; 00375 fi && (i <= frame_high || frame_high == -1); 00376 i++, fi = get_prev_frame (fi)) 00377 { 00378 struct cleanup *cleanup_frame; 00379 00380 QUIT; 00381 cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame"); 00382 ui_out_field_int (uiout, "level", i); 00383 list_args_or_locals (arguments, print_values, fi, skip_unavailable); 00384 do_cleanups (cleanup_frame); 00385 } 00386 } 00387 do_cleanups (cleanup_stack_args); 00388 } 00389 00390 /* Print a list of the local variables (including arguments) for the 00391 current frame. ARGC must be 1 and ARGV[0] specify if only the names, 00392 or both names and values of the variables must be printed. See 00393 parse_print_value for possible values. */ 00394 00395 void 00396 mi_cmd_stack_list_variables (char *command, char **argv, int argc) 00397 { 00398 struct frame_info *frame; 00399 int raw_arg = 0; 00400 enum py_bt_status result = PY_BT_ERROR; 00401 int print_value; 00402 int oind = 0; 00403 int skip_unavailable = 0; 00404 00405 if (argc > 1) 00406 { 00407 int i; 00408 enum opt 00409 { 00410 NO_FRAME_FILTERS, 00411 SKIP_UNAVAILABLE, 00412 }; 00413 static const struct mi_opt opts[] = 00414 { 00415 {"-no-frame-filters", NO_FRAME_FILTERS, 0}, 00416 {"-skip-unavailable", SKIP_UNAVAILABLE, 0}, 00417 { 0, 0, 0 } 00418 }; 00419 00420 while (1) 00421 { 00422 char *oarg; 00423 /* Don't parse 'print-values' as an option. */ 00424 int opt = mi_getopt ("-stack-list-variables", argc - 1, 00425 argv, opts, &oind, &oarg); 00426 if (opt < 0) 00427 break; 00428 switch ((enum opt) opt) 00429 { 00430 case NO_FRAME_FILTERS: 00431 raw_arg = oind; 00432 break; 00433 case SKIP_UNAVAILABLE: 00434 skip_unavailable = 1; 00435 break; 00436 } 00437 } 00438 } 00439 00440 /* After the last option is parsed, there should be only 00441 'print-values'. */ 00442 if (argc - oind != 1) 00443 error (_("-stack-list-variables: Usage: [--no-frame-filters] " \ 00444 "[--skip-unavailable] PRINT_VALUES")); 00445 00446 frame = get_selected_frame (NULL); 00447 print_value = mi_parse_print_values (argv[oind]); 00448 00449 if (! raw_arg && frame_filters) 00450 { 00451 int flags = PRINT_LEVEL | PRINT_ARGS | PRINT_LOCALS; 00452 00453 result = apply_frame_filter (frame, flags, print_value, 00454 current_uiout, 0, 0); 00455 } 00456 00457 /* Run the inbuilt backtrace if there are no filters registered, or 00458 if "--no-frame-filters" has been specified from the command. */ 00459 if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS) 00460 { 00461 list_args_or_locals (all, print_value, frame, 00462 skip_unavailable); 00463 } 00464 } 00465 00466 /* Print single local or argument. ARG must be already read in. For 00467 WHAT and VALUES see list_args_or_locals. 00468 00469 Errors are printed as if they would be the parameter value. Use 00470 zeroed ARG iff it should not be printed according to VALUES. If 00471 SKIP_UNAVAILABLE is true, only print ARG if it is available. */ 00472 00473 static void 00474 list_arg_or_local (const struct frame_arg *arg, enum what_to_list what, 00475 enum print_values values, int skip_unavailable) 00476 { 00477 struct cleanup *old_chain; 00478 struct ui_out *uiout = current_uiout; 00479 struct ui_file *stb; 00480 00481 gdb_assert (!arg->val || !arg->error); 00482 gdb_assert ((values == PRINT_NO_VALUES && arg->val == NULL 00483 && arg->error == NULL) 00484 || values == PRINT_SIMPLE_VALUES 00485 || (values == PRINT_ALL_VALUES 00486 && (arg->val != NULL || arg->error != NULL))); 00487 gdb_assert (arg->entry_kind == print_entry_values_no 00488 || (arg->entry_kind == print_entry_values_only 00489 && (arg->val || arg->error))); 00490 00491 if (skip_unavailable && arg->val != NULL 00492 && (value_entirely_unavailable (arg->val) 00493 /* A scalar object that does not have all bits available is 00494 also considered unavailable, because all bits contribute 00495 to its representation. */ 00496 || (val_print_scalar_type_p (value_type (arg->val)) 00497 && !value_bytes_available (arg->val, 00498 value_embedded_offset (arg->val), 00499 TYPE_LENGTH (value_type (arg->val)))))) 00500 return; 00501 00502 stb = mem_fileopen (); 00503 old_chain = make_cleanup_ui_file_delete (stb); 00504 00505 if (values != PRINT_NO_VALUES || what == all) 00506 make_cleanup_ui_out_tuple_begin_end (uiout, NULL); 00507 00508 fputs_filtered (SYMBOL_PRINT_NAME (arg->sym), stb); 00509 if (arg->entry_kind == print_entry_values_only) 00510 fputs_filtered ("@entry", stb); 00511 ui_out_field_stream (uiout, "name", stb); 00512 00513 if (what == all && SYMBOL_IS_ARGUMENT (arg->sym)) 00514 ui_out_field_int (uiout, "arg", 1); 00515 00516 if (values == PRINT_SIMPLE_VALUES) 00517 { 00518 check_typedef (arg->sym->type); 00519 type_print (arg->sym->type, "", stb, -1); 00520 ui_out_field_stream (uiout, "type", stb); 00521 } 00522 00523 if (arg->val || arg->error) 00524 { 00525 volatile struct gdb_exception except; 00526 00527 if (arg->error) 00528 except.message = arg->error; 00529 else 00530 { 00531 /* TRY_CATCH has two statements, wrap it in a block. */ 00532 00533 TRY_CATCH (except, RETURN_MASK_ERROR) 00534 { 00535 struct value_print_options opts; 00536 00537 get_no_prettyformat_print_options (&opts); 00538 opts.deref_ref = 1; 00539 common_val_print (arg->val, stb, 0, &opts, 00540 language_def (SYMBOL_LANGUAGE (arg->sym))); 00541 } 00542 } 00543 if (except.message) 00544 fprintf_filtered (stb, _("<error reading variable: %s>"), 00545 except.message); 00546 ui_out_field_stream (uiout, "value", stb); 00547 } 00548 00549 do_cleanups (old_chain); 00550 } 00551 00552 /* Print a list of the objects for the frame FI in a certain form, 00553 which is determined by VALUES. The objects can be locals, 00554 arguments or both, which is determined by WHAT. If SKIP_UNAVAILABLE 00555 is true, only print the arguments or local variables whose values 00556 are available. */ 00557 00558 static void 00559 list_args_or_locals (enum what_to_list what, enum print_values values, 00560 struct frame_info *fi, int skip_unavailable) 00561 { 00562 struct block *block; 00563 struct symbol *sym; 00564 struct block_iterator iter; 00565 struct cleanup *cleanup_list; 00566 struct type *type; 00567 char *name_of_result; 00568 struct ui_out *uiout = current_uiout; 00569 00570 block = get_frame_block (fi, 0); 00571 00572 switch (what) 00573 { 00574 case locals: 00575 name_of_result = "locals"; 00576 break; 00577 case arguments: 00578 name_of_result = "args"; 00579 break; 00580 case all: 00581 name_of_result = "variables"; 00582 break; 00583 default: 00584 internal_error (__FILE__, __LINE__, 00585 "unexpected what_to_list: %d", (int) what); 00586 } 00587 00588 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, name_of_result); 00589 00590 while (block != 0) 00591 { 00592 ALL_BLOCK_SYMBOLS (block, iter, sym) 00593 { 00594 int print_me = 0; 00595 00596 switch (SYMBOL_CLASS (sym)) 00597 { 00598 default: 00599 case LOC_UNDEF: /* catches errors */ 00600 case LOC_CONST: /* constant */ 00601 case LOC_TYPEDEF: /* local typedef */ 00602 case LOC_LABEL: /* local label */ 00603 case LOC_BLOCK: /* local function */ 00604 case LOC_CONST_BYTES: /* loc. byte seq. */ 00605 case LOC_UNRESOLVED: /* unresolved static */ 00606 case LOC_OPTIMIZED_OUT: /* optimized out */ 00607 print_me = 0; 00608 break; 00609 00610 case LOC_ARG: /* argument */ 00611 case LOC_REF_ARG: /* reference arg */ 00612 case LOC_REGPARM_ADDR: /* indirect register arg */ 00613 case LOC_LOCAL: /* stack local */ 00614 case LOC_STATIC: /* static */ 00615 case LOC_REGISTER: /* register */ 00616 case LOC_COMPUTED: /* computed location */ 00617 if (what == all) 00618 print_me = 1; 00619 else if (what == locals) 00620 print_me = !SYMBOL_IS_ARGUMENT (sym); 00621 else 00622 print_me = SYMBOL_IS_ARGUMENT (sym); 00623 break; 00624 } 00625 if (print_me) 00626 { 00627 struct symbol *sym2; 00628 struct frame_arg arg, entryarg; 00629 00630 if (SYMBOL_IS_ARGUMENT (sym)) 00631 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), 00632 block, VAR_DOMAIN, 00633 NULL); 00634 else 00635 sym2 = sym; 00636 gdb_assert (sym2 != NULL); 00637 00638 memset (&arg, 0, sizeof (arg)); 00639 arg.sym = sym2; 00640 arg.entry_kind = print_entry_values_no; 00641 memset (&entryarg, 0, sizeof (entryarg)); 00642 entryarg.sym = sym2; 00643 entryarg.entry_kind = print_entry_values_no; 00644 00645 switch (values) 00646 { 00647 case PRINT_SIMPLE_VALUES: 00648 type = check_typedef (sym2->type); 00649 if (TYPE_CODE (type) != TYPE_CODE_ARRAY 00650 && TYPE_CODE (type) != TYPE_CODE_STRUCT 00651 && TYPE_CODE (type) != TYPE_CODE_UNION) 00652 { 00653 case PRINT_ALL_VALUES: 00654 if (SYMBOL_IS_ARGUMENT (sym)) 00655 read_frame_arg (sym2, fi, &arg, &entryarg); 00656 else 00657 read_frame_local (sym2, fi, &arg); 00658 } 00659 break; 00660 } 00661 00662 if (arg.entry_kind != print_entry_values_only) 00663 list_arg_or_local (&arg, what, values, skip_unavailable); 00664 if (entryarg.entry_kind != print_entry_values_no) 00665 list_arg_or_local (&entryarg, what, values, skip_unavailable); 00666 xfree (arg.error); 00667 xfree (entryarg.error); 00668 } 00669 } 00670 00671 if (BLOCK_FUNCTION (block)) 00672 break; 00673 else 00674 block = BLOCK_SUPERBLOCK (block); 00675 } 00676 do_cleanups (cleanup_list); 00677 } 00678 00679 void 00680 mi_cmd_stack_select_frame (char *command, char **argv, int argc) 00681 { 00682 if (argc == 0 || argc > 1) 00683 error (_("-stack-select-frame: Usage: FRAME_SPEC")); 00684 00685 select_frame_command (argv[0], 1 /* not used */ ); 00686 } 00687 00688 void 00689 mi_cmd_stack_info_frame (char *command, char **argv, int argc) 00690 { 00691 if (argc > 0) 00692 error (_("-stack-info-frame: No arguments allowed")); 00693 00694 print_frame_info (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 0, 1); 00695 }