Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: David Jenkins <david@scootersoftware.com>
To: gdb@sourceware.org
Subject: Re: gdb on macOS 10.12 "Sierra"
Date: Thu, 03 Nov 2016 22:05:00 -0000	[thread overview]
Message-ID: <7638e6c3-2efd-1d07-10ef-d467b1a68352@scootersoftware.com> (raw)

I can confirm that refreshing the task port after exec does allow gdb to 
work.  Here is a patch that I made against 7.12 that allows gdb to run 
on Sierra.  I cannot speak to the complete quality of the patch as this 
is my first dive into gdb code.  We just starting using the patched gdb 
and it does allows us to break, step, and view variables in the Lazarus IDE.

Patch is inlined below

David Jenkins

Scooter Software


--- gdb-7.12/gdb/darwin-nat.c    2016-10-07 12:09:21.000000000 -0500
+++ gdb-7.12-dfj/gdb/darwin-nat.c    2016-11-03 15:56:49.000000000 -0500
@@ -171,6 +171,9 @@
    "</dict>\n"
    "</plist>\n";

+static kern_return_t darwin_save_exception_ports (darwin_inferior *inf);
+static kern_return_t darwin_restore_exception_ports (darwin_inferior *inf);
+
  static void inferior_debug (int level, const char *fmt, ...)
    ATTRIBUTE_PRINTF (2, 3);

@@ -401,6 +404,12 @@
  }

  static int
+find_inferior_pid_it (struct inferior *inf, void *pid_ptr)
+{
+  return inf->pid == *(int *)pid_ptr;
+}
+
+static int
  find_inferior_task_it (struct inferior *inf, void *port_ptr)
  {
    return inf->priv->task == *(task_t*)port_ptr;
@@ -412,6 +421,13 @@
    return inf->priv->notify_port == *(task_t*)port_ptr;
  }

+/* Return an inferior by pid.  */
+static struct inferior *
+darwin_find_inferior_by_pid (int pid)
+{
+  return iterate_over_inferiors (&find_inferior_pid_it, &pid);
+}
+
  /* Return an inferior by task port.  */
  static struct inferior *
  darwin_find_inferior_by_task (task_t port)
@@ -572,6 +588,7 @@
    thread_t thread_port;
    kern_return_t kret;
    int i;
+  int task_pid;

    /* Check message destination.  */
    if (hdr->msgh_local_port != darwin_ex_port)
@@ -606,6 +623,7 @@

    task_port = desc[1].name;
    thread_port = desc[0].name;
+  pid_for_task(task_port, &task_pid);

    /* We got new rights to the task, get rid of it.  Do not get rid of 
thread
       right, as we will need it to find the thread.  */
@@ -617,25 +635,61 @@
    *pinf = inf;
    if (inf == NULL)
      {
-      /* Not a known inferior.  This could happen if the child fork, as
-     the created process will inherit its exception port.
-     FIXME: should the exception port be restored ?  */
-      kern_return_t kret;
-      mig_reply_error_t reply;
+      if MACH_PORT_VALID(task_port)
+    {
+      //int task_pid;
+      mach_port_name_t new_task;
+      mach_port_t prev_not;
+      exception_mask_t mask;
+
+      inf = darwin_find_inferior_by_pid(task_pid);
+      if (inf == NULL)
+        return 0;

-      /* Free thread port (we don't know it).  */
-      kret = mach_port_deallocate (mach_task_self (), thread_port);
-      MACH_CHECK_ERROR (kret);
+      kret = mach_port_deallocate (mach_task_self (), inf->priv->task);
+      MACH_CHECK_ERROR (kret);

-      darwin_encode_reply (&reply, hdr, KERN_SUCCESS);
+      task_for_pid(gdb_task, task_pid, &new_task);

-      kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
-               reply.Head.msgh_size, 0,
-               MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
-               MACH_PORT_NULL);
-      MACH_CHECK_ERROR (kret);
+      inf->priv->task = new_task;
+      *pinf = inf;
+
+      mach_port_request_notification (gdb_task, inf->priv->task,
+                      MACH_NOTIFY_DEAD_NAME, 0,
+                      inf->priv->notify_port,
+                      MACH_MSG_TYPE_MAKE_SEND_ONCE,
+                      &prev_not);
+
+      darwin_restore_exception_ports(inf->priv);
+      if (enable_mach_exceptions)
+        mask = EXC_MASK_ALL;
+      else
+        mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
+      task_set_exception_ports (inf->priv->task, mask, darwin_ex_port,
+                    EXCEPTION_DEFAULT, THREAD_STATE_NONE);
+    }
+      else
+    {
+      /* Not a known inferior.  This could happen if the child fork, as
+         the created process will inherit its exception port.
+         FIXME: should the exception port be restored ?  */
+      kern_return_t kret;
+      mig_reply_error_t reply;
+
+      /* Free thread port (we don't know it).  */
+      kret = mach_port_deallocate (mach_task_self (), thread_port);
+      MACH_CHECK_ERROR (kret);

-      return 0;
+      darwin_encode_reply (&reply, hdr, KERN_SUCCESS);
+
+      kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
+               reply.Head.msgh_size, 0,
+               MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
+               MACH_PORT_NULL);
+      MACH_CHECK_ERROR (kret);
+
+      return 0;
+    }
      }

    /* Find thread by port.  */
@@ -900,6 +954,7 @@
  {
    darwin_thread_t *thread;
    struct inferior *inf;
+

    /* Exception message.  2401 == 0x961 is exc.  */
    if (hdr->msgh_id == 2401)
@@ -992,6 +1047,14 @@
        inf = darwin_find_inferior_by_notify (hdr->msgh_local_port);
        if (inf != NULL)
      {
+     mach_dead_name_notification_t *notif;
+     mach_port_t dead_port;
+
+    notif = (mach_dead_name_notification_t *)(hdr);
+    dead_port = notif->not_port;
+      if (dead_port != inf->priv->task)
+        return ptid_build (inf->pid, 0, 0);
+
        if (!inf->priv->no_ptrace)
          {
            pid_t res;



             reply	other threads:[~2016-11-03 22:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-03 22:05 David Jenkins [this message]
2016-11-09  9:35 ` [Patch] Fix " Tristan Gingold
  -- strict thread matches above, loose matches on Subject: below --
2016-10-31 23:14 C Western
2016-10-01 12:08 Jonas Maebe
2016-10-04 11:42 ` Tristan Gingold
2016-10-04 13:47   ` Tedeschi, Walfred
2016-10-31 11:54   ` Jonas Maebe
2016-10-31 12:00     ` Jack Howarth
2016-10-31 12:11       ` Jonas Maebe
2016-10-31 23:24     ` Jason Molenda
2016-11-03 16:00       ` Tristan Gingold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7638e6c3-2efd-1d07-10ef-d467b1a68352@scootersoftware.com \
    --to=david@scootersoftware.com \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox