diff --git a/gdb/infrun.c b/gdb/infrun.c
index 57405d7..f628fd1 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4317,6 +4317,74 @@
   set_resumed (mark_ptid, 0);
 }
 
+/* Handle a TARGET_WAITKIND_EXITED or TARGET_WAITKIND_SIGNALLED
+   event.  */
+
+static void
+handle_inferior_exit (ptid_t event_ptid,
+		      struct target_waitstatus ws)
+{
+  gdb_assert (ws.kind == TARGET_WAITKIND_EXITED
+	      || ws.kind == TARGET_WAITKIND_SIGNALLED);
+
+  inferior_ptid = event_ptid;
+  set_current_inferior (find_inferior_ptid (event_ptid));
+  set_current_program_space (current_inferior ()->pspace);
+  handle_vfork_child_exec_or_exit (0);
+  target_terminal::ours ();	/* Must do this before mourn anyway.  */
+
+  /* Clearing any previous state of convenience variables.  */
+  clear_exit_convenience_vars ();
+
+  if (ws.kind == TARGET_WAITKIND_EXITED)
+    {
+      /* Record the exit code in the convenience variable $_exitcode, so
+	 that the user can inspect this again later.  */
+      set_internalvar_integer (lookup_internalvar ("_exitcode"),
+			       (LONGEST) ws.value.integer);
+
+      /* Also record this in the inferior itself.  */
+      current_inferior ()->has_exit_code = 1;
+      current_inferior ()->exit_code = (LONGEST) ws.value.integer;
+
+      /* Support the --return-child-result option.  */
+      return_child_result_value = ws.value.integer;
+
+      gdb::observers::exited.notify (ws.value.integer);
+    }
+  else
+    {
+      struct gdbarch *gdbarch = current_inferior ()->gdbarch;
+
+      if (gdbarch_gdb_signal_to_target_p (gdbarch))
+	{
+	  /* Set the value of the internal variable $_exitsignal,
+	     which holds the signal uncaught by the inferior.  */
+	  set_internalvar_integer (lookup_internalvar ("_exitsignal"),
+				   gdbarch_gdb_signal_to_target (gdbarch,
+								 ws.value.sig));
+	}
+      else
+	{
+	  /* We don't have access to the target's method used for
+	     converting between signal numbers (GDB's internal
+	     representation <-> target's representation).
+	     Therefore, we cannot do a good job at displaying this
+	     information to the user.  It's better to just warn
+	     her about it (if infrun debugging is enabled), and
+	     give up.  */
+	  if (debug_infrun)
+	    fprintf_filtered (gdb_stdlog, _("\
+Cannot fill $_exitsignal with the correct signal number.\n"));
+	}
+
+      gdb::observers::signal_exited.notify (ws.value.sig);
+    }
+
+  gdb_flush (gdb_stdout);
+  target_mourn_inferior (inferior_ptid);
+}
+
 /* See infrun.h.  */
 
 void
@@ -4842,62 +4910,7 @@
 
     case TARGET_WAITKIND_EXITED:
     case TARGET_WAITKIND_SIGNALLED:
-      inferior_ptid = ecs->ptid;
-      set_current_inferior (find_inferior_ptid (ecs->ptid));
-      set_current_program_space (current_inferior ()->pspace);
-      handle_vfork_child_exec_or_exit (0);
-      target_terminal::ours ();	/* Must do this before mourn anyway.  */
-
-      /* Clearing any previous state of convenience variables.  */
-      clear_exit_convenience_vars ();
-
-      if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
-	{
-	  /* Record the exit code in the convenience variable $_exitcode, so
-	     that the user can inspect this again later.  */
-	  set_internalvar_integer (lookup_internalvar ("_exitcode"),
-				   (LONGEST) ecs->ws.value.integer);
-
-	  /* Also record this in the inferior itself.  */
-	  current_inferior ()->has_exit_code = 1;
-	  current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
-
-	  /* Support the --return-child-result option.  */
-	  return_child_result_value = ecs->ws.value.integer;
-
-	  gdb::observers::exited.notify (ecs->ws.value.integer);
-	}
-      else
-	{
-	  struct gdbarch *gdbarch = current_inferior ()->gdbarch;
-
-	  if (gdbarch_gdb_signal_to_target_p (gdbarch))
-	    {
-	      /* Set the value of the internal variable $_exitsignal,
-		 which holds the signal uncaught by the inferior.  */
-	      set_internalvar_integer (lookup_internalvar ("_exitsignal"),
-				       gdbarch_gdb_signal_to_target (gdbarch,
-							  ecs->ws.value.sig));
-	    }
-	  else
-	    {
-	      /* We don't have access to the target's method used for
-		 converting between signal numbers (GDB's internal
-		 representation <-> target's representation).
-		 Therefore, we cannot do a good job at displaying this
-		 information to the user.  It's better to just warn
-		 her about it (if infrun debugging is enabled), and
-		 give up.  */
-	      if (debug_infrun)
-		fprintf_filtered (gdb_stdlog, _("\
-Cannot fill $_exitsignal with the correct signal number.\n"));
-	    }
-
-	  gdb::observers::signal_exited.notify (ecs->ws.value.sig);
-	}
-
-      gdb_flush (gdb_stdout);
-      target_mourn_inferior (inferior_ptid);
+      handle_inferior_exit (ecs->ptid, ecs->ws);
       stop_print_frame = 0;
       stop_waiting (ecs);
       return;
