Home | History | Annotate | Line # | Download | only in gdb
i386-darwin-nat.c revision 1.3
      1  1.1  christos /* Darwin support for GDB, the GNU debugger.
      2  1.3  christos    Copyright (C) 1997-2015 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    Contributed by Apple Computer, Inc.
      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 "frame.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 "regcache.h"
     30  1.1  christos #include "i386-tdep.h"
     31  1.1  christos #include "i387-tdep.h"
     32  1.1  christos #include "gdbarch.h"
     33  1.1  christos #include "arch-utils.h"
     34  1.1  christos #include "gdbcore.h"
     35  1.1  christos 
     36  1.3  christos #include "x86-nat.h"
     37  1.1  christos #include "darwin-nat.h"
     38  1.1  christos #include "i386-darwin-tdep.h"
     39  1.1  christos 
     40  1.1  christos #ifdef BFD64
     41  1.1  christos #include "amd64-nat.h"
     42  1.1  christos #include "amd64-tdep.h"
     43  1.1  christos #include "amd64-darwin-tdep.h"
     44  1.1  christos #endif
     45  1.1  christos 
     46  1.1  christos /* Read register values from the inferior process.
     47  1.1  christos    If REGNO is -1, do this for all registers.
     48  1.1  christos    Otherwise, REGNO specifies which register (so we can save time).  */
     49  1.1  christos static void
     50  1.1  christos i386_darwin_fetch_inferior_registers (struct target_ops *ops,
     51  1.1  christos 				      struct regcache *regcache, int regno)
     52  1.1  christos {
     53  1.1  christos   thread_t current_thread = ptid_get_tid (inferior_ptid);
     54  1.1  christos   int fetched = 0;
     55  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
     56  1.1  christos 
     57  1.1  christos #ifdef BFD64
     58  1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 64)
     59  1.1  christos     {
     60  1.1  christos       if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
     61  1.1  christos         {
     62  1.1  christos           x86_thread_state_t gp_regs;
     63  1.1  christos           unsigned int gp_count = x86_THREAD_STATE_COUNT;
     64  1.1  christos           kern_return_t ret;
     65  1.1  christos 
     66  1.1  christos 	  ret = thread_get_state
     67  1.1  christos             (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
     68  1.1  christos              &gp_count);
     69  1.1  christos 	  if (ret != KERN_SUCCESS)
     70  1.1  christos 	    {
     71  1.1  christos 	      printf_unfiltered (_("Error calling thread_get_state for "
     72  1.1  christos 				   "GP registers for thread 0x%lx\n"),
     73  1.1  christos 				 (unsigned long) current_thread);
     74  1.1  christos 	      MACH_CHECK_ERROR (ret);
     75  1.1  christos 	    }
     76  1.3  christos 
     77  1.3  christos 	  /* Some kernels don't sanitize the values.  */
     78  1.3  christos 	  gp_regs.uts.ts64.__fs &= 0xffff;
     79  1.3  christos 	  gp_regs.uts.ts64.__gs &= 0xffff;
     80  1.3  christos 
     81  1.1  christos 	  amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
     82  1.1  christos           fetched++;
     83  1.1  christos         }
     84  1.1  christos 
     85  1.1  christos       if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
     86  1.1  christos         {
     87  1.1  christos           x86_float_state_t fp_regs;
     88  1.1  christos           unsigned int fp_count = x86_FLOAT_STATE_COUNT;
     89  1.1  christos           kern_return_t ret;
     90  1.1  christos 
     91  1.1  christos 	  ret = thread_get_state
     92  1.1  christos             (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
     93  1.1  christos              &fp_count);
     94  1.1  christos 	  if (ret != KERN_SUCCESS)
     95  1.1  christos 	    {
     96  1.1  christos 	      printf_unfiltered (_("Error calling thread_get_state for "
     97  1.1  christos 				   "float registers for thread 0x%lx\n"),
     98  1.1  christos 				 (unsigned long) current_thread);
     99  1.1  christos 	      MACH_CHECK_ERROR (ret);
    100  1.1  christos 	    }
    101  1.1  christos           amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
    102  1.1  christos           fetched++;
    103  1.1  christos         }
    104  1.1  christos     }
    105  1.1  christos   else
    106  1.1  christos #endif
    107  1.1  christos     {
    108  1.1  christos       if (regno == -1 || regno < I386_NUM_GREGS)
    109  1.1  christos         {
    110  1.1  christos           x86_thread_state32_t gp_regs;
    111  1.1  christos           unsigned int gp_count = x86_THREAD_STATE32_COUNT;
    112  1.1  christos           kern_return_t ret;
    113  1.1  christos 	  int i;
    114  1.1  christos 
    115  1.1  christos 	  ret = thread_get_state
    116  1.1  christos             (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
    117  1.1  christos              &gp_count);
    118  1.1  christos 	  if (ret != KERN_SUCCESS)
    119  1.1  christos 	    {
    120  1.1  christos 	      printf_unfiltered (_("Error calling thread_get_state for "
    121  1.1  christos 				   "GP registers for thread 0x%lx\n"),
    122  1.1  christos 				 (unsigned long) current_thread);
    123  1.1  christos 	      MACH_CHECK_ERROR (ret);
    124  1.1  christos 	    }
    125  1.1  christos 	  for (i = 0; i < I386_NUM_GREGS; i++)
    126  1.1  christos 	    regcache_raw_supply
    127  1.1  christos 	      (regcache, i,
    128  1.1  christos 	       (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
    129  1.1  christos 
    130  1.1  christos           fetched++;
    131  1.1  christos         }
    132  1.1  christos 
    133  1.1  christos       if (regno == -1
    134  1.1  christos 	  || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
    135  1.1  christos         {
    136  1.1  christos           x86_float_state32_t fp_regs;
    137  1.1  christos           unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
    138  1.1  christos           kern_return_t ret;
    139  1.1  christos 
    140  1.1  christos 	  ret = thread_get_state
    141  1.1  christos             (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
    142  1.1  christos              &fp_count);
    143  1.1  christos 	  if (ret != KERN_SUCCESS)
    144  1.1  christos 	    {
    145  1.1  christos 	      printf_unfiltered (_("Error calling thread_get_state for "
    146  1.1  christos 				   "float registers for thread 0x%lx\n"),
    147  1.1  christos 				 (unsigned long) current_thread);
    148  1.1  christos 	      MACH_CHECK_ERROR (ret);
    149  1.1  christos 	    }
    150  1.1  christos           i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
    151  1.1  christos           fetched++;
    152  1.1  christos         }
    153  1.1  christos     }
    154  1.1  christos 
    155  1.1  christos   if (! fetched)
    156  1.1  christos     {
    157  1.1  christos       warning (_("unknown register %d"), regno);
    158  1.1  christos       regcache_raw_supply (regcache, regno, NULL);
    159  1.1  christos     }
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos /* Store our register values back into the inferior.
    163  1.1  christos    If REGNO is -1, do this for all registers.
    164  1.1  christos    Otherwise, REGNO specifies which register (so we can save time).  */
    165  1.1  christos 
    166  1.1  christos static void
    167  1.1  christos i386_darwin_store_inferior_registers (struct target_ops *ops,
    168  1.1  christos 				      struct regcache *regcache, int regno)
    169  1.1  christos {
    170  1.1  christos   thread_t current_thread = ptid_get_tid (inferior_ptid);
    171  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    172  1.1  christos 
    173  1.1  christos #ifdef BFD64
    174  1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 64)
    175  1.1  christos     {
    176  1.1  christos       if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
    177  1.1  christos         {
    178  1.1  christos           x86_thread_state_t gp_regs;
    179  1.1  christos           kern_return_t ret;
    180  1.1  christos 	  unsigned int gp_count = x86_THREAD_STATE_COUNT;
    181  1.1  christos 
    182  1.1  christos 	  ret = thread_get_state
    183  1.1  christos 	    (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
    184  1.1  christos 	     &gp_count);
    185  1.1  christos           MACH_CHECK_ERROR (ret);
    186  1.1  christos 	  gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
    187  1.1  christos           gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
    188  1.1  christos 
    189  1.1  christos 	  amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
    190  1.1  christos 
    191  1.3  christos 	  /* Some kernels don't sanitize the values.  */
    192  1.3  christos 	  gp_regs.uts.ts64.__fs &= 0xffff;
    193  1.3  christos 	  gp_regs.uts.ts64.__gs &= 0xffff;
    194  1.3  christos 
    195  1.1  christos           ret = thread_set_state (current_thread, x86_THREAD_STATE,
    196  1.1  christos                                   (thread_state_t) &gp_regs,
    197  1.1  christos                                   x86_THREAD_STATE_COUNT);
    198  1.1  christos           MACH_CHECK_ERROR (ret);
    199  1.1  christos         }
    200  1.1  christos 
    201  1.1  christos       if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
    202  1.1  christos         {
    203  1.1  christos           x86_float_state_t fp_regs;
    204  1.1  christos           kern_return_t ret;
    205  1.1  christos 	  unsigned int fp_count = x86_FLOAT_STATE_COUNT;
    206  1.1  christos 
    207  1.1  christos 	  ret = thread_get_state
    208  1.1  christos 	    (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
    209  1.1  christos 	     &fp_count);
    210  1.1  christos           MACH_CHECK_ERROR (ret);
    211  1.1  christos           gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
    212  1.1  christos           gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
    213  1.1  christos 
    214  1.1  christos 	  amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
    215  1.1  christos 
    216  1.1  christos 	  ret = thread_set_state (current_thread, x86_FLOAT_STATE,
    217  1.1  christos 				  (thread_state_t) & fp_regs,
    218  1.1  christos 				  x86_FLOAT_STATE_COUNT);
    219  1.1  christos 	  MACH_CHECK_ERROR (ret);
    220  1.1  christos         }
    221  1.1  christos     }
    222  1.1  christos   else
    223  1.1  christos #endif
    224  1.1  christos     {
    225  1.1  christos       if (regno == -1 || regno < I386_NUM_GREGS)
    226  1.1  christos         {
    227  1.1  christos           x86_thread_state32_t gp_regs;
    228  1.1  christos           kern_return_t ret;
    229  1.1  christos           unsigned int gp_count = x86_THREAD_STATE32_COUNT;
    230  1.1  christos 	  int i;
    231  1.1  christos 
    232  1.1  christos           ret = thread_get_state
    233  1.1  christos             (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
    234  1.1  christos              &gp_count);
    235  1.1  christos 	  MACH_CHECK_ERROR (ret);
    236  1.1  christos 
    237  1.1  christos 	  for (i = 0; i < I386_NUM_GREGS; i++)
    238  1.1  christos 	    if (regno == -1 || regno == i)
    239  1.1  christos 	      regcache_raw_collect
    240  1.1  christos 		(regcache, i,
    241  1.1  christos 		 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
    242  1.1  christos 
    243  1.1  christos           ret = thread_set_state (current_thread, x86_THREAD_STATE32,
    244  1.1  christos                                   (thread_state_t) &gp_regs,
    245  1.1  christos                                   x86_THREAD_STATE32_COUNT);
    246  1.1  christos           MACH_CHECK_ERROR (ret);
    247  1.1  christos         }
    248  1.1  christos 
    249  1.1  christos       if (regno == -1
    250  1.1  christos 	  || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
    251  1.1  christos         {
    252  1.1  christos           x86_float_state32_t fp_regs;
    253  1.1  christos           unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
    254  1.1  christos           kern_return_t ret;
    255  1.1  christos 
    256  1.1  christos 	  ret = thread_get_state
    257  1.1  christos             (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
    258  1.1  christos              &fp_count);
    259  1.1  christos 	  MACH_CHECK_ERROR (ret);
    260  1.1  christos 
    261  1.1  christos 	  i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
    262  1.1  christos 
    263  1.1  christos 	  ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
    264  1.1  christos 				  (thread_state_t) &fp_regs,
    265  1.1  christos 				  x86_FLOAT_STATE32_COUNT);
    266  1.1  christos 	  MACH_CHECK_ERROR (ret);
    267  1.1  christos         }
    268  1.1  christos     }
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos /* Support for debug registers, boosted mostly from i386-linux-nat.c.  */
    272  1.1  christos 
    273  1.1  christos static void
    274  1.1  christos i386_darwin_dr_set (int regnum, CORE_ADDR value)
    275  1.1  christos {
    276  1.1  christos   int current_pid;
    277  1.1  christos   thread_t current_thread;
    278  1.1  christos   x86_debug_state_t dr_regs;
    279  1.1  christos   kern_return_t ret;
    280  1.1  christos   unsigned int dr_count;
    281  1.1  christos 
    282  1.1  christos   gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
    283  1.1  christos 
    284  1.1  christos   current_thread = ptid_get_tid (inferior_ptid);
    285  1.1  christos 
    286  1.1  christos   dr_regs.dsh.flavor = x86_DEBUG_STATE;
    287  1.1  christos   dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
    288  1.1  christos   dr_count = x86_DEBUG_STATE_COUNT;
    289  1.1  christos   ret = thread_get_state (current_thread, x86_DEBUG_STATE,
    290  1.1  christos                           (thread_state_t) &dr_regs, &dr_count);
    291  1.1  christos   MACH_CHECK_ERROR (ret);
    292  1.1  christos 
    293  1.1  christos   switch (dr_regs.dsh.flavor)
    294  1.1  christos     {
    295  1.1  christos     case x86_DEBUG_STATE32:
    296  1.1  christos       switch (regnum)
    297  1.1  christos 	{
    298  1.1  christos 	case 0:
    299  1.1  christos 	  dr_regs.uds.ds32.__dr0 = value;
    300  1.1  christos 	  break;
    301  1.1  christos 	case 1:
    302  1.1  christos 	  dr_regs.uds.ds32.__dr1 = value;
    303  1.1  christos 	  break;
    304  1.1  christos 	case 2:
    305  1.1  christos 	  dr_regs.uds.ds32.__dr2 = value;
    306  1.1  christos 	  break;
    307  1.1  christos 	case 3:
    308  1.1  christos 	  dr_regs.uds.ds32.__dr3 = value;
    309  1.1  christos 	  break;
    310  1.1  christos 	case 4:
    311  1.1  christos 	  dr_regs.uds.ds32.__dr4 = value;
    312  1.1  christos 	  break;
    313  1.1  christos 	case 5:
    314  1.1  christos 	  dr_regs.uds.ds32.__dr5 = value;
    315  1.1  christos 	  break;
    316  1.1  christos 	case 6:
    317  1.1  christos 	  dr_regs.uds.ds32.__dr6 = value;
    318  1.1  christos 	  break;
    319  1.1  christos 	case 7:
    320  1.1  christos 	  dr_regs.uds.ds32.__dr7 = value;
    321  1.1  christos 	  break;
    322  1.1  christos 	}
    323  1.1  christos       break;
    324  1.1  christos #ifdef BFD64
    325  1.1  christos     case x86_DEBUG_STATE64:
    326  1.1  christos       switch (regnum)
    327  1.1  christos 	{
    328  1.1  christos 	case 0:
    329  1.1  christos 	  dr_regs.uds.ds64.__dr0 = value;
    330  1.1  christos 	  break;
    331  1.1  christos 	case 1:
    332  1.1  christos 	  dr_regs.uds.ds64.__dr1 = value;
    333  1.1  christos 	  break;
    334  1.1  christos 	case 2:
    335  1.1  christos 	  dr_regs.uds.ds64.__dr2 = value;
    336  1.1  christos 	  break;
    337  1.1  christos 	case 3:
    338  1.1  christos 	  dr_regs.uds.ds64.__dr3 = value;
    339  1.1  christos 	  break;
    340  1.1  christos 	case 4:
    341  1.1  christos 	  dr_regs.uds.ds64.__dr4 = value;
    342  1.1  christos 	  break;
    343  1.1  christos 	case 5:
    344  1.1  christos 	  dr_regs.uds.ds64.__dr5 = value;
    345  1.1  christos 	  break;
    346  1.1  christos 	case 6:
    347  1.1  christos 	  dr_regs.uds.ds64.__dr6 = value;
    348  1.1  christos 	  break;
    349  1.1  christos 	case 7:
    350  1.1  christos 	  dr_regs.uds.ds64.__dr7 = value;
    351  1.1  christos 	  break;
    352  1.1  christos 	}
    353  1.1  christos       break;
    354  1.1  christos #endif
    355  1.1  christos     }
    356  1.1  christos 
    357  1.1  christos   ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
    358  1.1  christos                           (thread_state_t) &dr_regs.uds, dr_count);
    359  1.1  christos 
    360  1.1  christos   MACH_CHECK_ERROR (ret);
    361  1.1  christos }
    362  1.1  christos 
    363  1.1  christos static CORE_ADDR
    364  1.1  christos i386_darwin_dr_get (int regnum)
    365  1.1  christos {
    366  1.1  christos   thread_t current_thread;
    367  1.1  christos   x86_debug_state_t dr_regs;
    368  1.1  christos   kern_return_t ret;
    369  1.1  christos   unsigned int dr_count;
    370  1.1  christos 
    371  1.1  christos   gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
    372  1.1  christos 
    373  1.1  christos   current_thread = ptid_get_tid (inferior_ptid);
    374  1.1  christos 
    375  1.1  christos   dr_regs.dsh.flavor = x86_DEBUG_STATE;
    376  1.1  christos   dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
    377  1.1  christos   dr_count = x86_DEBUG_STATE_COUNT;
    378  1.1  christos   ret = thread_get_state (current_thread, x86_DEBUG_STATE,
    379  1.1  christos                           (thread_state_t) &dr_regs, &dr_count);
    380  1.1  christos   MACH_CHECK_ERROR (ret);
    381  1.1  christos 
    382  1.1  christos   switch (dr_regs.dsh.flavor)
    383  1.1  christos     {
    384  1.1  christos     case x86_DEBUG_STATE32:
    385  1.1  christos       switch (regnum)
    386  1.1  christos 	{
    387  1.1  christos 	case 0:
    388  1.1  christos 	  return dr_regs.uds.ds32.__dr0;
    389  1.1  christos 	case 1:
    390  1.1  christos 	  return dr_regs.uds.ds32.__dr1;
    391  1.1  christos 	case 2:
    392  1.1  christos 	  return dr_regs.uds.ds32.__dr2;
    393  1.1  christos 	case 3:
    394  1.1  christos 	  return dr_regs.uds.ds32.__dr3;
    395  1.1  christos 	case 4:
    396  1.1  christos 	  return dr_regs.uds.ds32.__dr4;
    397  1.1  christos 	case 5:
    398  1.1  christos 	  return dr_regs.uds.ds32.__dr5;
    399  1.1  christos 	case 6:
    400  1.1  christos 	  return dr_regs.uds.ds32.__dr6;
    401  1.1  christos 	case 7:
    402  1.1  christos 	  return dr_regs.uds.ds32.__dr7;
    403  1.1  christos 	default:
    404  1.1  christos 	  return -1;
    405  1.1  christos 	}
    406  1.1  christos       break;
    407  1.1  christos #ifdef BFD64
    408  1.1  christos     case x86_DEBUG_STATE64:
    409  1.1  christos       switch (regnum)
    410  1.1  christos 	{
    411  1.1  christos 	case 0:
    412  1.1  christos 	  return dr_regs.uds.ds64.__dr0;
    413  1.1  christos 	case 1:
    414  1.1  christos 	  return dr_regs.uds.ds64.__dr1;
    415  1.1  christos 	case 2:
    416  1.1  christos 	  return dr_regs.uds.ds64.__dr2;
    417  1.1  christos 	case 3:
    418  1.1  christos 	  return dr_regs.uds.ds64.__dr3;
    419  1.1  christos 	case 4:
    420  1.1  christos 	  return dr_regs.uds.ds64.__dr4;
    421  1.1  christos 	case 5:
    422  1.1  christos 	  return dr_regs.uds.ds64.__dr5;
    423  1.1  christos 	case 6:
    424  1.1  christos 	  return dr_regs.uds.ds64.__dr6;
    425  1.1  christos 	case 7:
    426  1.1  christos 	  return dr_regs.uds.ds64.__dr7;
    427  1.1  christos 	default:
    428  1.1  christos 	  return -1;
    429  1.1  christos 	}
    430  1.1  christos       break;
    431  1.1  christos #endif
    432  1.1  christos     default:
    433  1.1  christos       return -1;
    434  1.1  christos     }
    435  1.1  christos }
    436  1.1  christos 
    437  1.1  christos static void
    438  1.1  christos i386_darwin_dr_set_control (unsigned long control)
    439  1.1  christos {
    440  1.1  christos   i386_darwin_dr_set (DR_CONTROL, control);
    441  1.1  christos }
    442  1.1  christos 
    443  1.1  christos static void
    444  1.1  christos i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
    445  1.1  christos {
    446  1.1  christos   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
    447  1.1  christos 
    448  1.1  christos   i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
    449  1.1  christos }
    450  1.1  christos 
    451  1.1  christos static CORE_ADDR
    452  1.1  christos i386_darwin_dr_get_addr (int regnum)
    453  1.1  christos {
    454  1.1  christos   return i386_darwin_dr_get (regnum);
    455  1.1  christos }
    456  1.1  christos 
    457  1.1  christos static unsigned long
    458  1.1  christos i386_darwin_dr_get_status (void)
    459  1.1  christos {
    460  1.1  christos   return i386_darwin_dr_get (DR_STATUS);
    461  1.1  christos }
    462  1.1  christos 
    463  1.1  christos static unsigned long
    464  1.1  christos i386_darwin_dr_get_control (void)
    465  1.1  christos {
    466  1.1  christos   return i386_darwin_dr_get (DR_CONTROL);
    467  1.1  christos }
    468  1.1  christos 
    469  1.1  christos void
    470  1.1  christos darwin_check_osabi (darwin_inferior *inf, thread_t thread)
    471  1.1  christos {
    472  1.1  christos   if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
    473  1.1  christos     {
    474  1.1  christos       /* Attaching to a process.  Let's figure out what kind it is.  */
    475  1.1  christos       x86_thread_state_t gp_regs;
    476  1.1  christos       struct gdbarch_info info;
    477  1.1  christos       unsigned int gp_count = x86_THREAD_STATE_COUNT;
    478  1.1  christos       kern_return_t ret;
    479  1.1  christos 
    480  1.1  christos       ret = thread_get_state (thread, x86_THREAD_STATE,
    481  1.1  christos 			      (thread_state_t) &gp_regs, &gp_count);
    482  1.1  christos       if (ret != KERN_SUCCESS)
    483  1.1  christos 	{
    484  1.1  christos 	  MACH_CHECK_ERROR (ret);
    485  1.1  christos 	  return;
    486  1.1  christos 	}
    487  1.1  christos 
    488  1.1  christos       gdbarch_info_init (&info);
    489  1.1  christos       gdbarch_info_fill (&info);
    490  1.1  christos       info.byte_order = gdbarch_byte_order (target_gdbarch ());
    491  1.1  christos       info.osabi = GDB_OSABI_DARWIN;
    492  1.1  christos       if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
    493  1.1  christos 	info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
    494  1.1  christos 					      bfd_mach_x86_64);
    495  1.1  christos       else
    496  1.1  christos 	info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
    497  1.1  christos 					      bfd_mach_i386_i386);
    498  1.1  christos       gdbarch_update_p (info);
    499  1.1  christos     }
    500  1.1  christos }
    501  1.1  christos 
    502  1.1  christos #define X86_EFLAGS_T 0x100UL
    503  1.1  christos 
    504  1.1  christos /* Returning from a signal trampoline is done by calling a
    505  1.1  christos    special system call (sigreturn).  This system call
    506  1.1  christos    restores the registers that were saved when the signal was
    507  1.1  christos    raised, including %eflags/%rflags.  That means that single-stepping
    508  1.1  christos    won't work.  Instead, we'll have to modify the signal context
    509  1.1  christos    that's about to be restored, and set the trace flag there.  */
    510  1.1  christos 
    511  1.1  christos static int
    512  1.1  christos i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
    513  1.1  christos {
    514  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
    515  1.1  christos   static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
    516  1.1  christos   gdb_byte buf[sizeof (darwin_syscall)];
    517  1.1  christos 
    518  1.1  christos   /* Check if PC is at a sigreturn system call.  */
    519  1.1  christos   if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
    520  1.1  christos       && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
    521  1.1  christos       && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
    522  1.1  christos     {
    523  1.1  christos       ULONGEST uctx_addr;
    524  1.1  christos       ULONGEST mctx_addr;
    525  1.1  christos       ULONGEST flags_addr;
    526  1.1  christos       unsigned int eflags;
    527  1.1  christos 
    528  1.1  christos       uctx_addr = read_memory_unsigned_integer
    529  1.1  christos 		    (regs->uts.ts32.__esp + 4, 4, byte_order);
    530  1.1  christos       mctx_addr = read_memory_unsigned_integer
    531  1.1  christos 		    (uctx_addr + 28, 4, byte_order);
    532  1.1  christos 
    533  1.1  christos       flags_addr = mctx_addr + 12 + 9 * 4;
    534  1.1  christos       read_memory (flags_addr, (gdb_byte *) &eflags, 4);
    535  1.1  christos       eflags |= X86_EFLAGS_T;
    536  1.1  christos       write_memory (flags_addr, (gdb_byte *) &eflags, 4);
    537  1.1  christos 
    538  1.1  christos       return 1;
    539  1.1  christos     }
    540  1.1  christos   return 0;
    541  1.1  christos }
    542  1.1  christos 
    543  1.1  christos #ifdef BFD64
    544  1.1  christos static int
    545  1.1  christos amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
    546  1.1  christos {
    547  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
    548  1.1  christos   static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
    549  1.1  christos   gdb_byte buf[sizeof (darwin_syscall)];
    550  1.1  christos 
    551  1.1  christos   /* Check if PC is at a sigreturn system call.  */
    552  1.1  christos   if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
    553  1.1  christos       && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
    554  1.1  christos       && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
    555  1.1  christos     {
    556  1.1  christos       ULONGEST mctx_addr;
    557  1.1  christos       ULONGEST flags_addr;
    558  1.1  christos       unsigned int rflags;
    559  1.1  christos 
    560  1.1  christos       mctx_addr = read_memory_unsigned_integer
    561  1.1  christos 		    (regs->uts.ts64.__rdi + 48, 8, byte_order);
    562  1.1  christos       flags_addr = mctx_addr + 16 + 17 * 8;
    563  1.1  christos 
    564  1.1  christos       /* AMD64 is little endian.  */
    565  1.1  christos       read_memory (flags_addr, (gdb_byte *) &rflags, 4);
    566  1.1  christos       rflags |= X86_EFLAGS_T;
    567  1.1  christos       write_memory (flags_addr, (gdb_byte *) &rflags, 4);
    568  1.1  christos 
    569  1.1  christos       return 1;
    570  1.1  christos     }
    571  1.1  christos   return 0;
    572  1.1  christos }
    573  1.1  christos #endif
    574  1.1  christos 
    575  1.1  christos void
    576  1.1  christos darwin_set_sstep (thread_t thread, int enable)
    577  1.1  christos {
    578  1.1  christos   x86_thread_state_t regs;
    579  1.1  christos   unsigned int count = x86_THREAD_STATE_COUNT;
    580  1.1  christos   kern_return_t kret;
    581  1.1  christos 
    582  1.1  christos   kret = thread_get_state (thread, x86_THREAD_STATE,
    583  1.1  christos 			   (thread_state_t) &regs, &count);
    584  1.1  christos   if (kret != KERN_SUCCESS)
    585  1.1  christos     {
    586  1.1  christos       printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
    587  1.1  christos 			 kret, thread);
    588  1.1  christos       return;
    589  1.1  christos     }
    590  1.1  christos 
    591  1.1  christos   switch (regs.tsh.flavor)
    592  1.1  christos     {
    593  1.1  christos     case x86_THREAD_STATE32:
    594  1.1  christos       {
    595  1.1  christos 	__uint32_t bit = enable ? X86_EFLAGS_T : 0;
    596  1.1  christos 
    597  1.1  christos 	if (enable && i386_darwin_sstep_at_sigreturn (&regs))
    598  1.1  christos 	  return;
    599  1.1  christos 	if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
    600  1.1  christos 	  return;
    601  1.1  christos 	regs.uts.ts32.__eflags
    602  1.1  christos 	  = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
    603  1.1  christos 	kret = thread_set_state (thread, x86_THREAD_STATE,
    604  1.1  christos 				 (thread_state_t) &regs, count);
    605  1.1  christos 	MACH_CHECK_ERROR (kret);
    606  1.1  christos       }
    607  1.1  christos       break;
    608  1.1  christos #ifdef BFD64
    609  1.1  christos     case x86_THREAD_STATE64:
    610  1.1  christos       {
    611  1.1  christos 	__uint64_t bit = enable ? X86_EFLAGS_T : 0;
    612  1.1  christos 
    613  1.1  christos 	if (enable && amd64_darwin_sstep_at_sigreturn (&regs))
    614  1.1  christos 	  return;
    615  1.1  christos 	if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
    616  1.1  christos 	  return;
    617  1.1  christos 	regs.uts.ts64.__rflags
    618  1.1  christos 	  = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
    619  1.1  christos 	kret = thread_set_state (thread, x86_THREAD_STATE,
    620  1.1  christos 				 (thread_state_t) &regs, count);
    621  1.1  christos 	MACH_CHECK_ERROR (kret);
    622  1.1  christos       }
    623  1.1  christos       break;
    624  1.1  christos #endif
    625  1.1  christos     default:
    626  1.1  christos       error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
    627  1.1  christos     }
    628  1.1  christos }
    629  1.1  christos 
    630  1.1  christos void
    631  1.1  christos darwin_complete_target (struct target_ops *target)
    632  1.1  christos {
    633  1.1  christos #ifdef BFD64
    634  1.1  christos   amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
    635  1.1  christos   amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
    636  1.1  christos   amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
    637  1.1  christos   amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
    638  1.1  christos #endif
    639  1.1  christos 
    640  1.3  christos   x86_use_watchpoints (target);
    641  1.1  christos 
    642  1.3  christos   x86_dr_low.set_control = i386_darwin_dr_set_control;
    643  1.3  christos   x86_dr_low.set_addr = i386_darwin_dr_set_addr;
    644  1.3  christos   x86_dr_low.get_addr = i386_darwin_dr_get_addr;
    645  1.3  christos   x86_dr_low.get_status = i386_darwin_dr_get_status;
    646  1.3  christos   x86_dr_low.get_control = i386_darwin_dr_get_control;
    647  1.1  christos 
    648  1.1  christos   /* Let's assume that the kernel is 64 bits iff the executable is.  */
    649  1.1  christos #ifdef __x86_64__
    650  1.3  christos   x86_set_debug_register_length (8);
    651  1.1  christos #else
    652  1.3  christos   x86_set_debug_register_length (4);
    653  1.1  christos #endif
    654  1.1  christos 
    655  1.1  christos   target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
    656  1.1  christos   target->to_store_registers = i386_darwin_store_inferior_registers;
    657  1.1  christos }
    658