1 /* Target-struct-independent code to start (run) and stop an inferior
4 Copyright (C) 1986-2014 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "exceptions.h"
28 #include "breakpoint.h"
32 #include "cli/cli-script.h"
34 #include "gdbthread.h"
46 #include "dictionary.h"
48 #include "mi/mi-common.h"
49 #include "event-top.h"
51 #include "record-full.h"
52 #include "inline-frame.h"
54 #include "tracepoint.h"
55 #include "continuations.h"
60 #include "completer.h"
61 #include "target-descriptions.h"
62 #include "target-dcache.h"
64 /* Prototypes for local functions */
66 static void signals_info (char *, int);
68 static void handle_command (char *, int);
70 static void sig_print_info (enum gdb_signal);
72 static void sig_print_header (void);
74 static void resume_cleanups (void *);
76 static int hook_stop_stub (void *);
78 static int restore_selected_frame (void *);
80 static int follow_fork (void);
82 static void set_schedlock_func (char *args, int from_tty,
83 struct cmd_list_element *c);
85 static int currently_stepping (struct thread_info *tp);
87 static void xdb_handle_command (char *args, int from_tty);
89 void _initialize_infrun (void);
91 void nullify_last_target_wait_ptid (void);
93 static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
95 static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
97 static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
99 /* When set, stop the 'step' command if we enter a function which has
100 no line number information. The normal behavior is that we step
101 over such function. */
102 int step_stop_if_no_debug = 0;
104 show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
105 struct cmd_list_element *c, const char *value)
107 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
110 /* In asynchronous mode, but simulating synchronous execution. */
112 int sync_execution = 0;
114 /* proceed and normal_stop use this to notify the user when the
115 inferior stopped in a different thread than it had been running
118 static ptid_t previous_inferior_ptid;
120 /* If set (default for legacy reasons), when following a fork, GDB
121 will detach from one of the fork branches, child or parent.
122 Exactly which branch is detached depends on 'set follow-fork-mode'
125 static int detach_fork = 1;
127 int debug_displaced = 0;
129 show_debug_displaced (struct ui_file *file, int from_tty,
130 struct cmd_list_element *c, const char *value)
132 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
135 unsigned int debug_infrun = 0;
137 show_debug_infrun (struct ui_file *file, int from_tty,
138 struct cmd_list_element *c, const char *value)
140 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
144 /* Support for disabling address space randomization. */
146 int disable_randomization = 1;
149 show_disable_randomization (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
152 if (target_supports_disable_randomization ())
153 fprintf_filtered (file,
154 _("Disabling randomization of debuggee's "
155 "virtual address space is %s.\n"),
158 fputs_filtered (_("Disabling randomization of debuggee's "
159 "virtual address space is unsupported on\n"
160 "this platform.\n"), file);
164 set_disable_randomization (char *args, int from_tty,
165 struct cmd_list_element *c)
167 if (!target_supports_disable_randomization ())
168 error (_("Disabling randomization of debuggee's "
169 "virtual address space is unsupported on\n"
173 /* User interface for non-stop mode. */
176 static int non_stop_1 = 0;
179 set_non_stop (char *args, int from_tty,
180 struct cmd_list_element *c)
182 if (target_has_execution)
184 non_stop_1 = non_stop;
185 error (_("Cannot change this setting while the inferior is running."));
188 non_stop = non_stop_1;
192 show_non_stop (struct ui_file *file, int from_tty,
193 struct cmd_list_element *c, const char *value)
195 fprintf_filtered (file,
196 _("Controlling the inferior in non-stop mode is %s.\n"),
200 /* "Observer mode" is somewhat like a more extreme version of
201 non-stop, in which all GDB operations that might affect the
202 target's execution have been disabled. */
204 int observer_mode = 0;
205 static int observer_mode_1 = 0;
208 set_observer_mode (char *args, int from_tty,
209 struct cmd_list_element *c)
211 if (target_has_execution)
213 observer_mode_1 = observer_mode;
214 error (_("Cannot change this setting while the inferior is running."));
217 observer_mode = observer_mode_1;
219 may_write_registers = !observer_mode;
220 may_write_memory = !observer_mode;
221 may_insert_breakpoints = !observer_mode;
222 may_insert_tracepoints = !observer_mode;
223 /* We can insert fast tracepoints in or out of observer mode,
224 but enable them if we're going into this mode. */
226 may_insert_fast_tracepoints = 1;
227 may_stop = !observer_mode;
228 update_target_permissions ();
230 /* Going *into* observer mode we must force non-stop, then
231 going out we leave it that way. */
234 pagination_enabled = 0;
235 non_stop = non_stop_1 = 1;
239 printf_filtered (_("Observer mode is now %s.\n"),
240 (observer_mode ? "on" : "off"));
244 show_observer_mode (struct ui_file *file, int from_tty,
245 struct cmd_list_element *c, const char *value)
247 fprintf_filtered (file, _("Observer mode is %s.\n"), value);
250 /* This updates the value of observer mode based on changes in
251 permissions. Note that we are deliberately ignoring the values of
252 may-write-registers and may-write-memory, since the user may have
253 reason to enable these during a session, for instance to turn on a
254 debugging-related global. */
257 update_observer_mode (void)
261 newval = (!may_insert_breakpoints
262 && !may_insert_tracepoints
263 && may_insert_fast_tracepoints
267 /* Let the user know if things change. */
268 if (newval != observer_mode)
269 printf_filtered (_("Observer mode is now %s.\n"),
270 (newval ? "on" : "off"));
272 observer_mode = observer_mode_1 = newval;
275 /* Tables of how to react to signals; the user sets them. */
277 static unsigned char *signal_stop;
278 static unsigned char *signal_print;
279 static unsigned char *signal_program;
281 /* Table of signals that are registered with "catch signal". A
282 non-zero entry indicates that the signal is caught by some "catch
283 signal" command. This has size GDB_SIGNAL_LAST, to accommodate all
285 static unsigned char *signal_catch;
287 /* Table of signals that the target may silently handle.
288 This is automatically determined from the flags above,
289 and simply cached here. */
290 static unsigned char *signal_pass;
292 #define SET_SIGS(nsigs,sigs,flags) \
294 int signum = (nsigs); \
295 while (signum-- > 0) \
296 if ((sigs)[signum]) \
297 (flags)[signum] = 1; \
300 #define UNSET_SIGS(nsigs,sigs,flags) \
302 int signum = (nsigs); \
303 while (signum-- > 0) \
304 if ((sigs)[signum]) \
305 (flags)[signum] = 0; \
308 /* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
309 this function is to avoid exporting `signal_program'. */
312 update_signals_program_target (void)
314 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
317 /* Value to pass to target_resume() to cause all threads to resume. */
319 #define RESUME_ALL minus_one_ptid
321 /* Command list pointer for the "stop" placeholder. */
323 static struct cmd_list_element *stop_command;
325 /* Function inferior was in as of last step command. */
327 static struct symbol *step_start_function;
329 /* Nonzero if we want to give control to the user when we're notified
330 of shared library events by the dynamic linker. */
331 int stop_on_solib_events;
333 /* Enable or disable optional shared library event breakpoints
334 as appropriate when the above flag is changed. */
337 set_stop_on_solib_events (char *args, int from_tty, struct cmd_list_element *c)
339 update_solib_breakpoints ();
343 show_stop_on_solib_events (struct ui_file *file, int from_tty,
344 struct cmd_list_element *c, const char *value)
346 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
350 /* Nonzero means expecting a trace trap
351 and should stop the inferior and return silently when it happens. */
355 /* Save register contents here when executing a "finish" command or are
356 about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
357 Thus this contains the return value from the called function (assuming
358 values are returned in a register). */
360 struct regcache *stop_registers;
362 /* Nonzero after stop if current stack frame should be printed. */
364 static int stop_print_frame;
366 /* This is a cached copy of the pid/waitstatus of the last event
367 returned by target_wait()/deprecated_target_wait_hook(). This
368 information is returned by get_last_target_status(). */
369 static ptid_t target_last_wait_ptid;
370 static struct target_waitstatus target_last_waitstatus;
372 static void context_switch (ptid_t ptid);
374 void init_thread_stepping_state (struct thread_info *tss);
376 static void init_infwait_state (void);
378 static const char follow_fork_mode_child[] = "child";
379 static const char follow_fork_mode_parent[] = "parent";
381 static const char *const follow_fork_mode_kind_names[] = {
382 follow_fork_mode_child,
383 follow_fork_mode_parent,
387 static const char *follow_fork_mode_string = follow_fork_mode_parent;
389 show_follow_fork_mode_string (struct ui_file *file, int from_tty,
390 struct cmd_list_element *c, const char *value)
392 fprintf_filtered (file,
393 _("Debugger response to a program "
394 "call of fork or vfork is \"%s\".\n"),
399 /* Tell the target to follow the fork we're stopped at. Returns true
400 if the inferior should be resumed; false, if the target for some
401 reason decided it's best not to resume. */
406 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
407 int should_resume = 1;
408 struct thread_info *tp;
410 /* Copy user stepping state to the new inferior thread. FIXME: the
411 followed fork child thread should have a copy of most of the
412 parent thread structure's run control related fields, not just these.
413 Initialized to avoid "may be used uninitialized" warnings from gcc. */
414 struct breakpoint *step_resume_breakpoint = NULL;
415 struct breakpoint *exception_resume_breakpoint = NULL;
416 CORE_ADDR step_range_start = 0;
417 CORE_ADDR step_range_end = 0;
418 struct frame_id step_frame_id = { 0 };
419 struct interp *command_interp = NULL;
424 struct target_waitstatus wait_status;
426 /* Get the last target status returned by target_wait(). */
427 get_last_target_status (&wait_ptid, &wait_status);
429 /* If not stopped at a fork event, then there's nothing else to
431 if (wait_status.kind != TARGET_WAITKIND_FORKED
432 && wait_status.kind != TARGET_WAITKIND_VFORKED)
435 /* Check if we switched over from WAIT_PTID, since the event was
437 if (!ptid_equal (wait_ptid, minus_one_ptid)
438 && !ptid_equal (inferior_ptid, wait_ptid))
440 /* We did. Switch back to WAIT_PTID thread, to tell the
441 target to follow it (in either direction). We'll
442 afterwards refuse to resume, and inform the user what
444 switch_to_thread (wait_ptid);
449 tp = inferior_thread ();
451 /* If there were any forks/vforks that were caught and are now to be
452 followed, then do so now. */
453 switch (tp->pending_follow.kind)
455 case TARGET_WAITKIND_FORKED:
456 case TARGET_WAITKIND_VFORKED:
458 ptid_t parent, child;
460 /* If the user did a next/step, etc, over a fork call,
461 preserve the stepping state in the fork child. */
462 if (follow_child && should_resume)
464 step_resume_breakpoint = clone_momentary_breakpoint
465 (tp->control.step_resume_breakpoint);
466 step_range_start = tp->control.step_range_start;
467 step_range_end = tp->control.step_range_end;
468 step_frame_id = tp->control.step_frame_id;
469 exception_resume_breakpoint
470 = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
471 command_interp = tp->control.command_interp;
473 /* For now, delete the parent's sr breakpoint, otherwise,
474 parent/child sr breakpoints are considered duplicates,
475 and the child version will not be installed. Remove
476 this when the breakpoints module becomes aware of
477 inferiors and address spaces. */
478 delete_step_resume_breakpoint (tp);
479 tp->control.step_range_start = 0;
480 tp->control.step_range_end = 0;
481 tp->control.step_frame_id = null_frame_id;
482 delete_exception_resume_breakpoint (tp);
483 tp->control.command_interp = NULL;
486 parent = inferior_ptid;
487 child = tp->pending_follow.value.related_pid;
489 /* Tell the target to do whatever is necessary to follow
490 either parent or child. */
491 if (target_follow_fork (follow_child, detach_fork))
493 /* Target refused to follow, or there's some other reason
494 we shouldn't resume. */
499 /* This pending follow fork event is now handled, one way
500 or another. The previous selected thread may be gone
501 from the lists by now, but if it is still around, need
502 to clear the pending follow request. */
503 tp = find_thread_ptid (parent);
505 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
507 /* This makes sure we don't try to apply the "Switched
508 over from WAIT_PID" logic above. */
509 nullify_last_target_wait_ptid ();
511 /* If we followed the child, switch to it... */
514 switch_to_thread (child);
516 /* ... and preserve the stepping state, in case the
517 user was stepping over the fork call. */
520 tp = inferior_thread ();
521 tp->control.step_resume_breakpoint
522 = step_resume_breakpoint;
523 tp->control.step_range_start = step_range_start;
524 tp->control.step_range_end = step_range_end;
525 tp->control.step_frame_id = step_frame_id;
526 tp->control.exception_resume_breakpoint
527 = exception_resume_breakpoint;
528 tp->control.command_interp = command_interp;
532 /* If we get here, it was because we're trying to
533 resume from a fork catchpoint, but, the user
534 has switched threads away from the thread that
535 forked. In that case, the resume command
536 issued is most likely not applicable to the
537 child, so just warn, and refuse to resume. */
538 warning (_("Not resuming: switched threads "
539 "before following fork child.\n"));
542 /* Reset breakpoints in the child as appropriate. */
543 follow_inferior_reset_breakpoints ();
546 switch_to_thread (parent);
550 case TARGET_WAITKIND_SPURIOUS:
551 /* Nothing to follow. */
554 internal_error (__FILE__, __LINE__,
555 "Unexpected pending_follow.kind %d\n",
556 tp->pending_follow.kind);
560 return should_resume;
564 follow_inferior_reset_breakpoints (void)
566 struct thread_info *tp = inferior_thread ();
568 /* Was there a step_resume breakpoint? (There was if the user
569 did a "next" at the fork() call.) If so, explicitly reset its
570 thread number. Cloned step_resume breakpoints are disabled on
571 creation, so enable it here now that it is associated with the
574 step_resumes are a form of bp that are made to be per-thread.
575 Since we created the step_resume bp when the parent process
576 was being debugged, and now are switching to the child process,
577 from the breakpoint package's viewpoint, that's a switch of
578 "threads". We must update the bp's notion of which thread
579 it is for, or it'll be ignored when it triggers. */
581 if (tp->control.step_resume_breakpoint)
583 breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
584 tp->control.step_resume_breakpoint->loc->enabled = 1;
587 /* Treat exception_resume breakpoints like step_resume breakpoints. */
588 if (tp->control.exception_resume_breakpoint)
590 breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
591 tp->control.exception_resume_breakpoint->loc->enabled = 1;
594 /* Reinsert all breakpoints in the child. The user may have set
595 breakpoints after catching the fork, in which case those
596 were never set in the child, but only in the parent. This makes
597 sure the inserted breakpoints match the breakpoint list. */
599 breakpoint_re_set ();
600 insert_breakpoints ();
603 /* The child has exited or execed: resume threads of the parent the
604 user wanted to be executing. */
607 proceed_after_vfork_done (struct thread_info *thread,
610 int pid = * (int *) arg;
612 if (ptid_get_pid (thread->ptid) == pid
613 && is_running (thread->ptid)
614 && !is_executing (thread->ptid)
615 && !thread->stop_requested
616 && thread->suspend.stop_signal == GDB_SIGNAL_0)
619 fprintf_unfiltered (gdb_stdlog,
620 "infrun: resuming vfork parent thread %s\n",
621 target_pid_to_str (thread->ptid));
623 switch_to_thread (thread->ptid);
624 clear_proceed_status (0);
625 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0);
631 /* Called whenever we notice an exec or exit event, to handle
632 detaching or resuming a vfork parent. */
635 handle_vfork_child_exec_or_exit (int exec)
637 struct inferior *inf = current_inferior ();
639 if (inf->vfork_parent)
641 int resume_parent = -1;
643 /* This exec or exit marks the end of the shared memory region
644 between the parent and the child. If the user wanted to
645 detach from the parent, now is the time. */
647 if (inf->vfork_parent->pending_detach)
649 struct thread_info *tp;
650 struct cleanup *old_chain;
651 struct program_space *pspace;
652 struct address_space *aspace;
654 /* follow-fork child, detach-on-fork on. */
656 inf->vfork_parent->pending_detach = 0;
660 /* If we're handling a child exit, then inferior_ptid
661 points at the inferior's pid, not to a thread. */
662 old_chain = save_inferior_ptid ();
663 save_current_program_space ();
664 save_current_inferior ();
667 old_chain = save_current_space_and_thread ();
669 /* We're letting loose of the parent. */
670 tp = any_live_thread_of_process (inf->vfork_parent->pid);
671 switch_to_thread (tp->ptid);
673 /* We're about to detach from the parent, which implicitly
674 removes breakpoints from its address space. There's a
675 catch here: we want to reuse the spaces for the child,
676 but, parent/child are still sharing the pspace at this
677 point, although the exec in reality makes the kernel give
678 the child a fresh set of new pages. The problem here is
679 that the breakpoints module being unaware of this, would
680 likely chose the child process to write to the parent
681 address space. Swapping the child temporarily away from
682 the spaces has the desired effect. Yes, this is "sort
685 pspace = inf->pspace;
686 aspace = inf->aspace;
690 if (debug_infrun || info_verbose)
692 target_terminal_ours ();
695 fprintf_filtered (gdb_stdlog,
696 "Detaching vfork parent process "
697 "%d after child exec.\n",
698 inf->vfork_parent->pid);
700 fprintf_filtered (gdb_stdlog,
701 "Detaching vfork parent process "
702 "%d after child exit.\n",
703 inf->vfork_parent->pid);
706 target_detach (NULL, 0);
709 inf->pspace = pspace;
710 inf->aspace = aspace;
712 do_cleanups (old_chain);
716 /* We're staying attached to the parent, so, really give the
717 child a new address space. */
718 inf->pspace = add_program_space (maybe_new_address_space ());
719 inf->aspace = inf->pspace->aspace;
721 set_current_program_space (inf->pspace);
723 resume_parent = inf->vfork_parent->pid;
725 /* Break the bonds. */
726 inf->vfork_parent->vfork_child = NULL;
730 struct cleanup *old_chain;
731 struct program_space *pspace;
733 /* If this is a vfork child exiting, then the pspace and
734 aspaces were shared with the parent. Since we're
735 reporting the process exit, we'll be mourning all that is
736 found in the address space, and switching to null_ptid,
737 preparing to start a new inferior. But, since we don't
738 want to clobber the parent's address/program spaces, we
739 go ahead and create a new one for this exiting
742 /* Switch to null_ptid, so that clone_program_space doesn't want
743 to read the selected frame of a dead process. */
744 old_chain = save_inferior_ptid ();
745 inferior_ptid = null_ptid;
747 /* This inferior is dead, so avoid giving the breakpoints
748 module the option to write through to it (cloning a
749 program space resets breakpoints). */
752 pspace = add_program_space (maybe_new_address_space ());
753 set_current_program_space (pspace);
755 inf->symfile_flags = SYMFILE_NO_READ;
756 clone_program_space (pspace, inf->vfork_parent->pspace);
757 inf->pspace = pspace;
758 inf->aspace = pspace->aspace;
760 /* Put back inferior_ptid. We'll continue mourning this
762 do_cleanups (old_chain);
764 resume_parent = inf->vfork_parent->pid;
765 /* Break the bonds. */
766 inf->vfork_parent->vfork_child = NULL;
769 inf->vfork_parent = NULL;
771 gdb_assert (current_program_space == inf->pspace);
773 if (non_stop && resume_parent != -1)
775 /* If the user wanted the parent to be running, let it go
777 struct cleanup *old_chain = make_cleanup_restore_current_thread ();
780 fprintf_unfiltered (gdb_stdlog,
781 "infrun: resuming vfork parent process %d\n",
784 iterate_over_threads (proceed_after_vfork_done, &resume_parent);
786 do_cleanups (old_chain);
791 /* Enum strings for "set|show follow-exec-mode". */
793 static const char follow_exec_mode_new[] = "new";
794 static const char follow_exec_mode_same[] = "same";
795 static const char *const follow_exec_mode_names[] =
797 follow_exec_mode_new,
798 follow_exec_mode_same,
802 static const char *follow_exec_mode_string = follow_exec_mode_same;
804 show_follow_exec_mode_string (struct ui_file *file, int from_tty,
805 struct cmd_list_element *c, const char *value)
807 fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"), value);
810 /* EXECD_PATHNAME is assumed to be non-NULL. */
813 follow_exec (ptid_t pid, char *execd_pathname)
815 struct thread_info *th = inferior_thread ();
816 struct inferior *inf = current_inferior ();
818 /* This is an exec event that we actually wish to pay attention to.
819 Refresh our symbol table to the newly exec'd program, remove any
822 If there are breakpoints, they aren't really inserted now,
823 since the exec() transformed our inferior into a fresh set
826 We want to preserve symbolic breakpoints on the list, since
827 we have hopes that they can be reset after the new a.out's
828 symbol table is read.
830 However, any "raw" breakpoints must be removed from the list
831 (e.g., the solib bp's), since their address is probably invalid
834 And, we DON'T want to call delete_breakpoints() here, since
835 that may write the bp's "shadow contents" (the instruction
836 value that was overwritten witha TRAP instruction). Since
837 we now have a new a.out, those shadow contents aren't valid. */
839 mark_breakpoints_out ();
841 update_breakpoints_after_exec ();
843 /* If there was one, it's gone now. We cannot truly step-to-next
844 statement through an exec(). */
845 th->control.step_resume_breakpoint = NULL;
846 th->control.exception_resume_breakpoint = NULL;
847 th->control.step_range_start = 0;
848 th->control.step_range_end = 0;
850 /* The target reports the exec event to the main thread, even if
851 some other thread does the exec, and even if the main thread was
852 already stopped --- if debugging in non-stop mode, it's possible
853 the user had the main thread held stopped in the previous image
854 --- release it now. This is the same behavior as step-over-exec
855 with scheduler-locking on in all-stop mode. */
856 th->stop_requested = 0;
858 /* What is this a.out's name? */
859 printf_unfiltered (_("%s is executing new program: %s\n"),
860 target_pid_to_str (inferior_ptid),
863 /* We've followed the inferior through an exec. Therefore, the
864 inferior has essentially been killed & reborn. */
866 gdb_flush (gdb_stdout);
868 breakpoint_init_inferior (inf_execd);
870 if (gdb_sysroot && *gdb_sysroot)
872 char *name = alloca (strlen (gdb_sysroot)
873 + strlen (execd_pathname)
876 strcpy (name, gdb_sysroot);
877 strcat (name, execd_pathname);
878 execd_pathname = name;
881 /* Reset the shared library package. This ensures that we get a
882 shlib event when the child reaches "_start", at which point the
883 dld will have had a chance to initialize the child. */
884 /* Also, loading a symbol file below may trigger symbol lookups, and
885 we don't want those to be satisfied by the libraries of the
886 previous incarnation of this process. */
887 no_shared_libraries (NULL, 0);
889 if (follow_exec_mode_string == follow_exec_mode_new)
891 struct program_space *pspace;
893 /* The user wants to keep the old inferior and program spaces
894 around. Create a new fresh one, and switch to it. */
896 inf = add_inferior (current_inferior ()->pid);
897 pspace = add_program_space (maybe_new_address_space ());
898 inf->pspace = pspace;
899 inf->aspace = pspace->aspace;
901 exit_inferior_num_silent (current_inferior ()->num);
903 set_current_inferior (inf);
904 set_current_program_space (pspace);
908 /* The old description may no longer be fit for the new image.
909 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
910 old description; we'll read a new one below. No need to do
911 this on "follow-exec-mode new", as the old inferior stays
912 around (its description is later cleared/refetched on
914 target_clear_description ();
917 gdb_assert (current_program_space == inf->pspace);
919 /* That a.out is now the one to use. */
920 exec_file_attach (execd_pathname, 0);
922 /* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
923 (Position Independent Executable) main symbol file will get applied by
924 solib_create_inferior_hook below. breakpoint_re_set would fail to insert
925 the breakpoints with the zero displacement. */
927 symbol_file_add (execd_pathname,
929 | SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET),
932 if ((inf->symfile_flags & SYMFILE_NO_READ) == 0)
933 set_initial_language ();
935 /* If the target can specify a description, read it. Must do this
936 after flipping to the new executable (because the target supplied
937 description must be compatible with the executable's
938 architecture, and the old executable may e.g., be 32-bit, while
939 the new one 64-bit), and before anything involving memory or
941 target_find_description ();
943 solib_create_inferior_hook (0);
945 jit_inferior_created_hook ();
947 breakpoint_re_set ();
949 /* Reinsert all breakpoints. (Those which were symbolic have
950 been reset to the proper address in the new a.out, thanks
951 to symbol_file_command...). */
952 insert_breakpoints ();
954 /* The next resume of this inferior should bring it to the shlib
955 startup breakpoints. (If the user had also set bp's on
956 "main" from the old (parent) process, then they'll auto-
957 matically get reset there in the new process.). */
960 /* Non-zero if we just simulating a single-step. This is needed
961 because we cannot remove the breakpoints in the inferior process
962 until after the `wait' in `wait_for_inferior'. */
963 static int singlestep_breakpoints_inserted_p = 0;
965 /* The thread we inserted single-step breakpoints for. */
966 static ptid_t singlestep_ptid;
968 /* PC when we started this single-step. */
969 static CORE_ADDR singlestep_pc;
971 /* Info about an instruction that is being stepped over. Invalid if
974 struct step_over_info
976 /* The instruction's address space. */
977 struct address_space *aspace;
979 /* The instruction's address. */
983 /* The step-over info of the location that is being stepped over.
985 Note that with async/breakpoint always-inserted mode, a user might
986 set a new breakpoint/watchpoint/etc. exactly while a breakpoint is
987 being stepped over. As setting a new breakpoint inserts all
988 breakpoints, we need to make sure the breakpoint being stepped over
989 isn't inserted then. We do that by only clearing the step-over
990 info when the step-over is actually finished (or aborted).
992 Presently GDB can only step over one breakpoint at any given time.
993 Given threads that can't run code in the same address space as the
994 breakpoint's can't really miss the breakpoint, GDB could be taught
995 to step-over at most one breakpoint per address space (so this info
996 could move to the address space object if/when GDB is extended).
997 The set of breakpoints being stepped over will normally be much
998 smaller than the set of all breakpoints, so a flag in the
999 breakpoint location structure would be wasteful. A separate list
1000 also saves complexity and run-time, as otherwise we'd have to go
1001 through all breakpoint locations clearing their flag whenever we
1002 start a new sequence. Similar considerations weigh against storing
1003 this info in the thread object. Plus, not all step overs actually
1004 have breakpoint locations -- e.g., stepping past a single-step
1005 breakpoint, or stepping to complete a non-continuable
1007 static struct step_over_info step_over_info;
1009 /* Record the address of the breakpoint/instruction we're currently
1013 set_step_over_info (struct address_space *aspace, CORE_ADDR address)
1015 step_over_info.aspace = aspace;
1016 step_over_info.address = address;
1019 /* Called when we're not longer stepping over a breakpoint / an
1020 instruction, so all breakpoints are free to be (re)inserted. */
1023 clear_step_over_info (void)
1025 step_over_info.aspace = NULL;
1026 step_over_info.address = 0;
1029 /* See inferior.h. */
1032 stepping_past_instruction_at (struct address_space *aspace,
1035 return (step_over_info.aspace != NULL
1036 && breakpoint_address_match (aspace, address,
1037 step_over_info.aspace,
1038 step_over_info.address));
1042 /* Displaced stepping. */
1044 /* In non-stop debugging mode, we must take special care to manage
1045 breakpoints properly; in particular, the traditional strategy for
1046 stepping a thread past a breakpoint it has hit is unsuitable.
1047 'Displaced stepping' is a tactic for stepping one thread past a
1048 breakpoint it has hit while ensuring that other threads running
1049 concurrently will hit the breakpoint as they should.
1051 The traditional way to step a thread T off a breakpoint in a
1052 multi-threaded program in all-stop mode is as follows:
1054 a0) Initially, all threads are stopped, and breakpoints are not
1056 a1) We single-step T, leaving breakpoints uninserted.
1057 a2) We insert breakpoints, and resume all threads.
1059 In non-stop debugging, however, this strategy is unsuitable: we
1060 don't want to have to stop all threads in the system in order to
1061 continue or step T past a breakpoint. Instead, we use displaced
1064 n0) Initially, T is stopped, other threads are running, and
1065 breakpoints are inserted.
1066 n1) We copy the instruction "under" the breakpoint to a separate
1067 location, outside the main code stream, making any adjustments
1068 to the instruction, register, and memory state as directed by
1070 n2) We single-step T over the instruction at its new location.
1071 n3) We adjust the resulting register and memory state as directed
1072 by T's architecture. This includes resetting T's PC to point
1073 back into the main instruction stream.
1076 This approach depends on the following gdbarch methods:
1078 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1079 indicate where to copy the instruction, and how much space must
1080 be reserved there. We use these in step n1.
1082 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1083 address, and makes any necessary adjustments to the instruction,
1084 register contents, and memory. We use this in step n1.
1086 - gdbarch_displaced_step_fixup adjusts registers and memory after
1087 we have successfuly single-stepped the instruction, to yield the
1088 same effect the instruction would have had if we had executed it
1089 at its original address. We use this in step n3.
1091 - gdbarch_displaced_step_free_closure provides cleanup.
1093 The gdbarch_displaced_step_copy_insn and
1094 gdbarch_displaced_step_fixup functions must be written so that
1095 copying an instruction with gdbarch_displaced_step_copy_insn,
1096 single-stepping across the copied instruction, and then applying
1097 gdbarch_displaced_insn_fixup should have the same effects on the
1098 thread's memory and registers as stepping the instruction in place
1099 would have. Exactly which responsibilities fall to the copy and
1100 which fall to the fixup is up to the author of those functions.
1102 See the comments in gdbarch.sh for details.
1104 Note that displaced stepping and software single-step cannot
1105 currently be used in combination, although with some care I think
1106 they could be made to. Software single-step works by placing
1107 breakpoints on all possible subsequent instructions; if the
1108 displaced instruction is a PC-relative jump, those breakpoints
1109 could fall in very strange places --- on pages that aren't
1110 executable, or at addresses that are not proper instruction
1111 boundaries. (We do generally let other threads run while we wait
1112 to hit the software single-step breakpoint, and they might
1113 encounter such a corrupted instruction.) One way to work around
1114 this would be to have gdbarch_displaced_step_copy_insn fully
1115 simulate the effect of PC-relative instructions (and return NULL)
1116 on architectures that use software single-stepping.
1118 In non-stop mode, we can have independent and simultaneous step
1119 requests, so more than one thread may need to simultaneously step
1120 over a breakpoint. The current implementation assumes there is
1121 only one scratch space per process. In this case, we have to
1122 serialize access to the scratch space. If thread A wants to step
1123 over a breakpoint, but we are currently waiting for some other
1124 thread to complete a displaced step, we leave thread A stopped and
1125 place it in the displaced_step_request_queue. Whenever a displaced
1126 step finishes, we pick the next thread in the queue and start a new
1127 displaced step operation on it. See displaced_step_prepare and
1128 displaced_step_fixup for details. */
1130 struct displaced_step_request
1133 struct displaced_step_request *next;
1136 /* Per-inferior displaced stepping state. */
1137 struct displaced_step_inferior_state
1139 /* Pointer to next in linked list. */
1140 struct displaced_step_inferior_state *next;
1142 /* The process this displaced step state refers to. */
1145 /* A queue of pending displaced stepping requests. One entry per
1146 thread that needs to do a displaced step. */
1147 struct displaced_step_request *step_request_queue;
1149 /* If this is not null_ptid, this is the thread carrying out a
1150 displaced single-step in process PID. This thread's state will
1151 require fixing up once it has completed its step. */
1154 /* The architecture the thread had when we stepped it. */
1155 struct gdbarch *step_gdbarch;
1157 /* The closure provided gdbarch_displaced_step_copy_insn, to be used
1158 for post-step cleanup. */
1159 struct displaced_step_closure *step_closure;
1161 /* The address of the original instruction, and the copy we
1163 CORE_ADDR step_original, step_copy;
1165 /* Saved contents of copy area. */
1166 gdb_byte *step_saved_copy;
1169 /* The list of states of processes involved in displaced stepping
1171 static struct displaced_step_inferior_state *displaced_step_inferior_states;
1173 /* Get the displaced stepping state of process PID. */
1175 static struct displaced_step_inferior_state *
1176 get_displaced_stepping_state (int pid)
1178 struct displaced_step_inferior_state *state;
1180 for (state = displaced_step_inferior_states;
1182 state = state->next)
1183 if (state->pid == pid)
1189 /* Add a new displaced stepping state for process PID to the displaced
1190 stepping state list, or return a pointer to an already existing
1191 entry, if it already exists. Never returns NULL. */
1193 static struct displaced_step_inferior_state *
1194 add_displaced_stepping_state (int pid)
1196 struct displaced_step_inferior_state *state;
1198 for (state = displaced_step_inferior_states;
1200 state = state->next)
1201 if (state->pid == pid)
1204 state = xcalloc (1, sizeof (*state));
1206 state->next = displaced_step_inferior_states;
1207 displaced_step_inferior_states = state;
1212 /* If inferior is in displaced stepping, and ADDR equals to starting address
1213 of copy area, return corresponding displaced_step_closure. Otherwise,
1216 struct displaced_step_closure*
1217 get_displaced_step_closure_by_addr (CORE_ADDR addr)
1219 struct displaced_step_inferior_state *displaced
1220 = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1222 /* If checking the mode of displaced instruction in copy area. */
1223 if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
1224 && (displaced->step_copy == addr))
1225 return displaced->step_closure;
1230 /* Remove the displaced stepping state of process PID. */
1233 remove_displaced_stepping_state (int pid)
1235 struct displaced_step_inferior_state *it, **prev_next_p;
1237 gdb_assert (pid != 0);
1239 it = displaced_step_inferior_states;
1240 prev_next_p = &displaced_step_inferior_states;
1245 *prev_next_p = it->next;
1250 prev_next_p = &it->next;
1256 infrun_inferior_exit (struct inferior *inf)
1258 remove_displaced_stepping_state (inf->pid);
1261 /* If ON, and the architecture supports it, GDB will use displaced
1262 stepping to step over breakpoints. If OFF, or if the architecture
1263 doesn't support it, GDB will instead use the traditional
1264 hold-and-step approach. If AUTO (which is the default), GDB will
1265 decide which technique to use to step over breakpoints depending on
1266 which of all-stop or non-stop mode is active --- displaced stepping
1267 in non-stop mode; hold-and-step in all-stop mode. */
1269 static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
1272 show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1273 struct cmd_list_element *c,
1276 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
1277 fprintf_filtered (file,
1278 _("Debugger's willingness to use displaced stepping "
1279 "to step over breakpoints is %s (currently %s).\n"),
1280 value, non_stop ? "on" : "off");
1282 fprintf_filtered (file,
1283 _("Debugger's willingness to use displaced stepping "
1284 "to step over breakpoints is %s.\n"), value);
1287 /* Return non-zero if displaced stepping can/should be used to step
1288 over breakpoints. */
1291 use_displaced_stepping (struct gdbarch *gdbarch)
1293 return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO && non_stop)
1294 || can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
1295 && gdbarch_displaced_step_copy_insn_p (gdbarch)
1296 && find_record_target () == NULL);
1299 /* Clean out any stray displaced stepping state. */
1301 displaced_step_clear (struct displaced_step_inferior_state *displaced)
1303 /* Indicate that there is no cleanup pending. */
1304 displaced->step_ptid = null_ptid;
1306 if (displaced->step_closure)
1308 gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
1309 displaced->step_closure);
1310 displaced->step_closure = NULL;
1315 displaced_step_clear_cleanup (void *arg)
1317 struct displaced_step_inferior_state *state = arg;
1319 displaced_step_clear (state);
1322 /* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
1324 displaced_step_dump_bytes (struct ui_file *file,
1325 const gdb_byte *buf,
1330 for (i = 0; i < len; i++)
1331 fprintf_unfiltered (file, "%02x ", buf[i]);
1332 fputs_unfiltered ("\n", file);
1335 /* Prepare to single-step, using displaced stepping.
1337 Note that we cannot use displaced stepping when we have a signal to
1338 deliver. If we have a signal to deliver and an instruction to step
1339 over, then after the step, there will be no indication from the
1340 target whether the thread entered a signal handler or ignored the
1341 signal and stepped over the instruction successfully --- both cases
1342 result in a simple SIGTRAP. In the first case we mustn't do a
1343 fixup, and in the second case we must --- but we can't tell which.
1344 Comments in the code for 'random signals' in handle_inferior_event
1345 explain how we handle this case instead.
1347 Returns 1 if preparing was successful -- this thread is going to be
1348 stepped now; or 0 if displaced stepping this thread got queued. */
1350 displaced_step_prepare (ptid_t ptid)
1352 struct cleanup *old_cleanups, *ignore_cleanups;
1353 struct thread_info *tp = find_thread_ptid (ptid);
1354 struct regcache *regcache = get_thread_regcache (ptid);
1355 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1356 CORE_ADDR original, copy;
1358 struct displaced_step_closure *closure;
1359 struct displaced_step_inferior_state *displaced;
1362 /* We should never reach this function if the architecture does not
1363 support displaced stepping. */
1364 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
1366 /* Disable range stepping while executing in the scratch pad. We
1367 want a single-step even if executing the displaced instruction in
1368 the scratch buffer lands within the stepping range (e.g., a
1370 tp->control.may_range_step = 0;
1372 /* We have to displaced step one thread at a time, as we only have
1373 access to a single scratch space per inferior. */
1375 displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
1377 if (!ptid_equal (displaced->step_ptid, null_ptid))
1379 /* Already waiting for a displaced step to finish. Defer this
1380 request and place in queue. */
1381 struct displaced_step_request *req, *new_req;
1383 if (debug_displaced)
1384 fprintf_unfiltered (gdb_stdlog,
1385 "displaced: defering step of %s\n",
1386 target_pid_to_str (ptid));
1388 new_req = xmalloc (sizeof (*new_req));
1389 new_req->ptid = ptid;
1390 new_req->next = NULL;
1392 if (displaced->step_request_queue)
1394 for (req = displaced->step_request_queue;
1398 req->next = new_req;
1401 displaced->step_request_queue = new_req;
1407 if (debug_displaced)
1408 fprintf_unfiltered (gdb_stdlog,
1409 "displaced: stepping %s now\n",
1410 target_pid_to_str (ptid));
1413 displaced_step_clear (displaced);
1415 old_cleanups = save_inferior_ptid ();
1416 inferior_ptid = ptid;
1418 original = regcache_read_pc (regcache);
1420 copy = gdbarch_displaced_step_location (gdbarch);
1421 len = gdbarch_max_insn_length (gdbarch);
1423 /* Save the original contents of the copy area. */
1424 displaced->step_saved_copy = xmalloc (len);
1425 ignore_cleanups = make_cleanup (free_current_contents,
1426 &displaced->step_saved_copy);
1427 status = target_read_memory (copy, displaced->step_saved_copy, len);
1429 throw_error (MEMORY_ERROR,
1430 _("Error accessing memory address %s (%s) for "
1431 "displaced-stepping scratch space."),
1432 paddress (gdbarch, copy), safe_strerror (status));
1433 if (debug_displaced)
1435 fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
1436 paddress (gdbarch, copy));
1437 displaced_step_dump_bytes (gdb_stdlog,
1438 displaced->step_saved_copy,
1442 closure = gdbarch_displaced_step_copy_insn (gdbarch,
1443 original, copy, regcache);
1445 /* We don't support the fully-simulated case at present. */
1446 gdb_assert (closure);
1448 /* Save the information we need to fix things up if the step
1450 displaced->step_ptid = ptid;
1451 displaced->step_gdbarch = gdbarch;
1452 displaced->step_closure = closure;
1453 displaced->step_original = original;
1454 displaced->step_copy = copy;
1456 make_cleanup (displaced_step_clear_cleanup, displaced);
1458 /* Resume execution at the copy. */
1459 regcache_write_pc (regcache, copy);
1461 discard_cleanups (ignore_cleanups);
1463 do_cleanups (old_cleanups);
1465 if (debug_displaced)
1466 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
1467 paddress (gdbarch, copy));
1473 write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
1474 const gdb_byte *myaddr, int len)
1476 struct cleanup *ptid_cleanup = save_inferior_ptid ();
1478 inferior_ptid = ptid;
1479 write_memory (memaddr, myaddr, len);
1480 do_cleanups (ptid_cleanup);
1483 /* Restore the contents of the copy area for thread PTID. */
1486 displaced_step_restore (struct displaced_step_inferior_state *displaced,
1489 ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
1491 write_memory_ptid (ptid, displaced->step_copy,
1492 displaced->step_saved_copy, len);
1493 if (debug_displaced)
1494 fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
1495 target_pid_to_str (ptid),
1496 paddress (displaced->step_gdbarch,
1497 displaced->step_copy));
1501 displaced_step_fixup (ptid_t event_ptid, enum gdb_signal signal)
1503 struct cleanup *old_cleanups;
1504 struct displaced_step_inferior_state *displaced
1505 = get_displaced_stepping_state (ptid_get_pid (event_ptid));
1507 /* Was any thread of this process doing a displaced step? */
1508 if (displaced == NULL)
1511 /* Was this event for the pid we displaced? */
1512 if (ptid_equal (displaced->step_ptid, null_ptid)
1513 || ! ptid_equal (displaced->step_ptid, event_ptid))
1516 old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
1518 displaced_step_restore (displaced, displaced->step_ptid);
1520 /* Did the instruction complete successfully? */
1521 if (signal == GDB_SIGNAL_TRAP)
1523 /* Fix up the resulting state. */
1524 gdbarch_displaced_step_fixup (displaced->step_gdbarch,
1525 displaced->step_closure,
1526 displaced->step_original,
1527 displaced->step_copy,
1528 get_thread_regcache (displaced->step_ptid));
1532 /* Since the instruction didn't complete, all we can do is
1534 struct regcache *regcache = get_thread_regcache (event_ptid);
1535 CORE_ADDR pc = regcache_read_pc (regcache);
1537 pc = displaced->step_original + (pc - displaced->step_copy);
1538 regcache_write_pc (regcache, pc);
1541 do_cleanups (old_cleanups);
1543 displaced->step_ptid = null_ptid;
1545 /* Are there any pending displaced stepping requests? If so, run
1546 one now. Leave the state object around, since we're likely to
1547 need it again soon. */
1548 while (displaced->step_request_queue)
1550 struct displaced_step_request *head;
1552 struct regcache *regcache;
1553 struct gdbarch *gdbarch;
1554 CORE_ADDR actual_pc;
1555 struct address_space *aspace;
1557 head = displaced->step_request_queue;
1559 displaced->step_request_queue = head->next;
1562 context_switch (ptid);
1564 regcache = get_thread_regcache (ptid);
1565 actual_pc = regcache_read_pc (regcache);
1566 aspace = get_regcache_aspace (regcache);
1568 if (breakpoint_here_p (aspace, actual_pc))
1570 if (debug_displaced)
1571 fprintf_unfiltered (gdb_stdlog,
1572 "displaced: stepping queued %s now\n",
1573 target_pid_to_str (ptid));
1575 displaced_step_prepare (ptid);
1577 gdbarch = get_regcache_arch (regcache);
1579 if (debug_displaced)
1581 CORE_ADDR actual_pc = regcache_read_pc (regcache);
1584 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1585 paddress (gdbarch, actual_pc));
1586 read_memory (actual_pc, buf, sizeof (buf));
1587 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1590 if (gdbarch_displaced_step_hw_singlestep (gdbarch,
1591 displaced->step_closure))
1592 target_resume (ptid, 1, GDB_SIGNAL_0);
1594 target_resume (ptid, 0, GDB_SIGNAL_0);
1596 /* Done, we're stepping a thread. */
1602 struct thread_info *tp = inferior_thread ();
1604 /* The breakpoint we were sitting under has since been
1606 tp->control.trap_expected = 0;
1608 /* Go back to what we were trying to do. */
1609 step = currently_stepping (tp);
1611 if (debug_displaced)
1612 fprintf_unfiltered (gdb_stdlog,
1613 "displaced: breakpoint is gone: %s, step(%d)\n",
1614 target_pid_to_str (tp->ptid), step);
1616 target_resume (ptid, step, GDB_SIGNAL_0);
1617 tp->suspend.stop_signal = GDB_SIGNAL_0;
1619 /* This request was discarded. See if there's any other
1620 thread waiting for its turn. */
1625 /* Update global variables holding ptids to hold NEW_PTID if they were
1626 holding OLD_PTID. */
1628 infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1630 struct displaced_step_request *it;
1631 struct displaced_step_inferior_state *displaced;
1633 if (ptid_equal (inferior_ptid, old_ptid))
1634 inferior_ptid = new_ptid;
1636 if (ptid_equal (singlestep_ptid, old_ptid))
1637 singlestep_ptid = new_ptid;
1639 for (displaced = displaced_step_inferior_states;
1641 displaced = displaced->next)
1643 if (ptid_equal (displaced->step_ptid, old_ptid))
1644 displaced->step_ptid = new_ptid;
1646 for (it = displaced->step_request_queue; it; it = it->next)
1647 if (ptid_equal (it->ptid, old_ptid))
1648 it->ptid = new_ptid;
1655 /* Things to clean up if we QUIT out of resume (). */
1657 resume_cleanups (void *ignore)
1662 static const char schedlock_off[] = "off";
1663 static const char schedlock_on[] = "on";
1664 static const char schedlock_step[] = "step";
1665 static const char *const scheduler_enums[] = {
1671 static const char *scheduler_mode = schedlock_off;
1673 show_scheduler_mode (struct ui_file *file, int from_tty,
1674 struct cmd_list_element *c, const char *value)
1676 fprintf_filtered (file,
1677 _("Mode for locking scheduler "
1678 "during execution is \"%s\".\n"),
1683 set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
1685 if (!target_can_lock_scheduler)
1687 scheduler_mode = schedlock_off;
1688 error (_("Target '%s' cannot support this command."), target_shortname);
1692 /* True if execution commands resume all threads of all processes by
1693 default; otherwise, resume only threads of the current inferior
1695 int sched_multi = 0;
1697 /* Try to setup for software single stepping over the specified location.
1698 Return 1 if target_resume() should use hardware single step.
1700 GDBARCH the current gdbarch.
1701 PC the location to step over. */
1704 maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
1708 if (execution_direction == EXEC_FORWARD
1709 && gdbarch_software_single_step_p (gdbarch)
1710 && gdbarch_software_single_step (gdbarch, get_current_frame ()))
1713 /* Do not pull these breakpoints until after a `wait' in
1714 `wait_for_inferior'. */
1715 singlestep_breakpoints_inserted_p = 1;
1716 singlestep_ptid = inferior_ptid;
1723 user_visible_resume_ptid (int step)
1725 /* By default, resume all threads of all processes. */
1726 ptid_t resume_ptid = RESUME_ALL;
1728 /* Maybe resume only all threads of the current process. */
1729 if (!sched_multi && target_supports_multi_process ())
1731 resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1734 /* Maybe resume a single thread after all. */
1737 /* With non-stop mode on, threads are always handled
1739 resume_ptid = inferior_ptid;
1741 else if ((scheduler_mode == schedlock_on)
1742 || (scheduler_mode == schedlock_step
1743 && (step || singlestep_breakpoints_inserted_p)))
1745 /* User-settable 'scheduler' mode requires solo thread resume. */
1746 resume_ptid = inferior_ptid;
1749 /* We may actually resume fewer threads at first, e.g., if a thread
1750 is stopped at a breakpoint that needs stepping-off, but that
1751 should not be visible to the user/frontend, and neither should
1752 the frontend/user be allowed to proceed any of the threads that
1753 happen to be stopped for internal run control handling, if a
1754 previous command wanted them resumed. */
1758 /* Resume the inferior, but allow a QUIT. This is useful if the user
1759 wants to interrupt some lengthy single-stepping operation
1760 (for child processes, the SIGINT goes to the inferior, and so
1761 we get a SIGINT random_signal, but for remote debugging and perhaps
1762 other targets, that's not true).
1764 STEP nonzero if we should step (zero to continue instead).
1765 SIG is the signal to give the inferior (zero for none). */
1767 resume (int step, enum gdb_signal sig)
1769 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
1770 struct regcache *regcache = get_current_regcache ();
1771 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1772 struct thread_info *tp = inferior_thread ();
1773 CORE_ADDR pc = regcache_read_pc (regcache);
1774 struct address_space *aspace = get_regcache_aspace (regcache);
1776 /* From here on, this represents the caller's step vs continue
1777 request, while STEP represents what we'll actually request the
1778 target to do. STEP can decay from a step to a continue, if e.g.,
1779 we need to implement single-stepping with breakpoints (software
1780 single-step). When deciding whether "set scheduler-locking step"
1781 applies, it's the callers intention that counts. */
1782 const int entry_step = step;
1786 if (current_inferior ()->waiting_for_vfork_done)
1788 /* Don't try to single-step a vfork parent that is waiting for
1789 the child to get out of the shared memory region (by exec'ing
1790 or exiting). This is particularly important on software
1791 single-step archs, as the child process would trip on the
1792 software single step breakpoint inserted for the parent
1793 process. Since the parent will not actually execute any
1794 instruction until the child is out of the shared region (such
1795 are vfork's semantics), it is safe to simply continue it.
1796 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
1797 the parent, and tell it to `keep_going', which automatically
1798 re-sets it stepping. */
1800 fprintf_unfiltered (gdb_stdlog,
1801 "infrun: resume : clear step\n");
1806 fprintf_unfiltered (gdb_stdlog,
1807 "infrun: resume (step=%d, signal=%s), "
1808 "trap_expected=%d, current thread [%s] at %s\n",
1809 step, gdb_signal_to_symbol_string (sig),
1810 tp->control.trap_expected,
1811 target_pid_to_str (inferior_ptid),
1812 paddress (gdbarch, pc));
1814 /* Normally, by the time we reach `resume', the breakpoints are either
1815 removed or inserted, as appropriate. The exception is if we're sitting
1816 at a permanent breakpoint; we need to step over it, but permanent
1817 breakpoints can't be removed. So we have to test for it here. */
1818 if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
1820 if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1821 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
1824 The program is stopped at a permanent breakpoint, but GDB does not know\n\
1825 how to step past a permanent breakpoint on this architecture. Try using\n\
1826 a command like `return' or `jump' to continue execution."));
1829 /* If we have a breakpoint to step over, make sure to do a single
1830 step only. Same if we have software watchpoints. */
1831 if (tp->control.trap_expected || bpstat_should_step ())
1832 tp->control.may_range_step = 0;
1834 /* If enabled, step over breakpoints by executing a copy of the
1835 instruction at a different address.
1837 We can't use displaced stepping when we have a signal to deliver;
1838 the comments for displaced_step_prepare explain why. The
1839 comments in the handle_inferior event for dealing with 'random
1840 signals' explain what we do instead.
1842 We can't use displaced stepping when we are waiting for vfork_done
1843 event, displaced stepping breaks the vfork child similarly as single
1844 step software breakpoint. */
1845 if (use_displaced_stepping (gdbarch)
1846 && (tp->control.trap_expected
1847 || (step && gdbarch_software_single_step_p (gdbarch)))
1848 && sig == GDB_SIGNAL_0
1849 && !current_inferior ()->waiting_for_vfork_done)
1851 struct displaced_step_inferior_state *displaced;
1853 if (!displaced_step_prepare (inferior_ptid))
1855 /* Got placed in displaced stepping queue. Will be resumed
1856 later when all the currently queued displaced stepping
1857 requests finish. The thread is not executing at this
1858 point, and the call to set_executing will be made later.
1859 But we need to call set_running here, since from the
1860 user/frontend's point of view, threads were set running.
1861 Unless we're calling an inferior function, as in that
1862 case we pretend the inferior doesn't run at all. */
1863 if (!tp->control.in_infcall)
1864 set_running (user_visible_resume_ptid (entry_step), 1);
1865 discard_cleanups (old_cleanups);
1869 /* Update pc to reflect the new address from which we will execute
1870 instructions due to displaced stepping. */
1871 pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
1873 displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1874 step = gdbarch_displaced_step_hw_singlestep (gdbarch,
1875 displaced->step_closure);
1878 /* Do we need to do it the hard way, w/temp breakpoints? */
1880 step = maybe_software_singlestep (gdbarch, pc);
1882 /* Currently, our software single-step implementation leads to different
1883 results than hardware single-stepping in one situation: when stepping
1884 into delivering a signal which has an associated signal handler,
1885 hardware single-step will stop at the first instruction of the handler,
1886 while software single-step will simply skip execution of the handler.
1888 For now, this difference in behavior is accepted since there is no
1889 easy way to actually implement single-stepping into a signal handler
1890 without kernel support.
1892 However, there is one scenario where this difference leads to follow-on
1893 problems: if we're stepping off a breakpoint by removing all breakpoints
1894 and then single-stepping. In this case, the software single-step
1895 behavior means that even if there is a *breakpoint* in the signal
1896 handler, GDB still would not stop.
1898 Fortunately, we can at least fix this particular issue. We detect
1899 here the case where we are about to deliver a signal while software
1900 single-stepping with breakpoints removed. In this situation, we
1901 revert the decisions to remove all breakpoints and insert single-
1902 step breakpoints, and instead we install a step-resume breakpoint
1903 at the current address, deliver the signal without stepping, and
1904 once we arrive back at the step-resume breakpoint, actually step
1905 over the breakpoint we originally wanted to step over. */
1906 if (singlestep_breakpoints_inserted_p
1907 && tp->control.trap_expected && sig != GDB_SIGNAL_0)
1909 /* If we have nested signals or a pending signal is delivered
1910 immediately after a handler returns, might might already have
1911 a step-resume breakpoint set on the earlier handler. We cannot
1912 set another step-resume breakpoint; just continue on until the
1913 original breakpoint is hit. */
1914 if (tp->control.step_resume_breakpoint == NULL)
1916 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
1917 tp->step_after_step_resume_breakpoint = 1;
1920 remove_single_step_breakpoints ();
1921 singlestep_breakpoints_inserted_p = 0;
1923 clear_step_over_info ();
1924 tp->control.trap_expected = 0;
1926 insert_breakpoints ();
1929 /* If STEP is set, it's a request to use hardware stepping
1930 facilities. But in that case, we should never
1931 use singlestep breakpoint. */
1932 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1934 /* Decide the set of threads to ask the target to resume. Start
1935 by assuming everything will be resumed, than narrow the set
1936 by applying increasingly restricting conditions. */
1937 resume_ptid = user_visible_resume_ptid (entry_step);
1939 /* Even if RESUME_PTID is a wildcard, and we end up resuming less
1940 (e.g., we might need to step over a breakpoint), from the
1941 user/frontend's point of view, all threads in RESUME_PTID are now
1942 running. Unless we're calling an inferior function, as in that
1943 case pretend we inferior doesn't run at all. */
1944 if (!tp->control.in_infcall)
1945 set_running (resume_ptid, 1);
1947 /* Maybe resume a single thread after all. */
1948 if ((step || singlestep_breakpoints_inserted_p)
1949 && tp->control.trap_expected)
1951 /* We're allowing a thread to run past a breakpoint it has
1952 hit, by single-stepping the thread with the breakpoint
1953 removed. In which case, we need to single-step only this
1954 thread, and keep others stopped, as they can miss this
1955 breakpoint if allowed to run. */
1956 resume_ptid = inferior_ptid;
1959 if (gdbarch_cannot_step_breakpoint (gdbarch))
1961 /* Most targets can step a breakpoint instruction, thus
1962 executing it normally. But if this one cannot, just
1963 continue and we will hit it anyway. */
1964 if (step && breakpoint_inserted_here_p (aspace, pc))
1969 && use_displaced_stepping (gdbarch)
1970 && tp->control.trap_expected)
1972 struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
1973 struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
1974 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
1977 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1978 paddress (resume_gdbarch, actual_pc));
1979 read_memory (actual_pc, buf, sizeof (buf));
1980 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1983 if (tp->control.may_range_step)
1985 /* If we're resuming a thread with the PC out of the step
1986 range, then we're doing some nested/finer run control
1987 operation, like stepping the thread out of the dynamic
1988 linker or the displaced stepping scratch pad. We
1989 shouldn't have allowed a range step then. */
1990 gdb_assert (pc_in_thread_step_range (pc, tp));
1993 /* Install inferior's terminal modes. */
1994 target_terminal_inferior ();
1996 /* Avoid confusing the next resume, if the next stop/resume
1997 happens to apply to another thread. */
1998 tp->suspend.stop_signal = GDB_SIGNAL_0;
2000 /* Advise target which signals may be handled silently. If we have
2001 removed breakpoints because we are stepping over one (which can
2002 happen only if we are not using displaced stepping), we need to
2003 receive all signals to avoid accidentally skipping a breakpoint
2004 during execution of a signal handler. */
2005 if ((step || singlestep_breakpoints_inserted_p)
2006 && tp->control.trap_expected
2007 && !use_displaced_stepping (gdbarch))
2008 target_pass_signals (0, NULL);
2010 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
2012 target_resume (resume_ptid, step, sig);
2014 discard_cleanups (old_cleanups);
2019 /* Clear out all variables saying what to do when inferior is continued.
2020 First do this, then set the ones you want, then call `proceed'. */
2023 clear_proceed_status_thread (struct thread_info *tp)
2026 fprintf_unfiltered (gdb_stdlog,
2027 "infrun: clear_proceed_status_thread (%s)\n",
2028 target_pid_to_str (tp->ptid));
2030 /* If this signal should not be seen by program, give it zero.
2031 Used for debugging signals. */
2032 if (!signal_pass_state (tp->suspend.stop_signal))
2033 tp->suspend.stop_signal = GDB_SIGNAL_0;
2035 tp->control.trap_expected = 0;
2036 tp->control.step_range_start = 0;
2037 tp->control.step_range_end = 0;
2038 tp->control.may_range_step = 0;
2039 tp->control.step_frame_id = null_frame_id;
2040 tp->control.step_stack_frame_id = null_frame_id;
2041 tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
2042 tp->stop_requested = 0;
2044 tp->control.stop_step = 0;
2046 tp->control.proceed_to_finish = 0;
2048 tp->control.command_interp = NULL;
2050 /* Discard any remaining commands or status from previous stop. */
2051 bpstat_clear (&tp->control.stop_bpstat);
2055 clear_proceed_status (int step)
2059 struct thread_info *tp;
2062 resume_ptid = user_visible_resume_ptid (step);
2064 /* In all-stop mode, delete the per-thread status of all threads
2065 we're about to resume, implicitly and explicitly. */
2066 ALL_NON_EXITED_THREADS (tp)
2068 if (!ptid_match (tp->ptid, resume_ptid))
2070 clear_proceed_status_thread (tp);
2074 if (!ptid_equal (inferior_ptid, null_ptid))
2076 struct inferior *inferior;
2080 /* If in non-stop mode, only delete the per-thread status of
2081 the current thread. */
2082 clear_proceed_status_thread (inferior_thread ());
2085 inferior = current_inferior ();
2086 inferior->control.stop_soon = NO_STOP_QUIETLY;
2089 stop_after_trap = 0;
2091 clear_step_over_info ();
2093 observer_notify_about_to_proceed ();
2097 regcache_xfree (stop_registers);
2098 stop_registers = NULL;
2102 /* Returns true if TP is still stopped at a breakpoint that needs
2103 stepping-over in order to make progress. If the breakpoint is gone
2104 meanwhile, we can skip the whole step-over dance. */
2107 thread_still_needs_step_over (struct thread_info *tp)
2109 if (tp->stepping_over_breakpoint)
2111 struct regcache *regcache = get_thread_regcache (tp->ptid);
2113 if (breakpoint_here_p (get_regcache_aspace (regcache),
2114 regcache_read_pc (regcache)))
2117 tp->stepping_over_breakpoint = 0;
2123 /* Returns true if scheduler locking applies. STEP indicates whether
2124 we're about to do a step/next-like command to a thread. */
2127 schedlock_applies (int step)
2129 return (scheduler_mode == schedlock_on
2130 || (scheduler_mode == schedlock_step
2134 /* Look a thread other than EXCEPT that has previously reported a
2135 breakpoint event, and thus needs a step-over in order to make
2136 progress. Returns NULL is none is found. STEP indicates whether
2137 we're about to step the current thread, in order to decide whether
2138 "set scheduler-locking step" applies. */
2140 static struct thread_info *
2141 find_thread_needs_step_over (int step, struct thread_info *except)
2143 struct thread_info *tp, *current;
2145 /* With non-stop mode on, threads are always handled individually. */
2146 gdb_assert (! non_stop);
2148 current = inferior_thread ();
2150 /* If scheduler locking applies, we can avoid iterating over all
2152 if (schedlock_applies (step))
2154 if (except != current
2155 && thread_still_needs_step_over (current))
2161 ALL_NON_EXITED_THREADS (tp)
2163 /* Ignore the EXCEPT thread. */
2166 /* Ignore threads of processes we're not resuming. */
2168 && ptid_get_pid (tp->ptid) != ptid_get_pid (inferior_ptid))
2171 if (thread_still_needs_step_over (tp))
2178 /* Basic routine for continuing the program in various fashions.
2180 ADDR is the address to resume at, or -1 for resume where stopped.
2181 SIGGNAL is the signal to give it, or 0 for none,
2182 or -1 for act according to how it stopped.
2183 STEP is nonzero if should trap after one instruction.
2184 -1 means return after that and print nothing.
2185 You should probably set various step_... variables
2186 before calling here, if you are stepping.
2188 You should call clear_proceed_status before calling proceed. */
2191 proceed (CORE_ADDR addr, enum gdb_signal siggnal, int step)
2193 struct regcache *regcache;
2194 struct gdbarch *gdbarch;
2195 struct thread_info *tp;
2197 struct address_space *aspace;
2199 /* If we're stopped at a fork/vfork, follow the branch set by the
2200 "set follow-fork-mode" command; otherwise, we'll just proceed
2201 resuming the current thread. */
2202 if (!follow_fork ())
2204 /* The target for some reason decided not to resume. */
2206 if (target_can_async_p ())
2207 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2211 /* We'll update this if & when we switch to a new thread. */
2212 previous_inferior_ptid = inferior_ptid;
2214 regcache = get_current_regcache ();
2215 gdbarch = get_regcache_arch (regcache);
2216 aspace = get_regcache_aspace (regcache);
2217 pc = regcache_read_pc (regcache);
2218 tp = inferior_thread ();
2221 step_start_function = find_pc_function (pc);
2223 stop_after_trap = 1;
2225 /* Fill in with reasonable starting values. */
2226 init_thread_stepping_state (tp);
2228 if (addr == (CORE_ADDR) -1)
2230 if (pc == stop_pc && breakpoint_here_p (aspace, pc)
2231 && execution_direction != EXEC_REVERSE)
2232 /* There is a breakpoint at the address we will resume at,
2233 step one instruction before inserting breakpoints so that
2234 we do not stop right away (and report a second hit at this
2237 Note, we don't do this in reverse, because we won't
2238 actually be executing the breakpoint insn anyway.
2239 We'll be (un-)executing the previous instruction. */
2240 tp->stepping_over_breakpoint = 1;
2241 else if (gdbarch_single_step_through_delay_p (gdbarch)
2242 && gdbarch_single_step_through_delay (gdbarch,
2243 get_current_frame ()))
2244 /* We stepped onto an instruction that needs to be stepped
2245 again before re-inserting the breakpoint, do so. */
2246 tp->stepping_over_breakpoint = 1;
2250 regcache_write_pc (regcache, addr);
2253 if (siggnal != GDB_SIGNAL_DEFAULT)
2254 tp->suspend.stop_signal = siggnal;
2256 /* Record the interpreter that issued the execution command that
2257 caused this thread to resume. If the top level interpreter is
2258 MI/async, and the execution command was a CLI command
2259 (next/step/etc.), we'll want to print stop event output to the MI
2260 console channel (the stepped-to line, etc.), as if the user
2261 entered the execution command on a real GDB console. */
2262 inferior_thread ()->control.command_interp = command_interp ();
2265 fprintf_unfiltered (gdb_stdlog,
2266 "infrun: proceed (addr=%s, signal=%s, step=%d)\n",
2267 paddress (gdbarch, addr),
2268 gdb_signal_to_symbol_string (siggnal), step);
2271 /* In non-stop, each thread is handled individually. The context
2272 must already be set to the right thread here. */
2276 struct thread_info *step_over;
2278 /* In a multi-threaded task we may select another thread and
2279 then continue or step.
2281 But if the old thread was stopped at a breakpoint, it will
2282 immediately cause another breakpoint stop without any
2283 execution (i.e. it will report a breakpoint hit incorrectly).
2284 So we must step over it first.
2286 Look for a thread other than the current (TP) that reported a
2287 breakpoint hit and hasn't been resumed yet since. */
2288 step_over = find_thread_needs_step_over (step, tp);
2289 if (step_over != NULL)
2292 fprintf_unfiltered (gdb_stdlog,
2293 "infrun: need to step-over [%s] first\n",
2294 target_pid_to_str (step_over->ptid));
2296 /* Store the prev_pc for the stepping thread too, needed by
2297 switch_back_to_stepping thread. */
2298 tp->prev_pc = regcache_read_pc (get_current_regcache ());
2299 switch_to_thread (step_over->ptid);
2304 /* If we need to step over a breakpoint, and we're not using
2305 displaced stepping to do so, insert all breakpoints (watchpoints,
2306 etc.) but the one we're stepping over, step one instruction, and
2307 then re-insert the breakpoint when that step is finished. */
2308 if (tp->stepping_over_breakpoint && !use_displaced_stepping (gdbarch))
2310 struct regcache *regcache = get_current_regcache ();
2312 set_step_over_info (get_regcache_aspace (regcache),
2313 regcache_read_pc (regcache));
2316 clear_step_over_info ();
2318 insert_breakpoints ();
2320 tp->control.trap_expected = tp->stepping_over_breakpoint;
2322 annotate_starting ();
2324 /* Make sure that output from GDB appears before output from the
2326 gdb_flush (gdb_stdout);
2328 /* Refresh prev_pc value just prior to resuming. This used to be
2329 done in stop_waiting, however, setting prev_pc there did not handle
2330 scenarios such as inferior function calls or returning from
2331 a function via the return command. In those cases, the prev_pc
2332 value was not set properly for subsequent commands. The prev_pc value
2333 is used to initialize the starting line number in the ecs. With an
2334 invalid value, the gdb next command ends up stopping at the position
2335 represented by the next line table entry past our start position.
2336 On platforms that generate one line table entry per line, this
2337 is not a problem. However, on the ia64, the compiler generates
2338 extraneous line table entries that do not increase the line number.
2339 When we issue the gdb next command on the ia64 after an inferior call
2340 or a return command, we often end up a few instructions forward, still
2341 within the original line we started.
2343 An attempt was made to refresh the prev_pc at the same time the
2344 execution_control_state is initialized (for instance, just before
2345 waiting for an inferior event). But this approach did not work
2346 because of platforms that use ptrace, where the pc register cannot
2347 be read unless the inferior is stopped. At that point, we are not
2348 guaranteed the inferior is stopped and so the regcache_read_pc() call
2349 can fail. Setting the prev_pc value here ensures the value is updated
2350 correctly when the inferior is stopped. */
2351 tp->prev_pc = regcache_read_pc (get_current_regcache ());
2353 /* Reset to normal state. */
2354 init_infwait_state ();
2356 /* Resume inferior. */
2357 resume (tp->control.trap_expected || step || bpstat_should_step (),
2358 tp->suspend.stop_signal);
2360 /* Wait for it to stop (if not standalone)
2361 and in any case decode why it stopped, and act accordingly. */
2362 /* Do this only if we are not using the event loop, or if the target
2363 does not support asynchronous execution. */
2364 if (!target_can_async_p ())
2366 wait_for_inferior ();
2372 /* Start remote-debugging of a machine over a serial link. */
2375 start_remote (int from_tty)
2377 struct inferior *inferior;
2379 inferior = current_inferior ();
2380 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2382 /* Always go on waiting for the target, regardless of the mode. */
2383 /* FIXME: cagney/1999-09-23: At present it isn't possible to
2384 indicate to wait_for_inferior that a target should timeout if
2385 nothing is returned (instead of just blocking). Because of this,
2386 targets expecting an immediate response need to, internally, set
2387 things up so that the target_wait() is forced to eventually
2389 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
2390 differentiate to its caller what the state of the target is after
2391 the initial open has been performed. Here we're assuming that
2392 the target has stopped. It should be possible to eventually have
2393 target_open() return to the caller an indication that the target
2394 is currently running and GDB state should be set to the same as
2395 for an async run. */
2396 wait_for_inferior ();
2398 /* Now that the inferior has stopped, do any bookkeeping like
2399 loading shared libraries. We want to do this before normal_stop,
2400 so that the displayed frame is up to date. */
2401 post_create_inferior (¤t_target, from_tty);
2406 /* Initialize static vars when a new inferior begins. */
2409 init_wait_for_inferior (void)
2411 /* These are meaningless until the first time through wait_for_inferior. */
2413 breakpoint_init_inferior (inf_starting);
2415 clear_proceed_status (0);
2417 target_last_wait_ptid = minus_one_ptid;
2419 previous_inferior_ptid = inferior_ptid;
2420 init_infwait_state ();
2422 /* Discard any skipped inlined frames. */
2423 clear_inline_frame_state (minus_one_ptid);
2425 singlestep_ptid = null_ptid;
2430 /* This enum encodes possible reasons for doing a target_wait, so that
2431 wfi can call target_wait in one place. (Ultimately the call will be
2432 moved out of the infinite loop entirely.) */
2436 infwait_normal_state,
2437 infwait_step_watch_state,
2438 infwait_nonstep_watch_state
2441 /* The PTID we'll do a target_wait on.*/
2444 /* Current inferior wait state. */
2445 static enum infwait_states infwait_state;
2447 /* Data to be passed around while handling an event. This data is
2448 discarded between events. */
2449 struct execution_control_state
2452 /* The thread that got the event, if this was a thread event; NULL
2454 struct thread_info *event_thread;
2456 struct target_waitstatus ws;
2457 int stop_func_filled_in;
2458 CORE_ADDR stop_func_start;
2459 CORE_ADDR stop_func_end;
2460 const char *stop_func_name;
2463 /* We were in infwait_step_watch_state or
2464 infwait_nonstep_watch_state state, and the thread reported an
2466 int stepped_after_stopped_by_watchpoint;
2468 /* True if the event thread hit the single-step breakpoint of
2469 another thread. Thus the event doesn't cause a stop, the thread
2470 needs to be single-stepped past the single-step breakpoint before
2471 we can switch back to the original stepping thread. */
2472 int hit_singlestep_breakpoint;
2475 static void handle_inferior_event (struct execution_control_state *ecs);
2477 static void handle_step_into_function (struct gdbarch *gdbarch,
2478 struct execution_control_state *ecs);
2479 static void handle_step_into_function_backward (struct gdbarch *gdbarch,
2480 struct execution_control_state *ecs);
2481 static void handle_signal_stop (struct execution_control_state *ecs);
2482 static void check_exception_resume (struct execution_control_state *,
2483 struct frame_info *);
2485 static void end_stepping_range (struct execution_control_state *ecs);
2486 static void stop_waiting (struct execution_control_state *ecs);
2487 static void prepare_to_wait (struct execution_control_state *ecs);
2488 static void keep_going (struct execution_control_state *ecs);
2489 static void process_event_stop_test (struct execution_control_state *ecs);
2490 static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
2492 /* Callback for iterate over threads. If the thread is stopped, but
2493 the user/frontend doesn't know about that yet, go through
2494 normal_stop, as if the thread had just stopped now. ARG points at
2495 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
2496 ptid_is_pid(PTID) is true, applies to all threads of the process
2497 pointed at by PTID. Otherwise, apply only to the thread pointed by
2501 infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
2503 ptid_t ptid = * (ptid_t *) arg;
2505 if ((ptid_equal (info->ptid, ptid)
2506 || ptid_equal (minus_one_ptid, ptid)
2507 || (ptid_is_pid (ptid)
2508 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
2509 && is_running (info->ptid)
2510 && !is_executing (info->ptid))
2512 struct cleanup *old_chain;
2513 struct execution_control_state ecss;
2514 struct execution_control_state *ecs = &ecss;
2516 memset (ecs, 0, sizeof (*ecs));
2518 old_chain = make_cleanup_restore_current_thread ();
2520 overlay_cache_invalid = 1;
2521 /* Flush target cache before starting to handle each event.
2522 Target was running and cache could be stale. This is just a
2523 heuristic. Running threads may modify target memory, but we
2524 don't get any event. */
2525 target_dcache_invalidate ();
2527 /* Go through handle_inferior_event/normal_stop, so we always
2528 have consistent output as if the stop event had been
2530 ecs->ptid = info->ptid;
2531 ecs->event_thread = find_thread_ptid (info->ptid);
2532 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
2533 ecs->ws.value.sig = GDB_SIGNAL_0;
2535 handle_inferior_event (ecs);
2537 if (!ecs->wait_some_more)
2539 struct thread_info *tp;
2543 /* Finish off the continuations. */
2544 tp = inferior_thread ();
2545 do_all_intermediate_continuations_thread (tp, 1);
2546 do_all_continuations_thread (tp, 1);
2549 do_cleanups (old_chain);
2555 /* This function is attached as a "thread_stop_requested" observer.
2556 Cleanup local state that assumed the PTID was to be resumed, and
2557 report the stop to the frontend. */
2560 infrun_thread_stop_requested (ptid_t ptid)
2562 struct displaced_step_inferior_state *displaced;
2564 /* PTID was requested to stop. Remove it from the displaced
2565 stepping queue, so we don't try to resume it automatically. */
2567 for (displaced = displaced_step_inferior_states;
2569 displaced = displaced->next)
2571 struct displaced_step_request *it, **prev_next_p;
2573 it = displaced->step_request_queue;
2574 prev_next_p = &displaced->step_request_queue;
2577 if (ptid_match (it->ptid, ptid))
2579 *prev_next_p = it->next;
2585 prev_next_p = &it->next;
2592 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
2596 infrun_thread_thread_exit (struct thread_info *tp, int silent)
2598 if (ptid_equal (target_last_wait_ptid, tp->ptid))
2599 nullify_last_target_wait_ptid ();
2602 /* Callback for iterate_over_threads. */
2605 delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
2607 if (is_exited (info->ptid))
2610 delete_step_resume_breakpoint (info);
2611 delete_exception_resume_breakpoint (info);
2615 /* In all-stop, delete the step resume breakpoint of any thread that
2616 had one. In non-stop, delete the step resume breakpoint of the
2617 thread that just stopped. */
2620 delete_step_thread_step_resume_breakpoint (void)
2622 if (!target_has_execution
2623 || ptid_equal (inferior_ptid, null_ptid))
2624 /* If the inferior has exited, we have already deleted the step
2625 resume breakpoints out of GDB's lists. */
2630 /* If in non-stop mode, only delete the step-resume or
2631 longjmp-resume breakpoint of the thread that just stopped
2633 struct thread_info *tp = inferior_thread ();
2635 delete_step_resume_breakpoint (tp);
2636 delete_exception_resume_breakpoint (tp);
2639 /* In all-stop mode, delete all step-resume and longjmp-resume
2640 breakpoints of any thread that had them. */
2641 iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
2644 /* A cleanup wrapper. */
2647 delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
2649 delete_step_thread_step_resume_breakpoint ();
2652 /* Pretty print the results of target_wait, for debugging purposes. */
2655 print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
2656 const struct target_waitstatus *ws)
2658 char *status_string = target_waitstatus_to_string (ws);
2659 struct ui_file *tmp_stream = mem_fileopen ();
2662 /* The text is split over several lines because it was getting too long.
2663 Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
2664 output as a unit; we want only one timestamp printed if debug_timestamp
2667 fprintf_unfiltered (tmp_stream,
2668 "infrun: target_wait (%d", ptid_get_pid (waiton_ptid));
2669 if (ptid_get_pid (waiton_ptid) != -1)
2670 fprintf_unfiltered (tmp_stream,
2671 " [%s]", target_pid_to_str (waiton_ptid));
2672 fprintf_unfiltered (tmp_stream, ", status) =\n");
2673 fprintf_unfiltered (tmp_stream,
2674 "infrun: %d [%s],\n",
2675 ptid_get_pid (result_ptid),
2676 target_pid_to_str (result_ptid));
2677 fprintf_unfiltered (tmp_stream,
2681 text = ui_file_xstrdup (tmp_stream, NULL);
2683 /* This uses %s in part to handle %'s in the text, but also to avoid
2684 a gcc error: the format attribute requires a string literal. */
2685 fprintf_unfiltered (gdb_stdlog, "%s", text);
2687 xfree (status_string);
2689 ui_file_delete (tmp_stream);
2692 /* Prepare and stabilize the inferior for detaching it. E.g.,
2693 detaching while a thread is displaced stepping is a recipe for
2694 crashing it, as nothing would readjust the PC out of the scratch
2698 prepare_for_detach (void)
2700 struct inferior *inf = current_inferior ();
2701 ptid_t pid_ptid = pid_to_ptid (inf->pid);
2702 struct cleanup *old_chain_1;
2703 struct displaced_step_inferior_state *displaced;
2705 displaced = get_displaced_stepping_state (inf->pid);
2707 /* Is any thread of this process displaced stepping? If not,
2708 there's nothing else to do. */
2709 if (displaced == NULL || ptid_equal (displaced->step_ptid, null_ptid))
2713 fprintf_unfiltered (gdb_stdlog,
2714 "displaced-stepping in-process while detaching");
2716 old_chain_1 = make_cleanup_restore_integer (&inf->detaching);
2719 while (!ptid_equal (displaced->step_ptid, null_ptid))
2721 struct cleanup *old_chain_2;
2722 struct execution_control_state ecss;
2723 struct execution_control_state *ecs;
2726 memset (ecs, 0, sizeof (*ecs));
2728 overlay_cache_invalid = 1;
2729 /* Flush target cache before starting to handle each event.
2730 Target was running and cache could be stale. This is just a
2731 heuristic. Running threads may modify target memory, but we
2732 don't get any event. */
2733 target_dcache_invalidate ();
2735 if (deprecated_target_wait_hook)
2736 ecs->ptid = deprecated_target_wait_hook (pid_ptid, &ecs->ws, 0);
2738 ecs->ptid = target_wait (pid_ptid, &ecs->ws, 0);
2741 print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
2743 /* If an error happens while handling the event, propagate GDB's
2744 knowledge of the executing state to the frontend/user running
2746 old_chain_2 = make_cleanup (finish_thread_state_cleanup,
2749 /* Now figure out what to do with the result of the result. */
2750 handle_inferior_event (ecs);
2752 /* No error, don't finish the state yet. */
2753 discard_cleanups (old_chain_2);
2755 /* Breakpoints and watchpoints are not installed on the target
2756 at this point, and signals are passed directly to the
2757 inferior, so this must mean the process is gone. */
2758 if (!ecs->wait_some_more)
2760 discard_cleanups (old_chain_1);
2761 error (_("Program exited while detaching"));
2765 discard_cleanups (old_chain_1);
2768 /* Wait for control to return from inferior to debugger.
2770 If inferior gets a signal, we may decide to start it up again
2771 instead of returning. That is why there is a loop in this function.
2772 When this function actually returns it means the inferior
2773 should be left stopped and GDB should read more commands. */
2776 wait_for_inferior (void)
2778 struct cleanup *old_cleanups;
2782 (gdb_stdlog, "infrun: wait_for_inferior ()\n");
2785 make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
2789 struct execution_control_state ecss;
2790 struct execution_control_state *ecs = &ecss;
2791 struct cleanup *old_chain;
2793 memset (ecs, 0, sizeof (*ecs));
2795 overlay_cache_invalid = 1;
2797 /* Flush target cache before starting to handle each event.
2798 Target was running and cache could be stale. This is just a
2799 heuristic. Running threads may modify target memory, but we
2800 don't get any event. */
2801 target_dcache_invalidate ();
2803 if (deprecated_target_wait_hook)
2804 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
2806 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
2809 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2811 /* If an error happens while handling the event, propagate GDB's
2812 knowledge of the executing state to the frontend/user running
2814 old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2816 /* Now figure out what to do with the result of the result. */
2817 handle_inferior_event (ecs);
2819 /* No error, don't finish the state yet. */
2820 discard_cleanups (old_chain);
2822 if (!ecs->wait_some_more)
2826 do_cleanups (old_cleanups);
2829 /* Asynchronous version of wait_for_inferior. It is called by the
2830 event loop whenever a change of state is detected on the file
2831 descriptor corresponding to the target. It can be called more than
2832 once to complete a single execution command. In such cases we need
2833 to keep the state in a global variable ECSS. If it is the last time
2834 that this function is called for a single execution command, then
2835 report to the user that the inferior has stopped, and do the
2836 necessary cleanups. */
2839 fetch_inferior_event (void *client_data)
2841 struct execution_control_state ecss;
2842 struct execution_control_state *ecs = &ecss;
2843 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
2844 struct cleanup *ts_old_chain;
2845 int was_sync = sync_execution;
2848 memset (ecs, 0, sizeof (*ecs));
2850 /* We're handling a live event, so make sure we're doing live
2851 debugging. If we're looking at traceframes while the target is
2852 running, we're going to need to get back to that mode after
2853 handling the event. */
2856 make_cleanup_restore_current_traceframe ();
2857 set_current_traceframe (-1);
2861 /* In non-stop mode, the user/frontend should not notice a thread
2862 switch due to internal events. Make sure we reverse to the
2863 user selected thread and frame after handling the event and
2864 running any breakpoint commands. */
2865 make_cleanup_restore_current_thread ();
2867 overlay_cache_invalid = 1;
2868 /* Flush target cache before starting to handle each event. Target
2869 was running and cache could be stale. This is just a heuristic.
2870 Running threads may modify target memory, but we don't get any
2872 target_dcache_invalidate ();
2874 make_cleanup_restore_integer (&execution_direction);
2875 execution_direction = target_execution_direction ();
2877 if (deprecated_target_wait_hook)
2879 deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2881 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2884 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2886 /* If an error happens while handling the event, propagate GDB's
2887 knowledge of the executing state to the frontend/user running
2890 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2892 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
2894 /* Get executed before make_cleanup_restore_current_thread above to apply
2895 still for the thread which has thrown the exception. */
2896 make_bpstat_clear_actions_cleanup ();
2898 /* Now figure out what to do with the result of the result. */
2899 handle_inferior_event (ecs);
2901 if (!ecs->wait_some_more)
2903 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2905 delete_step_thread_step_resume_breakpoint ();
2907 /* We may not find an inferior if this was a process exit. */
2908 if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
2911 if (target_has_execution
2912 && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED
2913 && ecs->ws.kind != TARGET_WAITKIND_EXITED
2914 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2915 && ecs->event_thread->step_multi
2916 && ecs->event_thread->control.stop_step)
2917 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
2920 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2925 /* No error, don't finish the thread states yet. */
2926 discard_cleanups (ts_old_chain);
2928 /* Revert thread and frame. */
2929 do_cleanups (old_chain);
2931 /* If the inferior was in sync execution mode, and now isn't,
2932 restore the prompt (a synchronous execution command has finished,
2933 and we're ready for input). */
2934 if (interpreter_async && was_sync && !sync_execution)
2935 observer_notify_sync_execution_done ();
2939 && exec_done_display_p
2940 && (ptid_equal (inferior_ptid, null_ptid)
2941 || !is_running (inferior_ptid)))
2942 printf_unfiltered (_("completed.\n"));
2945 /* Record the frame and location we're currently stepping through. */
2947 set_step_info (struct frame_info *frame, struct symtab_and_line sal)
2949 struct thread_info *tp = inferior_thread ();
2951 tp->control.step_frame_id = get_frame_id (frame);
2952 tp->control.step_stack_frame_id = get_stack_frame_id (frame);
2954 tp->current_symtab = sal.symtab;
2955 tp->current_line = sal.line;
2958 /* Clear context switchable stepping state. */
2961 init_thread_stepping_state (struct thread_info *tss)
2963 tss->stepping_over_breakpoint = 0;
2964 tss->step_after_step_resume_breakpoint = 0;
2967 /* Set the cached copy of the last ptid/waitstatus. */
2970 set_last_target_status (ptid_t ptid, struct target_waitstatus status)
2972 target_last_wait_ptid = ptid;
2973 target_last_waitstatus = status;
2976 /* Return the cached copy of the last pid/waitstatus returned by
2977 target_wait()/deprecated_target_wait_hook(). The data is actually
2978 cached by handle_inferior_event(), which gets called immediately
2979 after target_wait()/deprecated_target_wait_hook(). */
2982 get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
2984 *ptidp = target_last_wait_ptid;
2985 *status = target_last_waitstatus;
2989 nullify_last_target_wait_ptid (void)
2991 target_last_wait_ptid = minus_one_ptid;
2994 /* Switch thread contexts. */
2997 context_switch (ptid_t ptid)
2999 if (debug_infrun && !ptid_equal (ptid, inferior_ptid))
3001 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
3002 target_pid_to_str (inferior_ptid));
3003 fprintf_unfiltered (gdb_stdlog, "to %s\n",
3004 target_pid_to_str (ptid));
3007 switch_to_thread (ptid);
3011 adjust_pc_after_break (struct execution_control_state *ecs)
3013 struct regcache *regcache;
3014 struct gdbarch *gdbarch;
3015 struct address_space *aspace;
3016 CORE_ADDR breakpoint_pc, decr_pc;
3018 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
3019 we aren't, just return.
3021 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
3022 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
3023 implemented by software breakpoints should be handled through the normal
3026 NOTE drow/2004-01-31: On some targets, breakpoints may generate
3027 different signals (SIGILL or SIGEMT for instance), but it is less
3028 clear where the PC is pointing afterwards. It may not match
3029 gdbarch_decr_pc_after_break. I don't know any specific target that
3030 generates these signals at breakpoints (the code has been in GDB since at
3031 least 1992) so I can not guess how to handle them here.
3033 In earlier versions of GDB, a target with
3034 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
3035 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
3036 target with both of these set in GDB history, and it seems unlikely to be
3037 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
3039 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
3042 if (ecs->ws.value.sig != GDB_SIGNAL_TRAP)
3045 /* In reverse execution, when a breakpoint is hit, the instruction
3046 under it has already been de-executed. The reported PC always
3047 points at the breakpoint address, so adjusting it further would
3048 be wrong. E.g., consider this case on a decr_pc_after_break == 1
3051 B1 0x08000000 : INSN1
3052 B2 0x08000001 : INSN2
3054 PC -> 0x08000003 : INSN4
3056 Say you're stopped at 0x08000003 as above. Reverse continuing
3057 from that point should hit B2 as below. Reading the PC when the
3058 SIGTRAP is reported should read 0x08000001 and INSN2 should have
3059 been de-executed already.
3061 B1 0x08000000 : INSN1
3062 B2 PC -> 0x08000001 : INSN2
3066 We can't apply the same logic as for forward execution, because
3067 we would wrongly adjust the PC to 0x08000000, since there's a
3068 breakpoint at PC - 1. We'd then report a hit on B1, although
3069 INSN1 hadn't been de-executed yet. Doing nothing is the correct
3071 if (execution_direction == EXEC_REVERSE)
3074 /* If this target does not decrement the PC after breakpoints, then
3075 we have nothing to do. */
3076 regcache = get_thread_regcache (ecs->ptid);
3077 gdbarch = get_regcache_arch (regcache);
3079 decr_pc = target_decr_pc_after_break (gdbarch);
3083 aspace = get_regcache_aspace (regcache);
3085 /* Find the location where (if we've hit a breakpoint) the
3086 breakpoint would be. */
3087 breakpoint_pc = regcache_read_pc (regcache) - decr_pc;
3089 /* Check whether there actually is a software breakpoint inserted at
3092 If in non-stop mode, a race condition is possible where we've
3093 removed a breakpoint, but stop events for that breakpoint were
3094 already queued and arrive later. To suppress those spurious
3095 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
3096 and retire them after a number of stop events are reported. */
3097 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
3098 || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
3100 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
3102 if (record_full_is_used ())
3103 record_full_gdb_operation_disable_set ();
3105 /* When using hardware single-step, a SIGTRAP is reported for both
3106 a completed single-step and a software breakpoint. Need to
3107 differentiate between the two, as the latter needs adjusting
3108 but the former does not.
3110 The SIGTRAP can be due to a completed hardware single-step only if
3111 - we didn't insert software single-step breakpoints
3112 - the thread to be examined is still the current thread
3113 - this thread is currently being stepped
3115 If any of these events did not occur, we must have stopped due
3116 to hitting a software breakpoint, and have to back up to the
3119 As a special case, we could have hardware single-stepped a
3120 software breakpoint. In this case (prev_pc == breakpoint_pc),
3121 we also need to back up to the breakpoint address. */
3123 if (singlestep_breakpoints_inserted_p
3124 || !ptid_equal (ecs->ptid, inferior_ptid)
3125 || !currently_stepping (ecs->event_thread)
3126 || ecs->event_thread->prev_pc == breakpoint_pc)
3127 regcache_write_pc (regcache, breakpoint_pc);
3129 do_cleanups (old_cleanups);
3134 init_infwait_state (void)
3136 waiton_ptid = pid_to_ptid (-1);
3137 infwait_state = infwait_normal_state;
3141 stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3143 for (frame = get_prev_frame (frame);
3145 frame = get_prev_frame (frame))
3147 if (frame_id_eq (get_frame_id (frame), step_frame_id))
3149 if (get_frame_type (frame) != INLINE_FRAME)
3156 /* Auxiliary function that handles syscall entry/return events.
3157 It returns 1 if the inferior should keep going (and GDB
3158 should ignore the event), or 0 if the event deserves to be
3162 handle_syscall_event (struct execution_control_state *ecs)
3164 struct regcache *regcache;
3167 if (!ptid_equal (ecs->ptid, inferior_ptid))
3168 context_switch (ecs->ptid);
3170 regcache = get_thread_regcache (ecs->ptid);
3171 syscall_number = ecs->ws.value.syscall_number;
3172 stop_pc = regcache_read_pc (regcache);
3174 if (catch_syscall_enabled () > 0
3175 && catching_syscall_number (syscall_number) > 0)
3178 fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3181 ecs->event_thread->control.stop_bpstat
3182 = bpstat_stop_status (get_regcache_aspace (regcache),
3183 stop_pc, ecs->ptid, &ecs->ws);
3185 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3187 /* Catchpoint hit. */
3192 /* If no catchpoint triggered for this, then keep going. */
3197 /* Lazily fill in the execution_control_state's stop_func_* fields. */
3200 fill_in_stop_func (struct gdbarch *gdbarch,
3201 struct execution_control_state *ecs)
3203 if (!ecs->stop_func_filled_in)
3205 /* Don't care about return value; stop_func_start and stop_func_name
3206 will both be 0 if it doesn't work. */
3207 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3208 &ecs->stop_func_start, &ecs->stop_func_end);
3209 ecs->stop_func_start
3210 += gdbarch_deprecated_function_start_offset (gdbarch);
3212 if (gdbarch_skip_entrypoint_p (gdbarch))
3213 ecs->stop_func_start = gdbarch_skip_entrypoint (gdbarch,
3214 ecs->stop_func_start);
3216 ecs->stop_func_filled_in = 1;
3221 /* Return the STOP_SOON field of the inferior pointed at by PTID. */
3223 static enum stop_kind
3224 get_inferior_stop_soon (ptid_t ptid)
3226 struct inferior *inf = find_inferior_pid (ptid_get_pid (ptid));
3228 gdb_assert (inf != NULL);
3229 return inf->control.stop_soon;
3232 /* Given an execution control state that has been freshly filled in by
3233 an event from the inferior, figure out what it means and take
3236 The alternatives are:
3238 1) stop_waiting and return; to really stop and return to the
3241 2) keep_going and return; to wait for the next event (set
3242 ecs->event_thread->stepping_over_breakpoint to 1 to single step
3246 handle_inferior_event (struct execution_control_state *ecs)
3248 enum stop_kind stop_soon;
3250 if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3252 /* We had an event in the inferior, but we are not interested in
3253 handling it at this level. The lower layers have already
3254 done what needs to be done, if anything.
3256 One of the possible circumstances for this is when the
3257 inferior produces output for the console. The inferior has
3258 not stopped, and we are ignoring the event. Another possible
3259 circumstance is any event which the lower level knows will be
3260 reported multiple times without an intervening resume. */
3262 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3263 prepare_to_wait (ecs);
3267 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED
3268 && target_can_async_p () && !sync_execution)
3270 /* There were no unwaited-for children left in the target, but,
3271 we're not synchronously waiting for events either. Just
3272 ignore. Otherwise, if we were running a synchronous
3273 execution command, we need to cancel it and give the user
3274 back the terminal. */
3276 fprintf_unfiltered (gdb_stdlog,
3277 "infrun: TARGET_WAITKIND_NO_RESUMED (ignoring)\n");
3278 prepare_to_wait (ecs);
3282 /* Cache the last pid/waitstatus. */
3283 set_last_target_status (ecs->ptid, ecs->ws);
3285 /* Always clear state belonging to the previous time we stopped. */
3286 stop_stack_dummy = STOP_NONE;
3288 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED)
3290 /* No unwaited-for children left. IOW, all resumed children
3293 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_RESUMED\n");
3295 stop_print_frame = 0;
3300 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
3301 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
3303 ecs->event_thread = find_thread_ptid (ecs->ptid);
3304 /* If it's a new thread, add it to the thread database. */
3305 if (ecs->event_thread == NULL)
3306 ecs->event_thread = add_thread (ecs->ptid);
3308 /* Disable range stepping. If the next step request could use a
3309 range, this will be end up re-enabled then. */
3310 ecs->event_thread->control.may_range_step = 0;
3313 /* Dependent on valid ECS->EVENT_THREAD. */
3314 adjust_pc_after_break (ecs);
3316 /* Dependent on the current PC value modified by adjust_pc_after_break. */
3317 reinit_frame_cache ();
3319 breakpoint_retire_moribund ();
3321 /* First, distinguish signals caused by the debugger from signals
3322 that have to do with the program's own actions. Note that
3323 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3324 on the operating system version. Here we detect when a SIGILL or
3325 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3326 something similar for SIGSEGV, since a SIGSEGV will be generated
3327 when we're trying to execute a breakpoint instruction on a
3328 non-executable stack. This happens for call dummy breakpoints
3329 for architectures like SPARC that place call dummies on the
3331 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
3332 && (ecs->ws.value.sig == GDB_SIGNAL_ILL
3333 || ecs->ws.value.sig == GDB_SIGNAL_SEGV
3334 || ecs->ws.value.sig == GDB_SIGNAL_EMT))
3336 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3338 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3339 regcache_read_pc (regcache)))
3342 fprintf_unfiltered (gdb_stdlog,
3343 "infrun: Treating signal as SIGTRAP\n");
3344 ecs->ws.value.sig = GDB_SIGNAL_TRAP;
3348 /* Mark the non-executing threads accordingly. In all-stop, all
3349 threads of all processes are stopped when we get any event
3350 reported. In non-stop mode, only the event thread stops. If
3351 we're handling a process exit in non-stop mode, there's nothing
3352 to do, as threads of the dead process are gone, and threads of
3353 any other process were left running. */
3355 set_executing (minus_one_ptid, 0);
3356 else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3357 && ecs->ws.kind != TARGET_WAITKIND_EXITED)
3358 set_executing (ecs->ptid, 0);
3360 switch (infwait_state)
3362 case infwait_normal_state:
3364 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
3367 case infwait_step_watch_state:
3369 fprintf_unfiltered (gdb_stdlog,
3370 "infrun: infwait_step_watch_state\n");
3372 ecs->stepped_after_stopped_by_watchpoint = 1;
3375 case infwait_nonstep_watch_state:
3377 fprintf_unfiltered (gdb_stdlog,
3378 "infrun: infwait_nonstep_watch_state\n");
3379 insert_breakpoints ();
3381 /* FIXME-maybe: is this cleaner than setting a flag? Does it
3382 handle things like signals arriving and other things happening
3383 in combination correctly? */
3384 ecs->stepped_after_stopped_by_watchpoint = 1;
3388 internal_error (__FILE__, __LINE__, _("bad switch"));
3391 infwait_state = infwait_normal_state;
3392 waiton_ptid = pid_to_ptid (-1);
3394 switch (ecs->ws.kind)
3396 case TARGET_WAITKIND_LOADED:
3398 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
3399 if (!ptid_equal (ecs->ptid, inferior_ptid))
3400 context_switch (ecs->ptid);
3401 /* Ignore gracefully during startup of the inferior, as it might
3402 be the shell which has just loaded some objects, otherwise
3403 add the symbols for the newly loaded objects. Also ignore at
3404 the beginning of an attach or remote session; we will query
3405 the full list of libraries once the connection is
3408 stop_soon = get_inferior_stop_soon (ecs->ptid);
3409 if (stop_soon == NO_STOP_QUIETLY)
3411 struct regcache *regcache;
3413 regcache = get_thread_regcache (ecs->ptid);
3415 handle_solib_event ();
3417 ecs->event_thread->control.stop_bpstat
3418 = bpstat_stop_status (get_regcache_aspace (regcache),
3419 stop_pc, ecs->ptid, &ecs->ws);
3421 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3423 /* A catchpoint triggered. */
3424 process_event_stop_test (ecs);
3428 /* If requested, stop when the dynamic linker notifies
3429 gdb of events. This allows the user to get control
3430 and place breakpoints in initializer routines for
3431 dynamically loaded objects (among other things). */
3432 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3433 if (stop_on_solib_events)
3435 /* Make sure we print "Stopped due to solib-event" in
3437 stop_print_frame = 1;
3444 /* If we are skipping through a shell, or through shared library
3445 loading that we aren't interested in, resume the program. If
3446 we're running the program normally, also resume. */
3447 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3449 /* Loading of shared libraries might have changed breakpoint
3450 addresses. Make sure new breakpoints are inserted. */
3451 if (stop_soon == NO_STOP_QUIETLY)
3452 insert_breakpoints ();
3453 resume (0, GDB_SIGNAL_0);
3454 prepare_to_wait (ecs);
3458 /* But stop if we're attaching or setting up a remote
3460 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3461 || stop_soon == STOP_QUIETLY_REMOTE)
3464 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
3469 internal_error (__FILE__, __LINE__,
3470 _("unhandled stop_soon: %d"), (int) stop_soon);
3472 case TARGET_WAITKIND_SPURIOUS:
3474 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
3475 if (!ptid_equal (ecs->ptid, inferior_ptid))
3476 context_switch (ecs->ptid);
3477 resume (0, GDB_SIGNAL_0);
3478 prepare_to_wait (ecs);
3481 case TARGET_WAITKIND_EXITED:
3482 case TARGET_WAITKIND_SIGNALLED:
3485 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3486 fprintf_unfiltered (gdb_stdlog,
3487 "infrun: TARGET_WAITKIND_EXITED\n");
3489 fprintf_unfiltered (gdb_stdlog,
3490 "infrun: TARGET_WAITKIND_SIGNALLED\n");
3493 inferior_ptid = ecs->ptid;
3494 set_current_inferior (find_inferior_pid (ptid_get_pid (ecs->ptid)));
3495 set_current_program_space (current_inferior ()->pspace);
3496 handle_vfork_child_exec_or_exit (0);
3497 target_terminal_ours (); /* Must do this before mourn anyway. */
3499 /* Clearing any previous state of convenience variables. */
3500 clear_exit_convenience_vars ();
3502 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3504 /* Record the exit code in the convenience variable $_exitcode, so
3505 that the user can inspect this again later. */
3506 set_internalvar_integer (lookup_internalvar ("_exitcode"),
3507 (LONGEST) ecs->ws.value.integer);
3509 /* Also record this in the inferior itself. */
3510 current_inferior ()->has_exit_code = 1;
3511 current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
3513 /* Support the --return-child-result option. */
3514 return_child_result_value = ecs->ws.value.integer;
3516 observer_notify_exited (ecs->ws.value.integer);
3520 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3521 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3523 if (gdbarch_gdb_signal_to_target_p (gdbarch))
3525 /* Set the value of the internal variable $_exitsignal,
3526 which holds the signal uncaught by the inferior. */
3527 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
3528 gdbarch_gdb_signal_to_target (gdbarch,
3529 ecs->ws.value.sig));
3533 /* We don't have access to the target's method used for
3534 converting between signal numbers (GDB's internal
3535 representation <-> target's representation).
3536 Therefore, we cannot do a good job at displaying this
3537 information to the user. It's better to just warn
3538 her about it (if infrun debugging is enabled), and
3541 fprintf_filtered (gdb_stdlog, _("\
3542 Cannot fill $_exitsignal with the correct signal number.\n"));
3545 observer_notify_signal_exited (ecs->ws.value.sig);
3548 gdb_flush (gdb_stdout);
3549 target_mourn_inferior ();
3550 singlestep_breakpoints_inserted_p = 0;
3551 cancel_single_step_breakpoints ();
3552 stop_print_frame = 0;
3556 /* The following are the only cases in which we keep going;
3557 the above cases end in a continue or goto. */
3558 case TARGET_WAITKIND_FORKED:
3559 case TARGET_WAITKIND_VFORKED:
3562 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3563 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
3565 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_VFORKED\n");
3568 /* Check whether the inferior is displaced stepping. */
3570 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3571 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3572 struct displaced_step_inferior_state *displaced
3573 = get_displaced_stepping_state (ptid_get_pid (ecs->ptid));
3575 /* If checking displaced stepping is supported, and thread
3576 ecs->ptid is displaced stepping. */
3577 if (displaced && ptid_equal (displaced->step_ptid, ecs->ptid))
3579 struct inferior *parent_inf
3580 = find_inferior_pid (ptid_get_pid (ecs->ptid));
3581 struct regcache *child_regcache;
3582 CORE_ADDR parent_pc;
3584 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
3585 indicating that the displaced stepping of syscall instruction
3586 has been done. Perform cleanup for parent process here. Note
3587 that this operation also cleans up the child process for vfork,
3588 because their pages are shared. */
3589 displaced_step_fixup (ecs->ptid, GDB_SIGNAL_TRAP);
3591 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3593 /* Restore scratch pad for child process. */
3594 displaced_step_restore (displaced, ecs->ws.value.related_pid);
3597 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
3598 the child's PC is also within the scratchpad. Set the child's PC
3599 to the parent's PC value, which has already been fixed up.
3600 FIXME: we use the parent's aspace here, although we're touching
3601 the child, because the child hasn't been added to the inferior
3602 list yet at this point. */
3605 = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
3607 parent_inf->aspace);
3608 /* Read PC value of parent process. */
3609 parent_pc = regcache_read_pc (regcache);
3611 if (debug_displaced)
3612 fprintf_unfiltered (gdb_stdlog,
3613 "displaced: write child pc from %s to %s\n",
3615 regcache_read_pc (child_regcache)),
3616 paddress (gdbarch, parent_pc));
3618 regcache_write_pc (child_regcache, parent_pc);
3622 if (!ptid_equal (ecs->ptid, inferior_ptid))
3623 context_switch (ecs->ptid);
3625 /* Immediately detach breakpoints from the child before there's
3626 any chance of letting the user delete breakpoints from the
3627 breakpoint lists. If we don't do this early, it's easy to
3628 leave left over traps in the child, vis: "break foo; catch
3629 fork; c; <fork>; del; c; <child calls foo>". We only follow
3630 the fork on the last `continue', and by that time the
3631 breakpoint at "foo" is long gone from the breakpoint table.
3632 If we vforked, then we don't need to unpatch here, since both
3633 parent and child are sharing the same memory pages; we'll
3634 need to unpatch at follow/detach time instead to be certain
3635 that new breakpoints added between catchpoint hit time and
3636 vfork follow are detached. */
3637 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
3639 /* This won't actually modify the breakpoint list, but will
3640 physically remove the breakpoints from the child. */
3641 detach_breakpoints (ecs->ws.value.related_pid);
3644 if (singlestep_breakpoints_inserted_p)
3646 /* Pull the single step breakpoints out of the target. */
3647 remove_single_step_breakpoints ();
3648 singlestep_breakpoints_inserted_p = 0;
3651 /* In case the event is caught by a catchpoint, remember that
3652 the event is to be followed at the next resume of the thread,
3653 and not immediately. */
3654 ecs->event_thread->pending_follow = ecs->ws;
3656 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3658 ecs->event_thread->control.stop_bpstat
3659 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3660 stop_pc, ecs->ptid, &ecs->ws);
3662 /* If no catchpoint triggered for this, then keep going. Note
3663 that we're interested in knowing the bpstat actually causes a
3664 stop, not just if it may explain the signal. Software
3665 watchpoints, for example, always appear in the bpstat. */
3666 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3672 = (follow_fork_mode_string == follow_fork_mode_child);
3674 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3676 should_resume = follow_fork ();
3679 child = ecs->ws.value.related_pid;
3681 /* In non-stop mode, also resume the other branch. */
3682 if (non_stop && !detach_fork)
3685 switch_to_thread (parent);
3687 switch_to_thread (child);
3689 ecs->event_thread = inferior_thread ();
3690 ecs->ptid = inferior_ptid;
3695 switch_to_thread (child);
3697 switch_to_thread (parent);
3699 ecs->event_thread = inferior_thread ();
3700 ecs->ptid = inferior_ptid;
3708 process_event_stop_test (ecs);
3711 case TARGET_WAITKIND_VFORK_DONE:
3712 /* Done with the shared memory region. Re-insert breakpoints in
3713 the parent, and keep going. */
3716 fprintf_unfiltered (gdb_stdlog,
3717 "infrun: TARGET_WAITKIND_VFORK_DONE\n");
3719 if (!ptid_equal (ecs->ptid, inferior_ptid))
3720 context_switch (ecs->ptid);
3722 current_inferior ()->waiting_for_vfork_done = 0;
3723 current_inferior ()->pspace->breakpoints_not_allowed = 0;
3724 /* This also takes care of reinserting breakpoints in the
3725 previously locked inferior. */
3729 case TARGET_WAITKIND_EXECD:
3731 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
3733 if (!ptid_equal (ecs->ptid, inferior_ptid))
3734 context_switch (ecs->ptid);
3736 singlestep_breakpoints_inserted_p = 0;
3737 cancel_single_step_breakpoints ();
3739 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3741 /* Do whatever is necessary to the parent branch of the vfork. */
3742 handle_vfork_child_exec_or_exit (1);
3744 /* This causes the eventpoints and symbol table to be reset.
3745 Must do this now, before trying to determine whether to
3747 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
3749 ecs->event_thread->control.stop_bpstat
3750 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3751 stop_pc, ecs->ptid, &ecs->ws);
3753 /* Note that this may be referenced from inside
3754 bpstat_stop_status above, through inferior_has_execd. */
3755 xfree (ecs->ws.value.execd_pathname);
3756 ecs->ws.value.execd_pathname = NULL;
3758 /* If no catchpoint triggered for this, then keep going. */
3759 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
3761 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3765 process_event_stop_test (ecs);
3768 /* Be careful not to try to gather much state about a thread
3769 that's in a syscall. It's frequently a losing proposition. */
3770 case TARGET_WAITKIND_SYSCALL_ENTRY:
3772 fprintf_unfiltered (gdb_stdlog,
3773 "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
3774 /* Getting the current syscall number. */
3775 if (handle_syscall_event (ecs) == 0)
3776 process_event_stop_test (ecs);
3779 /* Before examining the threads further, step this thread to
3780 get it entirely out of the syscall. (We get notice of the
3781 event when the thread is just on the verge of exiting a
3782 syscall. Stepping one instruction seems to get it back
3784 case TARGET_WAITKIND_SYSCALL_RETURN:
3786 fprintf_unfiltered (gdb_stdlog,
3787 "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
3788 if (handle_syscall_event (ecs) == 0)
3789 process_event_stop_test (ecs);
3792 case TARGET_WAITKIND_STOPPED:
3794 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
3795 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
3796 handle_signal_stop (ecs);
3799 case TARGET_WAITKIND_NO_HISTORY:
3801 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_HISTORY\n");
3802 /* Reverse execution: target ran out of history info. */
3804 /* Pull the single step breakpoints out of the target. */
3805 if (singlestep_breakpoints_inserted_p)
3807 if (!ptid_equal (ecs->ptid, inferior_ptid))
3808 context_switch (ecs->ptid);
3809 remove_single_step_breakpoints ();
3810 singlestep_breakpoints_inserted_p = 0;
3812 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3813 observer_notify_no_history ();
3819 /* Come here when the program has stopped with a signal. */
3822 handle_signal_stop (struct execution_control_state *ecs)
3824 struct frame_info *frame;
3825 struct gdbarch *gdbarch;
3826 int stopped_by_watchpoint;
3827 enum stop_kind stop_soon;
3830 gdb_assert (ecs->ws.kind == TARGET_WAITKIND_STOPPED);
3832 /* Do we need to clean up the state of a thread that has
3833 completed a displaced single-step? (Doing so usually affects
3834 the PC, so do it here, before we set stop_pc.) */
3835 displaced_step_fixup (ecs->ptid,
3836 ecs->event_thread->suspend.stop_signal);
3838 /* If we either finished a single-step or hit a breakpoint, but
3839 the user wanted this thread to be stopped, pretend we got a
3840 SIG0 (generic unsignaled stop). */
3841 if (ecs->event_thread->stop_requested
3842 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3843 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3845 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3849 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3850 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3851 struct cleanup *old_chain = save_inferior_ptid ();
3853 inferior_ptid = ecs->ptid;
3855 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
3856 paddress (gdbarch, stop_pc));
3857 if (target_stopped_by_watchpoint ())
3861 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
3863 if (target_stopped_data_address (¤t_target, &addr))
3864 fprintf_unfiltered (gdb_stdlog,
3865 "infrun: stopped data address = %s\n",
3866 paddress (gdbarch, addr));
3868 fprintf_unfiltered (gdb_stdlog,
3869 "infrun: (no data address available)\n");
3872 do_cleanups (old_chain);
3875 /* This is originated from start_remote(), start_inferior() and
3876 shared libraries hook functions. */
3877 stop_soon = get_inferior_stop_soon (ecs->ptid);
3878 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
3880 if (!ptid_equal (ecs->ptid, inferior_ptid))
3881 context_switch (ecs->ptid);
3883 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
3884 stop_print_frame = 1;
3889 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
3892 if (!ptid_equal (ecs->ptid, inferior_ptid))
3893 context_switch (ecs->ptid);
3895 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
3896 stop_print_frame = 0;
3901 /* This originates from attach_command(). We need to overwrite
3902 the stop_signal here, because some kernels don't ignore a
3903 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
3904 See more comments in inferior.h. On the other hand, if we
3905 get a non-SIGSTOP, report it to the user - assume the backend
3906 will handle the SIGSTOP if it should show up later.
3908 Also consider that the attach is complete when we see a
3909 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
3910 target extended-remote report it instead of a SIGSTOP
3911 (e.g. gdbserver). We already rely on SIGTRAP being our
3912 signal, so this is no exception.
3914 Also consider that the attach is complete when we see a
3915 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
3916 the target to stop all threads of the inferior, in case the
3917 low level attach operation doesn't stop them implicitly. If
3918 they weren't stopped implicitly, then the stub will report a
3919 GDB_SIGNAL_0, meaning: stopped for no particular reason
3920 other than GDB's request. */
3921 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3922 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
3923 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
3924 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
3926 stop_print_frame = 1;
3928 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
3932 /* See if something interesting happened to the non-current thread. If
3933 so, then switch to that thread. */
3934 if (!ptid_equal (ecs->ptid, inferior_ptid))
3937 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
3939 context_switch (ecs->ptid);
3941 if (deprecated_context_hook)
3942 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
3945 /* At this point, get hold of the now-current thread's frame. */
3946 frame = get_current_frame ();
3947 gdbarch = get_frame_arch (frame);
3949 /* Pull the single step breakpoints out of the target. */
3950 if (singlestep_breakpoints_inserted_p)
3952 /* However, before doing so, if this single-step breakpoint was
3953 actually for another thread, set this thread up for moving
3955 if (!ptid_equal (ecs->ptid, singlestep_ptid)
3956 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3958 struct regcache *regcache;
3959 struct address_space *aspace;
3962 regcache = get_thread_regcache (ecs->ptid);
3963 aspace = get_regcache_aspace (regcache);
3964 pc = regcache_read_pc (regcache);
3965 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3969 fprintf_unfiltered (gdb_stdlog,
3970 "infrun: [%s] hit step over single-step"
3971 " breakpoint of [%s]\n",
3972 target_pid_to_str (ecs->ptid),
3973 target_pid_to_str (singlestep_ptid));
3975 ecs->hit_singlestep_breakpoint = 1;
3979 remove_single_step_breakpoints ();
3980 singlestep_breakpoints_inserted_p = 0;
3983 if (ecs->stepped_after_stopped_by_watchpoint)
3984 stopped_by_watchpoint = 0;
3986 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
3988 /* If necessary, step over this watchpoint. We'll be back to display
3990 if (stopped_by_watchpoint
3991 && (target_have_steppable_watchpoint
3992 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
3994 /* At this point, we are stopped at an instruction which has
3995 attempted to write to a piece of memory under control of
3996 a watchpoint. The instruction hasn't actually executed
3997 yet. If we were to evaluate the watchpoint expression
3998 now, we would get the old value, and therefore no change
3999 would seem to have occurred.
4001 In order to make watchpoints work `right', we really need
4002 to complete the memory write, and then evaluate the
4003 watchpoint expression. We do this by single-stepping the
4006 It may not be necessary to disable the watchpoint to stop over
4007 it. For example, the PA can (with some kernel cooperation)
4008 single step over a watchpoint without disabling the watchpoint.
4010 It is far more common to need to disable a watchpoint to step
4011 the inferior over it. If we have non-steppable watchpoints,
4012 we must disable the current watchpoint; it's simplest to
4013 disable all watchpoints and breakpoints. */
4016 if (!target_have_steppable_watchpoint)
4018 remove_breakpoints ();
4019 /* See comment in resume why we need to stop bypassing signals
4020 while breakpoints have been removed. */
4021 target_pass_signals (0, NULL);
4024 hw_step = maybe_software_singlestep (gdbarch, stop_pc);
4025 target_resume (ecs->ptid, hw_step, GDB_SIGNAL_0);
4026 waiton_ptid = ecs->ptid;
4027 if (target_have_steppable_watchpoint)
4028 infwait_state = infwait_step_watch_state;
4030 infwait_state = infwait_nonstep_watch_state;
4031 prepare_to_wait (ecs);
4035 ecs->event_thread->stepping_over_breakpoint = 0;
4036 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4037 ecs->event_thread->control.stop_step = 0;
4038 stop_print_frame = 1;
4039 stopped_by_random_signal = 0;
4041 /* Hide inlined functions starting here, unless we just performed stepi or
4042 nexti. After stepi and nexti, always show the innermost frame (not any
4043 inline function call sites). */
4044 if (ecs->event_thread->control.step_range_end != 1)
4046 struct address_space *aspace =
4047 get_regcache_aspace (get_thread_regcache (ecs->ptid));
4049 /* skip_inline_frames is expensive, so we avoid it if we can
4050 determine that the address is one where functions cannot have
4051 been inlined. This improves performance with inferiors that
4052 load a lot of shared libraries, because the solib event
4053 breakpoint is defined as the address of a function (i.e. not
4054 inline). Note that we have to check the previous PC as well
4055 as the current one to catch cases when we have just
4056 single-stepped off a breakpoint prior to reinstating it.
4057 Note that we're assuming that the code we single-step to is
4058 not inline, but that's not definitive: there's nothing
4059 preventing the event breakpoint function from containing
4060 inlined code, and the single-step ending up there. If the
4061 user had set a breakpoint on that inlined code, the missing
4062 skip_inline_frames call would break things. Fortunately
4063 that's an extremely unlikely scenario. */
4064 if (!pc_at_non_inline_function (aspace, stop_pc, &ecs->ws)
4065 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4066 && ecs->event_thread->control.trap_expected
4067 && pc_at_non_inline_function (aspace,
4068 ecs->event_thread->prev_pc,
4071 skip_inline_frames (ecs->ptid);
4073 /* Re-fetch current thread's frame in case that invalidated
4075 frame = get_current_frame ();
4076 gdbarch = get_frame_arch (frame);
4080 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4081 && ecs->event_thread->control.trap_expected
4082 && gdbarch_single_step_through_delay_p (gdbarch)
4083 && currently_stepping (ecs->event_thread))
4085 /* We're trying to step off a breakpoint. Turns out that we're
4086 also on an instruction that needs to be stepped multiple
4087 times before it's been fully executing. E.g., architectures
4088 with a delay slot. It needs to be stepped twice, once for
4089 the instruction and once for the delay slot. */
4090 int step_through_delay
4091 = gdbarch_single_step_through_delay (gdbarch, frame);
4093 if (debug_infrun && step_through_delay)
4094 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
4095 if (ecs->event_thread->control.step_range_end == 0
4096 && step_through_delay)
4098 /* The user issued a continue when stopped at a breakpoint.
4099 Set up for another trap and get out of here. */
4100 ecs->event_thread->stepping_over_breakpoint = 1;
4104 else if (step_through_delay)
4106 /* The user issued a step when stopped at a breakpoint.
4107 Maybe we should stop, maybe we should not - the delay
4108 slot *might* correspond to a line of source. In any
4109 case, don't decide that here, just set
4110 ecs->stepping_over_breakpoint, making sure we
4111 single-step again before breakpoints are re-inserted. */
4112 ecs->event_thread->stepping_over_breakpoint = 1;
4116 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
4117 handles this event. */
4118 ecs->event_thread->control.stop_bpstat
4119 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
4120 stop_pc, ecs->ptid, &ecs->ws);
4122 /* Following in case break condition called a
4124 stop_print_frame = 1;
4126 /* This is where we handle "moribund" watchpoints. Unlike
4127 software breakpoints traps, hardware watchpoint traps are
4128 always distinguishable from random traps. If no high-level
4129 watchpoint is associated with the reported stop data address
4130 anymore, then the bpstat does not explain the signal ---
4131 simply make sure to ignore it if `stopped_by_watchpoint' is
4135 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4136 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4138 && stopped_by_watchpoint)
4139 fprintf_unfiltered (gdb_stdlog,
4140 "infrun: no user watchpoint explains "
4141 "watchpoint SIGTRAP, ignoring\n");
4143 /* NOTE: cagney/2003-03-29: These checks for a random signal
4144 at one stage in the past included checks for an inferior
4145 function call's call dummy's return breakpoint. The original
4146 comment, that went with the test, read:
4148 ``End of a stack dummy. Some systems (e.g. Sony news) give
4149 another signal besides SIGTRAP, so check here as well as
4152 If someone ever tries to get call dummys on a
4153 non-executable stack to work (where the target would stop
4154 with something like a SIGSEGV), then those tests might need
4155 to be re-instated. Given, however, that the tests were only
4156 enabled when momentary breakpoints were not being used, I
4157 suspect that it won't be the case.
4159 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4160 be necessary for call dummies on a non-executable stack on
4163 /* See if the breakpoints module can explain the signal. */
4165 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4166 ecs->event_thread->suspend.stop_signal);
4168 /* If not, perhaps stepping/nexting can. */
4170 random_signal = !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4171 && currently_stepping (ecs->event_thread));
4173 /* Perhaps the thread hit a single-step breakpoint of _another_
4174 thread. Single-step breakpoints are transparent to the
4175 breakpoints module. */
4177 random_signal = !ecs->hit_singlestep_breakpoint;
4179 /* No? Perhaps we got a moribund watchpoint. */
4181 random_signal = !stopped_by_watchpoint;
4183 /* For the program's own signals, act according to
4184 the signal handling tables. */
4188 /* Signal not for debugging purposes. */
4190 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
4191 enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
4194 fprintf_unfiltered (gdb_stdlog, "infrun: random signal (%s)\n",
4195 gdb_signal_to_symbol_string (stop_signal));
4197 stopped_by_random_signal = 1;
4199 if (signal_print[ecs->event_thread->suspend.stop_signal])
4201 /* The signal table tells us to print about this signal. */
4203 target_terminal_ours_for_output ();
4204 observer_notify_signal_received (ecs->event_thread->suspend.stop_signal);
4206 /* Always stop on signals if we're either just gaining control
4207 of the program, or the user explicitly requested this thread
4208 to remain stopped. */
4209 if (stop_soon != NO_STOP_QUIETLY
4210 || ecs->event_thread->stop_requested
4212 && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
4217 /* If not going to stop, give terminal back
4218 if we took it away. */
4220 target_terminal_inferior ();
4222 /* Clear the signal if it should not be passed. */
4223 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
4224 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
4226 if (ecs->event_thread->prev_pc == stop_pc
4227 && ecs->event_thread->control.trap_expected
4228 && ecs->event_thread->control.step_resume_breakpoint == NULL)
4230 /* We were just starting a new sequence, attempting to
4231 single-step off of a breakpoint and expecting a SIGTRAP.
4232 Instead this signal arrives. This signal will take us out
4233 of the stepping range so GDB needs to remember to, when
4234 the signal handler returns, resume stepping off that
4236 /* To simplify things, "continue" is forced to use the same
4237 code paths as single-step - set a breakpoint at the
4238 signal return address and then, once hit, step off that
4241 fprintf_unfiltered (gdb_stdlog,
4242 "infrun: signal arrived while stepping over "
4245 insert_hp_step_resume_breakpoint_at_frame (frame);
4246 ecs->event_thread->step_after_step_resume_breakpoint = 1;
4247 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4248 ecs->event_thread->control.trap_expected = 0;
4250 /* If we were nexting/stepping some other thread, switch to
4251 it, so that we don't continue it, losing control. */
4252 if (!switch_back_to_stepped_thread (ecs))
4257 if (ecs->event_thread->control.step_range_end != 0
4258 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
4259 && pc_in_thread_step_range (stop_pc, ecs->event_thread)
4260 && frame_id_eq (get_stack_frame_id (frame),
4261 ecs->event_thread->control.step_stack_frame_id)
4262 && ecs->event_thread->control.step_resume_breakpoint == NULL)
4264 /* The inferior is about to take a signal that will take it
4265 out of the single step range. Set a breakpoint at the
4266 current PC (which is presumably where the signal handler
4267 will eventually return) and then allow the inferior to
4270 Note that this is only needed for a signal delivered
4271 while in the single-step range. Nested signals aren't a
4272 problem as they eventually all return. */
4274 fprintf_unfiltered (gdb_stdlog,
4275 "infrun: signal may take us out of "
4276 "single-step range\n");
4278 insert_hp_step_resume_breakpoint_at_frame (frame);
4279 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4280 ecs->event_thread->control.trap_expected = 0;
4285 /* Note: step_resume_breakpoint may be non-NULL. This occures
4286 when either there's a nested signal, or when there's a
4287 pending signal enabled just as the signal handler returns
4288 (leaving the inferior at the step-resume-breakpoint without
4289 actually executing it). Either way continue until the
4290 breakpoint is really hit. */
4292 if (!switch_back_to_stepped_thread (ecs))
4295 fprintf_unfiltered (gdb_stdlog,
4296 "infrun: random signal, keep going\n");
4303 process_event_stop_test (ecs);
4306 /* Come here when we've got some debug event / signal we can explain
4307 (IOW, not a random signal), and test whether it should cause a
4308 stop, or whether we should resume the inferior (transparently).
4309 E.g., could be a breakpoint whose condition evaluates false; we
4310 could be still stepping within the line; etc. */
4313 process_event_stop_test (struct execution_control_state *ecs)
4315 struct symtab_and_line stop_pc_sal;
4316 struct frame_info *frame;
4317 struct gdbarch *gdbarch;
4318 CORE_ADDR jmp_buf_pc;
4319 struct bpstat_what what;
4321 /* Handle cases caused by hitting a breakpoint. */
4323 frame = get_current_frame ();
4324 gdbarch = get_frame_arch (frame);
4326 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
4328 if (what.call_dummy)
4330 stop_stack_dummy = what.call_dummy;
4333 /* If we hit an internal event that triggers symbol changes, the
4334 current frame will be invalidated within bpstat_what (e.g., if we
4335 hit an internal solib event). Re-fetch it. */
4336 frame = get_current_frame ();
4337 gdbarch = get_frame_arch (frame);
4339 switch (what.main_action)
4341 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4342 /* If we hit the breakpoint at longjmp while stepping, we
4343 install a momentary breakpoint at the target of the
4347 fprintf_unfiltered (gdb_stdlog,
4348 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
4350 ecs->event_thread->stepping_over_breakpoint = 1;
4352 if (what.is_longjmp)
4354 struct value *arg_value;
4356 /* If we set the longjmp breakpoint via a SystemTap probe,
4357 then use it to extract the arguments. The destination PC
4358 is the third argument to the probe. */
4359 arg_value = probe_safe_evaluate_at_pc (frame, 2);
4361 jmp_buf_pc = value_as_address (arg_value);
4362 else if (!gdbarch_get_longjmp_target_p (gdbarch)
4363 || !gdbarch_get_longjmp_target (gdbarch,
4364 frame, &jmp_buf_pc))
4367 fprintf_unfiltered (gdb_stdlog,
4368 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4369 "(!gdbarch_get_longjmp_target)\n");
4374 /* Insert a breakpoint at resume address. */
4375 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
4378 check_exception_resume (ecs, frame);
4382 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4384 struct frame_info *init_frame;
4386 /* There are several cases to consider.
4388 1. The initiating frame no longer exists. In this case we
4389 must stop, because the exception or longjmp has gone too
4392 2. The initiating frame exists, and is the same as the
4393 current frame. We stop, because the exception or longjmp
4396 3. The initiating frame exists and is different from the
4397 current frame. This means the exception or longjmp has
4398 been caught beneath the initiating frame, so keep going.
4400 4. longjmp breakpoint has been placed just to protect
4401 against stale dummy frames and user is not interested in
4402 stopping around longjmps. */
4405 fprintf_unfiltered (gdb_stdlog,
4406 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
4408 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4410 delete_exception_resume_breakpoint (ecs->event_thread);
4412 if (what.is_longjmp)
4414 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread);
4416 if (!frame_id_p (ecs->event_thread->initiating_frame))
4424 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
4428 struct frame_id current_id
4429 = get_frame_id (get_current_frame ());
4430 if (frame_id_eq (current_id,
4431 ecs->event_thread->initiating_frame))
4433 /* Case 2. Fall through. */
4443 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
4445 delete_step_resume_breakpoint (ecs->event_thread);
4447 end_stepping_range (ecs);
4451 case BPSTAT_WHAT_SINGLE:
4453 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4454 ecs->event_thread->stepping_over_breakpoint = 1;
4455 /* Still need to check other stuff, at least the case where we
4456 are stepping and step out of the right range. */
4459 case BPSTAT_WHAT_STEP_RESUME:
4461 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
4463 delete_step_resume_breakpoint (ecs->event_thread);
4464 if (ecs->event_thread->control.proceed_to_finish
4465 && execution_direction == EXEC_REVERSE)
4467 struct thread_info *tp = ecs->event_thread;
4469 /* We are finishing a function in reverse, and just hit the
4470 step-resume breakpoint at the start address of the
4471 function, and we're almost there -- just need to back up
4472 by one more single-step, which should take us back to the
4474 tp->control.step_range_start = tp->control.step_range_end = 1;
4478 fill_in_stop_func (gdbarch, ecs);
4479 if (stop_pc == ecs->stop_func_start
4480 && execution_direction == EXEC_REVERSE)
4482 /* We are stepping over a function call in reverse, and just
4483 hit the step-resume breakpoint at the start address of
4484 the function. Go back to single-stepping, which should
4485 take us back to the function call. */
4486 ecs->event_thread->stepping_over_breakpoint = 1;
4492 case BPSTAT_WHAT_STOP_NOISY:
4494 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
4495 stop_print_frame = 1;
4497 /* Assume the thread stopped for a breapoint. We'll still check
4498 whether a/the breakpoint is there when the thread is next
4500 ecs->event_thread->stepping_over_breakpoint = 1;
4505 case BPSTAT_WHAT_STOP_SILENT:
4507 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
4508 stop_print_frame = 0;
4510 /* Assume the thread stopped for a breapoint. We'll still check
4511 whether a/the breakpoint is there when the thread is next
4513 ecs->event_thread->stepping_over_breakpoint = 1;
4517 case BPSTAT_WHAT_HP_STEP_RESUME:
4519 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
4521 delete_step_resume_breakpoint (ecs->event_thread);
4522 if (ecs->event_thread->step_after_step_resume_breakpoint)
4524 /* Back when the step-resume breakpoint was inserted, we
4525 were trying to single-step off a breakpoint. Go back to
4527 ecs->event_thread->step_after_step_resume_breakpoint = 0;
4528 ecs->event_thread->stepping_over_breakpoint = 1;
4534 case BPSTAT_WHAT_KEEP_CHECKING:
4538 /* We come here if we hit a breakpoint but should not stop for it.
4539 Possibly we also were stepping and should stop for that. So fall
4540 through and test for stepping. But, if not stepping, do not
4543 /* In all-stop mode, if we're currently stepping but have stopped in
4544 some other thread, we need to switch back to the stepped thread. */
4545 if (switch_back_to_stepped_thread (ecs))
4548 if (ecs->event_thread->control.step_resume_breakpoint)
4551 fprintf_unfiltered (gdb_stdlog,
4552 "infrun: step-resume breakpoint is inserted\n");
4554 /* Having a step-resume breakpoint overrides anything
4555 else having to do with stepping commands until
4556 that breakpoint is reached. */
4561 if (ecs->event_thread->control.step_range_end == 0)
4564 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
4565 /* Likewise if we aren't even stepping. */
4570 /* Re-fetch current thread's frame in case the code above caused
4571 the frame cache to be re-initialized, making our FRAME variable
4572 a dangling pointer. */
4573 frame = get_current_frame ();
4574 gdbarch = get_frame_arch (frame);
4575 fill_in_stop_func (gdbarch, ecs);
4577 /* If stepping through a line, keep going if still within it.
4579 Note that step_range_end is the address of the first instruction
4580 beyond the step range, and NOT the address of the last instruction
4583 Note also that during reverse execution, we may be stepping
4584 through a function epilogue and therefore must detect when
4585 the current-frame changes in the middle of a line. */
4587 if (pc_in_thread_step_range (stop_pc, ecs->event_thread)
4588 && (execution_direction != EXEC_REVERSE
4589 || frame_id_eq (get_frame_id (frame),
4590 ecs->event_thread->control.step_frame_id)))
4594 (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
4595 paddress (gdbarch, ecs->event_thread->control.step_range_start),
4596 paddress (gdbarch, ecs->event_thread->control.step_range_end));
4598 /* Tentatively re-enable range stepping; `resume' disables it if
4599 necessary (e.g., if we're stepping over a breakpoint or we
4600 have software watchpoints). */
4601 ecs->event_thread->control.may_range_step = 1;
4603 /* When stepping backward, stop at beginning of line range
4604 (unless it's the function entry point, in which case
4605 keep going back to the call point). */
4606 if (stop_pc == ecs->event_thread->control.step_range_start
4607 && stop_pc != ecs->stop_func_start
4608 && execution_direction == EXEC_REVERSE)
4609 end_stepping_range (ecs);
4616 /* We stepped out of the stepping range. */
4618 /* If we are stepping at the source level and entered the runtime
4619 loader dynamic symbol resolution code...
4621 EXEC_FORWARD: we keep on single stepping until we exit the run
4622 time loader code and reach the callee's address.
4624 EXEC_REVERSE: we've already executed the callee (backward), and
4625 the runtime loader code is handled just like any other
4626 undebuggable function call. Now we need only keep stepping
4627 backward through the trampoline code, and that's handled further
4628 down, so there is nothing for us to do here. */
4630 if (execution_direction != EXEC_REVERSE
4631 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4632 && in_solib_dynsym_resolve_code (stop_pc))
4634 CORE_ADDR pc_after_resolver =
4635 gdbarch_skip_solib_resolver (gdbarch, stop_pc);
4638 fprintf_unfiltered (gdb_stdlog,
4639 "infrun: stepped into dynsym resolve code\n");
4641 if (pc_after_resolver)
4643 /* Set up a step-resume breakpoint at the address
4644 indicated by SKIP_SOLIB_RESOLVER. */
4645 struct symtab_and_line sr_sal;
4648 sr_sal.pc = pc_after_resolver;
4649 sr_sal.pspace = get_frame_program_space (frame);
4651 insert_step_resume_breakpoint_at_sal (gdbarch,
4652 sr_sal, null_frame_id);
4659 if (ecs->event_thread->control.step_range_end != 1
4660 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4661 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4662 && get_frame_type (frame) == SIGTRAMP_FRAME)
4665 fprintf_unfiltered (gdb_stdlog,
4666 "infrun: stepped into signal trampoline\n");
4667 /* The inferior, while doing a "step" or "next", has ended up in
4668 a signal trampoline (either by a signal being delivered or by
4669 the signal handler returning). Just single-step until the
4670 inferior leaves the trampoline (either by calling the handler
4676 /* If we're in the return path from a shared library trampoline,
4677 we want to proceed through the trampoline when stepping. */
4678 /* macro/2012-04-25: This needs to come before the subroutine
4679 call check below as on some targets return trampolines look
4680 like subroutine calls (MIPS16 return thunks). */
4681 if (gdbarch_in_solib_return_trampoline (gdbarch,
4682 stop_pc, ecs->stop_func_name)
4683 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
4685 /* Determine where this trampoline returns. */
4686 CORE_ADDR real_stop_pc;
4688 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4691 fprintf_unfiltered (gdb_stdlog,
4692 "infrun: stepped into solib return tramp\n");
4694 /* Only proceed through if we know where it's going. */
4697 /* And put the step-breakpoint there and go until there. */
4698 struct symtab_and_line sr_sal;
4700 init_sal (&sr_sal); /* initialize to zeroes */
4701 sr_sal.pc = real_stop_pc;
4702 sr_sal.section = find_pc_overlay (sr_sal.pc);
4703 sr_sal.pspace = get_frame_program_space (frame);
4705 /* Do not specify what the fp should be when we stop since
4706 on some machines the prologue is where the new fp value
4708 insert_step_resume_breakpoint_at_sal (gdbarch,
4709 sr_sal, null_frame_id);
4711 /* Restart without fiddling with the step ranges or
4718 /* Check for subroutine calls. The check for the current frame
4719 equalling the step ID is not necessary - the check of the
4720 previous frame's ID is sufficient - but it is a common case and
4721 cheaper than checking the previous frame's ID.
4723 NOTE: frame_id_eq will never report two invalid frame IDs as
4724 being equal, so to get into this block, both the current and
4725 previous frame must have valid frame IDs. */
4726 /* The outer_frame_id check is a heuristic to detect stepping
4727 through startup code. If we step over an instruction which
4728 sets the stack pointer from an invalid value to a valid value,
4729 we may detect that as a subroutine call from the mythical
4730 "outermost" function. This could be fixed by marking
4731 outermost frames as !stack_p,code_p,special_p. Then the
4732 initial outermost frame, before sp was valid, would
4733 have code_addr == &_start. See the comment in frame_id_eq
4735 if (!frame_id_eq (get_stack_frame_id (frame),
4736 ecs->event_thread->control.step_stack_frame_id)
4737 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
4738 ecs->event_thread->control.step_stack_frame_id)
4739 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
4741 || step_start_function != find_pc_function (stop_pc))))
4743 CORE_ADDR real_stop_pc;
4746 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
4748 if ((ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
4749 || ((ecs->event_thread->control.step_range_end == 1)
4750 && in_prologue (gdbarch, ecs->event_thread->prev_pc,
4751 ecs->stop_func_start)))
4753 /* I presume that step_over_calls is only 0 when we're
4754 supposed to be stepping at the assembly language level
4755 ("stepi"). Just stop. */
4756 /* Also, maybe we just did a "nexti" inside a prolog, so we
4757 thought it was a subroutine call but it was not. Stop as
4759 /* And this works the same backward as frontward. MVS */
4760 end_stepping_range (ecs);
4764 /* Reverse stepping through solib trampolines. */
4766 if (execution_direction == EXEC_REVERSE
4767 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
4768 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4769 || (ecs->stop_func_start == 0
4770 && in_solib_dynsym_resolve_code (stop_pc))))
4772 /* Any solib trampoline code can be handled in reverse
4773 by simply continuing to single-step. We have already
4774 executed the solib function (backwards), and a few
4775 steps will take us back through the trampoline to the
4781 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4783 /* We're doing a "next".
4785 Normal (forward) execution: set a breakpoint at the
4786 callee's return address (the address at which the caller
4789 Reverse (backward) execution. set the step-resume
4790 breakpoint at the start of the function that we just
4791 stepped into (backwards), and continue to there. When we
4792 get there, we'll need to single-step back to the caller. */
4794 if (execution_direction == EXEC_REVERSE)
4796 /* If we're already at the start of the function, we've either
4797 just stepped backward into a single instruction function,
4798 or stepped back out of a signal handler to the first instruction
4799 of the function. Just keep going, which will single-step back
4801 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
4803 struct symtab_and_line sr_sal;
4805 /* Normal function call return (static or dynamic). */
4807 sr_sal.pc = ecs->stop_func_start;
4808 sr_sal.pspace = get_frame_program_space (frame);
4809 insert_step_resume_breakpoint_at_sal (gdbarch,
4810 sr_sal, null_frame_id);
4814 insert_step_resume_breakpoint_at_caller (frame);
4820 /* If we are in a function call trampoline (a stub between the
4821 calling routine and the real function), locate the real
4822 function. That's what tells us (a) whether we want to step
4823 into it at all, and (b) what prologue we want to run to the
4824 end of, if we do step into it. */
4825 real_stop_pc = skip_language_trampoline (frame, stop_pc);
4826 if (real_stop_pc == 0)
4827 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4828 if (real_stop_pc != 0)
4829 ecs->stop_func_start = real_stop_pc;
4831 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
4833 struct symtab_and_line sr_sal;
4836 sr_sal.pc = ecs->stop_func_start;
4837 sr_sal.pspace = get_frame_program_space (frame);
4839 insert_step_resume_breakpoint_at_sal (gdbarch,
4840 sr_sal, null_frame_id);
4845 /* If we have line number information for the function we are
4846 thinking of stepping into and the function isn't on the skip
4849 If there are several symtabs at that PC (e.g. with include
4850 files), just want to know whether *any* of them have line
4851 numbers. find_pc_line handles this. */
4853 struct symtab_and_line tmp_sal;
4855 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
4856 if (tmp_sal.line != 0
4857 && !function_name_is_marked_for_skip (ecs->stop_func_name,
4860 if (execution_direction == EXEC_REVERSE)
4861 handle_step_into_function_backward (gdbarch, ecs);
4863 handle_step_into_function (gdbarch, ecs);
4868 /* If we have no line number and the step-stop-if-no-debug is
4869 set, we stop the step so that the user has a chance to switch
4870 in assembly mode. */
4871 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4872 && step_stop_if_no_debug)
4874 end_stepping_range (ecs);
4878 if (execution_direction == EXEC_REVERSE)
4880 /* If we're already at the start of the function, we've either just
4881 stepped backward into a single instruction function without line
4882 number info, or stepped back out of a signal handler to the first
4883 instruction of the function without line number info. Just keep
4884 going, which will single-step back to the caller. */
4885 if (ecs->stop_func_start != stop_pc)
4887 /* Set a breakpoint at callee's start address.
4888 From there we can step once and be back in the caller. */
4889 struct symtab_and_line sr_sal;
4892 sr_sal.pc = ecs->stop_func_start;
4893 sr_sal.pspace = get_frame_program_space (frame);
4894 insert_step_resume_breakpoint_at_sal (gdbarch,
4895 sr_sal, null_frame_id);
4899 /* Set a breakpoint at callee's return address (the address
4900 at which the caller will resume). */
4901 insert_step_resume_breakpoint_at_caller (frame);
4907 /* Reverse stepping through solib trampolines. */
4909 if (execution_direction == EXEC_REVERSE
4910 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
4912 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4913 || (ecs->stop_func_start == 0
4914 && in_solib_dynsym_resolve_code (stop_pc)))
4916 /* Any solib trampoline code can be handled in reverse
4917 by simply continuing to single-step. We have already
4918 executed the solib function (backwards), and a few
4919 steps will take us back through the trampoline to the
4924 else if (in_solib_dynsym_resolve_code (stop_pc))
4926 /* Stepped backward into the solib dynsym resolver.
4927 Set a breakpoint at its start and continue, then
4928 one more step will take us out. */
4929 struct symtab_and_line sr_sal;
4932 sr_sal.pc = ecs->stop_func_start;
4933 sr_sal.pspace = get_frame_program_space (frame);
4934 insert_step_resume_breakpoint_at_sal (gdbarch,
4935 sr_sal, null_frame_id);
4941 stop_pc_sal = find_pc_line (stop_pc, 0);
4943 /* NOTE: tausq/2004-05-24: This if block used to be done before all
4944 the trampoline processing logic, however, there are some trampolines
4945 that have no names, so we should do trampoline handling first. */
4946 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4947 && ecs->stop_func_name == NULL
4948 && stop_pc_sal.line == 0)
4951 fprintf_unfiltered (gdb_stdlog,
4952 "infrun: stepped into undebuggable function\n");
4954 /* The inferior just stepped into, or returned to, an
4955 undebuggable function (where there is no debugging information
4956 and no line number corresponding to the address where the
4957 inferior stopped). Since we want to skip this kind of code,
4958 we keep going until the inferior returns from this
4959 function - unless the user has asked us not to (via
4960 set step-mode) or we no longer know how to get back
4961 to the call site. */
4962 if (step_stop_if_no_debug
4963 || !frame_id_p (frame_unwind_caller_id (frame)))
4965 /* If we have no line number and the step-stop-if-no-debug
4966 is set, we stop the step so that the user has a chance to
4967 switch in assembly mode. */
4968 end_stepping_range (ecs);
4973 /* Set a breakpoint at callee's return address (the address
4974 at which the caller will resume). */
4975 insert_step_resume_breakpoint_at_caller (frame);
4981 if (ecs->event_thread->control.step_range_end == 1)
4983 /* It is stepi or nexti. We always want to stop stepping after
4986 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
4987 end_stepping_range (ecs);
4991 if (stop_pc_sal.line == 0)
4993 /* We have no line number information. That means to stop
4994 stepping (does this always happen right after one instruction,
4995 when we do "s" in a function with no line numbers,
4996 or can this happen as a result of a return or longjmp?). */
4998 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
4999 end_stepping_range (ecs);
5003 /* Look for "calls" to inlined functions, part one. If the inline
5004 frame machinery detected some skipped call sites, we have entered
5005 a new inline function. */
5007 if (frame_id_eq (get_frame_id (get_current_frame ()),
5008 ecs->event_thread->control.step_frame_id)
5009 && inline_skipped_frames (ecs->ptid))
5011 struct symtab_and_line call_sal;
5014 fprintf_unfiltered (gdb_stdlog,
5015 "infrun: stepped into inlined function\n");
5017 find_frame_sal (get_current_frame (), &call_sal);
5019 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
5021 /* For "step", we're going to stop. But if the call site
5022 for this inlined function is on the same source line as
5023 we were previously stepping, go down into the function
5024 first. Otherwise stop at the call site. */
5026 if (call_sal.line == ecs->event_thread->current_line
5027 && call_sal.symtab == ecs->event_thread->current_symtab)
5028 step_into_inline_frame (ecs->ptid);
5030 end_stepping_range (ecs);
5035 /* For "next", we should stop at the call site if it is on a
5036 different source line. Otherwise continue through the
5037 inlined function. */
5038 if (call_sal.line == ecs->event_thread->current_line
5039 && call_sal.symtab == ecs->event_thread->current_symtab)
5042 end_stepping_range (ecs);
5047 /* Look for "calls" to inlined functions, part two. If we are still
5048 in the same real function we were stepping through, but we have
5049 to go further up to find the exact frame ID, we are stepping
5050 through a more inlined call beyond its call site. */
5052 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5053 && !frame_id_eq (get_frame_id (get_current_frame ()),
5054 ecs->event_thread->control.step_frame_id)
5055 && stepped_in_from (get_current_frame (),
5056 ecs->event_thread->control.step_frame_id))
5059 fprintf_unfiltered (gdb_stdlog,
5060 "infrun: stepping through inlined function\n");
5062 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
5065 end_stepping_range (ecs);
5069 if ((stop_pc == stop_pc_sal.pc)
5070 && (ecs->event_thread->current_line != stop_pc_sal.line
5071 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
5073 /* We are at the start of a different line. So stop. Note that
5074 we don't stop if we step into the middle of a different line.
5075 That is said to make things like for (;;) statements work
5078 fprintf_unfiltered (gdb_stdlog,
5079 "infrun: stepped to a different line\n");
5080 end_stepping_range (ecs);
5084 /* We aren't done stepping.
5086 Optimize by setting the stepping range to the line.
5087 (We might not be in the original line, but if we entered a
5088 new line in mid-statement, we continue stepping. This makes
5089 things like for(;;) statements work better.) */
5091 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5092 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
5093 ecs->event_thread->control.may_range_step = 1;
5094 set_step_info (frame, stop_pc_sal);
5097 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
5101 /* In all-stop mode, if we're currently stepping but have stopped in
5102 some other thread, we may need to switch back to the stepped
5103 thread. Returns true we set the inferior running, false if we left
5104 it stopped (and the event needs further processing). */
5107 switch_back_to_stepped_thread (struct execution_control_state *ecs)
5111 struct thread_info *tp;
5112 struct thread_info *stepping_thread;
5113 struct thread_info *step_over;
5115 /* If any thread is blocked on some internal breakpoint, and we
5116 simply need to step over that breakpoint to get it going
5117 again, do that first. */
5119 /* However, if we see an event for the stepping thread, then we
5120 know all other threads have been moved past their breakpoints
5121 already. Let the caller check whether the step is finished,
5122 etc., before deciding to move it past a breakpoint. */
5123 if (ecs->event_thread->control.step_range_end != 0)
5126 /* Check if the current thread is blocked on an incomplete
5127 step-over, interrupted by a random signal. */
5128 if (ecs->event_thread->control.trap_expected
5129 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
5133 fprintf_unfiltered (gdb_stdlog,
5134 "infrun: need to finish step-over of [%s]\n",
5135 target_pid_to_str (ecs->event_thread->ptid));
5141 /* Check if the current thread is blocked by a single-step
5142 breakpoint of another thread. */
5143 if (ecs->hit_singlestep_breakpoint)
5147 fprintf_unfiltered (gdb_stdlog,
5148 "infrun: need to step [%s] over single-step "
5150 target_pid_to_str (ecs->ptid));
5156 /* Otherwise, we no longer expect a trap in the current thread.
5157 Clear the trap_expected flag before switching back -- this is
5158 what keep_going does as well, if we call it. */
5159 ecs->event_thread->control.trap_expected = 0;
5161 /* Likewise, clear the signal if it should not be passed. */
5162 if (!signal_program[ecs->event_thread->suspend.stop_signal])
5163 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
5165 /* If scheduler locking applies even if not stepping, there's no
5166 need to walk over threads. Above we've checked whether the
5167 current thread is stepping. If some other thread not the
5168 event thread is stepping, then it must be that scheduler
5169 locking is not in effect. */
5170 if (schedlock_applies (0))
5173 /* Look for the stepping/nexting thread, and check if any other
5174 thread other than the stepping thread needs to start a
5175 step-over. Do all step-overs before actually proceeding with
5177 stepping_thread = NULL;
5179 ALL_NON_EXITED_THREADS (tp)
5181 /* Ignore threads of processes we're not resuming. */
5183 && ptid_get_pid (tp->ptid) != ptid_get_pid (inferior_ptid))
5186 /* When stepping over a breakpoint, we lock all threads
5187 except the one that needs to move past the breakpoint.
5188 If a non-event thread has this set, the "incomplete
5189 step-over" check above should have caught it earlier. */
5190 gdb_assert (!tp->control.trap_expected);
5192 /* Did we find the stepping thread? */
5193 if (tp->control.step_range_end)
5195 /* Yep. There should only one though. */
5196 gdb_assert (stepping_thread == NULL);
5198 /* The event thread is handled at the top, before we
5200 gdb_assert (tp != ecs->event_thread);
5202 /* If some thread other than the event thread is
5203 stepping, then scheduler locking can't be in effect,
5204 otherwise we wouldn't have resumed the current event
5205 thread in the first place. */
5206 gdb_assert (!schedlock_applies (1));
5208 stepping_thread = tp;
5210 else if (thread_still_needs_step_over (tp))
5214 /* At the top we've returned early if the event thread
5215 is stepping. If some other thread not the event
5216 thread is stepping, then scheduler locking can't be
5217 in effect, and we can resume this thread. No need to
5218 keep looking for the stepping thread then. */
5223 if (step_over != NULL)
5228 fprintf_unfiltered (gdb_stdlog,
5229 "infrun: need to step-over [%s]\n",
5230 target_pid_to_str (tp->ptid));
5233 /* Only the stepping thread should have this set. */
5234 gdb_assert (tp->control.step_range_end == 0);
5236 ecs->ptid = tp->ptid;
5237 ecs->event_thread = tp;
5238 switch_to_thread (ecs->ptid);
5243 if (stepping_thread != NULL)
5245 struct frame_info *frame;
5246 struct gdbarch *gdbarch;
5248 tp = stepping_thread;
5250 /* If the stepping thread exited, then don't try to switch
5251 back and resume it, which could fail in several different
5252 ways depending on the target. Instead, just keep going.
5254 We can find a stepping dead thread in the thread list in
5257 - The target supports thread exit events, and when the
5258 target tries to delete the thread from the thread list,
5259 inferior_ptid pointed at the exiting thread. In such
5260 case, calling delete_thread does not really remove the
5261 thread from the list; instead, the thread is left listed,
5262 with 'exited' state.
5264 - The target's debug interface does not support thread
5265 exit events, and so we have no idea whatsoever if the
5266 previously stepping thread is still alive. For that
5267 reason, we need to synchronously query the target
5269 if (is_exited (tp->ptid)
5270 || !target_thread_alive (tp->ptid))
5273 fprintf_unfiltered (gdb_stdlog,
5274 "infrun: not switching back to "
5275 "stepped thread, it has vanished\n");
5277 delete_thread (tp->ptid);
5283 fprintf_unfiltered (gdb_stdlog,
5284 "infrun: switching back to stepped thread\n");
5286 ecs->event_thread = tp;
5287 ecs->ptid = tp->ptid;
5288 context_switch (ecs->ptid);
5290 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
5291 frame = get_current_frame ();
5292 gdbarch = get_frame_arch (frame);
5294 /* If the PC of the thread we were trying to single-step has
5295 changed, then that thread has trapped or been signaled,
5296 but the event has not been reported to GDB yet. Re-poll
5297 the target looking for this particular thread's event
5298 (i.e. temporarily enable schedlock) by:
5300 - setting a break at the current PC
5301 - resuming that particular thread, only (by setting
5304 This prevents us continuously moving the single-step
5305 breakpoint forward, one instruction at a time,
5308 if (gdbarch_software_single_step_p (gdbarch)
5309 && stop_pc != tp->prev_pc)
5312 fprintf_unfiltered (gdb_stdlog,
5313 "infrun: expected thread advanced also\n");
5315 insert_single_step_breakpoint (get_frame_arch (frame),
5316 get_frame_address_space (frame),
5318 singlestep_breakpoints_inserted_p = 1;
5319 ecs->event_thread->control.trap_expected = 1;
5320 singlestep_ptid = inferior_ptid;
5321 singlestep_pc = stop_pc;
5323 resume (0, GDB_SIGNAL_0);
5324 prepare_to_wait (ecs);
5329 fprintf_unfiltered (gdb_stdlog,
5330 "infrun: expected thread still "
5331 "hasn't advanced\n");
5341 /* Is thread TP in the middle of single-stepping? */
5344 currently_stepping (struct thread_info *tp)
5346 return ((tp->control.step_range_end
5347 && tp->control.step_resume_breakpoint == NULL)
5348 || tp->control.trap_expected
5349 || bpstat_should_step ());
5352 /* Inferior has stepped into a subroutine call with source code that
5353 we should not step over. Do step to the first line of code in
5357 handle_step_into_function (struct gdbarch *gdbarch,
5358 struct execution_control_state *ecs)
5361 struct symtab_and_line stop_func_sal, sr_sal;
5363 fill_in_stop_func (gdbarch, ecs);
5365 s = find_pc_symtab (stop_pc);
5366 if (s && s->language != language_asm)
5367 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5368 ecs->stop_func_start);
5370 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
5371 /* Use the step_resume_break to step until the end of the prologue,
5372 even if that involves jumps (as it seems to on the vax under
5374 /* If the prologue ends in the middle of a source line, continue to
5375 the end of that source line (if it is still within the function).
5376 Otherwise, just go to end of prologue. */
5377 if (stop_func_sal.end
5378 && stop_func_sal.pc != ecs->stop_func_start
5379 && stop_func_sal.end < ecs->stop_func_end)
5380 ecs->stop_func_start = stop_func_sal.end;
5382 /* Architectures which require breakpoint adjustment might not be able
5383 to place a breakpoint at the computed address. If so, the test
5384 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
5385 ecs->stop_func_start to an address at which a breakpoint may be
5386 legitimately placed.
5388 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
5389 made, GDB will enter an infinite loop when stepping through
5390 optimized code consisting of VLIW instructions which contain
5391 subinstructions corresponding to different source lines. On
5392 FR-V, it's not permitted to place a breakpoint on any but the
5393 first subinstruction of a VLIW instruction. When a breakpoint is
5394 set, GDB will adjust the breakpoint address to the beginning of
5395 the VLIW instruction. Thus, we need to make the corresponding
5396 adjustment here when computing the stop address. */
5398 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
5400 ecs->stop_func_start
5401 = gdbarch_adjust_breakpoint_address (gdbarch,
5402 ecs->stop_func_start);
5405 if (ecs->stop_func_start == stop_pc)
5407 /* We are already there: stop now. */
5408 end_stepping_range (ecs);
5413 /* Put the step-breakpoint there and go until there. */
5414 init_sal (&sr_sal); /* initialize to zeroes */
5415 sr_sal.pc = ecs->stop_func_start;
5416 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
5417 sr_sal.pspace = get_frame_program_space (get_current_frame ());
5419 /* Do not specify what the fp should be when we stop since on
5420 some machines the prologue is where the new fp value is
5422 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
5424 /* And make sure stepping stops right away then. */
5425 ecs->event_thread->control.step_range_end
5426 = ecs->event_thread->control.step_range_start;
5431 /* Inferior has stepped backward into a subroutine call with source
5432 code that we should not step over. Do step to the beginning of the
5433 last line of code in it. */
5436 handle_step_into_function_backward (struct gdbarch *gdbarch,
5437 struct execution_control_state *ecs)
5440 struct symtab_and_line stop_func_sal;
5442 fill_in_stop_func (gdbarch, ecs);
5444 s = find_pc_symtab (stop_pc);
5445 if (s && s->language != language_asm)
5446 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5447 ecs->stop_func_start);
5449 stop_func_sal = find_pc_line (stop_pc, 0);
5451 /* OK, we're just going to keep stepping here. */
5452 if (stop_func_sal.pc == stop_pc)
5454 /* We're there already. Just stop stepping now. */
5455 end_stepping_range (ecs);
5459 /* Else just reset the step range and keep going.
5460 No step-resume breakpoint, they don't work for
5461 epilogues, which can have multiple entry paths. */
5462 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
5463 ecs->event_thread->control.step_range_end = stop_func_sal.end;
5469 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
5470 This is used to both functions and to skip over code. */
5473 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
5474 struct symtab_and_line sr_sal,
5475 struct frame_id sr_id,
5476 enum bptype sr_type)
5478 /* There should never be more than one step-resume or longjmp-resume
5479 breakpoint per thread, so we should never be setting a new
5480 step_resume_breakpoint when one is already active. */
5481 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
5482 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
5485 fprintf_unfiltered (gdb_stdlog,
5486 "infrun: inserting step-resume breakpoint at %s\n",
5487 paddress (gdbarch, sr_sal.pc));
5489 inferior_thread ()->control.step_resume_breakpoint
5490 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
5494 insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
5495 struct symtab_and_line sr_sal,
5496 struct frame_id sr_id)
5498 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
5503 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
5504 This is used to skip a potential signal handler.
5506 This is called with the interrupted function's frame. The signal
5507 handler, when it returns, will resume the interrupted function at
5511 insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
5513 struct symtab_and_line sr_sal;
5514 struct gdbarch *gdbarch;
5516 gdb_assert (return_frame != NULL);
5517 init_sal (&sr_sal); /* initialize to zeros */
5519 gdbarch = get_frame_arch (return_frame);
5520 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
5521 sr_sal.section = find_pc_overlay (sr_sal.pc);
5522 sr_sal.pspace = get_frame_program_space (return_frame);
5524 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
5525 get_stack_frame_id (return_frame),
5529 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
5530 is used to skip a function after stepping into it (for "next" or if
5531 the called function has no debugging information).
5533 The current function has almost always been reached by single
5534 stepping a call or return instruction. NEXT_FRAME belongs to the
5535 current function, and the breakpoint will be set at the caller's
5538 This is a separate function rather than reusing
5539 insert_hp_step_resume_breakpoint_at_frame in order to avoid
5540 get_prev_frame, which may stop prematurely (see the implementation
5541 of frame_unwind_caller_id for an example). */
5544 insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
5546 struct symtab_and_line sr_sal;
5547 struct gdbarch *gdbarch;
5549 /* We shouldn't have gotten here if we don't know where the call site
5551 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
5553 init_sal (&sr_sal); /* initialize to zeros */
5555 gdbarch = frame_unwind_caller_arch (next_frame);
5556 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
5557 frame_unwind_caller_pc (next_frame));
5558 sr_sal.section = find_pc_overlay (sr_sal.pc);
5559 sr_sal.pspace = frame_unwind_program_space (next_frame);
5561 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
5562 frame_unwind_caller_id (next_frame));
5565 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
5566 new breakpoint at the target of a jmp_buf. The handling of
5567 longjmp-resume uses the same mechanisms used for handling
5568 "step-resume" breakpoints. */
5571 insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
5573 /* There should never be more than one longjmp-resume breakpoint per
5574 thread, so we should never be setting a new
5575 longjmp_resume_breakpoint when one is already active. */
5576 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
5579 fprintf_unfiltered (gdb_stdlog,
5580 "infrun: inserting longjmp-resume breakpoint at %s\n",
5581 paddress (gdbarch, pc));
5583 inferior_thread ()->control.exception_resume_breakpoint =
5584 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
5587 /* Insert an exception resume breakpoint. TP is the thread throwing
5588 the exception. The block B is the block of the unwinder debug hook
5589 function. FRAME is the frame corresponding to the call to this
5590 function. SYM is the symbol of the function argument holding the
5591 target PC of the exception. */
5594 insert_exception_resume_breakpoint (struct thread_info *tp,
5595 const struct block *b,
5596 struct frame_info *frame,
5599 volatile struct gdb_exception e;
5601 /* We want to ignore errors here. */
5602 TRY_CATCH (e, RETURN_MASK_ERROR)
5604 struct symbol *vsym;
5605 struct value *value;
5607 struct breakpoint *bp;
5609 vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN, NULL);
5610 value = read_var_value (vsym, frame);
5611 /* If the value was optimized out, revert to the old behavior. */
5612 if (! value_optimized_out (value))
5614 handler = value_as_address (value);
5617 fprintf_unfiltered (gdb_stdlog,
5618 "infrun: exception resume at %lx\n",
5619 (unsigned long) handler);
5621 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5622 handler, bp_exception_resume);
5624 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
5627 bp->thread = tp->num;
5628 inferior_thread ()->control.exception_resume_breakpoint = bp;
5633 /* A helper for check_exception_resume that sets an
5634 exception-breakpoint based on a SystemTap probe. */
5637 insert_exception_resume_from_probe (struct thread_info *tp,
5638 const struct bound_probe *probe,
5639 struct frame_info *frame)
5641 struct value *arg_value;
5643 struct breakpoint *bp;
5645 arg_value = probe_safe_evaluate_at_pc (frame, 1);
5649 handler = value_as_address (arg_value);
5652 fprintf_unfiltered (gdb_stdlog,
5653 "infrun: exception resume at %s\n",
5654 paddress (get_objfile_arch (probe->objfile),
5657 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5658 handler, bp_exception_resume);
5659 bp->thread = tp->num;
5660 inferior_thread ()->control.exception_resume_breakpoint = bp;
5663 /* This is called when an exception has been intercepted. Check to
5664 see whether the exception's destination is of interest, and if so,
5665 set an exception resume breakpoint there. */
5668 check_exception_resume (struct execution_control_state *ecs,
5669 struct frame_info *frame)
5671 volatile struct gdb_exception e;
5672 struct bound_probe probe;
5673 struct symbol *func;
5675 /* First see if this exception unwinding breakpoint was set via a
5676 SystemTap probe point. If so, the probe has two arguments: the
5677 CFA and the HANDLER. We ignore the CFA, extract the handler, and
5678 set a breakpoint there. */
5679 probe = find_probe_by_pc (get_frame_pc (frame));
5682 insert_exception_resume_from_probe (ecs->event_thread, &probe, frame);
5686 func = get_frame_function (frame);
5690 TRY_CATCH (e, RETURN_MASK_ERROR)
5692 const struct block *b;
5693 struct block_iterator iter;
5697 /* The exception breakpoint is a thread-specific breakpoint on
5698 the unwinder's debug hook, declared as:
5700 void _Unwind_DebugHook (void *cfa, void *handler);
5702 The CFA argument indicates the frame to which control is
5703 about to be transferred. HANDLER is the destination PC.
5705 We ignore the CFA and set a temporary breakpoint at HANDLER.
5706 This is not extremely efficient but it avoids issues in gdb
5707 with computing the DWARF CFA, and it also works even in weird
5708 cases such as throwing an exception from inside a signal
5711 b = SYMBOL_BLOCK_VALUE (func);
5712 ALL_BLOCK_SYMBOLS (b, iter, sym)
5714 if (!SYMBOL_IS_ARGUMENT (sym))
5721 insert_exception_resume_breakpoint (ecs->event_thread,
5730 stop_waiting (struct execution_control_state *ecs)
5733 fprintf_unfiltered (gdb_stdlog, "infrun: stop_waiting\n");
5735 clear_step_over_info ();
5737 /* Let callers know we don't want to wait for the inferior anymore. */
5738 ecs->wait_some_more = 0;
5741 /* Called when we should continue running the inferior, because the
5742 current event doesn't cause a user visible stop. This does the
5743 resuming part; waiting for the next event is done elsewhere. */
5746 keep_going (struct execution_control_state *ecs)
5748 /* Make sure normal_stop is called if we get a QUIT handled before
5750 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
5752 /* Save the pc before execution, to compare with pc after stop. */
5753 ecs->event_thread->prev_pc
5754 = regcache_read_pc (get_thread_regcache (ecs->ptid));
5756 if (ecs->event_thread->control.trap_expected
5757 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
5759 /* We haven't yet gotten our trap, and either: intercepted a
5760 non-signal event (e.g., a fork); or took a signal which we
5761 are supposed to pass through to the inferior. Simply
5763 discard_cleanups (old_cleanups);
5764 resume (currently_stepping (ecs->event_thread),
5765 ecs->event_thread->suspend.stop_signal);
5769 volatile struct gdb_exception e;
5770 struct regcache *regcache = get_current_regcache ();
5772 /* Either the trap was not expected, but we are continuing
5773 anyway (if we got a signal, the user asked it be passed to
5776 We got our expected trap, but decided we should resume from
5779 We're going to run this baby now!
5781 Note that insert_breakpoints won't try to re-insert
5782 already inserted breakpoints. Therefore, we don't
5783 care if breakpoints were already inserted, or not. */
5785 /* If we need to step over a breakpoint, and we're not using
5786 displaced stepping to do so, insert all breakpoints
5787 (watchpoints, etc.) but the one we're stepping over, step one
5788 instruction, and then re-insert the breakpoint when that step
5790 if ((ecs->hit_singlestep_breakpoint
5791 || thread_still_needs_step_over (ecs->event_thread))
5792 && !use_displaced_stepping (get_regcache_arch (regcache)))
5794 set_step_over_info (get_regcache_aspace (regcache),
5795 regcache_read_pc (regcache));
5798 clear_step_over_info ();
5800 /* Stop stepping if inserting breakpoints fails. */
5801 TRY_CATCH (e, RETURN_MASK_ERROR)
5803 insert_breakpoints ();
5807 exception_print (gdb_stderr, e);
5812 ecs->event_thread->control.trap_expected
5813 = (ecs->event_thread->stepping_over_breakpoint
5814 || ecs->hit_singlestep_breakpoint);
5816 /* Do not deliver GDB_SIGNAL_TRAP (except when the user
5817 explicitly specifies that such a signal should be delivered
5818 to the target program). Typically, that would occur when a
5819 user is debugging a target monitor on a simulator: the target
5820 monitor sets a breakpoint; the simulator encounters this
5821 breakpoint and halts the simulation handing control to GDB;
5822 GDB, noting that the stop address doesn't map to any known
5823 breakpoint, returns control back to the simulator; the
5824 simulator then delivers the hardware equivalent of a
5825 GDB_SIGNAL_TRAP to the program being debugged. */
5826 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
5827 && !signal_program[ecs->event_thread->suspend.stop_signal])
5828 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
5830 discard_cleanups (old_cleanups);
5831 resume (currently_stepping (ecs->event_thread),
5832 ecs->event_thread->suspend.stop_signal);
5835 prepare_to_wait (ecs);
5838 /* This function normally comes after a resume, before
5839 handle_inferior_event exits. It takes care of any last bits of
5840 housekeeping, and sets the all-important wait_some_more flag. */
5843 prepare_to_wait (struct execution_control_state *ecs)
5846 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
5848 /* This is the old end of the while loop. Let everybody know we
5849 want to wait for the inferior some more and get called again
5851 ecs->wait_some_more = 1;
5854 /* We are done with the step range of a step/next/si/ni command.
5855 Called once for each n of a "step n" operation. Notify observers
5856 if not in the middle of doing a "step N" operation for N > 1. */
5859 end_stepping_range (struct execution_control_state *ecs)
5861 ecs->event_thread->control.stop_step = 1;
5862 if (!ecs->event_thread->step_multi)
5863 observer_notify_end_stepping_range ();
5867 /* Several print_*_reason functions to print why the inferior has stopped.
5868 We always print something when the inferior exits, or receives a signal.
5869 The rest of the cases are dealt with later on in normal_stop and
5870 print_it_typical. Ideally there should be a call to one of these
5871 print_*_reason functions functions from handle_inferior_event each time
5872 stop_waiting is called.
5874 Note that we don't call these directly, instead we delegate that to
5875 the interpreters, through observers. Interpreters then call these
5876 with whatever uiout is right. */
5879 print_end_stepping_range_reason (struct ui_out *uiout)
5881 /* For CLI-like interpreters, print nothing. */
5883 if (ui_out_is_mi_like_p (uiout))
5885 ui_out_field_string (uiout, "reason",
5886 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
5891 print_signal_exited_reason (struct ui_out *uiout, enum gdb_signal siggnal)
5893 annotate_signalled ();
5894 if (ui_out_is_mi_like_p (uiout))
5896 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
5897 ui_out_text (uiout, "\nProgram terminated with signal ");
5898 annotate_signal_name ();
5899 ui_out_field_string (uiout, "signal-name",
5900 gdb_signal_to_name (siggnal));
5901 annotate_signal_name_end ();
5902 ui_out_text (uiout, ", ");
5903 annotate_signal_string ();
5904 ui_out_field_string (uiout, "signal-meaning",
5905 gdb_signal_to_string (siggnal));
5906 annotate_signal_string_end ();
5907 ui_out_text (uiout, ".\n");
5908 ui_out_text (uiout, "The program no longer exists.\n");
5912 print_exited_reason (struct ui_out *uiout, int exitstatus)
5914 struct inferior *inf = current_inferior ();
5915 const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
5917 annotate_exited (exitstatus);
5920 if (ui_out_is_mi_like_p (uiout))
5921 ui_out_field_string (uiout, "reason",
5922 async_reason_lookup (EXEC_ASYNC_EXITED));
5923 ui_out_text (uiout, "[Inferior ");
5924 ui_out_text (uiout, plongest (inf->num));
5925 ui_out_text (uiout, " (");
5926 ui_out_text (uiout, pidstr);
5927 ui_out_text (uiout, ") exited with code ");
5928 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
5929 ui_out_text (uiout, "]\n");
5933 if (ui_out_is_mi_like_p (uiout))
5935 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
5936 ui_out_text (uiout, "[Inferior ");
5937 ui_out_text (uiout, plongest (inf->num));
5938 ui_out_text (uiout, " (");
5939 ui_out_text (uiout, pidstr);
5940 ui_out_text (uiout, ") exited normally]\n");
5945 print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
5949 if (siggnal == GDB_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
5951 struct thread_info *t = inferior_thread ();
5953 ui_out_text (uiout, "\n[");
5954 ui_out_field_string (uiout, "thread-name",
5955 target_pid_to_str (t->ptid));
5956 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
5957 ui_out_text (uiout, " stopped");
5961 ui_out_text (uiout, "\nProgram received signal ");
5962 annotate_signal_name ();
5963 if (ui_out_is_mi_like_p (uiout))
5965 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
5966 ui_out_field_string (uiout, "signal-name",
5967 gdb_signal_to_name (siggnal));
5968 annotate_signal_name_end ();
5969 ui_out_text (uiout, ", ");
5970 annotate_signal_string ();
5971 ui_out_field_string (uiout, "signal-meaning",
5972 gdb_signal_to_string (siggnal));
5973 annotate_signal_string_end ();
5975 ui_out_text (uiout, ".\n");
5979 print_no_history_reason (struct ui_out *uiout)
5981 ui_out_text (uiout, "\nNo more reverse-execution history.\n");
5984 /* Print current location without a level number, if we have changed
5985 functions or hit a breakpoint. Print source line if we have one.
5986 bpstat_print contains the logic deciding in detail what to print,
5987 based on the event(s) that just occurred. */
5990 print_stop_event (struct target_waitstatus *ws)
5994 int do_frame_printing = 1;
5995 struct thread_info *tp = inferior_thread ();
5997 bpstat_ret = bpstat_print (tp->control.stop_bpstat, ws->kind);
6001 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
6002 should) carry around the function and does (or should) use
6003 that when doing a frame comparison. */
6004 if (tp->control.stop_step
6005 && frame_id_eq (tp->control.step_frame_id,
6006 get_frame_id (get_current_frame ()))
6007 && step_start_function == find_pc_function (stop_pc))
6009 /* Finished step, just print source line. */
6010 source_flag = SRC_LINE;
6014 /* Print location and source line. */
6015 source_flag = SRC_AND_LOC;
6018 case PRINT_SRC_AND_LOC:
6019 /* Print location and source line. */
6020 source_flag = SRC_AND_LOC;
6022 case PRINT_SRC_ONLY:
6023 source_flag = SRC_LINE;
6026 /* Something bogus. */
6027 source_flag = SRC_LINE;
6028 do_frame_printing = 0;
6031 internal_error (__FILE__, __LINE__, _("Unknown value."));
6034 /* The behavior of this routine with respect to the source
6036 SRC_LINE: Print only source line
6037 LOCATION: Print only location
6038 SRC_AND_LOC: Print location and source line. */
6039 if (do_frame_printing)
6040 print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
6042 /* Display the auto-display expressions. */
6046 /* Here to return control to GDB when the inferior stops for real.
6047 Print appropriate messages, remove breakpoints, give terminal our modes.
6049 STOP_PRINT_FRAME nonzero means print the executing frame
6050 (pc, function, args, file, line number and line text).
6051 BREAKPOINTS_FAILED nonzero means stop was due to error
6052 attempting to insert breakpoints. */
6057 struct target_waitstatus last;
6059 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
6061 get_last_target_status (&last_ptid, &last);
6063 /* If an exception is thrown from this point on, make sure to
6064 propagate GDB's knowledge of the executing state to the
6065 frontend/user running state. A QUIT is an easy exception to see
6066 here, so do this before any filtered output. */
6068 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
6069 else if (last.kind != TARGET_WAITKIND_SIGNALLED
6070 && last.kind != TARGET_WAITKIND_EXITED
6071 && last.kind != TARGET_WAITKIND_NO_RESUMED)
6072 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
6074 /* As with the notification of thread events, we want to delay
6075 notifying the user that we've switched thread context until
6076 the inferior actually stops.
6078 There's no point in saying anything if the inferior has exited.
6079 Note that SIGNALLED here means "exited with a signal", not
6080 "received a signal".
6082 Also skip saying anything in non-stop mode. In that mode, as we
6083 don't want GDB to switch threads behind the user's back, to avoid
6084 races where the user is typing a command to apply to thread x,
6085 but GDB switches to thread y before the user finishes entering
6086 the command, fetch_inferior_event installs a cleanup to restore
6087 the current thread back to the thread the user had selected right
6088 after this event is handled, so we're not really switching, only
6089 informing of a stop. */
6091 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
6092 && target_has_execution
6093 && last.kind != TARGET_WAITKIND_SIGNALLED
6094 && last.kind != TARGET_WAITKIND_EXITED
6095 && last.kind != TARGET_WAITKIND_NO_RESUMED)
6097 target_terminal_ours_for_output ();
6098 printf_filtered (_("[Switching to %s]\n"),
6099 target_pid_to_str (inferior_ptid));
6100 annotate_thread_changed ();
6101 previous_inferior_ptid = inferior_ptid;
6104 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
6106 gdb_assert (sync_execution || !target_can_async_p ());
6108 target_terminal_ours_for_output ();
6109 printf_filtered (_("No unwaited-for children left.\n"));
6112 if (!breakpoints_should_be_inserted_now () && target_has_execution)
6114 if (remove_breakpoints ())
6116 target_terminal_ours_for_output ();
6117 printf_filtered (_("Cannot remove breakpoints because "
6118 "program is no longer writable.\nFurther "
6119 "execution is probably impossible.\n"));
6123 /* If an auto-display called a function and that got a signal,
6124 delete that auto-display to avoid an infinite recursion. */
6126 if (stopped_by_random_signal)
6127 disable_current_display ();
6129 /* Don't print a message if in the middle of doing a "step n"
6130 operation for n > 1 */
6131 if (target_has_execution
6132 && last.kind != TARGET_WAITKIND_SIGNALLED
6133 && last.kind != TARGET_WAITKIND_EXITED
6134 && inferior_thread ()->step_multi
6135 && inferior_thread ()->control.stop_step)
6138 target_terminal_ours ();
6139 async_enable_stdin ();
6141 /* Set the current source location. This will also happen if we
6142 display the frame below, but the current SAL will be incorrect
6143 during a user hook-stop function. */
6144 if (has_stack_frames () && !stop_stack_dummy)
6145 set_current_sal_from_frame (get_current_frame ());
6147 /* Let the user/frontend see the threads as stopped, but do nothing
6148 if the thread was running an infcall. We may be e.g., evaluating
6149 a breakpoint condition. In that case, the thread had state
6150 THREAD_RUNNING before the infcall, and shall remain set to
6151 running, all without informing the user/frontend about state
6152 transition changes. If this is actually a call command, then the
6153 thread was originally already stopped, so there's no state to
6155 if (target_has_execution && inferior_thread ()->control.in_infcall)
6156 discard_cleanups (old_chain);
6158 do_cleanups (old_chain);
6160 /* Look up the hook_stop and run it (CLI internally handles problem
6161 of stop_command's pre-hook not existing). */
6163 catch_errors (hook_stop_stub, stop_command,
6164 "Error while running hook_stop:\n", RETURN_MASK_ALL);
6166 if (!has_stack_frames ())
6169 if (last.kind == TARGET_WAITKIND_SIGNALLED
6170 || last.kind == TARGET_WAITKIND_EXITED)
6173 /* Select innermost stack frame - i.e., current frame is frame 0,
6174 and current location is based on that.
6175 Don't do this on return from a stack dummy routine,
6176 or if the program has exited. */
6178 if (!stop_stack_dummy)
6180 select_frame (get_current_frame ());
6182 /* If --batch-silent is enabled then there's no need to print the current
6183 source location, and to try risks causing an error message about
6184 missing source files. */
6185 if (stop_print_frame && !batch_silent)
6186 print_stop_event (&last);
6189 /* Save the function value return registers, if we care.
6190 We might be about to restore their previous contents. */
6191 if (inferior_thread ()->control.proceed_to_finish
6192 && execution_direction != EXEC_REVERSE)
6194 /* This should not be necessary. */
6196 regcache_xfree (stop_registers);
6198 /* NB: The copy goes through to the target picking up the value of
6199 all the registers. */
6200 stop_registers = regcache_dup (get_current_regcache ());
6203 if (stop_stack_dummy == STOP_STACK_DUMMY)
6205 /* Pop the empty frame that contains the stack dummy.
6206 This also restores inferior state prior to the call
6207 (struct infcall_suspend_state). */
6208 struct frame_info *frame = get_current_frame ();
6210 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
6212 /* frame_pop() calls reinit_frame_cache as the last thing it
6213 does which means there's currently no selected frame. We
6214 don't need to re-establish a selected frame if the dummy call
6215 returns normally, that will be done by
6216 restore_infcall_control_state. However, we do have to handle
6217 the case where the dummy call is returning after being
6218 stopped (e.g. the dummy call previously hit a breakpoint).
6219 We can't know which case we have so just always re-establish
6220 a selected frame here. */
6221 select_frame (get_current_frame ());
6225 annotate_stopped ();
6227 /* Suppress the stop observer if we're in the middle of:
6229 - a step n (n > 1), as there still more steps to be done.
6231 - a "finish" command, as the observer will be called in
6232 finish_command_continuation, so it can include the inferior
6233 function's return value.
6235 - calling an inferior function, as we pretend we inferior didn't
6236 run at all. The return value of the call is handled by the
6237 expression evaluator, through call_function_by_hand. */
6239 if (!target_has_execution
6240 || last.kind == TARGET_WAITKIND_SIGNALLED
6241 || last.kind == TARGET_WAITKIND_EXITED
6242 || last.kind == TARGET_WAITKIND_NO_RESUMED
6243 || (!(inferior_thread ()->step_multi
6244 && inferior_thread ()->control.stop_step)
6245 && !(inferior_thread ()->control.stop_bpstat
6246 && inferior_thread ()->control.proceed_to_finish)
6247 && !inferior_thread ()->control.in_infcall))
6249 if (!ptid_equal (inferior_ptid, null_ptid))
6250 observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
6253 observer_notify_normal_stop (NULL, stop_print_frame);
6256 if (target_has_execution)
6258 if (last.kind != TARGET_WAITKIND_SIGNALLED
6259 && last.kind != TARGET_WAITKIND_EXITED)
6260 /* Delete the breakpoint we stopped at, if it wants to be deleted.
6261 Delete any breakpoint that is to be deleted at the next stop. */
6262 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
6265 /* Try to get rid of automatically added inferiors that are no
6266 longer needed. Keeping those around slows down things linearly.
6267 Note that this never removes the current inferior. */
6272 hook_stop_stub (void *cmd)
6274 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
6279 signal_stop_state (int signo)
6281 return signal_stop[signo];
6285 signal_print_state (int signo)
6287 return signal_print[signo];
6291 signal_pass_state (int signo)
6293 return signal_program[signo];
6297 signal_cache_update (int signo)
6301 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
6302 signal_cache_update (signo);
6307 signal_pass[signo] = (signal_stop[signo] == 0
6308 && signal_print[signo] == 0
6309 && signal_program[signo] == 1
6310 && signal_catch[signo] == 0);
6314 signal_stop_update (int signo, int state)
6316 int ret = signal_stop[signo];
6318 signal_stop[signo] = state;
6319 signal_cache_update (signo);
6324 signal_print_update (int signo, int state)
6326 int ret = signal_print[signo];
6328 signal_print[signo] = state;
6329 signal_cache_update (signo);
6334 signal_pass_update (int signo, int state)
6336 int ret = signal_program[signo];
6338 signal_program[signo] = state;
6339 signal_cache_update (signo);
6343 /* Update the global 'signal_catch' from INFO and notify the
6347 signal_catch_update (const unsigned int *info)
6351 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
6352 signal_catch[i] = info[i] > 0;
6353 signal_cache_update (-1);
6354 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6358 sig_print_header (void)
6360 printf_filtered (_("Signal Stop\tPrint\tPass "
6361 "to program\tDescription\n"));
6365 sig_print_info (enum gdb_signal oursig)
6367 const char *name = gdb_signal_to_name (oursig);
6368 int name_padding = 13 - strlen (name);
6370 if (name_padding <= 0)
6373 printf_filtered ("%s", name);
6374 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
6375 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6376 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6377 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
6378 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
6381 /* Specify how various signals in the inferior should be handled. */
6384 handle_command (char *args, int from_tty)
6387 int digits, wordlen;
6388 int sigfirst, signum, siglast;
6389 enum gdb_signal oursig;
6392 unsigned char *sigs;
6393 struct cleanup *old_chain;
6397 error_no_arg (_("signal to handle"));
6400 /* Allocate and zero an array of flags for which signals to handle. */
6402 nsigs = (int) GDB_SIGNAL_LAST;
6403 sigs = (unsigned char *) alloca (nsigs);
6404 memset (sigs, 0, nsigs);
6406 /* Break the command line up into args. */
6408 argv = gdb_buildargv (args);
6409 old_chain = make_cleanup_freeargv (argv);
6411 /* Walk through the args, looking for signal oursigs, signal names, and
6412 actions. Signal numbers and signal names may be interspersed with
6413 actions, with the actions being performed for all signals cumulatively
6414 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
6416 while (*argv != NULL)
6418 wordlen = strlen (*argv);
6419 for (digits = 0; isdigit ((*argv)[digits]); digits++)
6423 sigfirst = siglast = -1;
6425 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6427 /* Apply action to all signals except those used by the
6428 debugger. Silently skip those. */
6431 siglast = nsigs - 1;
6433 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
6435 SET_SIGS (nsigs, sigs, signal_stop);
6436 SET_SIGS (nsigs, sigs, signal_print);
6438 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
6440 UNSET_SIGS (nsigs, sigs, signal_program);
6442 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
6444 SET_SIGS (nsigs, sigs, signal_print);
6446 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
6448 SET_SIGS (nsigs, sigs, signal_program);
6450 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
6452 UNSET_SIGS (nsigs, sigs, signal_stop);
6454 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
6456 SET_SIGS (nsigs, sigs, signal_program);
6458 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
6460 UNSET_SIGS (nsigs, sigs, signal_print);
6461 UNSET_SIGS (nsigs, sigs, signal_stop);
6463 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
6465 UNSET_SIGS (nsigs, sigs, signal_program);
6467 else if (digits > 0)
6469 /* It is numeric. The numeric signal refers to our own
6470 internal signal numbering from target.h, not to host/target
6471 signal number. This is a feature; users really should be
6472 using symbolic names anyway, and the common ones like
6473 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
6475 sigfirst = siglast = (int)
6476 gdb_signal_from_command (atoi (*argv));
6477 if ((*argv)[digits] == '-')
6480 gdb_signal_from_command (atoi ((*argv) + digits + 1));
6482 if (sigfirst > siglast)
6484 /* Bet he didn't figure we'd think of this case... */
6492 oursig = gdb_signal_from_name (*argv);
6493 if (oursig != GDB_SIGNAL_UNKNOWN)
6495 sigfirst = siglast = (int) oursig;
6499 /* Not a number and not a recognized flag word => complain. */
6500 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
6504 /* If any signal numbers or symbol names were found, set flags for
6505 which signals to apply actions to. */
6507 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
6509 switch ((enum gdb_signal) signum)
6511 case GDB_SIGNAL_TRAP:
6512 case GDB_SIGNAL_INT:
6513 if (!allsigs && !sigs[signum])
6515 if (query (_("%s is used by the debugger.\n\
6516 Are you sure you want to change it? "),
6517 gdb_signal_to_name ((enum gdb_signal) signum)))
6523 printf_unfiltered (_("Not confirmed, unchanged.\n"));
6524 gdb_flush (gdb_stdout);
6529 case GDB_SIGNAL_DEFAULT:
6530 case GDB_SIGNAL_UNKNOWN:
6531 /* Make sure that "all" doesn't print these. */
6542 for (signum = 0; signum < nsigs; signum++)
6545 signal_cache_update (-1);
6546 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6547 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
6551 /* Show the results. */
6552 sig_print_header ();
6553 for (; signum < nsigs; signum++)
6555 sig_print_info (signum);
6561 do_cleanups (old_chain);
6564 /* Complete the "handle" command. */
6566 static VEC (char_ptr) *
6567 handle_completer (struct cmd_list_element *ignore,
6568 const char *text, const char *word)
6570 VEC (char_ptr) *vec_signals, *vec_keywords, *return_val;
6571 static const char * const keywords[] =
6585 vec_signals = signal_completer (ignore, text, word);
6586 vec_keywords = complete_on_enum (keywords, word, word);
6588 return_val = VEC_merge (char_ptr, vec_signals, vec_keywords);
6589 VEC_free (char_ptr, vec_signals);
6590 VEC_free (char_ptr, vec_keywords);
6595 xdb_handle_command (char *args, int from_tty)
6598 struct cleanup *old_chain;
6601 error_no_arg (_("xdb command"));
6603 /* Break the command line up into args. */
6605 argv = gdb_buildargv (args);
6606 old_chain = make_cleanup_freeargv (argv);
6607 if (argv[1] != (char *) NULL)
6612 bufLen = strlen (argv[0]) + 20;
6613 argBuf = (char *) xmalloc (bufLen);
6617 enum gdb_signal oursig;
6619 oursig = gdb_signal_from_name (argv[0]);
6620 memset (argBuf, 0, bufLen);
6621 if (strcmp (argv[1], "Q") == 0)
6622 sprintf (argBuf, "%s %s", argv[0], "noprint");
6625 if (strcmp (argv[1], "s") == 0)
6627 if (!signal_stop[oursig])
6628 sprintf (argBuf, "%s %s", argv[0], "stop");
6630 sprintf (argBuf, "%s %s", argv[0], "nostop");
6632 else if (strcmp (argv[1], "i") == 0)
6634 if (!signal_program[oursig])
6635 sprintf (argBuf, "%s %s", argv[0], "pass");
6637 sprintf (argBuf, "%s %s", argv[0], "nopass");
6639 else if (strcmp (argv[1], "r") == 0)
6641 if (!signal_print[oursig])
6642 sprintf (argBuf, "%s %s", argv[0], "print");
6644 sprintf (argBuf, "%s %s", argv[0], "noprint");
6650 handle_command (argBuf, from_tty);
6652 printf_filtered (_("Invalid signal handling flag.\n"));
6657 do_cleanups (old_chain);
6661 gdb_signal_from_command (int num)
6663 if (num >= 1 && num <= 15)
6664 return (enum gdb_signal) num;
6665 error (_("Only signals 1-15 are valid as numeric signals.\n\
6666 Use \"info signals\" for a list of symbolic signals."));
6669 /* Print current contents of the tables set by the handle command.
6670 It is possible we should just be printing signals actually used
6671 by the current target (but for things to work right when switching
6672 targets, all signals should be in the signal tables). */
6675 signals_info (char *signum_exp, int from_tty)
6677 enum gdb_signal oursig;
6679 sig_print_header ();
6683 /* First see if this is a symbol name. */
6684 oursig = gdb_signal_from_name (signum_exp);
6685 if (oursig == GDB_SIGNAL_UNKNOWN)
6687 /* No, try numeric. */
6689 gdb_signal_from_command (parse_and_eval_long (signum_exp));
6691 sig_print_info (oursig);
6695 printf_filtered ("\n");
6696 /* These ugly casts brought to you by the native VAX compiler. */
6697 for (oursig = GDB_SIGNAL_FIRST;
6698 (int) oursig < (int) GDB_SIGNAL_LAST;
6699 oursig = (enum gdb_signal) ((int) oursig + 1))
6703 if (oursig != GDB_SIGNAL_UNKNOWN
6704 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
6705 sig_print_info (oursig);
6708 printf_filtered (_("\nUse the \"handle\" command "
6709 "to change these tables.\n"));
6712 /* Check if it makes sense to read $_siginfo from the current thread
6713 at this point. If not, throw an error. */
6716 validate_siginfo_access (void)
6718 /* No current inferior, no siginfo. */
6719 if (ptid_equal (inferior_ptid, null_ptid))
6720 error (_("No thread selected."));
6722 /* Don't try to read from a dead thread. */
6723 if (is_exited (inferior_ptid))
6724 error (_("The current thread has terminated"));
6726 /* ... or from a spinning thread. */
6727 if (is_running (inferior_ptid))
6728 error (_("Selected thread is running."));
6731 /* The $_siginfo convenience variable is a bit special. We don't know
6732 for sure the type of the value until we actually have a chance to
6733 fetch the data. The type can change depending on gdbarch, so it is
6734 also dependent on which thread you have selected.
6736 1. making $_siginfo be an internalvar that creates a new value on
6739 2. making the value of $_siginfo be an lval_computed value. */
6741 /* This function implements the lval_computed support for reading a
6745 siginfo_value_read (struct value *v)
6747 LONGEST transferred;
6749 validate_siginfo_access ();
6752 target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO,
6754 value_contents_all_raw (v),
6756 TYPE_LENGTH (value_type (v)));
6758 if (transferred != TYPE_LENGTH (value_type (v)))
6759 error (_("Unable to read siginfo"));
6762 /* This function implements the lval_computed support for writing a
6766 siginfo_value_write (struct value *v, struct value *fromval)
6768 LONGEST transferred;
6770 validate_siginfo_access ();
6772 transferred = target_write (¤t_target,
6773 TARGET_OBJECT_SIGNAL_INFO,
6775 value_contents_all_raw (fromval),
6777 TYPE_LENGTH (value_type (fromval)));
6779 if (transferred != TYPE_LENGTH (value_type (fromval)))
6780 error (_("Unable to write siginfo"));
6783 static const struct lval_funcs siginfo_value_funcs =
6789 /* Return a new value with the correct type for the siginfo object of
6790 the current thread using architecture GDBARCH. Return a void value
6791 if there's no object available. */
6793 static struct value *
6794 siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
6797 if (target_has_stack
6798 && !ptid_equal (inferior_ptid, null_ptid)
6799 && gdbarch_get_siginfo_type_p (gdbarch))
6801 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6803 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
6806 return allocate_value (builtin_type (gdbarch)->builtin_void);
6810 /* infcall_suspend_state contains state about the program itself like its
6811 registers and any signal it received when it last stopped.
6812 This state must be restored regardless of how the inferior function call
6813 ends (either successfully, or after it hits a breakpoint or signal)
6814 if the program is to properly continue where it left off. */
6816 struct infcall_suspend_state
6818 struct thread_suspend_state thread_suspend;
6819 #if 0 /* Currently unused and empty structures are not valid C. */
6820 struct inferior_suspend_state inferior_suspend;
6825 struct regcache *registers;
6827 /* Format of SIGINFO_DATA or NULL if it is not present. */
6828 struct gdbarch *siginfo_gdbarch;
6830 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
6831 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
6832 content would be invalid. */
6833 gdb_byte *siginfo_data;
6836 struct infcall_suspend_state *
6837 save_infcall_suspend_state (void)
6839 struct infcall_suspend_state *inf_state;
6840 struct thread_info *tp = inferior_thread ();
6842 struct inferior *inf = current_inferior ();
6844 struct regcache *regcache = get_current_regcache ();
6845 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6846 gdb_byte *siginfo_data = NULL;
6848 if (gdbarch_get_siginfo_type_p (gdbarch))
6850 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6851 size_t len = TYPE_LENGTH (type);
6852 struct cleanup *back_to;
6854 siginfo_data = xmalloc (len);
6855 back_to = make_cleanup (xfree, siginfo_data);
6857 if (target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6858 siginfo_data, 0, len) == len)
6859 discard_cleanups (back_to);
6862 /* Errors ignored. */
6863 do_cleanups (back_to);
6864 siginfo_data = NULL;
6868 inf_state = XCNEW (struct infcall_suspend_state);
6872 inf_state->siginfo_gdbarch = gdbarch;
6873 inf_state->siginfo_data = siginfo_data;
6876 inf_state->thread_suspend = tp->suspend;
6877 #if 0 /* Currently unused and empty structures are not valid C. */
6878 inf_state->inferior_suspend = inf->suspend;
6881 /* run_inferior_call will not use the signal due to its `proceed' call with
6882 GDB_SIGNAL_0 anyway. */
6883 tp->suspend.stop_signal = GDB_SIGNAL_0;
6885 inf_state->stop_pc = stop_pc;
6887 inf_state->registers = regcache_dup (regcache);
6892 /* Restore inferior session state to INF_STATE. */
6895 restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6897 struct thread_info *tp = inferior_thread ();
6899 struct inferior *inf = current_inferior ();
6901 struct regcache *regcache = get_current_regcache ();
6902 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6904 tp->suspend = inf_state->thread_suspend;
6905 #if 0 /* Currently unused and empty structures are not valid C. */
6906 inf->suspend = inf_state->inferior_suspend;
6909 stop_pc = inf_state->stop_pc;
6911 if (inf_state->siginfo_gdbarch == gdbarch)
6913 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6915 /* Errors ignored. */
6916 target_write (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6917 inf_state->siginfo_data, 0, TYPE_LENGTH (type));
6920 /* The inferior can be gone if the user types "print exit(0)"
6921 (and perhaps other times). */
6922 if (target_has_execution)
6923 /* NB: The register write goes through to the target. */
6924 regcache_cpy (regcache, inf_state->registers);
6926 discard_infcall_suspend_state (inf_state);
6930 do_restore_infcall_suspend_state_cleanup (void *state)
6932 restore_infcall_suspend_state (state);
6936 make_cleanup_restore_infcall_suspend_state
6937 (struct infcall_suspend_state *inf_state)
6939 return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
6943 discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6945 regcache_xfree (inf_state->registers);
6946 xfree (inf_state->siginfo_data);
6951 get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
6953 return inf_state->registers;
6956 /* infcall_control_state contains state regarding gdb's control of the
6957 inferior itself like stepping control. It also contains session state like
6958 the user's currently selected frame. */
6960 struct infcall_control_state
6962 struct thread_control_state thread_control;
6963 struct inferior_control_state inferior_control;
6966 enum stop_stack_kind stop_stack_dummy;
6967 int stopped_by_random_signal;
6968 int stop_after_trap;
6970 /* ID if the selected frame when the inferior function call was made. */
6971 struct frame_id selected_frame_id;
6974 /* Save all of the information associated with the inferior<==>gdb
6977 struct infcall_control_state *
6978 save_infcall_control_state (void)
6980 struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
6981 struct thread_info *tp = inferior_thread ();
6982 struct inferior *inf = current_inferior ();
6984 inf_status->thread_control = tp->control;
6985 inf_status->inferior_control = inf->control;
6987 tp->control.step_resume_breakpoint = NULL;
6988 tp->control.exception_resume_breakpoint = NULL;
6990 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
6991 chain. If caller's caller is walking the chain, they'll be happier if we
6992 hand them back the original chain when restore_infcall_control_state is
6994 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
6997 inf_status->stop_stack_dummy = stop_stack_dummy;
6998 inf_status->stopped_by_random_signal = stopped_by_random_signal;
6999 inf_status->stop_after_trap = stop_after_trap;
7001 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
7007 restore_selected_frame (void *args)
7009 struct frame_id *fid = (struct frame_id *) args;
7010 struct frame_info *frame;
7012 frame = frame_find_by_id (*fid);
7014 /* If inf_status->selected_frame_id is NULL, there was no previously
7018 warning (_("Unable to restore previously selected frame."));
7022 select_frame (frame);
7027 /* Restore inferior session state to INF_STATUS. */
7030 restore_infcall_control_state (struct infcall_control_state *inf_status)
7032 struct thread_info *tp = inferior_thread ();
7033 struct inferior *inf = current_inferior ();
7035 if (tp->control.step_resume_breakpoint)
7036 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
7038 if (tp->control.exception_resume_breakpoint)
7039 tp->control.exception_resume_breakpoint->disposition
7040 = disp_del_at_next_stop;
7042 /* Handle the bpstat_copy of the chain. */
7043 bpstat_clear (&tp->control.stop_bpstat);
7045 tp->control = inf_status->thread_control;
7046 inf->control = inf_status->inferior_control;
7049 stop_stack_dummy = inf_status->stop_stack_dummy;
7050 stopped_by_random_signal = inf_status->stopped_by_random_signal;
7051 stop_after_trap = inf_status->stop_after_trap;
7053 if (target_has_stack)
7055 /* The point of catch_errors is that if the stack is clobbered,
7056 walking the stack might encounter a garbage pointer and
7057 error() trying to dereference it. */
7059 (restore_selected_frame, &inf_status->selected_frame_id,
7060 "Unable to restore previously selected frame:\n",
7061 RETURN_MASK_ERROR) == 0)
7062 /* Error in restoring the selected frame. Select the innermost
7064 select_frame (get_current_frame ());
7071 do_restore_infcall_control_state_cleanup (void *sts)
7073 restore_infcall_control_state (sts);
7077 make_cleanup_restore_infcall_control_state
7078 (struct infcall_control_state *inf_status)
7080 return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
7084 discard_infcall_control_state (struct infcall_control_state *inf_status)
7086 if (inf_status->thread_control.step_resume_breakpoint)
7087 inf_status->thread_control.step_resume_breakpoint->disposition
7088 = disp_del_at_next_stop;
7090 if (inf_status->thread_control.exception_resume_breakpoint)
7091 inf_status->thread_control.exception_resume_breakpoint->disposition
7092 = disp_del_at_next_stop;
7094 /* See save_infcall_control_state for info on stop_bpstat. */
7095 bpstat_clear (&inf_status->thread_control.stop_bpstat);
7100 /* restore_inferior_ptid() will be used by the cleanup machinery
7101 to restore the inferior_ptid value saved in a call to
7102 save_inferior_ptid(). */
7105 restore_inferior_ptid (void *arg)
7107 ptid_t *saved_ptid_ptr = arg;
7109 inferior_ptid = *saved_ptid_ptr;
7113 /* Save the value of inferior_ptid so that it may be restored by a
7114 later call to do_cleanups(). Returns the struct cleanup pointer
7115 needed for later doing the cleanup. */
7118 save_inferior_ptid (void)
7120 ptid_t *saved_ptid_ptr;
7122 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
7123 *saved_ptid_ptr = inferior_ptid;
7124 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
7127 /* See inferior.h. */
7130 clear_exit_convenience_vars (void)
7132 clear_internalvar (lookup_internalvar ("_exitsignal"));
7133 clear_internalvar (lookup_internalvar ("_exitcode"));
7137 /* User interface for reverse debugging:
7138 Set exec-direction / show exec-direction commands
7139 (returns error unless target implements to_set_exec_direction method). */
7141 int execution_direction = EXEC_FORWARD;
7142 static const char exec_forward[] = "forward";
7143 static const char exec_reverse[] = "reverse";
7144 static const char *exec_direction = exec_forward;
7145 static const char *const exec_direction_names[] = {
7152 set_exec_direction_func (char *args, int from_tty,
7153 struct cmd_list_element *cmd)
7155 if (target_can_execute_reverse)
7157 if (!strcmp (exec_direction, exec_forward))
7158 execution_direction = EXEC_FORWARD;
7159 else if (!strcmp (exec_direction, exec_reverse))
7160 execution_direction = EXEC_REVERSE;
7164 exec_direction = exec_forward;
7165 error (_("Target does not support this operation."));
7170 show_exec_direction_func (struct ui_file *out, int from_tty,
7171 struct cmd_list_element *cmd, const char *value)
7173 switch (execution_direction) {
7175 fprintf_filtered (out, _("Forward.\n"));
7178 fprintf_filtered (out, _("Reverse.\n"));
7181 internal_error (__FILE__, __LINE__,
7182 _("bogus execution_direction value: %d"),
7183 (int) execution_direction);
7188 show_schedule_multiple (struct ui_file *file, int from_tty,
7189 struct cmd_list_element *c, const char *value)
7191 fprintf_filtered (file, _("Resuming the execution of threads "
7192 "of all processes is %s.\n"), value);
7195 /* Implementation of `siginfo' variable. */
7197 static const struct internalvar_funcs siginfo_funcs =
7205 _initialize_infrun (void)
7209 struct cmd_list_element *c;
7211 add_info ("signals", signals_info, _("\
7212 What debugger does when program gets various signals.\n\
7213 Specify a signal as argument to print info on that signal only."));
7214 add_info_alias ("handle", "signals", 0);
7216 c = add_com ("handle", class_run, handle_command, _("\
7217 Specify how to handle signals.\n\
7218 Usage: handle SIGNAL [ACTIONS]\n\
7219 Args are signals and actions to apply to those signals.\n\
7220 If no actions are specified, the current settings for the specified signals\n\
7221 will be displayed instead.\n\
7223 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7224 from 1-15 are allowed for compatibility with old versions of GDB.\n\
7225 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7226 The special arg \"all\" is recognized to mean all signals except those\n\
7227 used by the debugger, typically SIGTRAP and SIGINT.\n\
7229 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
7230 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
7231 Stop means reenter debugger if this signal happens (implies print).\n\
7232 Print means print a message if this signal happens.\n\
7233 Pass means let program see this signal; otherwise program doesn't know.\n\
7234 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
7235 Pass and Stop may be combined.\n\
7237 Multiple signals may be specified. Signal numbers and signal names\n\
7238 may be interspersed with actions, with the actions being performed for\n\
7239 all signals cumulatively specified."));
7240 set_cmd_completer (c, handle_completer);
7244 add_com ("lz", class_info, signals_info, _("\
7245 What debugger does when program gets various signals.\n\
7246 Specify a signal as argument to print info on that signal only."));
7247 add_com ("z", class_run, xdb_handle_command, _("\
7248 Specify how to handle a signal.\n\
7249 Args are signals and actions to apply to those signals.\n\
7250 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7251 from 1-15 are allowed for compatibility with old versions of GDB.\n\
7252 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7253 The special arg \"all\" is recognized to mean all signals except those\n\
7254 used by the debugger, typically SIGTRAP and SIGINT.\n\
7255 Recognized actions include \"s\" (toggles between stop and nostop),\n\
7256 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
7257 nopass), \"Q\" (noprint)\n\
7258 Stop means reenter debugger if this signal happens (implies print).\n\
7259 Print means print a message if this signal happens.\n\
7260 Pass means let program see this signal; otherwise program doesn't know.\n\
7261 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
7262 Pass and Stop may be combined."));
7266 stop_command = add_cmd ("stop", class_obscure,
7267 not_just_help_class_command, _("\
7268 There is no `stop' command, but you can set a hook on `stop'.\n\
7269 This allows you to set a list of commands to be run each time execution\n\
7270 of the program stops."), &cmdlist);
7272 add_setshow_zuinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
7273 Set inferior debugging."), _("\
7274 Show inferior debugging."), _("\
7275 When non-zero, inferior specific debugging is enabled."),
7278 &setdebuglist, &showdebuglist);
7280 add_setshow_boolean_cmd ("displaced", class_maintenance,
7281 &debug_displaced, _("\
7282 Set displaced stepping debugging."), _("\
7283 Show displaced stepping debugging."), _("\
7284 When non-zero, displaced stepping specific debugging is enabled."),
7286 show_debug_displaced,
7287 &setdebuglist, &showdebuglist);
7289 add_setshow_boolean_cmd ("non-stop", no_class,
7291 Set whether gdb controls the inferior in non-stop mode."), _("\
7292 Show whether gdb controls the inferior in non-stop mode."), _("\
7293 When debugging a multi-threaded program and this setting is\n\
7294 off (the default, also called all-stop mode), when one thread stops\n\
7295 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7296 all other threads in the program while you interact with the thread of\n\
7297 interest. When you continue or step a thread, you can allow the other\n\
7298 threads to run, or have them remain stopped, but while you inspect any\n\
7299 thread's state, all threads stop.\n\
7301 In non-stop mode, when one thread stops, other threads can continue\n\
7302 to run freely. You'll be able to step each thread independently,\n\
7303 leave it stopped or free to run as needed."),
7309 numsigs = (int) GDB_SIGNAL_LAST;
7310 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
7311 signal_print = (unsigned char *)
7312 xmalloc (sizeof (signal_print[0]) * numsigs);
7313 signal_program = (unsigned char *)
7314 xmalloc (sizeof (signal_program[0]) * numsigs);
7315 signal_catch = (unsigned char *)
7316 xmalloc (sizeof (signal_catch[0]) * numsigs);
7317 signal_pass = (unsigned char *)
7318 xmalloc (sizeof (signal_pass[0]) * numsigs);
7319 for (i = 0; i < numsigs; i++)
7322 signal_print[i] = 1;
7323 signal_program[i] = 1;
7324 signal_catch[i] = 0;
7327 /* Signals caused by debugger's own actions
7328 should not be given to the program afterwards. */
7329 signal_program[GDB_SIGNAL_TRAP] = 0;
7330 signal_program[GDB_SIGNAL_INT] = 0;
7332 /* Signals that are not errors should not normally enter the debugger. */
7333 signal_stop[GDB_SIGNAL_ALRM] = 0;
7334 signal_print[GDB_SIGNAL_ALRM] = 0;
7335 signal_stop[GDB_SIGNAL_VTALRM] = 0;
7336 signal_print[GDB_SIGNAL_VTALRM] = 0;
7337 signal_stop[GDB_SIGNAL_PROF] = 0;
7338 signal_print[GDB_SIGNAL_PROF] = 0;
7339 signal_stop[GDB_SIGNAL_CHLD] = 0;
7340 signal_print[GDB_SIGNAL_CHLD] = 0;
7341 signal_stop[GDB_SIGNAL_IO] = 0;
7342 signal_print[GDB_SIGNAL_IO] = 0;
7343 signal_stop[GDB_SIGNAL_POLL] = 0;
7344 signal_print[GDB_SIGNAL_POLL] = 0;
7345 signal_stop[GDB_SIGNAL_URG] = 0;
7346 signal_print[GDB_SIGNAL_URG] = 0;
7347 signal_stop[GDB_SIGNAL_WINCH] = 0;
7348 signal_print[GDB_SIGNAL_WINCH] = 0;
7349 signal_stop[GDB_SIGNAL_PRIO] = 0;
7350 signal_print[GDB_SIGNAL_PRIO] = 0;
7352 /* These signals are used internally by user-level thread
7353 implementations. (See signal(5) on Solaris.) Like the above
7354 signals, a healthy program receives and handles them as part of
7355 its normal operation. */
7356 signal_stop[GDB_SIGNAL_LWP] = 0;
7357 signal_print[GDB_SIGNAL_LWP] = 0;
7358 signal_stop[GDB_SIGNAL_WAITING] = 0;
7359 signal_print[GDB_SIGNAL_WAITING] = 0;
7360 signal_stop[GDB_SIGNAL_CANCEL] = 0;
7361 signal_print[GDB_SIGNAL_CANCEL] = 0;
7363 /* Update cached state. */
7364 signal_cache_update (-1);
7366 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7367 &stop_on_solib_events, _("\
7368 Set stopping for shared library events."), _("\
7369 Show stopping for shared library events."), _("\
7370 If nonzero, gdb will give control to the user when the dynamic linker\n\
7371 notifies gdb of shared library events. The most common event of interest\n\
7372 to the user would be loading/unloading of a new library."),
7373 set_stop_on_solib_events,
7374 show_stop_on_solib_events,
7375 &setlist, &showlist);
7377 add_setshow_enum_cmd ("follow-fork-mode", class_run,
7378 follow_fork_mode_kind_names,
7379 &follow_fork_mode_string, _("\
7380 Set debugger response to a program call of fork or vfork."), _("\
7381 Show debugger response to a program call of fork or vfork."), _("\
7382 A fork or vfork creates a new process. follow-fork-mode can be:\n\
7383 parent - the original process is debugged after a fork\n\
7384 child - the new process is debugged after a fork\n\
7385 The unfollowed process will continue to run.\n\
7386 By default, the debugger will follow the parent process."),
7388 show_follow_fork_mode_string,
7389 &setlist, &showlist);
7391 add_setshow_enum_cmd ("follow-exec-mode", class_run,
7392 follow_exec_mode_names,
7393 &follow_exec_mode_string, _("\
7394 Set debugger response to a program call of exec."), _("\
7395 Show debugger response to a program call of exec."), _("\
7396 An exec call replaces the program image of a process.\n\
7398 follow-exec-mode can be:\n\
7400 new - the debugger creates a new inferior and rebinds the process\n\
7401 to this new inferior. The program the process was running before\n\
7402 the exec call can be restarted afterwards by restarting the original\n\
7405 same - the debugger keeps the process bound to the same inferior.\n\
7406 The new executable image replaces the previous executable loaded in\n\
7407 the inferior. Restarting the inferior after the exec call restarts\n\
7408 the executable the process was running after the exec call.\n\
7410 By default, the debugger will use the same inferior."),
7412 show_follow_exec_mode_string,
7413 &setlist, &showlist);
7415 add_setshow_enum_cmd ("scheduler-locking", class_run,
7416 scheduler_enums, &scheduler_mode, _("\
7417 Set mode for locking scheduler during execution."), _("\
7418 Show mode for locking scheduler during execution."), _("\
7419 off == no locking (threads may preempt at any time)\n\
7420 on == full locking (no thread except the current thread may run)\n\
7421 step == scheduler locked during every single-step operation.\n\
7422 In this mode, no other thread may run during a step command.\n\
7423 Other threads may run while stepping over a function call ('next')."),
7424 set_schedlock_func, /* traps on target vector */
7425 show_scheduler_mode,
7426 &setlist, &showlist);
7428 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7429 Set mode for resuming threads of all processes."), _("\
7430 Show mode for resuming threads of all processes."), _("\
7431 When on, execution commands (such as 'continue' or 'next') resume all\n\
7432 threads of all processes. When off (which is the default), execution\n\
7433 commands only resume the threads of the current process. The set of\n\
7434 threads that are resumed is further refined by the scheduler-locking\n\
7435 mode (see help set scheduler-locking)."),
7437 show_schedule_multiple,
7438 &setlist, &showlist);
7440 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7441 Set mode of the step operation."), _("\
7442 Show mode of the step operation."), _("\
7443 When set, doing a step over a function without debug line information\n\
7444 will stop at the first instruction of that function. Otherwise, the\n\
7445 function is skipped and the step command stops at a different source line."),
7447 show_step_stop_if_no_debug,
7448 &setlist, &showlist);
7450 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
7451 &can_use_displaced_stepping, _("\
7452 Set debugger's willingness to use displaced stepping."), _("\
7453 Show debugger's willingness to use displaced stepping."), _("\
7454 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7455 supported by the target architecture. If off, gdb will not use displaced\n\
7456 stepping to step over breakpoints, even if such is supported by the target\n\
7457 architecture. If auto (which is the default), gdb will use displaced stepping\n\
7458 if the target architecture supports it and non-stop mode is active, but will not\n\
7459 use it in all-stop mode (see help set non-stop)."),
7461 show_can_use_displaced_stepping,
7462 &setlist, &showlist);
7464 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7465 &exec_direction, _("Set direction of execution.\n\
7466 Options are 'forward' or 'reverse'."),
7467 _("Show direction of execution (forward/reverse)."),
7468 _("Tells gdb whether to execute forward or backward."),
7469 set_exec_direction_func, show_exec_direction_func,
7470 &setlist, &showlist);
7472 /* Set/show detach-on-fork: user-settable mode. */
7474 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7475 Set whether gdb will detach the child of a fork."), _("\
7476 Show whether gdb will detach the child of a fork."), _("\
7477 Tells gdb whether to detach the child of a fork."),
7478 NULL, NULL, &setlist, &showlist);
7480 /* Set/show disable address space randomization mode. */
7482 add_setshow_boolean_cmd ("disable-randomization", class_support,
7483 &disable_randomization, _("\
7484 Set disabling of debuggee's virtual address space randomization."), _("\
7485 Show disabling of debuggee's virtual address space randomization."), _("\
7486 When this mode is on (which is the default), randomization of the virtual\n\
7487 address space is disabled. Standalone programs run with the randomization\n\
7488 enabled by default on some platforms."),
7489 &set_disable_randomization,
7490 &show_disable_randomization,
7491 &setlist, &showlist);
7493 /* ptid initializations */
7494 inferior_ptid = null_ptid;
7495 target_last_wait_ptid = minus_one_ptid;
7497 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
7498 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
7499 observer_attach_thread_exit (infrun_thread_thread_exit);
7500 observer_attach_inferior_exit (infrun_inferior_exit);
7502 /* Explicitly create without lookup, since that tries to create a
7503 value with a void typed value, and when we get here, gdbarch
7504 isn't initialized yet. At this point, we're quite sure there
7505 isn't another convenience variable of the same name. */
7506 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
7508 add_setshow_boolean_cmd ("observer", no_class,
7509 &observer_mode_1, _("\
7510 Set whether gdb controls the inferior in observer mode."), _("\
7511 Show whether gdb controls the inferior in observer mode."), _("\
7512 In observer mode, GDB can get data from the inferior, but not\n\
7513 affect its execution. Registers and memory may not be changed,\n\
7514 breakpoints may not be set, and the program cannot be interrupted\n\