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 until_command (char *, int);
71 static void path_info (char *, int);
73 static void path_command (char *, int);
75 static void unset_command (char *, int);
77 static void info_float_command (char *, int);
79 static void disconnect_command (char *, int);
81 static void unset_environment_command (char *, int);
83 static void set_environment_command (char *, int);
85 static void environment_info (char *, int);
87 static void info_program_command (char *, int);
89 static void finish_command (char *, int);
91 static void signal_command (char *, int);
93 static void jump_command (char *, int);
95 static void step_1 (int, int, char *);
97 static void next_command (char *, int);
99 static void step_command (char *, int);
101 static void run_command (char *, int);
103 #define ERROR_NO_INFERIOR \
104 if (!target_has_execution) error (_("The program is not being run."));
106 /* Scratch area where string containing arguments to give to the
107 program will be stored by 'set args'. As soon as anything is
108 stored, notice_args_set will move it into per-inferior storage.
109 Arguments are separated by spaces. Empty string (pointer to '\0')
112 static char *inferior_args_scratch;
114 /* Scratch area where 'set inferior-tty' will store user-provided value.
115 We'll immediate copy it into per-inferior storage. */
117 static char *inferior_io_terminal_scratch;
119 /* Pid of our debugged inferior, or 0 if no inferior now.
120 Since various parts of infrun.c test this to see whether there is a program
121 being debugged it should be nonzero (currently 3 is used) for remote
124 ptid_t inferior_ptid;
126 /* Address at which inferior stopped. */
130 /* Nonzero if stopped due to completion of a stack dummy routine. */
132 enum stop_stack_kind stop_stack_dummy;
134 /* Nonzero if stopped due to a random (unexpected) signal in inferior
137 int stopped_by_random_signal;
139 /* See inferior.h. */
141 int startup_with_shell = 1;
144 /* Accessor routines. */
146 /* Set the io terminal for the current inferior. Ownership of
147 TERMINAL_NAME is not transferred. */
150 set_inferior_io_terminal (const char *terminal_name)
152 xfree (current_inferior ()->terminal);
154 if (terminal_name != NULL && *terminal_name != '\0')
155 current_inferior ()->terminal = xstrdup (terminal_name);
157 current_inferior ()->terminal = NULL;
161 get_inferior_io_terminal (void)
163 return current_inferior ()->terminal;
167 set_inferior_tty_command (char *args, int from_tty,
168 struct cmd_list_element *c)
170 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
171 Now route it to current inferior. */
172 set_inferior_io_terminal (inferior_io_terminal_scratch);
176 show_inferior_tty_command (struct ui_file *file, int from_tty,
177 struct cmd_list_element *c, const char *value)
179 /* Note that we ignore the passed-in value in favor of computing it
181 const char *inferior_io_terminal = get_inferior_io_terminal ();
183 if (inferior_io_terminal == NULL)
184 inferior_io_terminal = "";
185 fprintf_filtered (gdb_stdout,
186 _("Terminal for future runs of program being debugged "
187 "is \"%s\".\n"), inferior_io_terminal);
191 get_inferior_args (void)
193 if (current_inferior ()->argc != 0)
197 n = construct_inferior_arguments (current_inferior ()->argc,
198 current_inferior ()->argv);
199 set_inferior_args (n);
203 if (current_inferior ()->args == NULL)
204 current_inferior ()->args = xstrdup ("");
206 return current_inferior ()->args;
209 /* Set the arguments for the current inferior. Ownership of
210 NEWARGS is not transferred. */
213 set_inferior_args (char *newargs)
215 xfree (current_inferior ()->args);
216 current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
217 current_inferior ()->argc = 0;
218 current_inferior ()->argv = 0;
222 set_inferior_args_vector (int argc, char **argv)
224 current_inferior ()->argc = argc;
225 current_inferior ()->argv = argv;
228 /* Notice when `set args' is run. */
231 set_args_command (char *args, int from_tty, struct cmd_list_element *c)
233 /* CLI has assigned the user-provided value to inferior_args_scratch.
234 Now route it to current inferior. */
235 set_inferior_args (inferior_args_scratch);
238 /* Notice when `show args' is run. */
241 show_args_command (struct ui_file *file, int from_tty,
242 struct cmd_list_element *c, const char *value)
244 /* Note that we ignore the passed-in value in favor of computing it
246 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
250 /* Compute command-line string given argument vector. This does the
251 same shell processing as fork_inferior. */
254 construct_inferior_arguments (int argc, char **argv)
258 if (startup_with_shell)
261 /* This holds all the characters considered special to the
263 static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
264 static const char quote = '"';
266 /* This holds all the characters considered special to the
267 typical Unix shells. We include `^' because the SunOS
268 /bin/sh treats it as a synonym for `|'. */
269 static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
270 static const char quote = '\'';
276 /* We over-compute the size. It shouldn't matter. */
277 for (i = 0; i < argc; ++i)
278 length += 3 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
280 result = (char *) xmalloc (length);
283 for (i = 0; i < argc; ++i)
288 /* Need to handle empty arguments specially. */
289 if (argv[i][0] == '\0')
299 if (strpbrk (argv[i], special))
305 for (cp = argv[i]; *cp; ++cp)
309 /* A newline cannot be quoted with a backslash (it
310 just disappears), only by putting it inside
321 if (strchr (special, *cp) != NULL)
337 /* In this case we can't handle arguments that contain spaces,
338 tabs, or newlines -- see breakup_args(). */
342 for (i = 0; i < argc; ++i)
344 char *cp = strchr (argv[i], ' ');
346 cp = strchr (argv[i], '\t');
348 cp = strchr (argv[i], '\n');
350 error (_("can't handle command-line "
351 "argument containing whitespace"));
352 length += strlen (argv[i]) + 1;
355 result = (char *) xmalloc (length);
357 for (i = 0; i < argc; ++i)
360 strcat (result, " ");
361 strcat (result, argv[i]);
369 /* This function strips the '&' character (indicating background
370 execution) that is added as *the last* of the arguments ARGS of a
371 command. A copy of the incoming ARGS without the '&' is returned,
372 unless the resulting string after stripping is empty, in which case
373 NULL is returned. *BG_CHAR_P is an output boolean that indicates
374 whether the '&' character was found. */
377 strip_bg_char (const char *args, int *bg_char_p)
381 if (args == NULL || *args == '\0')
387 p = args + strlen (args);
391 while (p > args && isspace (p[-1]))
396 return savestring (args, p - args);
402 return xstrdup (args);
405 /* Common actions to take after creating any sort of inferior, by any
406 means (running, attaching, connecting, et cetera). The target
407 should be stopped. */
410 post_create_inferior (struct target_ops *target, int from_tty)
413 /* Be sure we own the terminal in case write operations are performed. */
414 target_terminal::ours_for_output ();
416 /* If the target hasn't taken care of this already, do it now.
417 Targets which need to access registers during to_open,
418 to_create_inferior, or to_attach should do it earlier; but many
420 target_find_description ();
422 /* Now that we know the register layout, retrieve current PC. But
423 if the PC is unavailable (e.g., we're opening a core file with
424 missing registers info), ignore it. */
428 stop_pc = regcache_read_pc (get_current_regcache ());
430 CATCH (ex, RETURN_MASK_ERROR)
432 if (ex.error != NOT_AVAILABLE_ERROR)
433 throw_exception (ex);
439 const unsigned solib_add_generation
440 = current_program_space->solib_add_generation;
442 /* Create the hooks to handle shared library load and unload
444 solib_create_inferior_hook (from_tty);
446 if (current_program_space->solib_add_generation == solib_add_generation)
448 /* The platform-specific hook should load initial shared libraries,
449 but didn't. FROM_TTY will be incorrectly 0 but such solib
450 targets should be fixed anyway. Call it only after the solib
451 target has been initialized by solib_create_inferior_hook. */
454 warning (_("platform-specific solib_create_inferior_hook did "
455 "not load initial shared libraries."));
457 /* If the solist is global across processes, there's no need to
459 if (!gdbarch_has_global_solist (target_gdbarch ()))
460 solib_add (NULL, 0, auto_solib_add);
464 /* If the user sets watchpoints before execution having started,
465 then she gets software watchpoints, because GDB can't know which
466 target will end up being pushed, or if it supports hardware
467 watchpoints or not. breakpoint_re_set takes care of promoting
468 watchpoints to hardware watchpoints if possible, however, if this
469 new inferior doesn't load shared libraries or we don't pull in
470 symbols from any other source on this target/arch,
471 breakpoint_re_set is never called. Call it now so that software
472 watchpoints get a chance to be promoted to hardware watchpoints
473 if the now pushed target supports hardware watchpoints. */
474 breakpoint_re_set ();
476 observer_notify_inferior_created (target, from_tty);
479 /* Kill the inferior if already running. This function is designed
480 to be called when we are about to start the execution of the program
481 from the beginning. Ask the user to confirm that he wants to restart
482 the program being debugged when FROM_TTY is non-null. */
485 kill_if_already_running (int from_tty)
487 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
489 /* Bail out before killing the program if we will not be able to
491 target_require_runnable ();
494 && !query (_("The program being debugged has been started already.\n\
495 Start it from the beginning? ")))
496 error (_("Program not restarted."));
501 /* See inferior.h. */
504 prepare_execution_command (struct target_ops *target, int background)
506 /* If we get a request for running in the bg but the target
507 doesn't support it, error out. */
508 if (background && !target->to_can_async_p (target))
509 error (_("Asynchronous execution not supported on this target."));
513 /* If we get a request for running in the fg, then we need to
514 simulate synchronous (fg) execution. Note no cleanup is
515 necessary for this. stdin is re-enabled whenever an error
516 reaches the top level. */
517 all_uis_on_sync_execution_starting ();
521 /* Determine how the new inferior will behave. */
525 /* Run program without any explicit stop during startup. */
528 /* Stop at the beginning of the program's main function. */
531 /* Stop at the first instruction of the program. */
532 RUN_STOP_AT_FIRST_INSN
535 /* Implement the "run" command. Force a stop during program start if
536 requested by RUN_HOW. */
539 run_command_1 (char *args, int from_tty, enum run_how run_how)
541 const char *exec_file;
542 struct cleanup *old_chain;
544 struct ui_out *uiout = current_uiout;
545 struct target_ops *run_target;
547 struct cleanup *args_chain;
552 kill_if_already_running (from_tty);
554 init_wait_for_inferior ();
555 clear_breakpoint_hit_counts ();
557 /* Clean up any leftovers from other runs. Some other things from
558 this function should probably be moved into target_pre_inferior. */
559 target_pre_inferior (from_tty);
561 /* The comment here used to read, "The exec file is re-read every
562 time we do a generic_mourn_inferior, so we just have to worry
563 about the symbol file." The `generic_mourn_inferior' function
564 gets called whenever the program exits. However, suppose the
565 program exits, and *then* the executable file changes? We need
566 to check again here. Since reopen_exec_file doesn't do anything
567 if the timestamp hasn't changed, I don't see the harm. */
571 args = strip_bg_char (args, &async_exec);
572 args_chain = make_cleanup (xfree, args);
574 /* Do validation and preparation before possibly changing anything
577 run_target = find_run_target ();
579 prepare_execution_command (run_target, async_exec);
581 if (non_stop && !run_target->to_supports_non_stop (run_target))
582 error (_("The target does not support running in non-stop mode."));
584 /* Done. Can now set breakpoints, change inferior args, etc. */
586 /* Insert temporary breakpoint in main function if requested. */
587 if (run_how == RUN_STOP_AT_MAIN)
588 tbreak_command (main_name (), 0);
590 exec_file = get_exec_file (0);
592 /* We keep symbols from add-symbol-file, on the grounds that the
593 user might want to add some symbols before running the program
594 (right?). But sometimes (dynamic loading where the user manually
595 introduces the new symbols with add-symbol-file), the code which
596 the symbols describe does not persist between runs. Currently
597 the user has to manually nuke all symbols between runs if they
598 want them to go away (PR 2207). This is probably reasonable. */
600 /* If there were other args, beside '&', process them. */
602 set_inferior_args (args);
606 uiout->field_string (NULL, "Starting program");
609 uiout->field_string ("execfile", exec_file);
611 /* We call get_inferior_args() because we might need to compute
613 uiout->field_string ("infargs", get_inferior_args ());
618 /* Done with ARGS. */
619 do_cleanups (args_chain);
621 /* We call get_inferior_args() because we might need to compute
623 run_target->to_create_inferior (run_target, exec_file,
624 std::string (get_inferior_args ()),
625 current_inferior ()->environment.envp (),
627 /* to_create_inferior should push the target, so after this point we
628 shouldn't refer to run_target again. */
631 /* We're starting off a new process. When we get out of here, in
632 non-stop mode, finish the state of all threads of that process,
633 but leave other threads alone, as they may be stopped in internal
634 events --- the frontend shouldn't see them as stopped. In
635 all-stop, always finish the state of all threads, as we may be
636 resuming more than just the new process. */
638 ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
640 ptid = minus_one_ptid;
641 old_chain = make_cleanup (finish_thread_state_cleanup, &ptid);
643 /* Pass zero for FROM_TTY, because at this point the "run" command
644 has done its thing; now we are setting up the running program. */
645 post_create_inferior (¤t_target, 0);
647 /* Queue a pending event so that the program stops immediately. */
648 if (run_how == RUN_STOP_AT_FIRST_INSN)
650 thread_info *thr = inferior_thread ();
651 thr->suspend.waitstatus_pending_p = 1;
652 thr->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
653 thr->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
656 /* Start the target running. Do not use -1 continuation as it would skip
657 breakpoint right at the entry point. */
658 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0);
660 /* Since there was no error, there's no need to finish the thread
662 discard_cleanups (old_chain);
666 run_command (char *args, int from_tty)
668 run_command_1 (args, from_tty, RUN_NORMAL);
671 /* Start the execution of the program up until the beginning of the main
675 start_command (char *args, int from_tty)
677 /* Some languages such as Ada need to search inside the program
678 minimal symbols for the location where to put the temporary
679 breakpoint before starting. */
680 if (!have_minimal_symbols ())
681 error (_("No symbol table loaded. Use the \"file\" command."));
683 /* Run the program until reaching the main procedure... */
684 run_command_1 (args, from_tty, RUN_STOP_AT_MAIN);
687 /* Start the execution of the program stopping at the first
691 starti_command (char *args, int from_tty)
693 run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN);
697 proceed_thread_callback (struct thread_info *thread, void *arg)
699 /* We go through all threads individually instead of compressing
700 into a single target `resume_all' request, because some threads
701 may be stopped in internal breakpoints/events, or stopped waiting
702 for its turn in the displaced stepping queue (that is, they are
703 running && !executing). The target side has no idea about why
704 the thread is stopped, so a `resume_all' command would resume too
705 much. If/when GDB gains a way to tell the target `hold this
706 thread stopped until I say otherwise', then we can optimize
708 if (!is_stopped (thread->ptid))
711 switch_to_thread (thread->ptid);
712 clear_proceed_status (0);
713 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
718 ensure_valid_thread (void)
720 if (ptid_equal (inferior_ptid, null_ptid)
721 || is_exited (inferior_ptid))
722 error (_("Cannot execute this command without a live selected thread."));
725 /* If the user is looking at trace frames, any resumption of execution
726 is likely to mix up recorded and live target data. So simply
727 disallow those commands. */
730 ensure_not_tfind_mode (void)
732 if (get_traceframe_number () >= 0)
733 error (_("Cannot execute this command while looking at trace frames."));
736 /* Throw an error indicating the current thread is running. */
739 error_is_running (void)
741 error (_("Cannot execute this command while "
742 "the selected thread is running."));
745 /* Calls error_is_running if the current thread is running. */
748 ensure_not_running (void)
750 if (is_running (inferior_ptid))
755 continue_1 (int all_threads)
758 ensure_not_tfind_mode ();
760 if (non_stop && all_threads)
762 /* Don't error out if the current thread is running, because
763 there may be other stopped threads. */
765 /* Backup current thread and selected frame and restore on scope
767 scoped_restore_current_thread restore_thread;
769 iterate_over_threads (proceed_thread_callback, NULL);
771 if (current_ui->prompt_state == PROMPT_BLOCKED)
773 /* If all threads in the target were already running,
774 proceed_thread_callback ends up never calling proceed,
775 and so nothing calls this to put the inferior's terminal
776 settings in effect and remove stdin from the event loop,
777 which we must when running a foreground command. E.g.:
781 <all threads are running now>
784 <no thread was resumed, but the inferior now owns the terminal>
786 target_terminal::inferior ();
791 ensure_valid_thread ();
792 ensure_not_running ();
793 clear_proceed_status (0);
794 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
798 /* continue [-a] [proceed-count] [&] */
801 continue_command (char *args, int from_tty)
805 struct cleanup *args_chain;
809 /* Find out whether we must run in the background. */
810 args = strip_bg_char (args, &async_exec);
811 args_chain = make_cleanup (xfree, args);
815 if (startswith (args, "-a"))
818 args += sizeof ("-a") - 1;
824 if (!non_stop && all_threads)
825 error (_("`-a' is meaningless in all-stop mode."));
827 if (args != NULL && all_threads)
828 error (_("Can't resume all threads and specify "
829 "proceed count simultaneously."));
831 /* If we have an argument left, set proceed count of breakpoint we
838 struct thread_info *tp;
841 tp = find_thread_ptid (inferior_ptid);
845 struct target_waitstatus ws;
847 get_last_target_status (&last_ptid, &ws);
848 tp = find_thread_ptid (last_ptid);
851 bs = tp->control.stop_bpstat;
853 while ((stat = bpstat_num (&bs, &num)) != 0)
856 set_ignore_count (num,
857 parse_and_eval_long (args) - 1,
859 /* set_ignore_count prints a message ending with a period.
860 So print two spaces before "Continuing.". */
862 printf_filtered (" ");
866 if (!stopped && from_tty)
869 ("Not stopped at any breakpoint; argument ignored.\n");
873 /* Done with ARGS. */
874 do_cleanups (args_chain);
877 ensure_not_tfind_mode ();
879 if (!non_stop || !all_threads)
881 ensure_valid_thread ();
882 ensure_not_running ();
885 prepare_execution_command (¤t_target, async_exec);
888 printf_filtered (_("Continuing.\n"));
890 continue_1 (all_threads);
893 /* Record the starting point of a "step" or "next" command. */
896 set_step_frame (void)
898 frame_info *frame = get_current_frame ();
900 symtab_and_line sal = find_frame_sal (frame);
901 set_step_info (frame, sal);
903 CORE_ADDR pc = get_frame_pc (frame);
904 thread_info *tp = inferior_thread ();
905 tp->control.step_start_function = find_pc_function (pc);
908 /* Step until outside of current statement. */
911 step_command (char *count_string, int from_tty)
913 step_1 (0, 0, count_string);
916 /* Likewise, but skip over subroutine calls as if single instructions. */
919 next_command (char *count_string, int from_tty)
921 step_1 (1, 0, count_string);
924 /* Likewise, but step only one instruction. */
927 stepi_command (char *count_string, int from_tty)
929 step_1 (0, 1, count_string);
933 nexti_command (char *count_string, int from_tty)
935 step_1 (1, 1, count_string);
939 delete_longjmp_breakpoint_cleanup (void *arg)
941 int thread = * (int *) arg;
942 delete_longjmp_breakpoint (thread);
945 /* Data for the FSM that manages the step/next/stepi/nexti
948 struct step_command_fsm
950 /* The base class. */
951 struct thread_fsm thread_fsm;
953 /* How many steps left in a "step N"-like command. */
956 /* If true, this is a next/nexti, otherwise a step/stepi. */
957 int skip_subroutines;
959 /* If true, this is a stepi/nexti, otherwise a step/step. */
963 static void step_command_fsm_clean_up (struct thread_fsm *self,
964 struct thread_info *thread);
965 static int step_command_fsm_should_stop (struct thread_fsm *self,
966 struct thread_info *thread);
967 static enum async_reply_reason
968 step_command_fsm_async_reply_reason (struct thread_fsm *self);
970 /* step_command_fsm's vtable. */
972 static struct thread_fsm_ops step_command_fsm_ops =
975 step_command_fsm_clean_up,
976 step_command_fsm_should_stop,
977 NULL, /* return_value */
978 step_command_fsm_async_reply_reason,
981 /* Allocate a new step_command_fsm. */
983 static struct step_command_fsm *
984 new_step_command_fsm (struct interp *cmd_interp)
986 struct step_command_fsm *sm;
988 sm = XCNEW (struct step_command_fsm);
989 thread_fsm_ctor (&sm->thread_fsm, &step_command_fsm_ops, cmd_interp);
994 /* Prepare for a step/next/etc. command. Any target resource
995 allocated here is undone in the FSM's clean_up method. */
998 step_command_fsm_prepare (struct step_command_fsm *sm,
999 int skip_subroutines, int single_inst,
1000 int count, struct thread_info *thread)
1002 sm->skip_subroutines = skip_subroutines;
1003 sm->single_inst = single_inst;
1006 /* Leave the si command alone. */
1007 if (!sm->single_inst || sm->skip_subroutines)
1008 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ()));
1010 thread->control.stepping_command = 1;
1013 static int prepare_one_step (struct step_command_fsm *sm);
1016 step_1 (int skip_subroutines, int single_inst, char *count_string)
1020 struct cleanup *args_chain;
1021 struct thread_info *thr;
1022 struct step_command_fsm *step_sm;
1025 ensure_not_tfind_mode ();
1026 ensure_valid_thread ();
1027 ensure_not_running ();
1029 count_string = strip_bg_char (count_string, &async_exec);
1030 args_chain = make_cleanup (xfree, count_string);
1032 prepare_execution_command (¤t_target, async_exec);
1034 count = count_string ? parse_and_eval_long (count_string) : 1;
1036 /* Done with ARGS. */
1037 do_cleanups (args_chain);
1039 clear_proceed_status (1);
1041 /* Setup the execution command state machine to handle all the COUNT
1043 thr = inferior_thread ();
1044 step_sm = new_step_command_fsm (command_interp ());
1045 thr->thread_fsm = &step_sm->thread_fsm;
1047 step_command_fsm_prepare (step_sm, skip_subroutines,
1048 single_inst, count, thr);
1050 /* Do only one step for now, before returning control to the event
1051 loop. Let the continuation figure out how many other steps we
1052 need to do, and handle them one at the time, through
1054 if (!prepare_one_step (step_sm))
1055 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1060 /* Stepped into an inline frame. Pretend that we've
1062 thread_fsm_clean_up (thr->thread_fsm, thr);
1063 proceeded = normal_stop ();
1065 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
1066 all_uis_check_sync_execution_done ();
1070 /* Implementation of the 'should_stop' FSM method for stepping
1071 commands. Called after we are done with one step operation, to
1072 check whether we need to step again, before we print the prompt and
1073 return control to the user. If count is > 1, returns false, as we
1074 will need to keep going. */
1077 step_command_fsm_should_stop (struct thread_fsm *self, struct thread_info *tp)
1079 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1081 if (tp->control.stop_step)
1083 /* There are more steps to make, and we did stop due to
1084 ending a stepping range. Do another step. */
1085 if (--sm->count > 0)
1086 return prepare_one_step (sm);
1088 thread_fsm_set_finished (self);
1094 /* Implementation of the 'clean_up' FSM method for stepping commands. */
1097 step_command_fsm_clean_up (struct thread_fsm *self, struct thread_info *thread)
1099 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1101 if (!sm->single_inst || sm->skip_subroutines)
1102 delete_longjmp_breakpoint (thread->global_num);
1105 /* Implementation of the 'async_reply_reason' FSM method for stepping
1108 static enum async_reply_reason
1109 step_command_fsm_async_reply_reason (struct thread_fsm *self)
1111 return EXEC_ASYNC_END_STEPPING_RANGE;
1114 /* Prepare for one step in "step N". The actual target resumption is
1115 done by the caller. Return true if we're done and should thus
1116 report a stop to the user. Returns false if the target needs to be
1120 prepare_one_step (struct step_command_fsm *sm)
1124 struct frame_info *frame = get_current_frame ();
1126 /* Don't assume THREAD is a valid thread id. It is set to -1 if
1127 the longjmp breakpoint was not required. Use the
1128 INFERIOR_PTID thread instead, which is the same thread when
1130 struct thread_info *tp = inferior_thread ();
1134 if (!sm->single_inst)
1138 /* Step at an inlined function behaves like "down". */
1139 if (!sm->skip_subroutines
1140 && inline_skipped_frames (inferior_ptid))
1144 /* Pretend that we've ran. */
1145 resume_ptid = user_visible_resume_ptid (1);
1146 set_running (resume_ptid, 1);
1148 step_into_inline_frame (inferior_ptid);
1150 return prepare_one_step (sm);
1153 pc = get_frame_pc (frame);
1154 find_pc_line_pc_range (pc,
1155 &tp->control.step_range_start,
1156 &tp->control.step_range_end);
1158 tp->control.may_range_step = 1;
1160 /* If we have no line info, switch to stepi mode. */
1161 if (tp->control.step_range_end == 0 && step_stop_if_no_debug)
1163 tp->control.step_range_start = tp->control.step_range_end = 1;
1164 tp->control.may_range_step = 0;
1166 else if (tp->control.step_range_end == 0)
1170 if (find_pc_partial_function (pc, &name,
1171 &tp->control.step_range_start,
1172 &tp->control.step_range_end) == 0)
1173 error (_("Cannot find bounds of current function"));
1175 target_terminal::ours_for_output ();
1176 printf_filtered (_("Single stepping until exit from function %s,"
1177 "\nwhich has no line number information.\n"),
1183 /* Say we are stepping, but stop after one insn whatever it does. */
1184 tp->control.step_range_start = tp->control.step_range_end = 1;
1185 if (!sm->skip_subroutines)
1187 Don't step over function calls, not even to functions lacking
1189 tp->control.step_over_calls = STEP_OVER_NONE;
1192 if (sm->skip_subroutines)
1193 tp->control.step_over_calls = STEP_OVER_ALL;
1199 thread_fsm_set_finished (&sm->thread_fsm);
1204 /* Continue program at specified address. */
1207 jump_command (char *arg, int from_tty)
1209 struct gdbarch *gdbarch = get_current_arch ();
1214 struct cleanup *args_chain;
1217 ensure_not_tfind_mode ();
1218 ensure_valid_thread ();
1219 ensure_not_running ();
1221 /* Find out whether we must run in the background. */
1222 arg = strip_bg_char (arg, &async_exec);
1223 args_chain = make_cleanup (xfree, arg);
1225 prepare_execution_command (¤t_target, async_exec);
1228 error_no_arg (_("starting address"));
1230 std::vector<symtab_and_line> sals
1231 = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE);
1232 if (sals.size () != 1)
1233 error (_("Unreasonable jump request"));
1235 /* Done with ARGS. */
1236 do_cleanups (args_chain);
1238 symtab_and_line &sal = sals[0];
1240 if (sal.symtab == 0 && sal.pc == 0)
1241 error (_("No source file has been specified."));
1243 resolve_sal_pc (&sal); /* May error out. */
1245 /* See if we are trying to jump to another function. */
1246 fn = get_frame_function (get_current_frame ());
1247 sfn = find_pc_function (sal.pc);
1248 if (fn != NULL && sfn != fn)
1250 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1251 SYMBOL_PRINT_NAME (fn)))
1253 error (_("Not confirmed."));
1260 struct obj_section *section;
1262 fixup_symbol_section (sfn, 0);
1263 section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn);
1264 if (section_is_overlay (section)
1265 && !section_is_mapped (section))
1267 if (!query (_("WARNING!!! Destination is in "
1268 "unmapped overlay! Jump anyway? ")))
1270 error (_("Not confirmed."));
1280 printf_filtered (_("Continuing at "));
1281 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1282 printf_filtered (".\n");
1285 clear_proceed_status (0);
1286 proceed (addr, GDB_SIGNAL_0);
1289 /* Continue program giving it specified signal. */
1292 signal_command (char *signum_exp, int from_tty)
1294 enum gdb_signal oursig;
1296 struct cleanup *args_chain;
1298 dont_repeat (); /* Too dangerous. */
1300 ensure_not_tfind_mode ();
1301 ensure_valid_thread ();
1302 ensure_not_running ();
1304 /* Find out whether we must run in the background. */
1305 signum_exp = strip_bg_char (signum_exp, &async_exec);
1306 args_chain = make_cleanup (xfree, signum_exp);
1308 prepare_execution_command (¤t_target, async_exec);
1311 error_no_arg (_("signal number"));
1313 /* It would be even slicker to make signal names be valid expressions,
1314 (the type could be "enum $signal" or some such), then the user could
1315 assign them to convenience variables. */
1316 oursig = gdb_signal_from_name (signum_exp);
1318 if (oursig == GDB_SIGNAL_UNKNOWN)
1320 /* No, try numeric. */
1321 int num = parse_and_eval_long (signum_exp);
1324 oursig = GDB_SIGNAL_0;
1326 oursig = gdb_signal_from_command (num);
1329 do_cleanups (args_chain);
1331 /* Look for threads other than the current that this command ends up
1332 resuming too (due to schedlock off), and warn if they'll get a
1333 signal delivered. "signal 0" is used to suppress a previous
1334 signal, but if the current thread is no longer the one that got
1335 the signal, then the user is potentially suppressing the signal
1336 of the wrong thread. */
1339 struct thread_info *tp;
1341 int must_confirm = 0;
1343 /* This indicates what will be resumed. Either a single thread,
1344 a whole process, or all threads of all processes. */
1345 resume_ptid = user_visible_resume_ptid (0);
1347 ALL_NON_EXITED_THREADS (tp)
1349 if (ptid_equal (tp->ptid, inferior_ptid))
1351 if (!ptid_match (tp->ptid, resume_ptid))
1354 if (tp->suspend.stop_signal != GDB_SIGNAL_0
1355 && signal_pass_state (tp->suspend.stop_signal))
1358 printf_unfiltered (_("Note:\n"));
1359 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1360 print_thread_id (tp),
1361 gdb_signal_to_name (tp->suspend.stop_signal),
1362 gdb_signal_to_string (tp->suspend.stop_signal));
1368 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1369 "still deliver the signals noted above to their respective threads.\n"
1370 "Continue anyway? "),
1371 print_thread_id (inferior_thread ())))
1372 error (_("Not confirmed."));
1377 if (oursig == GDB_SIGNAL_0)
1378 printf_filtered (_("Continuing with no signal.\n"));
1380 printf_filtered (_("Continuing with signal %s.\n"),
1381 gdb_signal_to_name (oursig));
1384 clear_proceed_status (0);
1385 proceed ((CORE_ADDR) -1, oursig);
1388 /* Queue a signal to be delivered to the current thread. */
1391 queue_signal_command (char *signum_exp, int from_tty)
1393 enum gdb_signal oursig;
1394 struct thread_info *tp;
1397 ensure_not_tfind_mode ();
1398 ensure_valid_thread ();
1399 ensure_not_running ();
1401 if (signum_exp == NULL)
1402 error_no_arg (_("signal number"));
1404 /* It would be even slicker to make signal names be valid expressions,
1405 (the type could be "enum $signal" or some such), then the user could
1406 assign them to convenience variables. */
1407 oursig = gdb_signal_from_name (signum_exp);
1409 if (oursig == GDB_SIGNAL_UNKNOWN)
1411 /* No, try numeric. */
1412 int num = parse_and_eval_long (signum_exp);
1415 oursig = GDB_SIGNAL_0;
1417 oursig = gdb_signal_from_command (num);
1420 if (oursig != GDB_SIGNAL_0
1421 && !signal_pass_state (oursig))
1422 error (_("Signal handling set to not pass this signal to the program."));
1424 tp = inferior_thread ();
1425 tp->suspend.stop_signal = oursig;
1428 /* Data for the FSM that manages the until (with no argument)
1431 struct until_next_fsm
1433 /* The base class. */
1434 struct thread_fsm thread_fsm;
1436 /* The thread that as current when the command was executed. */
1440 static int until_next_fsm_should_stop (struct thread_fsm *self,
1441 struct thread_info *thread);
1442 static void until_next_fsm_clean_up (struct thread_fsm *self,
1443 struct thread_info *thread);
1444 static enum async_reply_reason
1445 until_next_fsm_async_reply_reason (struct thread_fsm *self);
1447 /* until_next_fsm's vtable. */
1449 static struct thread_fsm_ops until_next_fsm_ops =
1452 until_next_fsm_clean_up,
1453 until_next_fsm_should_stop,
1454 NULL, /* return_value */
1455 until_next_fsm_async_reply_reason,
1458 /* Allocate a new until_next_fsm. */
1460 static struct until_next_fsm *
1461 new_until_next_fsm (struct interp *cmd_interp, int thread)
1463 struct until_next_fsm *sm;
1465 sm = XCNEW (struct until_next_fsm);
1466 thread_fsm_ctor (&sm->thread_fsm, &until_next_fsm_ops, cmd_interp);
1468 sm->thread = thread;
1473 /* Implementation of the 'should_stop' FSM method for the until (with
1477 until_next_fsm_should_stop (struct thread_fsm *self,
1478 struct thread_info *tp)
1480 if (tp->control.stop_step)
1481 thread_fsm_set_finished (self);
1486 /* Implementation of the 'clean_up' FSM method for the until (with no
1490 until_next_fsm_clean_up (struct thread_fsm *self, struct thread_info *thread)
1492 struct until_next_fsm *sm = (struct until_next_fsm *) self;
1494 delete_longjmp_breakpoint (thread->global_num);
1497 /* Implementation of the 'async_reply_reason' FSM method for the until
1498 (with no arg) command. */
1500 static enum async_reply_reason
1501 until_next_fsm_async_reply_reason (struct thread_fsm *self)
1503 return EXEC_ASYNC_END_STEPPING_RANGE;
1506 /* Proceed until we reach a different source line with pc greater than
1507 our current one or exit the function. We skip calls in both cases.
1509 Note that eventually this command should probably be changed so
1510 that only source lines are printed out when we hit the breakpoint
1511 we set. This may involve changes to wait_for_inferior and the
1512 proceed status code. */
1515 until_next_command (int from_tty)
1517 struct frame_info *frame;
1519 struct symbol *func;
1520 struct symtab_and_line sal;
1521 struct thread_info *tp = inferior_thread ();
1522 int thread = tp->global_num;
1523 struct cleanup *old_chain;
1524 struct until_next_fsm *sm;
1526 clear_proceed_status (0);
1529 frame = get_current_frame ();
1531 /* Step until either exited from this function or greater
1532 than the current line (if in symbolic section) or pc (if
1535 pc = get_frame_pc (frame);
1536 func = find_pc_function (pc);
1540 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1542 if (msymbol.minsym == NULL)
1543 error (_("Execution is not within a known function."));
1545 tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol);
1546 /* The upper-bound of step_range is exclusive. In order to make PC
1547 within the range, set the step_range_end with PC + 1. */
1548 tp->control.step_range_end = pc + 1;
1552 sal = find_pc_line (pc, 0);
1554 tp->control.step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1555 tp->control.step_range_end = sal.end;
1557 tp->control.may_range_step = 1;
1559 tp->control.step_over_calls = STEP_OVER_ALL;
1561 set_longjmp_breakpoint (tp, get_frame_id (frame));
1562 old_chain = make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
1564 sm = new_until_next_fsm (command_interp (), tp->global_num);
1565 tp->thread_fsm = &sm->thread_fsm;
1566 discard_cleanups (old_chain);
1568 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1572 until_command (char *arg, int from_tty)
1575 struct cleanup *args_chain;
1578 ensure_not_tfind_mode ();
1579 ensure_valid_thread ();
1580 ensure_not_running ();
1582 /* Find out whether we must run in the background. */
1583 arg = strip_bg_char (arg, &async_exec);
1584 args_chain = make_cleanup (xfree, arg);
1586 prepare_execution_command (¤t_target, async_exec);
1589 until_break_command (arg, from_tty, 0);
1591 until_next_command (from_tty);
1593 /* Done with ARGS. */
1594 do_cleanups (args_chain);
1598 advance_command (char *arg, int from_tty)
1601 struct cleanup *args_chain;
1604 ensure_not_tfind_mode ();
1605 ensure_valid_thread ();
1606 ensure_not_running ();
1609 error_no_arg (_("a location"));
1611 /* Find out whether we must run in the background. */
1612 arg = strip_bg_char (arg, &async_exec);
1613 args_chain = make_cleanup (xfree, arg);
1615 prepare_execution_command (¤t_target, async_exec);
1617 until_break_command (arg, from_tty, 1);
1619 /* Done with ARGS. */
1620 do_cleanups (args_chain);
1623 /* Return the value of the result of a function at the end of a 'finish'
1624 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1625 right after an inferior call has finished. */
1628 get_return_value (struct value *function, struct type *value_type)
1630 regcache stop_regs (regcache::readonly, *get_current_regcache ());
1631 struct gdbarch *gdbarch = stop_regs.arch ();
1632 struct value *value;
1634 value_type = check_typedef (value_type);
1635 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
1637 /* FIXME: 2003-09-27: When returning from a nested inferior function
1638 call, it's possible (with no help from the architecture vector)
1639 to locate and return/print a "struct return" value. This is just
1640 a more complicated case of what is already being done in the
1641 inferior function call code. In fact, when inferior function
1642 calls are made async, this will likely be made the norm. */
1644 switch (gdbarch_return_value (gdbarch, function, value_type,
1647 case RETURN_VALUE_REGISTER_CONVENTION:
1648 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1649 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1650 value = allocate_value (value_type);
1651 gdbarch_return_value (gdbarch, function, value_type, &stop_regs,
1652 value_contents_raw (value), NULL);
1654 case RETURN_VALUE_STRUCT_CONVENTION:
1658 internal_error (__FILE__, __LINE__, _("bad switch"));
1664 /* The captured function return value/type and its position in the
1667 struct return_value_info
1669 /* The captured return value. May be NULL if we weren't able to
1670 retrieve it. See get_return_value. */
1671 struct value *value;
1673 /* The return type. In some cases, we'll not be able extract the
1674 return value, but we always know the type. */
1677 /* If we captured a value, this is the value history index. */
1678 int value_history_index;
1681 /* Helper for print_return_value. */
1684 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1686 if (rv->value != NULL)
1688 struct value_print_options opts;
1691 uiout->text ("Value returned is ");
1692 uiout->field_fmt ("gdb-result-var", "$%d",
1693 rv->value_history_index);
1694 uiout->text (" = ");
1695 get_no_prettyformat_print_options (&opts);
1699 value_print (rv->value, &stb, &opts);
1700 uiout->field_stream ("return-value", stb);
1705 std::string type_name = type_to_string (rv->type);
1706 uiout->text ("Value returned has type: ");
1707 uiout->field_string ("return-type", type_name.c_str ());
1709 uiout->text (" Cannot determine contents\n");
1713 /* Print the result of a function at the end of a 'finish' command.
1714 RV points at an object representing the captured return value/type
1715 and its position in the value history. */
1718 print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1720 if (rv->type == NULL || TYPE_CODE (rv->type) == TYPE_CODE_VOID)
1725 /* print_return_value_1 can throw an exception in some
1726 circumstances. We need to catch this so that we still
1727 delete the breakpoint. */
1728 print_return_value_1 (uiout, rv);
1730 CATCH (ex, RETURN_MASK_ALL)
1732 exception_print (gdb_stdout, ex);
1737 /* Data for the FSM that manages the finish command. */
1739 struct finish_command_fsm
1741 /* The base class. */
1742 struct thread_fsm thread_fsm;
1744 /* The momentary breakpoint set at the function's return address in
1746 struct breakpoint *breakpoint;
1748 /* The function that we're stepping out of. */
1749 struct symbol *function;
1751 /* If the FSM finishes successfully, this stores the function's
1753 struct return_value_info return_value;
1756 static int finish_command_fsm_should_stop (struct thread_fsm *self,
1757 struct thread_info *thread);
1758 static void finish_command_fsm_clean_up (struct thread_fsm *self,
1759 struct thread_info *thread);
1760 static struct return_value_info *
1761 finish_command_fsm_return_value (struct thread_fsm *self);
1762 static enum async_reply_reason
1763 finish_command_fsm_async_reply_reason (struct thread_fsm *self);
1765 /* finish_command_fsm's vtable. */
1767 static struct thread_fsm_ops finish_command_fsm_ops =
1770 finish_command_fsm_clean_up,
1771 finish_command_fsm_should_stop,
1772 finish_command_fsm_return_value,
1773 finish_command_fsm_async_reply_reason,
1774 NULL, /* should_notify_stop */
1777 /* Allocate a new finish_command_fsm. */
1779 static struct finish_command_fsm *
1780 new_finish_command_fsm (struct interp *cmd_interp)
1782 struct finish_command_fsm *sm;
1784 sm = XCNEW (struct finish_command_fsm);
1785 thread_fsm_ctor (&sm->thread_fsm, &finish_command_fsm_ops, cmd_interp);
1790 /* Implementation of the 'should_stop' FSM method for the finish
1791 commands. Detects whether the thread stepped out of the function
1792 successfully, and if so, captures the function's return value and
1793 marks the FSM finished. */
1796 finish_command_fsm_should_stop (struct thread_fsm *self,
1797 struct thread_info *tp)
1799 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1800 struct return_value_info *rv = &f->return_value;
1802 if (f->function != NULL
1803 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1804 f->breakpoint) != NULL)
1807 thread_fsm_set_finished (self);
1809 rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (f->function));
1810 if (rv->type == NULL)
1811 internal_error (__FILE__, __LINE__,
1812 _("finish_command: function has no target type"));
1814 if (TYPE_CODE (rv->type) != TYPE_CODE_VOID)
1818 func = read_var_value (f->function, NULL, get_current_frame ());
1819 rv->value = get_return_value (func, rv->type);
1820 if (rv->value != NULL)
1821 rv->value_history_index = record_latest_value (rv->value);
1824 else if (tp->control.stop_step)
1826 /* Finishing from an inline frame, or reverse finishing. In
1827 either case, there's no way to retrieve the return value. */
1828 thread_fsm_set_finished (self);
1834 /* Implementation of the 'clean_up' FSM method for the finish
1838 finish_command_fsm_clean_up (struct thread_fsm *self,
1839 struct thread_info *thread)
1841 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1843 if (f->breakpoint != NULL)
1845 delete_breakpoint (f->breakpoint);
1846 f->breakpoint = NULL;
1848 delete_longjmp_breakpoint (thread->global_num);
1851 /* Implementation of the 'return_value' FSM method for the finish
1854 static struct return_value_info *
1855 finish_command_fsm_return_value (struct thread_fsm *self)
1857 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1859 return &f->return_value;
1862 /* Implementation of the 'async_reply_reason' FSM method for the
1865 static enum async_reply_reason
1866 finish_command_fsm_async_reply_reason (struct thread_fsm *self)
1868 if (execution_direction == EXEC_REVERSE)
1869 return EXEC_ASYNC_END_STEPPING_RANGE;
1871 return EXEC_ASYNC_FUNCTION_FINISHED;
1874 /* finish_backward -- helper function for finish_command. */
1877 finish_backward (struct finish_command_fsm *sm)
1879 struct symtab_and_line sal;
1880 struct thread_info *tp = inferior_thread ();
1882 CORE_ADDR func_addr;
1884 pc = get_frame_pc (get_current_frame ());
1886 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0)
1887 error (_("Cannot find bounds of current function"));
1889 sal = find_pc_line (func_addr, 0);
1891 tp->control.proceed_to_finish = 1;
1892 /* Special case: if we're sitting at the function entry point,
1893 then all we need to do is take a reverse singlestep. We
1894 don't need to set a breakpoint, and indeed it would do us
1897 Note that this can only happen at frame #0, since there's
1898 no way that a function up the stack can have a return address
1899 that's equal to its entry point. */
1903 struct frame_info *frame = get_selected_frame (NULL);
1904 struct gdbarch *gdbarch = get_frame_arch (frame);
1906 /* Set a step-resume at the function's entry point. Once that's
1907 hit, we'll do one more step backwards. */
1908 symtab_and_line sr_sal;
1910 sr_sal.pspace = get_frame_program_space (frame);
1911 insert_step_resume_breakpoint_at_sal (gdbarch,
1912 sr_sal, null_frame_id);
1914 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1918 /* We're almost there -- we just need to back up by one more
1920 tp->control.step_range_start = tp->control.step_range_end = 1;
1921 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1925 /* finish_forward -- helper function for finish_command. FRAME is the
1926 frame that called the function we're about to step out of. */
1929 finish_forward (struct finish_command_fsm *sm, struct frame_info *frame)
1931 struct frame_id frame_id = get_frame_id (frame);
1932 struct gdbarch *gdbarch = get_frame_arch (frame);
1933 struct symtab_and_line sal;
1934 struct thread_info *tp = inferior_thread ();
1936 sal = find_pc_line (get_frame_pc (frame), 0);
1937 sal.pc = get_frame_pc (frame);
1939 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1940 get_stack_frame_id (frame),
1943 /* set_momentary_breakpoint invalidates FRAME. */
1946 set_longjmp_breakpoint (tp, frame_id);
1948 /* We want to print return value, please... */
1949 tp->control.proceed_to_finish = 1;
1951 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1954 /* Skip frames for "finish". */
1956 static struct frame_info *
1957 skip_finish_frames (struct frame_info *frame)
1959 struct frame_info *start;
1965 frame = skip_tailcall_frames (frame);
1969 frame = skip_unwritable_frames (frame);
1973 while (start != frame);
1978 /* "finish": Set a temporary breakpoint at the place the selected
1979 frame will return to, then continue. */
1982 finish_command (char *arg, int from_tty)
1984 struct frame_info *frame;
1986 struct cleanup *args_chain;
1987 struct finish_command_fsm *sm;
1988 struct thread_info *tp;
1991 ensure_not_tfind_mode ();
1992 ensure_valid_thread ();
1993 ensure_not_running ();
1995 /* Find out whether we must run in the background. */
1996 arg = strip_bg_char (arg, &async_exec);
1997 args_chain = make_cleanup (xfree, arg);
1999 prepare_execution_command (¤t_target, async_exec);
2002 error (_("The \"finish\" command does not take any arguments."));
2004 /* Done with ARGS. */
2005 do_cleanups (args_chain);
2007 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
2009 error (_("\"finish\" not meaningful in the outermost frame."));
2011 clear_proceed_status (0);
2013 tp = inferior_thread ();
2015 sm = new_finish_command_fsm (command_interp ());
2017 tp->thread_fsm = &sm->thread_fsm;
2019 /* Finishing from an inline frame is completely different. We don't
2020 try to show the "return value" - no way to locate it. */
2021 if (get_frame_type (get_selected_frame (_("No selected frame.")))
2024 /* Claim we are stepping in the calling frame. An empty step
2025 range means that we will stop once we aren't in a function
2026 called by that frame. We don't use the magic "1" value for
2027 step_range_end, because then infrun will think this is nexti,
2028 and not step over the rest of this inlined function call. */
2029 set_step_info (frame, {});
2030 tp->control.step_range_start = get_frame_pc (frame);
2031 tp->control.step_range_end = tp->control.step_range_start;
2032 tp->control.step_over_calls = STEP_OVER_ALL;
2034 /* Print info on the selected frame, including level number but not
2038 printf_filtered (_("Run till exit from "));
2039 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2042 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2046 /* Find the function we will return from. */
2048 sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
2050 /* Print info on the selected frame, including level number but not
2054 if (execution_direction == EXEC_REVERSE)
2055 printf_filtered (_("Run back to call of "));
2058 if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type)
2059 && !query (_("warning: Function %s does not return normally.\n"
2060 "Try to finish anyway? "),
2061 SYMBOL_PRINT_NAME (sm->function)))
2062 error (_("Not confirmed."));
2063 printf_filtered (_("Run till exit from "));
2066 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2069 if (execution_direction == EXEC_REVERSE)
2070 finish_backward (sm);
2073 frame = skip_finish_frames (frame);
2076 error (_("Cannot find the caller frame."));
2078 finish_forward (sm, frame);
2084 info_program_command (char *args, int from_tty)
2088 struct thread_info *tp;
2091 if (!target_has_execution)
2093 printf_filtered (_("The program being debugged is not being run.\n"));
2098 ptid = inferior_ptid;
2101 struct target_waitstatus ws;
2103 get_last_target_status (&ptid, &ws);
2106 if (ptid_equal (ptid, null_ptid) || is_exited (ptid))
2107 error (_("Invalid selected thread."));
2108 else if (is_running (ptid))
2109 error (_("Selected thread is running."));
2111 tp = find_thread_ptid (ptid);
2112 bs = tp->control.stop_bpstat;
2113 stat = bpstat_num (&bs, &num);
2115 target_files_info ();
2116 printf_filtered (_("Program stopped at %s.\n"),
2117 paddress (target_gdbarch (), stop_pc));
2118 if (tp->control.stop_step)
2119 printf_filtered (_("It stopped after being stepped.\n"));
2122 /* There may be several breakpoints in the same place, so this
2123 isn't as strange as it seems. */
2128 printf_filtered (_("It stopped at a breakpoint "
2129 "that has since been deleted.\n"));
2132 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
2133 stat = bpstat_num (&bs, &num);
2136 else if (tp->suspend.stop_signal != GDB_SIGNAL_0)
2138 printf_filtered (_("It stopped with signal %s, %s.\n"),
2139 gdb_signal_to_name (tp->suspend.stop_signal),
2140 gdb_signal_to_string (tp->suspend.stop_signal));
2145 printf_filtered (_("Type \"info stack\" or \"info "
2146 "registers\" for more information.\n"));
2151 environment_info (char *var, int from_tty)
2155 const char *val = current_inferior ()->environment.get (var);
2159 puts_filtered (var);
2160 puts_filtered (" = ");
2161 puts_filtered (val);
2162 puts_filtered ("\n");
2166 puts_filtered ("Environment variable \"");
2167 puts_filtered (var);
2168 puts_filtered ("\" not defined.\n");
2173 char **envp = current_inferior ()->environment.envp ();
2175 for (int idx = 0; envp[idx] != NULL; ++idx)
2177 puts_filtered (envp[idx]);
2178 puts_filtered ("\n");
2184 set_environment_command (char *arg, int from_tty)
2186 char *p, *val, *var;
2190 error_no_arg (_("environment variable and value"));
2192 /* Find seperation between variable name and value. */
2193 p = (char *) strchr (arg, '=');
2194 val = (char *) strchr (arg, ' ');
2196 if (p != 0 && val != 0)
2198 /* We have both a space and an equals. If the space is before the
2199 equals, walk forward over the spaces til we see a nonspace
2200 (possibly the equals). */
2205 /* Now if the = is after the char following the spaces,
2206 take the char following the spaces. */
2210 else if (val != 0 && p == 0)
2214 error_no_arg (_("environment variable to set"));
2216 if (p == 0 || p[1] == 0)
2220 p = arg + strlen (arg); /* So that savestring below will work. */
2224 /* Not setting variable value to null. */
2226 while (*val == ' ' || *val == '\t')
2230 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2233 var = savestring (arg, p - arg);
2236 printf_filtered (_("Setting environment variable "
2237 "\"%s\" to null value.\n"),
2239 current_inferior ()->environment.set (var, "");
2242 current_inferior ()->environment.set (var, val);
2247 unset_environment_command (char *var, int from_tty)
2251 /* If there is no argument, delete all environment variables.
2252 Ask for confirmation if reading from the terminal. */
2253 if (!from_tty || query (_("Delete all environment variables? ")))
2254 current_inferior ()->environment.clear ();
2257 current_inferior ()->environment.unset (var);
2260 /* Handle the execution path (PATH variable). */
2262 static const char path_var_name[] = "PATH";
2265 path_info (char *args, int from_tty)
2267 puts_filtered ("Executable and object file path: ");
2268 puts_filtered (current_inferior ()->environment.get (path_var_name));
2269 puts_filtered ("\n");
2272 /* Add zero or more directories to the front of the execution path. */
2275 path_command (char *dirname, int from_tty)
2281 env = current_inferior ()->environment.get (path_var_name);
2282 /* Can be null if path is not set. */
2285 exec_path = xstrdup (env);
2286 mod_path (dirname, &exec_path);
2287 current_inferior ()->environment.set (path_var_name, exec_path);
2290 path_info ((char *) NULL, from_tty);
2294 /* Print out the register NAME with value VAL, to FILE, in the default
2298 default_print_one_register_info (struct ui_file *file,
2302 struct type *regtype = value_type (val);
2303 int print_raw_format;
2305 fputs_filtered (name, file);
2306 print_spaces_filtered (15 - strlen (name), file);
2308 print_raw_format = (value_entirely_available (val)
2309 && !value_optimized_out (val));
2311 /* If virtual format is floating, print it that way, and in raw
2313 if (TYPE_CODE (regtype) == TYPE_CODE_FLT
2314 || TYPE_CODE (regtype) == TYPE_CODE_DECFLOAT)
2316 struct value_print_options opts;
2317 const gdb_byte *valaddr = value_contents_for_printing (val);
2318 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (regtype));
2320 get_user_print_options (&opts);
2324 value_embedded_offset (val), 0,
2325 file, 0, val, &opts, current_language);
2327 if (print_raw_format)
2329 fprintf_filtered (file, "\t(raw ");
2330 print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order,
2332 fprintf_filtered (file, ")");
2337 struct value_print_options opts;
2339 /* Print the register in hex. */
2340 get_formatted_print_options (&opts, 'x');
2343 value_embedded_offset (val), 0,
2344 file, 0, val, &opts, current_language);
2345 /* If not a vector register, print it also according to its
2347 if (print_raw_format && TYPE_VECTOR (regtype) == 0)
2349 get_user_print_options (&opts);
2351 fprintf_filtered (file, "\t");
2353 value_embedded_offset (val), 0,
2354 file, 0, val, &opts, current_language);
2358 fprintf_filtered (file, "\n");
2361 /* Print out the machine register regnum. If regnum is -1, print all
2362 registers (print_all == 1) or all non-float and non-vector
2363 registers (print_all == 0).
2365 For most machines, having all_registers_info() print the
2366 register(s) one per line is good enough. If a different format is
2367 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2368 regs), or there is an existing convention for showing all the
2369 registers, define the architecture method PRINT_REGISTERS_INFO to
2370 provide that format. */
2373 default_print_registers_info (struct gdbarch *gdbarch,
2374 struct ui_file *file,
2375 struct frame_info *frame,
2376 int regnum, int print_all)
2379 const int numregs = gdbarch_num_regs (gdbarch)
2380 + gdbarch_num_pseudo_regs (gdbarch);
2382 for (i = 0; i < numregs; i++)
2384 /* Decide between printing all regs, non-float / vector regs, or
2390 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2395 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2405 /* If the register name is empty, it is undefined for this
2406 processor, so don't display anything. */
2407 if (gdbarch_register_name (gdbarch, i) == NULL
2408 || *(gdbarch_register_name (gdbarch, i)) == '\0')
2411 default_print_one_register_info (file,
2412 gdbarch_register_name (gdbarch, i),
2413 value_of_register (i, frame));
2418 registers_info (char *addr_exp, int fpregs)
2420 struct frame_info *frame;
2421 struct gdbarch *gdbarch;
2423 if (!target_has_registers)
2424 error (_("The program has no registers now."));
2425 frame = get_selected_frame (NULL);
2426 gdbarch = get_frame_arch (frame);
2430 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2435 while (*addr_exp != '\0')
2440 /* Skip leading white space. */
2441 addr_exp = skip_spaces (addr_exp);
2443 /* Discard any leading ``$''. Check that there is something
2444 resembling a register following it. */
2445 if (addr_exp[0] == '$')
2447 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2448 error (_("Missing register name"));
2450 /* Find the start/end of this register name/num/group. */
2452 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2456 /* Figure out what we've found and display it. */
2458 /* A register name? */
2460 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2464 /* User registers lie completely outside of the range of
2465 normal registers. Catch them early so that the target
2467 if (regnum >= gdbarch_num_regs (gdbarch)
2468 + gdbarch_num_pseudo_regs (gdbarch))
2470 struct value *regval = value_of_user_reg (regnum, frame);
2471 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2474 /* Print in the same fashion
2475 gdbarch_print_registers_info's default
2476 implementation prints. */
2477 default_print_one_register_info (gdb_stdout,
2482 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2483 frame, regnum, fpregs);
2488 /* A register group? */
2490 struct reggroup *group;
2492 for (group = reggroup_next (gdbarch, NULL);
2494 group = reggroup_next (gdbarch, group))
2496 /* Don't bother with a length check. Should the user
2497 enter a short register group name, go with the first
2498 group that matches. */
2499 if (strncmp (start, reggroup_name (group), end - start) == 0)
2507 regnum < gdbarch_num_regs (gdbarch)
2508 + gdbarch_num_pseudo_regs (gdbarch);
2511 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2512 gdbarch_print_registers_info (gdbarch,
2520 /* Nothing matched. */
2521 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2526 info_all_registers_command (char *addr_exp, int from_tty)
2528 registers_info (addr_exp, 1);
2532 info_registers_command (char *addr_exp, int from_tty)
2534 registers_info (addr_exp, 0);
2538 print_vector_info (struct ui_file *file,
2539 struct frame_info *frame, const char *args)
2541 struct gdbarch *gdbarch = get_frame_arch (frame);
2543 if (gdbarch_print_vector_info_p (gdbarch))
2544 gdbarch_print_vector_info (gdbarch, file, frame, args);
2548 int printed_something = 0;
2551 regnum < gdbarch_num_regs (gdbarch)
2552 + gdbarch_num_pseudo_regs (gdbarch);
2555 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2557 printed_something = 1;
2558 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2561 if (!printed_something)
2562 fprintf_filtered (file, "No vector information\n");
2567 info_vector_command (char *args, int from_tty)
2569 if (!target_has_registers)
2570 error (_("The program has no registers now."));
2572 print_vector_info (gdb_stdout, get_selected_frame (NULL), args);
2575 /* Kill the inferior process. Make us have no inferior. */
2578 kill_command (char *arg, int from_tty)
2580 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2581 It should be a distinct flag that indicates that a target is active, cuz
2582 some targets don't have processes! */
2584 if (ptid_equal (inferior_ptid, null_ptid))
2585 error (_("The program is not being run."));
2586 if (!query (_("Kill the program being debugged? ")))
2587 error (_("Not confirmed."));
2590 /* If we still have other inferiors to debug, then don't mess with
2591 with their threads. */
2592 if (!have_inferiors ())
2594 init_thread_list (); /* Destroy thread info. */
2596 /* Killing off the inferior can leave us with a core file. If
2597 so, print the state we are left in. */
2598 if (target_has_stack)
2600 printf_filtered (_("In %s,\n"), target_longname);
2601 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2604 bfd_cache_close_all ();
2607 /* Used in `attach&' command. ARG is a point to an integer
2608 representing a process id. Proceed threads of this process iff
2609 they stopped due to debugger request, and when they did, they
2610 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads
2611 that have been explicitly been told to stop. */
2614 proceed_after_attach_callback (struct thread_info *thread,
2617 int pid = * (int *) arg;
2619 if (ptid_get_pid (thread->ptid) == pid
2620 && !is_exited (thread->ptid)
2621 && !is_executing (thread->ptid)
2622 && !thread->stop_requested
2623 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2625 switch_to_thread (thread->ptid);
2626 clear_proceed_status (0);
2627 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2634 proceed_after_attach (int pid)
2636 /* Don't error out if the current thread is running, because
2637 there may be other stopped threads. */
2639 /* Backup current thread and selected frame. */
2640 scoped_restore_current_thread restore_thread;
2642 iterate_over_threads (proceed_after_attach_callback, &pid);
2645 /* See inferior.h. */
2648 setup_inferior (int from_tty)
2650 struct inferior *inferior;
2652 inferior = current_inferior ();
2653 inferior->needs_setup = 0;
2655 /* If no exec file is yet known, try to determine it from the
2657 if (get_exec_file (0) == NULL)
2658 exec_file_locate_attach (ptid_get_pid (inferior_ptid), 1, from_tty);
2661 reopen_exec_file ();
2665 /* Take any necessary post-attaching actions for this platform. */
2666 target_post_attach (ptid_get_pid (inferior_ptid));
2668 post_create_inferior (¤t_target, from_tty);
2671 /* What to do after the first program stops after attaching. */
2672 enum attach_post_wait_mode
2674 /* Do nothing. Leaves threads as they are. */
2675 ATTACH_POST_WAIT_NOTHING,
2677 /* Re-resume threads that are marked running. */
2678 ATTACH_POST_WAIT_RESUME,
2680 /* Stop all threads. */
2681 ATTACH_POST_WAIT_STOP,
2684 /* Called after we've attached to a process and we've seen it stop for
2685 the first time. If ASYNC_EXEC is true, re-resume threads that
2686 should be running. Else if ATTACH, */
2689 attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mode)
2691 struct inferior *inferior;
2693 inferior = current_inferior ();
2694 inferior->control.stop_soon = NO_STOP_QUIETLY;
2696 if (inferior->needs_setup)
2697 setup_inferior (from_tty);
2699 if (mode == ATTACH_POST_WAIT_RESUME)
2701 /* The user requested an `attach&', so be sure to leave threads
2702 that didn't get a signal running. */
2704 /* Immediatelly resume all suspended threads of this inferior,
2705 and this inferior only. This should have no effect on
2706 already running threads. If a thread has been stopped with a
2707 signal, leave it be. */
2709 proceed_after_attach (inferior->pid);
2712 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
2714 clear_proceed_status (0);
2715 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2719 else if (mode == ATTACH_POST_WAIT_STOP)
2721 /* The user requested a plain `attach', so be sure to leave
2722 the inferior stopped. */
2724 /* At least the current thread is already stopped. */
2726 /* In all-stop, by definition, all threads have to be already
2727 stopped at this point. In non-stop, however, although the
2728 selected thread is stopped, others may still be executing.
2729 Be sure to explicitly stop all threads of the process. This
2730 should have no effect on already stopped threads. */
2732 target_stop (pid_to_ptid (inferior->pid));
2733 else if (target_is_non_stop_p ())
2735 struct thread_info *thread;
2736 struct thread_info *lowest = inferior_thread ();
2737 int pid = current_inferior ()->pid;
2739 stop_all_threads ();
2741 /* It's not defined which thread will report the attach
2742 stop. For consistency, always select the thread with
2743 lowest GDB number, which should be the main thread, if it
2745 ALL_NON_EXITED_THREADS (thread)
2747 if (ptid_get_pid (thread->ptid) == pid)
2749 if (thread->inf->num < lowest->inf->num
2750 || thread->per_inf_num < lowest->per_inf_num)
2755 switch_to_thread (lowest->ptid);
2758 /* Tell the user/frontend where we're stopped. */
2760 if (deprecated_attach_hook)
2761 deprecated_attach_hook ();
2765 struct attach_command_continuation_args
2769 enum attach_post_wait_mode mode;
2773 attach_command_continuation (void *args, int err)
2775 struct attach_command_continuation_args *a
2776 = (struct attach_command_continuation_args *) args;
2781 attach_post_wait (a->args, a->from_tty, a->mode);
2785 attach_command_continuation_free_args (void *args)
2787 struct attach_command_continuation_args *a
2788 = (struct attach_command_continuation_args *) args;
2794 /* "attach" command entry point. Takes a program started up outside
2795 of gdb and ``attaches'' to it. This stops it cold in its tracks
2796 and allows us to start debugging it. */
2799 attach_command (char *args, int from_tty)
2802 struct cleanup *args_chain;
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 args = strip_bg_char (args, &async_exec);
2826 args_chain = make_cleanup (xfree, args);
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);
2904 /* Done with ARGS. */
2905 do_cleanups (args_chain);
2907 if (!target_is_async_p ())
2908 mark_infrun_async_event_handler ();
2912 /* Done with ARGS. */
2913 do_cleanups (args_chain);
2915 attach_post_wait (args, from_tty, mode);
2918 /* We had just found out that the target was already attached to an
2919 inferior. PTID points at a thread of this new inferior, that is
2920 the most likely to be stopped right now, but not necessarily so.
2921 The new inferior is assumed to be already added to the inferior
2922 list at this point. If LEAVE_RUNNING, then leave the threads of
2923 this inferior running, except those we've explicitly seen reported
2927 notice_new_inferior (ptid_t ptid, int leave_running, int from_tty)
2929 enum attach_post_wait_mode mode
2930 = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2932 gdb::optional<scoped_restore_current_thread> restore_thread;
2934 if (inferior_ptid != null_ptid)
2935 restore_thread.emplace ();
2937 /* Avoid reading registers -- we haven't fetched the target
2939 switch_to_thread_no_regs (find_thread_ptid (ptid));
2941 /* When we "notice" a new inferior we need to do all the things we
2942 would normally do if we had just attached to it. */
2944 if (is_executing (inferior_ptid))
2946 struct attach_command_continuation_args *a;
2947 struct inferior *inferior = current_inferior ();
2949 /* We're going to install breakpoints, and poke at memory,
2950 ensure that the inferior is stopped for a moment while we do
2952 target_stop (inferior_ptid);
2954 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2956 /* Wait for stop before proceeding. */
2957 a = XNEW (struct attach_command_continuation_args);
2958 a->args = xstrdup ("");
2959 a->from_tty = from_tty;
2961 add_inferior_continuation (attach_command_continuation, a,
2962 attach_command_continuation_free_args);
2967 attach_post_wait ("" /* args */, from_tty, mode);
2972 * takes a program previously attached to and detaches it.
2973 * The program resumes execution and will no longer stop
2974 * on signals, etc. We better not have left any breakpoints
2975 * in the program or it'll die when it hits one. For this
2976 * to work, it may be necessary for the process to have been
2977 * previously attached. It *might* work if the program was
2978 * started via the normal ptrace (PTRACE_TRACEME).
2982 detach_command (char *args, int from_tty)
2984 dont_repeat (); /* Not for the faint of heart. */
2986 if (ptid_equal (inferior_ptid, null_ptid))
2987 error (_("The program is not being run."));
2989 query_if_trace_running (from_tty);
2991 disconnect_tracing ();
2993 target_detach (args, from_tty);
2995 /* The current inferior process was just detached successfully. Get
2996 rid of breakpoints that no longer make sense. Note we don't do
2997 this within target_detach because that is also used when
2998 following child forks, and in that case we will want to transfer
2999 breakpoints to the child, not delete them. */
3000 breakpoint_init_inferior (inf_exited);
3002 /* If the solist is global across inferiors, don't clear it when we
3003 detach from a single inferior. */
3004 if (!gdbarch_has_global_solist (target_gdbarch ()))
3005 no_shared_libraries (NULL, from_tty);
3007 /* If we still have inferiors to debug, then don't mess with their
3009 if (!have_inferiors ())
3010 init_thread_list ();
3012 if (deprecated_detach_hook)
3013 deprecated_detach_hook ();
3016 /* Disconnect from the current target without resuming it (leaving it
3017 waiting for a debugger).
3019 We'd better not have left any breakpoints in the program or the
3020 next debugger will get confused. Currently only supported for some
3021 remote targets, since the normal attach mechanisms don't work on
3022 stopped processes on some native platforms (e.g. GNU/Linux). */
3025 disconnect_command (char *args, int from_tty)
3027 dont_repeat (); /* Not for the faint of heart. */
3028 query_if_trace_running (from_tty);
3029 disconnect_tracing ();
3030 target_disconnect (args, from_tty);
3031 no_shared_libraries (NULL, from_tty);
3032 init_thread_list ();
3033 if (deprecated_detach_hook)
3034 deprecated_detach_hook ();
3038 interrupt_target_1 (int all_threads)
3043 ptid = minus_one_ptid;
3045 ptid = inferior_ptid;
3050 target_interrupt (ptid);
3052 /* Tag the thread as having been explicitly requested to stop, so
3053 other parts of gdb know not to resume this thread automatically,
3054 if it was stopped due to an internal event. Limit this to
3055 non-stop mode, as when debugging a multi-threaded application in
3056 all-stop mode, we will only get one stop event --- it's undefined
3057 which thread will report the event. */
3059 set_stop_requested (ptid, 1);
3063 Stop the execution of the target while running in async mode, in
3064 the background. In all-stop, stop the whole process. In non-stop
3065 mode, stop the current thread only by default, or stop all threads
3066 if the `-a' switch is used. */
3069 interrupt_command (char *args, int from_tty)
3071 if (target_can_async_p ())
3073 int all_threads = 0;
3075 dont_repeat (); /* Not for the faint of heart. */
3078 && startswith (args, "-a"))
3081 if (!non_stop && all_threads)
3082 error (_("-a is meaningless in all-stop mode."));
3084 interrupt_target_1 (all_threads);
3088 /* See inferior.h. */
3091 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3092 struct frame_info *frame, const char *args)
3095 int printed_something = 0;
3098 regnum < gdbarch_num_regs (gdbarch)
3099 + gdbarch_num_pseudo_regs (gdbarch);
3102 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
3104 printed_something = 1;
3105 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
3108 if (!printed_something)
3109 fprintf_filtered (file, "No floating-point info "
3110 "available for this processor.\n");
3114 info_float_command (char *args, int from_tty)
3116 struct frame_info *frame;
3118 if (!target_has_registers)
3119 error (_("The program has no registers now."));
3121 frame = get_selected_frame (NULL);
3122 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
3126 unset_command (char *args, int from_tty)
3128 printf_filtered (_("\"unset\" must be followed by the "
3129 "name of an unset subcommand.\n"));
3130 help_list (unsetlist, "unset ", all_commands, gdb_stdout);
3133 /* Implement `info proc' family of commands. */
3136 info_proc_cmd_1 (char *args, enum info_proc_what what, int from_tty)
3138 struct gdbarch *gdbarch = get_current_arch ();
3140 if (!target_info_proc (args, what))
3142 if (gdbarch_info_proc_p (gdbarch))
3143 gdbarch_info_proc (gdbarch, args, what);
3145 error (_("Not supported on this target."));
3149 /* Implement `info proc' when given without any futher parameters. */
3152 info_proc_cmd (char *args, int from_tty)
3154 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
3157 /* Implement `info proc mappings'. */
3160 info_proc_cmd_mappings (char *args, int from_tty)
3162 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
3165 /* Implement `info proc stat'. */
3168 info_proc_cmd_stat (char *args, int from_tty)
3170 info_proc_cmd_1 (args, IP_STAT, from_tty);
3173 /* Implement `info proc status'. */
3176 info_proc_cmd_status (char *args, int from_tty)
3178 info_proc_cmd_1 (args, IP_STATUS, from_tty);
3181 /* Implement `info proc cwd'. */
3184 info_proc_cmd_cwd (char *args, int from_tty)
3186 info_proc_cmd_1 (args, IP_CWD, from_tty);
3189 /* Implement `info proc cmdline'. */
3192 info_proc_cmd_cmdline (char *args, int from_tty)
3194 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
3197 /* Implement `info proc exe'. */
3200 info_proc_cmd_exe (char *args, int from_tty)
3202 info_proc_cmd_1 (args, IP_EXE, from_tty);
3205 /* Implement `info proc all'. */
3208 info_proc_cmd_all (char *args, int from_tty)
3210 info_proc_cmd_1 (args, IP_ALL, from_tty);
3213 /* This help string is used for the run, start, and starti commands.
3214 It is defined as a macro to prevent duplication. */
3216 #define RUN_ARGS_HELP \
3217 "You may specify arguments to give it.\n\
3218 Args may include \"*\", or \"[...]\"; they are expanded using the\n\
3219 shell that will start the program (specified by the \"$SHELL\" environment\n\
3220 variable). Input and output redirection with \">\", \"<\", or \">>\"\n\
3221 are also allowed.\n\
3223 With no arguments, uses arguments last specified (with \"run\" or \n\
3224 \"set args\"). To cancel previous arguments and run with no arguments,\n\
3225 use \"set args\" without arguments.\n\
3227 To start the inferior without using a shell, use \"set startup-with-shell off\"."
3230 _initialize_infcmd (void)
3232 static struct cmd_list_element *info_proc_cmdlist;
3233 struct cmd_list_element *c = NULL;
3234 const char *cmd_name;
3236 /* Add the filename of the terminal connected to inferior I/O. */
3237 add_setshow_optional_filename_cmd ("inferior-tty", class_run,
3238 &inferior_io_terminal_scratch, _("\
3239 Set terminal for future runs of program being debugged."), _("\
3240 Show terminal for future runs of program being debugged."), _("\
3241 Usage: set inferior-tty [TTY]\n\n\
3242 If TTY is omitted, the default behavior of using the same terminal as GDB\n\
3244 set_inferior_tty_command,
3245 show_inferior_tty_command,
3246 &setlist, &showlist);
3247 cmd_name = "inferior-tty";
3248 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3249 gdb_assert (c != NULL);
3250 add_alias_cmd ("tty", c, class_alias, 0, &cmdlist);
3253 add_setshow_string_noescape_cmd (cmd_name, class_run,
3254 &inferior_args_scratch, _("\
3255 Set argument list to give program being debugged when it is started."), _("\
3256 Show argument list to give program being debugged when it is started."), _("\
3257 Follow this command with any number of args, to be passed to the program."),
3260 &setlist, &showlist);
3261 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3262 gdb_assert (c != NULL);
3263 set_cmd_completer (c, filename_completer);
3265 c = add_cmd ("environment", no_class, environment_info, _("\
3266 The environment to give the program, or one variable's value.\n\
3267 With an argument VAR, prints the value of environment variable VAR to\n\
3268 give the program being debugged. With no arguments, prints the entire\n\
3269 environment to be given to the program."), &showlist);
3270 set_cmd_completer (c, noop_completer);
3272 add_prefix_cmd ("unset", no_class, unset_command,
3273 _("Complement to certain \"set\" commands."),
3274 &unsetlist, "unset ", 0, &cmdlist);
3276 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3277 Cancel environment variable VAR for the program.\n\
3278 This does not affect the program until the next \"run\" command."),
3280 set_cmd_completer (c, noop_completer);
3282 c = add_cmd ("environment", class_run, set_environment_command, _("\
3283 Set environment variable value to give the program.\n\
3284 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3285 VALUES of environment variables are uninterpreted strings.\n\
3286 This does not affect the program until the next \"run\" command."),
3288 set_cmd_completer (c, noop_completer);
3290 c = add_com ("path", class_files, path_command, _("\
3291 Add directory DIR(s) to beginning of search path for object files.\n\
3292 $cwd in the path means the current working directory.\n\
3293 This path is equivalent to the $PATH shell variable. It is a list of\n\
3294 directories, separated by colons. These directories are searched to find\n\
3295 fully linked executable files and separately compiled object files as \
3297 set_cmd_completer (c, filename_completer);
3299 c = add_cmd ("paths", no_class, path_info, _("\
3300 Current search path for finding object files.\n\
3301 $cwd in the path means the current working directory.\n\
3302 This path is equivalent to the $PATH shell variable. It is a list of\n\
3303 directories, separated by colons. These directories are searched to find\n\
3304 fully linked executable files and separately compiled object files as \
3307 set_cmd_completer (c, noop_completer);
3309 add_prefix_cmd ("kill", class_run, kill_command,
3310 _("Kill execution of program being debugged."),
3311 &killlist, "kill ", 0, &cmdlist);
3313 add_com ("attach", class_run, attach_command, _("\
3314 Attach to a process or file outside of GDB.\n\
3315 This command attaches to another target, of the same type as your last\n\
3316 \"target\" command (\"info files\" will show your target stack).\n\
3317 The command may take as argument a process id or a device file.\n\
3318 For a process id, you must have permission to send the process a signal,\n\
3319 and it must have the same effective uid as the debugger.\n\
3320 When using \"attach\" with a process id, the debugger finds the\n\
3321 program running in the process, looking first in the current working\n\
3322 directory, or (if not found there) using the source file search path\n\
3323 (see the \"directory\" command). You can also use the \"file\" command\n\
3324 to specify the program, and to load its symbol table."));
3326 add_prefix_cmd ("detach", class_run, detach_command, _("\
3327 Detach a process or file previously attached.\n\
3328 If a process, it is no longer traced, and it continues its execution. If\n\
3329 you were debugging a file, the file is closed and gdb no longer accesses it."),
3330 &detachlist, "detach ", 0, &cmdlist);
3332 add_com ("disconnect", class_run, disconnect_command, _("\
3333 Disconnect from a target.\n\
3334 The target will wait for another debugger to connect. Not available for\n\
3337 c = add_com ("signal", class_run, signal_command, _("\
3338 Continue program with the specified signal.\n\
3339 Usage: signal SIGNAL\n\
3340 The SIGNAL argument is processed the same as the handle command.\n\
3342 An argument of \"0\" means continue the program without sending it a signal.\n\
3343 This is useful in cases where the program stopped because of a signal,\n\
3344 and you want to resume the program while discarding the signal.\n\
3346 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3347 the current thread only."));
3348 set_cmd_completer (c, signal_completer);
3350 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3351 Queue a signal to be delivered to the current thread when it is resumed.\n\
3352 Usage: queue-signal SIGNAL\n\
3353 The SIGNAL argument is processed the same as the handle command.\n\
3354 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3356 An argument of \"0\" means remove any currently queued signal from\n\
3357 the current thread. This is useful in cases where the program stopped\n\
3358 because of a signal, and you want to resume it while discarding the signal.\n\
3360 In a multi-threaded program the signal is queued with, or discarded from,\n\
3361 the current thread only."));
3362 set_cmd_completer (c, signal_completer);
3364 add_com ("stepi", class_run, stepi_command, _("\
3365 Step one instruction exactly.\n\
3367 Argument N means step N times (or till program stops for another \
3369 add_com_alias ("si", "stepi", class_alias, 0);
3371 add_com ("nexti", class_run, nexti_command, _("\
3372 Step one instruction, but proceed through subroutine calls.\n\
3374 Argument N means step N times (or till program stops for another \
3376 add_com_alias ("ni", "nexti", class_alias, 0);
3378 add_com ("finish", class_run, finish_command, _("\
3379 Execute until selected stack frame returns.\n\
3381 Upon return, the value returned is printed and put in the value history."));
3382 add_com_alias ("fin", "finish", class_run, 1);
3384 add_com ("next", class_run, next_command, _("\
3385 Step program, proceeding through subroutine calls.\n\
3387 Unlike \"step\", if the current source line calls a subroutine,\n\
3388 this command does not enter the subroutine, but instead steps over\n\
3389 the call, in effect treating it as a single source line."));
3390 add_com_alias ("n", "next", class_run, 1);
3392 add_com ("step", class_run, step_command, _("\
3393 Step program until it reaches a different source line.\n\
3395 Argument N means step N times (or till program stops for another \
3397 add_com_alias ("s", "step", class_run, 1);
3399 c = add_com ("until", class_run, until_command, _("\
3400 Execute until the program reaches a source line greater than the current\n\
3401 or a specified location (same args as break command) within the current \
3403 set_cmd_completer (c, location_completer);
3404 add_com_alias ("u", "until", class_run, 1);
3406 c = add_com ("advance", class_run, advance_command, _("\
3407 Continue the program up to the given location (same form as args for break \
3409 Execution will also stop upon exit from the current stack frame."));
3410 set_cmd_completer (c, location_completer);
3412 c = add_com ("jump", class_run, jump_command, _("\
3413 Continue program being debugged at specified line or address.\n\
3414 Usage: jump <location>\n\
3415 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3416 for an address to start at."));
3417 set_cmd_completer (c, location_completer);
3418 add_com_alias ("j", "jump", class_run, 1);
3420 add_com ("continue", class_run, continue_command, _("\
3421 Continue program being debugged, after signal or breakpoint.\n\
3422 Usage: continue [N]\n\
3423 If proceeding from breakpoint, a number N may be used as an argument,\n\
3424 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3425 the breakpoint won't break until the Nth time it is reached).\n\
3427 If non-stop mode is enabled, continue only the current thread,\n\
3428 otherwise all the threads in the program are continued. To \n\
3429 continue all stopped threads in non-stop mode, use the -a option.\n\
3430 Specifying -a and an ignore count simultaneously is an error."));
3431 add_com_alias ("c", "cont", class_run, 1);
3432 add_com_alias ("fg", "cont", class_run, 1);
3434 c = add_com ("run", class_run, run_command, _("\
3435 Start debugged program.\n"
3437 set_cmd_completer (c, filename_completer);
3438 add_com_alias ("r", "run", class_run, 1);
3440 c = add_com ("start", class_run, start_command, _("\
3441 Start the debugged program stopping at the beginning of the main procedure.\n"
3443 set_cmd_completer (c, filename_completer);
3445 c = add_com ("starti", class_run, starti_command, _("\
3446 Start the debugged program stopping at the first instruction.\n"
3448 set_cmd_completer (c, filename_completer);
3450 add_com ("interrupt", class_run, interrupt_command,
3451 _("Interrupt the execution of the debugged program.\n\
3452 If non-stop mode is enabled, interrupt only the current thread,\n\
3453 otherwise all the threads in the program are stopped. To \n\
3454 interrupt all running threads in non-stop mode, use the -a option."));
3456 c = add_info ("registers", info_registers_command, _("\
3457 List of integer registers and their contents, for selected stack frame.\n\
3458 Register name as argument means describe only that register."));
3459 add_info_alias ("r", "registers", 1);
3460 set_cmd_completer (c, reg_or_group_completer);
3462 c = add_info ("all-registers", info_all_registers_command, _("\
3463 List of all registers and their contents, for selected stack frame.\n\
3464 Register name as argument means describe only that register."));
3465 set_cmd_completer (c, reg_or_group_completer);
3467 add_info ("program", info_program_command,
3468 _("Execution status of the program."));
3470 add_info ("float", info_float_command,
3471 _("Print the status of the floating point unit\n"));
3473 add_info ("vector", info_vector_command,
3474 _("Print the status of the vector unit\n"));
3476 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3478 Show /proc process information about any running process.\n\
3479 Specify any process id, or use the program being debugged by default."),
3480 &info_proc_cmdlist, "info proc ",
3481 1/*allow-unknown*/, &infolist);
3483 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3484 List of mapped memory regions."),
3485 &info_proc_cmdlist);
3487 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3488 List process info from /proc/PID/stat."),
3489 &info_proc_cmdlist);
3491 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3492 List process info from /proc/PID/status."),
3493 &info_proc_cmdlist);
3495 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3496 List current working directory of the process."),
3497 &info_proc_cmdlist);
3499 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3500 List command line arguments of the process."),
3501 &info_proc_cmdlist);
3503 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3504 List absolute filename for executable of the process."),
3505 &info_proc_cmdlist);
3507 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3508 List all available /proc info."),
3509 &info_proc_cmdlist);