]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/darwin-nat.c
Fix build failure in darwin-nat.c
[thirdparty/binutils-gdb.git] / gdb / darwin-nat.c
index 6ca659f4a3aab5ef424eedbf407e5a0af882ccbb..6e420c42555e5d5b30212f0aa4404cc956aed34a 100644 (file)
@@ -1,5 +1,5 @@
 /* Darwin support for GDB, the GNU debugger.
-   Copyright (C) 2008-2016 Free Software Foundation, Inc.
+   Copyright (C) 2008-2017 Free Software Foundation, Inc.
 
    Contributed by AdaCore.
 
@@ -25,7 +25,6 @@
 #include "symfile.h"
 #include "symtab.h"
 #include "objfiles.h"
-#include "gdb.h"
 #include "gdbcmd.h"
 #include "gdbcore.h"
 #include "gdbthread.h"
@@ -63,6 +62,7 @@
 
 #include "darwin-nat.h"
 #include "common/filestuff.h"
+#include "nat/fork-inferior.h"
 
 /* Quick overview.
    Darwin kernel is Mach + BSD derived kernel.  Note that they share the
@@ -102,12 +102,14 @@ static void darwin_ptrace_me (void);
 
 static void darwin_ptrace_him (int pid);
 
-static void darwin_create_inferior (struct target_ops *ops, char *exec_file,
-                                   char *allargs, char **env, int from_tty);
+static void darwin_create_inferior (struct target_ops *ops,
+                                   const char *exec_file,
+                                   const std::string &allargs,
+                                   char **env, int from_tty);
 
 static void darwin_files_info (struct target_ops *ops);
 
-static char *darwin_pid_to_str (struct target_ops *ops, ptid_t tpid);
+static const char *darwin_pid_to_str (struct target_ops *ops, ptid_t tpid);
 
 static int darwin_thread_alive (struct target_ops *ops, ptid_t tpid);
 
@@ -364,29 +366,14 @@ darwin_check_new_threads (struct inferior *inf)
       if (new_ix < new_nbr && (old_ix == old_nbr || new_id < old_id))
        {
          /* A thread was created.  */
-         struct thread_info *tp;
          struct private_thread_info *pti;
 
          pti = XCNEW (struct private_thread_info);
          pti->gdb_port = new_id;
          pti->msg_state = DARWIN_RUNNING;
 
-         if (old_nbr == 0 && new_ix == 0)
-           {
-             /* A ptid is create when the inferior is started (see
-                fork-child.c) with lwp=tid=0.  This ptid will be renamed
-                later by darwin_init_thread_list ().  */
-             tp = find_thread_ptid (ptid_build (inf->pid, 0, 0));
-             gdb_assert (tp);
-             gdb_assert (tp->priv == NULL);
-             tp->priv = pti;
-           }
-         else
-           {
-             /* Add the new thread.  */
-             tp = add_thread_with_info
-               (ptid_build (inf->pid, 0, new_id), pti);
-           }
+         /* Add the new thread.  */
+         add_thread_with_info (ptid_build (inf->pid, 0, new_id), pti);
          VEC_safe_push (darwin_thread_t, thread_vec, pti);
          new_ix++;
          continue;
@@ -1203,11 +1190,11 @@ cancel_breakpoint (ptid_t ptid)
      tripped on it.  */
 
   struct regcache *regcache = get_thread_regcache (ptid);
-  struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  struct gdbarch *gdbarch = regcache->arch ();
   CORE_ADDR pc;
 
   pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
-  if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
+  if (breakpoint_inserted_here_p (regcache->aspace (), pc))
     {
       inferior_debug (4, "cancel_breakpoint for thread 0x%lx\n",
                      (unsigned long) ptid_get_tid (ptid));
@@ -1698,23 +1685,38 @@ darwin_attach_pid (struct inferior *inf)
     push_target (darwin_ops);
 }
 
