1 /* Memory-access and commands for "inferior" process, for GDB. 2 3 Copyright (C) 1986-2020 Free Software Foundation, Inc. 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 3 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, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "arch-utils.h" 22 #include "symtab.h" 23 #include "gdbtypes.h" 24 #include "frame.h" 25 #include "inferior.h" 26 #include "infrun.h" 27 #include "gdbsupport/environ.h" 28 #include "value.h" 29 #include "gdbcmd.h" 30 #include "symfile.h" 31 #include "gdbcore.h" 32 #include "target.h" 33 #include "language.h" 34 #include "objfiles.h" 35 #include "completer.h" 36 #include "ui-out.h" 37 #include "regcache.h" 38 #include "reggroups.h" 39 #include "block.h" 40 #include "solib.h" 41 #include <ctype.h> 42 #include "observable.h" 43 #include "target-descriptions.h" 44 #include "user-regs.h" 45 #include "gdbthread.h" 46 #include "valprint.h" 47 #include "inline-frame.h" 48 #include "tracepoint.h" 49 #include "inf-loop.h" 50 #include "continuations.h" 51 #include "linespec.h" 52 #include "thread-fsm.h" 53 #include "top.h" 54 #include "interps.h" 55 #include "skip.h" 56 #include "gdbsupport/gdb_optional.h" 57 #include "source.h" 58 #include "cli/cli-style.h" 59 60 /* Local functions: */ 61 62 static void until_next_command (int); 63 64 static void step_1 (int, int, const char *); 65 66 #define ERROR_NO_INFERIOR \ 67 if (!target_has_execution) error (_("The program is not being run.")); 68 69 /* Scratch area where string containing arguments to give to the 70 program will be stored by 'set args'. As soon as anything is 71 stored, notice_args_set will move it into per-inferior storage. 72 Arguments are separated by spaces. Empty string (pointer to '\0') 73 means no args. */ 74 75 static char *inferior_args_scratch; 76 77 /* Scratch area where the new cwd will be stored by 'set cwd'. */ 78 79 static char *inferior_cwd_scratch; 80 81 /* Scratch area where 'set inferior-tty' will store user-provided value. 82 We'll immediate copy it into per-inferior storage. */ 83 84 static char *inferior_io_terminal_scratch; 85 86 /* Pid of our debugged inferior, or 0 if no inferior now. 87 Since various parts of infrun.c test this to see whether there is a program 88 being debugged it should be nonzero (currently 3 is used) for remote 89 debugging. */ 90 91 ptid_t inferior_ptid; 92 93 /* Nonzero if stopped due to completion of a stack dummy routine. */ 94 95 enum stop_stack_kind stop_stack_dummy; 96 97 /* Nonzero if stopped due to a random (unexpected) signal in inferior 98 process. */ 99 100 int stopped_by_random_signal; 101 102 103 104 static void 105 set_inferior_tty_command (const char *args, int from_tty, 106 struct cmd_list_element *c) 107 { 108 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch. 109 Now route it to current inferior. */ 110 current_inferior ()->set_tty (inferior_io_terminal_scratch); 111 } 112 113 static void 114 show_inferior_tty_command (struct ui_file *file, int from_tty, 115 struct cmd_list_element *c, const char *value) 116 { 117 /* Note that we ignore the passed-in value in favor of computing it 118 directly. */ 119 const char *inferior_tty = current_inferior ()->tty (); 120 121 if (inferior_tty == nullptr) 122 inferior_tty = ""; 123 fprintf_filtered (gdb_stdout, 124 _("Terminal for future runs of program being debugged " 125 "is \"%s\".\n"), inferior_tty); 126 } 127 128 const char * 129 get_inferior_args (void) 130 { 131 if (current_inferior ()->argc != 0) 132 { 133 gdb::array_view<char * const> args (current_inferior ()->argv, 134 current_inferior ()->argc); 135 std::string n = construct_inferior_arguments (args); 136 set_inferior_args (n.c_str ()); 137 } 138 139 if (current_inferior ()->args == NULL) 140 current_inferior ()->args = xstrdup (""); 141 142 return current_inferior ()->args; 143 } 144 145 /* Set the arguments for the current inferior. Ownership of 146 NEWARGS is not transferred. */ 147 148 void 149 set_inferior_args (const char *newargs) 150 { 151 xfree (current_inferior ()->args); 152 current_inferior ()->args = newargs ? xstrdup (newargs) : NULL; 153 current_inferior ()->argc = 0; 154 current_inferior ()->argv = 0; 155 } 156 157 void 158 set_inferior_args_vector (int argc, char **argv) 159 { 160 current_inferior ()->argc = argc; 161 current_inferior ()->argv = argv; 162 } 163 164 /* Notice when `set args' is run. */ 165 166 static void 167 set_args_command (const char *args, int from_tty, struct cmd_list_element *c) 168 { 169 /* CLI has assigned the user-provided value to inferior_args_scratch. 170 Now route it to current inferior. */ 171 set_inferior_args (inferior_args_scratch); 172 } 173 174 /* Notice when `show args' is run. */ 175 176 static void 177 show_args_command (struct ui_file *file, int from_tty, 178 struct cmd_list_element *c, const char *value) 179 { 180 /* Note that we ignore the passed-in value in favor of computing it 181 directly. */ 182 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ()); 183 } 184 185 /* See gdbsupport/common-inferior.h. */ 186 187 void 188 set_inferior_cwd (const char *cwd) 189 { 190 struct inferior *inf = current_inferior (); 191 192 gdb_assert (inf != NULL); 193 194 if (cwd == NULL) 195 inf->cwd.reset (); 196 else 197 inf->cwd.reset (xstrdup (cwd)); 198 } 199 200 /* See gdbsupport/common-inferior.h. */ 201 202 const char * 203 get_inferior_cwd () 204 { 205 return current_inferior ()->cwd.get (); 206 } 207 208 /* Handle the 'set cwd' command. */ 209 210 static void 211 set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c) 212 { 213 if (*inferior_cwd_scratch == '\0') 214 set_inferior_cwd (NULL); 215 else 216 set_inferior_cwd (inferior_cwd_scratch); 217 } 218 219 /* Handle the 'show cwd' command. */ 220 221 static void 222 show_cwd_command (struct ui_file *file, int from_tty, 223 struct cmd_list_element *c, const char *value) 224 { 225 const char *cwd = get_inferior_cwd (); 226 227 if (cwd == NULL) 228 fprintf_filtered (gdb_stdout, 229 _("\ 230 You have not set the inferior's current working directory.\n\ 231 The inferior will inherit GDB's cwd if native debugging, or the remote\n\ 232 server's cwd if remote debugging.\n")); 233 else 234 fprintf_filtered (gdb_stdout, 235 _("Current working directory that will be used " 236 "when starting the inferior is \"%s\".\n"), cwd); 237 } 238 239 240 /* This function strips the '&' character (indicating background 241 execution) that is added as *the last* of the arguments ARGS of a 242 command. A copy of the incoming ARGS without the '&' is returned, 243 unless the resulting string after stripping is empty, in which case 244 NULL is returned. *BG_CHAR_P is an output boolean that indicates 245 whether the '&' character was found. */ 246 247 static gdb::unique_xmalloc_ptr<char> 248 strip_bg_char (const char *args, int *bg_char_p) 249 { 250 const char *p; 251 252 if (args == NULL || *args == '\0') 253 { 254 *bg_char_p = 0; 255 return NULL; 256 } 257 258 p = args + strlen (args); 259 if (p[-1] == '&') 260 { 261 p--; 262 while (p > args && isspace (p[-1])) 263 p--; 264 265 *bg_char_p = 1; 266 if (p != args) 267 return gdb::unique_xmalloc_ptr<char> 268 (savestring (args, p - args)); 269 else 270 return gdb::unique_xmalloc_ptr<char> (nullptr); 271 } 272 273 *bg_char_p = 0; 274 return make_unique_xstrdup (args); 275 } 276 277 /* Common actions to take after creating any sort of inferior, by any 278 means (running, attaching, connecting, et cetera). The target 279 should be stopped. */ 280 281 void 282 post_create_inferior (struct target_ops *target, int from_tty) 283 { 284 285 /* Be sure we own the terminal in case write operations are performed. */ 286 target_terminal::ours_for_output (); 287 288 /* If the target hasn't taken care of this already, do it now. 289 Targets which need to access registers during to_open, 290 to_create_inferior, or to_attach should do it earlier; but many 291 don't need to. */ 292 target_find_description (); 293 294 /* Now that we know the register layout, retrieve current PC. But 295 if the PC is unavailable (e.g., we're opening a core file with 296 missing registers info), ignore it. */ 297 thread_info *thr = inferior_thread (); 298 299 thr->suspend.stop_pc = 0; 300 try 301 { 302 regcache *rc = get_thread_regcache (thr); 303 thr->suspend.stop_pc = regcache_read_pc (rc); 304 } 305 catch (const gdb_exception_error &ex) 306 { 307 if (ex.error != NOT_AVAILABLE_ERROR) 308 throw; 309 } 310 311 if (exec_bfd) 312 { 313 const unsigned solib_add_generation 314 = current_program_space->solib_add_generation; 315 316 /* Create the hooks to handle shared library load and unload 317 events. */ 318 solib_create_inferior_hook (from_tty); 319 320 if (current_program_space->solib_add_generation == solib_add_generation) 321 { 322 /* The platform-specific hook should load initial shared libraries, 323 but didn't. FROM_TTY will be incorrectly 0 but such solib 324 targets should be fixed anyway. Call it only after the solib 325 target has been initialized by solib_create_inferior_hook. */ 326 327 if (info_verbose) 328 warning (_("platform-specific solib_create_inferior_hook did " 329 "not load initial shared libraries.")); 330 331 /* If the solist is global across processes, there's no need to 332 refetch it here. */ 333 if (!gdbarch_has_global_solist (target_gdbarch ())) 334 solib_add (NULL, 0, auto_solib_add); 335 } 336 } 337 338 /* If the user sets watchpoints before execution having started, 339 then she gets software watchpoints, because GDB can't know which 340 target will end up being pushed, or if it supports hardware 341 watchpoints or not. breakpoint_re_set takes care of promoting 342 watchpoints to hardware watchpoints if possible, however, if this 343 new inferior doesn't load shared libraries or we don't pull in 344 symbols from any other source on this target/arch, 345 breakpoint_re_set is never called. Call it now so that software 346 watchpoints get a chance to be promoted to hardware watchpoints 347 if the now pushed target supports hardware watchpoints. */ 348 breakpoint_re_set (); 349 350 gdb::observers::inferior_created.notify (target, from_tty); 351 } 352 353 /* Kill the inferior if already running. This function is designed 354 to be called when we are about to start the execution of the program 355 from the beginning. Ask the user to confirm that he wants to restart 356 the program being debugged when FROM_TTY is non-null. */ 357 358 static void 359 kill_if_already_running (int from_tty) 360 { 361 if (inferior_ptid != null_ptid && target_has_execution) 362 { 363 /* Bail out before killing the program if we will not be able to 364 restart it. */ 365 target_require_runnable (); 366 367 if (from_tty 368 && !query (_("The program being debugged has been started already.\n\ 369 Start it from the beginning? "))) 370 error (_("Program not restarted.")); 371 target_kill (); 372 } 373 } 374 375 /* See inferior.h. */ 376 377 void 378 prepare_execution_command (struct target_ops *target, int background) 379 { 380 /* If we get a request for running in the bg but the target 381 doesn't support it, error out. */ 382 if (background && !target->can_async_p ()) 383 error (_("Asynchronous execution not supported on this target.")); 384 385 if (!background) 386 { 387 /* If we get a request for running in the fg, then we need to 388 simulate synchronous (fg) execution. Note no cleanup is 389 necessary for this. stdin is re-enabled whenever an error 390 reaches the top level. */ 391 all_uis_on_sync_execution_starting (); 392 } 393 } 394 395 /* Determine how the new inferior will behave. */ 396 397 enum run_how 398 { 399 /* Run program without any explicit stop during startup. */ 400 RUN_NORMAL, 401 402 /* Stop at the beginning of the program's main function. */ 403 RUN_STOP_AT_MAIN, 404 405 /* Stop at the first instruction of the program. */ 406 RUN_STOP_AT_FIRST_INSN 407 }; 408 409 /* Implement the "run" command. Force a stop during program start if 410 requested by RUN_HOW. */ 411 412 static void 413 run_command_1 (const char *args, int from_tty, enum run_how run_how) 414 { 415 const char *exec_file; 416 struct ui_out *uiout = current_uiout; 417 struct target_ops *run_target; 418 int async_exec; 419 420 dont_repeat (); 421 422 kill_if_already_running (from_tty); 423 424 init_wait_for_inferior (); 425 clear_breakpoint_hit_counts (); 426 427 /* Clean up any leftovers from other runs. Some other things from 428 this function should probably be moved into target_pre_inferior. */ 429 target_pre_inferior (from_tty); 430 431 /* The comment here used to read, "The exec file is re-read every 432 time we do a generic_mourn_inferior, so we just have to worry 433 about the symbol file." The `generic_mourn_inferior' function 434 gets called whenever the program exits. However, suppose the 435 program exits, and *then* the executable file changes? We need 436 to check again here. Since reopen_exec_file doesn't do anything 437 if the timestamp hasn't changed, I don't see the harm. */ 438 reopen_exec_file (); 439 reread_symbols (); 440 441 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); 442 args = stripped.get (); 443 444 /* Do validation and preparation before possibly changing anything 445 in the inferior. */ 446 447 run_target = find_run_target (); 448 449 prepare_execution_command (run_target, async_exec); 450 451 if (non_stop && !run_target->supports_non_stop ()) 452 error (_("The target does not support running in non-stop mode.")); 453 454 /* Done. Can now set breakpoints, change inferior args, etc. */ 455 456 /* Insert temporary breakpoint in main function if requested. */ 457 if (run_how == RUN_STOP_AT_MAIN) 458 { 459 std::string arg = string_printf ("-qualified %s", main_name ()); 460 tbreak_command (arg.c_str (), 0); 461 } 462 463 exec_file = get_exec_file (0); 464 465 /* We keep symbols from add-symbol-file, on the grounds that the 466 user might want to add some symbols before running the program 467 (right?). But sometimes (dynamic loading where the user manually 468 introduces the new symbols with add-symbol-file), the code which 469 the symbols describe does not persist between runs. Currently 470 the user has to manually nuke all symbols between runs if they 471 want them to go away (PR 2207). This is probably reasonable. */ 472 473 /* If there were other args, beside '&', process them. */ 474 if (args != NULL) 475 set_inferior_args (args); 476 477 if (from_tty) 478 { 479 uiout->field_string (NULL, "Starting program"); 480 uiout->text (": "); 481 if (exec_file) 482 uiout->field_string ("execfile", exec_file); 483 uiout->spaces (1); 484 /* We call get_inferior_args() because we might need to compute 485 the value now. */ 486 uiout->field_string ("infargs", get_inferior_args ()); 487 uiout->text ("\n"); 488 uiout->flush (); 489 } 490 491 /* We call get_inferior_args() because we might need to compute 492 the value now. */ 493 run_target->create_inferior (exec_file, 494 std::string (get_inferior_args ()), 495 current_inferior ()->environment.envp (), 496 from_tty); 497 /* to_create_inferior should push the target, so after this point we 498 shouldn't refer to run_target again. */ 499 run_target = NULL; 500 501 /* We're starting off a new process. When we get out of here, in 502 non-stop mode, finish the state of all threads of that process, 503 but leave other threads alone, as they may be stopped in internal 504 events --- the frontend shouldn't see them as stopped. In 505 all-stop, always finish the state of all threads, as we may be 506 resuming more than just the new process. */ 507 process_stratum_target *finish_target; 508 ptid_t finish_ptid; 509 if (non_stop) 510 { 511 finish_target = current_inferior ()->process_target (); 512 finish_ptid = ptid_t (current_inferior ()->pid); 513 } 514 else 515 { 516 finish_target = nullptr; 517 finish_ptid = minus_one_ptid; 518 } 519 scoped_finish_thread_state finish_state (finish_target, finish_ptid); 520 521 /* Pass zero for FROM_TTY, because at this point the "run" command 522 has done its thing; now we are setting up the running program. */ 523 post_create_inferior (current_top_target (), 0); 524 525 /* Queue a pending event so that the program stops immediately. */ 526 if (run_how == RUN_STOP_AT_FIRST_INSN) 527 { 528 thread_info *thr = inferior_thread (); 529 thr->suspend.waitstatus_pending_p = 1; 530 thr->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED; 531 thr->suspend.waitstatus.value.sig = GDB_SIGNAL_0; 532 } 533 534 /* Start the target running. Do not use -1 continuation as it would skip 535 breakpoint right at the entry point. */ 536 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0); 537 538 /* Since there was no error, there's no need to finish the thread 539 states here. */ 540 finish_state.release (); 541 } 542 543 static void 544 run_command (const char *args, int from_tty) 545 { 546 run_command_1 (args, from_tty, RUN_NORMAL); 547 } 548 549 /* Start the execution of the program up until the beginning of the main 550 program. */ 551 552 static void 553 start_command (const char *args, int from_tty) 554 { 555 /* Some languages such as Ada need to search inside the program 556 minimal symbols for the location where to put the temporary 557 breakpoint before starting. */ 558 if (!have_minimal_symbols ()) 559 error (_("No symbol table loaded. Use the \"file\" command.")); 560 561 /* Run the program until reaching the main procedure... */ 562 run_command_1 (args, from_tty, RUN_STOP_AT_MAIN); 563 } 564 565 /* Start the execution of the program stopping at the first 566 instruction. */ 567 568 static void 569 starti_command (const char *args, int from_tty) 570 { 571 run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN); 572 } 573 574 static int 575 proceed_thread_callback (struct thread_info *thread, void *arg) 576 { 577 /* We go through all threads individually instead of compressing 578 into a single target `resume_all' request, because some threads 579 may be stopped in internal breakpoints/events, or stopped waiting 580 for its turn in the displaced stepping queue (that is, they are 581 running && !executing). The target side has no idea about why 582 the thread is stopped, so a `resume_all' command would resume too 583 much. If/when GDB gains a way to tell the target `hold this 584 thread stopped until I say otherwise', then we can optimize 585 this. */ 586 if (thread->state != THREAD_STOPPED) 587 return 0; 588 589 if (!thread->inf->has_execution ()) 590 return 0; 591 592 switch_to_thread (thread); 593 clear_proceed_status (0); 594 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 595 return 0; 596 } 597 598 static void 599 ensure_valid_thread (void) 600 { 601 if (inferior_ptid == null_ptid 602 || inferior_thread ()->state == THREAD_EXITED) 603 error (_("Cannot execute this command without a live selected thread.")); 604 } 605 606 /* If the user is looking at trace frames, any resumption of execution 607 is likely to mix up recorded and live target data. So simply 608 disallow those commands. */ 609 610 static void 611 ensure_not_tfind_mode (void) 612 { 613 if (get_traceframe_number () >= 0) 614 error (_("Cannot execute this command while looking at trace frames.")); 615 } 616 617 /* Throw an error indicating the current thread is running. */ 618 619 static void 620 error_is_running (void) 621 { 622 error (_("Cannot execute this command while " 623 "the selected thread is running.")); 624 } 625 626 /* Calls error_is_running if the current thread is running. */ 627 628 static void 629 ensure_not_running (void) 630 { 631 if (inferior_thread ()->state == THREAD_RUNNING) 632 error_is_running (); 633 } 634 635 void 636 continue_1 (int all_threads) 637 { 638 ERROR_NO_INFERIOR; 639 ensure_not_tfind_mode (); 640 641 if (non_stop && all_threads) 642 { 643 /* Don't error out if the current thread is running, because 644 there may be other stopped threads. */ 645 646 /* Backup current thread and selected frame and restore on scope 647 exit. */ 648 scoped_restore_current_thread restore_thread; 649 650 iterate_over_threads (proceed_thread_callback, NULL); 651 652 if (current_ui->prompt_state == PROMPT_BLOCKED) 653 { 654 /* If all threads in the target were already running, 655 proceed_thread_callback ends up never calling proceed, 656 and so nothing calls this to put the inferior's terminal 657 settings in effect and remove stdin from the event loop, 658 which we must when running a foreground command. E.g.: 659 660 (gdb) c -a& 661 Continuing. 662 <all threads are running now> 663 (gdb) c -a 664 Continuing. 665 <no thread was resumed, but the inferior now owns the terminal> 666 */ 667 target_terminal::inferior (); 668 } 669 } 670 else 671 { 672 ensure_valid_thread (); 673 ensure_not_running (); 674 clear_proceed_status (0); 675 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 676 } 677 } 678 679 /* continue [-a] [proceed-count] [&] */ 680 681 static void 682 continue_command (const char *args, int from_tty) 683 { 684 int async_exec; 685 bool all_threads_p = false; 686 687 ERROR_NO_INFERIOR; 688 689 /* Find out whether we must run in the background. */ 690 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); 691 args = stripped.get (); 692 693 if (args != NULL) 694 { 695 if (startswith (args, "-a")) 696 { 697 all_threads_p = true; 698 args += sizeof ("-a") - 1; 699 if (*args == '\0') 700 args = NULL; 701 } 702 } 703 704 if (!non_stop && all_threads_p) 705 error (_("`-a' is meaningless in all-stop mode.")); 706 707 if (args != NULL && all_threads_p) 708 error (_("Can't resume all threads and specify " 709 "proceed count simultaneously.")); 710 711 /* If we have an argument left, set proceed count of breakpoint we 712 stopped at. */ 713 if (args != NULL) 714 { 715 bpstat bs = NULL; 716 int num, stat; 717 int stopped = 0; 718 struct thread_info *tp; 719 720 if (non_stop) 721 tp = inferior_thread (); 722 else 723 { 724 process_stratum_target *last_target; 725 ptid_t last_ptid; 726 727 get_last_target_status (&last_target, &last_ptid, nullptr); 728 tp = find_thread_ptid (last_target, last_ptid); 729 } 730 if (tp != NULL) 731 bs = tp->control.stop_bpstat; 732 733 while ((stat = bpstat_num (&bs, &num)) != 0) 734 if (stat > 0) 735 { 736 set_ignore_count (num, 737 parse_and_eval_long (args) - 1, 738 from_tty); 739 /* set_ignore_count prints a message ending with a period. 740 So print two spaces before "Continuing.". */ 741 if (from_tty) 742 printf_filtered (" "); 743 stopped = 1; 744 } 745 746 if (!stopped && from_tty) 747 { 748 printf_filtered 749 ("Not stopped at any breakpoint; argument ignored.\n"); 750 } 751 } 752 753 ERROR_NO_INFERIOR; 754 ensure_not_tfind_mode (); 755 756 if (!non_stop || !all_threads_p) 757 { 758 ensure_valid_thread (); 759 ensure_not_running (); 760 } 761 762 prepare_execution_command (current_top_target (), async_exec); 763 764 if (from_tty) 765 printf_filtered (_("Continuing.\n")); 766 767 continue_1 (all_threads_p); 768 } 769 770 /* Record in TP the starting point of a "step" or "next" command. */ 771 772 static void 773 set_step_frame (thread_info *tp) 774 { 775 /* This can be removed once this function no longer implicitly relies on the 776 inferior_ptid value. */ 777 gdb_assert (inferior_ptid == tp->ptid); 778 779 frame_info *frame = get_current_frame (); 780 781 symtab_and_line sal = find_frame_sal (frame); 782 set_step_info (tp, frame, sal); 783 784 CORE_ADDR pc = get_frame_pc (frame); 785 tp->control.step_start_function = find_pc_function (pc); 786 } 787 788 /* Step until outside of current statement. */ 789 790 static void 791 step_command (const char *count_string, int from_tty) 792 { 793 step_1 (0, 0, count_string); 794 } 795 796 /* Likewise, but skip over subroutine calls as if single instructions. */ 797 798 static void 799 next_command (const char *count_string, int from_tty) 800 { 801 step_1 (1, 0, count_string); 802 } 803 804 /* Likewise, but step only one instruction. */ 805 806 static void 807 stepi_command (const char *count_string, int from_tty) 808 { 809 step_1 (0, 1, count_string); 810 } 811 812 static void 813 nexti_command (const char *count_string, int from_tty) 814 { 815 step_1 (1, 1, count_string); 816 } 817 818 /* Data for the FSM that manages the step/next/stepi/nexti 819 commands. */ 820 821 struct step_command_fsm : public thread_fsm 822 { 823 /* How many steps left in a "step N"-like command. */ 824 int count; 825 826 /* If true, this is a next/nexti, otherwise a step/stepi. */ 827 int skip_subroutines; 828 829 /* If true, this is a stepi/nexti, otherwise a step/step. */ 830 int single_inst; 831 832 explicit step_command_fsm (struct interp *cmd_interp) 833 : thread_fsm (cmd_interp) 834 { 835 } 836 837 void clean_up (struct thread_info *thread) override; 838 bool should_stop (struct thread_info *thread) override; 839 enum async_reply_reason do_async_reply_reason () override; 840 }; 841 842 /* Prepare for a step/next/etc. command. Any target resource 843 allocated here is undone in the FSM's clean_up method. */ 844 845 static void 846 step_command_fsm_prepare (struct step_command_fsm *sm, 847 int skip_subroutines, int single_inst, 848 int count, struct thread_info *thread) 849 { 850 sm->skip_subroutines = skip_subroutines; 851 sm->single_inst = single_inst; 852 sm->count = count; 853 854 /* Leave the si command alone. */ 855 if (!sm->single_inst || sm->skip_subroutines) 856 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ())); 857 858 thread->control.stepping_command = 1; 859 } 860 861 static int prepare_one_step (thread_info *, struct step_command_fsm *sm); 862 863 static void 864 step_1 (int skip_subroutines, int single_inst, const char *count_string) 865 { 866 int count; 867 int async_exec; 868 struct thread_info *thr; 869 struct step_command_fsm *step_sm; 870 871 ERROR_NO_INFERIOR; 872 ensure_not_tfind_mode (); 873 ensure_valid_thread (); 874 ensure_not_running (); 875 876 gdb::unique_xmalloc_ptr<char> stripped 877 = strip_bg_char (count_string, &async_exec); 878 count_string = stripped.get (); 879 880 prepare_execution_command (current_top_target (), async_exec); 881 882 count = count_string ? parse_and_eval_long (count_string) : 1; 883 884 clear_proceed_status (1); 885 886 /* Setup the execution command state machine to handle all the COUNT 887 steps. */ 888 thr = inferior_thread (); 889 step_sm = new step_command_fsm (command_interp ()); 890 thr->thread_fsm = step_sm; 891 892 step_command_fsm_prepare (step_sm, skip_subroutines, 893 single_inst, count, thr); 894 895 /* Do only one step for now, before returning control to the event 896 loop. Let the continuation figure out how many other steps we 897 need to do, and handle them one at the time, through 898 step_once. */ 899 if (!prepare_one_step (thr, step_sm)) 900 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 901 else 902 { 903 int proceeded; 904 905 /* Stepped into an inline frame. Pretend that we've 906 stopped. */ 907 thr->thread_fsm->clean_up (thr); 908 proceeded = normal_stop (); 909 if (!proceeded) 910 inferior_event_handler (INF_EXEC_COMPLETE); 911 all_uis_check_sync_execution_done (); 912 } 913 } 914 915 /* Implementation of the 'should_stop' FSM method for stepping 916 commands. Called after we are done with one step operation, to 917 check whether we need to step again, before we print the prompt and 918 return control to the user. If count is > 1, returns false, as we 919 will need to keep going. */ 920 921 bool 922 step_command_fsm::should_stop (struct thread_info *tp) 923 { 924 if (tp->control.stop_step) 925 { 926 /* There are more steps to make, and we did stop due to 927 ending a stepping range. Do another step. */ 928 if (--count > 0) 929 return prepare_one_step (tp, this); 930 931 set_finished (); 932 } 933 934 return true; 935 } 936 937 /* Implementation of the 'clean_up' FSM method for stepping commands. */ 938 939 void 940 step_command_fsm::clean_up (struct thread_info *thread) 941 { 942 if (!single_inst || skip_subroutines) 943 delete_longjmp_breakpoint (thread->global_num); 944 } 945 946 /* Implementation of the 'async_reply_reason' FSM method for stepping 947 commands. */ 948 949 enum async_reply_reason 950 step_command_fsm::do_async_reply_reason () 951 { 952 return EXEC_ASYNC_END_STEPPING_RANGE; 953 } 954 955 /* Prepare for one step in "step N". The actual target resumption is 956 done by the caller. Return true if we're done and should thus 957 report a stop to the user. Returns false if the target needs to be 958 resumed. */ 959 960 static int 961 prepare_one_step (thread_info *tp, struct step_command_fsm *sm) 962 { 963 /* This can be removed once this function no longer implicitly relies on the 964 inferior_ptid value. */ 965 gdb_assert (inferior_ptid == tp->ptid); 966 967 if (sm->count > 0) 968 { 969 struct frame_info *frame = get_current_frame (); 970 971 set_step_frame (tp); 972 973 if (!sm->single_inst) 974 { 975 CORE_ADDR pc; 976 977 /* Step at an inlined function behaves like "down". */ 978 if (!sm->skip_subroutines 979 && inline_skipped_frames (tp)) 980 { 981 ptid_t resume_ptid; 982 const char *fn = NULL; 983 symtab_and_line sal; 984 struct symbol *sym; 985 986 /* Pretend that we've ran. */ 987 resume_ptid = user_visible_resume_ptid (1); 988 set_running (tp->inf->process_target (), resume_ptid, true); 989 990 step_into_inline_frame (tp); 991 992 frame = get_current_frame (); 993 sal = find_frame_sal (frame); 994 sym = get_frame_function (frame); 995 996 if (sym != NULL) 997 fn = sym->print_name (); 998 999 if (sal.line == 0 1000 || !function_name_is_marked_for_skip (fn, sal)) 1001 { 1002 sm->count--; 1003 return prepare_one_step (tp, sm); 1004 } 1005 } 1006 1007 pc = get_frame_pc (frame); 1008 find_pc_line_pc_range (pc, 1009 &tp->control.step_range_start, 1010 &tp->control.step_range_end); 1011 1012 tp->control.may_range_step = 1; 1013 1014 /* If we have no line info, switch to stepi mode. */ 1015 if (tp->control.step_range_end == 0 && step_stop_if_no_debug) 1016 { 1017 tp->control.step_range_start = tp->control.step_range_end = 1; 1018 tp->control.may_range_step = 0; 1019 } 1020 else if (tp->control.step_range_end == 0) 1021 { 1022 const char *name; 1023 1024 if (find_pc_partial_function (pc, &name, 1025 &tp->control.step_range_start, 1026 &tp->control.step_range_end) == 0) 1027 error (_("Cannot find bounds of current function")); 1028 1029 target_terminal::ours_for_output (); 1030 printf_filtered (_("Single stepping until exit from function %s," 1031 "\nwhich has no line number information.\n"), 1032 name); 1033 } 1034 } 1035 else 1036 { 1037 /* Say we are stepping, but stop after one insn whatever it does. */ 1038 tp->control.step_range_start = tp->control.step_range_end = 1; 1039 if (!sm->skip_subroutines) 1040 /* It is stepi. 1041 Don't step over function calls, not even to functions lacking 1042 line numbers. */ 1043 tp->control.step_over_calls = STEP_OVER_NONE; 1044 } 1045 1046 if (sm->skip_subroutines) 1047 tp->control.step_over_calls = STEP_OVER_ALL; 1048 1049 return 0; 1050 } 1051 1052 /* Done. */ 1053 sm->set_finished (); 1054 return 1; 1055 } 1056 1057 1058 /* Continue program at specified address. */ 1059 1060 static void 1061 jump_command (const char *arg, int from_tty) 1062 { 1063 struct gdbarch *gdbarch = get_current_arch (); 1064 CORE_ADDR addr; 1065 struct symbol *fn; 1066 struct symbol *sfn; 1067 int async_exec; 1068 1069 ERROR_NO_INFERIOR; 1070 ensure_not_tfind_mode (); 1071 ensure_valid_thread (); 1072 ensure_not_running (); 1073 1074 /* Find out whether we must run in the background. */ 1075 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); 1076 arg = stripped.get (); 1077 1078 prepare_execution_command (current_top_target (), async_exec); 1079 1080 if (!arg) 1081 error_no_arg (_("starting address")); 1082 1083 std::vector<symtab_and_line> sals 1084 = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE); 1085 if (sals.size () != 1) 1086 error (_("Unreasonable jump request")); 1087 1088 symtab_and_line &sal = sals[0]; 1089 1090 if (sal.symtab == 0 && sal.pc == 0) 1091 error (_("No source file has been specified.")); 1092 1093 resolve_sal_pc (&sal); /* May error out. */ 1094 1095 /* See if we are trying to jump to another function. */ 1096 fn = get_frame_function (get_current_frame ()); 1097 sfn = find_pc_function (sal.pc); 1098 if (fn != NULL && sfn != fn) 1099 { 1100 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line, 1101 fn->print_name ())) 1102 { 1103 error (_("Not confirmed.")); 1104 /* NOTREACHED */ 1105 } 1106 } 1107 1108 if (sfn != NULL) 1109 { 1110 struct obj_section *section; 1111 1112 fixup_symbol_section (sfn, 0); 1113 section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn); 1114 if (section_is_overlay (section) 1115 && !section_is_mapped (section)) 1116 { 1117 if (!query (_("WARNING!!! Destination is in " 1118 "unmapped overlay! Jump anyway? "))) 1119 { 1120 error (_("Not confirmed.")); 1121 /* NOTREACHED */ 1122 } 1123 } 1124 } 1125 1126 addr = sal.pc; 1127 1128 if (from_tty) 1129 { 1130 printf_filtered (_("Continuing at ")); 1131 fputs_filtered (paddress (gdbarch, addr), gdb_stdout); 1132 printf_filtered (".\n"); 1133 } 1134 1135 clear_proceed_status (0); 1136 proceed (addr, GDB_SIGNAL_0); 1137 } 1138 1139 /* Continue program giving it specified signal. */ 1140 1141 static void 1142 signal_command (const char *signum_exp, int from_tty) 1143 { 1144 enum gdb_signal oursig; 1145 int async_exec; 1146 1147 dont_repeat (); /* Too dangerous. */ 1148 ERROR_NO_INFERIOR; 1149 ensure_not_tfind_mode (); 1150 ensure_valid_thread (); 1151 ensure_not_running (); 1152 1153 /* Find out whether we must run in the background. */ 1154 gdb::unique_xmalloc_ptr<char> stripped 1155 = strip_bg_char (signum_exp, &async_exec); 1156 signum_exp = stripped.get (); 1157 1158 prepare_execution_command (current_top_target (), async_exec); 1159 1160 if (!signum_exp) 1161 error_no_arg (_("signal number")); 1162 1163 /* It would be even slicker to make signal names be valid expressions, 1164 (the type could be "enum $signal" or some such), then the user could 1165 assign them to convenience variables. */ 1166 oursig = gdb_signal_from_name (signum_exp); 1167 1168 if (oursig == GDB_SIGNAL_UNKNOWN) 1169 { 1170 /* No, try numeric. */ 1171 int num = parse_and_eval_long (signum_exp); 1172 1173 if (num == 0) 1174 oursig = GDB_SIGNAL_0; 1175 else 1176 oursig = gdb_signal_from_command (num); 1177 } 1178 1179 /* Look for threads other than the current that this command ends up 1180 resuming too (due to schedlock off), and warn if they'll get a 1181 signal delivered. "signal 0" is used to suppress a previous 1182 signal, but if the current thread is no longer the one that got 1183 the signal, then the user is potentially suppressing the signal 1184 of the wrong thread. */ 1185 if (!non_stop) 1186 { 1187 int must_confirm = 0; 1188 1189 /* This indicates what will be resumed. Either a single thread, 1190 a whole process, or all threads of all processes. */ 1191 ptid_t resume_ptid = user_visible_resume_ptid (0); 1192 process_stratum_target *resume_target 1193 = user_visible_resume_target (resume_ptid); 1194 1195 thread_info *current = inferior_thread (); 1196 1197 for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid)) 1198 { 1199 if (tp == current) 1200 continue; 1201 1202 if (tp->suspend.stop_signal != GDB_SIGNAL_0 1203 && signal_pass_state (tp->suspend.stop_signal)) 1204 { 1205 if (!must_confirm) 1206 printf_unfiltered (_("Note:\n")); 1207 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"), 1208 print_thread_id (tp), 1209 gdb_signal_to_name (tp->suspend.stop_signal), 1210 gdb_signal_to_string (tp->suspend.stop_signal)); 1211 must_confirm = 1; 1212 } 1213 } 1214 1215 if (must_confirm 1216 && !query (_("Continuing thread %s (the current thread) with specified signal will\n" 1217 "still deliver the signals noted above to their respective threads.\n" 1218 "Continue anyway? "), 1219 print_thread_id (inferior_thread ()))) 1220 error (_("Not confirmed.")); 1221 } 1222 1223 if (from_tty) 1224 { 1225 if (oursig == GDB_SIGNAL_0) 1226 printf_filtered (_("Continuing with no signal.\n")); 1227 else 1228 printf_filtered (_("Continuing with signal %s.\n"), 1229 gdb_signal_to_name (oursig)); 1230 } 1231 1232 clear_proceed_status (0); 1233 proceed ((CORE_ADDR) -1, oursig); 1234 } 1235 1236 /* Queue a signal to be delivered to the current thread. */ 1237 1238 static void 1239 queue_signal_command (const char *signum_exp, int from_tty) 1240 { 1241 enum gdb_signal oursig; 1242 struct thread_info *tp; 1243 1244 ERROR_NO_INFERIOR; 1245 ensure_not_tfind_mode (); 1246 ensure_valid_thread (); 1247 ensure_not_running (); 1248 1249 if (signum_exp == NULL) 1250 error_no_arg (_("signal number")); 1251 1252 /* It would be even slicker to make signal names be valid expressions, 1253 (the type could be "enum $signal" or some such), then the user could 1254 assign them to convenience variables. */ 1255 oursig = gdb_signal_from_name (signum_exp); 1256 1257 if (oursig == GDB_SIGNAL_UNKNOWN) 1258 { 1259 /* No, try numeric. */ 1260 int num = parse_and_eval_long (signum_exp); 1261 1262 if (num == 0) 1263 oursig = GDB_SIGNAL_0; 1264 else 1265 oursig = gdb_signal_from_command (num); 1266 } 1267 1268 if (oursig != GDB_SIGNAL_0 1269 && !signal_pass_state (oursig)) 1270 error (_("Signal handling set to not pass this signal to the program.")); 1271 1272 tp = inferior_thread (); 1273 tp->suspend.stop_signal = oursig; 1274 } 1275 1276 /* Data for the FSM that manages the until (with no argument) 1277 command. */ 1278 1279 struct until_next_fsm : public thread_fsm 1280 { 1281 /* The thread that as current when the command was executed. */ 1282 int thread; 1283 1284 until_next_fsm (struct interp *cmd_interp, int thread) 1285 : thread_fsm (cmd_interp), 1286 thread (thread) 1287 { 1288 } 1289 1290 bool should_stop (struct thread_info *thread) override; 1291 void clean_up (struct thread_info *thread) override; 1292 enum async_reply_reason do_async_reply_reason () override; 1293 }; 1294 1295 /* Implementation of the 'should_stop' FSM method for the until (with 1296 no arg) command. */ 1297 1298 bool 1299 until_next_fsm::should_stop (struct thread_info *tp) 1300 { 1301 if (tp->control.stop_step) 1302 set_finished (); 1303 1304 return true; 1305 } 1306 1307 /* Implementation of the 'clean_up' FSM method for the until (with no 1308 arg) command. */ 1309 1310 void 1311 until_next_fsm::clean_up (struct thread_info *thread) 1312 { 1313 delete_longjmp_breakpoint (thread->global_num); 1314 } 1315 1316 /* Implementation of the 'async_reply_reason' FSM method for the until 1317 (with no arg) command. */ 1318 1319 enum async_reply_reason 1320 until_next_fsm::do_async_reply_reason () 1321 { 1322 return EXEC_ASYNC_END_STEPPING_RANGE; 1323 } 1324 1325 /* Proceed until we reach a different source line with pc greater than 1326 our current one or exit the function. We skip calls in both cases. 1327 1328 Note that eventually this command should probably be changed so 1329 that only source lines are printed out when we hit the breakpoint 1330 we set. This may involve changes to wait_for_inferior and the 1331 proceed status code. */ 1332 1333 static void 1334 until_next_command (int from_tty) 1335 { 1336 struct frame_info *frame; 1337 CORE_ADDR pc; 1338 struct symbol *func; 1339 struct symtab_and_line sal; 1340 struct thread_info *tp = inferior_thread (); 1341 int thread = tp->global_num; 1342 struct until_next_fsm *sm; 1343 1344 clear_proceed_status (0); 1345 set_step_frame (tp); 1346 1347 frame = get_current_frame (); 1348 1349 /* Step until either exited from this function or greater 1350 than the current line (if in symbolic section) or pc (if 1351 not). */ 1352 1353 pc = get_frame_pc (frame); 1354 func = find_pc_function (pc); 1355 1356 if (!func) 1357 { 1358 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc); 1359 1360 if (msymbol.minsym == NULL) 1361 error (_("Execution is not within a known function.")); 1362 1363 tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol); 1364 /* The upper-bound of step_range is exclusive. In order to make PC 1365 within the range, set the step_range_end with PC + 1. */ 1366 tp->control.step_range_end = pc + 1; 1367 } 1368 else 1369 { 1370 sal = find_pc_line (pc, 0); 1371 1372 tp->control.step_range_start = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func)); 1373 tp->control.step_range_end = sal.end; 1374 } 1375 tp->control.may_range_step = 1; 1376 1377 tp->control.step_over_calls = STEP_OVER_ALL; 1378 1379 set_longjmp_breakpoint (tp, get_frame_id (frame)); 1380 delete_longjmp_breakpoint_cleanup lj_deleter (thread); 1381 1382 sm = new until_next_fsm (command_interp (), tp->global_num); 1383 tp->thread_fsm = sm; 1384 lj_deleter.release (); 1385 1386 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 1387 } 1388 1389 static void 1390 until_command (const char *arg, int from_tty) 1391 { 1392 int async_exec; 1393 1394 ERROR_NO_INFERIOR; 1395 ensure_not_tfind_mode (); 1396 ensure_valid_thread (); 1397 ensure_not_running (); 1398 1399 /* Find out whether we must run in the background. */ 1400 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); 1401 arg = stripped.get (); 1402 1403 prepare_execution_command (current_top_target (), async_exec); 1404 1405 if (arg) 1406 until_break_command (arg, from_tty, 0); 1407 else 1408 until_next_command (from_tty); 1409 } 1410 1411 static void 1412 advance_command (const char *arg, int from_tty) 1413 { 1414 int async_exec; 1415 1416 ERROR_NO_INFERIOR; 1417 ensure_not_tfind_mode (); 1418 ensure_valid_thread (); 1419 ensure_not_running (); 1420 1421 if (arg == NULL) 1422 error_no_arg (_("a location")); 1423 1424 /* Find out whether we must run in the background. */ 1425 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); 1426 arg = stripped.get (); 1427 1428 prepare_execution_command (current_top_target (), async_exec); 1429 1430 until_break_command (arg, from_tty, 1); 1431 } 1432 1433 /* Return the value of the result of a function at the end of a 'finish' 1434 command/BP. DTOR_DATA (if not NULL) can represent inferior registers 1435 right after an inferior call has finished. */ 1436 1437 struct value * 1438 get_return_value (struct value *function, struct type *value_type) 1439 { 1440 regcache *stop_regs = get_current_regcache (); 1441 struct gdbarch *gdbarch = stop_regs->arch (); 1442 struct value *value; 1443 1444 value_type = check_typedef (value_type); 1445 gdb_assert (value_type->code () != TYPE_CODE_VOID); 1446 1447 /* FIXME: 2003-09-27: When returning from a nested inferior function 1448 call, it's possible (with no help from the architecture vector) 1449 to locate and return/print a "struct return" value. This is just 1450 a more complicated case of what is already being done in the 1451 inferior function call code. In fact, when inferior function 1452 calls are made async, this will likely be made the norm. */ 1453 1454 switch (gdbarch_return_value (gdbarch, function, value_type, 1455 NULL, NULL, NULL)) 1456 { 1457 case RETURN_VALUE_REGISTER_CONVENTION: 1458 case RETURN_VALUE_ABI_RETURNS_ADDRESS: 1459 case RETURN_VALUE_ABI_PRESERVES_ADDRESS: 1460 value = allocate_value (value_type); 1461 gdbarch_return_value (gdbarch, function, value_type, stop_regs, 1462 value_contents_raw (value), NULL); 1463 break; 1464 case RETURN_VALUE_STRUCT_CONVENTION: 1465 value = NULL; 1466 break; 1467 default: 1468 internal_error (__FILE__, __LINE__, _("bad switch")); 1469 } 1470 1471 return value; 1472 } 1473 1474 /* The captured function return value/type and its position in the 1475 value history. */ 1476 1477 struct return_value_info 1478 { 1479 /* The captured return value. May be NULL if we weren't able to 1480 retrieve it. See get_return_value. */ 1481 struct value *value; 1482 1483 /* The return type. In some cases, we'll not be able extract the 1484 return value, but we always know the type. */ 1485 struct type *type; 1486 1487 /* If we captured a value, this is the value history index. */ 1488 int value_history_index; 1489 }; 1490 1491 /* Helper for print_return_value. */ 1492 1493 static void 1494 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv) 1495 { 1496 if (rv->value != NULL) 1497 { 1498 struct value_print_options opts; 1499 1500 /* Print it. */ 1501 uiout->text ("Value returned is "); 1502 uiout->field_fmt ("gdb-result-var", "$%d", 1503 rv->value_history_index); 1504 uiout->text (" = "); 1505 get_user_print_options (&opts); 1506 1507 if (opts.finish_print) 1508 { 1509 string_file stb; 1510 value_print (rv->value, &stb, &opts); 1511 uiout->field_stream ("return-value", stb); 1512 } 1513 else 1514 uiout->field_string ("return-value", _("<not displayed>"), 1515 metadata_style.style ()); 1516 uiout->text ("\n"); 1517 } 1518 else 1519 { 1520 std::string type_name = type_to_string (rv->type); 1521 uiout->text ("Value returned has type: "); 1522 uiout->field_string ("return-type", type_name.c_str ()); 1523 uiout->text ("."); 1524 uiout->text (" Cannot determine contents\n"); 1525 } 1526 } 1527 1528 /* Print the result of a function at the end of a 'finish' command. 1529 RV points at an object representing the captured return value/type 1530 and its position in the value history. */ 1531 1532 void 1533 print_return_value (struct ui_out *uiout, struct return_value_info *rv) 1534 { 1535 if (rv->type == NULL 1536 || check_typedef (rv->type)->code () == TYPE_CODE_VOID) 1537 return; 1538 1539 try 1540 { 1541 /* print_return_value_1 can throw an exception in some 1542 circumstances. We need to catch this so that we still 1543 delete the breakpoint. */ 1544 print_return_value_1 (uiout, rv); 1545 } 1546 catch (const gdb_exception &ex) 1547 { 1548 exception_print (gdb_stdout, ex); 1549 } 1550 } 1551 1552 /* Data for the FSM that manages the finish command. */ 1553 1554 struct finish_command_fsm : public thread_fsm 1555 { 1556 /* The momentary breakpoint set at the function's return address in 1557 the caller. */ 1558 breakpoint_up breakpoint; 1559 1560 /* The function that we're stepping out of. */ 1561 struct symbol *function = nullptr; 1562 1563 /* If the FSM finishes successfully, this stores the function's 1564 return value. */ 1565 struct return_value_info return_value_info {}; 1566 1567 explicit finish_command_fsm (struct interp *cmd_interp) 1568 : thread_fsm (cmd_interp) 1569 { 1570 } 1571 1572 bool should_stop (struct thread_info *thread) override; 1573 void clean_up (struct thread_info *thread) override; 1574 struct return_value_info *return_value () override; 1575 enum async_reply_reason do_async_reply_reason () override; 1576 }; 1577 1578 /* Implementation of the 'should_stop' FSM method for the finish 1579 commands. Detects whether the thread stepped out of the function 1580 successfully, and if so, captures the function's return value and 1581 marks the FSM finished. */ 1582 1583 bool 1584 finish_command_fsm::should_stop (struct thread_info *tp) 1585 { 1586 struct return_value_info *rv = &return_value_info; 1587 1588 if (function != NULL 1589 && bpstat_find_breakpoint (tp->control.stop_bpstat, 1590 breakpoint.get ()) != NULL) 1591 { 1592 /* We're done. */ 1593 set_finished (); 1594 1595 rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function)); 1596 if (rv->type == NULL) 1597 internal_error (__FILE__, __LINE__, 1598 _("finish_command: function has no target type")); 1599 1600 if (check_typedef (rv->type)->code () != TYPE_CODE_VOID) 1601 { 1602 struct value *func; 1603 1604 func = read_var_value (function, NULL, get_current_frame ()); 1605 rv->value = get_return_value (func, rv->type); 1606 if (rv->value != NULL) 1607 rv->value_history_index = record_latest_value (rv->value); 1608 } 1609 } 1610 else if (tp->control.stop_step) 1611 { 1612 /* Finishing from an inline frame, or reverse finishing. In 1613 either case, there's no way to retrieve the return value. */ 1614 set_finished (); 1615 } 1616 1617 return true; 1618 } 1619 1620 /* Implementation of the 'clean_up' FSM method for the finish 1621 commands. */ 1622 1623 void 1624 finish_command_fsm::clean_up (struct thread_info *thread) 1625 { 1626 breakpoint.reset (); 1627 delete_longjmp_breakpoint (thread->global_num); 1628 } 1629 1630 /* Implementation of the 'return_value' FSM method for the finish 1631 commands. */ 1632 1633 struct return_value_info * 1634 finish_command_fsm::return_value () 1635 { 1636 return &return_value_info; 1637 } 1638 1639 /* Implementation of the 'async_reply_reason' FSM method for the 1640 finish commands. */ 1641 1642 enum async_reply_reason 1643 finish_command_fsm::do_async_reply_reason () 1644 { 1645 if (execution_direction == EXEC_REVERSE) 1646 return EXEC_ASYNC_END_STEPPING_RANGE; 1647 else 1648 return EXEC_ASYNC_FUNCTION_FINISHED; 1649 } 1650 1651 /* finish_backward -- helper function for finish_command. */ 1652 1653 static void 1654 finish_backward (struct finish_command_fsm *sm) 1655 { 1656 struct symtab_and_line sal; 1657 struct thread_info *tp = inferior_thread (); 1658 CORE_ADDR pc; 1659 CORE_ADDR func_addr; 1660 1661 pc = get_frame_pc (get_current_frame ()); 1662 1663 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0) 1664 error (_("Cannot find bounds of current function")); 1665 1666 sal = find_pc_line (func_addr, 0); 1667 1668 tp->control.proceed_to_finish = 1; 1669 /* Special case: if we're sitting at the function entry point, 1670 then all we need to do is take a reverse singlestep. We 1671 don't need to set a breakpoint, and indeed it would do us 1672 no good to do so. 1673 1674 Note that this can only happen at frame #0, since there's 1675 no way that a function up the stack can have a return address 1676 that's equal to its entry point. */ 1677 1678 if (sal.pc != pc) 1679 { 1680 struct frame_info *frame = get_selected_frame (NULL); 1681 struct gdbarch *gdbarch = get_frame_arch (frame); 1682 1683 /* Set a step-resume at the function's entry point. Once that's 1684 hit, we'll do one more step backwards. */ 1685 symtab_and_line sr_sal; 1686 sr_sal.pc = sal.pc; 1687 sr_sal.pspace = get_frame_program_space (frame); 1688 insert_step_resume_breakpoint_at_sal (gdbarch, 1689 sr_sal, null_frame_id); 1690 1691 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 1692 } 1693 else 1694 { 1695 /* We're almost there -- we just need to back up by one more 1696 single-step. */ 1697 tp->control.step_range_start = tp->control.step_range_end = 1; 1698 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 1699 } 1700 } 1701 1702 /* finish_forward -- helper function for finish_command. FRAME is the 1703 frame that called the function we're about to step out of. */ 1704 1705 static void 1706 finish_forward (struct finish_command_fsm *sm, struct frame_info *frame) 1707 { 1708 struct frame_id frame_id = get_frame_id (frame); 1709 struct gdbarch *gdbarch = get_frame_arch (frame); 1710 struct symtab_and_line sal; 1711 struct thread_info *tp = inferior_thread (); 1712 1713 sal = find_pc_line (get_frame_pc (frame), 0); 1714 sal.pc = get_frame_pc (frame); 1715 1716 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal, 1717 get_stack_frame_id (frame), 1718 bp_finish); 1719 1720 /* set_momentary_breakpoint invalidates FRAME. */ 1721 frame = NULL; 1722 1723 set_longjmp_breakpoint (tp, frame_id); 1724 1725 /* We want to print return value, please... */ 1726 tp->control.proceed_to_finish = 1; 1727 1728 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 1729 } 1730 1731 /* Skip frames for "finish". */ 1732 1733 static struct frame_info * 1734 skip_finish_frames (struct frame_info *frame) 1735 { 1736 struct frame_info *start; 1737 1738 do 1739 { 1740 start = frame; 1741 1742 frame = skip_tailcall_frames (frame); 1743 if (frame == NULL) 1744 break; 1745 1746 frame = skip_unwritable_frames (frame); 1747 if (frame == NULL) 1748 break; 1749 } 1750 while (start != frame); 1751 1752 return frame; 1753 } 1754 1755 /* "finish": Set a temporary breakpoint at the place the selected 1756 frame will return to, then continue. */ 1757 1758 static void 1759 finish_command (const char *arg, int from_tty) 1760 { 1761 struct frame_info *frame; 1762 int async_exec; 1763 struct finish_command_fsm *sm; 1764 struct thread_info *tp; 1765 1766 ERROR_NO_INFERIOR; 1767 ensure_not_tfind_mode (); 1768 ensure_valid_thread (); 1769 ensure_not_running (); 1770 1771 /* Find out whether we must run in the background. */ 1772 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); 1773 arg = stripped.get (); 1774 1775 prepare_execution_command (current_top_target (), async_exec); 1776 1777 if (arg) 1778 error (_("The \"finish\" command does not take any arguments.")); 1779 1780 frame = get_prev_frame (get_selected_frame (_("No selected frame."))); 1781 if (frame == 0) 1782 error (_("\"finish\" not meaningful in the outermost frame.")); 1783 1784 clear_proceed_status (0); 1785 1786 tp = inferior_thread (); 1787 1788 sm = new finish_command_fsm (command_interp ()); 1789 1790 tp->thread_fsm = sm; 1791 1792 /* Finishing from an inline frame is completely different. We don't 1793 try to show the "return value" - no way to locate it. */ 1794 if (get_frame_type (get_selected_frame (_("No selected frame."))) 1795 == INLINE_FRAME) 1796 { 1797 /* Claim we are stepping in the calling frame. An empty step 1798 range means that we will stop once we aren't in a function 1799 called by that frame. We don't use the magic "1" value for 1800 step_range_end, because then infrun will think this is nexti, 1801 and not step over the rest of this inlined function call. */ 1802 set_step_info (tp, frame, {}); 1803 tp->control.step_range_start = get_frame_pc (frame); 1804 tp->control.step_range_end = tp->control.step_range_start; 1805 tp->control.step_over_calls = STEP_OVER_ALL; 1806 1807 /* Print info on the selected frame, including level number but not 1808 source. */ 1809 if (from_tty) 1810 { 1811 printf_filtered (_("Run till exit from ")); 1812 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); 1813 } 1814 1815 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 1816 return; 1817 } 1818 1819 /* Find the function we will return from. */ 1820 1821 sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL))); 1822 1823 /* Print info on the selected frame, including level number but not 1824 source. */ 1825 if (from_tty) 1826 { 1827 if (execution_direction == EXEC_REVERSE) 1828 printf_filtered (_("Run back to call of ")); 1829 else 1830 { 1831 if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type) 1832 && !query (_("warning: Function %s does not return normally.\n" 1833 "Try to finish anyway? "), 1834 sm->function->print_name ())) 1835 error (_("Not confirmed.")); 1836 printf_filtered (_("Run till exit from ")); 1837 } 1838 1839 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); 1840 } 1841 1842 if (execution_direction == EXEC_REVERSE) 1843 finish_backward (sm); 1844 else 1845 { 1846 frame = skip_finish_frames (frame); 1847 1848 if (frame == NULL) 1849 error (_("Cannot find the caller frame.")); 1850 1851 finish_forward (sm, frame); 1852 } 1853 } 1854 1855 1856 static void 1857 info_program_command (const char *args, int from_tty) 1858 { 1859 bpstat bs; 1860 int num, stat; 1861 ptid_t ptid; 1862 process_stratum_target *proc_target; 1863 1864 if (!target_has_execution) 1865 { 1866 printf_filtered (_("The program being debugged is not being run.\n")); 1867 return; 1868 } 1869 1870 if (non_stop) 1871 { 1872 ptid = inferior_ptid; 1873 proc_target = current_inferior ()->process_target (); 1874 } 1875 else 1876 get_last_target_status (&proc_target, &ptid, nullptr); 1877 1878 if (ptid == null_ptid || ptid == minus_one_ptid) 1879 error (_("No selected thread.")); 1880 1881 thread_info *tp = find_thread_ptid (proc_target, ptid); 1882 1883 if (tp->state == THREAD_EXITED) 1884 error (_("Invalid selected thread.")); 1885 else if (tp->state == THREAD_RUNNING) 1886 error (_("Selected thread is running.")); 1887 1888 bs = tp->control.stop_bpstat; 1889 stat = bpstat_num (&bs, &num); 1890 1891 target_files_info (); 1892 printf_filtered (_("Program stopped at %s.\n"), 1893 paddress (target_gdbarch (), tp->suspend.stop_pc)); 1894 if (tp->control.stop_step) 1895 printf_filtered (_("It stopped after being stepped.\n")); 1896 else if (stat != 0) 1897 { 1898 /* There may be several breakpoints in the same place, so this 1899 isn't as strange as it seems. */ 1900 while (stat != 0) 1901 { 1902 if (stat < 0) 1903 { 1904 printf_filtered (_("It stopped at a breakpoint " 1905 "that has since been deleted.\n")); 1906 } 1907 else 1908 printf_filtered (_("It stopped at breakpoint %d.\n"), num); 1909 stat = bpstat_num (&bs, &num); 1910 } 1911 } 1912 else if (tp->suspend.stop_signal != GDB_SIGNAL_0) 1913 { 1914 printf_filtered (_("It stopped with signal %s, %s.\n"), 1915 gdb_signal_to_name (tp->suspend.stop_signal), 1916 gdb_signal_to_string (tp->suspend.stop_signal)); 1917 } 1918 1919 if (from_tty) 1920 { 1921 printf_filtered (_("Type \"info stack\" or \"info " 1922 "registers\" for more information.\n")); 1923 } 1924 } 1925 1926 static void 1927 environment_info (const char *var, int from_tty) 1928 { 1929 if (var) 1930 { 1931 const char *val = current_inferior ()->environment.get (var); 1932 1933 if (val) 1934 { 1935 puts_filtered (var); 1936 puts_filtered (" = "); 1937 puts_filtered (val); 1938 puts_filtered ("\n"); 1939 } 1940 else 1941 { 1942 puts_filtered ("Environment variable \""); 1943 puts_filtered (var); 1944 puts_filtered ("\" not defined.\n"); 1945 } 1946 } 1947 else 1948 { 1949 char **envp = current_inferior ()->environment.envp (); 1950 1951 for (int idx = 0; envp[idx] != NULL; ++idx) 1952 { 1953 puts_filtered (envp[idx]); 1954 puts_filtered ("\n"); 1955 } 1956 } 1957 } 1958 1959 static void 1960 set_environment_command (const char *arg, int from_tty) 1961 { 1962 const char *p, *val; 1963 int nullset = 0; 1964 1965 if (arg == 0) 1966 error_no_arg (_("environment variable and value")); 1967 1968 /* Find separation between variable name and value. */ 1969 p = (char *) strchr (arg, '='); 1970 val = (char *) strchr (arg, ' '); 1971 1972 if (p != 0 && val != 0) 1973 { 1974 /* We have both a space and an equals. If the space is before the 1975 equals, walk forward over the spaces til we see a nonspace 1976 (possibly the equals). */ 1977 if (p > val) 1978 while (*val == ' ') 1979 val++; 1980 1981 /* Now if the = is after the char following the spaces, 1982 take the char following the spaces. */ 1983 if (p > val) 1984 p = val - 1; 1985 } 1986 else if (val != 0 && p == 0) 1987 p = val; 1988 1989 if (p == arg) 1990 error_no_arg (_("environment variable to set")); 1991 1992 if (p == 0 || p[1] == 0) 1993 { 1994 nullset = 1; 1995 if (p == 0) 1996 p = arg + strlen (arg); /* So that savestring below will work. */ 1997 } 1998 else 1999 { 2000 /* Not setting variable value to null. */ 2001 val = p + 1; 2002 while (*val == ' ' || *val == '\t') 2003 val++; 2004 } 2005 2006 while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) 2007 p--; 2008 2009 std::string var (arg, p - arg); 2010 if (nullset) 2011 { 2012 printf_filtered (_("Setting environment variable " 2013 "\"%s\" to null value.\n"), 2014 var.c_str ()); 2015 current_inferior ()->environment.set (var.c_str (), ""); 2016 } 2017 else 2018 current_inferior ()->environment.set (var.c_str (), val); 2019 } 2020 2021 static void 2022 unset_environment_command (const char *var, int from_tty) 2023 { 2024 if (var == 0) 2025 { 2026 /* If there is no argument, delete all environment variables. 2027 Ask for confirmation if reading from the terminal. */ 2028 if (!from_tty || query (_("Delete all environment variables? "))) 2029 current_inferior ()->environment.clear (); 2030 } 2031 else 2032 current_inferior ()->environment.unset (var); 2033 } 2034 2035 /* Handle the execution path (PATH variable). */ 2036 2037 static const char path_var_name[] = "PATH"; 2038 2039 static void 2040 path_info (const char *args, int from_tty) 2041 { 2042 puts_filtered ("Executable and object file path: "); 2043 puts_filtered (current_inferior ()->environment.get (path_var_name)); 2044 puts_filtered ("\n"); 2045 } 2046 2047 /* Add zero or more directories to the front of the execution path. */ 2048 2049 static void 2050 path_command (const char *dirname, int from_tty) 2051 { 2052 char *exec_path; 2053 const char *env; 2054 2055 dont_repeat (); 2056 env = current_inferior ()->environment.get (path_var_name); 2057 /* Can be null if path is not set. */ 2058 if (!env) 2059 env = ""; 2060 exec_path = xstrdup (env); 2061 mod_path (dirname, &exec_path); 2062 current_inferior ()->environment.set (path_var_name, exec_path); 2063 xfree (exec_path); 2064 if (from_tty) 2065 path_info (NULL, from_tty); 2066 } 2067 2068 2069 static void 2070 pad_to_column (string_file &stream, int col) 2071 { 2072 /* At least one space must be printed to separate columns. */ 2073 stream.putc (' '); 2074 const int size = stream.size (); 2075 if (size < col) 2076 stream.puts (n_spaces (col - size)); 2077 } 2078 2079 /* Print out the register NAME with value VAL, to FILE, in the default 2080 fashion. */ 2081 2082 static void 2083 default_print_one_register_info (struct ui_file *file, 2084 const char *name, 2085 struct value *val) 2086 { 2087 struct type *regtype = value_type (val); 2088 int print_raw_format; 2089 string_file format_stream; 2090 enum tab_stops 2091 { 2092 value_column_1 = 15, 2093 /* Give enough room for "0x", 16 hex digits and two spaces in 2094 preceding column. */ 2095 value_column_2 = value_column_1 + 2 + 16 + 2, 2096 }; 2097 2098 format_stream.puts (name); 2099 pad_to_column (format_stream, value_column_1); 2100 2101 print_raw_format = (value_entirely_available (val) 2102 && !value_optimized_out (val)); 2103 2104 /* If virtual format is floating, print it that way, and in raw 2105 hex. */ 2106 if (regtype->code () == TYPE_CODE_FLT 2107 || regtype->code () == TYPE_CODE_DECFLOAT) 2108 { 2109 struct value_print_options opts; 2110 const gdb_byte *valaddr = value_contents_for_printing (val); 2111 enum bfd_endian byte_order = type_byte_order (regtype); 2112 2113 get_user_print_options (&opts); 2114 opts.deref_ref = 1; 2115 2116 common_val_print (val, &format_stream, 0, &opts, current_language); 2117 2118 if (print_raw_format) 2119 { 2120 pad_to_column (format_stream, value_column_2); 2121 format_stream.puts ("(raw "); 2122 print_hex_chars (&format_stream, valaddr, TYPE_LENGTH (regtype), 2123 byte_order, true); 2124 format_stream.putc (')'); 2125 } 2126 } 2127 else 2128 { 2129 struct value_print_options opts; 2130 2131 /* Print the register in hex. */ 2132 get_formatted_print_options (&opts, 'x'); 2133 opts.deref_ref = 1; 2134 common_val_print (val, &format_stream, 0, &opts, current_language); 2135 /* If not a vector register, print it also according to its 2136 natural format. */ 2137 if (print_raw_format && TYPE_VECTOR (regtype) == 0) 2138 { 2139 pad_to_column (format_stream, value_column_2); 2140 get_user_print_options (&opts); 2141 opts.deref_ref = 1; 2142 common_val_print (val, &format_stream, 0, &opts, current_language); 2143 } 2144 } 2145 2146 fputs_filtered (format_stream.c_str (), file); 2147 fprintf_filtered (file, "\n"); 2148 } 2149 2150 /* Print out the machine register regnum. If regnum is -1, print all 2151 registers (print_all == 1) or all non-float and non-vector 2152 registers (print_all == 0). 2153 2154 For most machines, having all_registers_info() print the 2155 register(s) one per line is good enough. If a different format is 2156 required, (eg, for MIPS or Pyramid 90x, which both have lots of 2157 regs), or there is an existing convention for showing all the 2158 registers, define the architecture method PRINT_REGISTERS_INFO to 2159 provide that format. */ 2160 2161 void 2162 default_print_registers_info (struct gdbarch *gdbarch, 2163 struct ui_file *file, 2164 struct frame_info *frame, 2165 int regnum, int print_all) 2166 { 2167 int i; 2168 const int numregs = gdbarch_num_cooked_regs (gdbarch); 2169 2170 for (i = 0; i < numregs; i++) 2171 { 2172 /* Decide between printing all regs, non-float / vector regs, or 2173 specific reg. */ 2174 if (regnum == -1) 2175 { 2176 if (print_all) 2177 { 2178 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup)) 2179 continue; 2180 } 2181 else 2182 { 2183 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup)) 2184 continue; 2185 } 2186 } 2187 else 2188 { 2189 if (i != regnum) 2190 continue; 2191 } 2192 2193 /* If the register name is empty, it is undefined for this 2194 processor, so don't display anything. */ 2195 if (gdbarch_register_name (gdbarch, i) == NULL 2196 || *(gdbarch_register_name (gdbarch, i)) == '\0') 2197 continue; 2198 2199 default_print_one_register_info (file, 2200 gdbarch_register_name (gdbarch, i), 2201 value_of_register (i, frame)); 2202 } 2203 } 2204 2205 void 2206 registers_info (const char *addr_exp, int fpregs) 2207 { 2208 struct frame_info *frame; 2209 struct gdbarch *gdbarch; 2210 2211 if (!target_has_registers) 2212 error (_("The program has no registers now.")); 2213 frame = get_selected_frame (NULL); 2214 gdbarch = get_frame_arch (frame); 2215 2216 if (!addr_exp) 2217 { 2218 gdbarch_print_registers_info (gdbarch, gdb_stdout, 2219 frame, -1, fpregs); 2220 return; 2221 } 2222 2223 while (*addr_exp != '\0') 2224 { 2225 const char *start; 2226 const char *end; 2227 2228 /* Skip leading white space. */ 2229 addr_exp = skip_spaces (addr_exp); 2230 2231 /* Discard any leading ``$''. Check that there is something 2232 resembling a register following it. */ 2233 if (addr_exp[0] == '$') 2234 addr_exp++; 2235 if (isspace ((*addr_exp)) || (*addr_exp) == '\0') 2236 error (_("Missing register name")); 2237 2238 /* Find the start/end of this register name/num/group. */ 2239 start = addr_exp; 2240 while ((*addr_exp) != '\0' && !isspace ((*addr_exp))) 2241 addr_exp++; 2242 end = addr_exp; 2243 2244 /* Figure out what we've found and display it. */ 2245 2246 /* A register name? */ 2247 { 2248 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start); 2249 2250 if (regnum >= 0) 2251 { 2252 /* User registers lie completely outside of the range of 2253 normal registers. Catch them early so that the target 2254 never sees them. */ 2255 if (regnum >= gdbarch_num_cooked_regs (gdbarch)) 2256 { 2257 struct value *regval = value_of_user_reg (regnum, frame); 2258 const char *regname = user_reg_map_regnum_to_name (gdbarch, 2259 regnum); 2260 2261 /* Print in the same fashion 2262 gdbarch_print_registers_info's default 2263 implementation prints. */ 2264 default_print_one_register_info (gdb_stdout, 2265 regname, 2266 regval); 2267 } 2268 else 2269 gdbarch_print_registers_info (gdbarch, gdb_stdout, 2270 frame, regnum, fpregs); 2271 continue; 2272 } 2273 } 2274 2275 /* A register group? */ 2276 { 2277 struct reggroup *group; 2278 2279 for (group = reggroup_next (gdbarch, NULL); 2280 group != NULL; 2281 group = reggroup_next (gdbarch, group)) 2282 { 2283 /* Don't bother with a length check. Should the user 2284 enter a short register group name, go with the first 2285 group that matches. */ 2286 if (strncmp (start, reggroup_name (group), end - start) == 0) 2287 break; 2288 } 2289 if (group != NULL) 2290 { 2291 int regnum; 2292 2293 for (regnum = 0; 2294 regnum < gdbarch_num_cooked_regs (gdbarch); 2295 regnum++) 2296 { 2297 if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) 2298 gdbarch_print_registers_info (gdbarch, 2299 gdb_stdout, frame, 2300 regnum, fpregs); 2301 } 2302 continue; 2303 } 2304 } 2305 2306 /* Nothing matched. */ 2307 error (_("Invalid register `%.*s'"), (int) (end - start), start); 2308 } 2309 } 2310 2311 static void 2312 info_all_registers_command (const char *addr_exp, int from_tty) 2313 { 2314 registers_info (addr_exp, 1); 2315 } 2316 2317 static void 2318 info_registers_command (const char *addr_exp, int from_tty) 2319 { 2320 registers_info (addr_exp, 0); 2321 } 2322 2323 static void 2324 print_vector_info (struct ui_file *file, 2325 struct frame_info *frame, const char *args) 2326 { 2327 struct gdbarch *gdbarch = get_frame_arch (frame); 2328 2329 if (gdbarch_print_vector_info_p (gdbarch)) 2330 gdbarch_print_vector_info (gdbarch, file, frame, args); 2331 else 2332 { 2333 int regnum; 2334 int printed_something = 0; 2335 2336 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) 2337 { 2338 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup)) 2339 { 2340 printed_something = 1; 2341 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); 2342 } 2343 } 2344 if (!printed_something) 2345 fprintf_filtered (file, "No vector information\n"); 2346 } 2347 } 2348 2349 static void 2350 info_vector_command (const char *args, int from_tty) 2351 { 2352 if (!target_has_registers) 2353 error (_("The program has no registers now.")); 2354 2355 print_vector_info (gdb_stdout, get_selected_frame (NULL), args); 2356 } 2357 2358 /* Kill the inferior process. Make us have no inferior. */ 2359 2360 static void 2361 kill_command (const char *arg, int from_tty) 2362 { 2363 /* FIXME: This should not really be inferior_ptid (or target_has_execution). 2364 It should be a distinct flag that indicates that a target is active, cuz 2365 some targets don't have processes! */ 2366 2367 if (inferior_ptid == null_ptid) 2368 error (_("The program is not being run.")); 2369 if (!query (_("Kill the program being debugged? "))) 2370 error (_("Not confirmed.")); 2371 2372 int pid = current_inferior ()->pid; 2373 /* Save the pid as a string before killing the inferior, since that 2374 may unpush the current target, and we need the string after. */ 2375 std::string pid_str = target_pid_to_str (ptid_t (pid)); 2376 int infnum = current_inferior ()->num; 2377 2378 target_kill (); 2379 2380 if (print_inferior_events) 2381 printf_unfiltered (_("[Inferior %d (%s) killed]\n"), 2382 infnum, pid_str.c_str ()); 2383 2384 bfd_cache_close_all (); 2385 } 2386 2387 /* Used in `attach&' command. Proceed threads of inferior INF iff 2388 they stopped due to debugger request, and when they did, they 2389 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that 2390 have been explicitly been told to stop. */ 2391 2392 static void 2393 proceed_after_attach (inferior *inf) 2394 { 2395 /* Don't error out if the current thread is running, because 2396 there may be other stopped threads. */ 2397 2398 /* Backup current thread and selected frame. */ 2399 scoped_restore_current_thread restore_thread; 2400 2401 for (thread_info *thread : inf->non_exited_threads ()) 2402 if (!thread->executing 2403 && !thread->stop_requested 2404 && thread->suspend.stop_signal == GDB_SIGNAL_0) 2405 { 2406 switch_to_thread (thread); 2407 clear_proceed_status (0); 2408 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 2409 } 2410 } 2411 2412 /* See inferior.h. */ 2413 2414 void 2415 setup_inferior (int from_tty) 2416 { 2417 struct inferior *inferior; 2418 2419 inferior = current_inferior (); 2420 inferior->needs_setup = 0; 2421 2422 /* If no exec file is yet known, try to determine it from the 2423 process itself. */ 2424 if (get_exec_file (0) == NULL) 2425 exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty); 2426 else 2427 { 2428 reopen_exec_file (); 2429 reread_symbols (); 2430 } 2431 2432 /* Take any necessary post-attaching actions for this platform. */ 2433 target_post_attach (inferior_ptid.pid ()); 2434 2435 post_create_inferior (current_top_target (), from_tty); 2436 } 2437 2438 /* What to do after the first program stops after attaching. */ 2439 enum attach_post_wait_mode 2440 { 2441 /* Do nothing. Leaves threads as they are. */ 2442 ATTACH_POST_WAIT_NOTHING, 2443 2444 /* Re-resume threads that are marked running. */ 2445 ATTACH_POST_WAIT_RESUME, 2446 2447 /* Stop all threads. */ 2448 ATTACH_POST_WAIT_STOP, 2449 }; 2450 2451 /* Called after we've attached to a process and we've seen it stop for 2452 the first time. If ASYNC_EXEC is true, re-resume threads that 2453 should be running. Else if ATTACH, */ 2454 2455 static void 2456 attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mode) 2457 { 2458 struct inferior *inferior; 2459 2460 inferior = current_inferior (); 2461 inferior->control.stop_soon = NO_STOP_QUIETLY; 2462 2463 if (inferior->needs_setup) 2464 setup_inferior (from_tty); 2465 2466 if (mode == ATTACH_POST_WAIT_RESUME) 2467 { 2468 /* The user requested an `attach&', so be sure to leave threads 2469 that didn't get a signal running. */ 2470 2471 /* Immediatelly resume all suspended threads of this inferior, 2472 and this inferior only. This should have no effect on 2473 already running threads. If a thread has been stopped with a 2474 signal, leave it be. */ 2475 if (non_stop) 2476 proceed_after_attach (inferior); 2477 else 2478 { 2479 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0) 2480 { 2481 clear_proceed_status (0); 2482 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); 2483 } 2484 } 2485 } 2486 else if (mode == ATTACH_POST_WAIT_STOP) 2487 { 2488 /* The user requested a plain `attach', so be sure to leave 2489 the inferior stopped. */ 2490 2491 /* At least the current thread is already stopped. */ 2492 2493 /* In all-stop, by definition, all threads have to be already 2494 stopped at this point. In non-stop, however, although the 2495 selected thread is stopped, others may still be executing. 2496 Be sure to explicitly stop all threads of the process. This 2497 should have no effect on already stopped threads. */ 2498 if (non_stop) 2499 target_stop (ptid_t (inferior->pid)); 2500 else if (target_is_non_stop_p ()) 2501 { 2502 struct thread_info *lowest = inferior_thread (); 2503 2504 stop_all_threads (); 2505 2506 /* It's not defined which thread will report the attach 2507 stop. For consistency, always select the thread with 2508 lowest GDB number, which should be the main thread, if it 2509 still exists. */ 2510 for (thread_info *thread : current_inferior ()->non_exited_threads ()) 2511 if (thread->inf->num < lowest->inf->num 2512 || thread->per_inf_num < lowest->per_inf_num) 2513 lowest = thread; 2514 2515 switch_to_thread (lowest); 2516 } 2517 2518 /* Tell the user/frontend where we're stopped. */ 2519 normal_stop (); 2520 if (deprecated_attach_hook) 2521 deprecated_attach_hook (); 2522 } 2523 } 2524 2525 struct attach_command_continuation_args 2526 { 2527 char *args; 2528 int from_tty; 2529 enum attach_post_wait_mode mode; 2530 }; 2531 2532 static void 2533 attach_command_continuation (void *args, int err) 2534 { 2535 struct attach_command_continuation_args *a 2536 = (struct attach_command_continuation_args *) args; 2537 2538 if (err) 2539 return; 2540 2541 attach_post_wait (a->args, a->from_tty, a->mode); 2542 } 2543 2544 static void 2545 attach_command_continuation_free_args (void *args) 2546 { 2547 struct attach_command_continuation_args *a 2548 = (struct attach_command_continuation_args *) args; 2549 2550 xfree (a->args); 2551 xfree (a); 2552 } 2553 2554 /* "attach" command entry point. Takes a program started up outside 2555 of gdb and ``attaches'' to it. This stops it cold in its tracks 2556 and allows us to start debugging it. */ 2557 2558 void 2559 attach_command (const char *args, int from_tty) 2560 { 2561 int async_exec; 2562 struct target_ops *attach_target; 2563 struct inferior *inferior = current_inferior (); 2564 enum attach_post_wait_mode mode; 2565 2566 dont_repeat (); /* Not for the faint of heart */ 2567 2568 if (gdbarch_has_global_solist (target_gdbarch ())) 2569 /* Don't complain if all processes share the same symbol 2570 space. */ 2571 ; 2572 else if (target_has_execution) 2573 { 2574 if (query (_("A program is being debugged already. Kill it? "))) 2575 target_kill (); 2576 else 2577 error (_("Not killed.")); 2578 } 2579 2580 /* Clean up any leftovers from other runs. Some other things from 2581 this function should probably be moved into target_pre_inferior. */ 2582 target_pre_inferior (from_tty); 2583 2584 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); 2585 args = stripped.get (); 2586 2587 attach_target = find_attach_target (); 2588 2589 prepare_execution_command (attach_target, async_exec); 2590 2591 if (non_stop && !attach_target->supports_non_stop ()) 2592 error (_("Cannot attach to this target in non-stop mode")); 2593 2594 attach_target->attach (args, from_tty); 2595 /* to_attach should push the target, so after this point we 2596 shouldn't refer to attach_target again. */ 2597 attach_target = NULL; 2598 2599 /* Set up the "saved terminal modes" of the inferior 2600 based on what modes we are starting it with. */ 2601 target_terminal::init (); 2602 2603 /* Install inferior's terminal modes. This may look like a no-op, 2604 as we've just saved them above, however, this does more than 2605 restore terminal settings: 2606 2607 - installs a SIGINT handler that forwards SIGINT to the inferior. 2608 Otherwise a Ctrl-C pressed just while waiting for the initial 2609 stop would end up as a spurious Quit. 2610 2611 - removes stdin from the event loop, which we need if attaching 2612 in the foreground, otherwise on targets that report an initial 2613 stop on attach (which are most) we'd process input/commands 2614 while we're in the event loop waiting for that stop. That is, 2615 before the attach continuation runs and the command is really 2616 finished. */ 2617 target_terminal::inferior (); 2618 2619 /* Set up execution context to know that we should return from 2620 wait_for_inferior as soon as the target reports a stop. */ 2621 init_wait_for_inferior (); 2622 clear_proceed_status (0); 2623 2624 inferior->needs_setup = 1; 2625 2626 if (target_is_non_stop_p ()) 2627 { 2628 /* If we find that the current thread isn't stopped, explicitly 2629 do so now, because we're going to install breakpoints and 2630 poke at memory. */ 2631 2632 if (async_exec) 2633 /* The user requested an `attach&'; stop just one thread. */ 2634 target_stop (inferior_ptid); 2635 else 2636 /* The user requested an `attach', so stop all threads of this 2637 inferior. */ 2638 target_stop (ptid_t (inferior_ptid.pid ())); 2639 } 2640 2641 /* Check for exec file mismatch, and let the user solve it. */ 2642 validate_exec_file (from_tty); 2643 2644 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP; 2645 2646 /* Some system don't generate traps when attaching to inferior. 2647 E.g. Mach 3 or GNU hurd. */ 2648 if (!target_attach_no_wait ()) 2649 { 2650 struct attach_command_continuation_args *a; 2651 2652 /* Careful here. See comments in inferior.h. Basically some 2653 OSes don't ignore SIGSTOPs on continue requests anymore. We 2654 need a way for handle_inferior_event to reset the stop_signal 2655 variable after an attach, and this is what 2656 STOP_QUIETLY_NO_SIGSTOP is for. */ 2657 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP; 2658 2659 /* Wait for stop. */ 2660 a = XNEW (struct attach_command_continuation_args); 2661 a->args = xstrdup (args); 2662 a->from_tty = from_tty; 2663 a->mode = mode; 2664 add_inferior_continuation (attach_command_continuation, a, 2665 attach_command_continuation_free_args); 2666 2667 /* Let infrun consider waiting for events out of this 2668 target. */ 2669 inferior->process_target ()->threads_executing = true; 2670 2671 if (!target_is_async_p ()) 2672 mark_infrun_async_event_handler (); 2673 return; 2674 } 2675 else 2676 attach_post_wait (args, from_tty, mode); 2677 } 2678 2679 /* We had just found out that the target was already attached to an 2680 inferior. PTID points at a thread of this new inferior, that is 2681 the most likely to be stopped right now, but not necessarily so. 2682 The new inferior is assumed to be already added to the inferior 2683 list at this point. If LEAVE_RUNNING, then leave the threads of 2684 this inferior running, except those we've explicitly seen reported 2685 as stopped. */ 2686 2687 void 2688 notice_new_inferior (thread_info *thr, int leave_running, int from_tty) 2689 { 2690 enum attach_post_wait_mode mode 2691 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING; 2692 2693 gdb::optional<scoped_restore_current_thread> restore_thread; 2694 2695 if (inferior_ptid != null_ptid) 2696 restore_thread.emplace (); 2697 2698 /* Avoid reading registers -- we haven't fetched the target 2699 description yet. */ 2700 switch_to_thread_no_regs (thr); 2701 2702 /* When we "notice" a new inferior we need to do all the things we 2703 would normally do if we had just attached to it. */ 2704 2705 if (thr->executing) 2706 { 2707 struct attach_command_continuation_args *a; 2708 struct inferior *inferior = current_inferior (); 2709 2710 /* We're going to install breakpoints, and poke at memory, 2711 ensure that the inferior is stopped for a moment while we do 2712 that. */ 2713 target_stop (inferior_ptid); 2714 2715 inferior->control.stop_soon = STOP_QUIETLY_REMOTE; 2716 2717 /* Wait for stop before proceeding. */ 2718 a = XNEW (struct attach_command_continuation_args); 2719 a->args = xstrdup (""); 2720 a->from_tty = from_tty; 2721 a->mode = mode; 2722 add_inferior_continuation (attach_command_continuation, a, 2723 attach_command_continuation_free_args); 2724 2725 return; 2726 } 2727 2728 attach_post_wait ("" /* args */, from_tty, mode); 2729 } 2730 2731 /* 2732 * detach_command -- 2733 * takes a program previously attached to and detaches it. 2734 * The program resumes execution and will no longer stop 2735 * on signals, etc. We better not have left any breakpoints 2736 * in the program or it'll die when it hits one. For this 2737 * to work, it may be necessary for the process to have been 2738 * previously attached. It *might* work if the program was 2739 * started via the normal ptrace (PTRACE_TRACEME). 2740 */ 2741 2742 void 2743 detach_command (const char *args, int from_tty) 2744 { 2745 dont_repeat (); /* Not for the faint of heart. */ 2746 2747 if (inferior_ptid == null_ptid) 2748 error (_("The program is not being run.")); 2749 2750 query_if_trace_running (from_tty); 2751 2752 disconnect_tracing (); 2753 2754 target_detach (current_inferior (), from_tty); 2755 2756 /* The current inferior process was just detached successfully. Get 2757 rid of breakpoints that no longer make sense. Note we don't do 2758 this within target_detach because that is also used when 2759 following child forks, and in that case we will want to transfer 2760 breakpoints to the child, not delete them. */ 2761 breakpoint_init_inferior (inf_exited); 2762 2763 /* If the solist is global across inferiors, don't clear it when we 2764 detach from a single inferior. */ 2765 if (!gdbarch_has_global_solist (target_gdbarch ())) 2766 no_shared_libraries (NULL, from_tty); 2767 2768 if (deprecated_detach_hook) 2769 deprecated_detach_hook (); 2770 } 2771 2772 /* Disconnect from the current target without resuming it (leaving it 2773 waiting for a debugger). 2774 2775 We'd better not have left any breakpoints in the program or the 2776 next debugger will get confused. Currently only supported for some 2777 remote targets, since the normal attach mechanisms don't work on 2778 stopped processes on some native platforms (e.g. GNU/Linux). */ 2779 2780 static void 2781 disconnect_command (const char *args, int from_tty) 2782 { 2783 dont_repeat (); /* Not for the faint of heart. */ 2784 query_if_trace_running (from_tty); 2785 disconnect_tracing (); 2786 target_disconnect (args, from_tty); 2787 no_shared_libraries (NULL, from_tty); 2788 init_thread_list (); 2789 if (deprecated_detach_hook) 2790 deprecated_detach_hook (); 2791 } 2792 2793 /* Stop PTID in the current target, and tag the PTID threads as having 2794 been explicitly requested to stop. PTID can be a thread, a 2795 process, or minus_one_ptid, meaning all threads of all inferiors of 2796 the current target. */ 2797 2798 static void 2799 stop_current_target_threads_ns (ptid_t ptid) 2800 { 2801 target_stop (ptid); 2802 2803 /* Tag the thread as having been explicitly requested to stop, so 2804 other parts of gdb know not to resume this thread automatically, 2805 if it was stopped due to an internal event. Limit this to 2806 non-stop mode, as when debugging a multi-threaded application in 2807 all-stop mode, we will only get one stop event --- it's undefined 2808 which thread will report the event. */ 2809 set_stop_requested (current_inferior ()->process_target (), 2810 ptid, 1); 2811 } 2812 2813 /* See inferior.h. */ 2814 2815 void 2816 interrupt_target_1 (bool all_threads) 2817 { 2818 if (non_stop) 2819 { 2820 if (all_threads) 2821 { 2822 scoped_restore_current_thread restore_thread; 2823 2824 for (inferior *inf : all_inferiors ()) 2825 { 2826 switch_to_inferior_no_thread (inf); 2827 stop_current_target_threads_ns (minus_one_ptid); 2828 } 2829 } 2830 else 2831 stop_current_target_threads_ns (inferior_ptid); 2832 } 2833 else 2834 target_interrupt (); 2835 } 2836 2837 /* interrupt [-a] 2838 Stop the execution of the target while running in async mode, in 2839 the background. In all-stop, stop the whole process. In non-stop 2840 mode, stop the current thread only by default, or stop all threads 2841 if the `-a' switch is used. */ 2842 2843 static void 2844 interrupt_command (const char *args, int from_tty) 2845 { 2846 if (target_can_async_p ()) 2847 { 2848 int all_threads = 0; 2849 2850 dont_repeat (); /* Not for the faint of heart. */ 2851 2852 if (args != NULL 2853 && startswith (args, "-a")) 2854 all_threads = 1; 2855 2856 if (!non_stop && all_threads) 2857 error (_("-a is meaningless in all-stop mode.")); 2858 2859 interrupt_target_1 (all_threads); 2860 } 2861 } 2862 2863 /* See inferior.h. */ 2864 2865 void 2866 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file, 2867 struct frame_info *frame, const char *args) 2868 { 2869 int regnum; 2870 int printed_something = 0; 2871 2872 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) 2873 { 2874 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup)) 2875 { 2876 printed_something = 1; 2877 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); 2878 } 2879 } 2880 if (!printed_something) 2881 fprintf_filtered (file, "No floating-point info " 2882 "available for this processor.\n"); 2883 } 2884 2885 static void 2886 info_float_command (const char *args, int from_tty) 2887 { 2888 struct frame_info *frame; 2889 2890 if (!target_has_registers) 2891 error (_("The program has no registers now.")); 2892 2893 frame = get_selected_frame (NULL); 2894 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args); 2895 } 2896 2897 /* Implement `info proc' family of commands. */ 2898 2899 static void 2900 info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty) 2901 { 2902 struct gdbarch *gdbarch = get_current_arch (); 2903 2904 if (!target_info_proc (args, what)) 2905 { 2906 if (gdbarch_info_proc_p (gdbarch)) 2907 gdbarch_info_proc (gdbarch, args, what); 2908 else 2909 error (_("Not supported on this target.")); 2910 } 2911 } 2912 2913 /* Implement `info proc' when given without any further parameters. */ 2914 2915 static void 2916 info_proc_cmd (const char *args, int from_tty) 2917 { 2918 info_proc_cmd_1 (args, IP_MINIMAL, from_tty); 2919 } 2920 2921 /* Implement `info proc mappings'. */ 2922 2923 static void 2924 info_proc_cmd_mappings (const char *args, int from_tty) 2925 { 2926 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty); 2927 } 2928 2929 /* Implement `info proc stat'. */ 2930 2931 static void 2932 info_proc_cmd_stat (const char *args, int from_tty) 2933 { 2934 info_proc_cmd_1 (args, IP_STAT, from_tty); 2935 } 2936 2937 /* Implement `info proc status'. */ 2938 2939 static void 2940 info_proc_cmd_status (const char *args, int from_tty) 2941 { 2942 info_proc_cmd_1 (args, IP_STATUS, from_tty); 2943 } 2944 2945 /* Implement `info proc cwd'. */ 2946 2947 static void 2948 info_proc_cmd_cwd (const char *args, int from_tty) 2949 { 2950 info_proc_cmd_1 (args, IP_CWD, from_tty); 2951 } 2952 2953 /* Implement `info proc cmdline'. */ 2954 2955 static void 2956 info_proc_cmd_cmdline (const char *args, int from_tty) 2957 { 2958 info_proc_cmd_1 (args, IP_CMDLINE, from_tty); 2959 } 2960 2961 /* Implement `info proc exe'. */ 2962 2963 static void 2964 info_proc_cmd_exe (const char *args, int from_tty) 2965 { 2966 info_proc_cmd_1 (args, IP_EXE, from_tty); 2967 } 2968 2969 /* Implement `info proc files'. */ 2970 2971 static void 2972 info_proc_cmd_files (const char *args, int from_tty) 2973 { 2974 info_proc_cmd_1 (args, IP_FILES, from_tty); 2975 } 2976 2977 /* Implement `info proc all'. */ 2978 2979 static void 2980 info_proc_cmd_all (const char *args, int from_tty) 2981 { 2982 info_proc_cmd_1 (args, IP_ALL, from_tty); 2983 } 2984 2985 /* Implement `show print finish'. */ 2986 2987 static void 2988 show_print_finish (struct ui_file *file, int from_tty, 2989 struct cmd_list_element *c, 2990 const char *value) 2991 { 2992 fprintf_filtered (file, _("\ 2993 Printing of return value after `finish' is %s.\n"), 2994 value); 2995 } 2996 2997 2998 /* This help string is used for the run, start, and starti commands. 2999 It is defined as a macro to prevent duplication. */ 3000 3001 #define RUN_ARGS_HELP \ 3002 "You may specify arguments to give it.\n\ 3003 Args may include \"*\", or \"[...]\"; they are expanded using the\n\ 3004 shell that will start the program (specified by the \"$SHELL\" environment\n\ 3005 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\ 3006 are also allowed.\n\ 3007 \n\ 3008 With no arguments, uses arguments last specified (with \"run\" or \n\ 3009 \"set args\"). To cancel previous arguments and run with no arguments,\n\ 3010 use \"set args\" without arguments.\n\ 3011 \n\ 3012 To start the inferior without using a shell, use \"set startup-with-shell off\"." 3013 3014 void _initialize_infcmd (); 3015 void 3016 _initialize_infcmd () 3017 { 3018 static struct cmd_list_element *info_proc_cmdlist; 3019 struct cmd_list_element *c = NULL; 3020 const char *cmd_name; 3021 3022 /* Add the filename of the terminal connected to inferior I/O. */ 3023 add_setshow_optional_filename_cmd ("inferior-tty", class_run, 3024 &inferior_io_terminal_scratch, _("\ 3025 Set terminal for future runs of program being debugged."), _("\ 3026 Show terminal for future runs of program being debugged."), _("\ 3027 Usage: set inferior-tty [TTY]\n\n\ 3028 If TTY is omitted, the default behavior of using the same terminal as GDB\n\ 3029 is restored."), 3030 set_inferior_tty_command, 3031 show_inferior_tty_command, 3032 &setlist, &showlist); 3033 cmd_name = "inferior-tty"; 3034 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); 3035 gdb_assert (c != NULL); 3036 add_alias_cmd ("tty", c, class_run, 0, &cmdlist); 3037 3038 cmd_name = "args"; 3039 add_setshow_string_noescape_cmd (cmd_name, class_run, 3040 &inferior_args_scratch, _("\ 3041 Set argument list to give program being debugged when it is started."), _("\ 3042 Show argument list to give program being debugged when it is started."), _("\ 3043 Follow this command with any number of args, to be passed to the program."), 3044 set_args_command, 3045 show_args_command, 3046 &setlist, &showlist); 3047 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); 3048 gdb_assert (c != NULL); 3049 set_cmd_completer (c, filename_completer); 3050 3051 cmd_name = "cwd"; 3052 add_setshow_string_noescape_cmd (cmd_name, class_run, 3053 &inferior_cwd_scratch, _("\ 3054 Set the current working directory to be used when the inferior is started.\n\ 3055 Changing this setting does not have any effect on inferiors that are\n\ 3056 already running."), 3057 _("\ 3058 Show the current working directory that is used when the inferior is started."), 3059 _("\ 3060 Use this command to change the current working directory that will be used\n\ 3061 when the inferior is started. This setting does not affect GDB's current\n\ 3062 working directory."), 3063 set_cwd_command, 3064 show_cwd_command, 3065 &setlist, &showlist); 3066 c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); 3067 gdb_assert (c != NULL); 3068 set_cmd_completer (c, filename_completer); 3069 3070 c = add_cmd ("environment", no_class, environment_info, _("\ 3071 The environment to give the program, or one variable's value.\n\ 3072 With an argument VAR, prints the value of environment variable VAR to\n\ 3073 give the program being debugged. With no arguments, prints the entire\n\ 3074 environment to be given to the program."), &showlist); 3075 set_cmd_completer (c, noop_completer); 3076 3077 add_basic_prefix_cmd ("unset", no_class, 3078 _("Complement to certain \"set\" commands."), 3079 &unsetlist, "unset ", 0, &cmdlist); 3080 3081 c = add_cmd ("environment", class_run, unset_environment_command, _("\ 3082 Cancel environment variable VAR for the program.\n\ 3083 This does not affect the program until the next \"run\" command."), 3084 &unsetlist); 3085 set_cmd_completer (c, noop_completer); 3086 3087 c = add_cmd ("environment", class_run, set_environment_command, _("\ 3088 Set environment variable value to give the program.\n\ 3089 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\ 3090 VALUES of environment variables are uninterpreted strings.\n\ 3091 This does not affect the program until the next \"run\" command."), 3092 &setlist); 3093 set_cmd_completer (c, noop_completer); 3094 3095 c = add_com ("path", class_files, path_command, _("\ 3096 Add directory DIR(s) to beginning of search path for object files.\n\ 3097 $cwd in the path means the current working directory.\n\ 3098 This path is equivalent to the $PATH shell variable. It is a list of\n\ 3099 directories, separated by colons. These directories are searched to find\n\ 3100 fully linked executable files and separately compiled object files as \ 3101 needed.")); 3102 set_cmd_completer (c, filename_completer); 3103 3104 c = add_cmd ("paths", no_class, path_info, _("\ 3105 Current search path for finding object files.\n\ 3106 $cwd in the path means the current working directory.\n\ 3107 This path is equivalent to the $PATH shell variable. It is a list of\n\ 3108 directories, separated by colons. These directories are searched to find\n\ 3109 fully linked executable files and separately compiled object files as \ 3110 needed."), 3111 &showlist); 3112 set_cmd_completer (c, noop_completer); 3113 3114 add_prefix_cmd ("kill", class_run, kill_command, 3115 _("Kill execution of program being debugged."), 3116 &killlist, "kill ", 0, &cmdlist); 3117 3118 add_com ("attach", class_run, attach_command, _("\ 3119 Attach to a process or file outside of GDB.\n\ 3120 This command attaches to another target, of the same type as your last\n\ 3121 \"target\" command (\"info files\" will show your target stack).\n\ 3122 The command may take as argument a process id or a device file.\n\ 3123 For a process id, you must have permission to send the process a signal,\n\ 3124 and it must have the same effective uid as the debugger.\n\ 3125 When using \"attach\" with a process id, the debugger finds the\n\ 3126 program running in the process, looking first in the current working\n\ 3127 directory, or (if not found there) using the source file search path\n\ 3128 (see the \"directory\" command). You can also use the \"file\" command\n\ 3129 to specify the program, and to load its symbol table.")); 3130 3131 add_prefix_cmd ("detach", class_run, detach_command, _("\ 3132 Detach a process or file previously attached.\n\ 3133 If a process, it is no longer traced, and it continues its execution. If\n\ 3134 you were debugging a file, the file is closed and gdb no longer accesses it."), 3135 &detachlist, "detach ", 0, &cmdlist); 3136 3137 add_com ("disconnect", class_run, disconnect_command, _("\ 3138 Disconnect from a target.\n\ 3139 The target will wait for another debugger to connect. Not available for\n\ 3140 all targets.")); 3141 3142 c = add_com ("signal", class_run, signal_command, _("\ 3143 Continue program with the specified signal.\n\ 3144 Usage: signal SIGNAL\n\ 3145 The SIGNAL argument is processed the same as the handle command.\n\ 3146 \n\ 3147 An argument of \"0\" means continue the program without sending it a signal.\n\ 3148 This is useful in cases where the program stopped because of a signal,\n\ 3149 and you want to resume the program while discarding the signal.\n\ 3150 \n\ 3151 In a multi-threaded program the signal is delivered to, or discarded from,\n\ 3152 the current thread only.")); 3153 set_cmd_completer (c, signal_completer); 3154 3155 c = add_com ("queue-signal", class_run, queue_signal_command, _("\ 3156 Queue a signal to be delivered to the current thread when it is resumed.\n\ 3157 Usage: queue-signal SIGNAL\n\ 3158 The SIGNAL argument is processed the same as the handle command.\n\ 3159 It is an error if the handling state of SIGNAL is \"nopass\".\n\ 3160 \n\ 3161 An argument of \"0\" means remove any currently queued signal from\n\ 3162 the current thread. This is useful in cases where the program stopped\n\ 3163 because of a signal, and you want to resume it while discarding the signal.\n\ 3164 \n\ 3165 In a multi-threaded program the signal is queued with, or discarded from,\n\ 3166 the current thread only.")); 3167 set_cmd_completer (c, signal_completer); 3168 3169 add_com ("stepi", class_run, stepi_command, _("\ 3170 Step one instruction exactly.\n\ 3171 Usage: stepi [N]\n\ 3172 Argument N means step N times (or till program stops for another \ 3173 reason).")); 3174 add_com_alias ("si", "stepi", class_run, 0); 3175 3176 add_com ("nexti", class_run, nexti_command, _("\ 3177 Step one instruction, but proceed through subroutine calls.\n\ 3178 Usage: nexti [N]\n\ 3179 Argument N means step N times (or till program stops for another \ 3180 reason).")); 3181 add_com_alias ("ni", "nexti", class_run, 0); 3182 3183 add_com ("finish", class_run, finish_command, _("\ 3184 Execute until selected stack frame returns.\n\ 3185 Usage: finish\n\ 3186 Upon return, the value returned is printed and put in the value history.")); 3187 add_com_alias ("fin", "finish", class_run, 1); 3188 3189 add_com ("next", class_run, next_command, _("\ 3190 Step program, proceeding through subroutine calls.\n\ 3191 Usage: next [N]\n\ 3192 Unlike \"step\", if the current source line calls a subroutine,\n\ 3193 this command does not enter the subroutine, but instead steps over\n\ 3194 the call, in effect treating it as a single source line.")); 3195 add_com_alias ("n", "next", class_run, 1); 3196 3197 add_com ("step", class_run, step_command, _("\ 3198 Step program until it reaches a different source line.\n\ 3199 Usage: step [N]\n\ 3200 Argument N means step N times (or till program stops for another \ 3201 reason).")); 3202 add_com_alias ("s", "step", class_run, 1); 3203 3204 c = add_com ("until", class_run, until_command, _("\ 3205 Execute until past the current line or past a LOCATION.\n\ 3206 Execute until the program reaches a source line greater than the current\n\ 3207 or a specified location (same args as break command) within the current \ 3208 frame.")); 3209 set_cmd_completer (c, location_completer); 3210 add_com_alias ("u", "until", class_run, 1); 3211 3212 c = add_com ("advance", class_run, advance_command, _("\ 3213 Continue the program up to the given location (same form as args for break \ 3214 command).\n\ 3215 Execution will also stop upon exit from the current stack frame.")); 3216 set_cmd_completer (c, location_completer); 3217 3218 c = add_com ("jump", class_run, jump_command, _("\ 3219 Continue program being debugged at specified line or address.\n\ 3220 Usage: jump LOCATION\n\ 3221 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\ 3222 for an address to start at.")); 3223 set_cmd_completer (c, location_completer); 3224 add_com_alias ("j", "jump", class_run, 1); 3225 3226 add_com ("continue", class_run, continue_command, _("\ 3227 Continue program being debugged, after signal or breakpoint.\n\ 3228 Usage: continue [N]\n\ 3229 If proceeding from breakpoint, a number N may be used as an argument,\n\ 3230 which means to set the ignore count of that breakpoint to N - 1 (so that\n\ 3231 the breakpoint won't break until the Nth time it is reached).\n\ 3232 \n\ 3233 If non-stop mode is enabled, continue only the current thread,\n\ 3234 otherwise all the threads in the program are continued. To \n\ 3235 continue all stopped threads in non-stop mode, use the -a option.\n\ 3236 Specifying -a and an ignore count simultaneously is an error.")); 3237 add_com_alias ("c", "cont", class_run, 1); 3238 add_com_alias ("fg", "cont", class_run, 1); 3239 3240 c = add_com ("run", class_run, run_command, _("\ 3241 Start debugged program.\n" 3242 RUN_ARGS_HELP)); 3243 set_cmd_completer (c, filename_completer); 3244 add_com_alias ("r", "run", class_run, 1); 3245 3246 c = add_com ("start", class_run, start_command, _("\ 3247 Start the debugged program stopping at the beginning of the main procedure.\n" 3248 RUN_ARGS_HELP)); 3249 set_cmd_completer (c, filename_completer); 3250 3251 c = add_com ("starti", class_run, starti_command, _("\ 3252 Start the debugged program stopping at the first instruction.\n" 3253 RUN_ARGS_HELP)); 3254 set_cmd_completer (c, filename_completer); 3255 3256 add_com ("interrupt", class_run, interrupt_command, 3257 _("Interrupt the execution of the debugged program.\n\ 3258 If non-stop mode is enabled, interrupt only the current thread,\n\ 3259 otherwise all the threads in the program are stopped. To \n\ 3260 interrupt all running threads in non-stop mode, use the -a option.")); 3261 3262 c = add_info ("registers", info_registers_command, _("\ 3263 List of integer registers and their contents, for selected stack frame.\n\ 3264 One or more register names as argument means describe the given registers.\n\ 3265 One or more register group names as argument means describe the registers\n\ 3266 in the named register groups.")); 3267 add_info_alias ("r", "registers", 1); 3268 set_cmd_completer (c, reg_or_group_completer); 3269 3270 c = add_info ("all-registers", info_all_registers_command, _("\ 3271 List of all registers and their contents, for selected stack frame.\n\ 3272 One or more register names as argument means describe the given registers.\n\ 3273 One or more register group names as argument means describe the registers\n\ 3274 in the named register groups.")); 3275 set_cmd_completer (c, reg_or_group_completer); 3276 3277 add_info ("program", info_program_command, 3278 _("Execution status of the program.")); 3279 3280 add_info ("float", info_float_command, 3281 _("Print the status of the floating point unit.")); 3282 3283 add_info ("vector", info_vector_command, 3284 _("Print the status of the vector unit.")); 3285 3286 add_prefix_cmd ("proc", class_info, info_proc_cmd, 3287 _("\ 3288 Show additional information about a process.\n\ 3289 Specify any process id, or use the program being debugged by default."), 3290 &info_proc_cmdlist, "info proc ", 3291 1/*allow-unknown*/, &infolist); 3292 3293 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\ 3294 List memory regions mapped by the specified process."), 3295 &info_proc_cmdlist); 3296 3297 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\ 3298 List process info from /proc/PID/stat."), 3299 &info_proc_cmdlist); 3300 3301 add_cmd ("status", class_info, info_proc_cmd_status, _("\ 3302 List process info from /proc/PID/status."), 3303 &info_proc_cmdlist); 3304 3305 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\ 3306 List current working directory of the specified process."), 3307 &info_proc_cmdlist); 3308 3309 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\ 3310 List command line arguments of the specified process."), 3311 &info_proc_cmdlist); 3312 3313 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\ 3314 List absolute filename for executable of the specified process."), 3315 &info_proc_cmdlist); 3316 3317 add_cmd ("files", class_info, info_proc_cmd_files, _("\ 3318 List files opened by the specified process."), 3319 &info_proc_cmdlist); 3320 3321 add_cmd ("all", class_info, info_proc_cmd_all, _("\ 3322 List all available info about the specified process."), 3323 &info_proc_cmdlist); 3324 3325 add_setshow_boolean_cmd ("finish", class_support, 3326 &user_print_options.finish_print, _("\ 3327 Set whether `finish' prints the return value."), _("\ 3328 Show whether `finish' prints the return value."), NULL, 3329 NULL, 3330 show_print_finish, 3331 &setprintlist, &showprintlist); 3332 } 3333