+ int should_resume = 1;
+ struct thread_info *tp;
+
+ /* Copy user stepping state to the new inferior thread. FIXME: the
+ followed fork child thread should have a copy of most of the
+ parent thread structure's run control related fields, not just
+ these. */
+ struct breakpoint *step_resume_breakpoint;
+ CORE_ADDR step_range_start;
+ CORE_ADDR step_range_end;
+ struct frame_id step_frame_id;
+
+ if (!non_stop)
+ {
+ ptid_t wait_ptid;
+ struct target_waitstatus wait_status;
+
+ /* Get the last target status returned by target_wait(). */
+ get_last_target_status (&wait_ptid, &wait_status);
+
+ /* If not stopped at a fork event, then there's nothing else to
+ do. */
+ if (wait_status.kind != TARGET_WAITKIND_FORKED
+ && wait_status.kind != TARGET_WAITKIND_VFORKED)
+ return 1;
+
+ /* Check if we switched over from WAIT_PTID, since the event was
+ reported. */
+ if (!ptid_equal (wait_ptid, minus_one_ptid)
+ && !ptid_equal (inferior_ptid, wait_ptid))
+ {
+ /* We did. Switch back to WAIT_PTID thread, to tell the
+ target to follow it (in either direction). We'll
+ afterwards refuse to resume, and inform the user what
+ happened. */
+ switch_to_thread (wait_ptid);
+ should_resume = 0;
+ }
+ }
+
+ tp = inferior_thread ();
+
+ /* If there were any forks/vforks that were caught and are now to be
+ followed, then do so now. */
+ switch (tp->pending_follow.kind)
+ {
+ case TARGET_WAITKIND_FORKED:
+ case TARGET_WAITKIND_VFORKED:
+ {
+ ptid_t parent, child;
+
+ /* If the user did a next/step, etc, over a fork call,
+ preserve the stepping state in the fork child. */
+ if (follow_child && should_resume)
+ {
+ step_resume_breakpoint
+ = clone_momentary_breakpoint (tp->step_resume_breakpoint);
+ step_range_start = tp->step_range_start;
+ step_range_end = tp->step_range_end;
+ step_frame_id = tp->step_frame_id;
+
+ /* For now, delete the parent's sr breakpoint, otherwise,
+ parent/child sr breakpoints are considered duplicates,
+ and the child version will not be installed. Remove
+ this when the breakpoints module becomes aware of
+ inferiors and address spaces. */
+ delete_step_resume_breakpoint (tp);
+ tp->step_range_start = 0;
+ tp->step_range_end = 0;
+ tp->step_frame_id = null_frame_id;
+ }
+
+ parent = inferior_ptid;
+ child = tp->pending_follow.value.related_pid;
+
+ /* Tell the target to do whatever is necessary to follow
+ either parent or child. */
+ if (target_follow_fork (follow_child))
+ {
+ /* Target refused to follow, or there's some other reason
+ we shouldn't resume. */
+ should_resume = 0;
+ }
+ else
+ {
+ /* This pending follow fork event is now handled, one way
+ or another. The previous selected thread may be gone
+ from the lists by now, but if it is still around, need
+ to clear the pending follow request. */
+ tp = find_thread_pid (parent);
+ if (tp)
+ tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
+
+ /* This makes sure we don't try to apply the "Switched
+ over from WAIT_PID" logic above. */
+ nullify_last_target_wait_ptid ();
+
+ /* If we followed the child, switch to it... */
+ if (follow_child)
+ {
+ switch_to_thread (child);
+
+ /* ... and preserve the stepping state, in case the
+ user was stepping over the fork call. */
+ if (should_resume)
+ {
+ tp = inferior_thread ();
+ tp->step_resume_breakpoint = step_resume_breakpoint;
+ tp->step_range_start = step_range_start;
+ tp->step_range_end = step_range_end;
+ tp->step_frame_id = step_frame_id;
+ }
+ else
+ {
+ /* If we get here, it was because we're trying to
+ resume from a fork catchpoint, but, the user
+ has switched threads away from the thread that
+ forked. In that case, the resume command
+ issued is most likely not applicable to the
+ child, so just warn, and refuse to resume. */
+ warning (_("\
+Not resuming: switched threads before following fork child.\n"));
+ }
+
+ /* Reset breakpoints in the child as appropriate. */
+ follow_inferior_reset_breakpoints ();
+ }
+ else
+ switch_to_thread (parent);
+ }
+ }
+ break;
+ case TARGET_WAITKIND_SPURIOUS:
+ /* Nothing to follow. */
+ break;
+ default:
+ internal_error (__FILE__, __LINE__,
+ "Unexpected pending_follow.kind %d\n",
+ tp->pending_follow.kind);
+ break;
+ }