ODR warning for "enum string_repr_result"
[binutils-gdb.git] / gdb / inf-child.c
index 8e5ebfa2c2276cfb4a416f4b233037fb03d53906..56ebd2a5549cadbb910446ba0b27b748328750be 100644 (file)
@@ -1,6 +1,6 @@
 /* Base/prototype target for default child (native) targets.
 
-   Copyright (C) 1988-2018 Free Software Foundation, Inc.
+   Copyright (C) 1988-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "inferior.h"
 #include <sys/stat.h>
 #include "inf-child.h"
-#include "fileio.h"
-#include "agent.h"
-#include "gdb_wait.h"
-#include "filestuff.h"
+#include "gdbsupport/fileio.h"
+#include "gdbsupport/agent.h"
+#include "gdbsupport/gdb_wait.h"
+#include "gdbsupport/filestuff.h"
 
 #include <sys/types.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-/* Helper function for child_wait and the derivatives of child_wait.
-   HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
-   translation of that in OURSTATUS.  */
-void
-store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
+static const target_info inf_child_target_info = {
+  "native",
+  N_("Native process"),
+  N_("Native process (started by the \"run\" command).")
+};
+
+const target_info &
+inf_child_target::info () const
+{
+  return inf_child_target_info;
+}
+
+/* See inf-child.h.  */
+
+target_waitstatus
+host_status_to_waitstatus (int hoststatus)
 {
   if (WIFEXITED (hoststatus))
-    {
-      ourstatus->kind = TARGET_WAITKIND_EXITED;
-      ourstatus->value.integer = WEXITSTATUS (hoststatus);
-    }
+    return target_waitstatus ().set_exited (WEXITSTATUS (hoststatus));
   else if (!WIFSTOPPED (hoststatus))
-    {
-      ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
-      ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
-    }
+    return target_waitstatus ().set_signalled
+      (gdb_signal_from_host (WTERMSIG (hoststatus)));
   else
-    {
-      ourstatus->kind = TARGET_WAITKIND_STOPPED;
-      ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
-    }
+    return target_waitstatus ().set_stopped
+      (gdb_signal_from_host (WSTOPSIG (hoststatus)));
 }
 
 inf_child_target::~inf_child_target ()
@@ -101,6 +105,12 @@ inf_child_target::terminal_inferior ()
   child_terminal_inferior (this);
 }
 
+void
+inf_child_target::terminal_save_inferior ()
+{
+  child_terminal_save_inferior (this);
+}
+
 void
 inf_child_target::terminal_ours_for_output ()
 {
@@ -139,20 +149,19 @@ static int inf_child_explicitly_opened;
 /* See inf-child.h.  */
 
 void
-inf_child_open_target (struct target_ops *target, const char *arg,
-                      int from_tty)
+inf_child_open_target (const char *arg, int from_tty)
 {
+  target_ops *target = get_native_target ();
+
+  /* There's always only ever one native target, and if we get here,
+     it better be an inf-child target.  */
+  gdb_assert (dynamic_cast<inf_child_target *> (target) != NULL);
+
   target_preopen (from_tty);
-  push_target (target);
+  current_inferior ()->push_target (target);
   inf_child_explicitly_opened = 1;
   if (from_tty)
-    printf_filtered ("Done.  Use the \"run\" command to start a process.\n");
-}
-
-void
-inf_child_target::open (const char *arg, int from_tty)
-{
-  inf_child_open_target (this, arg, from_tty);
+    gdb_printf ("Done.  Use the \"run\" command to start a process.\n");
 }
 
 /* Implement the to_disconnect target_ops method.  */
@@ -189,15 +198,8 @@ inf_child_target::mourn_inferior ()
 void
 inf_child_target::maybe_unpush_target ()
 {
-  if (!inf_child_explicitly_opened && !have_inferiors ())
-    unpush_target (this);
-}
-
-void
-inf_child_target::post_startup_inferior (ptid_t ptid)
-{
-  /* This target doesn't require a meaningful "post startup inferior"
-     operation by a debugger.  */
+  if (!inf_child_explicitly_opened)
+    current_inferior ()->unpush_target (this);
 }
 
 bool
@@ -218,7 +220,7 @@ inf_child_target::can_attach ()
   return true;
 }
 
-char *
+const char *
 inf_child_target::pid_to_exec_file (int pid)
 {
   /* This target doesn't support translation of a process ID to the
@@ -226,36 +228,6 @@ inf_child_target::pid_to_exec_file (int pid)
   return NULL;
 }
 
-bool
-inf_child_target::has_all_memory ()
-{
-  return default_child_has_all_memory ();
-}
-
-bool
-inf_child_target::has_memory ()
-{
-  return default_child_has_memory ();
-}
-
-bool
-inf_child_target::has_stack ()
-{
-  return default_child_has_stack ();
-}
-
-bool
-inf_child_target::has_registers ()
-{
-  return default_child_has_registers ();
-}
-
-bool
-inf_child_target::has_execution (ptid_t ptid)
-{
-  return default_child_has_execution (ptid);
-}
-
 /* Implementation of to_fileio_open.  */
 
 int
@@ -274,7 +246,7 @@ inf_child_target::fileio_open (struct inferior *inf, const char *filename,
       return -1;
     }
 
-  fd = gdb_open_cloexec (filename, nat_flags, nat_mode);
+  fd = gdb_open_cloexec (filename, nat_flags, nat_mode).release ();
   if (fd == -1)
     *target_errno = host_to_fileio_error (errno);
 
@@ -422,7 +394,29 @@ inf_child_target::can_use_agent ()
   return agent_loaded_p ();
 }
 
-inf_child_target::inf_child_target ()
+void
+inf_child_target::follow_exec (inferior *follow_inf, ptid_t ptid,
+                              const char *execd_pathname)
+{
+  inferior *orig_inf = current_inferior ();
+
+  process_stratum_target::follow_exec (follow_inf, ptid, execd_pathname);
+
+  if (orig_inf != follow_inf)
+    {
+      /* If the target was implicitly push in the original inferior, unpush
+         it.  */
+      scoped_restore_current_thread restore_thread;
+      switch_to_inferior_no_thread (orig_inf);
+      maybe_unpush_target ();
+    }
+}
+
+/* See inf-child.h.  */
+
+void
+add_inf_child_target (inf_child_target *target)
 {
-  this->to_stratum = process_stratum;
+  set_native_target (target);
+  add_target (inf_child_target_info, inf_child_open_target);
 }