1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
36 #include "completer.h"
38 #include "event-top.h"
39 #include "parser-defs.h"
41 #include "reggroups.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "cli/cli-decode.h"
49 #include "gdbthread.h"
51 #include "inline-frame.h"
52 #include "tracepoint.h"
54 #include "continuations.h"
56 #include "cli/cli-utils.h"
58 #include "thread-fsm.h"
61 #include "common/gdb_optional.h"
63 /* Local functions: */
65 static void info_registers_command (char *, int);
67 static void until_next_command (int);
69 static void info_float_command (char *, int);
71 static void info_program_command (char *, int);
73 static void step_1 (int, int, const char *);
75 #define ERROR_NO_INFERIOR \
76 if (!target_has_execution) error (_("The program is not being run."));
78 /* Scratch area where string containing arguments to give to the
79 program will be stored by 'set args'. As soon as anything is
80 stored, notice_args_set will move it into per-inferior storage.
81 Arguments are separated by spaces. Empty string (pointer to '\0')
84 static char *inferior_args_scratch;
86 /* Scratch area where the new cwd will be stored by 'set cwd'. */
88 static char *inferior_cwd_scratch;
90 /* Scratch area where 'set inferior-tty' will store user-provided value.
91 We'll immediate copy it into per-inferior storage. */
93 static char *inferior_io_terminal_scratch;
95 /* Pid of our debugged inferior, or 0 if no inferior now.
96 Since various parts of infrun.c test this to see whether there is a program
97 being debugged it should be nonzero (currently 3 is used) for remote
100 ptid_t inferior_ptid;
102 /* Address at which inferior stopped. */
106 /* Nonzero if stopped due to completion of a stack dummy routine. */
108 enum stop_stack_kind stop_stack_dummy;
110 /* Nonzero if stopped due to a random (unexpected) signal in inferior
113 int stopped_by_random_signal;
115 /* See inferior.h. */
117 int startup_with_shell = 1;
120 /* Accessor routines. */
122 /* Set the io terminal for the current inferior. Ownership of
123 TERMINAL_NAME is not transferred. */
126 set_inferior_io_terminal (const char *terminal_name)
128 xfree (current_inferior ()->terminal);
130 if (terminal_name != NULL && *terminal_name != '\0')
131 current_inferior ()->terminal = xstrdup (terminal_name);
133 current_inferior ()->terminal = NULL;
137 get_inferior_io_terminal (void)
139 return current_inferior ()->terminal;
143 set_inferior_tty_command (char *args, int from_tty,
144 struct cmd_list_element *c)
146 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
147 Now route it to current inferior. */
148 set_inferior_io_terminal (inferior_io_terminal_scratch);
152 show_inferior_tty_command (struct ui_file *file, int from_tty,
153 struct cmd_list_element *c, const char *value)
155 /* Note that we ignore the passed-in value in favor of computing it
157 const char *inferior_io_terminal = get_inferior_io_terminal ();
159 if (inferior_io_terminal == NULL)
160 inferior_io_terminal = "";
161 fprintf_filtered (gdb_stdout,
162 _("Terminal for future runs of program being debugged "
163 "is \"%s\".\n"), inferior_io_terminal);
167 get_inferior_args (void)
169 if (current_inferior ()->argc != 0)
173 n = construct_inferior_arguments (current_inferior ()->argc,
174 current_inferior ()->argv);
175 set_inferior_args (n);
179 if (current_inferior ()->args == NULL)
180 current_inferior ()->args = xstrdup ("");
182 return current_inferior ()->args;
185 /* Set the arguments for the current inferior. Ownership of
186 NEWARGS is not transferred. */
189 set_inferior_args (const char *newargs)
191 xfree (current_inferior ()->args);
192 current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
193 current_inferior ()->argc = 0;
194 current_inferior ()->argv = 0;
198 set_inferior_args_vector (int argc, char **argv)
200 current_inferior ()->argc = argc;
201 current_inferior ()->argv = argv;
204 /* Notice when `set args' is run. */
207 set_args_command (char *args, int from_tty, struct cmd_list_element *c)
209 /* CLI has assigned the user-provided value to inferior_args_scratch.
210 Now route it to current inferior. */
211 set_inferior_args (inferior_args_scratch);
214 /* Notice when `show args' is run. */
217 show_args_command (struct ui_file *file, int from_tty,
218 struct cmd_list_element *c, const char *value)
220 /* Note that we ignore the passed-in value in favor of computing it
222 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
225 /* See common/common-inferior.h. */
228 set_inferior_cwd (const char *cwd)
230 struct inferior *inf = current_inferior ();
232 gdb_assert (inf != NULL);
237 inf->cwd.reset (xstrdup (cwd));
240 /* See common/common-inferior.h. */
245 return current_inferior ()->cwd.get ();
248 /* Handle the 'set cwd' command. */
251 set_cwd_command (char *args, int from_tty, struct cmd_list_element *c)
253 if (*inferior_cwd_scratch == '\0')
254 set_inferior_cwd (NULL);
256 set_inferior_cwd (inferior_cwd_scratch);
259 /* Handle the 'show cwd' command. */
262 show_cwd_command (struct ui_file *file, int from_tty,
263 struct cmd_list_element *c, const char *value)
265 const char *cwd = get_inferior_cwd ();
268 fprintf_filtered (gdb_stdout,
270 You have not set the inferior's current working directory.\n\
271 The inferior will inherit GDB's cwd if native debugging, or the remote\n\
272 server's cwd if remote debugging.\n"));
274 fprintf_filtered (gdb_stdout,
275 _("Current working directory that will be used "
276 "when starting the inferior is \"%s\".\n"), cwd);
280 /* Compute command-line string given argument vector. This does the
281 same shell processing as fork_inferior. */
284 construct_inferior_arguments (int argc, char **argv)
288 if (startup_with_shell)
291 /* This holds all the characters considered special to the
293 static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
294 static const char quote = '"';
296 /* This holds all the characters considered special to the
297 typical Unix shells. We include `^' because the SunOS
298 /bin/sh treats it as a synonym for `|'. */
299 static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
300 static const char quote = '\'';
306 /* We over-compute the size. It shouldn't matter. */
307 for (i = 0; i < argc; ++i)
308 length += 3 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
310 result = (char *) xmalloc (length);
313 for (i = 0; i < argc; ++i)
318 /* Need to handle empty arguments specially. */
319 if (argv[i][0] == '\0')
329 if (strpbrk (argv[i], special))
335 for (cp = argv[i]; *cp; ++cp)
339 /* A newline cannot be quoted with a backslash (it
340 just disappears), only by putting it inside
351 if (strchr (special, *cp) != NULL)
367 /* In this case we can't handle arguments that contain spaces,
368 tabs, or newlines -- see breakup_args(). */
372 for (i = 0; i < argc; ++i)
374 char *cp = strchr (argv[i], ' ');
376 cp = strchr (argv[i], '\t');
378 cp = strchr (argv[i], '\n');
380 error (_("can't handle command-line "
381 "argument containing whitespace"));
382 length += strlen (argv[i]) + 1;
385 result = (char *) xmalloc (length);
387 for (i = 0; i < argc; ++i)
390 strcat (result, " ");
391 strcat (result, argv[i]);
399 /* This function strips the '&' character (indicating background
400 execution) that is added as *the last* of the arguments ARGS of a
401 command. A copy of the incoming ARGS without the '&' is returned,
402 unless the resulting string after stripping is empty, in which case
403 NULL is returned. *BG_CHAR_P is an output boolean that indicates
404 whether the '&' character was found. */
406 static gdb::unique_xmalloc_ptr<char>
407 strip_bg_char (const char *args, int *bg_char_p)
411 if (args == NULL || *args == '\0')
417 p = args + strlen (args);
421 while (p > args && isspace (p[-1]))
426 return gdb::unique_xmalloc_ptr<char>
427 (savestring (args, p - args));
429 return gdb::unique_xmalloc_ptr<char> (nullptr);
433 return gdb::unique_xmalloc_ptr<char> (xstrdup (args));
436 /* Common actions to take after creating any sort of inferior, by any
437 means (running, attaching, connecting, et cetera). The target
438 should be stopped. */
441 post_create_inferior (struct target_ops *target, int from_tty)
444 /* Be sure we own the terminal in case write operations are performed. */
445 target_terminal::ours_for_output ();
447 /* If the target hasn't taken care of this already, do it now.
448 Targets which need to access registers during to_open,
449 to_create_inferior, or to_attach should do it earlier; but many
451 target_find_description ();
453 /* Now that we know the register layout, retrieve current PC. But
454 if the PC is unavailable (e.g., we're opening a core file with
455 missing registers info), ignore it. */
459 stop_pc = regcache_read_pc (get_current_regcache ());
461 CATCH (ex, RETURN_MASK_ERROR)
463 if (ex.error != NOT_AVAILABLE_ERROR)
464 throw_exception (ex);
470 const unsigned solib_add_generation
471 = current_program_space->solib_add_generation;
473 /* Create the hooks to handle shared library load and unload
475 solib_create_inferior_hook (from_tty);
477 if (current_program_space->solib_add_generation == solib_add_generation)
479 /* The platform-specific hook should load initial shared libraries,
480 but didn't. FROM_TTY will be incorrectly 0 but such solib
481 targets should be fixed anyway. Call it only after the solib
482 target has been initialized by solib_create_inferior_hook. */
485 warning (_("platform-specific solib_create_inferior_hook did "
486 "not load initial shared libraries."));
488 /* If the solist is global across processes, there's no need to
490 if (!gdbarch_has_global_solist (target_gdbarch ()))
491 solib_add (NULL, 0, auto_solib_add);
495 /* If the user sets watchpoints before execution having started,
496 then she gets software watchpoints, because GDB can't know which
497 target will end up being pushed, or if it supports hardware
498 watchpoints or not. breakpoint_re_set takes care of promoting
499 watchpoints to hardware watchpoints if possible, however, if this
500 new inferior doesn't load shared libraries or we don't pull in
501 symbols from any other source on this target/arch,
502 breakpoint_re_set is never called. Call it now so that software
503 watchpoints get a chance to be promoted to hardware watchpoints
504 if the now pushed target supports hardware watchpoints. */
505 breakpoint_re_set ();
507 observer_notify_inferior_created (target, from_tty);
510 /* Kill the inferior if already running. This function is designed
511 to be called when we are about to start the execution of the program
512 from the beginning. Ask the user to confirm that he wants to restart
513 the program being debugged when FROM_TTY is non-null. */
516 kill_if_already_running (int from_tty)
518 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
520 /* Bail out before killing the program if we will not be able to
522 target_require_runnable ();
525 && !query (_("The program being debugged has been started already.\n\
526 Start it from the beginning? ")))
527 error (_("Program not restarted."));
532 /* See inferior.h. */
535 prepare_execution_command (struct target_ops *target, int background)
537 /* If we get a request for running in the bg but the target
538 doesn't support it, error out. */
539 if (background && !target->to_can_async_p (target))
540 error (_("Asynchronous execution not supported on this target."));
544 /* If we get a request for running in the fg, then we need to
545 simulate synchronous (fg) execution. Note no cleanup is
546 necessary for this. stdin is re-enabled whenever an error
547 reaches the top level. */
548 all_uis_on_sync_execution_starting ();
552 /* Determine how the new inferior will behave. */
556 /* Run program without any explicit stop during startup. */
559 /* Stop at the beginning of the program's main function. */
562 /* Stop at the first instruction of the program. */
563 RUN_STOP_AT_FIRST_INSN
566 /* Implement the "run" command. Force a stop during program start if
567 requested by RUN_HOW. */
570 run_command_1 (const char *args, int from_tty, enum run_how run_how)
572 const char *exec_file;
573 struct cleanup *old_chain;
575 struct ui_out *uiout = current_uiout;
576 struct target_ops *run_target;
582 kill_if_already_running (from_tty);
584 init_wait_for_inferior ();
585 clear_breakpoint_hit_counts ();
587 /* Clean up any leftovers from other runs. Some other things from
588 this function should probably be moved into target_pre_inferior. */
589 target_pre_inferior (from_tty);
591 /* The comment here used to read, "The exec file is re-read every
592 time we do a generic_mourn_inferior, so we just have to worry
593 about the symbol file." The `generic_mourn_inferior' function
594 gets called whenever the program exits. However, suppose the
595 program exits, and *then* the executable file changes? We need
596 to check again here. Since reopen_exec_file doesn't do anything
597 if the timestamp hasn't changed, I don't see the harm. */
601 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
602 args = stripped.get ();
604 /* Do validation and preparation before possibly changing anything
607 run_target = find_run_target ();
609 prepare_execution_command (run_target, async_exec);
611 if (non_stop && !run_target->to_supports_non_stop (run_target))
612 error (_("The target does not support running in non-stop mode."));
614 /* Done. Can now set breakpoints, change inferior args, etc. */
616 /* Insert temporary breakpoint in main function if requested. */
617 if (run_how == RUN_STOP_AT_MAIN)
618 tbreak_command (main_name (), 0);
620 exec_file = get_exec_file (0);
622 /* We keep symbols from add-symbol-file, on the grounds that the
623 user might want to add some symbols before running the program
624 (right?). But sometimes (dynamic loading where the user manually
625 introduces the new symbols with add-symbol-file), the code which
626 the symbols describe does not persist between runs. Currently
627 the user has to manually nuke all symbols between runs if they
628 want them to go away (PR 2207). This is probably reasonable. */
630 /* If there were other args, beside '&', process them. */
632 set_inferior_args (args);
636 uiout->field_string (NULL, "Starting program");
639 uiout->field_string ("execfile", exec_file);
641 /* We call get_inferior_args() because we might need to compute
643 uiout->field_string ("infargs", get_inferior_args ());
648 /* We call get_inferior_args() because we might need to compute
650 run_target->to_create_inferior (run_target, exec_file,
651 std::string (get_inferior_args ()),
652 current_inferior ()->environment.envp (),
654 /* to_create_inferior should push the target, so after this point we
655 shouldn't refer to run_target again. */
658 /* We're starting off a new process. When we get out of here, in
659 non-stop mode, finish the state of all threads of that process,
660 but leave other threads alone, as they may be stopped in internal
661 events --- the frontend shouldn't see them as stopped. In
662 all-stop, always finish the state of all threads, as we may be
663 resuming more than just the new process. */
665 ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
667 ptid = minus_one_ptid;
668 old_chain = make_cleanup (finish_thread_state_cleanup, &ptid);
670 /* Pass zero for FROM_TTY, because at this point the "run" command
671 has done its thing; now we are setting up the running program. */
672 post_create_inferior (¤t_target, 0);
674 /* Queue a pending event so that the program stops immediately. */
675 if (run_how == RUN_STOP_AT_FIRST_INSN)
677 thread_info *thr = inferior_thread ();
678 thr->suspend.waitstatus_pending_p = 1;
679 thr->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
680 thr->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
683 /* Start the target running. Do not use -1 continuation as it would skip
684 breakpoint right at the entry point. */
685 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0);
687 /* Since there was no error, there's no need to finish the thread
689 discard_cleanups (old_chain);
693 run_command (const char *args, int from_tty)
695 run_command_1 (args, from_tty, RUN_NORMAL);
698 /* Start the execution of the program up until the beginning of the main
702 start_command (const char *args, int from_tty)
704 /* Some languages such as Ada need to search inside the program
705 minimal symbols for the location where to put the temporary
706 breakpoint before starting. */
707 if (!have_minimal_symbols ())
708 error (_("No symbol table loaded. Use the \"file\" command."));
710 /* Run the program until reaching the main procedure... */
711 run_command_1 (args, from_tty, RUN_STOP_AT_MAIN);
714 /* Start the execution of the program stopping at the first
718 starti_command (const char *args, int from_tty)
720 run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN);
724 proceed_thread_callback (struct thread_info *thread, void *arg)
726 /* We go through all threads individually instead of compressing
727 into a single target `resume_all' request, because some threads
728 may be stopped in internal breakpoints/events, or stopped waiting
729 for its turn in the displaced stepping queue (that is, they are
730 running && !executing). The target side has no idea about why
731 the thread is stopped, so a `resume_all' command would resume too
732 much. If/when GDB gains a way to tell the target `hold this
733 thread stopped until I say otherwise', then we can optimize
735 if (!is_stopped (thread->ptid))
738 switch_to_thread (thread->ptid);
739 clear_proceed_status (0);
740 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
745 ensure_valid_thread (void)
747 if (ptid_equal (inferior_ptid, null_ptid)
748 || is_exited (inferior_ptid))
749 error (_("Cannot execute this command without a live selected thread."));
752 /* If the user is looking at trace frames, any resumption of execution
753 is likely to mix up recorded and live target data. So simply
754 disallow those commands. */
757 ensure_not_tfind_mode (void)
759 if (get_traceframe_number () >= 0)
760 error (_("Cannot execute this command while looking at trace frames."));
763 /* Throw an error indicating the current thread is running. */
766 error_is_running (void)
768 error (_("Cannot execute this command while "
769 "the selected thread is running."));
772 /* Calls error_is_running if the current thread is running. */
775 ensure_not_running (void)
777 if (is_running (inferior_ptid))
782 continue_1 (int all_threads)
785 ensure_not_tfind_mode ();
787 if (non_stop && all_threads)
789 /* Don't error out if the current thread is running, because
790 there may be other stopped threads. */
792 /* Backup current thread and selected frame and restore on scope
794 scoped_restore_current_thread restore_thread;
796 iterate_over_threads (proceed_thread_callback, NULL);
798 if (current_ui->prompt_state == PROMPT_BLOCKED)
800 /* If all threads in the target were already running,
801 proceed_thread_callback ends up never calling proceed,
802 and so nothing calls this to put the inferior's terminal
803 settings in effect and remove stdin from the event loop,
804 which we must when running a foreground command. E.g.:
808 <all threads are running now>
811 <no thread was resumed, but the inferior now owns the terminal>
813 target_terminal::inferior ();
818 ensure_valid_thread ();
819 ensure_not_running ();
820 clear_proceed_status (0);
821 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
825 /* continue [-a] [proceed-count] [&] */
828 continue_command (const char *args, int from_tty)
835 /* Find out whether we must run in the background. */
836 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
837 args = stripped.get ();
841 if (startswith (args, "-a"))
844 args += sizeof ("-a") - 1;
850 if (!non_stop && all_threads)
851 error (_("`-a' is meaningless in all-stop mode."));
853 if (args != NULL && all_threads)
854 error (_("Can't resume all threads and specify "
855 "proceed count simultaneously."));
857 /* If we have an argument left, set proceed count of breakpoint we
864 struct thread_info *tp;
867 tp = find_thread_ptid (inferior_ptid);
871 struct target_waitstatus ws;
873 get_last_target_status (&last_ptid, &ws);
874 tp = find_thread_ptid (last_ptid);
877 bs = tp->control.stop_bpstat;
879 while ((stat = bpstat_num (&bs, &num)) != 0)
882 set_ignore_count (num,
883 parse_and_eval_long (args) - 1,
885 /* set_ignore_count prints a message ending with a period.
886 So print two spaces before "Continuing.". */
888 printf_filtered (" ");
892 if (!stopped && from_tty)
895 ("Not stopped at any breakpoint; argument ignored.\n");
900 ensure_not_tfind_mode ();
902 if (!non_stop || !all_threads)
904 ensure_valid_thread ();
905 ensure_not_running ();
908 prepare_execution_command (¤t_target, async_exec);
911 printf_filtered (_("Continuing.\n"));
913 continue_1 (all_threads);
916 /* Record the starting point of a "step" or "next" command. */
919 set_step_frame (void)
921 frame_info *frame = get_current_frame ();
923 symtab_and_line sal = find_frame_sal (frame);
924 set_step_info (frame, sal);
926 CORE_ADDR pc = get_frame_pc (frame);
927 thread_info *tp = inferior_thread ();
928 tp->control.step_start_function = find_pc_function (pc);
931 /* Step until outside of current statement. */
934 step_command (const char *count_string, int from_tty)
936 step_1 (0, 0, count_string);
939 /* Likewise, but skip over subroutine calls as if single instructions. */
942 next_command (const char *count_string, int from_tty)
944 step_1 (1, 0, count_string);
947 /* Likewise, but step only one instruction. */
950 stepi_command (const char *count_string, int from_tty)
952 step_1 (0, 1, count_string);
956 nexti_command (const char *count_string, int from_tty)
958 step_1 (1, 1, count_string);
962 delete_longjmp_breakpoint_cleanup (void *arg)
964 int thread = * (int *) arg;
965 delete_longjmp_breakpoint (thread);
968 /* Data for the FSM that manages the step/next/stepi/nexti
971 struct step_command_fsm
973 /* The base class. */
974 struct thread_fsm thread_fsm;
976 /* How many steps left in a "step N"-like command. */
979 /* If true, this is a next/nexti, otherwise a step/stepi. */
980 int skip_subroutines;
982 /* If true, this is a stepi/nexti, otherwise a step/step. */
986 static void step_command_fsm_clean_up (struct thread_fsm *self,
987 struct thread_info *thread);
988 static int step_command_fsm_should_stop (struct thread_fsm *self,
989 struct thread_info *thread);
990 static enum async_reply_reason
991 step_command_fsm_async_reply_reason (struct thread_fsm *self);
993 /* step_command_fsm's vtable. */
995 static struct thread_fsm_ops step_command_fsm_ops =
998 step_command_fsm_clean_up,
999 step_command_fsm_should_stop,
1000 NULL, /* return_value */
1001 step_command_fsm_async_reply_reason,
1004 /* Allocate a new step_command_fsm. */
1006 static struct step_command_fsm *
1007 new_step_command_fsm (struct interp *cmd_interp)
1009 struct step_command_fsm *sm;
1011 sm = XCNEW (struct step_command_fsm);
1012 thread_fsm_ctor (&sm->thread_fsm, &step_command_fsm_ops, cmd_interp);
1017 /* Prepare for a step/next/etc. command. Any target resource
1018 allocated here is undone in the FSM's clean_up method. */
1021 step_command_fsm_prepare (struct step_command_fsm *sm,
1022 int skip_subroutines, int single_inst,
1023 int count, struct thread_info *thread)
1025 sm->skip_subroutines = skip_subroutines;
1026 sm->single_inst = single_inst;
1029 /* Leave the si command alone. */
1030 if (!sm->single_inst || sm->skip_subroutines)
1031 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ()));
1033 thread->control.stepping_command = 1;
1036 static int prepare_one_step (struct step_command_fsm *sm);
1039 step_1 (int skip_subroutines, int single_inst, const char *count_string)
1043 struct thread_info *thr;
1044 struct step_command_fsm *step_sm;
1047 ensure_not_tfind_mode ();
1048 ensure_valid_thread ();
1049 ensure_not_running ();
1051 gdb::unique_xmalloc_ptr<char> stripped
1052 = strip_bg_char (count_string, &async_exec);
1053 count_string = stripped.get ();
1055 prepare_execution_command (¤t_target, async_exec);
1057 count = count_string ? parse_and_eval_long (count_string) : 1;
1059 clear_proceed_status (1);
1061 /* Setup the execution command state machine to handle all the COUNT
1063 thr = inferior_thread ();
1064 step_sm = new_step_command_fsm (command_interp ());
1065 thr->thread_fsm = &step_sm->thread_fsm;
1067 step_command_fsm_prepare (step_sm, skip_subroutines,
1068 single_inst, count, thr);
1070 /* Do only one step for now, before returning control to the event
1071 loop. Let the continuation figure out how many other steps we
1072 need to do, and handle them one at the time, through
1074 if (!prepare_one_step (step_sm))
1075 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1080 /* Stepped into an inline frame. Pretend that we've
1082 thread_fsm_clean_up (thr->thread_fsm, thr);
1083 proceeded = normal_stop ();
1085 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
1086 all_uis_check_sync_execution_done ();
1090 /* Implementation of the 'should_stop' FSM method for stepping
1091 commands. Called after we are done with one step operation, to
1092 check whether we need to step again, before we print the prompt and
1093 return control to the user. If count is > 1, returns false, as we
1094 will need to keep going. */
1097 step_command_fsm_should_stop (struct thread_fsm *self, struct thread_info *tp)
1099 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1101 if (tp->control.stop_step)
1103 /* There are more steps to make, and we did stop due to
1104 ending a stepping range. Do another step. */
1105 if (--sm->count > 0)
1106 return prepare_one_step (sm);
1108 thread_fsm_set_finished (self);
1114 /* Implementation of the 'clean_up' FSM method for stepping commands. */
1117 step_command_fsm_clean_up (struct thread_fsm *self, struct thread_info *thread)
1119 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1121 if (!sm->single_inst || sm->skip_subroutines)
1122 delete_longjmp_breakpoint (thread->global_num);
1125 /* Implementation of the 'async_reply_reason' FSM method for stepping
1128 static enum async_reply_reason
1129 step_command_fsm_async_reply_reason (struct thread_fsm *self)
1131 return EXEC_ASYNC_END_STEPPING_RANGE;
1134 /* Prepare for one step in "step N". The actual target resumption is
1135 done by the caller. Return true if we're done and should thus
1136 report a stop to the user. Returns false if the target needs to be
1140 prepare_one_step (struct step_command_fsm *sm)
1144 struct frame_info *frame = get_current_frame ();
1146 /* Don't assume THREAD is a valid thread id. It is set to -1 if
1147 the longjmp breakpoint was not required. Use the
1148 INFERIOR_PTID thread instead, which is the same thread when
1150 struct thread_info *tp = inferior_thread ();
1154 if (!sm->single_inst)
1158 /* Step at an inlined function behaves like "down". */
1159 if (!sm->skip_subroutines
1160 && inline_skipped_frames (inferior_ptid))
1164 /* Pretend that we've ran. */
1165 resume_ptid = user_visible_resume_ptid (1);
1166 set_running (resume_ptid, 1);
1168 step_into_inline_frame (inferior_ptid);
1170 return prepare_one_step (sm);
1173 pc = get_frame_pc (frame);
1174 find_pc_line_pc_range (pc,
1175 &tp->control.step_range_start,
1176 &tp->control.step_range_end);
1178 tp->control.may_range_step = 1;
1180 /* If we have no line info, switch to stepi mode. */
1181 if (tp->control.step_range_end == 0 && step_stop_if_no_debug)
1183 tp->control.step_range_start = tp->control.step_range_end = 1;
1184 tp->control.may_range_step = 0;
1186 else if (tp->control.step_range_end == 0)
1190 if (find_pc_partial_function (pc, &name,
1191 &tp->control.step_range_start,
1192 &tp->control.step_range_end) == 0)
1193 error (_("Cannot find bounds of current function"));
1195 target_terminal::ours_for_output ();
1196 printf_filtered (_("Single stepping until exit from function %s,"
1197 "\nwhich has no line number information.\n"),
1203 /* Say we are stepping, but stop after one insn whatever it does. */
1204 tp->control.step_range_start = tp->control.step_range_end = 1;
1205 if (!sm->skip_subroutines)
1207 Don't step over function calls, not even to functions lacking
1209 tp->control.step_over_calls = STEP_OVER_NONE;
1212 if (sm->skip_subroutines)
1213 tp->control.step_over_calls = STEP_OVER_ALL;
1219 thread_fsm_set_finished (&sm->thread_fsm);
1224 /* Continue program at specified address. */
1227 jump_command (const char *arg, int from_tty)
1229 struct gdbarch *gdbarch = get_current_arch ();
1236 ensure_not_tfind_mode ();
1237 ensure_valid_thread ();
1238 ensure_not_running ();
1240 /* Find out whether we must run in the background. */
1241 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1242 arg = stripped.get ();
1244 prepare_execution_command (¤t_target, async_exec);
1247 error_no_arg (_("starting address"));
1249 std::vector<symtab_and_line> sals
1250 = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE);
1251 if (sals.size () != 1)
1252 error (_("Unreasonable jump request"));
1254 symtab_and_line &sal = sals[0];
1256 if (sal.symtab == 0 && sal.pc == 0)
1257 error (_("No source file has been specified."));
1259 resolve_sal_pc (&sal); /* May error out. */
1261 /* See if we are trying to jump to another function. */
1262 fn = get_frame_function (get_current_frame ());
1263 sfn = find_pc_function (sal.pc);
1264 if (fn != NULL && sfn != fn)
1266 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1267 SYMBOL_PRINT_NAME (fn)))
1269 error (_("Not confirmed."));
1276 struct obj_section *section;
1278 fixup_symbol_section (sfn, 0);
1279 section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn);
1280 if (section_is_overlay (section)
1281 && !section_is_mapped (section))
1283 if (!query (_("WARNING!!! Destination is in "
1284 "unmapped overlay! Jump anyway? ")))
1286 error (_("Not confirmed."));
1296 printf_filtered (_("Continuing at "));
1297 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1298 printf_filtered (".\n");
1301 clear_proceed_status (0);
1302 proceed (addr, GDB_SIGNAL_0);
1305 /* Continue program giving it specified signal. */
1308 signal_command (const char *signum_exp, int from_tty)
1310 enum gdb_signal oursig;
1313 dont_repeat (); /* Too dangerous. */
1315 ensure_not_tfind_mode ();
1316 ensure_valid_thread ();
1317 ensure_not_running ();
1319 /* Find out whether we must run in the background. */
1320 gdb::unique_xmalloc_ptr<char> stripped
1321 = strip_bg_char (signum_exp, &async_exec);
1322 signum_exp = stripped.get ();
1324 prepare_execution_command (¤t_target, async_exec);
1327 error_no_arg (_("signal number"));
1329 /* It would be even slicker to make signal names be valid expressions,
1330 (the type could be "enum $signal" or some such), then the user could
1331 assign them to convenience variables. */
1332 oursig = gdb_signal_from_name (signum_exp);
1334 if (oursig == GDB_SIGNAL_UNKNOWN)
1336 /* No, try numeric. */
1337 int num = parse_and_eval_long (signum_exp);
1340 oursig = GDB_SIGNAL_0;
1342 oursig = gdb_signal_from_command (num);
1345 /* Look for threads other than the current that this command ends up
1346 resuming too (due to schedlock off), and warn if they'll get a
1347 signal delivered. "signal 0" is used to suppress a previous
1348 signal, but if the current thread is no longer the one that got
1349 the signal, then the user is potentially suppressing the signal
1350 of the wrong thread. */
1353 struct thread_info *tp;
1355 int must_confirm = 0;
1357 /* This indicates what will be resumed. Either a single thread,
1358 a whole process, or all threads of all processes. */
1359 resume_ptid = user_visible_resume_ptid (0);
1361 ALL_NON_EXITED_THREADS (tp)
1363 if (ptid_equal (tp->ptid, inferior_ptid))
1365 if (!ptid_match (tp->ptid, resume_ptid))
1368 if (tp->suspend.stop_signal != GDB_SIGNAL_0
1369 && signal_pass_state (tp->suspend.stop_signal))
1372 printf_unfiltered (_("Note:\n"));
1373 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1374 print_thread_id (tp),
1375 gdb_signal_to_name (tp->suspend.stop_signal),
1376 gdb_signal_to_string (tp->suspend.stop_signal));
1382 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1383 "still deliver the signals noted above to their respective threads.\n"
1384 "Continue anyway? "),
1385 print_thread_id (inferior_thread ())))
1386 error (_("Not confirmed."));
1391 if (oursig == GDB_SIGNAL_0)
1392 printf_filtered (_("Continuing with no signal.\n"));
1394 printf_filtered (_("Continuing with signal %s.\n"),
1395 gdb_signal_to_name (oursig));
1398 clear_proceed_status (0);
1399 proceed ((CORE_ADDR) -1, oursig);
1402 /* Queue a signal to be delivered to the current thread. */
1405 queue_signal_command (const char *signum_exp, int from_tty)
1407 enum gdb_signal oursig;
1408 struct thread_info *tp;
1411 ensure_not_tfind_mode ();
1412 ensure_valid_thread ();
1413 ensure_not_running ();
1415 if (signum_exp == NULL)
1416 error_no_arg (_("signal number"));
1418 /* It would be even slicker to make signal names be valid expressions,
1419 (the type could be "enum $signal" or some such), then the user could
1420 assign them to convenience variables. */
1421 oursig = gdb_signal_from_name (signum_exp);
1423 if (oursig == GDB_SIGNAL_UNKNOWN)
1425 /* No, try numeric. */
1426 int num = parse_and_eval_long (signum_exp);
1429 oursig = GDB_SIGNAL_0;
1431 oursig = gdb_signal_from_command (num);
1434 if (oursig != GDB_SIGNAL_0
1435 && !signal_pass_state (oursig))
1436 error (_("Signal handling set to not pass this signal to the program."));
1438 tp = inferior_thread ();
1439 tp->suspend.stop_signal = oursig;
1442 /* Data for the FSM that manages the until (with no argument)
1445 struct until_next_fsm
1447 /* The base class. */
1448 struct thread_fsm thread_fsm;
1450 /* The thread that as current when the command was executed. */
1454 static int until_next_fsm_should_stop (struct thread_fsm *self,
1455 struct thread_info *thread);
1456 static void until_next_fsm_clean_up (struct thread_fsm *self,
1457 struct thread_info *thread);
1458 static enum async_reply_reason
1459 until_next_fsm_async_reply_reason (struct thread_fsm *self);
1461 /* until_next_fsm's vtable. */
1463 static struct thread_fsm_ops until_next_fsm_ops =
1466 until_next_fsm_clean_up,
1467 until_next_fsm_should_stop,
1468 NULL, /* return_value */
1469 until_next_fsm_async_reply_reason,
1472 /* Allocate a new until_next_fsm. */
1474 static struct until_next_fsm *
1475 new_until_next_fsm (struct interp *cmd_interp, int thread)
1477 struct until_next_fsm *sm;
1479 sm = XCNEW (struct until_next_fsm);
1480 thread_fsm_ctor (&sm->thread_fsm, &until_next_fsm_ops, cmd_interp);
1482 sm->thread = thread;
1487 /* Implementation of the 'should_stop' FSM method for the until (with
1491 until_next_fsm_should_stop (struct thread_fsm *self,
1492 struct thread_info *tp)
1494 if (tp->control.stop_step)
1495 thread_fsm_set_finished (self);
1500 /* Implementation of the 'clean_up' FSM method for the until (with no
1504 until_next_fsm_clean_up (struct thread_fsm *self, struct thread_info *thread)
1506 struct until_next_fsm *sm = (struct until_next_fsm *) self;
1508 delete_longjmp_breakpoint (thread->global_num);
1511 /* Implementation of the 'async_reply_reason' FSM method for the until
1512 (with no arg) command. */
1514 static enum async_reply_reason
1515 until_next_fsm_async_reply_reason (struct thread_fsm *self)
1517 return EXEC_ASYNC_END_STEPPING_RANGE;
1520 /* Proceed until we reach a different source line with pc greater than
1521 our current one or exit the function. We skip calls in both cases.
1523 Note that eventually this command should probably be changed so
1524 that only source lines are printed out when we hit the breakpoint
1525 we set. This may involve changes to wait_for_inferior and the
1526 proceed status code. */
1529 until_next_command (int from_tty)
1531 struct frame_info *frame;
1533 struct symbol *func;
1534 struct symtab_and_line sal;
1535 struct thread_info *tp = inferior_thread ();
1536 int thread = tp->global_num;
1537 struct cleanup *old_chain;
1538 struct until_next_fsm *sm;
1540 clear_proceed_status (0);
1543 frame = get_current_frame ();
1545 /* Step until either exited from this function or greater
1546 than the current line (if in symbolic section) or pc (if
1549 pc = get_frame_pc (frame);
1550 func = find_pc_function (pc);
1554 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1556 if (msymbol.minsym == NULL)
1557 error (_("Execution is not within a known function."));
1559 tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol);
1560 /* The upper-bound of step_range is exclusive. In order to make PC
1561 within the range, set the step_range_end with PC + 1. */
1562 tp->control.step_range_end = pc + 1;
1566 sal = find_pc_line (pc, 0);
1568 tp->control.step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1569 tp->control.step_range_end = sal.end;
1571 tp->control.may_range_step = 1;
1573 tp->control.step_over_calls = STEP_OVER_ALL;
1575 set_longjmp_breakpoint (tp, get_frame_id (frame));
1576 old_chain = make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
1578 sm = new_until_next_fsm (command_interp (), tp->global_num);
1579 tp->thread_fsm = &sm->thread_fsm;
1580 discard_cleanups (old_chain);
1582 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1586 until_command (const char *arg, int from_tty)
1591 ensure_not_tfind_mode ();
1592 ensure_valid_thread ();
1593 ensure_not_running ();
1595 /* Find out whether we must run in the background. */
1596 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1597 arg = stripped.get ();
1599 prepare_execution_command (¤t_target, async_exec);
1602 until_break_command (arg, from_tty, 0);
1604 until_next_command (from_tty);
1608 advance_command (const char *arg, int from_tty)
1613 ensure_not_tfind_mode ();
1614 ensure_valid_thread ();
1615 ensure_not_running ();
1618 error_no_arg (_("a location"));
1620 /* Find out whether we must run in the background. */
1621 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
1622 arg = stripped.get ();
1624 prepare_execution_command (¤t_target, async_exec);
1626 until_break_command (arg, from_tty, 1);
1629 /* Return the value of the result of a function at the end of a 'finish'
1630 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1631 right after an inferior call has finished. */
1634 get_return_value (struct value *function, struct type *value_type)
1636 regcache stop_regs (regcache::readonly, *get_current_regcache ());
1637 struct gdbarch *gdbarch = stop_regs.arch ();
1638 struct value *value;
1640 value_type = check_typedef (value_type);
1641 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
1643 /* FIXME: 2003-09-27: When returning from a nested inferior function
1644 call, it's possible (with no help from the architecture vector)
1645 to locate and return/print a "struct return" value. This is just
1646 a more complicated case of what is already being done in the
1647 inferior function call code. In fact, when inferior function
1648 calls are made async, this will likely be made the norm. */
1650 switch (gdbarch_return_value (gdbarch, function, value_type,
1653 case RETURN_VALUE_REGISTER_CONVENTION:
1654 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1655 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1656 value = allocate_value (value_type);
1657 gdbarch_return_value (gdbarch, function, value_type, &stop_regs,
1658 value_contents_raw (value), NULL);
1660 case RETURN_VALUE_STRUCT_CONVENTION:
1664 internal_error (__FILE__, __LINE__, _("bad switch"));
1670 /* The captured function return value/type and its position in the
1673 struct return_value_info
1675 /* The captured return value. May be NULL if we weren't able to
1676 retrieve it. See get_return_value. */
1677 struct value *value;
1679 /* The return type. In some cases, we'll not be able extract the
1680 return value, but we always know the type. */
1683 /* If we captured a value, this is the value history index. */
1684 int value_history_index;
1687 /* Helper for print_return_value. */
1690 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1692 if (rv->value != NULL)
1694 struct value_print_options opts;
1697 uiout->text ("Value returned is ");
1698 uiout->field_fmt ("gdb-result-var", "$%d",
1699 rv->value_history_index);
1700 uiout->text (" = ");
1701 get_no_prettyformat_print_options (&opts);
1705 value_print (rv->value, &stb, &opts);
1706 uiout->field_stream ("return-value", stb);
1711 std::string type_name = type_to_string (rv->type);
1712 uiout->text ("Value returned has type: ");
1713 uiout->field_string ("return-type", type_name.c_str ());
1715 uiout->text (" Cannot determine contents\n");
1719 /* Print the result of a function at the end of a 'finish' command.
1720 RV points at an object representing the captured return value/type
1721 and its position in the value history. */
1724 print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1726 if (rv->type == NULL || TYPE_CODE (rv->type) == TYPE_CODE_VOID)
1731 /* print_return_value_1 can throw an exception in some
1732 circumstances. We need to catch this so that we still
1733 delete the breakpoint. */
1734 print_return_value_1 (uiout, rv);
1736 CATCH (ex, RETURN_MASK_ALL)
1738 exception_print (gdb_stdout, ex);
1743 /* Data for the FSM that manages the finish command. */
1745 struct finish_command_fsm
1747 /* The base class. */
1748 struct thread_fsm thread_fsm;
1750 /* The momentary breakpoint set at the function's return address in
1752 struct breakpoint *breakpoint;
1754 /* The function that we're stepping out of. */
1755 struct symbol *function;
1757 /* If the FSM finishes successfully, this stores the function's
1759 struct return_value_info return_value;
1762 static int finish_command_fsm_should_stop (struct thread_fsm *self,
1763 struct thread_info *thread);
1764 static void finish_command_fsm_clean_up (struct thread_fsm *self,
1765 struct thread_info *thread);
1766 static struct return_value_info *
1767 finish_command_fsm_return_value (struct thread_fsm *self);
1768 static enum async_reply_reason
1769 finish_command_fsm_async_reply_reason (struct thread_fsm *self);
1771 /* finish_command_fsm's vtable. */
1773 static struct thread_fsm_ops finish_command_fsm_ops =
1776 finish_command_fsm_clean_up,
1777 finish_command_fsm_should_stop,
1778 finish_command_fsm_return_value,
1779 finish_command_fsm_async_reply_reason,
1780 NULL, /* should_notify_stop */
1783 /* Allocate a new finish_command_fsm. */
1785 static struct finish_command_fsm *
1786 new_finish_command_fsm (struct interp *cmd_interp)
1788 struct finish_command_fsm *sm;
1790 sm = XCNEW (struct finish_command_fsm);
1791 thread_fsm_ctor (&sm->thread_fsm, &finish_command_fsm_ops, cmd_interp);
1796 /* Implementation of the 'should_stop' FSM method for the finish
1797 commands. Detects whether the thread stepped out of the function
1798 successfully, and if so, captures the function's return value and
1799 marks the FSM finished. */
1802 finish_command_fsm_should_stop (struct thread_fsm *self,
1803 struct thread_info *tp)
1805 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1806 struct return_value_info *rv = &f->return_value;
1808 if (f->function != NULL
1809 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1810 f->breakpoint) != NULL)
1813 thread_fsm_set_finished (self);
1815 rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (f->function));
1816 if (rv->type == NULL)
1817 internal_error (__FILE__, __LINE__,
1818 _("finish_command: function has no target type"));
1820 if (TYPE_CODE (rv->type) != TYPE_CODE_VOID)
1824 func = read_var_value (f->function, NULL, get_current_frame ());
1825 rv->value = get_return_value (func, rv->type);
1826 if (rv->value != NULL)
1827 rv->value_history_index = record_latest_value (rv->value);
1830 else if (tp->control.stop_step)
1832 /* Finishing from an inline frame, or reverse finishing. In
1833 either case, there's no way to retrieve the return value. */
1834 thread_fsm_set_finished (self);
1840 /* Implementation of the 'clean_up' FSM method for the finish
1844 finish_command_fsm_clean_up (struct thread_fsm *self,
1845 struct thread_info *thread)
1847 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1849 if (f->breakpoint != NULL)
1851 delete_breakpoint (f->breakpoint);
1852 f->breakpoint = NULL;
1854 delete_longjmp_breakpoint (thread->global_num);
1857 /* Implementation of the 'return_value' FSM method for the finish
1860 static struct return_value_info *
1861 finish_command_fsm_return_value (struct thread_fsm *self)
1863 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1865 return &f->return_value;
1868 /* Implementation of the 'async_reply_reason' FSM method for the
1871 static enum async_reply_reason
1872 finish_command_fsm_async_reply_reason (struct thread_fsm *self)
1874 if (execution_direction == EXEC_REVERSE)
1875 return EXEC_ASYNC_END_STEPPING_RANGE;
1877 return EXEC_ASYNC_FUNCTION_FINISHED;
1880 /* finish_backward -- helper function for finish_command. */
1883 finish_backward (struct finish_command_fsm *sm)
1885 struct symtab_and_line sal;
1886 struct thread_info *tp = inferior_thread ();
1888 CORE_ADDR func_addr;
1890 pc = get_frame_pc (get_current_frame ());
1892 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0)
1893 error (_("Cannot find bounds of current function"));
1895 sal = find_pc_line (func_addr, 0);
1897 tp->control.proceed_to_finish = 1;
1898 /* Special case: if we're sitting at the function entry point,
1899 then all we need to do is take a reverse singlestep. We
1900 don't need to set a breakpoint, and indeed it would do us
1903 Note that this can only happen at frame #0, since there's
1904 no way that a function up the stack can have a return address
1905 that's equal to its entry point. */
1909 struct frame_info *frame = get_selected_frame (NULL);
1910 struct gdbarch *gdbarch = get_frame_arch (frame);
1912 /* Set a step-resume at the function's entry point. Once that's
1913 hit, we'll do one more step backwards. */
1914 symtab_and_line sr_sal;
1916 sr_sal.pspace = get_frame_program_space (frame);
1917 insert_step_resume_breakpoint_at_sal (gdbarch,
1918 sr_sal, null_frame_id);
1920 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1924 /* We're almost there -- we just need to back up by one more
1926 tp->control.step_range_start = tp->control.step_range_end = 1;
1927 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1931 /* finish_forward -- helper function for finish_command. FRAME is the
1932 frame that called the function we're about to step out of. */
1935 finish_forward (struct finish_command_fsm *sm, struct frame_info *frame)
1937 struct frame_id frame_id = get_frame_id (frame);
1938 struct gdbarch *gdbarch = get_frame_arch (frame);
1939 struct symtab_and_line sal;
1940 struct thread_info *tp = inferior_thread ();
1942 sal = find_pc_line (get_frame_pc (frame), 0);
1943 sal.pc = get_frame_pc (frame);
1945 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1946 get_stack_frame_id (frame),
1947 bp_finish).release ();
1949 /* set_momentary_breakpoint invalidates FRAME. */
1952 set_longjmp_breakpoint (tp, frame_id);
1954 /* We want to print return value, please... */
1955 tp->control.proceed_to_finish = 1;
1957 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1960 /* Skip frames for "finish". */
1962 static struct frame_info *
1963 skip_finish_frames (struct frame_info *frame)
1965 struct frame_info *start;
1971 frame = skip_tailcall_frames (frame);
1975 frame = skip_unwritable_frames (frame);
1979 while (start != frame);
1984 /* "finish": Set a temporary breakpoint at the place the selected
1985 frame will return to, then continue. */
1988 finish_command (const char *arg, int from_tty)
1990 struct frame_info *frame;
1992 struct finish_command_fsm *sm;
1993 struct thread_info *tp;
1996 ensure_not_tfind_mode ();
1997 ensure_valid_thread ();
1998 ensure_not_running ();
2000 /* Find out whether we must run in the background. */
2001 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec);
2002 arg = stripped.get ();
2004 prepare_execution_command (¤t_target, async_exec);
2007 error (_("The \"finish\" command does not take any arguments."));
2009 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
2011 error (_("\"finish\" not meaningful in the outermost frame."));
2013 clear_proceed_status (0);
2015 tp = inferior_thread ();
2017 sm = new_finish_command_fsm (command_interp ());
2019 tp->thread_fsm = &sm->thread_fsm;
2021 /* Finishing from an inline frame is completely different. We don't
2022 try to show the "return value" - no way to locate it. */
2023 if (get_frame_type (get_selected_frame (_("No selected frame.")))
2026 /* Claim we are stepping in the calling frame. An empty step
2027 range means that we will stop once we aren't in a function
2028 called by that frame. We don't use the magic "1" value for
2029 step_range_end, because then infrun will think this is nexti,
2030 and not step over the rest of this inlined function call. */
2031 set_step_info (frame, {});
2032 tp->control.step_range_start = get_frame_pc (frame);
2033 tp->control.step_range_end = tp->control.step_range_start;
2034 tp->control.step_over_calls = STEP_OVER_ALL;
2036 /* Print info on the selected frame, including level number but not
2040 printf_filtered (_("Run till exit from "));
2041 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2044 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2048 /* Find the function we will return from. */
2050 sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
2052 /* Print info on the selected frame, including level number but not
2056 if (execution_direction == EXEC_REVERSE)
2057 printf_filtered (_("Run back to call of "));
2060 if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type)
2061 && !query (_("warning: Function %s does not return normally.\n"
2062 "Try to finish anyway? "),
2063 SYMBOL_PRINT_NAME (sm->function)))
2064 error (_("Not confirmed."));
2065 printf_filtered (_("Run till exit from "));
2068 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2071 if (execution_direction == EXEC_REVERSE)
2072 finish_backward (sm);
2075 frame = skip_finish_frames (frame);
2078 error (_("Cannot find the caller frame."));
2080 finish_forward (sm, frame);
2086 info_program_command (char *args, int from_tty)
2090 struct thread_info *tp;
2093 if (!target_has_execution)
2095 printf_filtered (_("The program being debugged is not being run.\n"));
2100 ptid = inferior_ptid;
2103 struct target_waitstatus ws;
2105 get_last_target_status (&ptid, &ws);
2108 if (ptid_equal (ptid, null_ptid) || is_exited (ptid))
2109 error (_("Invalid selected thread."));
2110 else if (is_running (ptid))
2111 error (_("Selected thread is running."));
2113 tp = find_thread_ptid (ptid);
2114 bs = tp->control.stop_bpstat;
2115 stat = bpstat_num (&bs, &num);
2117 target_files_info ();
2118 printf_filtered (_("Program stopped at %s.\n"),
2119 paddress (target_gdbarch (), stop_pc));
2120 if (tp->control.stop_step)
2121 printf_filtered (_("It stopped after being stepped.\n"));
2124 /* There may be several breakpoints in the same place, so this
2125 isn't as strange as it seems. */
2130 printf_filtered (_("It stopped at a breakpoint "
2131 "that has since been deleted.\n"));
2134 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
2135 stat = bpstat_num (&bs, &num);
2138 else if (tp->suspend.stop_signal != GDB_SIGNAL_0)
2140 printf_filtered (_("It stopped with signal %s, %s.\n"),
2141 gdb_signal_to_name (tp->suspend.stop_signal),
2142 gdb_signal_to_string (tp->suspend.stop_signal));
2147 printf_filtered (_("Type \"info stack\" or \"info "
2148 "registers\" for more information.\n"));
2153 environment_info (const char *var, int from_tty)
2157 const char *val = current_inferior ()->environment.get (var);
2161 puts_filtered (var);
2162 puts_filtered (" = ");
2163 puts_filtered (val);
2164 puts_filtered ("\n");
2168 puts_filtered ("Environment variable \"");
2169 puts_filtered (var);
2170 puts_filtered ("\" not defined.\n");
2175 char **envp = current_inferior ()->environment.envp ();
2177 for (int idx = 0; envp[idx] != NULL; ++idx)
2179 puts_filtered (envp[idx]);
2180 puts_filtered ("\n");
2186 set_environment_command (const char *arg, int from_tty)
2188 const char *p, *val;
2192 error_no_arg (_("environment variable and value"));
2194 /* Find seperation between variable name and value. */
2195 p = (char *) strchr (arg, '=');
2196 val = (char *) strchr (arg, ' ');
2198 if (p != 0 && val != 0)
2200 /* We have both a space and an equals. If the space is before the
2201 equals, walk forward over the spaces til we see a nonspace
2202 (possibly the equals). */
2207 /* Now if the = is after the char following the spaces,
2208 take the char following the spaces. */
2212 else if (val != 0 && p == 0)
2216 error_no_arg (_("environment variable to set"));
2218 if (p == 0 || p[1] == 0)
2222 p = arg + strlen (arg); /* So that savestring below will work. */
2226 /* Not setting variable value to null. */
2228 while (*val == ' ' || *val == '\t')
2232 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2235 std::string var (arg, p - arg);
2238 printf_filtered (_("Setting environment variable "
2239 "\"%s\" to null value.\n"),
2241 current_inferior ()->environment.set (var.c_str (), "");
2244 current_inferior ()->environment.set (var.c_str (), val);
2248 unset_environment_command (const char *var, int from_tty)
2252 /* If there is no argument, delete all environment variables.
2253 Ask for confirmation if reading from the terminal. */
2254 if (!from_tty || query (_("Delete all environment variables? ")))
2255 current_inferior ()->environment.clear ();
2258 current_inferior ()->environment.unset (var);
2261 /* Handle the execution path (PATH variable). */
2263 static const char path_var_name[] = "PATH";
2266 path_info (const char *args, int from_tty)
2268 puts_filtered ("Executable and object file path: ");
2269 puts_filtered (current_inferior ()->environment.get (path_var_name));
2270 puts_filtered ("\n");
2273 /* Add zero or more directories to the front of the execution path. */
2276 path_command (const char *dirname, int from_tty)
2282 env = current_inferior ()->environment.get (path_var_name);
2283 /* Can be null if path is not set. */
2286 exec_path = xstrdup (env);
2287 mod_path (dirname, &exec_path);
2288 current_inferior ()->environment.set (path_var_name, exec_path);
2291 path_info ((char *) NULL, from_tty);
2295 /* Print out the register NAME with value VAL, to FILE, in the default
2299 default_print_one_register_info (struct ui_file *file,
2303 struct type *regtype = value_type (val);
2304 int print_raw_format;
2306 fputs_filtered (name, file);
2307 print_spaces_filtered (15 - strlen (name), file);
2309 print_raw_format = (value_entirely_available (val)
2310 && !value_optimized_out (val));
2312 /* If virtual format is floating, print it that way, and in raw
2314 if (TYPE_CODE (regtype) == TYPE_CODE_FLT
2315 || TYPE_CODE (regtype) == TYPE_CODE_DECFLOAT)
2317 struct value_print_options opts;
2318 const gdb_byte *valaddr = value_contents_for_printing (val);
2319 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (regtype));
2321 get_user_print_options (&opts);
2325 value_embedded_offset (val), 0,
2326 file, 0, val, &opts, current_language);
2328 if (print_raw_format)
2330 fprintf_filtered (file, "\t(raw ");
2331 print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order,
2333 fprintf_filtered (file, ")");
2338 struct value_print_options opts;
2340 /* Print the register in hex. */
2341 get_formatted_print_options (&opts, 'x');
2344 value_embedded_offset (val), 0,
2345 file, 0, val, &opts, current_language);
2346 /* If not a vector register, print it also according to its
2348 if (print_raw_format && TYPE_VECTOR (regtype) == 0)
2350 get_user_print_options (&opts);
2352 fprintf_filtered (file, "\t");
2354 value_embedded_offset (val), 0,
2355 file, 0, val, &opts, current_language);
2359 fprintf_filtered (file, "\n");
2362 /* Print out the machine register regnum. If regnum is -1, print all
2363 registers (print_all == 1) or all non-float and non-vector
2364 registers (print_all == 0).
2366 For most machines, having all_registers_info() print the
2367 register(s) one per line is good enough. If a different format is
2368 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2369 regs), or there is an existing convention for showing all the
2370 registers, define the architecture method PRINT_REGISTERS_INFO to
2371 provide that format. */
2374 default_print_registers_info (struct gdbarch *gdbarch,
2375 struct ui_file *file,
2376 struct frame_info *frame,
2377 int regnum, int print_all)
2380 const int numregs = gdbarch_num_regs (gdbarch)
2381 + gdbarch_num_pseudo_regs (gdbarch);
2383 for (i = 0; i < numregs; i++)
2385 /* Decide between printing all regs, non-float / vector regs, or
2391 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2396 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2406 /* If the register name is empty, it is undefined for this
2407 processor, so don't display anything. */
2408 if (gdbarch_register_name (gdbarch, i) == NULL
2409 || *(gdbarch_register_name (gdbarch, i)) == '\0')
2412 default_print_one_register_info (file,
2413 gdbarch_register_name (gdbarch, i),
2414 value_of_register (i, frame));
2419 registers_info (char *addr_exp, int fpregs)
2421 struct frame_info *frame;
2422 struct gdbarch *gdbarch;
2424 if (!target_has_registers)
2425 error (_("The program has no registers now."));
2426 frame = get_selected_frame (NULL);
2427 gdbarch = get_frame_arch (frame);
2431 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2436 while (*addr_exp != '\0')
2441 /* Skip leading white space. */
2442 addr_exp = skip_spaces (addr_exp);
2444 /* Discard any leading ``$''. Check that there is something
2445 resembling a register following it. */
2446 if (addr_exp[0] == '$')
2448 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2449 error (_("Missing register name"));
2451 /* Find the start/end of this register name/num/group. */
2453 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2457 /* Figure out what we've found and display it. */
2459 /* A register name? */
2461 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2465 /* User registers lie completely outside of the range of
2466 normal registers. Catch them early so that the target
2468 if (regnum >= gdbarch_num_regs (gdbarch)
2469 + gdbarch_num_pseudo_regs (gdbarch))
2471 struct value *regval = value_of_user_reg (regnum, frame);
2472 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2475 /* Print in the same fashion
2476 gdbarch_print_registers_info's default
2477 implementation prints. */
2478 default_print_one_register_info (gdb_stdout,
2483 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2484 frame, regnum, fpregs);
2489 /* A register group? */
2491 struct reggroup *group;
2493 for (group = reggroup_next (gdbarch, NULL);
2495 group = reggroup_next (gdbarch, group))
2497 /* Don't bother with a length check. Should the user
2498 enter a short register group name, go with the first
2499 group that matches. */
2500 if (strncmp (start, reggroup_name (group), end - start) == 0)
2508 regnum < gdbarch_num_regs (gdbarch)
2509 + gdbarch_num_pseudo_regs (gdbarch);
2512 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2513 gdbarch_print_registers_info (gdbarch,
2521 /* Nothing matched. */
2522 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2527 info_all_registers_command (char *addr_exp, int from_tty)
2529 registers_info (addr_exp, 1);
2533 info_registers_command (char *addr_exp, int from_tty)
2535 registers_info (addr_exp, 0);
2539 print_vector_info (struct ui_file *file,
2540 struct frame_info *frame, const char *args)
2542 struct gdbarch *gdbarch = get_frame_arch (frame);
2544 if (gdbarch_print_vector_info_p (gdbarch))
2545 gdbarch_print_vector_info (gdbarch, file, frame, args);
2549 int printed_something = 0;
2552 regnum < gdbarch_num_regs (gdbarch)
2553 + gdbarch_num_pseudo_regs (gdbarch);
2556 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2558 printed_something = 1;
2559 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2562 if (!printed_something)
2563 fprintf_filtered (file, "No vector information\n");
2568 info_vector_command (char *args, int from_tty)
2570 if (!target_has_registers)
2571 error (_("The program has no registers now."));
2573 print_vector_info (gdb_stdout, get_selected_frame (NULL), args);
2576 /* Kill the inferior process. Make us have no inferior. */
2579 kill_command (const char *arg, int from_tty)
2581 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2582 It should be a distinct flag that indicates that a target is active, cuz
2583 some targets don't have processes! */
2585 if (ptid_equal (inferior_ptid, null_ptid))
2586 error (_("The program is not being run."));
2587 if (!query (_("Kill the program being debugged? ")))
2588 error (_("Not confirmed."));
2591 /* If we still have other inferiors to debug, then don't mess with
2592 with their threads. */
2593 if (!have_inferiors ())
2595 init_thread_list (); /* Destroy thread info. */
2597 /* Killing off the inferior can leave us with a core file. If
2598 so, print the state we are left in. */
2599 if (target_has_stack)
2601 printf_filtered (_("In %s,\n"), target_longname);
2602 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2605 bfd_cache_close_all ();
2608 /* Used in `attach&' command. ARG is a point to an integer
2609 representing a process id. Proceed threads of this process iff
2610 they stopped due to debugger request, and when they did, they
2611 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads
2612 that have been explicitly been told to stop. */
2615 proceed_after_attach_callback (struct thread_info *thread,
2618 int pid = * (int *) arg;
2620 if (ptid_get_pid (thread->ptid) == pid
2621 && !is_exited (thread->ptid)
2622 && !is_executing (thread->ptid)
2623 && !thread->stop_requested
2624 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2626 switch_to_thread (thread->ptid);
2627 clear_proceed_status (0);
2628 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2635 proceed_after_attach (int pid)
2637 /* Don't error out if the current thread is running, because
2638 there may be other stopped threads. */
2640 /* Backup current thread and selected frame. */
2641 scoped_restore_current_thread restore_thread;
2643 iterate_over_threads (proceed_after_attach_callback, &pid);
2646 /* See inferior.h. */
2649 setup_inferior (int from_tty)
2651 struct inferior *inferior;
2653 inferior = current_inferior ();
2654 inferior->needs_setup = 0;
2656 /* If no exec file is yet known, try to determine it from the
2658 if (get_exec_file (0) == NULL)
2659 exec_file_locate_attach (ptid_get_pid (inferior_ptid), 1, from_tty);
2662 reopen_exec_file ();
2666 /* Take any necessary post-attaching actions for this platform. */
2667 target_post_attach (ptid_get_pid (inferior_ptid));
2669 post_create_inferior (¤t_target, from_tty);
2672 /* What to do after the first program stops after attaching. */
2673 enum attach_post_wait_mode
2675 /* Do nothing. Leaves threads as they are. */
2676 ATTACH_POST_WAIT_NOTHING,
2678 /* Re-resume threads that are marked running. */
2679 ATTACH_POST_WAIT_RESUME,
2681 /* Stop all threads. */
2682 ATTACH_POST_WAIT_STOP,
2685 /* Called after we've attached to a process and we've seen it stop for
2686 the first time. If ASYNC_EXEC is true, re-resume threads that
2687 should be running. Else if ATTACH, */
2690 attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mode)
2692 struct inferior *inferior;
2694 inferior = current_inferior ();
2695 inferior->control.stop_soon = NO_STOP_QUIETLY;
2697 if (inferior->needs_setup)
2698 setup_inferior (from_tty);
2700 if (mode == ATTACH_POST_WAIT_RESUME)
2702 /* The user requested an `attach&', so be sure to leave threads
2703 that didn't get a signal running. */
2705 /* Immediatelly resume all suspended threads of this inferior,
2706 and this inferior only. This should have no effect on
2707 already running threads. If a thread has been stopped with a
2708 signal, leave it be. */
2710 proceed_after_attach (inferior->pid);
2713 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
2715 clear_proceed_status (0);
2716 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2720 else if (mode == ATTACH_POST_WAIT_STOP)
2722 /* The user requested a plain `attach', so be sure to leave
2723 the inferior stopped. */
2725 /* At least the current thread is already stopped. */
2727 /* In all-stop, by definition, all threads have to be already
2728 stopped at this point. In non-stop, however, although the
2729 selected thread is stopped, others may still be executing.
2730 Be sure to explicitly stop all threads of the process. This
2731 should have no effect on already stopped threads. */
2733 target_stop (pid_to_ptid (inferior->pid));
2734 else if (target_is_non_stop_p ())
2736 struct thread_info *thread;
2737 struct thread_info *lowest = inferior_thread ();
2738 int pid = current_inferior ()->pid;
2740 stop_all_threads ();
2742 /* It's not defined which thread will report the attach
2743 stop. For consistency, always select the thread with
2744 lowest GDB number, which should be the main thread, if it
2746 ALL_NON_EXITED_THREADS (thread)
2748 if (ptid_get_pid (thread->ptid) == pid)
2750 if (thread->inf->num < lowest->inf->num
2751 || thread->per_inf_num < lowest->per_inf_num)
2756 switch_to_thread (lowest->ptid);
2759 /* Tell the user/frontend where we're stopped. */
2761 if (deprecated_attach_hook)
2762 deprecated_attach_hook ();
2766 struct attach_command_continuation_args
2770 enum attach_post_wait_mode mode;
2774 attach_command_continuation (void *args, int err)
2776 struct attach_command_continuation_args *a
2777 = (struct attach_command_continuation_args *) args;
2782 attach_post_wait (a->args, a->from_tty, a->mode);
2786 attach_command_continuation_free_args (void *args)
2788 struct attach_command_continuation_args *a
2789 = (struct attach_command_continuation_args *) args;
2795 /* "attach" command entry point. Takes a program started up outside
2796 of gdb and ``attaches'' to it. This stops it cold in its tracks
2797 and allows us to start debugging it. */
2800 attach_command (const char *args, int from_tty)
2803 struct target_ops *attach_target;
2804 struct inferior *inferior = current_inferior ();
2805 enum attach_post_wait_mode mode;
2807 dont_repeat (); /* Not for the faint of heart */
2809 if (gdbarch_has_global_solist (target_gdbarch ()))
2810 /* Don't complain if all processes share the same symbol
2813 else if (target_has_execution)
2815 if (query (_("A program is being debugged already. Kill it? ")))
2818 error (_("Not killed."));
2821 /* Clean up any leftovers from other runs. Some other things from
2822 this function should probably be moved into target_pre_inferior. */
2823 target_pre_inferior (from_tty);
2825 gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec);
2826 args = stripped.get ();
2828 attach_target = find_attach_target ();
2830 prepare_execution_command (attach_target, async_exec);
2832 if (non_stop && !attach_target->to_supports_non_stop (attach_target))
2833 error (_("Cannot attach to this target in non-stop mode"));
2835 attach_target->to_attach (attach_target, args, from_tty);
2836 /* to_attach should push the target, so after this point we
2837 shouldn't refer to attach_target again. */
2838 attach_target = NULL;
2840 /* Set up the "saved terminal modes" of the inferior
2841 based on what modes we are starting it with. */
2842 target_terminal::init ();
2844 /* Install inferior's terminal modes. This may look like a no-op,
2845 as we've just saved them above, however, this does more than
2846 restore terminal settings:
2848 - installs a SIGINT handler that forwards SIGINT to the inferior.
2849 Otherwise a Ctrl-C pressed just while waiting for the initial
2850 stop would end up as a spurious Quit.
2852 - removes stdin from the event loop, which we need if attaching
2853 in the foreground, otherwise on targets that report an initial
2854 stop on attach (which are most) we'd process input/commands
2855 while we're in the event loop waiting for that stop. That is,
2856 before the attach continuation runs and the command is really
2858 target_terminal::inferior ();
2860 /* Set up execution context to know that we should return from
2861 wait_for_inferior as soon as the target reports a stop. */
2862 init_wait_for_inferior ();
2863 clear_proceed_status (0);
2865 inferior->needs_setup = 1;
2867 if (target_is_non_stop_p ())
2869 /* If we find that the current thread isn't stopped, explicitly
2870 do so now, because we're going to install breakpoints and
2874 /* The user requested an `attach&'; stop just one thread. */
2875 target_stop (inferior_ptid);
2877 /* The user requested an `attach', so stop all threads of this
2879 target_stop (pid_to_ptid (ptid_get_pid (inferior_ptid)));
2882 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP;
2884 /* Some system don't generate traps when attaching to inferior.
2885 E.g. Mach 3 or GNU hurd. */
2886 if (!target_attach_no_wait)
2888 struct attach_command_continuation_args *a;
2890 /* Careful here. See comments in inferior.h. Basically some
2891 OSes don't ignore SIGSTOPs on continue requests anymore. We
2892 need a way for handle_inferior_event to reset the stop_signal
2893 variable after an attach, and this is what
2894 STOP_QUIETLY_NO_SIGSTOP is for. */
2895 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2897 /* Wait for stop. */
2898 a = XNEW (struct attach_command_continuation_args);
2899 a->args = xstrdup (args);
2900 a->from_tty = from_tty;
2902 add_inferior_continuation (attach_command_continuation, a,
2903 attach_command_continuation_free_args);
2905 if (!target_is_async_p ())
2906 mark_infrun_async_event_handler ();
2910 attach_post_wait (args, from_tty, mode);
2913 /* We had just found out that the target was already attached to an
2914 inferior. PTID points at a thread of this new inferior, that is
2915 the most likely to be stopped right now, but not necessarily so.
2916 The new inferior is assumed to be already added to the inferior
2917 list at this point. If LEAVE_RUNNING, then leave the threads of
2918 this inferior running, except those we've explicitly seen reported
2922 notice_new_inferior (ptid_t ptid, int leave_running, int from_tty)
2924 enum attach_post_wait_mode mode
2925 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2927 gdb::optional<scoped_restore_current_thread> restore_thread;
2929 if (inferior_ptid != null_ptid)
2930 restore_thread.emplace ();
2932 /* Avoid reading registers -- we haven't fetched the target
2934 switch_to_thread_no_regs (find_thread_ptid (ptid));
2936 /* When we "notice" a new inferior we need to do all the things we
2937 would normally do if we had just attached to it. */
2939 if (is_executing (inferior_ptid))
2941 struct attach_command_continuation_args *a;
2942 struct inferior *inferior = current_inferior ();
2944 /* We're going to install breakpoints, and poke at memory,
2945 ensure that the inferior is stopped for a moment while we do
2947 target_stop (inferior_ptid);
2949 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2951 /* Wait for stop before proceeding. */
2952 a = XNEW (struct attach_command_continuation_args);
2953 a->args = xstrdup ("");
2954 a->from_tty = from_tty;
2956 add_inferior_continuation (attach_command_continuation, a,
2957 attach_command_continuation_free_args);
2962 attach_post_wait ("" /* args */, from_tty, mode);
2967 * takes a program previously attached to and detaches it.
2968 * The program resumes execution and will no longer stop
2969 * on signals, etc. We better not have left any breakpoints
2970 * in the program or it'll die when it hits one. For this
2971 * to work, it may be necessary for the process to have been
2972 * previously attached. It *might* work if the program was
2973 * started via the normal ptrace (PTRACE_TRACEME).
2977 detach_command (const char *args, int from_tty)
2979 dont_repeat (); /* Not for the faint of heart. */
2981 if (ptid_equal (inferior_ptid, null_ptid))
2982 error (_("The program is not being run."));
2984 query_if_trace_running (from_tty);
2986 disconnect_tracing ();
2988 target_detach (args, from_tty);
2990 /* The current inferior process was just detached successfully. Get
2991 rid of breakpoints that no longer make sense. Note we don't do
2992 this within target_detach because that is also used when
2993 following child forks, and in that case we will want to transfer
2994 breakpoints to the child, not delete them. */
2995 breakpoint_init_inferior (inf_exited);
2997 /* If the solist is global across inferiors, don't clear it when we
2998 detach from a single inferior. */
2999 if (!gdbarch_has_global_solist (target_gdbarch ()))
3000 no_shared_libraries (NULL, from_tty);
3002 /* If we still have inferiors to debug, then don't mess with their
3004 if (!have_inferiors ())
3005 init_thread_list ();
3007 if (deprecated_detach_hook)
3008 deprecated_detach_hook ();
3011 /* Disconnect from the current target without resuming it (leaving it
3012 waiting for a debugger).
3014 We'd better not have left any breakpoints in the program or the
3015 next debugger will get confused. Currently only supported for some
3016 remote targets, since the normal attach mechanisms don't work on
3017 stopped processes on some native platforms (e.g. GNU/Linux). */
3020 disconnect_command (const char *args, int from_tty)
3022 dont_repeat (); /* Not for the faint of heart. */
3023 query_if_trace_running (from_tty);
3024 disconnect_tracing ();
3025 target_disconnect (args, from_tty);
3026 no_shared_libraries (NULL, from_tty);
3027 init_thread_list ();
3028 if (deprecated_detach_hook)
3029 deprecated_detach_hook ();
3033 interrupt_target_1 (int all_threads)
3038 ptid = minus_one_ptid;
3040 ptid = inferior_ptid;
3045 target_interrupt (ptid);
3047 /* Tag the thread as having been explicitly requested to stop, so
3048 other parts of gdb know not to resume this thread automatically,
3049 if it was stopped due to an internal event. Limit this to
3050 non-stop mode, as when debugging a multi-threaded application in
3051 all-stop mode, we will only get one stop event --- it's undefined
3052 which thread will report the event. */
3054 set_stop_requested (ptid, 1);
3058 Stop the execution of the target while running in async mode, in
3059 the background. In all-stop, stop the whole process. In non-stop
3060 mode, stop the current thread only by default, or stop all threads
3061 if the `-a' switch is used. */
3064 interrupt_command (const char *args, int from_tty)
3066 if (target_can_async_p ())
3068 int all_threads = 0;
3070 dont_repeat (); /* Not for the faint of heart. */
3073 && startswith (args, "-a"))
3076 if (!non_stop && all_threads)
3077 error (_("-a is meaningless in all-stop mode."));
3079 interrupt_target_1 (all_threads);
3083 /* See inferior.h. */
3086 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3087 struct frame_info *frame, const char *args)
3090 int printed_something = 0;
3093 regnum < gdbarch_num_regs (gdbarch)
3094 + gdbarch_num_pseudo_regs (gdbarch);
3097 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
3099 printed_something = 1;
3100 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
3103 if (!printed_something)
3104 fprintf_filtered (file, "No floating-point info "
3105 "available for this processor.\n");
3109 info_float_command (char *args, int from_tty)
3111 struct frame_info *frame;
3113 if (!target_has_registers)
3114 error (_("The program has no registers now."));
3116 frame = get_selected_frame (NULL);
3117 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
3121 unset_command (const char *args, int from_tty)
3123 printf_filtered (_("\"unset\" must be followed by the "
3124 "name of an unset subcommand.\n"));
3125 help_list (unsetlist, "unset ", all_commands, gdb_stdout);
3128 /* Implement `info proc' family of commands. */
3131 info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty)
3133 struct gdbarch *gdbarch = get_current_arch ();
3135 if (!target_info_proc (args, what))
3137 if (gdbarch_info_proc_p (gdbarch))
3138 gdbarch_info_proc (gdbarch, args, what);
3140 error (_("Not supported on this target."));
3144 /* Implement `info proc' when given without any futher parameters. */
3147 info_proc_cmd (const char *args, int from_tty)
3149 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
3152 /* Implement `info proc mappings'. */
3155 info_proc_cmd_mappings (const char *args, int from_tty)
3157 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
3160 /* Implement `info proc stat'. */
3163 info_proc_cmd_stat (const char *args, int from_tty)
3165 info_proc_cmd_1 (args, IP_STAT, from_tty);
3168 /* Implement `info proc status'. */
3171 info_proc_cmd_status (const char *args, int from_tty)
3173 info_proc_cmd_1 (args, IP_STATUS, from_tty);
3176 /* Implement `info proc cwd'. */
3179 info_proc_cmd_cwd (const char *args, int from_tty)
3181 info_proc_cmd_1 (args, IP_CWD, from_tty);
3184 /* Implement `info proc cmdline'. */
3187 info_proc_cmd_cmdline (const char *args, int from_tty)
3189 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
3192 /* Implement `info proc exe'. */
3195 info_proc_cmd_exe (const char *args, int from_tty)
3197 info_proc_cmd_1 (args, IP_EXE, from_tty);
3200 /* Implement `info proc all'. */
3203 info_proc_cmd_all (const char *args, int from_tty)
3205 info_proc_cmd_1 (args, IP_ALL, from_tty);
3208 /* This help string is used for the run, start, and starti commands.
3209 It is defined as a macro to prevent duplication. */
3211 #define RUN_ARGS_HELP \
3212 "You may specify arguments to give it.\n\
3213 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3214 shell that will start the program (specified by the \"$SHELL\" environment\n\
3215 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3216 are also allowed.\n\
3218 With no arguments, uses arguments last specified (with \"run\" or \n\
3219 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3220 use \"set args\" without arguments.\n\
3222 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3225 _initialize_infcmd (void)
3227 static struct cmd_list_element *info_proc_cmdlist;
3228 struct cmd_list_element *c = NULL;
3229 const char *cmd_name;
3231 /* Add the filename of the terminal connected to inferior I/O. */
3232 add_setshow_optional_filename_cmd ("inferior-tty", class_run,
3233 &inferior_io_terminal_scratch, _("\
3234 Set terminal for future runs of program being debugged."), _("\
3235 Show terminal for future runs of program being debugged."), _("\
3236 Usage: set inferior-tty [TTY]\n\n\
3237 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3239 set_inferior_tty_command,
3240 show_inferior_tty_command,
3241 &setlist, &showlist);
3242 cmd_name = "inferior-tty";
3243 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3244 gdb_assert (c != NULL);
3245 add_alias_cmd ("tty", c, class_alias, 0, &cmdlist);
3248 add_setshow_string_noescape_cmd (cmd_name, class_run,
3249 &inferior_args_scratch, _("\
3250 Set argument list to give program being debugged when it is started."), _("\
3251 Show argument list to give program being debugged when it is started."), _("\
3252 Follow this command with any number of args, to be passed to the program."),
3255 &setlist, &showlist);
3256 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3257 gdb_assert (c != NULL);
3258 set_cmd_completer (c, filename_completer);
3261 add_setshow_string_noescape_cmd (cmd_name, class_run,
3262 &inferior_cwd_scratch, _("\
3263 Set the current working directory to be used when the inferior is started.\n\
3264 Changing this setting does not have any effect on inferiors that are\n\
3267 Show the current working directory that is used when the inferior is started."),
3269 Use this command to change the current working directory that will be used\n\
3270 when the inferior is started. This setting does not affect GDB's current\n\
3271 working directory."),
3274 &setlist, &showlist);
3275 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3276 gdb_assert (c != NULL);
3277 set_cmd_completer (c, filename_completer);
3279 c = add_cmd ("environment", no_class, environment_info, _("\
3280 The environment to give the program, or one variable's value.\n\
3281 With an argument VAR, prints the value of environment variable VAR to\n\
3282 give the program being debugged. With no arguments, prints the entire\n\
3283 environment to be given to the program."), &showlist);
3284 set_cmd_completer (c, noop_completer);
3286 add_prefix_cmd ("unset", no_class, unset_command,
3287 _("Complement to certain \"set\" commands."),
3288 &unsetlist, "unset ", 0, &cmdlist);
3290 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3291 Cancel environment variable VAR for the program.\n\
3292 This does not affect the program until the next \"run\" command."),
3294 set_cmd_completer (c, noop_completer);
3296 c = add_cmd ("environment", class_run, set_environment_command, _("\
3297 Set environment variable value to give the program.\n\
3298 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3299 VALUES of environment variables are uninterpreted strings.\n\
3300 This does not affect the program until the next \"run\" command."),
3302 set_cmd_completer (c, noop_completer);
3304 c = add_com ("path", class_files, path_command, _("\
3305 Add directory DIR(s) to beginning of search path for object files.\n\
3306 $cwd in the path means the current working directory.\n\
3307 This path is equivalent to the $PATH shell variable. It is a list of\n\
3308 directories, separated by colons. These directories are searched to find\n\
3309 fully linked executable files and separately compiled object files as \
3311 set_cmd_completer (c, filename_completer);
3313 c = add_cmd ("paths", no_class, path_info, _("\
3314 Current search path for finding object files.\n\
3315 $cwd in the path means the current working directory.\n\
3316 This path is equivalent to the $PATH shell variable. It is a list of\n\
3317 directories, separated by colons. These directories are searched to find\n\
3318 fully linked executable files and separately compiled object files as \
3321 set_cmd_completer (c, noop_completer);
3323 add_prefix_cmd ("kill", class_run, kill_command,
3324 _("Kill execution of program being debugged."),
3325 &killlist, "kill ", 0, &cmdlist);
3327 add_com ("attach", class_run, attach_command, _("\
3328 Attach to a process or file outside of GDB.\n\
3329 This command attaches to another target, of the same type as your last\n\
3330 \"target\" command (\"info files\" will show your target stack).\n\
3331 The command may take as argument a process id or a device file.\n\
3332 For a process id, you must have permission to send the process a signal,\n\
3333 and it must have the same effective uid as the debugger.\n\
3334 When using \"attach\" with a process id, the debugger finds the\n\
3335 program running in the process, looking first in the current working\n\
3336 directory, or (if not found there) using the source file search path\n\
3337 (see the \"directory\" command). You can also use the \"file\" command\n\
3338 to specify the program, and to load its symbol table."));
3340 add_prefix_cmd ("detach", class_run, detach_command, _("\
3341 Detach a process or file previously attached.\n\
3342 If a process, it is no longer traced, and it continues its execution. If\n\
3343 you were debugging a file, the file is closed and gdb no longer accesses it."),
3344 &detachlist, "detach ", 0, &cmdlist);
3346 add_com ("disconnect", class_run, disconnect_command, _("\
3347 Disconnect from a target.\n\
3348 The target will wait for another debugger to connect. Not available for\n\
3351 c = add_com ("signal", class_run, signal_command, _("\
3352 Continue program with the specified signal.\n\
3353 Usage: signal SIGNAL\n\
3354 The SIGNAL argument is processed the same as the handle command.\n\
3356 An argument of \"0\" means continue the program without sending it a signal.\n\
3357 This is useful in cases where the program stopped because of a signal,\n\
3358 and you want to resume the program while discarding the signal.\n\
3360 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3361 the current thread only."));
3362 set_cmd_completer (c, signal_completer);
3364 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3365 Queue a signal to be delivered to the current thread when it is resumed.\n\
3366 Usage: queue-signal SIGNAL\n\
3367 The SIGNAL argument is processed the same as the handle command.\n\
3368 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3370 An argument of \"0\" means remove any currently queued signal from\n\
3371 the current thread. This is useful in cases where the program stopped\n\
3372 because of a signal, and you want to resume it while discarding the signal.\n\
3374 In a multi-threaded program the signal is queued with, or discarded from,\n\
3375 the current thread only."));
3376 set_cmd_completer (c, signal_completer);
3378 add_com ("stepi", class_run, stepi_command, _("\
3379 Step one instruction exactly.\n\
3381 Argument N means step N times (or till program stops for another \
3383 add_com_alias ("si", "stepi", class_alias, 0);
3385 add_com ("nexti", class_run, nexti_command, _("\
3386 Step one instruction, but proceed through subroutine calls.\n\
3388 Argument N means step N times (or till program stops for another \
3390 add_com_alias ("ni", "nexti", class_alias, 0);
3392 add_com ("finish", class_run, finish_command, _("\
3393 Execute until selected stack frame returns.\n\
3395 Upon return, the value returned is printed and put in the value history."));
3396 add_com_alias ("fin", "finish", class_run, 1);
3398 add_com ("next", class_run, next_command, _("\
3399 Step program, proceeding through subroutine calls.\n\
3401 Unlike \"step\", if the current source line calls a subroutine,\n\
3402 this command does not enter the subroutine, but instead steps over\n\
3403 the call, in effect treating it as a single source line."));
3404 add_com_alias ("n", "next", class_run, 1);
3406 add_com ("step", class_run, step_command, _("\
3407 Step program until it reaches a different source line.\n\
3409 Argument N means step N times (or till program stops for another \
3411 add_com_alias ("s", "step", class_run, 1);
3413 c = add_com ("until", class_run, until_command, _("\
3414 Execute until the program reaches a source line greater than the current\n\
3415 or a specified location (same args as break command) within the current \
3417 set_cmd_completer (c, location_completer);
3418 add_com_alias ("u", "until", class_run, 1);
3420 c = add_com ("advance", class_run, advance_command, _("\
3421 Continue the program up to the given location (same form as args for break \
3423 Execution will also stop upon exit from the current stack frame."));
3424 set_cmd_completer (c, location_completer);
3426 c = add_com ("jump", class_run, jump_command, _("\
3427 Continue program being debugged at specified line or address.\n\
3428 Usage: jump <location>\n\
3429 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3430 for an address to start at."));
3431 set_cmd_completer (c, location_completer);
3432 add_com_alias ("j", "jump", class_run, 1);
3434 add_com ("continue", class_run, continue_command, _("\
3435 Continue program being debugged, after signal or breakpoint.\n\
3436 Usage: continue [N]\n\
3437 If proceeding from breakpoint, a number N may be used as an argument,\n\
3438 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3439 the breakpoint won't break until the Nth time it is reached).\n\
3441 If non-stop mode is enabled, continue only the current thread,\n\
3442 otherwise all the threads in the program are continued. To \n\
3443 continue all stopped threads in non-stop mode, use the -a option.\n\
3444 Specifying -a and an ignore count simultaneously is an error."));
3445 add_com_alias ("c", "cont", class_run, 1);
3446 add_com_alias ("fg", "cont", class_run, 1);
3448 c = add_com ("run", class_run, run_command, _("\
3449 Start debugged program.\n"
3451 set_cmd_completer (c, filename_completer);
3452 add_com_alias ("r", "run", class_run, 1);
3454 c = add_com ("start", class_run, start_command, _("\
3455 Start the debugged program stopping at the beginning of the main procedure.\n"
3457 set_cmd_completer (c, filename_completer);
3459 c = add_com ("starti", class_run, starti_command, _("\
3460 Start the debugged program stopping at the first instruction.\n"
3462 set_cmd_completer (c, filename_completer);
3464 add_com ("interrupt", class_run, interrupt_command,
3465 _("Interrupt the execution of the debugged program.\n\
3466 If non-stop mode is enabled, interrupt only the current thread,\n\
3467 otherwise all the threads in the program are stopped. To \n\
3468 interrupt all running threads in non-stop mode, use the -a option."));
3470 c = add_info ("registers", info_registers_command, _("\
3471 List of integer registers and their contents, for selected stack frame.\n\
3472 Register name as argument means describe only that register."));
3473 add_info_alias ("r", "registers", 1);
3474 set_cmd_completer (c, reg_or_group_completer);
3476 c = add_info ("all-registers", info_all_registers_command, _("\
3477 List of all registers and their contents, for selected stack frame.\n\
3478 Register name as argument means describe only that register."));
3479 set_cmd_completer (c, reg_or_group_completer);
3481 add_info ("program", info_program_command,
3482 _("Execution status of the program."));
3484 add_info ("float", info_float_command,
3485 _("Print the status of the floating point unit\n"));
3487 add_info ("vector", info_vector_command,
3488 _("Print the status of the vector unit\n"));
3490 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3492 Show /proc process information about any running process.\n\
3493 Specify any process id, or use the program being debugged by default."),
3494 &info_proc_cmdlist, "info proc ",
3495 1/*allow-unknown*/, &infolist);
3497 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3498 List of mapped memory regions."),
3499 &info_proc_cmdlist);
3501 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3502 List process info from /proc/PID/stat."),
3503 &info_proc_cmdlist);
3505 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3506 List process info from /proc/PID/status."),
3507 &info_proc_cmdlist);
3509 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3510 List current working directory of the process."),
3511 &info_proc_cmdlist);
3513 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3514 List command line arguments of the process."),
3515 &info_proc_cmdlist);
3517 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3518 List absolute filename for executable of the process."),
3519 &info_proc_cmdlist);
3521 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3522 List all available /proc info."),
3523 &info_proc_cmdlist);