Share fork_inferior et al with gdbserver
[deliverable/binutils-gdb.git] / gdb / gdbserver / spu-low.c
index 117b871408cba137f5c94b42eadc6ff9c5b554d3..0f770a0df1f206263798f2fa159ca173567d555d 100644 (file)
@@ -27,6 +27,7 @@
 #include <sys/syscall.h>
 #include "filestuff.h"
 #include "hostio.h"
+#include "nat/fork-inferior.h"
 
 /* Some older glibc versions do not define this.  */
 #ifndef __WNOTHREAD
@@ -261,36 +262,37 @@ spu_proc_xfer_spu (const char *annex, unsigned char *readbuf,
   return ret;
 }
 
+/* Callback to be used when calling fork_inferior, responsible for
+   actually initiating the tracing of the inferior.  */
+
+static void
+spu_ptrace_fun ()
+{
+  if (ptrace (PTRACE_TRACEME, 0, 0, 0) < 0)
+    trace_start_error_with_name ("ptrace");
+  if (setpgid (0, 0) < 0)
+    trace_start_error_with_name ("setpgid");
+}
 
 /* Start an inferior process and returns its pid.
-   ALLARGS is a vector of program-name and args. */
+   PROGRAM is the name of the program to be started, and PROGRAM_ARGS
+   are its arguments.  */
+
 static int
-spu_create_inferior (char *program, char **allargs)
+spu_create_inferior (const char *program,
+                    const std::vector<char *> &program_argv)
 {
   int pid;
   ptid_t ptid;
   struct process_info *proc;
+  std::string str_program_args = stringify_argv (program_args);
 
-  pid = fork ();
-  if (pid < 0)
-    perror_with_name ("fork");
-
-  if (pid == 0)
-    {
-      close_most_fds ();
-      ptrace (PTRACE_TRACEME, 0, 0, 0);
-
-      setpgid (0, 0);
-
-      execv (program, allargs);
-      if (errno == ENOENT)
-       execvp (program, allargs);
+  pid = fork_inferior (program,
+                      str_program_args.c_str (),
+                      environ_vector (get_environ ()), spu_ptrace_fun,
+                      NULL, NULL, NULL, NULL);
 
-      fprintf (stderr, "Cannot exec %s: %s.\n", program,
-              strerror (errno));
-      fflush (stderr);
-      _exit (0177);
-    }
+  post_fork_inferior (pid, program);
 
   proc = add_process (pid, 0);
   proc->tdesc = tdesc_spu;
This page took 0.028032 seconds and 4 git commands to generate.