py_decref: Don't check for NULL before calling Py_DECREF.
[deliverable/binutils-gdb.git] / gdb / ppc-linux-nat.c
index 275de7885c357a5e6d3c4fe5b1fc5d3f0bdf3f14..1ff00a6d04e6251eb5206b0d5b041be297d543be 100644 (file)
@@ -1,8 +1,6 @@
 /* PPC GNU/Linux native support.
 
-   Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003,
-   2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 1988-2013 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -549,7 +547,7 @@ fetch_register (struct regcache *regcache, int tid, int regno)
   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
   int bytes_transferred;
   unsigned int offset;         /* Offset of registers within the u area.  */
-  char buf[MAX_REGISTER_SIZE];
+  gdb_byte buf[MAX_REGISTER_SIZE];
 
   if (altivec_register_p (gdbarch, regno))
     {
@@ -594,17 +592,19 @@ fetch_register (struct regcache *regcache, int tid, int regno)
        bytes_transferred < register_size (gdbarch, regno);
        bytes_transferred += sizeof (long))
     {
+      long l;
+
       errno = 0;
-      *(long *) &buf[bytes_transferred]
-        = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
+      l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
       regaddr += sizeof (long);
       if (errno != 0)
        {
           char message[128];
-         sprintf (message, "reading register %s (#%d)", 
-                  gdbarch_register_name (gdbarch, regno), regno);
+         xsnprintf (message, sizeof (message), "reading register %s (#%d)",
+                    gdbarch_register_name (gdbarch, regno), regno);
          perror_with_name (message);
        }
+      memcpy (&buf[bytes_transferred], &l, sizeof (l));
     }
 
   /* Now supply the register.  Keep in mind that the regcache's idea
@@ -1034,7 +1034,7 @@ store_register (const struct regcache *regcache, int tid, int regno)
   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
   int i;
   size_t bytes_to_transfer;
-  char buf[MAX_REGISTER_SIZE];
+  gdb_byte buf[MAX_REGISTER_SIZE];
 
   if (altivec_register_p (gdbarch, regno))
     {
@@ -1074,9 +1074,11 @@ store_register (const struct regcache *regcache, int tid, int regno)
 
   for (i = 0; i < bytes_to_transfer; i += sizeof (long))
     {
+      long l;
+
+      memcpy (&l, &buf[i], sizeof (l));
       errno = 0;
-      ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr,
-             *(long *) &buf[i]);
+      ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
       regaddr += sizeof (long);
 
       if (errno == EIO 
@@ -1092,8 +1094,8 @@ store_register (const struct regcache *regcache, int tid, int regno)
       if (errno != 0)
        {
           char message[128];
-         sprintf (message, "writing register %s (#%d)", 
-                  gdbarch_register_name (gdbarch, regno), regno);
+         xsnprintf (message, sizeof (message), "writing register %s (#%d)",
+                    gdbarch_register_name (gdbarch, regno), regno);
          perror_with_name (message);
        }
     }
@@ -1348,7 +1350,8 @@ store_ppc_registers (const struct regcache *regcache, int tid)
 }
 
 /* Fetch the AT_HWCAP entry from the aux vector.  */
-unsigned long ppc_linux_get_hwcap (void)
+static unsigned long
+ppc_linux_get_hwcap (void)
 {
   CORE_ADDR field;
 
@@ -1417,17 +1420,20 @@ have_ptrace_booke_interface (void)
       /* Check for kernel support for BOOKE debug registers.  */
       if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &booke_debug_info) >= 0)
        {
-         have_ptrace_booke_interface = 1;
-         max_slots_number = booke_debug_info.num_instruction_bps
-           + booke_debug_info.num_data_bps
-           + booke_debug_info.num_condition_regs;
-       }
-      else
-       {
-         /* Old school interface and no BOOKE debug registers support.  */
-         have_ptrace_booke_interface = 0;
-         memset (&booke_debug_info, 0, sizeof (struct ppc_debug_info));
+         /* Check whether ptrace BOOKE interface is functional and
+            provides any supported feature.  */
+         if (booke_debug_info.features != 0)
+           {
+             have_ptrace_booke_interface = 1;
+             max_slots_number = booke_debug_info.num_instruction_bps
+               + booke_debug_info.num_data_bps
+               + booke_debug_info.num_condition_regs;
+             return have_ptrace_booke_interface;
+           }
        }
+      /* Old school interface and no BOOKE debug registers support.  */
+      have_ptrace_booke_interface = 0;
+      memset (&booke_debug_info, 0, sizeof (struct ppc_debug_info));
     }
 
   return have_ptrace_booke_interface;
