gdb/arm-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GNU/Linux on ARM native support.
  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "gdbcore.h"
  17. #include "regcache.h"
  18. #include "target.h"
  19. #include "linux-nat.h"
  20. #include "target-descriptions.h"
  21. #include "auxv.h"
  22. #include "observer.h"
  23. #include "gdbthread.h"

  24. #include "arm-tdep.h"
  25. #include "arm-linux-tdep.h"

  26. #include <elf/common.h>
  27. #include <sys/user.h>
  28. #include <sys/ptrace.h>
  29. #include <sys/utsname.h>
  30. #include <sys/procfs.h>

  31. /* Prototypes for supply_gregset etc.  */
  32. #include "gregset.h"

  33. /* Defines ps_err_e, struct ps_prochandle.  */
  34. #include "gdb_proc_service.h"

  35. #ifndef PTRACE_GET_THREAD_AREA
  36. #define PTRACE_GET_THREAD_AREA 22
  37. #endif

  38. #ifndef PTRACE_GETWMMXREGS
  39. #define PTRACE_GETWMMXREGS 18
  40. #define PTRACE_SETWMMXREGS 19
  41. #endif

  42. #ifndef PTRACE_GETVFPREGS
  43. #define PTRACE_GETVFPREGS 27
  44. #define PTRACE_SETVFPREGS 28
  45. #endif

  46. #ifndef PTRACE_GETHBPREGS
  47. #define PTRACE_GETHBPREGS 29
  48. #define PTRACE_SETHBPREGS 30
  49. #endif

  50. /* A flag for whether the WMMX registers are available.  */
  51. static int arm_linux_has_wmmx_registers;

  52. /* The number of 64-bit VFP registers we have (expect this to be 0,
  53.    16, or 32).  */
  54. static int arm_linux_vfp_register_count;

  55. extern int arm_apcs_32;

  56. /* On GNU/Linux, threads are implemented as pseudo-processes, in which
  57.    case we may be tracing more than one process at a time.  In that
  58.    case, inferior_ptid will contain the main process ID and the
  59.    individual thread (process) ID.  get_thread_id () is used to get
  60.    the thread id if it's available, and the process id otherwise.  */

  61. static int
  62. get_thread_id (ptid_t ptid)
  63. {
  64.   int tid = ptid_get_lwp (ptid);
  65.   if (0 == tid)
  66.     tid = ptid_get_pid (ptid);
  67.   return tid;
  68. }

  69. #define GET_THREAD_ID(PTID)        get_thread_id (PTID)

  70. /* Get the value of a particular register from the floating point
  71.    state of the process and store it into regcache.  */

  72. static void
  73. fetch_fpregister (struct regcache *regcache, int regno)
  74. {
  75.   int ret, tid;
  76.   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];

  77.   /* Get the thread id for the ptrace call.  */
  78.   tid = GET_THREAD_ID (inferior_ptid);

  79.   /* Read the floating point state.  */
  80.   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
  81.   if (ret < 0)
  82.     {
  83.       warning (_("Unable to fetch floating point register."));
  84.       return;
  85.     }

  86.   /* Fetch fpsr.  */
  87.   if (ARM_FPS_REGNUM == regno)
  88.     regcache_raw_supply (regcache, ARM_FPS_REGNUM,
  89.                          fp + NWFPE_FPSR_OFFSET);

  90.   /* Fetch the floating point register.  */
  91.   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
  92.     supply_nwfpe_register (regcache, regno, fp);
  93. }

  94. /* Get the whole floating point state of the process and store it
  95.    into regcache.  */

  96. static void
  97. fetch_fpregs (struct regcache *regcache)
  98. {
  99.   int ret, regno, tid;
  100.   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];

  101.   /* Get the thread id for the ptrace call.  */
  102.   tid = GET_THREAD_ID (inferior_ptid);

  103.   /* Read the floating point state.  */
  104.   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
  105.   if (ret < 0)
  106.     {
  107.       warning (_("Unable to fetch the floating point registers."));
  108.       return;
  109.     }

  110.   /* Fetch fpsr.  */
  111.   regcache_raw_supply (regcache, ARM_FPS_REGNUM,
  112.                        fp + NWFPE_FPSR_OFFSET);

  113.   /* Fetch the floating point registers.  */
  114.   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
  115.     supply_nwfpe_register (regcache, regno, fp);
  116. }

  117. /* Save a particular register into the floating point state of the
  118.    process using the contents from regcache.  */

  119. static void
  120. store_fpregister (const struct regcache *regcache, int regno)
  121. {
  122.   int ret, tid;
  123.   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];

  124.   /* Get the thread id for the ptrace call.  */
  125.   tid = GET_THREAD_ID (inferior_ptid);

  126.   /* Read the floating point state.  */
  127.   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
  128.   if (ret < 0)
  129.     {
  130.       warning (_("Unable to fetch the floating point registers."));
  131.       return;
  132.     }

  133.   /* Store fpsr.  */
  134.   if (ARM_FPS_REGNUM == regno
  135.       && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
  136.     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);

  137.   /* Store the floating point register.  */
  138.   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
  139.     collect_nwfpe_register (regcache, regno, fp);

  140.   ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
  141.   if (ret < 0)
  142.     {
  143.       warning (_("Unable to store floating point register."));
  144.       return;
  145.     }
  146. }

  147. /* Save the whole floating point state of the process using
  148.    the contents from regcache.  */

  149. static void
  150. store_fpregs (const struct regcache *regcache)
  151. {
  152.   int ret, regno, tid;
  153.   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];

  154.   /* Get the thread id for the ptrace call.  */
  155.   tid = GET_THREAD_ID (inferior_ptid);

  156.   /* Read the floating point state.  */
  157.   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
  158.   if (ret < 0)
  159.     {
  160.       warning (_("Unable to fetch the floating point registers."));
  161.       return;
  162.     }

  163.   /* Store fpsr.  */
  164.   if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
  165.     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);

  166.   /* Store the floating point registers.  */
  167.   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
  168.     if (REG_VALID == regcache_register_status (regcache, regno))
  169.       collect_nwfpe_register (regcache, regno, fp);

  170.   ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
  171.   if (ret < 0)
  172.     {
  173.       warning (_("Unable to store floating point registers."));
  174.       return;
  175.     }
  176. }

  177. /* Fetch a general register of the process and store into
  178.    regcache.  */

  179. static void
  180. fetch_register (struct regcache *regcache, int regno)
  181. {
  182.   int ret, tid;
  183.   elf_gregset_t regs;

  184.   /* Get the thread id for the ptrace call.  */
  185.   tid = GET_THREAD_ID (inferior_ptid);

  186.   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
  187.   if (ret < 0)
  188.     {
  189.       warning (_("Unable to fetch general register."));
  190.       return;
  191.     }

  192.   if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
  193.     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);

  194.   if (ARM_PS_REGNUM == regno)
  195.     {
  196.       if (arm_apcs_32)
  197.         regcache_raw_supply (regcache, ARM_PS_REGNUM,
  198.                              (char *) &regs[ARM_CPSR_GREGNUM]);
  199.       else
  200.         regcache_raw_supply (regcache, ARM_PS_REGNUM,
  201.                              (char *) &regs[ARM_PC_REGNUM]);
  202.     }

  203.   if (ARM_PC_REGNUM == regno)
  204.     {
  205.       regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
  206.                               (get_regcache_arch (regcache),
  207.                                regs[ARM_PC_REGNUM]);
  208.       regcache_raw_supply (regcache, ARM_PC_REGNUM,
  209.                            (char *) &regs[ARM_PC_REGNUM]);
  210.     }
  211. }

  212. /* Fetch all general registers of the process and store into
  213.    regcache.  */

  214. static void
  215. fetch_regs (struct regcache *regcache)
  216. {
  217.   int ret, regno, tid;
  218.   elf_gregset_t regs;

  219.   /* Get the thread id for the ptrace call.  */
  220.   tid = GET_THREAD_ID (inferior_ptid);

  221.   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
  222.   if (ret < 0)
  223.     {
  224.       warning (_("Unable to fetch general registers."));
  225.       return;
  226.     }

  227.   for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
  228.     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);

  229.   if (arm_apcs_32)
  230.     regcache_raw_supply (regcache, ARM_PS_REGNUM,
  231.                          (char *) &regs[ARM_CPSR_GREGNUM]);
  232.   else
  233.     regcache_raw_supply (regcache, ARM_PS_REGNUM,
  234.                          (char *) &regs[ARM_PC_REGNUM]);

  235.   regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
  236.                           (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
  237.   regcache_raw_supply (regcache, ARM_PC_REGNUM,
  238.                        (char *) &regs[ARM_PC_REGNUM]);
  239. }

  240. /* Store all general registers of the process from the values in
  241.    regcache.  */

  242. static void
  243. store_register (const struct regcache *regcache, int regno)
  244. {
  245.   int ret, tid;
  246.   elf_gregset_t regs;

  247.   if (REG_VALID != regcache_register_status (regcache, regno))
  248.     return;

  249.   /* Get the thread id for the ptrace call.  */
  250.   tid = GET_THREAD_ID (inferior_ptid);

  251.   /* Get the general registers from the process.  */
  252.   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
  253.   if (ret < 0)
  254.     {
  255.       warning (_("Unable to fetch general registers."));
  256.       return;
  257.     }

  258.   if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
  259.     regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
  260.   else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
  261.     regcache_raw_collect (regcache, regno,
  262.                          (char *) &regs[ARM_CPSR_GREGNUM]);
  263.   else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
  264.     regcache_raw_collect (regcache, ARM_PC_REGNUM,
  265.                          (char *) &regs[ARM_PC_REGNUM]);

  266.   ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
  267.   if (ret < 0)
  268.     {
  269.       warning (_("Unable to store general register."));
  270.       return;
  271.     }
  272. }

  273. static void
  274. store_regs (const struct regcache *regcache)
  275. {
  276.   int ret, regno, tid;
  277.   elf_gregset_t regs;

  278.   /* Get the thread id for the ptrace call.  */
  279.   tid = GET_THREAD_ID (inferior_ptid);

  280.   /* Fetch the general registers.  */
  281.   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
  282.   if (ret < 0)
  283.     {
  284.       warning (_("Unable to fetch general registers."));
  285.       return;
  286.     }

  287.   for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
  288.     {
  289.       if (REG_VALID == regcache_register_status (regcache, regno))
  290.         regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
  291.     }

  292.   if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
  293.     regcache_raw_collect (regcache, ARM_PS_REGNUM,
  294.                          (char *) &regs[ARM_CPSR_GREGNUM]);

  295.   ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);

  296.   if (ret < 0)
  297.     {
  298.       warning (_("Unable to store general registers."));
  299.       return;
  300.     }
  301. }

  302. /* Fetch all WMMX registers of the process and store into
  303.    regcache.  */

  304. #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)

  305. static void
  306. fetch_wmmx_regs (struct regcache *regcache)
  307. {
  308.   char regbuf[IWMMXT_REGS_SIZE];
  309.   int ret, regno, tid;

  310.   /* Get the thread id for the ptrace call.  */
  311.   tid = GET_THREAD_ID (inferior_ptid);

  312.   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
  313.   if (ret < 0)
  314.     {
  315.       warning (_("Unable to fetch WMMX registers."));
  316.       return;
  317.     }

  318.   for (regno = 0; regno < 16; regno++)
  319.     regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
  320.                          &regbuf[regno * 8]);

  321.   for (regno = 0; regno < 2; regno++)
  322.     regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
  323.                          &regbuf[16 * 8 + regno * 4]);

  324.   for (regno = 0; regno < 4; regno++)
  325.     regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
  326.                          &regbuf[16 * 8 + 2 * 4 + regno * 4]);
  327. }

  328. static void
  329. store_wmmx_regs (const struct regcache *regcache)
  330. {
  331.   char regbuf[IWMMXT_REGS_SIZE];
  332.   int ret, regno, tid;

  333.   /* Get the thread id for the ptrace call.  */
  334.   tid = GET_THREAD_ID (inferior_ptid);

  335.   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
  336.   if (ret < 0)
  337.     {
  338.       warning (_("Unable to fetch WMMX registers."));
  339.       return;
  340.     }

  341.   for (regno = 0; regno < 16; regno++)
  342.     if (REG_VALID == regcache_register_status (regcache,
  343.                                                regno + ARM_WR0_REGNUM))
  344.       regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
  345.                             &regbuf[regno * 8]);

  346.   for (regno = 0; regno < 2; regno++)
  347.     if (REG_VALID == regcache_register_status (regcache,
  348.                                                regno + ARM_WCSSF_REGNUM))
  349.       regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
  350.                             &regbuf[16 * 8 + regno * 4]);

  351.   for (regno = 0; regno < 4; regno++)
  352.     if (REG_VALID == regcache_register_status (regcache,
  353.                                                regno + ARM_WCGR0_REGNUM))
  354.       regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
  355.                             &regbuf[16 * 8 + 2 * 4 + regno * 4]);

  356.   ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);

  357.   if (ret < 0)
  358.     {
  359.       warning (_("Unable to store WMMX registers."));
  360.       return;
  361.     }
  362. }

  363. /* Fetch and store VFP Registers.  The kernel object has space for 32
  364.    64-bit registers, and the FPSCR.  This is even when on a VFPv2 or
  365.    VFPv3D16 target.  */
  366. #define VFP_REGS_SIZE (32 * 8 + 4)

  367. static void
  368. fetch_vfp_regs (struct regcache *regcache)
  369. {
  370.   char regbuf[VFP_REGS_SIZE];
  371.   int ret, regno, tid;

  372.   /* Get the thread id for the ptrace call.  */
  373.   tid = GET_THREAD_ID (inferior_ptid);

  374.   ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
  375.   if (ret < 0)
  376.     {
  377.       warning (_("Unable to fetch VFP registers."));
  378.       return;
  379.     }

  380.   for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
  381.     regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
  382.                          (char *) regbuf + regno * 8);

  383.   regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
  384.                        (char *) regbuf + 32 * 8);
  385. }

  386. static void
  387. store_vfp_regs (const struct regcache *regcache)
  388. {
  389.   char regbuf[VFP_REGS_SIZE];
  390.   int ret, regno, tid;

  391.   /* Get the thread id for the ptrace call.  */
  392.   tid = GET_THREAD_ID (inferior_ptid);

  393.   ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
  394.   if (ret < 0)
  395.     {
  396.       warning (_("Unable to fetch VFP registers (for update)."));
  397.       return;
  398.     }

  399.   for (regno = 0; regno < arm_linux_vfp_register_count; regno++)
  400.     regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
  401.                           (char *) regbuf + regno * 8);

  402.   regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
  403.                         (char *) regbuf + 32 * 8);

  404.   ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);

  405.   if (ret < 0)
  406.     {
  407.       warning (_("Unable to store VFP registers."));
  408.       return;
  409.     }
  410. }

  411. /* Fetch registers from the child process.  Fetch all registers if
  412.    regno == -1, otherwise fetch all general registers or all floating
  413.    point registers depending upon the value of regno.  */

  414. static void
  415. arm_linux_fetch_inferior_registers (struct target_ops *ops,
  416.                                     struct regcache *regcache, int regno)
  417. {
  418.   if (-1 == regno)
  419.     {
  420.       fetch_regs (regcache);
  421.       fetch_fpregs (regcache);
  422.       if (arm_linux_has_wmmx_registers)
  423.         fetch_wmmx_regs (regcache);
  424.       if (arm_linux_vfp_register_count > 0)
  425.         fetch_vfp_regs (regcache);
  426.     }
  427.   else
  428.     {
  429.       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
  430.         fetch_register (regcache, regno);
  431.       else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
  432.         fetch_fpregister (regcache, regno);
  433.       else if (arm_linux_has_wmmx_registers
  434.                && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
  435.         fetch_wmmx_regs (regcache);
  436.       else if (arm_linux_vfp_register_count > 0
  437.                && regno >= ARM_D0_REGNUM
  438.                && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
  439.         fetch_vfp_regs (regcache);
  440.     }
  441. }

  442. /* Store registers back into the inferior.  Store all registers if
  443.    regno == -1, otherwise store all general registers or all floating
  444.    point registers depending upon the value of regno.  */

  445. static void
  446. arm_linux_store_inferior_registers (struct target_ops *ops,
  447.                                     struct regcache *regcache, int regno)
  448. {
  449.   if (-1 == regno)
  450.     {
  451.       store_regs (regcache);
  452.       store_fpregs (regcache);
  453.       if (arm_linux_has_wmmx_registers)
  454.         store_wmmx_regs (regcache);
  455.       if (arm_linux_vfp_register_count > 0)
  456.         store_vfp_regs (regcache);
  457.     }
  458.   else
  459.     {
  460.       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
  461.         store_register (regcache, regno);
  462.       else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
  463.         store_fpregister (regcache, regno);
  464.       else if (arm_linux_has_wmmx_registers
  465.                && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
  466.         store_wmmx_regs (regcache);
  467.       else if (arm_linux_vfp_register_count > 0
  468.                && regno >= ARM_D0_REGNUM
  469.                && regno <= ARM_D0_REGNUM + arm_linux_vfp_register_count)
  470.         store_vfp_regs (regcache);
  471.     }
  472. }

  473. /* Wrapper functions for the standard regset handling, used by
  474.    thread debugging.  */

  475. void
  476. fill_gregset (const struct regcache *regcache,
  477.               gdb_gregset_t *gregsetp, int regno)
  478. {
  479.   arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
  480. }

  481. void
  482. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  483. {
  484.   arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
  485. }

  486. void
  487. fill_fpregset (const struct regcache *regcache,
  488.                gdb_fpregset_t *fpregsetp, int regno)
  489. {
  490.   arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
  491. }

  492. /* Fill GDB's register array with the floating-point register values
  493.    in *fpregsetp.  */

  494. void
  495. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  496. {
  497.   arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
  498. }

  499. /* Fetch the thread-local storage pointer for libthread_db.  */

  500. ps_err_e
  501. ps_get_thread_area (const struct ps_prochandle *ph,
  502.                     lwpid_t lwpid, int idx, void **base)
  503. {
  504.   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  505.     return PS_ERR;

  506.   /* IDX is the bias from the thread pointer to the beginning of the
  507.      thread descriptor.  It has to be subtracted due to implementation
  508.      quirks in libthread_db.  */
  509.   *base = (void *) ((char *)*base - idx);

  510.   return PS_OK;
  511. }

  512. static const struct target_desc *
  513. arm_linux_read_description (struct target_ops *ops)
  514. {
  515.   CORE_ADDR arm_hwcap = 0;
  516.   arm_linux_has_wmmx_registers = 0;
  517.   arm_linux_vfp_register_count = 0;

  518.   if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
  519.     {
  520.       return ops->beneath->to_read_description (ops->beneath);
  521.     }

  522.   if (arm_hwcap & HWCAP_IWMMXT)
  523.     {
  524.       arm_linux_has_wmmx_registers = 1;
  525.       return tdesc_arm_with_iwmmxt;
  526.     }

  527.   if (arm_hwcap & HWCAP_VFP)
  528.     {
  529.       int pid;
  530.       char *buf;
  531.       const struct target_desc * result = NULL;

  532.       /* NEON implies VFPv3-D32 or no-VFP unit.  Say that we only support
  533.          Neon with VFPv3-D32.  */
  534.       if (arm_hwcap & HWCAP_NEON)
  535.         {
  536.           arm_linux_vfp_register_count = 32;
  537.           result = tdesc_arm_with_neon;
  538.         }
  539.       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
  540.         {
  541.           arm_linux_vfp_register_count = 32;
  542.           result = tdesc_arm_with_vfpv3;
  543.         }
  544.       else
  545.         {
  546.           arm_linux_vfp_register_count = 16;
  547.           result = tdesc_arm_with_vfpv2;
  548.         }

  549.       /* Now make sure that the kernel supports reading these
  550.          registers.  Support was added in 2.6.30.  */
  551.       pid = ptid_get_lwp (inferior_ptid);
  552.       errno = 0;
  553.       buf = alloca (VFP_REGS_SIZE);
  554.       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
  555.           && errno == EIO)
  556.         result = NULL;

  557.       return result;
  558.     }

  559.   return ops->beneath->to_read_description (ops->beneath);
  560. }

  561. /* Information describing the hardware breakpoint capabilities.  */
  562. struct arm_linux_hwbp_cap
  563. {
  564.   gdb_byte arch;
  565.   gdb_byte max_wp_length;
  566.   gdb_byte wp_count;
  567.   gdb_byte bp_count;
  568. };

  569. /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
  570.    assume a maximum number of supported break-/watchpoints.  */
  571. #define MAX_BPTS 16
  572. #define MAX_WPTS 16

  573. /* Get hold of the Hardware Breakpoint information for the target we are
  574.    attached to.  Returns NULL if the kernel doesn't support Hardware
  575.    breakpoints at all, or a pointer to the information structure.  */
  576. static const struct arm_linux_hwbp_cap *
  577. arm_linux_get_hwbp_cap (void)
  578. {
  579.   /* The info structure we return.  */
  580.   static struct arm_linux_hwbp_cap info;

  581.   /* Is INFO in a good state?  -1 means that no attempt has been made to
  582.      initialize INFO; 0 means an attempt has been made, but it failed; 1
  583.      means INFO is in an initialized state.  */
  584.   static int available = -1;

  585.   if (available == -1)
  586.     {
  587.       int tid;
  588.       unsigned int val;

  589.       tid = GET_THREAD_ID (inferior_ptid);
  590.       if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
  591.         available = 0;
  592.       else
  593.         {
  594.           info.arch = (gdb_byte)((val >> 24) & 0xff);
  595.           info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
  596.           info.wp_count = (gdb_byte)((val >> 8) & 0xff);
  597.           info.bp_count = (gdb_byte)(val & 0xff);

  598.       if (info.wp_count > MAX_WPTS)
  599.         {
  600.           warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
  601.                       supports %d"), MAX_WPTS, info.wp_count);
  602.           info.wp_count = MAX_WPTS;
  603.         }

  604.       if (info.bp_count > MAX_BPTS)
  605.         {
  606.           warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
  607.                       supports %d"), MAX_BPTS, info.bp_count);
  608.           info.bp_count = MAX_BPTS;
  609.         }
  610.           available = (info.arch != 0);
  611.         }
  612.     }

  613.   return available == 1 ? &info : NULL;
  614. }

  615. /* How many hardware breakpoints are available?  */
  616. static int
  617. arm_linux_get_hw_breakpoint_count (void)
  618. {
  619.   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
  620.   return cap != NULL ? cap->bp_count : 0;
  621. }

  622. /* How many hardware watchpoints are available?  */
  623. static int
  624. arm_linux_get_hw_watchpoint_count (void)
  625. {
  626.   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
  627.   return cap != NULL ? cap->wp_count : 0;
  628. }

  629. /* Have we got a free break-/watch-point available for use?  Returns -1 if
  630.    there is not an appropriate resource available, otherwise returns 1.  */
  631. static int
  632. arm_linux_can_use_hw_breakpoint (struct target_ops *self,
  633.                                  int type, int cnt, int ot)
  634. {
  635.   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
  636.       || type == bp_access_watchpoint || type == bp_watchpoint)
  637.     {
  638.       if (cnt + ot > arm_linux_get_hw_watchpoint_count ())
  639.         return -1;
  640.     }
  641.   else if (type == bp_hardware_breakpoint)
  642.     {
  643.       if (cnt > arm_linux_get_hw_breakpoint_count ())
  644.         return -1;
  645.     }
  646.   else
  647.     gdb_assert (FALSE);

  648.   return 1;
  649. }

  650. /* Enum describing the different types of ARM hardware break-/watch-points.  */
  651. typedef enum
  652. {
  653.   arm_hwbp_break = 0,
  654.   arm_hwbp_load = 1,
  655.   arm_hwbp_store = 2,
  656.   arm_hwbp_access = 3
  657. } arm_hwbp_type;

  658. /* Type describing an ARM Hardware Breakpoint Control register value.  */
  659. typedef unsigned int arm_hwbp_control_t;

  660. /* Structure used to keep track of hardware break-/watch-points.  */
  661. struct arm_linux_hw_breakpoint
  662. {
  663.   /* Address to break on, or being watched.  */
  664.   unsigned int address;
  665.   /* Control register for break-/watch- point.  */
  666.   arm_hwbp_control_t control;
  667. };

  668. /* Structure containing arrays of per process hardware break-/watchpoints
  669.    for caching address and control information.

  670.    The Linux ptrace interface to hardware break-/watch-points presents the
  671.    values in a vector centred around 0 (which is used fo generic information).
  672.    Positive indicies refer to breakpoint addresses/control registers, negative
  673.    indices to watchpoint addresses/control registers.

  674.    The Linux vector is indexed as follows:
  675.       -((i << 1) + 2): Control register for watchpoint i.
  676.       -((i << 1) + 1): Address register for watchpoint i.
  677.                     0: Information register.
  678.        ((i << 1) + 1): Address register for breakpoint i.
  679.        ((i << 1) + 2): Control register for breakpoint i.

  680.    This structure is used as a per-thread cache of the state stored by the
  681.    kernel, so that we don't need to keep calling into the kernel to find a
  682.    free breakpoint.

  683.    We treat break-/watch-points with their enable bit clear as being deleted.
  684.    */
  685. struct arm_linux_debug_reg_state
  686. {
  687.   /* Hardware breakpoints for this process.  */
  688.   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
  689.   /* Hardware watchpoints for this process.  */
  690.   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
  691. };

  692. /* Per-process arch-specific data we want to keep.  */
  693. struct arm_linux_process_info
  694. {
  695.   /* Linked list.  */
  696.   struct arm_linux_process_info *next;
  697.   /* The process identifier.  */
  698.   pid_t pid;
  699.   /* Hardware break-/watchpoints state information.  */
  700.   struct arm_linux_debug_reg_state state;

  701. };

  702. /* Per-thread arch-specific data we want to keep.  */
  703. struct arch_lwp_info
  704. {
  705.   /* Non-zero if our copy differs from what's recorded in the thread.  */
  706.   char bpts_changed[MAX_BPTS];
  707.   char wpts_changed[MAX_WPTS];
  708. };

  709. static struct arm_linux_process_info *arm_linux_process_list = NULL;

  710. /* Find process data for process PID.  */

  711. static struct arm_linux_process_info *
  712. arm_linux_find_process_pid (pid_t pid)
  713. {
  714.   struct arm_linux_process_info *proc;

  715.   for (proc = arm_linux_process_list; proc; proc = proc->next)
  716.     if (proc->pid == pid)
  717.       return proc;

  718.   return NULL;
  719. }

  720. /* Add process data for process PID.  Returns newly allocated info
  721.    object.  */

  722. static struct arm_linux_process_info *
  723. arm_linux_add_process (pid_t pid)
  724. {
  725.   struct arm_linux_process_info *proc;

  726.   proc = xcalloc (1, sizeof (*proc));
  727.   proc->pid = pid;

  728.   proc->next = arm_linux_process_list;
  729.   arm_linux_process_list = proc;

  730.   return proc;
  731. }

  732. /* Get data specific info for process PID, creating it if necessary.
  733.    Never returns NULL.  */

  734. static struct arm_linux_process_info *
  735. arm_linux_process_info_get (pid_t pid)
  736. {
  737.   struct arm_linux_process_info *proc;

  738.   proc = arm_linux_find_process_pid (pid);
  739.   if (proc == NULL)
  740.     proc = arm_linux_add_process (pid);

  741.   return proc;
  742. }

  743. /* Called whenever GDB is no longer debugging process PID.  It deletes
  744.    data structures that keep track of debug register state.  */

  745. static void
  746. arm_linux_forget_process (pid_t pid)
  747. {
  748.   struct arm_linux_process_info *proc, **proc_link;

  749.   proc = arm_linux_process_list;
  750.   proc_link = &arm_linux_process_list;

  751.   while (proc != NULL)
  752.     {
  753.       if (proc->pid == pid)
  754.     {
  755.       *proc_link = proc->next;

  756.       xfree (proc);
  757.       return;
  758.     }

  759.       proc_link = &proc->next;
  760.       proc = *proc_link;
  761.     }
  762. }

  763. /* Get hardware break-/watchpoint state for process PID.  */

  764. static struct arm_linux_debug_reg_state *
  765. arm_linux_get_debug_reg_state (pid_t pid)
  766. {
  767.   return &arm_linux_process_info_get (pid)->state;
  768. }

  769. /* Initialize an ARM hardware break-/watch-point control register value.
  770.    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
  771.    type of break-/watch-point; ENABLE indicates whether the point is enabled.
  772.    */
  773. static arm_hwbp_control_t
  774. arm_hwbp_control_initialize (unsigned byte_address_select,
  775.                              arm_hwbp_type hwbp_type,
  776.                              int enable)
  777. {
  778.   gdb_assert ((byte_address_select & ~0xffU) == 0);
  779.   gdb_assert (hwbp_type != arm_hwbp_break
  780.               || ((byte_address_select & 0xfU) != 0));

  781.   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
  782. }

  783. /* Does the breakpoint control value CONTROL have the enable bit set?  */
  784. static int
  785. arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
  786. {
  787.   return control & 0x1;
  788. }

  789. /* Change a breakpoint control word so that it is in the disabled state.  */
  790. static arm_hwbp_control_t
  791. arm_hwbp_control_disable (arm_hwbp_control_t control)
  792. {
  793.   return control & ~0x1;
  794. }

  795. /* Initialise the hardware breakpoint structure P.  The breakpoint will be
  796.    enabled, and will point to the placed address of BP_TGT.  */
  797. static void
  798. arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
  799.                                     struct bp_target_info *bp_tgt,
  800.                                     struct arm_linux_hw_breakpoint *p)
  801. {
  802.   unsigned mask;
  803.   CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;

  804.   /* We have to create a mask for the control register which says which bits
  805.      of the word pointed to by address to break on.  */
  806.   if (arm_pc_is_thumb (gdbarch, address))
  807.     {
  808.       mask = 0x3;
  809.       address &= ~1;
  810.     }
  811.   else
  812.     {
  813.       mask = 0xf;
  814.       address &= ~3;
  815.     }

  816.   p->address = (unsigned int) address;
  817.   p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
  818. }

  819. /* Get the ARM hardware breakpoint type from the RW value we're given when
  820.    asked to set a watchpoint.  */
  821. static arm_hwbp_type
  822. arm_linux_get_hwbp_type (int rw)
  823. {
  824.   if (rw == hw_read)
  825.     return arm_hwbp_load;
  826.   else if (rw == hw_write)
  827.     return arm_hwbp_store;
  828.   else
  829.     return arm_hwbp_access;
  830. }

  831. /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
  832.    to LEN.  The type of watchpoint is given in RW.  */
  833. static void
  834. arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
  835.                                     struct arm_linux_hw_breakpoint *p)
  836. {
  837.   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
  838.   unsigned mask;

  839.   gdb_assert (cap != NULL);
  840.   gdb_assert (cap->max_wp_length != 0);

  841.   mask = (1 << len) - 1;

  842.   p->address = (unsigned int) addr;
  843.   p->control = arm_hwbp_control_initialize (mask,
  844.                                             arm_linux_get_hwbp_type (rw), 1);
  845. }

  846. /* Are two break-/watch-points equal?  */
  847. static int
  848. arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
  849.                                const struct arm_linux_hw_breakpoint *p2)
  850. {
  851.   return p1->address == p2->address && p1->control == p2->control;
  852. }

  853. /* Callback to mark a watch-/breakpoint to be updated in all threads of
  854.    the current process.  */

  855. struct update_registers_data
  856. {
  857.   int watch;
  858.   int index;
  859. };

  860. static int
  861. update_registers_callback (struct lwp_info *lwp, void *arg)
  862. {
  863.   struct update_registers_data *data = (struct update_registers_data *) arg;

  864.   if (lwp->arch_private == NULL)
  865.     lwp->arch_private = XCNEW (struct arch_lwp_info);

  866.   /* The actual update is done later just before resuming the lwp,
  867.      we just mark that the registers need updating.  */
  868.   if (data->watch)
  869.     lwp->arch_private->wpts_changed[data->index] = 1;
  870.   else
  871.     lwp->arch_private->bpts_changed[data->index] = 1;

  872.   /* If the lwp isn't stopped, force it to momentarily pause, so
  873.      we can update its breakpoint registers.  */
  874.   if (!lwp->stopped)
  875.     linux_stop_lwp (lwp);

  876.   return 0;
  877. }

  878. /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
  879.    =1) BPT for thread TID.  */
  880. static void
  881. arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt,
  882.                                  int watchpoint)
  883. {
  884.   int pid;
  885.   ptid_t pid_ptid;
  886.   gdb_byte count, i;
  887.   struct arm_linux_hw_breakpoint* bpts;
  888.   struct update_registers_data data;

  889.   pid = ptid_get_pid (inferior_ptid);
  890.   pid_ptid = pid_to_ptid (pid);

  891.   if (watchpoint)
  892.     {
  893.       count = arm_linux_get_hw_watchpoint_count ();
  894.       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
  895.     }
  896.   else
  897.     {
  898.       count = arm_linux_get_hw_breakpoint_count ();
  899.       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
  900.     }

  901.   for (i = 0; i < count; ++i)
  902.     if (!arm_hwbp_control_is_enabled (bpts[i].control))
  903.       {
  904.         data.watch = watchpoint;
  905.         data.index = i;
  906.         bpts[i] = *bpt;
  907.         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
  908.         break;
  909.       }

  910.   gdb_assert (i != count);
  911. }

  912. /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
  913.    (WATCHPOINT = 1) BPT for thread TID.  */
  914. static void
  915. arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt,
  916.                                  int watchpoint)
  917. {
  918.   int pid;
  919.   gdb_byte count, i;
  920.   ptid_t pid_ptid;
  921.   struct arm_linux_hw_breakpoint* bpts;
  922.   struct update_registers_data data;

  923.   pid = ptid_get_pid (inferior_ptid);
  924.   pid_ptid = pid_to_ptid (pid);

  925.   if (watchpoint)
  926.     {
  927.       count = arm_linux_get_hw_watchpoint_count ();
  928.       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
  929.     }
  930.   else
  931.     {
  932.       count = arm_linux_get_hw_breakpoint_count ();
  933.       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
  934.     }

  935.   for (i = 0; i < count; ++i)
  936.     if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
  937.       {
  938.         data.watch = watchpoint;
  939.         data.index = i;
  940.         bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
  941.         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
  942.         break;
  943.       }

  944.   gdb_assert (i != count);
  945. }

  946. /* Insert a Hardware breakpoint.  */
  947. static int
  948. arm_linux_insert_hw_breakpoint (struct target_ops *self,
  949.                                 struct gdbarch *gdbarch,
  950.                                 struct bp_target_info *bp_tgt)
  951. {
  952.   struct lwp_info *lp;
  953.   struct arm_linux_hw_breakpoint p;

  954.   if (arm_linux_get_hw_breakpoint_count () == 0)
  955.     return -1;

  956.   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);

  957.   arm_linux_insert_hw_breakpoint1 (&p, 0);

  958.   return 0;
  959. }

  960. /* Remove a hardware breakpoint.  */
  961. static int
  962. arm_linux_remove_hw_breakpoint (struct target_ops *self,
  963.                                 struct gdbarch *gdbarch,
  964.                                 struct bp_target_info *bp_tgt)
  965. {
  966.   struct lwp_info *lp;
  967.   struct arm_linux_hw_breakpoint p;

  968.   if (arm_linux_get_hw_breakpoint_count () == 0)
  969.     return -1;

  970.   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);

  971.   arm_linux_remove_hw_breakpoint1 (&p, 0);

  972.   return 0;
  973. }

  974. /* Are we able to use a hardware watchpoint for the LEN bytes starting at
  975.    ADDR?  */
  976. static int
  977. arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
  978.                                        CORE_ADDR addr, int len)
  979. {
  980.   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
  981.   CORE_ADDR max_wp_length, aligned_addr;

  982.   /* Can not set watchpoints for zero or negative lengths.  */
  983.   if (len <= 0)
  984.     return 0;

  985.   /* Need to be able to use the ptrace interface.  */
  986.   if (cap == NULL || cap->wp_count == 0)
  987.     return 0;

  988.   /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
  989.      range covered by a watchpoint.  */
  990.   max_wp_length = (CORE_ADDR)cap->max_wp_length;
  991.   aligned_addr = addr & ~(max_wp_length - 1);

  992.   if (aligned_addr + max_wp_length < addr + len)
  993.     return 0;

  994.   /* The current ptrace interface can only handle watchpoints that are a
  995.      power of 2.  */
  996.   if ((len & (len - 1)) != 0)
  997.     return 0;

  998.   /* All tests passed so we must be able to set a watchpoint.  */
  999.   return 1;
  1000. }

  1001. /* Insert a Hardware breakpoint.  */
  1002. static int
  1003. arm_linux_insert_watchpoint (struct target_ops *self,
  1004.                              CORE_ADDR addr, int len, int rw,
  1005.                              struct expression *cond)
  1006. {
  1007.   struct lwp_info *lp;
  1008.   struct arm_linux_hw_breakpoint p;

  1009.   if (arm_linux_get_hw_watchpoint_count () == 0)
  1010.     return -1;

  1011.   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);

  1012.   arm_linux_insert_hw_breakpoint1 (&p, 1);

  1013.   return 0;
  1014. }

  1015. /* Remove a hardware breakpoint.  */
  1016. static int
  1017. arm_linux_remove_watchpoint (struct target_ops *self,
  1018.                              CORE_ADDR addr, int len, int rw,
  1019.                              struct expression *cond)
  1020. {
  1021.   struct lwp_info *lp;
  1022.   struct arm_linux_hw_breakpoint p;

  1023.   if (arm_linux_get_hw_watchpoint_count () == 0)
  1024.     return -1;

  1025.   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);

  1026.   arm_linux_remove_hw_breakpoint1 (&p, 1);

  1027.   return 0;
  1028. }

  1029. /* What was the data address the target was stopped on accessing.  */
  1030. static int
  1031. arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
  1032. {
  1033.   siginfo_t siginfo;
  1034.   int slot;

  1035.   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  1036.     return 0;

  1037.   /* This must be a hardware breakpoint.  */
  1038.   if (siginfo.si_signo != SIGTRAP
  1039.       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  1040.     return 0;

  1041.   /* We must be able to set hardware watchpoints.  */
  1042.   if (arm_linux_get_hw_watchpoint_count () == 0)
  1043.     return 0;

  1044.   slot = siginfo.si_errno;

  1045.   /* If we are in a positive slot then we're looking at a breakpoint and not
  1046.      a watchpoint.  */
  1047.   if (slot >= 0)
  1048.     return 0;

  1049.   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
  1050.   return 1;
  1051. }

  1052. /* Has the target been stopped by hitting a watchpoint?  */
  1053. static int
  1054. arm_linux_stopped_by_watchpoint (struct target_ops *ops)
  1055. {
  1056.   CORE_ADDR addr;
  1057.   return arm_linux_stopped_data_address (ops, &addr);
  1058. }

  1059. static int
  1060. arm_linux_watchpoint_addr_within_range (struct target_ops *target,
  1061.                                         CORE_ADDR addr,
  1062.                                         CORE_ADDR start, int length)
  1063. {
  1064.   return start <= addr && start + length - 1 >= addr;
  1065. }

  1066. /* Handle thread creation.  We need to copy the breakpoints and watchpoints
  1067.    in the parent thread to the child thread.  */
  1068. static void
  1069. arm_linux_new_thread (struct lwp_info *lp)
  1070. {
  1071.   int i;
  1072.   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);

  1073.   /* Mark that all the hardware breakpoint/watchpoint register pairs
  1074.      for this thread need to be initialized.  */

  1075.   for (i = 0; i < MAX_BPTS; i++)
  1076.     {
  1077.       info->bpts_changed[i] = 1;
  1078.       info->wpts_changed[i] = 1;
  1079.     }

  1080.   lp->arch_private = info;
  1081. }

  1082. /* Called when resuming a thread.
  1083.    The hardware debug registers are updated when there is any change.  */

  1084. static void
  1085. arm_linux_prepare_to_resume (struct lwp_info *lwp)
  1086. {
  1087.   int pid, i;
  1088.   struct arm_linux_hw_breakpoint *bpts, *wpts;
  1089.   struct arch_lwp_info *arm_lwp_info = lwp->arch_private;

  1090.   pid = ptid_get_lwp (lwp->ptid);
  1091.   bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
  1092.   wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;

  1093.   /* NULL means this is the main thread still going through the shell,
  1094.      or, no watchpoint has been set yet.  In that case, there's
  1095.      nothing to do.  */
  1096.   if (arm_lwp_info == NULL)
  1097.     return;

  1098.   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
  1099.     if (arm_lwp_info->bpts_changed[i])
  1100.       {
  1101.         errno = 0;
  1102.         if (arm_hwbp_control_is_enabled (bpts[i].control))
  1103.           if (ptrace (PTRACE_SETHBPREGS, pid,
  1104.               (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
  1105.             perror_with_name (_("Unexpected error setting breakpoint"));

  1106.         if (bpts[i].control != 0)
  1107.           if (ptrace (PTRACE_SETHBPREGS, pid,
  1108.               (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
  1109.             perror_with_name (_("Unexpected error setting breakpoint"));

  1110.         arm_lwp_info->bpts_changed[i] = 0;
  1111.       }

  1112.   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
  1113.     if (arm_lwp_info->wpts_changed[i])
  1114.       {
  1115.         errno = 0;
  1116.         if (arm_hwbp_control_is_enabled (wpts[i].control))
  1117.           if (ptrace (PTRACE_SETHBPREGS, pid,
  1118.               (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
  1119.             perror_with_name (_("Unexpected error setting watchpoint"));

  1120.         if (wpts[i].control != 0)
  1121.           if (ptrace (PTRACE_SETHBPREGS, pid,
  1122.               (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
  1123.             perror_with_name (_("Unexpected error setting watchpoint"));

  1124.         arm_lwp_info->wpts_changed[i] = 0;
  1125.       }
  1126. }

  1127. /* linux_nat_new_fork hook.  */

  1128. static void
  1129. arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
  1130. {
  1131.   pid_t parent_pid;
  1132.   struct arm_linux_debug_reg_state *parent_state;
  1133.   struct arm_linux_debug_reg_state *child_state;

  1134.   /* NULL means no watchpoint has ever been set in the parent.  In
  1135.      that case, there's nothing to do.  */
  1136.   if (parent->arch_private == NULL)
  1137.     return;

  1138.   /* GDB core assumes the child inherits the watchpoints/hw
  1139.      breakpoints of the parent, and will remove them all from the
  1140.      forked off process.  Copy the debug registers mirrors into the
  1141.      new process so that all breakpoints and watchpoints can be
  1142.      removed together.  */

  1143.   parent_pid = ptid_get_pid (parent->ptid);
  1144.   parent_state = arm_linux_get_debug_reg_state (parent_pid);
  1145.   child_state = arm_linux_get_debug_reg_state (child_pid);
  1146.   *child_state = *parent_state;
  1147. }

  1148. void _initialize_arm_linux_nat (void);

  1149. void
  1150. _initialize_arm_linux_nat (void)
  1151. {
  1152.   struct target_ops *t;

  1153.   /* Fill in the generic GNU/Linux methods.  */
  1154.   t = linux_target ();

  1155.   /* Add our register access methods.  */
  1156.   t->to_fetch_registers = arm_linux_fetch_inferior_registers;
  1157.   t->to_store_registers = arm_linux_store_inferior_registers;

  1158.   /* Add our hardware breakpoint and watchpoint implementation.  */
  1159.   t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
  1160.   t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
  1161.   t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
  1162.   t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
  1163.   t->to_insert_watchpoint = arm_linux_insert_watchpoint;
  1164.   t->to_remove_watchpoint = arm_linux_remove_watchpoint;
  1165.   t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
  1166.   t->to_stopped_data_address = arm_linux_stopped_data_address;
  1167.   t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;

  1168.   t->to_read_description = arm_linux_read_description;

  1169.   /* Register the target.  */
  1170.   linux_nat_add_target (t);

  1171.   /* Handle thread creation and exit.  */
  1172.   linux_nat_set_new_thread (t, arm_linux_new_thread);
  1173.   linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);

  1174.   /* Handle process creation and exit.  */
  1175.   linux_nat_set_new_fork (t, arm_linux_new_fork);
  1176.   linux_nat_set_forget_process (t, arm_linux_forget_process);
  1177. }