+/* Get the thread_info object corresponding to this private_thread_info.  */
+
+static struct thread_info *
+thread_info_from_private_thread_info (private_thread_info *pti)
+{
+  struct thread_info *it;
+
+  ALL_THREADS (it)
+    {
+      if (it->priv->gdb_port == pti->gdb_port)
+       break;
+    }
+
+  gdb_assert (it != NULL);
+
+  return it;
+}
+
 static void
 darwin_init_thread_list (struct inferior *inf)
 {
-  darwin_thread_t *thread;
-  ptid_t new_ptid;
-
   darwin_check_new_threads (inf);
 
-  gdb_assert (inf->priv->threads
-             && VEC_length (darwin_thread_t, inf->priv->threads) > 0);
-  thread = VEC_index (darwin_thread_t, inf->priv->threads, 0);
+  gdb_assert (inf->priv->threads != NULL);
+  gdb_assert (VEC_length (darwin_thread_t, inf->priv->threads) > 0);
+
+  private_thread_info *first_pti
+    = VEC_index (darwin_thread_t, inf->priv->threads, 0);
+  struct thread_info *first_thread
+    = thread_info_from_private_thread_info (first_pti);
 
-  /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
-     Fix up.  */
-  new_ptid = ptid_build (inf->pid, 0, thread->gdb_port);
-  thread_change_ptid (inferior_ptid, new_ptid);
-  inferior_ptid = new_ptid;
+  inferior_ptid = first_thread->ptid;
 }
 
 /* The child must synchronize with gdb: gdb must set the exception port
@@ -1729,22 +1731,28 @@ darwin_ptrace_me (void)
   char c;
 
   /* Close write end point.  */
-  close (ptrace_fds[1]);
+  if (close (ptrace_fds[1]) < 0)
+    trace_start_error_with_name ("close");
 
   /* Wait until gdb is ready.  */
   res = read (ptrace_fds[0], &c, 1);
   if (res != 0)
-    error (_("unable to read from pipe, read returned: %d"), res);
-  close (ptrace_fds[0]);
+    trace_start_error (_("unable to read from pipe, read returned: %d"), res);
+
+  if (close (ptrace_fds[0]) < 0)
+    trace_start_error_with_name ("close");
 
   /* Get rid of privileges.  */
-  setegid (getgid ());
+  if (setegid (getgid ()) < 0)
+    trace_start_error_with_name ("setegid");
 
   /* Set TRACEME.  */
-  PTRACE (PT_TRACE_ME, 0, 0, 0);
+  if (PTRACE (PT_TRACE_ME, 0, 0, 0) < 0)
+    trace_start_error_with_name ("PTRACE");
 
   /* Redirect signals to exception port.  */
-  PTRACE (PT_SIGEXC, 0, 0, 0);
+  if (PTRACE (PT_SIGEXC, 0, 0, 0) < 0)
+    trace_start_error_with_name ("PTRACE");
 }
 
 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).  */
@@ -1782,7 +1790,7 @@ darwin_ptrace_him (int pid)
 
   darwin_init_thread_list (inf);
 
-  startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
+  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
 }
 
 static void
@@ -1820,16 +1828,15 @@ darwin_execvp (const char *file, char * const argv[], char * const env[])
 }
 
 static void
-darwin_create_inferior (struct target_ops *ops, char *exec_file,
-                       char *allargs, char **env, int from_tty)
+darwin_create_inferior (struct target_ops *ops,
+                       const char *exec_file,
+                       const std::string &allargs,
+                       char **env, int from_tty)
 {
   /* Do the hard work.  */
-  fork_inferior (exec_file, allargs, env, darwin_ptrace_me, darwin_ptrace_him,
-                darwin_pre_ptrace, NULL, darwin_execvp);
-
-  /* Return now in case of error.  */
-  if (ptid_equal (inferior_ptid, null_ptid))
-    return;
+  fork_inferior (exec_file, allargs, env, darwin_ptrace_me,
+                darwin_ptrace_him, darwin_pre_ptrace, NULL,
+                darwin_execvp);
 }
 \f
 
@@ -1899,9 +1906,6 @@ darwin_attach (struct target_ops *ops, const char *args, int from_tty)
   inferior_appeared (inf, pid);
   inf->attach_flag = 1;
 
-  /* Always add a main thread.  */
-  add_thread_silent (inferior_ptid);
-
   darwin_attach_pid (inf);
 
   darwin_suspend_inferior (inf);
@@ -1964,7 +1968,7 @@ darwin_files_info (struct target_ops *ops)
 {
 }
 
-static char *
+static const char *
 darwin_pid_to_str (struct target_ops *ops, ptid_t ptid)
 {
   static char buf[80];
@@ -2225,7 +2229,7 @@ darwin_xfer_partial (struct target_ops *ops,
 }
 
 static void
-set_enable_mach_exceptions (char *args, int from_tty,
+set_enable_mach_exceptions (const char *args, int from_tty,
                            struct cmd_list_element *c)
 {
   if (!ptid_equal (inferior_ptid, null_ptid))
@@ -2333,9 +2337,6 @@ darwin_supports_multi_process (struct target_ops *self)
   return 1;
 }
 
-/* -Wmissing-prototypes */
-extern initialize_file_ftype _initialize_darwin_inferior;
-
 void
 _initialize_darwin_inferior (void)
 {