@@ -1457,7 +1463,7 @@ ppc_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
       || type == bp_access_watchpoint || type == bp_watchpoint)
     {
-      if (cnt > total_hw_wp)
+      if (cnt + ot > total_hw_wp)
        return -1;
     }
   else if (type == bp_hardware_breakpoint)
@@ -1497,16 +1503,19 @@ ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
      to determine the hardcoded watchable region for watchpoints.  */
   if (have_ptrace_booke_interface ())
     {
-      /* DAC-based processors (i.e., embedded processors), like the PowerPC 440
-        have ranged watchpoints and can watch any access within an arbitrary
-        memory region.  This is useful to watch arrays and structs, for
-        instance.  It takes two hardware watchpoints though.  */
+      /* Embedded DAC-based processors, like the PowerPC 440 have ranged
+        watchpoints and can watch any access within an arbitrary memory
+        region. This is useful to watch arrays and structs, for instance.  It
+         takes two hardware watchpoints though.  */
       if (len > 1
-         && booke_debug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE)
+         && booke_debug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
+         && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
        return 2;
-      else if (booke_debug_info.data_bp_alignment
-              && (addr + len > (addr & ~(booke_debug_info.data_bp_alignment - 1))
-                  + booke_debug_info.data_bp_alignment))
+      /* Server processors provide one hardware watchpoint and addr+len should
+         fall in the watchable region provided by the ptrace interface.  */
+      if (booke_debug_info.data_bp_alignment
+         && (addr + len > (addr & ~(booke_debug_info.data_bp_alignment - 1))
+             + booke_debug_info.data_bp_alignment))
        return 0;
     }
   /* addr+len must fall in the 8 byte watchable region for DABR-based
@@ -1654,7 +1663,6 @@ static int
 ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
                                  struct bp_target_info *bp_tgt)
 {
-  ptid_t ptid;
   struct lwp_info *lp;
   struct ppc_hw_breakpoint p;
 
@@ -1681,8 +1689,8 @@ ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
       p.addr2 = 0;
     }
 
-  ALL_LWPS (lp, ptid)
-    booke_insert_point (&p, TIDGET (ptid));
+  ALL_LWPS (lp)
+    booke_insert_point (&p, TIDGET (lp->ptid));
 
   return 0;
 }
@@ -1691,7 +1699,6 @@ static int
 ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
                                  struct bp_target_info *bp_tgt)
 {
-  ptid_t ptid;
   struct lwp_info *lp;
   struct ppc_hw_breakpoint p;
 
@@ -1718,8 +1725,8 @@ ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
       p.addr2 = 0;
     }
 
-  ALL_LWPS (lp, ptid)
-    booke_remove_point (&p, TIDGET (ptid));
+  ALL_LWPS (lp)
+    booke_remove_point (&p, TIDGET (lp->ptid));
 
   return 0;
 }
@@ -1748,7 +1755,6 @@ static int
 ppc_linux_insert_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
                                  CORE_ADDR mask, int rw)
 {
-  ptid_t ptid;
   struct lwp_info *lp;
   struct ppc_hw_breakpoint p;
 
@@ -1762,8 +1768,8 @@ ppc_linux_insert_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
   p.addr2 = mask;
   p.condition_value = 0;
 
-  ALL_LWPS (lp, ptid)
-    booke_insert_point (&p, TIDGET (ptid));
+  ALL_LWPS (lp)
+    booke_insert_point (&p, TIDGET (lp->ptid));
 
   return 0;
 }
@@ -1777,7 +1783,6 @@ static int
 ppc_linux_remove_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
                                  CORE_ADDR mask, int rw)
 {
-  ptid_t ptid;
   struct lwp_info *lp;
   struct ppc_hw_breakpoint p;
 
@@ -1791,8 +1796,8 @@ ppc_linux_remove_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
   p.addr2 = mask;
   p.condition_value = 0;
 
-  ALL_LWPS (lp, ptid)
-    booke_remove_point (&p, TIDGET (ptid));
+  ALL_LWPS (lp)
+    booke_remove_point (&p, TIDGET (lp->ptid));
 
   return 0;
 }
@@ -2014,7 +2019,8 @@ create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
                           int len, int rw, struct expression *cond,
                           int insert)
 {
-  if (len == 1)
+  if (len == 1
+      || !(booke_debug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
     {
       int use_condition;
       CORE_ADDR data_value;
@@ -2059,7 +2065,6 @@ ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
                             struct expression *cond)
 {
   struct lwp_info *lp;
-  ptid_t ptid;
   int ret = -1;
 
   if (have_ptrace_booke_interface ())
@@ -2068,8 +2073,8 @@ ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
 
       create_watchpoint_request (&p, addr, len, rw, cond, 1);
 
-      ALL_LWPS (lp, ptid)
-       booke_insert_point (&p, TIDGET (ptid));
+      ALL_LWPS (lp)
+       booke_insert_point (&p, TIDGET (lp->ptid));
 
       ret = 0;
     }
@@ -2112,8 +2117,8 @@ ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
 
       saved_dabr_value = dabr_value;
 
-      ALL_LWPS (lp, ptid)
-       if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0,
+      ALL_LWPS (lp)
+       if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (lp->ptid), 0,
                    saved_dabr_value) < 0)
          return -1;
 
@@ -2128,7 +2133,6 @@ ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
                             struct expression *cond)
 {
   struct lwp_info *lp;
-  ptid_t ptid;
   int ret = -1;
 
   if (have_ptrace_booke_interface ())
@@ -2137,16 +2141,16 @@ ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
 
       create_watchpoint_request (&p, addr, len, rw, cond, 0);
 
-      ALL_LWPS (lp, ptid)
-       booke_remove_point (&p, TIDGET (ptid));
+      ALL_LWPS (lp)
+       booke_remove_point (&p, TIDGET (lp->ptid));
 
       ret = 0;
     }
   else
     {
       saved_dabr_value = 0;
-      ALL_LWPS (lp, ptid)
-       if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (ptid), 0,
+      ALL_LWPS (lp)
+       if (ptrace (PTRACE_SET_DEBUGREG, TIDGET (lp->ptid), 0,
                    saved_dabr_value) < 0)
          return -1;
 
@@ -2157,9 +2161,9 @@ ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
 }
 
 static void
-ppc_linux_new_thread (ptid_t ptid)
+ppc_linux_new_thread (struct lwp_info *lp)
 {
-  int tid = TIDGET (ptid);
+  int tid = TIDGET (lp->ptid);
 
   if (have_ptrace_booke_interface ())
     {
@@ -2177,7 +2181,18 @@ ppc_linux_new_thread (ptid_t ptid)
       /* Copy that thread's breakpoints and watchpoints to the new thread.  */
       for (i = 0; i < max_slots_number; i++)
        if (hw_breaks[i].hw_break)
