Home | History | Annotate | Line # | Download | only in gdb
darwin-nat.c revision 1.10
      1   1.1  christos /* Darwin support for GDB, the GNU debugger.
      2  1.10  christos    Copyright (C) 2008-2023 Free Software Foundation, Inc.
      3   1.1  christos 
      4   1.1  christos    Contributed by AdaCore.
      5   1.1  christos 
      6   1.1  christos    This file is part of GDB.
      7   1.1  christos 
      8   1.1  christos    This program is free software; you can redistribute it and/or modify
      9   1.1  christos    it under the terms of the GNU General Public License as published by
     10   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11   1.1  christos    (at your option) any later version.
     12   1.1  christos 
     13   1.1  christos    This program is distributed in the hope that it will be useful,
     14   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16   1.1  christos    GNU General Public License for more details.
     17   1.1  christos 
     18   1.1  christos    You should have received a copy of the GNU General Public License
     19   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20   1.1  christos 
     21   1.1  christos #include "defs.h"
     22   1.1  christos #include "top.h"
     23   1.1  christos #include "inferior.h"
     24   1.1  christos #include "target.h"
     25   1.1  christos #include "symfile.h"
     26   1.1  christos #include "symtab.h"
     27   1.1  christos #include "objfiles.h"
     28   1.1  christos #include "gdbcmd.h"
     29   1.1  christos #include "gdbcore.h"
     30   1.1  christos #include "gdbthread.h"
     31   1.1  christos #include "regcache.h"
     32   1.1  christos #include "event-top.h"
     33   1.1  christos #include "inf-loop.h"
     34   1.1  christos #include <sys/stat.h>
     35   1.1  christos #include "inf-child.h"
     36   1.1  christos #include "value.h"
     37   1.1  christos #include "arch-utils.h"
     38   1.1  christos #include "bfd.h"
     39   1.1  christos #include "bfd/mach-o.h"
     40   1.9  christos #include "gdbarch.h"
     41   1.1  christos 
     42   1.8  christos #include <copyfile.h>
     43   1.1  christos #include <sys/ptrace.h>
     44   1.1  christos #include <sys/signal.h>
     45   1.3  christos #include <setjmp.h>
     46   1.1  christos #include <sys/types.h>
     47   1.1  christos #include <unistd.h>
     48   1.1  christos #include <signal.h>
     49   1.1  christos #include <ctype.h>
     50   1.1  christos #include <sys/sysctl.h>
     51   1.1  christos #include <sys/proc.h>
     52   1.1  christos #include <libproc.h>
     53   1.1  christos #include <sys/syscall.h>
     54   1.1  christos #include <spawn.h>
     55   1.1  christos 
     56   1.1  christos #include <mach/mach_error.h>
     57   1.1  christos #include <mach/mach_vm.h>
     58   1.1  christos #include <mach/mach_init.h>
     59   1.1  christos #include <mach/vm_map.h>
     60   1.1  christos #include <mach/task.h>
     61   1.1  christos #include <mach/mach_port.h>
     62   1.1  christos #include <mach/thread_act.h>
     63   1.1  christos #include <mach/port.h>
     64   1.1  christos 
     65   1.1  christos #include "darwin-nat.h"
     66   1.8  christos #include "filenames.h"
     67   1.9  christos #include "gdbsupport/filestuff.h"
     68   1.9  christos #include "gdbsupport/gdb_unlinker.h"
     69   1.9  christos #include "gdbsupport/pathstuff.h"
     70   1.9  christos #include "gdbsupport/scoped_fd.h"
     71   1.8  christos #include "nat/fork-inferior.h"
     72   1.1  christos 
     73   1.1  christos /* Quick overview.
     74   1.1  christos    Darwin kernel is Mach + BSD derived kernel.  Note that they share the
     75   1.1  christos    same memory space and are linked together (ie there is no micro-kernel).
     76   1.1  christos 
     77   1.1  christos    Although ptrace(2) is available on Darwin, it is not complete.  We have
     78   1.1  christos    to use Mach calls to read and write memory and to modify registers.  We
     79   1.1  christos    also use Mach to get inferior faults.  As we cannot use select(2) or
     80   1.1  christos    signals with Mach port (the Mach communication channel), signals are
     81   1.1  christos    reported to gdb as an exception.  Furthermore we detect death of the
     82   1.1  christos    inferior through a Mach notification message.  This way we only wait
     83   1.1  christos    on Mach ports.
     84   1.1  christos 
     85   1.1  christos    Some Mach documentation is available for Apple xnu source package or
     86   1.1  christos    from the web.  */
     87   1.1  christos 
     88   1.1  christos 
     89   1.1  christos #define PTRACE(CMD, PID, ADDR, SIG) \
     90   1.1  christos  darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
     91   1.1  christos 
     92   1.1  christos static void darwin_ptrace_me (void);
     93   1.1  christos 
     94   1.3  christos static void darwin_encode_reply (mig_reply_error_t *reply,
     95   1.3  christos 				 mach_msg_header_t *hdr, integer_t code);
     96   1.3  christos 
     97   1.7  christos static void darwin_setup_request_notification (struct inferior *inf);
     98   1.7  christos static void darwin_deallocate_exception_ports (darwin_inferior *inf);
     99   1.7  christos static void darwin_setup_exceptions (struct inferior *inf);
    100   1.7  christos static void darwin_deallocate_threads (struct inferior *inf);
    101   1.7  christos 
    102   1.1  christos /* Task identifier of gdb.  */
    103   1.1  christos static task_t gdb_task;
    104   1.1  christos 
    105   1.1  christos /* A copy of mach_host_self ().  */
    106   1.1  christos mach_port_t darwin_host_self;
    107   1.1  christos 
    108   1.1  christos /* Exception port.  */
    109   1.1  christos mach_port_t darwin_ex_port;
    110   1.1  christos 
    111   1.3  christos /* Port set, to wait for answer on all ports.  */
    112   1.1  christos mach_port_t darwin_port_set;
    113   1.1  christos 
    114   1.1  christos /* Page size.  */
    115   1.1  christos static vm_size_t mach_page_size;
    116   1.1  christos 
    117   1.1  christos /* If Set, catch all mach exceptions (before they are converted to signals
    118   1.1  christos    by the kernel).  */
    119   1.9  christos static bool enable_mach_exceptions;
    120   1.1  christos 
    121   1.1  christos /* Inferior that should report a fake stop event.  */
    122   1.1  christos static struct inferior *darwin_inf_fake_stop;
    123   1.1  christos 
    124   1.8  christos /* If non-NULL, the shell we actually invoke.  See maybe_cache_shell
    125   1.8  christos    for details.  */
    126   1.8  christos static const char *copied_shell;
    127   1.8  christos 
    128   1.1  christos #define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1))
    129   1.1  christos #define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1)
    130   1.1  christos 
    131   1.1  christos /* This controls output of inferior debugging.  */
    132   1.1  christos static unsigned int darwin_debug_flag = 0;
    133   1.1  christos 
    134   1.1  christos /* Create a __TEXT __info_plist section in the executable so that gdb could
    135   1.1  christos    be signed.  This is required to get an authorization for task_for_pid.
    136   1.1  christos 
    137   1.3  christos    Once gdb is built, you must codesign it with any system-trusted signing
    138   1.3  christos    authority.  See taskgated(8) for details.  */
    139   1.1  christos static const unsigned char info_plist[]
    140   1.1  christos __attribute__ ((section ("__TEXT,__info_plist"),used)) =
    141   1.1  christos   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    142   1.1  christos   "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
    143   1.1  christos   " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
    144   1.1  christos   "<plist version=\"1.0\">\n"
    145   1.1  christos   "<dict>\n"
    146   1.1  christos   "  <key>CFBundleIdentifier</key>\n"
    147   1.1  christos   "  <string>org.gnu.gdb</string>\n"
    148   1.1  christos   "  <key>CFBundleName</key>\n"
    149   1.1  christos   "  <string>gdb</string>\n"
    150   1.1  christos   "  <key>CFBundleVersion</key>\n"
    151   1.1  christos   "  <string>1.0</string>\n"
    152   1.1  christos   "  <key>SecTaskAccess</key>\n"
    153   1.1  christos   "  <array>\n"
    154   1.1  christos   "    <string>allowed</string>\n"
    155   1.1  christos   "    <string>debug</string>\n"
    156   1.1  christos   "  </array>\n"
    157   1.1  christos   "</dict>\n"
    158   1.1  christos   "</plist>\n";
    159   1.1  christos 
    160   1.5  christos static void inferior_debug (int level, const char *fmt, ...)
    161   1.5  christos   ATTRIBUTE_PRINTF (2, 3);
    162   1.5  christos 
    163   1.1  christos static void
    164   1.1  christos inferior_debug (int level, const char *fmt, ...)
    165   1.1  christos {
    166   1.1  christos   va_list ap;
    167   1.1  christos 
    168   1.1  christos   if (darwin_debug_flag < level)
    169   1.1  christos     return;
    170   1.1  christos 
    171   1.1  christos   va_start (ap, fmt);
    172  1.10  christos   gdb_printf (gdb_stdlog, _("[%d inferior]: "), getpid ());
    173  1.10  christos   gdb_vprintf (gdb_stdlog, fmt, ap);
    174   1.1  christos   va_end (ap);
    175   1.1  christos }
    176   1.1  christos 
    177   1.1  christos void
    178   1.1  christos mach_check_error (kern_return_t ret, const char *file,
    179  1.10  christos 		  unsigned int line, const char *func)
    180   1.1  christos {
    181   1.1  christos   if (ret == KERN_SUCCESS)
    182   1.1  christos     return;
    183   1.1  christos   if (func == NULL)
    184   1.1  christos     func = _("[UNKNOWN]");
    185   1.1  christos 
    186   1.1  christos   warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"),
    187   1.1  christos 	   file, line, func, mach_error_string (ret), (unsigned long) ret);
    188   1.1  christos }
    189   1.1  christos 
    190   1.1  christos static const char *
    191   1.1  christos unparse_exception_type (unsigned int i)
    192   1.1  christos {
    193   1.1  christos   static char unknown_exception_buf[32];
    194   1.1  christos 
    195   1.1  christos   switch (i)
    196   1.1  christos     {
    197   1.1  christos     case EXC_BAD_ACCESS:
    198   1.1  christos       return "EXC_BAD_ACCESS";
    199   1.1  christos     case EXC_BAD_INSTRUCTION:
    200   1.1  christos       return "EXC_BAD_INSTRUCTION";
    201   1.1  christos     case EXC_ARITHMETIC:
    202   1.1  christos       return "EXC_ARITHMETIC";
    203   1.1  christos     case EXC_EMULATION:
    204   1.1  christos       return "EXC_EMULATION";
    205   1.1  christos     case EXC_SOFTWARE:
    206   1.1  christos       return "EXC_SOFTWARE";
    207   1.1  christos     case EXC_BREAKPOINT:
    208   1.1  christos       return "EXC_BREAKPOINT";
    209   1.1  christos     case EXC_SYSCALL:
    210   1.1  christos       return "EXC_SYSCALL";
    211   1.1  christos     case EXC_MACH_SYSCALL:
    212   1.1  christos       return "EXC_MACH_SYSCALL";
    213   1.1  christos     case EXC_RPC_ALERT:
    214   1.1  christos       return "EXC_RPC_ALERT";
    215   1.1  christos     case EXC_CRASH:
    216   1.1  christos       return "EXC_CRASH";
    217   1.1  christos     default:
    218   1.1  christos       snprintf (unknown_exception_buf, 32, _("unknown (%d)"), i);
    219   1.1  christos       return unknown_exception_buf;
    220   1.1  christos     }
    221   1.1  christos }
    222   1.1  christos 
    223   1.1  christos /* Set errno to zero, and then call ptrace with the given arguments.
    224   1.1  christos    If inferior debugging traces are on, then also print a debug
    225   1.1  christos    trace.
    226   1.1  christos 
    227   1.1  christos    The returned value is the same as the value returned by ptrace,
    228   1.1  christos    except in the case where that value is -1 but errno is zero.
    229   1.1  christos    This case is documented to be a non-error situation, so we
    230   1.1  christos    return zero in that case. */
    231   1.1  christos 
    232   1.1  christos static int
    233   1.1  christos darwin_ptrace (const char *name,
    234   1.6  christos 	       int request, int pid, caddr_t arg3, int arg4)
    235   1.1  christos {
    236   1.1  christos   int ret;
    237   1.1  christos 
    238   1.1  christos   errno = 0;
    239   1.6  christos   ret = ptrace (request, pid, arg3, arg4);
    240   1.1  christos   if (ret == -1 && errno == 0)
    241   1.1  christos     ret = 0;
    242   1.1  christos 
    243   1.6  christos   inferior_debug (4, _("ptrace (%s, %d, 0x%lx, %d): %d (%s)\n"),
    244  1.10  christos 		  name, pid, (unsigned long) arg3, arg4, ret,
    245  1.10  christos 		  (ret != 0) ? safe_strerror (errno) : _("no error"));
    246   1.1  christos   return ret;
    247   1.1  christos }
    248   1.1  christos 
    249   1.1  christos static int
    250   1.1  christos cmp_thread_t (const void *l, const void *r)
    251   1.1  christos {
    252   1.1  christos   thread_t tl = *(const thread_t *)l;
    253   1.1  christos   thread_t tr = *(const thread_t *)r;
    254   1.1  christos   return (int)(tl - tr);
    255   1.1  christos }
    256   1.1  christos 
    257   1.9  christos void
    258   1.9  christos darwin_nat_target::check_new_threads (inferior *inf)
    259   1.1  christos {
    260   1.1  christos   kern_return_t kret;
    261   1.1  christos   thread_array_t thread_list;
    262   1.1  christos   unsigned int new_nbr;
    263   1.1  christos   unsigned int old_nbr;
    264   1.1  christos   unsigned int new_ix, old_ix;
    265   1.8  christos   darwin_inferior *darwin_inf = get_darwin_inferior (inf);
    266   1.8  christos   std::vector<darwin_thread_t *> new_thread_vec;
    267   1.8  christos 
    268   1.8  christos   if (darwin_inf == nullptr)
    269   1.8  christos     return;
    270   1.1  christos 
    271   1.1  christos   /* Get list of threads.  */
    272   1.1  christos   kret = task_threads (darwin_inf->task, &thread_list, &new_nbr);
    273   1.1  christos   MACH_CHECK_ERROR (kret);
    274   1.1  christos   if (kret != KERN_SUCCESS)
    275   1.1  christos     return;
    276   1.1  christos 
    277   1.1  christos   /* Sort the list.  */
    278   1.1  christos   if (new_nbr > 1)
    279   1.1  christos     qsort (thread_list, new_nbr, sizeof (thread_t), cmp_thread_t);
    280   1.1  christos 
    281   1.8  christos   old_nbr = darwin_inf->threads.size ();
    282   1.1  christos 
    283   1.1  christos   /* Quick check for no changes.  */
    284   1.1  christos   if (old_nbr == new_nbr)
    285   1.1  christos     {
    286   1.8  christos       size_t i;
    287   1.8  christos 
    288   1.1  christos       for (i = 0; i < new_nbr; i++)
    289   1.8  christos 	if (thread_list[i] != darwin_inf->threads[i]->gdb_port)
    290   1.1  christos 	  break;
    291   1.1  christos       if (i == new_nbr)
    292   1.1  christos 	{
    293   1.3  christos 	  /* Deallocate ports.  */
    294   1.3  christos 	  for (i = 0; i < new_nbr; i++)
    295   1.3  christos 	    {
    296   1.3  christos 	      kret = mach_port_deallocate (mach_task_self (), thread_list[i]);
    297   1.3  christos 	      MACH_CHECK_ERROR (kret);
    298   1.3  christos 	    }
    299   1.3  christos 
    300   1.3  christos 	  /* Deallocate the buffer.  */
    301   1.1  christos 	  kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
    302   1.1  christos 				new_nbr * sizeof (int));
    303   1.1  christos 	  MACH_CHECK_ERROR (kret);
    304   1.3  christos 
    305   1.1  christos 	  return;
    306   1.1  christos 	}
    307   1.1  christos     }
    308   1.1  christos 
    309   1.7  christos   /* Full handling: detect new threads, remove dead threads.  */
    310   1.8  christos 
    311   1.8  christos   new_thread_vec.reserve (new_nbr);
    312   1.1  christos 
    313   1.1  christos   for (new_ix = 0, old_ix = 0; new_ix < new_nbr || old_ix < old_nbr;)
    314   1.1  christos     {
    315   1.8  christos       thread_t new_id = (new_ix < new_nbr) ? thread_list[new_ix] : THREAD_NULL;
    316   1.8  christos       darwin_thread_t *old
    317   1.8  christos 	= (old_ix < old_nbr) ? darwin_inf->threads[old_ix] : NULL;
    318   1.8  christos       thread_t old_id = old != NULL ? old->gdb_port : THREAD_NULL;
    319   1.1  christos 
    320   1.1  christos       inferior_debug
    321   1.1  christos 	(12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:0x%x old_id:0x%x\n"),
    322   1.1  christos 	 new_ix, new_nbr, old_ix, old_nbr, new_id, old_id);
    323   1.1  christos 
    324   1.1  christos       if (old_id == new_id)
    325   1.1  christos 	{
    326   1.1  christos 	  /* Thread still exist.  */
    327   1.8  christos 	  new_thread_vec.push_back (old);
    328   1.1  christos 	  new_ix++;
    329   1.1  christos 	  old_ix++;
    330   1.1  christos 
    331   1.3  christos 	  /* Deallocate the port.  */
    332   1.3  christos 	  kret = mach_port_deallocate (gdb_task, new_id);
    333   1.1  christos 	  MACH_CHECK_ERROR (kret);
    334   1.3  christos 
    335   1.1  christos 	  continue;
    336   1.1  christos 	}
    337   1.1  christos       if (new_ix < new_nbr && new_id == MACH_PORT_DEAD)
    338   1.1  christos 	{
    339   1.1  christos 	  /* Ignore dead ports.
    340   1.1  christos 	     In some weird cases, we might get dead ports.  They should
    341   1.1  christos 	     correspond to dead thread so they could safely be ignored.  */
    342   1.1  christos 	  new_ix++;
    343   1.1  christos 	  continue;
    344   1.1  christos 	}
    345   1.1  christos       if (new_ix < new_nbr && (old_ix == old_nbr || new_id < old_id))
    346   1.1  christos 	{
    347   1.1  christos 	  /* A thread was created.  */
    348   1.8  christos 	  darwin_thread_info *pti = new darwin_thread_info;
    349   1.1  christos 
    350   1.1  christos 	  pti->gdb_port = new_id;
    351   1.1  christos 	  pti->msg_state = DARWIN_RUNNING;
    352   1.1  christos 
    353   1.8  christos 	  /* Add the new thread.  */
    354   1.9  christos 	  add_thread_with_info (this, ptid_t (inf->pid, 0, new_id), pti);
    355   1.8  christos 	  new_thread_vec.push_back (pti);
    356   1.1  christos 	  new_ix++;
    357   1.1  christos 	  continue;
    358   1.1  christos 	}
    359   1.1  christos       if (old_ix < old_nbr && (new_ix == new_nbr || new_id > old_id))
    360   1.1  christos 	{
    361   1.1  christos 	  /* A thread was removed.  */
    362   1.8  christos 	  struct thread_info *thr
    363   1.9  christos 	    = find_thread_ptid (this, ptid_t (inf->pid, 0, old_id));
    364   1.8  christos 	  delete_thread (thr);
    365   1.1  christos 	  kret = mach_port_deallocate (gdb_task, old_id);
    366   1.1  christos 	  MACH_CHECK_ERROR (kret);
    367   1.1  christos 	  old_ix++;
    368   1.1  christos 	  continue;
    369   1.1  christos 	}
    370   1.1  christos       gdb_assert_not_reached ("unexpected thread case");
    371   1.1  christos     }
    372   1.1  christos 
    373   1.8  christos   darwin_inf->threads = std::move (new_thread_vec);
    374   1.1  christos 
    375   1.3  christos   /* Deallocate the buffer.  */
    376   1.1  christos   kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
    377   1.1  christos 			new_nbr * sizeof (int));
    378   1.1  christos   MACH_CHECK_ERROR (kret);
    379   1.1  christos }
    380   1.1  christos 
    381   1.1  christos /* Return an inferior by task port.  */
    382   1.1  christos static struct inferior *
    383   1.1  christos darwin_find_inferior_by_task (task_t port)
    384   1.1  christos {
    385   1.9  christos   for (inferior *inf : all_inferiors ())
    386   1.9  christos     {
    387   1.9  christos       darwin_inferior *priv = get_darwin_inferior (inf);
    388   1.9  christos 
    389   1.9  christos       if (priv != nullptr && priv->task == port)
    390   1.9  christos 	return inf;
    391   1.9  christos     }
    392   1.9  christos   return nullptr;
    393   1.1  christos }
    394   1.1  christos 
    395   1.7  christos /* Return an inferior by pid port.  */
    396   1.1  christos static struct inferior *
    397   1.7  christos darwin_find_inferior_by_pid (int pid)
    398   1.1  christos {
    399   1.9  christos   for (inferior *inf : all_inferiors ())
    400   1.9  christos     {
    401   1.9  christos       if (inf->pid == pid)
    402   1.9  christos 	return inf;
    403   1.9  christos     }
    404   1.9  christos   return nullptr;
    405   1.1  christos }
    406   1.1  christos 
    407   1.1  christos /* Return a thread by port.  */
    408   1.1  christos static darwin_thread_t *
    409   1.1  christos darwin_find_thread (struct inferior *inf, thread_t thread)
    410   1.1  christos {
    411   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    412   1.8  christos 
    413   1.8  christos   if (priv != nullptr)
    414   1.8  christos     for (darwin_thread_t *t : priv->threads)
    415   1.8  christos       {
    416   1.8  christos 	if (t->gdb_port == thread)
    417   1.8  christos 	  return t;
    418   1.8  christos       }
    419   1.1  christos 
    420   1.1  christos   return NULL;
    421   1.1  christos }
    422   1.1  christos 
    423   1.1  christos /* Suspend (ie stop) an inferior at Mach level.  */
    424   1.1  christos 
    425   1.1  christos static void
    426   1.1  christos darwin_suspend_inferior (struct inferior *inf)
    427   1.1  christos {
    428   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    429   1.8  christos 
    430   1.8  christos   if (priv != nullptr && !priv->suspended)
    431   1.1  christos     {
    432   1.1  christos       kern_return_t kret;
    433   1.1  christos 
    434   1.8  christos       kret = task_suspend (priv->task);
    435   1.1  christos       MACH_CHECK_ERROR (kret);
    436   1.1  christos 
    437   1.8  christos       priv->suspended = 1;
    438   1.1  christos     }
    439   1.1  christos }
    440   1.1  christos 
    441   1.1  christos /* Resume an inferior at Mach level.  */
    442   1.1  christos 
    443   1.1  christos static void
    444   1.1  christos darwin_resume_inferior (struct inferior *inf)
    445   1.1  christos {
    446   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    447   1.8  christos 
    448   1.8  christos   if (priv != nullptr && priv->suspended)
    449   1.1  christos     {
    450   1.1  christos       kern_return_t kret;
    451   1.1  christos 
    452   1.8  christos       kret = task_resume (priv->task);
    453   1.1  christos       MACH_CHECK_ERROR (kret);
    454   1.1  christos 
    455   1.8  christos       priv->suspended = 0;
    456   1.1  christos     }
    457   1.1  christos }
    458   1.1  christos 
    459   1.1  christos static void
    460   1.1  christos darwin_dump_message (mach_msg_header_t *hdr, int disp_body)
    461   1.1  christos {
    462  1.10  christos   gdb_printf (gdb_stdlog,
    463  1.10  christos 	      _("message header:\n"));
    464  1.10  christos   gdb_printf (gdb_stdlog,
    465  1.10  christos 	      _(" bits: 0x%x\n"), hdr->msgh_bits);
    466  1.10  christos   gdb_printf (gdb_stdlog,
    467  1.10  christos 	      _(" size: 0x%x\n"), hdr->msgh_size);
    468  1.10  christos   gdb_printf (gdb_stdlog,
    469  1.10  christos 	      _(" remote-port: 0x%x\n"), hdr->msgh_remote_port);
    470  1.10  christos   gdb_printf (gdb_stdlog,
    471  1.10  christos 	      _(" local-port: 0x%x\n"), hdr->msgh_local_port);
    472  1.10  christos   gdb_printf (gdb_stdlog,
    473  1.10  christos 	      _(" reserved: 0x%x\n"), hdr->msgh_reserved);
    474  1.10  christos   gdb_printf (gdb_stdlog,
    475  1.10  christos 	      _(" id: 0x%x\n"), hdr->msgh_id);
    476   1.1  christos 
    477   1.1  christos   if (disp_body)
    478   1.1  christos     {
    479   1.1  christos       const unsigned char *data;
    480   1.3  christos       const unsigned int *ldata;
    481   1.1  christos       int size;
    482   1.1  christos       int i;
    483   1.1  christos 
    484   1.1  christos       data = (unsigned char *)(hdr + 1);
    485   1.1  christos       size = hdr->msgh_size - sizeof (mach_msg_header_t);
    486   1.1  christos 
    487   1.1  christos       if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
    488   1.1  christos 	{
    489   1.1  christos 	  mach_msg_body_t *bod = (mach_msg_body_t*)data;
    490   1.1  christos 	  mach_msg_port_descriptor_t *desc =
    491   1.1  christos 	    (mach_msg_port_descriptor_t *)(bod + 1);
    492   1.1  christos 	  int k;
    493   1.1  christos 	  NDR_record_t *ndr;
    494  1.10  christos 	  gdb_printf (gdb_stdlog,
    495  1.10  christos 		      _("body: descriptor_count=%u\n"),
    496  1.10  christos 		      bod->msgh_descriptor_count);
    497   1.1  christos 	  data += sizeof (mach_msg_body_t);
    498   1.1  christos 	  size -= sizeof (mach_msg_body_t);
    499   1.1  christos 	  for (k = 0; k < bod->msgh_descriptor_count; k++)
    500   1.1  christos 	    switch (desc[k].type)
    501   1.1  christos 	      {
    502   1.1  christos 	      case MACH_MSG_PORT_DESCRIPTOR:
    503  1.10  christos 		gdb_printf
    504  1.10  christos 		  (gdb_stdlog,
    505  1.10  christos 		   _(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
    506   1.1  christos 		   k, desc[k].type, desc[k].name, desc[k].disposition);
    507   1.1  christos 		break;
    508   1.1  christos 	      default:
    509  1.10  christos 		gdb_printf (gdb_stdlog,
    510  1.10  christos 			    _(" descr %d: type=%u\n"),
    511  1.10  christos 			    k, desc[k].type);
    512   1.1  christos 		break;
    513   1.1  christos 	      }
    514   1.1  christos 	  data += bod->msgh_descriptor_count
    515   1.1  christos 	    * sizeof (mach_msg_port_descriptor_t);
    516   1.1  christos 	  size -= bod->msgh_descriptor_count
    517   1.1  christos 	    * sizeof (mach_msg_port_descriptor_t);
    518   1.1  christos 	  ndr = (NDR_record_t *)(desc + bod->msgh_descriptor_count);
    519  1.10  christos 	  gdb_printf
    520  1.10  christos 	    (gdb_stdlog,
    521  1.10  christos 	     _("NDR: mig=%02x if=%02x encod=%02x "
    522   1.1  christos 	       "int=%02x char=%02x float=%02x\n"),
    523   1.1  christos 	     ndr->mig_vers, ndr->if_vers, ndr->mig_encoding,
    524   1.1  christos 	     ndr->int_rep, ndr->char_rep, ndr->float_rep);
    525   1.1  christos 	  data += sizeof (NDR_record_t);
    526   1.1  christos 	  size -= sizeof (NDR_record_t);
    527   1.1  christos 	}
    528   1.1  christos 
    529  1.10  christos       gdb_printf (gdb_stdlog, _("  data:"));
    530   1.3  christos       ldata = (const unsigned int *)data;
    531   1.3  christos       for (i = 0; i < size / sizeof (unsigned int); i++)
    532  1.10  christos 	gdb_printf (gdb_stdlog, " %08x", ldata[i]);
    533  1.10  christos       gdb_printf (gdb_stdlog, _("\n"));
    534   1.1  christos     }
    535   1.1  christos }
    536   1.1  christos 
    537   1.7  christos /* Adjust inferior data when a new task was created.  */
    538   1.7  christos 
    539   1.7  christos static struct inferior *
    540   1.7  christos darwin_find_new_inferior (task_t task_port, thread_t thread_port)
    541   1.7  christos {
    542   1.7  christos   int task_pid;
    543   1.7  christos   struct inferior *inf;
    544   1.7  christos   kern_return_t kret;
    545   1.7  christos   mach_port_t prev;
    546   1.7  christos 
    547   1.7  christos   /* Find the corresponding pid.  */
    548   1.7  christos   kret = pid_for_task (task_port, &task_pid);
    549   1.7  christos   if (kret != KERN_SUCCESS)
    550   1.7  christos     {
    551   1.7  christos       MACH_CHECK_ERROR (kret);
    552   1.7  christos       return NULL;
    553   1.7  christos     }
    554   1.7  christos 
    555   1.7  christos   /* Find the inferior for this pid.  */
    556   1.7  christos   inf = darwin_find_inferior_by_pid (task_pid);
    557   1.7  christos   if (inf == NULL)
    558   1.7  christos     return NULL;
    559   1.7  christos 
    560   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    561   1.8  christos 
    562   1.7  christos   /* Deallocate saved exception ports.  */
    563   1.8  christos   darwin_deallocate_exception_ports (priv);
    564   1.7  christos 
    565   1.7  christos   /* No need to remove dead_name notification, but still...  */
    566   1.8  christos   kret = mach_port_request_notification (gdb_task, priv->task,
    567   1.7  christos 					 MACH_NOTIFY_DEAD_NAME, 0,
    568   1.7  christos 					 MACH_PORT_NULL,
    569   1.7  christos 					 MACH_MSG_TYPE_MAKE_SEND_ONCE,
    570   1.7  christos 					 &prev);
    571   1.7  christos   if (kret != KERN_INVALID_ARGUMENT)
    572   1.7  christos     MACH_CHECK_ERROR (kret);
    573   1.7  christos 
    574   1.7  christos   /* Replace old task port.  */
    575   1.8  christos   kret = mach_port_deallocate (gdb_task, priv->task);
    576   1.7  christos   MACH_CHECK_ERROR (kret);
    577   1.8  christos   priv->task = task_port;
    578   1.7  christos 
    579   1.7  christos   darwin_setup_request_notification (inf);
    580   1.7  christos   darwin_setup_exceptions (inf);
    581   1.7  christos 
    582   1.7  christos   return inf;
    583   1.7  christos }
    584   1.7  christos 
    585   1.7  christos /* Check data representation.  */
    586   1.7  christos 
    587   1.7  christos static int
    588   1.7  christos darwin_check_message_ndr (NDR_record_t *ndr)
    589   1.7  christos {
    590   1.7  christos   if (ndr->mig_vers != NDR_PROTOCOL_2_0
    591   1.7  christos       || ndr->if_vers != NDR_PROTOCOL_2_0
    592   1.7  christos       || ndr->mig_encoding != NDR_record.mig_encoding
    593   1.7  christos       || ndr->int_rep != NDR_record.int_rep
    594   1.7  christos       || ndr->char_rep != NDR_record.char_rep
    595   1.7  christos       || ndr->float_rep != NDR_record.float_rep)
    596   1.7  christos     return -1;
    597   1.7  christos   return 0;
    598   1.7  christos }
    599   1.7  christos 
    600   1.7  christos /* Decode an exception message.  */
    601   1.7  christos 
    602   1.9  christos int
    603   1.9  christos darwin_nat_target::decode_exception_message (mach_msg_header_t *hdr,
    604   1.9  christos 					     inferior **pinf,
    605   1.9  christos 					     darwin_thread_t **pthread)
    606   1.1  christos {
    607   1.1  christos   mach_msg_body_t *bod = (mach_msg_body_t*)(hdr + 1);
    608   1.1  christos   mach_msg_port_descriptor_t *desc = (mach_msg_port_descriptor_t *)(bod + 1);
    609   1.1  christos   NDR_record_t *ndr;
    610   1.1  christos   integer_t *data;
    611   1.1  christos   struct inferior *inf;
    612   1.1  christos   darwin_thread_t *thread;
    613   1.1  christos   task_t task_port;
    614   1.1  christos   thread_t thread_port;
    615   1.1  christos   kern_return_t kret;
    616   1.1  christos   int i;
    617   1.1  christos 
    618   1.3  christos   /* Check message destination.  */
    619   1.3  christos   if (hdr->msgh_local_port != darwin_ex_port)
    620   1.1  christos     return -1;
    621   1.1  christos 
    622   1.1  christos   /* Check message header.  */
    623   1.1  christos   if (!(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX))
    624   1.1  christos     return -1;
    625   1.1  christos 
    626   1.1  christos   /* Check descriptors.  */
    627   1.1  christos   if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
    628   1.1  christos 			+ sizeof (*ndr) + 2 * sizeof (integer_t))
    629   1.1  christos       || bod->msgh_descriptor_count != 2
    630   1.1  christos       || desc[0].type != MACH_MSG_PORT_DESCRIPTOR
    631   1.1  christos       || desc[0].disposition != MACH_MSG_TYPE_MOVE_SEND
    632   1.1  christos       || desc[1].type != MACH_MSG_PORT_DESCRIPTOR
    633   1.1  christos       || desc[1].disposition != MACH_MSG_TYPE_MOVE_SEND)
    634   1.1  christos     return -1;
    635   1.1  christos 
    636   1.1  christos   /* Check data representation.  */
    637   1.1  christos   ndr = (NDR_record_t *)(desc + 2);
    638   1.7  christos   if (darwin_check_message_ndr (ndr) != 0)
    639   1.1  christos     return -1;
    640   1.1  christos 
    641   1.1  christos   /* Ok, the hard work.  */
    642   1.1  christos   data = (integer_t *)(ndr + 1);
    643   1.1  christos 
    644   1.1  christos   task_port = desc[1].name;
    645   1.1  christos   thread_port = desc[0].name;
    646   1.3  christos 
    647   1.3  christos   /* Find process by port.  */
    648   1.1  christos   inf = darwin_find_inferior_by_task (task_port);
    649   1.3  christos   *pinf = inf;
    650   1.7  christos 
    651   1.7  christos   if (inf == NULL && data[0] == EXC_SOFTWARE && data[1] == 2
    652   1.7  christos       && data[2] == EXC_SOFT_SIGNAL && data[3] == SIGTRAP)
    653   1.7  christos     {
    654   1.7  christos       /* Not a known inferior, but a sigtrap.  This happens on darwin 16.1.0,
    655   1.7  christos 	 as a new Mach task is created when a process exec.  */
    656   1.7  christos       inf = darwin_find_new_inferior (task_port, thread_port);
    657   1.7  christos       *pinf = inf;
    658   1.7  christos 
    659   1.7  christos       if (inf == NULL)
    660   1.7  christos 	{
    661   1.7  christos 	  /* Deallocate task_port, unless it was saved.  */
    662   1.7  christos 	  kret = mach_port_deallocate (mach_task_self (), task_port);
    663   1.7  christos 	  MACH_CHECK_ERROR (kret);
    664   1.7  christos 	}
    665   1.7  christos     }
    666   1.7  christos   else
    667   1.7  christos     {
    668   1.7  christos       /* We got new rights to the task, get rid of it.  Do not get rid of
    669   1.7  christos 	 thread right, as we will need it to find the thread.  */
    670   1.7  christos       kret = mach_port_deallocate (mach_task_self (), task_port);
    671   1.7  christos       MACH_CHECK_ERROR (kret);
    672   1.7  christos     }
    673   1.7  christos 
    674   1.1  christos   if (inf == NULL)
    675   1.3  christos     {
    676   1.3  christos       /* Not a known inferior.  This could happen if the child fork, as
    677   1.3  christos 	 the created process will inherit its exception port.
    678   1.3  christos 	 FIXME: should the exception port be restored ?  */
    679   1.3  christos       mig_reply_error_t reply;
    680   1.3  christos 
    681   1.7  christos       inferior_debug
    682   1.7  christos 	(4, _("darwin_decode_exception_message: unknown task 0x%x\n"),
    683   1.7  christos 	 task_port);
    684   1.7  christos 
    685   1.3  christos       /* Free thread port (we don't know it).  */
    686   1.3  christos       kret = mach_port_deallocate (mach_task_self (), thread_port);
    687   1.3  christos       MACH_CHECK_ERROR (kret);
    688   1.3  christos 
    689   1.3  christos       darwin_encode_reply (&reply, hdr, KERN_SUCCESS);
    690   1.3  christos 
    691   1.3  christos       kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
    692   1.3  christos 		       reply.Head.msgh_size, 0,
    693   1.3  christos 		       MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
    694   1.3  christos 		       MACH_PORT_NULL);
    695   1.3  christos       MACH_CHECK_ERROR (kret);
    696   1.3  christos 
    697   1.3  christos       return 0;
    698   1.3  christos     }
    699   1.1  christos 
    700   1.1  christos   /* Find thread by port.  */
    701   1.1  christos   /* Check for new threads.  Do it early so that the port in the exception
    702   1.1  christos      message can be deallocated.  */
    703   1.9  christos   check_new_threads (inf);
    704   1.1  christos 
    705   1.3  christos   /* Free the thread port (as gdb knows the thread, it has already has a right
    706   1.3  christos      for it, so this just decrement a reference counter).  */
    707   1.1  christos   kret = mach_port_deallocate (mach_task_self (), thread_port);
    708   1.1  christos   MACH_CHECK_ERROR (kret);
    709   1.1  christos 
    710   1.1  christos   thread = darwin_find_thread (inf, thread_port);
    711   1.1  christos   if (thread == NULL)
    712   1.1  christos     return -1;
    713   1.1  christos   *pthread = thread;
    714   1.1  christos 
    715   1.3  christos   /* The thread should be running.  However we have observed cases where a
    716   1.3  christos      thread got a SIGTTIN message after being stopped.  */
    717   1.1  christos   gdb_assert (thread->msg_state != DARWIN_MESSAGE);
    718   1.1  christos 
    719   1.1  christos   /* Finish decoding.  */
    720   1.1  christos   thread->event.header = *hdr;
    721   1.1  christos   thread->event.thread_port = thread_port;
    722   1.1  christos   thread->event.task_port = task_port;
    723   1.1  christos   thread->event.ex_type = data[0];
    724   1.1  christos   thread->event.data_count = data[1];
    725   1.1  christos 
    726   1.1  christos   if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
    727   1.1  christos 			+ sizeof (*ndr) + 2 * sizeof (integer_t)
    728   1.1  christos 			+ data[1] * sizeof (integer_t)))
    729   1.1  christos       return -1;
    730   1.1  christos   for (i = 0; i < data[1]; i++)
    731   1.1  christos     thread->event.ex_data[i] = data[2 + i];
    732   1.1  christos 
    733   1.1  christos   thread->msg_state = DARWIN_MESSAGE;
    734   1.1  christos 
    735   1.1  christos   return 0;
    736   1.1  christos }
    737   1.1  christos 
    738   1.7  christos /* Decode dead_name notify message.  */
    739   1.7  christos 
    740   1.7  christos static int
    741   1.7  christos darwin_decode_notify_message (mach_msg_header_t *hdr, struct inferior **pinf)
    742   1.7  christos {
    743   1.7  christos   NDR_record_t *ndr = (NDR_record_t *)(hdr + 1);
    744   1.7  christos   integer_t *data = (integer_t *)(ndr + 1);
    745   1.7  christos   struct inferior *inf;
    746   1.7  christos   task_t task_port;
    747   1.7  christos 
    748   1.7  christos   /* Check message header.  */
    749   1.7  christos   if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
    750   1.7  christos     return -1;
    751   1.7  christos 
    752   1.7  christos   /* Check descriptors.  */
    753   1.7  christos   if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*ndr) + sizeof (integer_t)))
    754   1.7  christos     return -2;
    755   1.7  christos 
    756   1.7  christos   /* Check data representation.  */
    757   1.7  christos   if (darwin_check_message_ndr (ndr) != 0)
    758   1.7  christos     return -3;
    759   1.7  christos 
    760   1.7  christos   task_port = data[0];
    761   1.7  christos 
    762   1.7  christos   /* Find process by port.  */
    763   1.7  christos   inf = darwin_find_inferior_by_task (task_port);
    764   1.7  christos   *pinf = inf;
    765   1.7  christos 
    766   1.7  christos   /* Check message destination.  */
    767   1.8  christos   if (inf != NULL)
    768   1.8  christos     {
    769   1.8  christos       darwin_inferior *priv = get_darwin_inferior (inf);
    770   1.8  christos       if (hdr->msgh_local_port != priv->notify_port)
    771   1.8  christos 	return -4;
    772   1.8  christos     }
    773   1.7  christos 
    774   1.7  christos   return 0;
    775   1.7  christos }
    776   1.7  christos 
    777   1.1  christos static void
    778   1.1  christos darwin_encode_reply (mig_reply_error_t *reply, mach_msg_header_t *hdr,
    779   1.1  christos 		     integer_t code)
    780   1.1  christos {
    781   1.1  christos   mach_msg_header_t *rh = &reply->Head;
    782   1.3  christos 
    783   1.3  christos   rh->msgh_bits = MACH_MSGH_BITS (MACH_MSGH_BITS_REMOTE (hdr->msgh_bits), 0);
    784   1.1  christos   rh->msgh_remote_port = hdr->msgh_remote_port;
    785   1.3  christos   rh->msgh_size = (mach_msg_size_t) sizeof (mig_reply_error_t);
    786   1.1  christos   rh->msgh_local_port = MACH_PORT_NULL;
    787   1.1  christos   rh->msgh_id = hdr->msgh_id + 100;
    788   1.1  christos 
    789   1.1  christos   reply->NDR = NDR_record;
    790   1.1  christos   reply->RetCode = code;
    791   1.1  christos }
    792   1.1  christos 
    793   1.1  christos static void
    794   1.1  christos darwin_send_reply (struct inferior *inf, darwin_thread_t *thread)
    795   1.1  christos {
    796   1.1  christos   kern_return_t kret;
    797   1.1  christos   mig_reply_error_t reply;
    798   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    799   1.1  christos 
    800   1.1  christos   darwin_encode_reply (&reply, &thread->event.header, KERN_SUCCESS);
    801   1.1  christos 
    802   1.1  christos   kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
    803   1.1  christos 		   reply.Head.msgh_size, 0,
    804   1.1  christos 		   MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
    805   1.1  christos 		   MACH_PORT_NULL);
    806   1.1  christos   MACH_CHECK_ERROR (kret);
    807   1.1  christos 
    808   1.8  christos   priv->pending_messages--;
    809   1.8  christos }
    810   1.8  christos 
    811   1.8  christos /* Wrapper around the __pthread_kill syscall.  We use this instead of the
    812   1.8  christos    pthread_kill function to be able to send a signal to any kind of thread,
    813   1.8  christos    including GCD threads.  */
    814   1.8  christos 
    815   1.8  christos static int
    816   1.8  christos darwin_pthread_kill (darwin_thread_t *thread, int nsignal)
    817   1.8  christos {
    818   1.8  christos   DIAGNOSTIC_PUSH;
    819   1.8  christos   DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS;
    820   1.8  christos   int res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal);
    821   1.8  christos   DIAGNOSTIC_POP;
    822   1.8  christos   return res;
    823   1.1  christos }
    824   1.1  christos 
    825   1.1  christos static void
    826   1.1  christos darwin_resume_thread (struct inferior *inf, darwin_thread_t *thread,
    827   1.1  christos 		      int step, int nsignal)
    828   1.1  christos {
    829   1.1  christos   inferior_debug
    830   1.1  christos     (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
    831   1.1  christos      thread->msg_state, thread->gdb_port, step, nsignal);
    832   1.1  christos 
    833   1.1  christos   switch (thread->msg_state)
    834   1.1  christos     {
    835   1.1  christos     case DARWIN_MESSAGE:
    836   1.1  christos       if (thread->event.ex_type == EXC_SOFTWARE
    837   1.1  christos 	  && thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
    838   1.1  christos 	{
    839   1.1  christos 	  /* Either deliver a new signal or cancel the signal received.  */
    840   1.8  christos 	  int res = PTRACE (PT_THUPDATE, inf->pid,
    841   1.8  christos 			    (caddr_t) (uintptr_t) thread->gdb_port, nsignal);
    842   1.1  christos 	  if (res < 0)
    843   1.1  christos 	    inferior_debug (1, _("ptrace THUP: res=%d\n"), res);
    844   1.1  christos 	}
    845   1.1  christos       else if (nsignal)
    846   1.1  christos 	{
    847   1.1  christos 	  /* Note: ptrace is allowed only if the process is stopped.
    848   1.1  christos 	     Directly send the signal to the thread.  */
    849   1.8  christos 	  int res = darwin_pthread_kill (thread, nsignal);
    850   1.1  christos 	  inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
    851   1.1  christos 			  thread->gdb_port, nsignal, res);
    852   1.1  christos 	  thread->signaled = 1;
    853   1.1  christos 	}
    854   1.1  christos 
    855   1.1  christos       /* Set or reset single step.  */
    856   1.6  christos       inferior_debug (4, _("darwin_set_sstep (thread=0x%x, enable=%d)\n"),
    857   1.6  christos 		      thread->gdb_port, step);
    858   1.6  christos       darwin_set_sstep (thread->gdb_port, step);
    859   1.6  christos       thread->single_step = step;
    860   1.1  christos 
    861   1.1  christos       darwin_send_reply (inf, thread);
    862   1.1  christos       thread->msg_state = DARWIN_RUNNING;
    863   1.1  christos       break;
    864   1.1  christos 
    865   1.1  christos     case DARWIN_RUNNING:
    866   1.1  christos       break;
    867   1.1  christos 
    868   1.1  christos     case DARWIN_STOPPED:
    869   1.8  christos       kern_return_t kret = thread_resume (thread->gdb_port);
    870   1.1  christos       MACH_CHECK_ERROR (kret);
    871   1.1  christos 
    872   1.1  christos       thread->msg_state = DARWIN_RUNNING;
    873   1.1  christos       break;
    874   1.1  christos     }
    875   1.1  christos }
    876   1.1  christos 
    877   1.1  christos /* Resume all threads of the inferior.  */
    878   1.1  christos 
    879   1.1  christos static void
    880   1.1  christos darwin_resume_inferior_threads (struct inferior *inf, int step, int nsignal)
    881   1.1  christos {
    882   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    883   1.1  christos 
    884   1.8  christos   if (priv != nullptr)
    885   1.8  christos     for (darwin_thread_t *thread : priv->threads)
    886   1.8  christos       darwin_resume_thread (inf, thread, step, nsignal);
    887   1.1  christos }
    888   1.1  christos 
    889   1.1  christos /* Suspend all threads of INF.  */
    890   1.1  christos 
    891   1.1  christos static void
    892   1.1  christos darwin_suspend_inferior_threads (struct inferior *inf)
    893   1.1  christos {
    894   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
    895   1.1  christos 
    896   1.8  christos   for (darwin_thread_t *thread : priv->threads)
    897   1.8  christos     {
    898   1.8  christos       switch (thread->msg_state)
    899   1.8  christos 	{
    900   1.8  christos 	case DARWIN_STOPPED:
    901   1.8  christos 	case DARWIN_MESSAGE:
    902   1.8  christos 	  break;
    903   1.8  christos 	case DARWIN_RUNNING:
    904   1.8  christos 	  {
    905   1.8  christos 	    kern_return_t kret = thread_suspend (thread->gdb_port);
    906   1.8  christos 	    MACH_CHECK_ERROR (kret);
    907   1.8  christos 	    thread->msg_state = DARWIN_STOPPED;
    908   1.8  christos 	    break;
    909   1.8  christos 	  }
    910   1.8  christos 	}
    911   1.8  christos     }
    912   1.1  christos }
    913   1.1  christos 
    914   1.8  christos void
    915   1.8  christos darwin_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
    916   1.1  christos {
    917   1.1  christos   int nsignal;
    918   1.1  christos 
    919   1.1  christos   inferior_debug
    920  1.10  christos     (2, _("darwin_resume: ptid=%s, step=%d, signal=%d\n"),
    921  1.10  christos      ptid.to_string ().c_str (), step, signal);
    922   1.1  christos 
    923   1.1  christos   if (signal == GDB_SIGNAL_0)
    924   1.1  christos     nsignal = 0;
    925   1.1  christos   else
    926   1.1  christos     nsignal = gdb_signal_to_host (signal);
    927   1.1  christos 
    928   1.1  christos   /* Don't try to single step all threads.  */
    929   1.1  christos   if (step)
    930   1.1  christos     ptid = inferior_ptid;
    931   1.1  christos 
    932   1.1  christos   /* minus_one_ptid is RESUME_ALL.  */
    933   1.8  christos   if (ptid == minus_one_ptid)
    934   1.1  christos     {
    935   1.9  christos       /* Resume threads.  */
    936   1.9  christos       for (inferior *inf : all_inferiors ())
    937   1.9  christos 	darwin_resume_inferior_threads (inf, step, nsignal);
    938   1.1  christos 
    939   1.1  christos       /* Resume tasks.  */
    940   1.9  christos       for (inferior *inf : all_inferiors ())
    941   1.9  christos 	darwin_resume_inferior (inf);
    942   1.1  christos     }
    943   1.1  christos   else
    944   1.1  christos     {
    945   1.9  christos       inferior *inf = find_inferior_ptid (this, ptid);
    946   1.8  christos       long tid = ptid.tid ();
    947   1.1  christos 
    948   1.1  christos       /* Stop the inferior (should be useless).  */
    949   1.1  christos       darwin_suspend_inferior (inf);
    950   1.1  christos 
    951   1.1  christos       if (tid == 0)
    952  1.10  christos 	darwin_resume_inferior_threads (inf, step, nsignal);
    953   1.1  christos       else
    954  1.10  christos 	{
    955  1.10  christos 	  darwin_thread_t *thread;
    956   1.1  christos 
    957  1.10  christos 	  /* Suspend threads of the task.  */
    958  1.10  christos 	  darwin_suspend_inferior_threads (inf);
    959   1.1  christos 
    960  1.10  christos 	  /* Resume the selected thread.  */
    961  1.10  christos 	  thread = darwin_find_thread (inf, tid);
    962  1.10  christos 	  gdb_assert (thread);
    963  1.10  christos 	  darwin_resume_thread (inf, thread, step, nsignal);
    964  1.10  christos 	}
    965   1.1  christos 
    966   1.1  christos       /* Resume the task.  */
    967   1.1  christos       darwin_resume_inferior (inf);
    968   1.1  christos     }
    969   1.1  christos }
    970   1.1  christos 
    971   1.9  christos ptid_t
    972   1.9  christos darwin_nat_target::decode_message (mach_msg_header_t *hdr,
    973   1.9  christos 				   darwin_thread_t **pthread,
    974   1.9  christos 				   inferior **pinf,
    975   1.9  christos 				   target_waitstatus *status)
    976   1.1  christos {
    977   1.1  christos   darwin_thread_t *thread;
    978   1.1  christos   struct inferior *inf;
    979   1.1  christos 
    980   1.3  christos   /* Exception message.  2401 == 0x961 is exc.  */
    981   1.3  christos   if (hdr->msgh_id == 2401)
    982   1.1  christos     {
    983   1.1  christos       int res;
    984   1.1  christos 
    985   1.1  christos       /* Decode message.  */
    986   1.9  christos       res = decode_exception_message (hdr, &inf, &thread);
    987   1.1  christos 
    988   1.1  christos       if (res < 0)
    989   1.1  christos 	{
    990   1.1  christos 	  /* Should not happen...  */
    991  1.10  christos 	  warning (_("darwin_wait: ill-formatted message (id=0x%x)\n"),
    992  1.10  christos 		   hdr->msgh_id);
    993   1.1  christos 	  /* FIXME: send a failure reply?  */
    994  1.10  christos 	  status->set_ignore ();
    995   1.3  christos 	  return minus_one_ptid;
    996   1.3  christos 	}
    997   1.3  christos       if (inf == NULL)
    998   1.3  christos 	{
    999  1.10  christos 	  status->set_ignore ();
   1000   1.1  christos 	  return minus_one_ptid;
   1001   1.1  christos 	}
   1002   1.1  christos       *pinf = inf;
   1003   1.1  christos       *pthread = thread;
   1004   1.8  christos 
   1005   1.8  christos       darwin_inferior *priv = get_darwin_inferior (inf);
   1006   1.8  christos 
   1007   1.8  christos       priv->pending_messages++;
   1008   1.1  christos 
   1009   1.1  christos       thread->msg_state = DARWIN_MESSAGE;
   1010   1.1  christos 
   1011   1.1  christos       inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"),
   1012   1.1  christos 		      thread->gdb_port,
   1013   1.1  christos 		      unparse_exception_type (thread->event.ex_type));
   1014   1.1  christos 
   1015   1.1  christos       switch (thread->event.ex_type)
   1016   1.1  christos 	{
   1017   1.1  christos 	case EXC_BAD_ACCESS:
   1018  1.10  christos 	  status->set_stopped (GDB_EXC_BAD_ACCESS);
   1019   1.1  christos 	  break;
   1020   1.1  christos 	case EXC_BAD_INSTRUCTION:
   1021  1.10  christos 	  status->set_stopped (GDB_EXC_BAD_INSTRUCTION);
   1022   1.1  christos 	  break;
   1023   1.1  christos 	case EXC_ARITHMETIC:
   1024  1.10  christos 	  status->set_stopped (GDB_EXC_ARITHMETIC);
   1025   1.1  christos 	  break;
   1026   1.1  christos 	case EXC_EMULATION:
   1027  1.10  christos 	  status->set_stopped (GDB_EXC_EMULATION);
   1028   1.1  christos 	  break;
   1029   1.1  christos 	case EXC_SOFTWARE:
   1030   1.1  christos 	  if (thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
   1031   1.1  christos 	    {
   1032  1.10  christos 	      status->set_stopped
   1033  1.10  christos 		(gdb_signal_from_host (thread->event.ex_data[1]));
   1034   1.1  christos 	      inferior_debug (5, _("  (signal %d: %s)\n"),
   1035   1.1  christos 			      thread->event.ex_data[1],
   1036  1.10  christos 			      gdb_signal_to_name (status->sig ()));
   1037   1.1  christos 
   1038   1.1  christos 	      /* If the thread is stopped because it has received a signal
   1039   1.1  christos 		 that gdb has just sent, continue.  */
   1040   1.1  christos 	      if (thread->signaled)
   1041   1.1  christos 		{
   1042   1.1  christos 		  thread->signaled = 0;
   1043   1.1  christos 		  darwin_send_reply (inf, thread);
   1044   1.1  christos 		  thread->msg_state = DARWIN_RUNNING;
   1045  1.10  christos 		  status->set_ignore ();
   1046   1.1  christos 		}
   1047   1.1  christos 	    }
   1048   1.1  christos 	  else
   1049  1.10  christos 	    status->set_stopped (GDB_EXC_SOFTWARE);
   1050   1.1  christos 	  break;
   1051   1.1  christos 	case EXC_BREAKPOINT:
   1052   1.1  christos 	  /* Many internal GDB routines expect breakpoints to be reported
   1053   1.1  christos 	     as GDB_SIGNAL_TRAP, and will report GDB_EXC_BREAKPOINT
   1054   1.1  christos 	     as a spurious signal.  */
   1055  1.10  christos 	  status->set_stopped (GDB_SIGNAL_TRAP);
   1056   1.1  christos 	  break;
   1057   1.1  christos 	default:
   1058  1.10  christos 	  status->set_stopped (GDB_SIGNAL_UNKNOWN);
   1059   1.1  christos 	  break;
   1060   1.1  christos 	}
   1061   1.1  christos 
   1062   1.8  christos       return ptid_t (inf->pid, 0, thread->gdb_port);
   1063   1.1  christos     }
   1064   1.3  christos   else if (hdr->msgh_id == 0x48)
   1065   1.3  christos     {
   1066  1.10  christos       /* MACH_NOTIFY_DEAD_NAME: notification for exit *or* WIFSTOPPED.  */
   1067   1.7  christos       int res;
   1068   1.7  christos 
   1069   1.7  christos       res = darwin_decode_notify_message (hdr, &inf);
   1070   1.7  christos 
   1071   1.7  christos       if (res < 0)
   1072   1.7  christos 	{
   1073   1.7  christos 	  /* Should not happen...  */
   1074  1.10  christos 	  warning
   1075   1.7  christos 	    (_("darwin_wait: ill-formatted message (id=0x%x, res=%d)\n"),
   1076   1.7  christos 	     hdr->msgh_id, res);
   1077   1.7  christos 	}
   1078   1.7  christos 
   1079   1.3  christos       *pinf = NULL;
   1080   1.3  christos       *pthread = NULL;
   1081   1.1  christos 
   1082   1.7  christos       if (res < 0 || inf == NULL)
   1083   1.7  christos 	{
   1084  1.10  christos 	  status->set_ignore ();
   1085   1.7  christos 	  return minus_one_ptid;
   1086   1.7  christos 	}
   1087   1.7  christos 
   1088   1.3  christos       if (inf != NULL)
   1089   1.3  christos 	{
   1090   1.8  christos 	  darwin_inferior *priv = get_darwin_inferior (inf);
   1091   1.8  christos 
   1092   1.8  christos 	  if (!priv->no_ptrace)
   1093   1.3  christos 	    {
   1094   1.9  christos 	      pid_t res_pid;
   1095   1.3  christos 	      int wstatus;
   1096   1.1  christos 
   1097   1.9  christos 	      res_pid = wait4 (inf->pid, &wstatus, 0, NULL);
   1098   1.9  christos 	      if (res_pid < 0 || res_pid != inf->pid)
   1099   1.3  christos 		{
   1100  1.10  christos 		  warning (_("wait4: res=%d: %s\n"),
   1101  1.10  christos 			   res_pid, safe_strerror (errno));
   1102  1.10  christos 		  status->set_ignore ();
   1103   1.3  christos 		  return minus_one_ptid;
   1104   1.3  christos 		}
   1105   1.3  christos 	      if (WIFEXITED (wstatus))
   1106   1.3  christos 		{
   1107  1.10  christos 		  status->set_exited (WEXITSTATUS (wstatus));
   1108  1.10  christos 	          inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"),
   1109  1.10  christos 				  res_pid, wstatus);
   1110  1.10  christos 		}
   1111  1.10  christos 	      else if (WIFSTOPPED (wstatus))
   1112  1.10  christos 		{
   1113  1.10  christos 		  /* Ignore stopped state, it will be handled by the next
   1114  1.10  christos 		     exception.  */
   1115  1.10  christos 		  status->set_ignore ();
   1116  1.10  christos 		  inferior_debug (4, _("darwin_wait: pid %d received WIFSTOPPED\n"),
   1117  1.10  christos 				  res_pid);
   1118  1.10  christos 		  return minus_one_ptid;
   1119  1.10  christos 		}
   1120  1.10  christos 	      else if (WIFSIGNALED (wstatus))
   1121  1.10  christos 		{
   1122  1.10  christos 		  status->set_signalled
   1123  1.10  christos 		    (gdb_signal_from_host (WTERMSIG (wstatus)));
   1124  1.10  christos 		  inferior_debug (4, _("darwin_wait: pid=%d received signal %d\n"),
   1125  1.10  christos 				  res_pid, status->sig());
   1126   1.3  christos 		}
   1127   1.3  christos 	      else
   1128   1.3  christos 		{
   1129  1.10  christos 		  status->set_ignore ();
   1130  1.10  christos 		  warning (_("Unexpected wait status after MACH_NOTIFY_DEAD_NAME "
   1131  1.10  christos 		             "notification: 0x%x"), wstatus);
   1132  1.10  christos 		  return minus_one_ptid;
   1133   1.3  christos 		}
   1134   1.3  christos 
   1135   1.9  christos 	      return ptid_t (inf->pid);
   1136   1.1  christos 	    }
   1137   1.3  christos 	  else
   1138   1.1  christos 	    {
   1139   1.3  christos 	      inferior_debug (4, _("darwin_wait: pid=%d\n"), inf->pid);
   1140  1.10  christos 	      status->set_exited (0 /* Don't know.  */);
   1141   1.8  christos 	      return ptid_t (inf->pid, 0, 0);
   1142   1.1  christos 	    }
   1143   1.1  christos 	}
   1144   1.1  christos     }
   1145   1.1  christos 
   1146   1.3  christos   /* Unknown message.  */
   1147   1.3  christos   warning (_("darwin: got unknown message, id: 0x%x"), hdr->msgh_id);
   1148  1.10  christos   status->set_ignore ();
   1149   1.1  christos   return minus_one_ptid;
   1150   1.1  christos }
   1151   1.1  christos 
   1152   1.9  christos int
   1153   1.9  christos darwin_nat_target::cancel_breakpoint (ptid_t ptid)
   1154   1.1  christos {
   1155   1.1  christos   /* Arrange for a breakpoint to be hit again later.  We will handle
   1156   1.1  christos      the current event, eventually we will resume this thread, and this
   1157   1.1  christos      breakpoint will trap again.
   1158   1.1  christos 
   1159   1.1  christos      If we do not do this, then we run the risk that the user will
   1160   1.1  christos      delete or disable the breakpoint, but the thread will have already
   1161   1.1  christos      tripped on it.  */
   1162   1.1  christos 
   1163   1.9  christos   struct regcache *regcache = get_thread_regcache (this, ptid);
   1164   1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
   1165   1.1  christos   CORE_ADDR pc;
   1166   1.1  christos 
   1167   1.5  christos   pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
   1168   1.8  christos   if (breakpoint_inserted_here_p (regcache->aspace (), pc))
   1169   1.1  christos     {
   1170   1.6  christos       inferior_debug (4, "cancel_breakpoint for thread 0x%lx\n",
   1171   1.8  christos 		      (unsigned long) ptid.tid ());
   1172   1.1  christos 
   1173   1.1  christos       /* Back up the PC if necessary.  */
   1174   1.5  christos       if (gdbarch_decr_pc_after_break (gdbarch))
   1175   1.1  christos 	regcache_write_pc (regcache, pc);
   1176   1.1  christos 
   1177   1.1  christos       return 1;
   1178   1.1  christos     }
   1179   1.1  christos   return 0;
   1180   1.1  christos }
   1181   1.1  christos 
   1182   1.9  christos ptid_t
   1183   1.9  christos darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status)
   1184   1.1  christos {
   1185   1.1  christos   kern_return_t kret;
   1186   1.1  christos   union
   1187   1.1  christos   {
   1188   1.1  christos     mach_msg_header_t hdr;
   1189   1.1  christos     char data[0x100];
   1190   1.1  christos   } msgin;
   1191   1.1  christos   mach_msg_header_t *hdr = &msgin.hdr;
   1192   1.1  christos   ptid_t res;
   1193   1.1  christos   darwin_thread_t *thread;
   1194   1.1  christos 
   1195   1.1  christos   inferior_debug
   1196  1.10  christos     (2, _("darwin_wait: waiting for a message ptid=%s\n"),
   1197  1.10  christos      ptid.to_string ().c_str ());
   1198   1.1  christos 
   1199   1.1  christos   /* Handle fake stop events at first.  */
   1200   1.1  christos   if (darwin_inf_fake_stop != NULL)
   1201   1.1  christos     {
   1202   1.9  christos       inferior *inf = darwin_inf_fake_stop;
   1203   1.1  christos       darwin_inf_fake_stop = NULL;
   1204   1.1  christos 
   1205   1.8  christos       darwin_inferior *priv = get_darwin_inferior (inf);
   1206   1.8  christos 
   1207  1.10  christos       status->set_stopped (GDB_SIGNAL_TRAP);
   1208   1.8  christos       thread = priv->threads[0];
   1209   1.1  christos       thread->msg_state = DARWIN_STOPPED;
   1210   1.8  christos       return ptid_t (inf->pid, 0, thread->gdb_port);
   1211   1.1  christos     }
   1212   1.1  christos 
   1213   1.1  christos   do
   1214   1.1  christos     {
   1215   1.1  christos       /* set_sigint_trap (); */
   1216   1.1  christos 
   1217   1.1  christos       /* Wait for a message.  */
   1218   1.1  christos       kret = mach_msg (&msgin.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0,
   1219   1.1  christos 		       sizeof (msgin.data), darwin_port_set, 0, MACH_PORT_NULL);
   1220   1.1  christos 
   1221   1.1  christos       /* clear_sigint_trap (); */
   1222   1.1  christos 
   1223   1.1  christos       if (kret == MACH_RCV_INTERRUPTED)
   1224   1.1  christos 	{
   1225  1.10  christos 	  status->set_ignore ();
   1226   1.1  christos 	  return minus_one_ptid;
   1227   1.1  christos 	}
   1228   1.1  christos 
   1229   1.1  christos       if (kret != MACH_MSG_SUCCESS)
   1230   1.1  christos 	{
   1231   1.1  christos 	  inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret);
   1232  1.10  christos 	  status->set_spurious ();
   1233   1.1  christos 	  return minus_one_ptid;
   1234   1.1  christos 	}
   1235   1.1  christos 
   1236   1.1  christos       /* Debug: display message.  */
   1237   1.1  christos       if (darwin_debug_flag > 10)
   1238   1.1  christos 	darwin_dump_message (hdr, darwin_debug_flag > 11);
   1239   1.1  christos 
   1240   1.9  christos       inferior *inf;
   1241   1.9  christos       res = decode_message (hdr, &thread, &inf, status);
   1242   1.8  christos       if (res == minus_one_ptid)
   1243   1.3  christos 	continue;
   1244   1.1  christos 
   1245   1.3  christos       /* Early return in case an inferior has exited.  */
   1246   1.1  christos       if (inf == NULL)
   1247   1.1  christos 	return res;
   1248   1.1  christos     }
   1249  1.10  christos   while (status->kind () == TARGET_WAITKIND_IGNORE);
   1250   1.1  christos 
   1251   1.1  christos   /* Stop all tasks.  */
   1252   1.9  christos   for (inferior *inf : all_inferiors (this))
   1253   1.9  christos     {
   1254   1.9  christos       darwin_suspend_inferior (inf);
   1255   1.9  christos       check_new_threads (inf);
   1256   1.9  christos     }
   1257   1.1  christos 
   1258   1.1  christos   /* Read pending messages.  */
   1259   1.1  christos   while (1)
   1260   1.1  christos     {
   1261   1.1  christos       struct target_waitstatus status2;
   1262   1.1  christos       ptid_t ptid2;
   1263   1.1  christos 
   1264   1.1  christos       kret = mach_msg (&msgin.hdr,
   1265   1.1  christos 		       MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
   1266   1.1  christos 		       sizeof (msgin.data), darwin_port_set, 1, MACH_PORT_NULL);
   1267   1.1  christos 
   1268   1.1  christos       if (kret == MACH_RCV_TIMED_OUT)
   1269   1.1  christos 	break;
   1270   1.1  christos       if (kret != MACH_MSG_SUCCESS)
   1271   1.1  christos 	{
   1272   1.1  christos 	  inferior_debug
   1273   1.1  christos 	    (5, _("darwin_wait: mach_msg(pending) ret=0x%x\n"), kret);
   1274   1.1  christos 	  break;
   1275   1.1  christos 	}
   1276   1.1  christos 
   1277   1.3  christos       /* Debug: display message.  */
   1278   1.3  christos       if (darwin_debug_flag > 10)
   1279   1.3  christos 	darwin_dump_message (hdr, darwin_debug_flag > 11);
   1280   1.3  christos 
   1281   1.9  christos       inferior *inf;
   1282   1.9  christos       ptid2 = decode_message (hdr, &thread, &inf, &status2);
   1283   1.1  christos 
   1284   1.1  christos       if (inf != NULL && thread != NULL
   1285   1.1  christos 	  && thread->event.ex_type == EXC_BREAKPOINT)
   1286   1.1  christos 	{
   1287   1.1  christos 	  if (thread->single_step
   1288   1.8  christos 	      || cancel_breakpoint (ptid_t (inf->pid, 0, thread->gdb_port)))
   1289   1.1  christos 	    {
   1290   1.1  christos 	      gdb_assert (thread->msg_state == DARWIN_MESSAGE);
   1291   1.1  christos 	      darwin_send_reply (inf, thread);
   1292   1.1  christos 	      thread->msg_state = DARWIN_RUNNING;
   1293   1.1  christos 	    }
   1294   1.1  christos 	  else
   1295   1.1  christos 	    inferior_debug
   1296   1.1  christos 	      (3, _("darwin_wait: thread 0x%x hit a non-gdb breakpoint\n"),
   1297   1.1  christos 	       thread->gdb_port);
   1298   1.1  christos 	}
   1299   1.1  christos       else
   1300   1.1  christos 	inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
   1301   1.1  christos     }
   1302   1.1  christos   return res;
   1303   1.1  christos }
   1304   1.1  christos 
   1305   1.8  christos ptid_t
   1306   1.8  christos darwin_nat_target::wait (ptid_t ptid, struct target_waitstatus *status,
   1307  1.10  christos 			 target_wait_flags options)
   1308   1.1  christos {
   1309   1.9  christos   return wait_1 (ptid, status);
   1310   1.1  christos }
   1311   1.1  christos 
   1312   1.8  christos void
   1313   1.8  christos darwin_nat_target::interrupt ()
   1314   1.1  christos {
   1315   1.1  christos   struct inferior *inf = current_inferior ();
   1316   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1317   1.1  christos 
   1318   1.1  christos   /* FIXME: handle in no_ptrace mode.  */
   1319   1.8  christos   gdb_assert (!priv->no_ptrace);
   1320   1.8  christos   ::kill (inf->pid, SIGINT);
   1321   1.1  christos }
   1322   1.1  christos 
   1323   1.7  christos /* Deallocate threads port and vector.  */
   1324   1.7  christos 
   1325   1.1  christos static void
   1326   1.7  christos darwin_deallocate_threads (struct inferior *inf)
   1327   1.1  christos {
   1328   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1329   1.8  christos 
   1330   1.8  christos   for (darwin_thread_t *t : priv->threads)
   1331   1.1  christos     {
   1332   1.8  christos       kern_return_t kret = mach_port_deallocate (gdb_task, t->gdb_port);
   1333   1.8  christos       MACH_CHECK_ERROR (kret);
   1334   1.1  christos     }
   1335   1.8  christos 
   1336   1.8  christos   priv->threads.clear ();
   1337   1.7  christos }
   1338   1.7  christos 
   1339   1.8  christos void
   1340   1.8  christos darwin_nat_target::mourn_inferior ()
   1341   1.7  christos {
   1342   1.7  christos   struct inferior *inf = current_inferior ();
   1343   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1344   1.7  christos   kern_return_t kret;
   1345   1.7  christos   mach_port_t prev;
   1346   1.7  christos 
   1347   1.7  christos   /* Deallocate threads.  */
   1348   1.7  christos   darwin_deallocate_threads (inf);
   1349   1.1  christos 
   1350   1.7  christos   /* Remove notify_port from darwin_port_set.  */
   1351   1.1  christos   kret = mach_port_move_member (gdb_task,
   1352   1.8  christos 				priv->notify_port, MACH_PORT_NULL);
   1353   1.1  christos   MACH_CHECK_ERROR (kret);
   1354   1.1  christos 
   1355   1.7  christos   /* Remove task port dead_name notification.  */
   1356   1.8  christos   kret = mach_port_request_notification (gdb_task, priv->task,
   1357   1.1  christos 					 MACH_NOTIFY_DEAD_NAME, 0,
   1358   1.1  christos 					 MACH_PORT_NULL,
   1359   1.1  christos 					 MACH_MSG_TYPE_MAKE_SEND_ONCE,
   1360   1.1  christos 					 &prev);
   1361   1.1  christos   /* This can fail if the task is dead.  */
   1362   1.1  christos   inferior_debug (4, "task=0x%x, prev=0x%x, notify_port=0x%x\n",
   1363   1.8  christos 		  priv->task, prev, priv->notify_port);
   1364   1.1  christos 
   1365   1.1  christos   if (kret == KERN_SUCCESS)
   1366   1.1  christos     {
   1367   1.1  christos       kret = mach_port_deallocate (gdb_task, prev);
   1368   1.1  christos       MACH_CHECK_ERROR (kret);
   1369   1.1  christos     }
   1370   1.1  christos 
   1371   1.7  christos   /* Destroy notify_port.  */
   1372   1.8  christos   kret = mach_port_destroy (gdb_task, priv->notify_port);
   1373   1.1  christos   MACH_CHECK_ERROR (kret);
   1374   1.1  christos 
   1375   1.1  christos   /* Deallocate saved exception ports.  */
   1376   1.8  christos   darwin_deallocate_exception_ports (priv);
   1377   1.1  christos 
   1378   1.7  christos   /* Deallocate task port.  */
   1379   1.8  christos   kret = mach_port_deallocate (gdb_task, priv->task);
   1380   1.1  christos   MACH_CHECK_ERROR (kret);
   1381   1.1  christos 
   1382   1.5  christos   inf->priv = NULL;
   1383   1.1  christos 
   1384   1.8  christos   inf_child_target::mourn_inferior ();
   1385   1.1  christos }
   1386   1.1  christos 
   1387   1.1  christos static void
   1388   1.1  christos darwin_reply_to_all_pending_messages (struct inferior *inf)
   1389   1.1  christos {
   1390   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1391   1.1  christos 
   1392   1.8  christos   for (darwin_thread_t *t : priv->threads)
   1393   1.1  christos     {
   1394   1.1  christos       if (t->msg_state == DARWIN_MESSAGE)
   1395   1.1  christos 	darwin_resume_thread (inf, t, 0, 0);
   1396   1.1  christos     }
   1397   1.1  christos }
   1398   1.1  christos 
   1399   1.9  christos void
   1400   1.9  christos darwin_nat_target::stop_inferior (inferior *inf)
   1401   1.1  christos {
   1402   1.1  christos   struct target_waitstatus wstatus;
   1403   1.1  christos   ptid_t ptid;
   1404   1.1  christos   int res;
   1405   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1406   1.1  christos 
   1407   1.1  christos   gdb_assert (inf != NULL);
   1408   1.1  christos 
   1409   1.1  christos   darwin_suspend_inferior (inf);
   1410   1.1  christos 
   1411   1.1  christos   darwin_reply_to_all_pending_messages (inf);
   1412   1.1  christos 
   1413   1.8  christos   if (priv->no_ptrace)
   1414   1.1  christos     return;
   1415   1.1  christos 
   1416   1.9  christos   res = ::kill (inf->pid, SIGSTOP);
   1417   1.1  christos   if (res != 0)
   1418   1.1  christos     warning (_("cannot kill: %s"), safe_strerror (errno));
   1419   1.1  christos 
   1420   1.1  christos   /* Wait until the process is really stopped.  */
   1421   1.1  christos   while (1)
   1422   1.1  christos     {
   1423   1.9  christos       ptid = wait_1 (ptid_t (inf->pid), &wstatus);
   1424  1.10  christos       if (wstatus.kind () == TARGET_WAITKIND_STOPPED
   1425  1.10  christos 	  && wstatus.sig () == GDB_SIGNAL_STOP)
   1426   1.1  christos 	break;
   1427   1.1  christos     }
   1428   1.1  christos }
   1429   1.1  christos 
   1430   1.1  christos static kern_return_t
   1431   1.1  christos darwin_save_exception_ports (darwin_inferior *inf)
   1432   1.1  christos {
   1433   1.1  christos   kern_return_t kret;
   1434   1.1  christos 
   1435   1.1  christos   inf->exception_info.count =
   1436   1.1  christos     sizeof (inf->exception_info.ports) / sizeof (inf->exception_info.ports[0]);
   1437   1.1  christos 
   1438   1.1  christos   kret = task_get_exception_ports
   1439   1.1  christos     (inf->task, EXC_MASK_ALL, inf->exception_info.masks,
   1440   1.1  christos      &inf->exception_info.count, inf->exception_info.ports,
   1441   1.1  christos      inf->exception_info.behaviors, inf->exception_info.flavors);
   1442   1.1  christos   return kret;
   1443   1.1  christos }
   1444   1.1  christos 
   1445   1.1  christos static kern_return_t
   1446   1.1  christos darwin_restore_exception_ports (darwin_inferior *inf)
   1447   1.1  christos {
   1448   1.1  christos   int i;
   1449   1.1  christos   kern_return_t kret;
   1450   1.1  christos 
   1451   1.1  christos   for (i = 0; i < inf->exception_info.count; i++)
   1452   1.1  christos     {
   1453   1.1  christos       kret = task_set_exception_ports
   1454  1.10  christos 	(inf->task, inf->exception_info.masks[i], inf->exception_info.ports[i],
   1455   1.1  christos 	 inf->exception_info.behaviors[i], inf->exception_info.flavors[i]);
   1456   1.1  christos       if (kret != KERN_SUCCESS)
   1457  1.10  christos 	return kret;
   1458   1.1  christos     }
   1459   1.1  christos 
   1460   1.1  christos   return KERN_SUCCESS;
   1461   1.1  christos }
   1462   1.1  christos 
   1463   1.7  christos /* Deallocate saved exception ports.  */
   1464   1.7  christos 
   1465   1.7  christos static void
   1466   1.7  christos darwin_deallocate_exception_ports (darwin_inferior *inf)
   1467   1.7  christos {
   1468   1.7  christos   int i;
   1469   1.7  christos   kern_return_t kret;
   1470   1.7  christos 
   1471   1.7  christos   for (i = 0; i < inf->exception_info.count; i++)
   1472   1.7  christos     {
   1473   1.7  christos       kret = mach_port_deallocate (gdb_task, inf->exception_info.ports[i]);
   1474   1.7  christos       MACH_CHECK_ERROR (kret);
   1475   1.7  christos     }
   1476   1.7  christos   inf->exception_info.count = 0;
   1477   1.7  christos }
   1478   1.7  christos 
   1479   1.7  christos static void
   1480   1.7  christos darwin_setup_exceptions (struct inferior *inf)
   1481   1.7  christos {
   1482   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1483   1.7  christos   kern_return_t kret;
   1484   1.7  christos   exception_mask_t mask;
   1485   1.7  christos 
   1486   1.8  christos   kret = darwin_save_exception_ports (priv);
   1487   1.7  christos   if (kret != KERN_SUCCESS)
   1488   1.7  christos     error (_("Unable to save exception ports, task_get_exception_ports"
   1489   1.7  christos 	     "returned: %d"),
   1490   1.7  christos 	   kret);
   1491   1.7  christos 
   1492   1.7  christos   /* Set exception port.  */
   1493   1.7  christos   if (enable_mach_exceptions)
   1494   1.7  christos     mask = EXC_MASK_ALL;
   1495   1.7  christos   else
   1496   1.7  christos     mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
   1497   1.8  christos   kret = task_set_exception_ports (priv->task, mask, darwin_ex_port,
   1498   1.7  christos 				   EXCEPTION_DEFAULT, THREAD_STATE_NONE);
   1499   1.7  christos   if (kret != KERN_SUCCESS)
   1500   1.7  christos     error (_("Unable to set exception ports, task_set_exception_ports"
   1501   1.7  christos 	     "returned: %d"),
   1502   1.7  christos 	   kret);
   1503   1.7  christos }
   1504   1.7  christos 
   1505   1.8  christos void
   1506   1.8  christos darwin_nat_target::kill ()
   1507   1.1  christos {
   1508   1.1  christos   struct inferior *inf = current_inferior ();
   1509   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1510   1.1  christos   struct target_waitstatus wstatus;
   1511   1.1  christos   ptid_t ptid;
   1512   1.1  christos   kern_return_t kret;
   1513   1.1  christos   int res;
   1514   1.1  christos 
   1515   1.8  christos   if (inferior_ptid == null_ptid)
   1516   1.1  christos     return;
   1517   1.1  christos 
   1518   1.1  christos   gdb_assert (inf != NULL);
   1519   1.1  christos 
   1520   1.8  christos   kret = darwin_restore_exception_ports (priv);
   1521   1.1  christos   MACH_CHECK_ERROR (kret);
   1522   1.1  christos 
   1523   1.1  christos   darwin_reply_to_all_pending_messages (inf);
   1524   1.1  christos 
   1525   1.8  christos   res = ::kill (inf->pid, 9);
   1526   1.1  christos 
   1527   1.1  christos   if (res == 0)
   1528   1.1  christos     {
   1529   1.8  christos       /* On MacOS version Sierra, the darwin_restore_exception_ports call
   1530  1.10  christos 	 does not work as expected.
   1531  1.10  christos 	 When the kill function is called, the SIGKILL signal is received
   1532  1.10  christos 	 by gdb whereas it should have been received by the kernel since
   1533  1.10  christos 	 the exception ports have been restored.
   1534  1.10  christos 	 This behavior is not the expected one thus gdb does not reply to
   1535  1.10  christos 	 the received SIGKILL message. This situation leads to a "busy"
   1536  1.10  christos 	 resource from the kernel point of view and the inferior is never
   1537  1.10  christos 	 released, causing it to remain as a zombie process, even after
   1538   1.8  christos 	 GDB exits.
   1539  1.10  christos 	 To work around this, we mark all the threads of the inferior as
   1540  1.10  christos 	 signaled thus darwin_decode_message function knows that the kill
   1541  1.10  christos 	 signal was sent by gdb and will take the appropriate action
   1542  1.10  christos 	 (cancel signal and reply to the signal message).  */
   1543   1.8  christos       for (darwin_thread_t *thread : priv->threads)
   1544  1.10  christos 	thread->signaled = 1;
   1545   1.8  christos 
   1546   1.1  christos       darwin_resume_inferior (inf);
   1547   1.3  christos 
   1548   1.9  christos       ptid = wait_1 (ptid_t (inf->pid), &wstatus);
   1549   1.1  christos     }
   1550   1.1  christos   else if (errno != ESRCH)
   1551   1.1  christos     warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"),
   1552   1.1  christos 	     inf->pid, safe_strerror (errno));
   1553   1.1  christos 
   1554   1.9  christos   target_mourn_inferior (ptid_t (inf->pid));
   1555   1.7  christos }
   1556   1.7  christos 
   1557   1.7  christos static void
   1558   1.7  christos darwin_setup_request_notification (struct inferior *inf)
   1559   1.7  christos {
   1560   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1561   1.7  christos   kern_return_t kret;
   1562   1.7  christos   mach_port_t prev_not;
   1563   1.7  christos 
   1564   1.8  christos   kret = mach_port_request_notification (gdb_task, priv->task,
   1565   1.7  christos 					 MACH_NOTIFY_DEAD_NAME, 0,
   1566   1.8  christos 					 priv->notify_port,
   1567   1.7  christos 					 MACH_MSG_TYPE_MAKE_SEND_ONCE,
   1568   1.7  christos 					 &prev_not);
   1569   1.7  christos   if (kret != KERN_SUCCESS)
   1570   1.7  christos     error (_("Termination notification request failed, "
   1571   1.7  christos 	     "mach_port_request_notification\n"
   1572   1.7  christos 	     "returned: %d"),
   1573   1.7  christos 	   kret);
   1574   1.7  christos   if (prev_not != MACH_PORT_NULL)
   1575   1.7  christos     {
   1576   1.7  christos       /* This is unexpected, as there should not be any previously
   1577   1.7  christos 	 registered notification request.  But this is not a fatal
   1578   1.7  christos 	 issue, so just emit a warning.  */
   1579   1.7  christos       warning (_("\
   1580   1.7  christos A task termination request was registered before the debugger registered\n\
   1581   1.7  christos its own.  This is unexpected, but should otherwise not have any actual\n\
   1582   1.7  christos impact on the debugging session."));
   1583   1.7  christos     }
   1584   1.1  christos }
   1585   1.1  christos 
   1586   1.1  christos static void
   1587   1.1  christos darwin_attach_pid (struct inferior *inf)
   1588   1.1  christos {
   1589   1.1  christos   kern_return_t kret;
   1590   1.1  christos 
   1591   1.8  christos   darwin_inferior *priv = new darwin_inferior;
   1592   1.8  christos   inf->priv.reset (priv);
   1593   1.1  christos 
   1594   1.9  christos   try
   1595   1.1  christos     {
   1596   1.8  christos       kret = task_for_pid (gdb_task, inf->pid, &priv->task);
   1597   1.8  christos       if (kret != KERN_SUCCESS)
   1598   1.8  christos 	{
   1599   1.8  christos 	  int status;
   1600   1.1  christos 
   1601   1.8  christos 	  if (!inf->attach_flag)
   1602   1.8  christos 	    {
   1603   1.8  christos 	      kill (inf->pid, 9);
   1604   1.8  christos 	      waitpid (inf->pid, &status, 0);
   1605   1.8  christos 	    }
   1606   1.8  christos 
   1607   1.8  christos 	  error
   1608   1.8  christos 	    (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
   1609   1.8  christos 	       " (please check gdb is codesigned - see taskgated(8))"),
   1610   1.8  christos 	     inf->pid, mach_error_string (kret), (unsigned long) kret);
   1611   1.1  christos 	}
   1612   1.1  christos 
   1613   1.8  christos       inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
   1614   1.8  christos 		      priv->task, inf->pid);
   1615   1.8  christos 
   1616   1.8  christos       if (darwin_ex_port == MACH_PORT_NULL)
   1617   1.8  christos 	{
   1618   1.8  christos 	  /* Create a port to get exceptions.  */
   1619   1.8  christos 	  kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
   1620   1.8  christos 				     &darwin_ex_port);
   1621   1.8  christos 	  if (kret != KERN_SUCCESS)
   1622   1.8  christos 	    error (_("Unable to create exception port, mach_port_allocate "
   1623   1.8  christos 		     "returned: %d"),
   1624   1.8  christos 		   kret);
   1625   1.8  christos 
   1626   1.8  christos 	  kret = mach_port_insert_right (gdb_task, darwin_ex_port,
   1627   1.8  christos 					 darwin_ex_port,
   1628   1.8  christos 					 MACH_MSG_TYPE_MAKE_SEND);
   1629   1.8  christos 	  if (kret != KERN_SUCCESS)
   1630   1.8  christos 	    error (_("Unable to create exception port, mach_port_insert_right "
   1631   1.8  christos 		     "returned: %d"),
   1632   1.8  christos 		   kret);
   1633   1.8  christos 
   1634   1.8  christos 	  /* Create a port set and put ex_port in it.  */
   1635   1.8  christos 	  kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_PORT_SET,
   1636   1.8  christos 				     &darwin_port_set);
   1637   1.8  christos 	  if (kret != KERN_SUCCESS)
   1638   1.8  christos 	    error (_("Unable to create port set, mach_port_allocate "
   1639   1.8  christos 		     "returned: %d"),
   1640   1.8  christos 		   kret);
   1641   1.1  christos 
   1642   1.8  christos 	  kret = mach_port_move_member (gdb_task, darwin_ex_port,
   1643   1.8  christos 					darwin_port_set);
   1644   1.8  christos 	  if (kret != KERN_SUCCESS)
   1645   1.8  christos 	    error (_("Unable to move exception port into new port set, "
   1646   1.8  christos 		     "mach_port_move_member\n"
   1647   1.8  christos 		     "returned: %d"),
   1648   1.8  christos 		   kret);
   1649   1.8  christos 	}
   1650   1.1  christos 
   1651   1.8  christos       /* Create a port to be notified when the child task terminates.  */
   1652   1.1  christos       kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
   1653   1.8  christos 				 &priv->notify_port);
   1654   1.1  christos       if (kret != KERN_SUCCESS)
   1655   1.8  christos 	error (_("Unable to create notification port, mach_port_allocate "
   1656   1.1  christos 		 "returned: %d"),
   1657   1.1  christos 	       kret);
   1658   1.1  christos 
   1659   1.8  christos       kret = mach_port_move_member (gdb_task,
   1660   1.8  christos 				    priv->notify_port, darwin_port_set);
   1661   1.1  christos       if (kret != KERN_SUCCESS)
   1662   1.8  christos 	error (_("Unable to move notification port into new port set, "
   1663   1.8  christos 		 "mach_port_move_member\n"
   1664   1.1  christos 		 "returned: %d"),
   1665   1.1  christos 	       kret);
   1666   1.1  christos 
   1667   1.8  christos       darwin_setup_request_notification (inf);
   1668   1.8  christos 
   1669   1.8  christos       darwin_setup_exceptions (inf);
   1670   1.8  christos     }
   1671   1.9  christos   catch (const gdb_exception &ex)
   1672   1.8  christos     {
   1673   1.8  christos       exit_inferior (inf);
   1674   1.9  christos       switch_to_no_thread ();
   1675   1.1  christos 
   1676   1.9  christos       throw;
   1677   1.1  christos     }
   1678   1.1  christos 
   1679   1.8  christos   target_ops *darwin_ops = get_native_target ();
   1680  1.10  christos   if (!inf->target_is_pushed (darwin_ops))
   1681  1.10  christos     inf->push_target (darwin_ops);
   1682   1.8  christos }
   1683   1.1  christos 
   1684   1.8  christos /* Get the thread_info object corresponding to this darwin_thread_info.  */
   1685   1.1  christos 
   1686   1.8  christos static struct thread_info *
   1687   1.8  christos thread_info_from_private_thread_info (darwin_thread_info *pti)
   1688   1.8  christos {
   1689   1.8  christos   for (struct thread_info *it : all_threads ())
   1690   1.8  christos     {
   1691   1.8  christos       darwin_thread_info *iter_pti = get_darwin_thread_info (it);
   1692   1.1  christos 
   1693   1.8  christos       if (iter_pti->gdb_port == pti->gdb_port)
   1694   1.8  christos 	return it;
   1695   1.8  christos     }
   1696   1.1  christos 
   1697   1.8  christos   gdb_assert_not_reached ("did not find gdb thread for darwin thread");
   1698   1.1  christos }
   1699   1.1  christos 
   1700   1.9  christos void
   1701   1.9  christos darwin_nat_target::init_thread_list (inferior *inf)
   1702   1.1  christos {
   1703   1.9  christos   check_new_threads (inf);
   1704   1.8  christos 
   1705   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   1706   1.8  christos 
   1707   1.8  christos   gdb_assert (!priv->threads.empty ());
   1708   1.1  christos 
   1709   1.8  christos   darwin_thread_info *first_pti = priv->threads.front ();
   1710   1.8  christos   struct thread_info *first_thread
   1711   1.8  christos     = thread_info_from_private_thread_info (first_pti);
   1712   1.1  christos 
   1713   1.9  christos   switch_to_thread (first_thread);
   1714   1.1  christos }
   1715   1.1  christos 
   1716   1.1  christos /* The child must synchronize with gdb: gdb must set the exception port
   1717   1.1  christos    before the child call PTRACE_SIGEXC.  We use a pipe to achieve this.
   1718   1.1  christos    FIXME: is there a lighter way ?  */
   1719   1.1  christos static int ptrace_fds[2];
   1720   1.1  christos 
   1721   1.1  christos static void
   1722   1.1  christos darwin_ptrace_me (void)
   1723   1.1  christos {
   1724   1.1  christos   int res;
   1725   1.1  christos   char c;
   1726   1.1  christos 
   1727   1.1  christos   /* Close write end point.  */
   1728   1.7  christos   if (close (ptrace_fds[1]) < 0)
   1729   1.7  christos     trace_start_error_with_name ("close");
   1730   1.1  christos 
   1731   1.1  christos   /* Wait until gdb is ready.  */
   1732   1.1  christos   res = read (ptrace_fds[0], &c, 1);
   1733   1.1  christos   if (res != 0)
   1734   1.7  christos     trace_start_error (_("unable to read from pipe, read returned: %d"), res);
   1735   1.7  christos 
   1736   1.7  christos   if (close (ptrace_fds[0]) < 0)
   1737   1.7  christos     trace_start_error_with_name ("close");
   1738   1.1  christos 
   1739   1.1  christos   /* Get rid of privileges.  */
   1740   1.7  christos   if (setegid (getgid ()) < 0)
   1741   1.7  christos     trace_start_error_with_name ("setegid");
   1742   1.1  christos 
   1743   1.1  christos   /* Set TRACEME.  */
   1744   1.7  christos   if (PTRACE (PT_TRACE_ME, 0, 0, 0) < 0)
   1745   1.7  christos     trace_start_error_with_name ("PTRACE");
   1746   1.1  christos 
   1747   1.1  christos   /* Redirect signals to exception port.  */
   1748   1.7  christos   if (PTRACE (PT_SIGEXC, 0, 0, 0) < 0)
   1749   1.7  christos     trace_start_error_with_name ("PTRACE");
   1750   1.1  christos }
   1751   1.1  christos 
   1752   1.1  christos /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).  */
   1753   1.1  christos static void
   1754   1.1  christos darwin_pre_ptrace (void)
   1755   1.1  christos {
   1756   1.1  christos   if (pipe (ptrace_fds) != 0)
   1757   1.1  christos     {
   1758   1.1  christos       ptrace_fds[0] = -1;
   1759   1.1  christos       ptrace_fds[1] = -1;
   1760   1.1  christos       error (_("unable to create a pipe: %s"), safe_strerror (errno));
   1761   1.1  christos     }
   1762   1.1  christos 
   1763   1.1  christos   mark_fd_no_cloexec (ptrace_fds[0]);
   1764   1.1  christos   mark_fd_no_cloexec (ptrace_fds[1]);
   1765   1.1  christos }
   1766   1.1  christos 
   1767   1.9  christos void
   1768   1.9  christos darwin_nat_target::ptrace_him (int pid)
   1769   1.1  christos {
   1770   1.1  christos   struct inferior *inf = current_inferior ();
   1771   1.1  christos 
   1772   1.1  christos   darwin_attach_pid (inf);
   1773   1.1  christos 
   1774   1.1  christos   /* Let's the child run.  */
   1775   1.9  christos   ::close (ptrace_fds[0]);
   1776   1.9  christos   ::close (ptrace_fds[1]);
   1777   1.1  christos 
   1778   1.1  christos   unmark_fd_no_cloexec (ptrace_fds[0]);
   1779   1.1  christos   unmark_fd_no_cloexec (ptrace_fds[1]);
   1780   1.1  christos 
   1781   1.9  christos   init_thread_list (inf);
   1782   1.1  christos 
   1783   1.8  christos   gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
   1784   1.1  christos }
   1785   1.1  christos 
   1786   1.1  christos static void
   1787   1.1  christos darwin_execvp (const char *file, char * const argv[], char * const env[])
   1788   1.1  christos {
   1789   1.1  christos   posix_spawnattr_t attr;
   1790   1.1  christos   short ps_flags = 0;
   1791   1.1  christos   int res;
   1792   1.1  christos 
   1793   1.1  christos   res = posix_spawnattr_init (&attr);
   1794   1.1  christos   if (res != 0)
   1795   1.1  christos     {
   1796  1.10  christos       gdb_printf
   1797  1.10  christos 	(gdb_stderr, "Cannot initialize attribute for posix_spawn\n");
   1798   1.1  christos       return;
   1799   1.1  christos     }
   1800   1.1  christos 
   1801   1.1  christos   /* Do like execve: replace the image.  */
   1802   1.1  christos   ps_flags = POSIX_SPAWN_SETEXEC;
   1803   1.1  christos 
   1804   1.1  christos   /* Disable ASLR.  The constant doesn't look to be available outside the
   1805   1.1  christos      kernel include files.  */
   1806   1.1  christos #ifndef _POSIX_SPAWN_DISABLE_ASLR
   1807   1.1  christos #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
   1808   1.1  christos #endif
   1809   1.1  christos   ps_flags |= _POSIX_SPAWN_DISABLE_ASLR;
   1810   1.1  christos   res = posix_spawnattr_setflags (&attr, ps_flags);
   1811   1.1  christos   if (res != 0)
   1812   1.1  christos     {
   1813  1.10  christos       gdb_printf (gdb_stderr, "Cannot set posix_spawn flags\n");
   1814   1.1  christos       return;
   1815   1.1  christos     }
   1816   1.1  christos 
   1817   1.1  christos   posix_spawnp (NULL, argv[0], NULL, &attr, argv, env);
   1818   1.1  christos }
   1819   1.1  christos 
   1820   1.8  christos /* Read kernel version, and return TRUE if this host may have System
   1821   1.8  christos    Integrity Protection (Sierra or later).  */
   1822   1.8  christos 
   1823   1.8  christos static bool
   1824   1.8  christos may_have_sip ()
   1825   1.8  christos {
   1826   1.8  christos   char str[16];
   1827   1.8  christos   size_t sz = sizeof (str);
   1828   1.8  christos   int ret;
   1829   1.8  christos 
   1830   1.8  christos   ret = sysctlbyname ("kern.osrelease", str, &sz, NULL, 0);
   1831   1.8  christos   if (ret == 0 && sz < sizeof (str))
   1832   1.8  christos     {
   1833   1.8  christos       unsigned long ver = strtoul (str, NULL, 10);
   1834   1.8  christos       if (ver >= 16)
   1835  1.10  christos 	return true;
   1836   1.8  christos     }
   1837   1.8  christos   return false;
   1838   1.8  christos }
   1839   1.8  christos 
   1840   1.8  christos /* A helper for maybe_cache_shell.  This copies the shell to the
   1841   1.8  christos    cache.  It will throw an exception on any failure.  */
   1842   1.8  christos 
   1843   1.1  christos static void
   1844   1.8  christos copy_shell_to_cache (const char *shell, const std::string &new_name)
   1845   1.8  christos {
   1846  1.10  christos   scoped_fd from_fd = gdb_open_cloexec (shell, O_RDONLY, 0);
   1847   1.8  christos   if (from_fd.get () < 0)
   1848   1.8  christos     error (_("Could not open shell (%s) for reading: %s"),
   1849   1.8  christos 	   shell, safe_strerror (errno));
   1850   1.8  christos 
   1851   1.8  christos   std::string new_dir = ldirname (new_name.c_str ());
   1852   1.8  christos   if (!mkdir_recursive (new_dir.c_str ()))
   1853   1.8  christos     error (_("Could not make cache directory \"%s\": %s"),
   1854   1.8  christos 	   new_dir.c_str (), safe_strerror (errno));
   1855   1.8  christos 
   1856   1.8  christos   gdb::char_vector temp_name = make_temp_filename (new_name);
   1857  1.10  christos   scoped_fd to_fd = gdb_mkostemp_cloexec (&temp_name[0]);
   1858   1.8  christos   gdb::unlinker unlink_file_on_error (temp_name.data ());
   1859   1.8  christos 
   1860   1.8  christos   if (to_fd.get () < 0)
   1861   1.8  christos     error (_("Could not open temporary file \"%s\" for writing: %s"),
   1862   1.8  christos 	   temp_name.data (), safe_strerror (errno));
   1863   1.8  christos 
   1864   1.8  christos   if (fcopyfile (from_fd.get (), to_fd.get (), nullptr,
   1865   1.8  christos 		 COPYFILE_STAT | COPYFILE_DATA) != 0)
   1866   1.8  christos     error (_("Could not copy shell to cache as \"%s\": %s"),
   1867   1.8  christos 	   temp_name.data (), safe_strerror (errno));
   1868   1.8  christos 
   1869   1.8  christos   /* Be sure that the caching is atomic so that we don't get bad
   1870   1.8  christos      results from multiple copies of gdb running at the same time.  */
   1871   1.8  christos   if (rename (temp_name.data (), new_name.c_str ()) != 0)
   1872   1.8  christos     error (_("Could not rename shell cache file to \"%s\": %s"),
   1873   1.8  christos 	   new_name.c_str (), safe_strerror (errno));
   1874   1.8  christos 
   1875   1.8  christos   unlink_file_on_error.keep ();
   1876   1.8  christos }
   1877   1.8  christos 
   1878   1.8  christos /* If $SHELL is restricted, try to cache a copy.  Starting with El
   1879   1.8  christos    Capitan, macOS introduced System Integrity Protection.  Among other
   1880   1.8  christos    things, this prevents certain executables from being ptrace'd.  In
   1881   1.8  christos    particular, executables in /bin, like most shells, are affected.
   1882   1.8  christos    To work around this, while preserving command-line glob expansion
   1883   1.8  christos    and redirections, gdb will cache a copy of the shell.  Return true
   1884   1.8  christos    if all is well -- either the shell is not subject to SIP or it has
   1885   1.8  christos    been successfully cached.  Returns false if something failed.  */
   1886   1.8  christos 
   1887   1.8  christos static bool
   1888   1.8  christos maybe_cache_shell ()
   1889   1.1  christos {
   1890   1.8  christos   /* SF_RESTRICTED is defined in sys/stat.h and lets us determine if a
   1891   1.8  christos      given file is subject to SIP.  */
   1892   1.8  christos #ifdef SF_RESTRICTED
   1893   1.8  christos 
   1894   1.8  christos   /* If a check fails we want to revert -- maybe the user deleted the
   1895   1.8  christos      cache while gdb was running, or something like that.  */
   1896   1.8  christos   copied_shell = nullptr;
   1897   1.8  christos 
   1898   1.8  christos   const char *shell = get_shell ();
   1899   1.8  christos   if (!IS_ABSOLUTE_PATH (shell))
   1900   1.8  christos     {
   1901   1.8  christos       warning (_("This version of macOS has System Integrity Protection.\n\
   1902   1.8  christos Normally gdb would try to work around this by caching a copy of your shell,\n\
   1903   1.8  christos but because your shell (%s) is not an absolute path, this is being skipped."),
   1904   1.8  christos 	       shell);
   1905   1.8  christos       return false;
   1906   1.8  christos     }
   1907   1.8  christos 
   1908   1.8  christos   struct stat sb;
   1909   1.8  christos   if (stat (shell, &sb) < 0)
   1910   1.8  christos     {
   1911   1.8  christos       warning (_("This version of macOS has System Integrity Protection.\n\
   1912   1.8  christos Normally gdb would try to work around this by caching a copy of your shell,\n\
   1913   1.8  christos but because gdb could not stat your shell (%s), this is being skipped.\n\
   1914   1.8  christos The error was: %s"),
   1915   1.8  christos 	       shell, safe_strerror (errno));
   1916   1.8  christos       return false;
   1917   1.8  christos     }
   1918   1.8  christos 
   1919   1.8  christos   if ((sb.st_flags & SF_RESTRICTED) == 0)
   1920   1.8  christos     return true;
   1921   1.8  christos 
   1922   1.8  christos   /* Put the copy somewhere like ~/Library/Caches/gdb/bin/sh.  */
   1923   1.8  christos   std::string new_name = get_standard_cache_dir ();
   1924   1.8  christos   /* There's no need to insert a directory separator here, because
   1925   1.8  christos      SHELL is known to be absolute.  */
   1926   1.8  christos   new_name.append (shell);
   1927   1.8  christos 
   1928   1.8  christos   /* Maybe it was cached by some earlier gdb.  */
   1929   1.8  christos   if (stat (new_name.c_str (), &sb) != 0 || !S_ISREG (sb.st_mode))
   1930   1.8  christos     {
   1931   1.9  christos       try
   1932   1.8  christos 	{
   1933   1.8  christos 	  copy_shell_to_cache (shell, new_name);
   1934   1.8  christos 	}
   1935   1.9  christos       catch (const gdb_exception_error &ex)
   1936   1.8  christos 	{
   1937   1.8  christos 	  warning (_("This version of macOS has System Integrity Protection.\n\
   1938   1.8  christos Because `startup-with-shell' is enabled, gdb tried to work around SIP by\n\
   1939   1.8  christos caching a copy of your shell.  However, this failed:\n\
   1940   1.8  christos %s\n\
   1941   1.8  christos If you correct the problem, gdb will automatically try again the next time\n\
   1942   1.8  christos you \"run\".  To prevent these attempts, you can use:\n\
   1943   1.8  christos     set startup-with-shell off"),
   1944   1.9  christos 		   ex.what ());
   1945   1.8  christos 	  return false;
   1946   1.8  christos 	}
   1947   1.8  christos 
   1948  1.10  christos       gdb_printf (_("Note: this version of macOS has System Integrity Protection.\n\
   1949   1.8  christos Because `startup-with-shell' is enabled, gdb has worked around this by\n\
   1950   1.8  christos caching a copy of your shell.  The shell used by \"run\" is now:\n\
   1951   1.8  christos     %s\n"),
   1952  1.10  christos 		  new_name.c_str ());
   1953   1.8  christos     }
   1954   1.8  christos 
   1955   1.8  christos   /* We need to make sure that the new name has the correct lifetime.  */
   1956   1.8  christos   static std::string saved_shell = std::move (new_name);
   1957   1.8  christos   copied_shell = saved_shell.c_str ();
   1958   1.8  christos 
   1959   1.8  christos #endif /* SF_RESTRICTED */
   1960   1.8  christos 
   1961   1.8  christos   return true;
   1962   1.8  christos }
   1963   1.8  christos 
   1964   1.8  christos void
   1965   1.8  christos darwin_nat_target::create_inferior (const char *exec_file,
   1966   1.8  christos 				    const std::string &allargs,
   1967   1.8  christos 				    char **env, int from_tty)
   1968   1.8  christos {
   1969   1.9  christos   gdb::optional<scoped_restore_tmpl<bool>> restore_startup_with_shell;
   1970   1.9  christos   darwin_nat_target *the_target = this;
   1971   1.8  christos 
   1972   1.8  christos   if (startup_with_shell && may_have_sip ())
   1973   1.8  christos     {
   1974   1.8  christos       if (!maybe_cache_shell ())
   1975   1.8  christos 	{
   1976   1.8  christos 	  warning (_("startup-with-shell is now temporarily disabled"));
   1977   1.8  christos 	  restore_startup_with_shell.emplace (&startup_with_shell, 0);
   1978   1.8  christos 	}
   1979   1.8  christos     }
   1980   1.8  christos 
   1981   1.1  christos   /* Do the hard work.  */
   1982   1.8  christos   fork_inferior (exec_file, allargs, env, darwin_ptrace_me,
   1983   1.9  christos 		 [the_target] (int pid)
   1984   1.9  christos 		   {
   1985   1.9  christos 		     the_target->ptrace_him (pid);
   1986   1.9  christos 		   },
   1987   1.9  christos 		 darwin_pre_ptrace, copied_shell,
   1988   1.8  christos 		 darwin_execvp);
   1989   1.1  christos }
   1990   1.1  christos 
   1991   1.1  christos 
   1993   1.1  christos /* Set things up such that the next call to darwin_wait will immediately
   1994   1.1  christos    return a fake stop event for inferior INF.
   1995   1.1  christos 
   1996   1.1  christos    This assumes that the inferior's thread list has been initialized,
   1997   1.1  christos    as it will suspend the inferior's first thread.  */
   1998   1.1  christos 
   1999   1.1  christos static void
   2000   1.1  christos darwin_setup_fake_stop_event (struct inferior *inf)
   2001   1.8  christos {
   2002   1.1  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   2003   1.1  christos   darwin_thread_t *thread;
   2004   1.1  christos   kern_return_t kret;
   2005   1.1  christos 
   2006   1.1  christos   gdb_assert (darwin_inf_fake_stop == NULL);
   2007   1.1  christos   darwin_inf_fake_stop = inf;
   2008   1.1  christos 
   2009   1.1  christos   /* When detecting a fake pending stop event, darwin_wait returns
   2010   1.1  christos      an event saying that the first thread is in a DARWIN_STOPPED
   2011   1.1  christos      state.  To make that accurate, we need to suspend that thread
   2012   1.1  christos      as well.  Otherwise, we'll try resuming it when resuming the
   2013   1.1  christos      inferior, and get a warning because the thread's suspend count
   2014   1.8  christos      is already zero, making the resume request useless.  */
   2015   1.1  christos   thread = priv->threads[0];
   2016   1.1  christos   kret = thread_suspend (thread->gdb_port);
   2017   1.1  christos   MACH_CHECK_ERROR (kret);
   2018   1.1  christos }
   2019   1.1  christos 
   2020   1.1  christos /* Attach to process PID, then initialize for debugging it
   2021   1.8  christos    and wait for the trace-trap that results from attaching.  */
   2022   1.8  christos void
   2023   1.1  christos darwin_nat_target::attach (const char *args, int from_tty)
   2024   1.1  christos {
   2025   1.1  christos   pid_t pid;
   2026   1.1  christos   struct inferior *inf;
   2027   1.1  christos 
   2028   1.1  christos   pid = parse_pid_to_attach (args);
   2029   1.1  christos 
   2030   1.1  christos   if (pid == getpid ())		/* Trying to masturbate?  */
   2031   1.1  christos     error (_("I refuse to debug myself!"));
   2032  1.10  christos 
   2033   1.1  christos   target_announce_attach (from_tty, pid);
   2034   1.8  christos 
   2035   1.1  christos   if (pid == 0 || ::kill (pid, 0) < 0)
   2036  1.10  christos     error (_("Can't attach to process %d: %s (%d)"),
   2037   1.1  christos 	   pid, safe_strerror (errno), errno);
   2038   1.1  christos 
   2039   1.1  christos   inf = current_inferior ();
   2040  1.10  christos   inferior_appeared (inf, pid);
   2041   1.1  christos   inf->attach_flag = true;
   2042   1.1  christos 
   2043   1.1  christos   darwin_attach_pid (inf);
   2044   1.1  christos 
   2045   1.1  christos   darwin_suspend_inferior (inf);
   2046   1.9  christos 
   2047   1.1  christos   init_thread_list (inf);
   2048   1.8  christos 
   2049   1.8  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   2050   1.8  christos 
   2051   1.1  christos   darwin_check_osabi (priv, inferior_ptid.tid ());
   2052   1.1  christos 
   2053   1.1  christos   darwin_setup_fake_stop_event (inf);
   2054   1.8  christos 
   2055   1.1  christos   priv->no_ptrace = 1;
   2056   1.1  christos }
   2057   1.1  christos 
   2058   1.1  christos /* Take a program previously attached to and detaches it.
   2059   1.1  christos    The program resumes execution and will no longer stop
   2060   1.1  christos    on signals, etc.  We'd better not have left any breakpoints
   2061   1.1  christos    in the program or it'll die when it hits one.  For this
   2062   1.1  christos    to work, it may be necessary for the process to have been
   2063   1.1  christos    previously attached.  It *might* work if the program was
   2064   1.8  christos    started via fork.  */
   2065   1.8  christos 
   2066   1.8  christos void
   2067   1.1  christos darwin_nat_target::detach (inferior *inf, int from_tty)
   2068   1.8  christos {
   2069   1.1  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   2070   1.1  christos   kern_return_t kret;
   2071   1.1  christos   int res;
   2072   1.1  christos 
   2073   1.6  christos   /* Display message.  */
   2074   1.1  christos   target_announce_detach (from_tty);
   2075   1.1  christos 
   2076   1.8  christos   /* If ptrace() is in use, stop the process.  */
   2077   1.9  christos   if (!priv->no_ptrace)
   2078   1.1  christos     stop_inferior (inf);
   2079   1.8  christos 
   2080   1.1  christos   kret = darwin_restore_exception_ports (priv);
   2081   1.1  christos   MACH_CHECK_ERROR (kret);
   2082   1.8  christos 
   2083   1.1  christos   if (!priv->no_ptrace)
   2084   1.1  christos     {
   2085   1.1  christos       res = PTRACE (PT_DETACH, inf->pid, 0, 0);
   2086  1.10  christos       if (res != 0)
   2087  1.10  christos 	warning (_("Unable to detach from process-id %d: %s (%d)"),
   2088   1.1  christos 		 inf->pid, safe_strerror (errno), errno);
   2089   1.1  christos     }
   2090   1.1  christos 
   2091   1.1  christos   darwin_reply_to_all_pending_messages (inf);
   2092   1.1  christos 
   2093   1.1  christos   /* When using ptrace, we have just performed a PT_DETACH, which
   2094   1.1  christos      resumes the inferior.  On the other hand, when we are not using
   2095   1.8  christos      ptrace, we need to resume its execution ourselves.  */
   2096   1.1  christos   if (priv->no_ptrace)
   2097   1.1  christos     darwin_resume_inferior (inf);
   2098   1.8  christos 
   2099   1.1  christos   mourn_inferior ();
   2100   1.1  christos }
   2101   1.9  christos 
   2102   1.8  christos std::string
   2103   1.1  christos darwin_nat_target::pid_to_str (ptid_t ptid)
   2104   1.8  christos {
   2105   1.1  christos   long tid = ptid.tid ();
   2106   1.1  christos 
   2107   1.9  christos   if (tid != 0)
   2108   1.9  christos     return string_printf (_("Thread 0x%lx of process %u"),
   2109   1.1  christos 			  tid, ptid.pid ());
   2110   1.1  christos 
   2111   1.1  christos   return normal_pid_to_str (ptid);
   2112   1.1  christos }
   2113   1.8  christos 
   2114   1.8  christos bool
   2115   1.1  christos darwin_nat_target::thread_alive (ptid_t ptid)
   2116   1.8  christos {
   2117   1.1  christos   return true;
   2118   1.1  christos }
   2119   1.1  christos 
   2120   1.1  christos /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
   2121   1.1  christos    copy it to RDADDR in gdb's address space.
   2122   1.1  christos    If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
   2123   1.9  christos    to ADDR in inferior task's address space.
   2124   1.6  christos    Return 0 on failure; number of bytes read / written otherwise.  */
   2125   1.1  christos 
   2126   1.1  christos static int
   2127   1.1  christos darwin_read_write_inferior (task_t task, CORE_ADDR addr,
   2128   1.3  christos 			    gdb_byte *rdaddr, const gdb_byte *wraddr,
   2129   1.1  christos 			    ULONGEST length)
   2130   1.1  christos {
   2131   1.6  christos   kern_return_t kret;
   2132   1.1  christos   mach_vm_size_t res_length = 0;
   2133   1.3  christos 
   2134   1.3  christos   inferior_debug (8, _("darwin_read_write_inferior(task=0x%x, %s, len=%s)\n"),
   2135   1.1  christos 		  task, core_addr_to_string (addr), pulongest (length));
   2136   1.6  christos 
   2137   1.6  christos   /* First read.  */
   2138   1.1  christos   if (rdaddr != NULL)
   2139   1.6  christos     {
   2140   1.1  christos       mach_vm_size_t count;
   2141   1.6  christos 
   2142   1.6  christos       /* According to target.h(to_xfer_partial), one and only one may be
   2143   1.6  christos 	 non-null.  */
   2144   1.1  christos       gdb_assert (wraddr == NULL);
   2145   1.6  christos 
   2146   1.6  christos       kret = mach_vm_read_overwrite (task, addr, length,
   2147   1.6  christos 				     (mach_vm_address_t) rdaddr, &count);
   2148   1.6  christos       if (kret != KERN_SUCCESS)
   2149   1.6  christos 	{
   2150   1.6  christos 	  inferior_debug
   2151   1.6  christos 	    (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
   2152   1.6  christos 	     core_addr_to_string (addr), mach_error_string (kret));
   2153   1.6  christos 	  return 0;
   2154   1.6  christos 	}
   2155   1.6  christos       return count;
   2156   1.1  christos     }
   2157   1.6  christos 
   2158   1.6  christos   /* See above.  */
   2159   1.1  christos   gdb_assert (wraddr != NULL);
   2160   1.6  christos 
   2161   1.1  christos   while (length != 0)
   2162   1.6  christos     {
   2163   1.6  christos       mach_vm_address_t offset = addr & (mach_page_size - 1);
   2164   1.6  christos       mach_vm_address_t region_address = (mach_vm_address_t) (addr - offset);
   2165   1.6  christos       mach_vm_size_t aligned_length =
   2166   1.1  christos 	(mach_vm_size_t) PAGE_ROUND (offset + length);
   2167   1.6  christos       vm_region_submap_short_info_data_64_t info;
   2168   1.6  christos       mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
   2169   1.1  christos       natural_t region_depth = 1000;
   2170   1.6  christos       mach_vm_address_t region_start = region_address;
   2171   1.6  christos       mach_vm_size_t region_length;
   2172   1.1  christos       mach_vm_size_t write_length;
   2173   1.6  christos 
   2174   1.1  christos       /* Read page protection.  */
   2175   1.1  christos       kret = mach_vm_region_recurse
   2176   1.1  christos 	(task, &region_start, &region_length, &region_depth,
   2177   1.1  christos 	 (vm_region_recurse_info_t) &info, &count);
   2178   1.1  christos 
   2179   1.1  christos       if (kret != KERN_SUCCESS)
   2180   1.1  christos 	{
   2181   1.1  christos 	  inferior_debug (1, _("darwin_read_write_inferior: "
   2182   1.1  christos 			       "mach_vm_region_recurse failed at %s: %s\n"),
   2183   1.1  christos 			  core_addr_to_string (region_address),
   2184   1.6  christos 			  mach_error_string (kret));
   2185   1.1  christos 	  return res_length;
   2186   1.1  christos 	}
   2187   1.1  christos 
   2188   1.1  christos       inferior_debug
   2189   1.1  christos 	(9, _("darwin_read_write_inferior: "
   2190   1.1  christos 	      "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
   2191   1.1  christos 	 core_addr_to_string (region_address),
   2192   1.1  christos 	 core_addr_to_string (region_start),
   2193   1.1  christos 	 core_addr_to_string (region_length));
   2194   1.1  christos 
   2195   1.1  christos       /* Check for holes in memory.  */
   2196   1.1  christos       if (region_start > region_address)
   2197   1.1  christos 	{
   2198   1.1  christos 	  warning (_("No memory at %s (vs %s+0x%x).  Nothing written"),
   2199   1.1  christos 		   core_addr_to_string (region_address),
   2200   1.1  christos 		   core_addr_to_string (region_start),
   2201   1.6  christos 		   (unsigned)region_length);
   2202   1.1  christos 	  return res_length;
   2203   1.1  christos 	}
   2204   1.1  christos 
   2205   1.1  christos       /* Adjust the length.  */
   2206   1.6  christos       region_length -= (region_address - region_start);
   2207   1.6  christos       if (region_length > aligned_length)
   2208   1.1  christos 	region_length = aligned_length;
   2209   1.6  christos 
   2210   1.6  christos       /* Make the pages RW.  */
   2211   1.1  christos       if (!(info.protection & VM_PROT_WRITE))
   2212   1.6  christos 	{
   2213   1.6  christos 	  vm_prot_t prot = VM_PROT_READ | VM_PROT_WRITE;
   2214   1.6  christos 
   2215   1.6  christos 	  kret = mach_vm_protect (task, region_address, region_length,
   2216   1.1  christos 				  FALSE, prot);
   2217   1.1  christos 	  if (kret != KERN_SUCCESS)
   2218   1.6  christos 	    {
   2219   1.6  christos 	      prot |= VM_PROT_COPY;
   2220   1.6  christos 	      kret = mach_vm_protect (task, region_address, region_length,
   2221   1.6  christos 				      FALSE, prot);
   2222   1.6  christos 	    }
   2223   1.6  christos 	  if (kret != KERN_SUCCESS)
   2224   1.6  christos 	    {
   2225   1.6  christos 	      warning (_("darwin_read_write_inferior: "
   2226   1.6  christos 			 "mach_vm_protect failed at %s "
   2227   1.1  christos 			 "(len=0x%lx, prot=0x%x): %s"),
   2228   1.6  christos 		       core_addr_to_string (region_address),
   2229   1.1  christos 		       (unsigned long) region_length, (unsigned) prot,
   2230   1.6  christos 		       mach_error_string (kret));
   2231   1.1  christos 	      return res_length;
   2232   1.1  christos 	    }
   2233   1.1  christos 	}
   2234   1.6  christos 
   2235   1.6  christos       if (offset + length > region_length)
   2236   1.6  christos 	write_length = region_length - offset;
   2237   1.6  christos       else
   2238   1.6  christos 	write_length = length;
   2239   1.6  christos 
   2240   1.6  christos       /* Write.  */
   2241   1.6  christos       kret = mach_vm_write (task, addr, (vm_offset_t) wraddr, write_length);
   2242   1.6  christos       if (kret != KERN_SUCCESS)
   2243   1.6  christos 	{
   2244   1.6  christos 	  warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
   2245   1.6  christos 		   mach_error_string (kret));
   2246   1.6  christos 	  return res_length;
   2247   1.6  christos 	}
   2248   1.6  christos 
   2249   1.1  christos       /* Restore page rights.  */
   2250   1.1  christos       if (!(info.protection & VM_PROT_WRITE))
   2251   1.1  christos 	{
   2252   1.6  christos 	  kret = mach_vm_protect (task, region_address, region_length,
   2253   1.1  christos 				  FALSE, info.protection);
   2254   1.1  christos 	  if (kret != KERN_SUCCESS)
   2255   1.6  christos 	    {
   2256   1.6  christos 	      warning (_("darwin_read_write_inferior: "
   2257   1.6  christos 			 "mach_vm_protect restore failed at %s "
   2258   1.1  christos 			 "(len=0x%lx): %s"),
   2259   1.6  christos 		       core_addr_to_string (region_address),
   2260   1.6  christos 		       (unsigned long) region_length,
   2261   1.1  christos 		       mach_error_string (kret));
   2262   1.1  christos 	    }
   2263   1.6  christos 	}
   2264   1.6  christos 
   2265   1.6  christos       addr += write_length;
   2266   1.6  christos       wraddr += write_length;
   2267   1.6  christos       res_length += write_length;
   2268   1.1  christos       length -= write_length;
   2269   1.1  christos     }
   2270   1.6  christos 
   2271   1.1  christos   return res_length;
   2272   1.1  christos }
   2273   1.1  christos 
   2274   1.6  christos /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
   2275   1.1  christos    to RDADDR (in big endian).
   2276   1.1  christos    Return 0 on failure; number of bytes read / written otherwise.  */
   2277   1.1  christos 
   2278   1.1  christos #ifdef TASK_DYLD_INFO_COUNT
   2279   1.3  christos /* This is not available in Darwin 9.  */
   2280   1.1  christos static enum target_xfer_status
   2281   1.3  christos darwin_read_dyld_info (task_t task, CORE_ADDR addr, gdb_byte *rdaddr,
   2282   1.1  christos 		       ULONGEST length, ULONGEST *xfered_len)
   2283   1.1  christos {
   2284   1.1  christos   struct task_dyld_info task_dyld_info;
   2285   1.1  christos   mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
   2286   1.1  christos   kern_return_t kret;
   2287   1.6  christos 
   2288   1.3  christos   if (addr != 0 || length > sizeof (mach_vm_address_t))
   2289   1.1  christos     return TARGET_XFER_EOF;
   2290   1.6  christos 
   2291   1.6  christos   kret = task_info (task, TASK_DYLD_INFO,
   2292   1.1  christos 		    (task_info_t) &task_dyld_info, &count);
   2293   1.1  christos   MACH_CHECK_ERROR (kret);
   2294   1.3  christos   if (kret != KERN_SUCCESS)
   2295   1.6  christos     return TARGET_XFER_E_IO;
   2296   1.6  christos 
   2297   1.6  christos   store_unsigned_integer (rdaddr, length, BFD_ENDIAN_BIG,
   2298   1.3  christos 			  task_dyld_info.all_image_info_addr);
   2299   1.3  christos   *xfered_len = (ULONGEST) length;
   2300   1.1  christos   return TARGET_XFER_OK;
   2301   1.1  christos }
   2302   1.1  christos #endif
   2303   1.1  christos 
   2304   1.1  christos 
   2305   1.8  christos 
   2307   1.8  christos enum target_xfer_status
   2308   1.8  christos darwin_nat_target::xfer_partial (enum target_object object, const char *annex,
   2309   1.8  christos 				 gdb_byte *readbuf, const gdb_byte *writebuf,
   2310   1.1  christos 				 ULONGEST offset, ULONGEST len,
   2311   1.1  christos 				 ULONGEST *xfered_len)
   2312   1.8  christos {
   2313   1.1  christos   struct inferior *inf = current_inferior ();
   2314   1.1  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   2315   1.3  christos 
   2316   1.3  christos   inferior_debug
   2317   1.1  christos     (8, _("darwin_xfer_partial(%s, %s, rbuf=%s, wbuf=%s) pid=%u\n"),
   2318   1.1  christos      core_addr_to_string (offset), pulongest (len),
   2319   1.1  christos      host_address_to_string (readbuf), host_address_to_string (writebuf),
   2320   1.1  christos      inf->pid);
   2321   1.1  christos 
   2322   1.1  christos   switch (object)
   2323   1.3  christos     {
   2324   1.8  christos     case TARGET_OBJECT_MEMORY:
   2325   1.3  christos       {
   2326   1.3  christos 	int l = darwin_read_write_inferior (priv->task, offset,
   2327   1.3  christos 					    readbuf, writebuf, len);
   2328   1.3  christos 
   2329   1.3  christos 	if (l == 0)
   2330   1.3  christos 	  return TARGET_XFER_EOF;
   2331   1.3  christos 	else
   2332   1.3  christos 	  {
   2333   1.3  christos 	    gdb_assert (l > 0);
   2334   1.3  christos 	    *xfered_len = (ULONGEST) l;
   2335   1.3  christos 	    return TARGET_XFER_OK;
   2336   1.1  christos 	  }
   2337   1.1  christos       }
   2338   1.1  christos #ifdef TASK_DYLD_INFO_COUNT
   2339  1.10  christos     case TARGET_OBJECT_DARWIN_DYLD_INFO:
   2340  1.10  christos       if (writebuf != NULL || readbuf == NULL)
   2341  1.10  christos 	{
   2342  1.10  christos 	  /* Support only read.  */
   2343   1.8  christos 	  return TARGET_XFER_E_IO;
   2344   1.3  christos 	}
   2345   1.1  christos       return darwin_read_dyld_info (priv->task, offset, readbuf, len,
   2346   1.1  christos 				    xfered_len);
   2347   1.3  christos #endif
   2348   1.1  christos     default:
   2349   1.1  christos       return TARGET_XFER_E_IO;
   2350   1.1  christos     }
   2351   1.1  christos 
   2352   1.1  christos }
   2353   1.8  christos 
   2354   1.1  christos static void
   2355   1.1  christos set_enable_mach_exceptions (const char *args, int from_tty,
   2356   1.8  christos 			    struct cmd_list_element *c)
   2357   1.1  christos {
   2358   1.1  christos   if (inferior_ptid != null_ptid)
   2359   1.8  christos     {
   2360   1.1  christos       struct inferior *inf = current_inferior ();
   2361   1.1  christos       darwin_inferior *priv = get_darwin_inferior (inf);
   2362   1.1  christos       exception_mask_t mask;
   2363   1.1  christos       kern_return_t kret;
   2364   1.1  christos 
   2365   1.1  christos       if (enable_mach_exceptions)
   2366   1.1  christos 	mask = EXC_MASK_ALL;
   2367   1.8  christos       else
   2368   1.1  christos 	{
   2369   1.1  christos 	  darwin_restore_exception_ports (priv);
   2370   1.8  christos 	  mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
   2371   1.1  christos 	}
   2372   1.1  christos       kret = task_set_exception_ports (priv->task, mask, darwin_ex_port,
   2373   1.1  christos 				       EXCEPTION_DEFAULT, THREAD_STATE_NONE);
   2374   1.1  christos       MACH_CHECK_ERROR (kret);
   2375   1.1  christos     }
   2376  1.10  christos }
   2377   1.8  christos 
   2378   1.1  christos const char *
   2379   1.3  christos darwin_nat_target::pid_to_exec_file (int pid)
   2380   1.1  christos {
   2381   1.1  christos   static char path[PATH_MAX];
   2382   1.1  christos   int res;
   2383   1.1  christos 
   2384   1.1  christos   res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, PATH_MAX);
   2385   1.1  christos   if (res >= 0)
   2386   1.1  christos     return path;
   2387   1.1  christos   else
   2388   1.1  christos     return NULL;
   2389   1.8  christos }
   2390  1.10  christos 
   2391   1.1  christos ptid_t
   2392   1.1  christos darwin_nat_target::get_ada_task_ptid (long lwp, ULONGEST thread)
   2393   1.8  christos {
   2394   1.1  christos   struct inferior *inf = current_inferior ();
   2395   1.1  christos   darwin_inferior *priv = get_darwin_inferior (inf);
   2396   1.1  christos   kern_return_t kret;
   2397   1.1  christos   mach_port_name_array_t names;
   2398   1.1  christos   mach_msg_type_number_t names_count;
   2399   1.1  christos   mach_port_type_array_t types;
   2400   1.1  christos   mach_msg_type_number_t types_count;
   2401   1.1  christos   long res = 0;
   2402   1.8  christos 
   2403   1.8  christos   /* First linear search.  */
   2404   1.8  christos   for (darwin_thread_t *t : priv->threads)
   2405   1.8  christos     {
   2406   1.8  christos       if (t->inf_port == lwp)
   2407   1.1  christos 	return ptid_t (inferior_ptid.pid (), 0, t->gdb_port);
   2408   1.1  christos     }
   2409   1.1  christos 
   2410   1.1  christos   /* Maybe the port was never extract.  Do it now.  */
   2411   1.8  christos 
   2412   1.1  christos   /* First get inferior port names.  */
   2413   1.1  christos   kret = mach_port_names (priv->task, &names, &names_count, &types,
   2414   1.1  christos 			  &types_count);
   2415   1.1  christos   MACH_CHECK_ERROR (kret);
   2416   1.1  christos   if (kret != KERN_SUCCESS)
   2417   1.1  christos     return null_ptid;
   2418   1.1  christos 
   2419   1.1  christos   /* For each name, copy the right in the gdb space and then compare with
   2420   1.8  christos      our view of the inferior threads.  We don't forget to deallocate the
   2421   1.1  christos      right.  */
   2422   1.1  christos   for (int i = 0; i < names_count; i++)
   2423   1.1  christos     {
   2424   1.1  christos       mach_port_t local_name;
   2425   1.1  christos       mach_msg_type_name_t local_type;
   2426   1.1  christos 
   2427   1.8  christos       /* We just need to know the corresponding name in gdb name space.
   2428   1.1  christos 	 So extract and deallocate the right.  */
   2429   1.1  christos       kret = mach_port_extract_right (priv->task, names[i],
   2430   1.1  christos 				      MACH_MSG_TYPE_COPY_SEND,
   2431   1.1  christos 				      &local_name, &local_type);
   2432   1.1  christos       if (kret != KERN_SUCCESS)
   2433   1.1  christos 	continue;
   2434   1.8  christos       mach_port_deallocate (gdb_task, local_name);
   2435   1.8  christos 
   2436   1.8  christos       for (darwin_thread_t *t : priv->threads)
   2437   1.8  christos 	{
   2438   1.8  christos 	  if (t->gdb_port == local_name)
   2439   1.8  christos 	    {
   2440   1.8  christos 	      t->inf_port = names[i];
   2441   1.8  christos 	      if (names[i] == lwp)
   2442   1.8  christos 		res = t->gdb_port;
   2443   1.1  christos 	    }
   2444   1.1  christos 	}
   2445   1.1  christos     }
   2446  1.10  christos 
   2447   1.1  christos   vm_deallocate (gdb_task, (vm_address_t) names,
   2448   1.1  christos 		 names_count * sizeof (mach_port_t));
   2449   1.9  christos 
   2450   1.1  christos   if (res)
   2451   1.1  christos     return ptid_t (current_inferior ()->pid, 0, res);
   2452   1.1  christos   else
   2453   1.1  christos     return null_ptid;
   2454   1.8  christos }
   2455   1.8  christos 
   2456   1.1  christos bool
   2457   1.8  christos darwin_nat_target::supports_multi_process ()
   2458   1.1  christos {
   2459   1.1  christos   return true;
   2460   1.9  christos }
   2461   1.1  christos 
   2462   1.8  christos void _initialize_darwin_nat ();
   2463   1.1  christos void
   2464   1.1  christos _initialize_darwin_nat ()
   2465   1.1  christos {
   2466   1.1  christos   kern_return_t kret;
   2467   1.1  christos 
   2468   1.1  christos   gdb_task = mach_task_self ();
   2469   1.1  christos   darwin_host_self = mach_host_self ();
   2470   1.1  christos 
   2471   1.1  christos   /* Read page size.  */
   2472   1.1  christos   kret = host_page_size (darwin_host_self, &mach_page_size);
   2473   1.1  christos   if (kret != KERN_SUCCESS)
   2474   1.1  christos     {
   2475   1.1  christos       mach_page_size = 0x1000;
   2476   1.1  christos       MACH_CHECK_ERROR (kret);
   2477   1.6  christos     }
   2478   1.6  christos 
   2479   1.1  christos   inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"),
   2480   1.1  christos 		  (unsigned long) mach_task_self (), getpid ());
   2481   1.1  christos 
   2482   1.1  christos   add_setshow_zuinteger_cmd ("darwin", class_obscure,
   2483   1.1  christos 			     &darwin_debug_flag, _("\
   2484   1.1  christos Set if printing inferior communication debugging statements."), _("\
   2485   1.1  christos Show if printing inferior communication debugging statements."), NULL,
   2486   1.1  christos 			     NULL, NULL,
   2487   1.1  christos 			     &setdebuglist, &showdebuglist);
   2488   1.1  christos 
   2489   1.1  christos   add_setshow_boolean_cmd ("mach-exceptions", class_support,
   2490   1.1  christos 			   &enable_mach_exceptions, _("\
   2491   1.1  christos Set if mach exceptions are caught."), _("\
   2492   1.1  christos Show if mach exceptions are caught."), _("\
   2493   1.1  christos When this mode is on, all low level exceptions are reported before being\n\
   2494   1.1  christos reported by the kernel."),
   2495   1.1  christos 			   &set_enable_mach_exceptions, NULL,
   2496                 			   &setlist, &showlist);
   2497                 }
   2498