-         booke_insert_point (hw_breaks[i].hw_break, tid);
+         {
+           /* Older kernels did not make new threads inherit their parent
+              thread's debug state, so we always clear the slot and replicate
+              the debug state ourselves, ensuring compatibility with all
+              kernels.  */
+
+           /* The ppc debug resource accounting is done through "slots".
+              Ask the kernel the deallocate this specific *point's slot.  */
+           ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot);
+
+           booke_insert_point (hw_breaks[i].hw_break, tid);
+         }
     }
   else
     ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
@@ -2219,12 +2234,13 @@ ppc_linux_thread_exit (struct thread_info *tp, int silent)
 static int
 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
 {
-  struct siginfo *siginfo_p;
+  siginfo_t siginfo;
 
-  siginfo_p = linux_nat_get_siginfo (inferior_ptid);
+  if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
+    return 0;
 
-  if (siginfo_p->si_signo != SIGTRAP
-      || (siginfo_p->si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
+  if (siginfo.si_signo != SIGTRAP
+      || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
     return 0;
 
   if (have_ptrace_booke_interface ())
@@ -2233,7 +2249,7 @@ ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
       struct thread_points *t;
       struct hw_break_tuple *hw_breaks;
       /* The index (or slot) of the *point is passed in the si_errno field.  */
-      int slot = siginfo_p->si_errno;
+      int slot = siginfo.si_errno;
 
       t = booke_find_thread_points_by_tid (TIDGET (inferior_ptid), 0);
 
@@ -2250,7 +2266,7 @@ ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
        }
     }
 
-  *addr_p = (CORE_ADDR) (uintptr_t) siginfo_p->si_addr;
+  *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
   return 1;
 }
 
@@ -2391,7 +2407,7 @@ ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
                       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
 {
   int sizeof_auxv_field = ppc_linux_target_wordsize ();
-  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
+  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
   gdb_byte *ptr = *readptr;
 
   if (endptr == ptr)
This page took 0.030112 seconds and 4 git commands to generate.