Remove global variable arm_linux_vfp_register_count in arm-linux-nat.c
[external/binutils.git] / gdb / arm-linux-nat.c
1 /* GNU/Linux on ARM native support.
2    Copyright (C) 1999-2015 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "defs.h"
20 #include "inferior.h"
21 #include "gdbcore.h"
22 #include "regcache.h"
23 #include "target.h"
24 #include "linux-nat.h"
25 #include "target-descriptions.h"
26 #include "auxv.h"
27 #include "observer.h"
28 #include "gdbthread.h"
29
30 #include "arm-tdep.h"
31 #include "arm-linux-tdep.h"
32
33 #include <elf/common.h>
34 #include <sys/user.h>
35 #include <sys/ptrace.h>
36 #include <sys/utsname.h>
37 #include <sys/procfs.h>
38
39 /* Prototypes for supply_gregset etc.  */
40 #include "gregset.h"
41
42 /* Defines ps_err_e, struct ps_prochandle.  */
43 #include "gdb_proc_service.h"
44
45 #ifndef PTRACE_GET_THREAD_AREA
46 #define PTRACE_GET_THREAD_AREA 22
47 #endif
48
49 #ifndef PTRACE_GETWMMXREGS
50 #define PTRACE_GETWMMXREGS 18
51 #define PTRACE_SETWMMXREGS 19
52 #endif
53
54 #ifndef PTRACE_GETVFPREGS
55 #define PTRACE_GETVFPREGS 27
56 #define PTRACE_SETVFPREGS 28
57 #endif
58
59 #ifndef PTRACE_GETHBPREGS
60 #define PTRACE_GETHBPREGS 29
61 #define PTRACE_SETHBPREGS 30
62 #endif
63
64 /* A flag for whether the WMMX registers are available.  */
65 static int arm_linux_has_wmmx_registers;
66
67 extern int arm_apcs_32;
68
69 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
70    case we may be tracing more than one process at a time.  In that
71    case, inferior_ptid will contain the main process ID and the
72    individual thread (process) ID.  get_thread_id () is used to get
73    the thread id if it's available, and the process id otherwise.  */
74
75 static int
76 get_thread_id (ptid_t ptid)
77 {
78   int tid = ptid_get_lwp (ptid);
79   if (0 == tid)
80     tid = ptid_get_pid (ptid);
81   return tid;
82 }
83
84 #define GET_THREAD_ID(PTID)     get_thread_id (PTID)
85
86 /* Get the value of a particular register from the floating point
87    state of the process and store it into regcache.  */
88
89 static void
90 fetch_fpregister (struct regcache *regcache, int regno)
91 {
92   int ret, tid;
93   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
94   
95   /* Get the thread id for the ptrace call.  */
96   tid = GET_THREAD_ID (inferior_ptid);
97
98   /* Read the floating point state.  */
99   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
100   if (ret < 0)
101     {
102       warning (_("Unable to fetch floating point register."));
103       return;
104     }
105
106   /* Fetch fpsr.  */
107   if (ARM_FPS_REGNUM == regno)
108     regcache_raw_supply (regcache, ARM_FPS_REGNUM,
109                          fp + NWFPE_FPSR_OFFSET);
110
111   /* Fetch the floating point register.  */
112   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
113     supply_nwfpe_register (regcache, regno, fp);
114 }
115
116 /* Get the whole floating point state of the process and store it
117    into regcache.  */
118
119 static void
120 fetch_fpregs (struct regcache *regcache)
121 {
122   int ret, regno, tid;
123   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
124
125   /* Get the thread id for the ptrace call.  */
126   tid = GET_THREAD_ID (inferior_ptid);
127   
128   /* Read the floating point state.  */
129   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
130   if (ret < 0)
131     {
132       warning (_("Unable to fetch the floating point registers."));
133       return;
134     }
135
136   /* Fetch fpsr.  */
137   regcache_raw_supply (regcache, ARM_FPS_REGNUM,
138                        fp + NWFPE_FPSR_OFFSET);
139
140   /* Fetch the floating point registers.  */
141   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
142     supply_nwfpe_register (regcache, regno, fp);
143 }
144
145 /* Save a particular register into the floating point state of the
146    process using the contents from regcache.  */
147
148 static void
149 store_fpregister (const struct regcache *regcache, int regno)
150 {
151   int ret, tid;
152   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
153
154   /* Get the thread id for the ptrace call.  */
155   tid = GET_THREAD_ID (inferior_ptid);
156   
157   /* Read the floating point state.  */
158   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
159   if (ret < 0)
160     {
161       warning (_("Unable to fetch the floating point registers."));
162       return;
163     }
164
165   /* Store fpsr.  */
166   if (ARM_FPS_REGNUM == regno
167       && REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
168     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
169
170   /* Store the floating point register.  */
171   if (regno >= ARM_F0_REGNUM && regno <= ARM_F7_REGNUM)
172     collect_nwfpe_register (regcache, regno, fp);
173
174   ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
175   if (ret < 0)
176     {
177       warning (_("Unable to store floating point register."));
178       return;
179     }
180 }
181
182 /* Save the whole floating point state of the process using
183    the contents from regcache.  */
184
185 static void
186 store_fpregs (const struct regcache *regcache)
187 {
188   int ret, regno, tid;
189   gdb_byte fp[ARM_LINUX_SIZEOF_NWFPE];
190
191   /* Get the thread id for the ptrace call.  */
192   tid = GET_THREAD_ID (inferior_ptid);
193   
194   /* Read the floating point state.  */
195   ret = ptrace (PT_GETFPREGS, tid, 0, fp);
196   if (ret < 0)
197     {
198       warning (_("Unable to fetch the floating point registers."));
199       return;
200     }
201
202   /* Store fpsr.  */
203   if (REG_VALID == regcache_register_status (regcache, ARM_FPS_REGNUM))
204     regcache_raw_collect (regcache, ARM_FPS_REGNUM, fp + NWFPE_FPSR_OFFSET);
205
206   /* Store the floating point registers.  */
207   for (regno = ARM_F0_REGNUM; regno <= ARM_F7_REGNUM; regno++)
208     if (REG_VALID == regcache_register_status (regcache, regno))
209       collect_nwfpe_register (regcache, regno, fp);
210
211   ret = ptrace (PTRACE_SETFPREGS, tid, 0, fp);
212   if (ret < 0)
213     {
214       warning (_("Unable to store floating point registers."));
215       return;
216     }
217 }
218
219 /* Fetch a general register of the process and store into
220    regcache.  */
221
222 static void
223 fetch_register (struct regcache *regcache, int regno)
224 {
225   int ret, tid;
226   elf_gregset_t regs;
227
228   /* Get the thread id for the ptrace call.  */
229   tid = GET_THREAD_ID (inferior_ptid);
230   
231   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
232   if (ret < 0)
233     {
234       warning (_("Unable to fetch general register."));
235       return;
236     }
237
238   if (regno >= ARM_A1_REGNUM && regno < ARM_PC_REGNUM)
239     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
240
241   if (ARM_PS_REGNUM == regno)
242     {
243       if (arm_apcs_32)
244         regcache_raw_supply (regcache, ARM_PS_REGNUM,
245                              (char *) &regs[ARM_CPSR_GREGNUM]);
246       else
247         regcache_raw_supply (regcache, ARM_PS_REGNUM,
248                              (char *) &regs[ARM_PC_REGNUM]);
249     }
250     
251   if (ARM_PC_REGNUM == regno)
252     { 
253       regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
254                               (get_regcache_arch (regcache),
255                                regs[ARM_PC_REGNUM]);
256       regcache_raw_supply (regcache, ARM_PC_REGNUM,
257                            (char *) &regs[ARM_PC_REGNUM]);
258     }
259 }
260
261 /* Fetch all general registers of the process and store into
262    regcache.  */
263
264 static void
265 fetch_regs (struct regcache *regcache)
266 {
267   int ret, regno, tid;
268   elf_gregset_t regs;
269
270   /* Get the thread id for the ptrace call.  */
271   tid = GET_THREAD_ID (inferior_ptid);
272   
273   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
274   if (ret < 0)
275     {
276       warning (_("Unable to fetch general registers."));
277       return;
278     }
279
280   for (regno = ARM_A1_REGNUM; regno < ARM_PC_REGNUM; regno++)
281     regcache_raw_supply (regcache, regno, (char *) &regs[regno]);
282
283   if (arm_apcs_32)
284     regcache_raw_supply (regcache, ARM_PS_REGNUM,
285                          (char *) &regs[ARM_CPSR_GREGNUM]);
286   else
287     regcache_raw_supply (regcache, ARM_PS_REGNUM,
288                          (char *) &regs[ARM_PC_REGNUM]);
289
290   regs[ARM_PC_REGNUM] = gdbarch_addr_bits_remove
291                           (get_regcache_arch (regcache), regs[ARM_PC_REGNUM]);
292   regcache_raw_supply (regcache, ARM_PC_REGNUM,
293                        (char *) &regs[ARM_PC_REGNUM]);
294 }
295
296 /* Store all general registers of the process from the values in
297    regcache.  */
298
299 static void
300 store_register (const struct regcache *regcache, int regno)
301 {
302   int ret, tid;
303   elf_gregset_t regs;
304   
305   if (REG_VALID != regcache_register_status (regcache, regno))
306     return;
307
308   /* Get the thread id for the ptrace call.  */
309   tid = GET_THREAD_ID (inferior_ptid);
310   
311   /* Get the general registers from the process.  */
312   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
313   if (ret < 0)
314     {
315       warning (_("Unable to fetch general registers."));
316       return;
317     }
318
319   if (regno >= ARM_A1_REGNUM && regno <= ARM_PC_REGNUM)
320     regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
321   else if (arm_apcs_32 && regno == ARM_PS_REGNUM)
322     regcache_raw_collect (regcache, regno,
323                          (char *) &regs[ARM_CPSR_GREGNUM]);
324   else if (!arm_apcs_32 && regno == ARM_PS_REGNUM)
325     regcache_raw_collect (regcache, ARM_PC_REGNUM,
326                          (char *) &regs[ARM_PC_REGNUM]);
327
328   ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
329   if (ret < 0)
330     {
331       warning (_("Unable to store general register."));
332       return;
333     }
334 }
335
336 static void
337 store_regs (const struct regcache *regcache)
338 {
339   int ret, regno, tid;
340   elf_gregset_t regs;
341
342   /* Get the thread id for the ptrace call.  */
343   tid = GET_THREAD_ID (inferior_ptid);
344   
345   /* Fetch the general registers.  */
346   ret = ptrace (PTRACE_GETREGS, tid, 0, &regs);
347   if (ret < 0)
348     {
349       warning (_("Unable to fetch general registers."));
350       return;
351     }
352
353   for (regno = ARM_A1_REGNUM; regno <= ARM_PC_REGNUM; regno++)
354     {
355       if (REG_VALID == regcache_register_status (regcache, regno))
356         regcache_raw_collect (regcache, regno, (char *) &regs[regno]);
357     }
358
359   if (arm_apcs_32 && REG_VALID == regcache_register_status (regcache, ARM_PS_REGNUM))
360     regcache_raw_collect (regcache, ARM_PS_REGNUM,
361                          (char *) &regs[ARM_CPSR_GREGNUM]);
362
363   ret = ptrace (PTRACE_SETREGS, tid, 0, &regs);
364
365   if (ret < 0)
366     {
367       warning (_("Unable to store general registers."));
368       return;
369     }
370 }
371
372 /* Fetch all WMMX registers of the process and store into
373    regcache.  */
374
375 #define IWMMXT_REGS_SIZE (16 * 8 + 6 * 4)
376
377 static void
378 fetch_wmmx_regs (struct regcache *regcache)
379 {
380   char regbuf[IWMMXT_REGS_SIZE];
381   int ret, regno, tid;
382
383   /* Get the thread id for the ptrace call.  */
384   tid = GET_THREAD_ID (inferior_ptid);
385
386   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
387   if (ret < 0)
388     {
389       warning (_("Unable to fetch WMMX registers."));
390       return;
391     }
392
393   for (regno = 0; regno < 16; regno++)
394     regcache_raw_supply (regcache, regno + ARM_WR0_REGNUM,
395                          &regbuf[regno * 8]);
396
397   for (regno = 0; regno < 2; regno++)
398     regcache_raw_supply (regcache, regno + ARM_WCSSF_REGNUM,
399                          &regbuf[16 * 8 + regno * 4]);
400
401   for (regno = 0; regno < 4; regno++)
402     regcache_raw_supply (regcache, regno + ARM_WCGR0_REGNUM,
403                          &regbuf[16 * 8 + 2 * 4 + regno * 4]);
404 }
405
406 static void
407 store_wmmx_regs (const struct regcache *regcache)
408 {
409   char regbuf[IWMMXT_REGS_SIZE];
410   int ret, regno, tid;
411
412   /* Get the thread id for the ptrace call.  */
413   tid = GET_THREAD_ID (inferior_ptid);
414
415   ret = ptrace (PTRACE_GETWMMXREGS, tid, 0, regbuf);
416   if (ret < 0)
417     {
418       warning (_("Unable to fetch WMMX registers."));
419       return;
420     }
421
422   for (regno = 0; regno < 16; regno++)
423     if (REG_VALID == regcache_register_status (regcache,
424                                                regno + ARM_WR0_REGNUM))
425       regcache_raw_collect (regcache, regno + ARM_WR0_REGNUM,
426                             &regbuf[regno * 8]);
427
428   for (regno = 0; regno < 2; regno++)
429     if (REG_VALID == regcache_register_status (regcache,
430                                                regno + ARM_WCSSF_REGNUM))
431       regcache_raw_collect (regcache, regno + ARM_WCSSF_REGNUM,
432                             &regbuf[16 * 8 + regno * 4]);
433
434   for (regno = 0; regno < 4; regno++)
435     if (REG_VALID == regcache_register_status (regcache,
436                                                regno + ARM_WCGR0_REGNUM))
437       regcache_raw_collect (regcache, regno + ARM_WCGR0_REGNUM,
438                             &regbuf[16 * 8 + 2 * 4 + regno * 4]);
439
440   ret = ptrace (PTRACE_SETWMMXREGS, tid, 0, regbuf);
441
442   if (ret < 0)
443     {
444       warning (_("Unable to store WMMX registers."));
445       return;
446     }
447 }
448
449 /* Fetch and store VFP Registers.  The kernel object has space for 32
450    64-bit registers, and the FPSCR.  This is even when on a VFPv2 or
451    VFPv3D16 target.  */
452 #define VFP_REGS_SIZE (32 * 8 + 4)
453
454 static void
455 fetch_vfp_regs (struct regcache *regcache)
456 {
457   char regbuf[VFP_REGS_SIZE];
458   int ret, regno, tid;
459   struct gdbarch *gdbarch = get_regcache_arch (regcache);
460   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
461
462   /* Get the thread id for the ptrace call.  */
463   tid = GET_THREAD_ID (inferior_ptid);
464
465   ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
466   if (ret < 0)
467     {
468       warning (_("Unable to fetch VFP registers."));
469       return;
470     }
471
472   for (regno = 0; regno < tdep->vfp_register_count; regno++)
473     regcache_raw_supply (regcache, regno + ARM_D0_REGNUM,
474                          (char *) regbuf + regno * 8);
475
476   regcache_raw_supply (regcache, ARM_FPSCR_REGNUM,
477                        (char *) regbuf + 32 * 8);
478 }
479
480 static void
481 store_vfp_regs (const struct regcache *regcache)
482 {
483   char regbuf[VFP_REGS_SIZE];
484   int ret, regno, tid;
485   struct gdbarch *gdbarch = get_regcache_arch (regcache);
486   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
487
488   /* Get the thread id for the ptrace call.  */
489   tid = GET_THREAD_ID (inferior_ptid);
490
491   ret = ptrace (PTRACE_GETVFPREGS, tid, 0, regbuf);
492   if (ret < 0)
493     {
494       warning (_("Unable to fetch VFP registers (for update)."));
495       return;
496     }
497
498   for (regno = 0; regno < tdep->vfp_register_count; regno++)
499     regcache_raw_collect (regcache, regno + ARM_D0_REGNUM,
500                           (char *) regbuf + regno * 8);
501
502   regcache_raw_collect (regcache, ARM_FPSCR_REGNUM,
503                         (char *) regbuf + 32 * 8);
504
505   ret = ptrace (PTRACE_SETVFPREGS, tid, 0, regbuf);
506
507   if (ret < 0)
508     {
509       warning (_("Unable to store VFP registers."));
510       return;
511     }
512 }
513
514 /* Fetch registers from the child process.  Fetch all registers if
515    regno == -1, otherwise fetch all general registers or all floating
516    point registers depending upon the value of regno.  */
517
518 static void
519 arm_linux_fetch_inferior_registers (struct target_ops *ops,
520                                     struct regcache *regcache, int regno)
521 {
522   struct gdbarch *gdbarch = get_regcache_arch (regcache);
523   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
524
525   if (-1 == regno)
526     {
527       fetch_regs (regcache);
528       fetch_fpregs (regcache);
529       if (arm_linux_has_wmmx_registers)
530         fetch_wmmx_regs (regcache);
531       if (tdep->vfp_register_count > 0)
532         fetch_vfp_regs (regcache);
533     }
534   else 
535     {
536       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
537         fetch_register (regcache, regno);
538       else if (regno >= ARM_F0_REGNUM && regno <= ARM_FPS_REGNUM)
539         fetch_fpregister (regcache, regno);
540       else if (arm_linux_has_wmmx_registers
541                && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
542         fetch_wmmx_regs (regcache);
543       else if (tdep->vfp_register_count > 0
544                && regno >= ARM_D0_REGNUM
545                && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
546         fetch_vfp_regs (regcache);
547     }
548 }
549
550 /* Store registers back into the inferior.  Store all registers if
551    regno == -1, otherwise store all general registers or all floating
552    point registers depending upon the value of regno.  */
553
554 static void
555 arm_linux_store_inferior_registers (struct target_ops *ops,
556                                     struct regcache *regcache, int regno)
557 {
558   struct gdbarch *gdbarch = get_regcache_arch (regcache);
559   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
560
561   if (-1 == regno)
562     {
563       store_regs (regcache);
564       store_fpregs (regcache);
565       if (arm_linux_has_wmmx_registers)
566         store_wmmx_regs (regcache);
567       if (tdep->vfp_register_count > 0)
568         store_vfp_regs (regcache);
569     }
570   else
571     {
572       if (regno < ARM_F0_REGNUM || regno == ARM_PS_REGNUM)
573         store_register (regcache, regno);
574       else if ((regno >= ARM_F0_REGNUM) && (regno <= ARM_FPS_REGNUM))
575         store_fpregister (regcache, regno);
576       else if (arm_linux_has_wmmx_registers
577                && regno >= ARM_WR0_REGNUM && regno <= ARM_WCGR7_REGNUM)
578         store_wmmx_regs (regcache);
579       else if (tdep->vfp_register_count > 0
580                && regno >= ARM_D0_REGNUM
581                && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
582         store_vfp_regs (regcache);
583     }
584 }
585
586 /* Wrapper functions for the standard regset handling, used by
587    thread debugging.  */
588
589 void
590 fill_gregset (const struct regcache *regcache,  
591               gdb_gregset_t *gregsetp, int regno)
592 {
593   arm_linux_collect_gregset (NULL, regcache, regno, gregsetp, 0);
594 }
595
596 void
597 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
598 {
599   arm_linux_supply_gregset (NULL, regcache, -1, gregsetp, 0);
600 }
601
602 void
603 fill_fpregset (const struct regcache *regcache,
604                gdb_fpregset_t *fpregsetp, int regno)
605 {
606   arm_linux_collect_nwfpe (NULL, regcache, regno, fpregsetp, 0);
607 }
608
609 /* Fill GDB's register array with the floating-point register values
610    in *fpregsetp.  */
611
612 void
613 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
614 {
615   arm_linux_supply_nwfpe (NULL, regcache, -1, fpregsetp, 0);
616 }
617
618 /* Fetch the thread-local storage pointer for libthread_db.  */
619
620 ps_err_e
621 ps_get_thread_area (const struct ps_prochandle *ph,
622                     lwpid_t lwpid, int idx, void **base)
623 {
624   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
625     return PS_ERR;
626
627   /* IDX is the bias from the thread pointer to the beginning of the
628      thread descriptor.  It has to be subtracted due to implementation
629      quirks in libthread_db.  */
630   *base = (void *) ((char *)*base - idx);
631
632   return PS_OK;
633 }
634
635 static const struct target_desc *
636 arm_linux_read_description (struct target_ops *ops)
637 {
638   CORE_ADDR arm_hwcap = 0;
639   arm_linux_has_wmmx_registers = 0;
640
641   if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1)
642     {
643       return ops->beneath->to_read_description (ops->beneath);
644     }
645
646   if (arm_hwcap & HWCAP_IWMMXT)
647     {
648       arm_linux_has_wmmx_registers = 1;
649       return tdesc_arm_with_iwmmxt;
650     }
651
652   if (arm_hwcap & HWCAP_VFP)
653     {
654       int pid;
655       char *buf;
656       const struct target_desc * result = NULL;
657
658       /* NEON implies VFPv3-D32 or no-VFP unit.  Say that we only support
659          Neon with VFPv3-D32.  */
660       if (arm_hwcap & HWCAP_NEON)
661         result = tdesc_arm_with_neon;
662       else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
663         result = tdesc_arm_with_vfpv3;
664       else
665         result = tdesc_arm_with_vfpv2;
666
667       /* Now make sure that the kernel supports reading these
668          registers.  Support was added in 2.6.30.  */
669       pid = ptid_get_lwp (inferior_ptid);
670       errno = 0;
671       buf = alloca (VFP_REGS_SIZE);
672       if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0
673           && errno == EIO)
674         result = NULL;
675
676       return result;
677     }
678
679   return ops->beneath->to_read_description (ops->beneath);
680 }
681
682 /* Information describing the hardware breakpoint capabilities.  */
683 struct arm_linux_hwbp_cap
684 {
685   gdb_byte arch;
686   gdb_byte max_wp_length;
687   gdb_byte wp_count;
688   gdb_byte bp_count;
689 };
690
691 /* Since we cannot dynamically allocate subfields of arm_linux_process_info,
692    assume a maximum number of supported break-/watchpoints.  */
693 #define MAX_BPTS 16
694 #define MAX_WPTS 16
695
696 /* Get hold of the Hardware Breakpoint information for the target we are
697    attached to.  Returns NULL if the kernel doesn't support Hardware 
698    breakpoints at all, or a pointer to the information structure.  */
699 static const struct arm_linux_hwbp_cap *
700 arm_linux_get_hwbp_cap (void)
701 {
702   /* The info structure we return.  */
703   static struct arm_linux_hwbp_cap info;
704
705   /* Is INFO in a good state?  -1 means that no attempt has been made to
706      initialize INFO; 0 means an attempt has been made, but it failed; 1
707      means INFO is in an initialized state.  */
708   static int available = -1;
709
710   if (available == -1)
711     {
712       int tid;
713       unsigned int val;
714
715       tid = GET_THREAD_ID (inferior_ptid);
716       if (ptrace (PTRACE_GETHBPREGS, tid, 0, &val) < 0)
717         available = 0;
718       else
719         {
720           info.arch = (gdb_byte)((val >> 24) & 0xff);
721           info.max_wp_length = (gdb_byte)((val >> 16) & 0xff);
722           info.wp_count = (gdb_byte)((val >> 8) & 0xff);
723           info.bp_count = (gdb_byte)(val & 0xff);
724
725       if (info.wp_count > MAX_WPTS)
726         {
727           warning (_("arm-linux-gdb supports %d hardware watchpoints but target \
728                       supports %d"), MAX_WPTS, info.wp_count);
729           info.wp_count = MAX_WPTS;
730         }
731
732       if (info.bp_count > MAX_BPTS)
733         {
734           warning (_("arm-linux-gdb supports %d hardware breakpoints but target \
735                       supports %d"), MAX_BPTS, info.bp_count);
736           info.bp_count = MAX_BPTS;
737         }
738           available = (info.arch != 0);
739         }
740     }
741
742   return available == 1 ? &info : NULL;
743 }
744
745 /* How many hardware breakpoints are available?  */
746 static int
747 arm_linux_get_hw_breakpoint_count (void)
748 {
749   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
750   return cap != NULL ? cap->bp_count : 0;
751 }
752
753 /* How many hardware watchpoints are available?  */
754 static int
755 arm_linux_get_hw_watchpoint_count (void)
756 {
757   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
758   return cap != NULL ? cap->wp_count : 0;
759 }
760
761 /* Have we got a free break-/watch-point available for use?  Returns -1 if
762    there is not an appropriate resource available, otherwise returns 1.  */
763 static int
764 arm_linux_can_use_hw_breakpoint (struct target_ops *self,
765                                  int type, int cnt, int ot)
766 {
767   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
768       || type == bp_access_watchpoint || type == bp_watchpoint)
769     {
770       int count = arm_linux_get_hw_watchpoint_count ();
771
772       if (count == 0)
773         return 0;
774       else if (cnt + ot > count)
775         return -1;
776     }
777   else if (type == bp_hardware_breakpoint)
778     {
779       int count = arm_linux_get_hw_breakpoint_count ();
780
781       if (count == 0)
782         return 0;
783       else if (cnt > count)
784         return -1;
785     }
786   else
787     gdb_assert (FALSE);
788
789   return 1;
790 }
791
792 /* Enum describing the different types of ARM hardware break-/watch-points.  */
793 typedef enum
794 {
795   arm_hwbp_break = 0,
796   arm_hwbp_load = 1,
797   arm_hwbp_store = 2,
798   arm_hwbp_access = 3
799 } arm_hwbp_type;
800
801 /* Type describing an ARM Hardware Breakpoint Control register value.  */
802 typedef unsigned int arm_hwbp_control_t;
803
804 /* Structure used to keep track of hardware break-/watch-points.  */
805 struct arm_linux_hw_breakpoint
806 {
807   /* Address to break on, or being watched.  */
808   unsigned int address;
809   /* Control register for break-/watch- point.  */
810   arm_hwbp_control_t control;
811 };
812
813 /* Structure containing arrays of per process hardware break-/watchpoints
814    for caching address and control information.
815
816    The Linux ptrace interface to hardware break-/watch-points presents the 
817    values in a vector centred around 0 (which is used fo generic information).
818    Positive indicies refer to breakpoint addresses/control registers, negative
819    indices to watchpoint addresses/control registers.
820
821    The Linux vector is indexed as follows:
822       -((i << 1) + 2): Control register for watchpoint i.
823       -((i << 1) + 1): Address register for watchpoint i.
824                     0: Information register.
825        ((i << 1) + 1): Address register for breakpoint i.
826        ((i << 1) + 2): Control register for breakpoint i.
827
828    This structure is used as a per-thread cache of the state stored by the 
829    kernel, so that we don't need to keep calling into the kernel to find a 
830    free breakpoint.
831
832    We treat break-/watch-points with their enable bit clear as being deleted.
833    */
834 struct arm_linux_debug_reg_state
835 {
836   /* Hardware breakpoints for this process.  */
837   struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
838   /* Hardware watchpoints for this process.  */
839   struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
840 };
841
842 /* Per-process arch-specific data we want to keep.  */
843 struct arm_linux_process_info
844 {
845   /* Linked list.  */
846   struct arm_linux_process_info *next;
847   /* The process identifier.  */
848   pid_t pid;
849   /* Hardware break-/watchpoints state information.  */
850   struct arm_linux_debug_reg_state state;
851
852 };
853
854 /* Per-thread arch-specific data we want to keep.  */
855 struct arch_lwp_info
856 {
857   /* Non-zero if our copy differs from what's recorded in the thread.  */
858   char bpts_changed[MAX_BPTS];
859   char wpts_changed[MAX_WPTS];
860 };
861
862 static struct arm_linux_process_info *arm_linux_process_list = NULL;
863
864 /* Find process data for process PID.  */
865
866 static struct arm_linux_process_info *
867 arm_linux_find_process_pid (pid_t pid)
868 {
869   struct arm_linux_process_info *proc;
870
871   for (proc = arm_linux_process_list; proc; proc = proc->next)
872     if (proc->pid == pid)
873       return proc;
874
875   return NULL;
876 }
877
878 /* Add process data for process PID.  Returns newly allocated info
879    object.  */
880
881 static struct arm_linux_process_info *
882 arm_linux_add_process (pid_t pid)
883 {
884   struct arm_linux_process_info *proc;
885
886   proc = xcalloc (1, sizeof (*proc));
887   proc->pid = pid;
888
889   proc->next = arm_linux_process_list;
890   arm_linux_process_list = proc;
891
892   return proc;
893 }
894
895 /* Get data specific info for process PID, creating it if necessary.
896    Never returns NULL.  */
897
898 static struct arm_linux_process_info *
899 arm_linux_process_info_get (pid_t pid)
900 {
901   struct arm_linux_process_info *proc;
902
903   proc = arm_linux_find_process_pid (pid);
904   if (proc == NULL)
905     proc = arm_linux_add_process (pid);
906
907   return proc;
908 }
909
910 /* Called whenever GDB is no longer debugging process PID.  It deletes
911    data structures that keep track of debug register state.  */
912
913 static void
914 arm_linux_forget_process (pid_t pid)
915 {
916   struct arm_linux_process_info *proc, **proc_link;
917
918   proc = arm_linux_process_list;
919   proc_link = &arm_linux_process_list;
920
921   while (proc != NULL)
922     {
923       if (proc->pid == pid)
924     {
925       *proc_link = proc->next;
926
927       xfree (proc);
928       return;
929     }
930
931       proc_link = &proc->next;
932       proc = *proc_link;
933     }
934 }
935
936 /* Get hardware break-/watchpoint state for process PID.  */
937
938 static struct arm_linux_debug_reg_state *
939 arm_linux_get_debug_reg_state (pid_t pid)
940 {
941   return &arm_linux_process_info_get (pid)->state;
942 }
943
944 /* Initialize an ARM hardware break-/watch-point control register value.
945    BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the 
946    type of break-/watch-point; ENABLE indicates whether the point is enabled.
947    */
948 static arm_hwbp_control_t 
949 arm_hwbp_control_initialize (unsigned byte_address_select,
950                              arm_hwbp_type hwbp_type,
951                              int enable)
952 {
953   gdb_assert ((byte_address_select & ~0xffU) == 0);
954   gdb_assert (hwbp_type != arm_hwbp_break 
955               || ((byte_address_select & 0xfU) != 0));
956
957   return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
958 }
959
960 /* Does the breakpoint control value CONTROL have the enable bit set?  */
961 static int
962 arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
963 {
964   return control & 0x1;
965 }
966
967 /* Change a breakpoint control word so that it is in the disabled state.  */
968 static arm_hwbp_control_t
969 arm_hwbp_control_disable (arm_hwbp_control_t control)
970 {
971   return control & ~0x1;
972 }
973
974 /* Initialise the hardware breakpoint structure P.  The breakpoint will be
975    enabled, and will point to the placed address of BP_TGT.  */
976 static void
977 arm_linux_hw_breakpoint_initialize (struct gdbarch *gdbarch,
978                                     struct bp_target_info *bp_tgt,
979                                     struct arm_linux_hw_breakpoint *p)
980 {
981   unsigned mask;
982   CORE_ADDR address = bp_tgt->placed_address = bp_tgt->reqstd_address;
983
984   /* We have to create a mask for the control register which says which bits
985      of the word pointed to by address to break on.  */
986   if (arm_pc_is_thumb (gdbarch, address))
987     {
988       mask = 0x3;
989       address &= ~1;
990     }
991   else
992     {
993       mask = 0xf;
994       address &= ~3;
995     }
996
997   p->address = (unsigned int) address;
998   p->control = arm_hwbp_control_initialize (mask, arm_hwbp_break, 1);
999 }
1000
1001 /* Get the ARM hardware breakpoint type from the RW value we're given when
1002    asked to set a watchpoint.  */
1003 static arm_hwbp_type 
1004 arm_linux_get_hwbp_type (int rw)
1005 {
1006   if (rw == hw_read)
1007     return arm_hwbp_load;
1008   else if (rw == hw_write)
1009     return arm_hwbp_store;
1010   else
1011     return arm_hwbp_access;
1012 }
1013
1014 /* Initialize the hardware breakpoint structure P for a watchpoint at ADDR
1015    to LEN.  The type of watchpoint is given in RW.  */
1016 static void
1017 arm_linux_hw_watchpoint_initialize (CORE_ADDR addr, int len, int rw,
1018                                     struct arm_linux_hw_breakpoint *p)
1019 {
1020   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1021   unsigned mask;
1022
1023   gdb_assert (cap != NULL);
1024   gdb_assert (cap->max_wp_length != 0);
1025
1026   mask = (1 << len) - 1;
1027
1028   p->address = (unsigned int) addr;
1029   p->control = arm_hwbp_control_initialize (mask, 
1030                                             arm_linux_get_hwbp_type (rw), 1);
1031 }
1032
1033 /* Are two break-/watch-points equal?  */
1034 static int
1035 arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
1036                                const struct arm_linux_hw_breakpoint *p2)
1037 {
1038   return p1->address == p2->address && p1->control == p2->control;
1039 }
1040
1041 /* Callback to mark a watch-/breakpoint to be updated in all threads of
1042    the current process.  */
1043
1044 struct update_registers_data
1045 {
1046   int watch;
1047   int index;
1048 };
1049
1050 static int
1051 update_registers_callback (struct lwp_info *lwp, void *arg)
1052 {
1053   struct update_registers_data *data = (struct update_registers_data *) arg;
1054
1055   if (lwp->arch_private == NULL)
1056     lwp->arch_private = XCNEW (struct arch_lwp_info);
1057
1058   /* The actual update is done later just before resuming the lwp,
1059      we just mark that the registers need updating.  */
1060   if (data->watch)
1061     lwp->arch_private->wpts_changed[data->index] = 1;
1062   else
1063     lwp->arch_private->bpts_changed[data->index] = 1;
1064
1065   /* If the lwp isn't stopped, force it to momentarily pause, so
1066      we can update its breakpoint registers.  */
1067   if (!lwp->stopped)
1068     linux_stop_lwp (lwp);
1069
1070   return 0;
1071 }
1072
1073 /* Insert the hardware breakpoint (WATCHPOINT = 0) or watchpoint (WATCHPOINT
1074    =1) BPT for thread TID.  */
1075 static void
1076 arm_linux_insert_hw_breakpoint1 (const struct arm_linux_hw_breakpoint* bpt, 
1077                                  int watchpoint)
1078 {
1079   int pid;
1080   ptid_t pid_ptid;
1081   gdb_byte count, i;
1082   struct arm_linux_hw_breakpoint* bpts;
1083   struct update_registers_data data;
1084
1085   pid = ptid_get_pid (inferior_ptid);
1086   pid_ptid = pid_to_ptid (pid);
1087
1088   if (watchpoint)
1089     {
1090       count = arm_linux_get_hw_watchpoint_count ();
1091       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1092     }
1093   else
1094     {
1095       count = arm_linux_get_hw_breakpoint_count ();
1096       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1097     }
1098
1099   for (i = 0; i < count; ++i)
1100     if (!arm_hwbp_control_is_enabled (bpts[i].control))
1101       {
1102         data.watch = watchpoint;
1103         data.index = i;
1104         bpts[i] = *bpt;
1105         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1106         break;
1107       }
1108
1109   gdb_assert (i != count);
1110 }
1111
1112 /* Remove the hardware breakpoint (WATCHPOINT = 0) or watchpoint
1113    (WATCHPOINT = 1) BPT for thread TID.  */
1114 static void
1115 arm_linux_remove_hw_breakpoint1 (const struct arm_linux_hw_breakpoint *bpt, 
1116                                  int watchpoint)
1117 {
1118   int pid;
1119   gdb_byte count, i;
1120   ptid_t pid_ptid;
1121   struct arm_linux_hw_breakpoint* bpts;
1122   struct update_registers_data data;
1123
1124   pid = ptid_get_pid (inferior_ptid);
1125   pid_ptid = pid_to_ptid (pid);
1126
1127   if (watchpoint)
1128     {
1129       count = arm_linux_get_hw_watchpoint_count ();
1130       bpts = arm_linux_get_debug_reg_state (pid)->wpts;
1131     }
1132   else
1133     {
1134       count = arm_linux_get_hw_breakpoint_count ();
1135       bpts = arm_linux_get_debug_reg_state (pid)->bpts;
1136     }
1137
1138   for (i = 0; i < count; ++i)
1139     if (arm_linux_hw_breakpoint_equal (bpt, bpts + i))
1140       {
1141         data.watch = watchpoint;
1142         data.index = i;
1143         bpts[i].control = arm_hwbp_control_disable (bpts[i].control);
1144         iterate_over_lwps (pid_ptid, update_registers_callback, &data);
1145         break;
1146       }
1147
1148   gdb_assert (i != count);
1149 }
1150
1151 /* Insert a Hardware breakpoint.  */
1152 static int
1153 arm_linux_insert_hw_breakpoint (struct target_ops *self,
1154                                 struct gdbarch *gdbarch, 
1155                                 struct bp_target_info *bp_tgt)
1156 {
1157   struct lwp_info *lp;
1158   struct arm_linux_hw_breakpoint p;
1159
1160   if (arm_linux_get_hw_breakpoint_count () == 0)
1161     return -1;
1162
1163   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1164
1165   arm_linux_insert_hw_breakpoint1 (&p, 0);
1166
1167   return 0;
1168 }
1169
1170 /* Remove a hardware breakpoint.  */
1171 static int
1172 arm_linux_remove_hw_breakpoint (struct target_ops *self,
1173                                 struct gdbarch *gdbarch, 
1174                                 struct bp_target_info *bp_tgt)
1175 {
1176   struct lwp_info *lp;
1177   struct arm_linux_hw_breakpoint p;
1178
1179   if (arm_linux_get_hw_breakpoint_count () == 0)
1180     return -1;
1181
1182   arm_linux_hw_breakpoint_initialize (gdbarch, bp_tgt, &p);
1183
1184   arm_linux_remove_hw_breakpoint1 (&p, 0);
1185
1186   return 0;
1187 }
1188
1189 /* Are we able to use a hardware watchpoint for the LEN bytes starting at 
1190    ADDR?  */
1191 static int
1192 arm_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
1193                                        CORE_ADDR addr, int len)
1194 {
1195   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
1196   CORE_ADDR max_wp_length, aligned_addr;
1197
1198   /* Can not set watchpoints for zero or negative lengths.  */
1199   if (len <= 0)
1200     return 0;
1201
1202   /* Need to be able to use the ptrace interface.  */
1203   if (cap == NULL || cap->wp_count == 0)
1204     return 0;
1205
1206   /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
1207      range covered by a watchpoint.  */
1208   max_wp_length = (CORE_ADDR)cap->max_wp_length;
1209   aligned_addr = addr & ~(max_wp_length - 1);
1210
1211   if (aligned_addr + max_wp_length < addr + len)
1212     return 0;
1213
1214   /* The current ptrace interface can only handle watchpoints that are a
1215      power of 2.  */
1216   if ((len & (len - 1)) != 0)
1217     return 0;
1218
1219   /* All tests passed so we must be able to set a watchpoint.  */
1220   return 1;
1221 }
1222
1223 /* Insert a Hardware breakpoint.  */
1224 static int
1225 arm_linux_insert_watchpoint (struct target_ops *self,
1226                              CORE_ADDR addr, int len, int rw,
1227                              struct expression *cond)
1228 {
1229   struct lwp_info *lp;
1230   struct arm_linux_hw_breakpoint p;
1231
1232   if (arm_linux_get_hw_watchpoint_count () == 0)
1233     return -1;
1234
1235   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1236
1237   arm_linux_insert_hw_breakpoint1 (&p, 1);
1238
1239   return 0;
1240 }
1241
1242 /* Remove a hardware breakpoint.  */
1243 static int
1244 arm_linux_remove_watchpoint (struct target_ops *self,
1245                              CORE_ADDR addr, int len, int rw,
1246                              struct expression *cond)
1247 {
1248   struct lwp_info *lp;
1249   struct arm_linux_hw_breakpoint p;
1250
1251   if (arm_linux_get_hw_watchpoint_count () == 0)
1252     return -1;
1253
1254   arm_linux_hw_watchpoint_initialize (addr, len, rw, &p);
1255
1256   arm_linux_remove_hw_breakpoint1 (&p, 1);
1257
1258   return 0;
1259 }
1260
1261 /* What was the data address the target was stopped on accessing.  */
1262 static int
1263 arm_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1264 {
1265   siginfo_t siginfo;
1266   int slot;
1267
1268   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
1269     return 0;
1270
1271   /* This must be a hardware breakpoint.  */
1272   if (siginfo.si_signo != SIGTRAP
1273       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
1274     return 0;
1275
1276   /* We must be able to set hardware watchpoints.  */
1277   if (arm_linux_get_hw_watchpoint_count () == 0)
1278     return 0;
1279
1280   slot = siginfo.si_errno;
1281
1282   /* If we are in a positive slot then we're looking at a breakpoint and not
1283      a watchpoint.  */
1284   if (slot >= 0)
1285     return 0;
1286
1287   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
1288   return 1;
1289 }
1290
1291 /* Has the target been stopped by hitting a watchpoint?  */
1292 static int
1293 arm_linux_stopped_by_watchpoint (struct target_ops *ops)
1294 {
1295   CORE_ADDR addr;
1296   return arm_linux_stopped_data_address (ops, &addr);
1297 }
1298
1299 static int
1300 arm_linux_watchpoint_addr_within_range (struct target_ops *target,
1301                                         CORE_ADDR addr,
1302                                         CORE_ADDR start, int length)
1303 {
1304   return start <= addr && start + length - 1 >= addr;
1305 }
1306
1307 /* Handle thread creation.  We need to copy the breakpoints and watchpoints
1308    in the parent thread to the child thread.  */
1309 static void
1310 arm_linux_new_thread (struct lwp_info *lp)
1311 {
1312   int i;
1313   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
1314
1315   /* Mark that all the hardware breakpoint/watchpoint register pairs
1316      for this thread need to be initialized.  */
1317
1318   for (i = 0; i < MAX_BPTS; i++)
1319     {
1320       info->bpts_changed[i] = 1;
1321       info->wpts_changed[i] = 1;
1322     }
1323
1324   lp->arch_private = info;
1325 }
1326
1327 /* Called when resuming a thread.
1328    The hardware debug registers are updated when there is any change.  */
1329
1330 static void
1331 arm_linux_prepare_to_resume (struct lwp_info *lwp)
1332 {
1333   int pid, i;
1334   struct arm_linux_hw_breakpoint *bpts, *wpts;
1335   struct arch_lwp_info *arm_lwp_info = lwp->arch_private;
1336
1337   pid = ptid_get_lwp (lwp->ptid);
1338   bpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->bpts;
1339   wpts = arm_linux_get_debug_reg_state (ptid_get_pid (lwp->ptid))->wpts;
1340
1341   /* NULL means this is the main thread still going through the shell,
1342      or, no watchpoint has been set yet.  In that case, there's
1343      nothing to do.  */
1344   if (arm_lwp_info == NULL)
1345     return;
1346
1347   for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
1348     if (arm_lwp_info->bpts_changed[i])
1349       {
1350         errno = 0;
1351         if (arm_hwbp_control_is_enabled (bpts[i].control))
1352           if (ptrace (PTRACE_SETHBPREGS, pid,
1353               (PTRACE_TYPE_ARG3) ((i << 1) + 1), &bpts[i].address) < 0)
1354             perror_with_name (_("Unexpected error setting breakpoint"));
1355
1356         if (bpts[i].control != 0)
1357           if (ptrace (PTRACE_SETHBPREGS, pid,
1358               (PTRACE_TYPE_ARG3) ((i << 1) + 2), &bpts[i].control) < 0)
1359             perror_with_name (_("Unexpected error setting breakpoint"));
1360
1361         arm_lwp_info->bpts_changed[i] = 0;
1362       }
1363
1364   for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
1365     if (arm_lwp_info->wpts_changed[i])
1366       {
1367         errno = 0;
1368         if (arm_hwbp_control_is_enabled (wpts[i].control))
1369           if (ptrace (PTRACE_SETHBPREGS, pid,
1370               (PTRACE_TYPE_ARG3) -((i << 1) + 1), &wpts[i].address) < 0)
1371             perror_with_name (_("Unexpected error setting watchpoint"));
1372
1373         if (wpts[i].control != 0)
1374           if (ptrace (PTRACE_SETHBPREGS, pid,
1375               (PTRACE_TYPE_ARG3) -((i << 1) + 2), &wpts[i].control) < 0)
1376             perror_with_name (_("Unexpected error setting watchpoint"));
1377
1378         arm_lwp_info->wpts_changed[i] = 0;
1379       }
1380 }
1381
1382 /* linux_nat_new_fork hook.  */
1383
1384 static void
1385 arm_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
1386 {
1387   pid_t parent_pid;
1388   struct arm_linux_debug_reg_state *parent_state;
1389   struct arm_linux_debug_reg_state *child_state;
1390
1391   /* NULL means no watchpoint has ever been set in the parent.  In
1392      that case, there's nothing to do.  */
1393   if (parent->arch_private == NULL)
1394     return;
1395
1396   /* GDB core assumes the child inherits the watchpoints/hw
1397      breakpoints of the parent, and will remove them all from the
1398      forked off process.  Copy the debug registers mirrors into the
1399      new process so that all breakpoints and watchpoints can be
1400      removed together.  */
1401
1402   parent_pid = ptid_get_pid (parent->ptid);
1403   parent_state = arm_linux_get_debug_reg_state (parent_pid);
1404   child_state = arm_linux_get_debug_reg_state (child_pid);
1405   *child_state = *parent_state;
1406 }
1407
1408 void _initialize_arm_linux_nat (void);
1409
1410 void
1411 _initialize_arm_linux_nat (void)
1412 {
1413   struct target_ops *t;
1414
1415   /* Fill in the generic GNU/Linux methods.  */
1416   t = linux_target ();
1417
1418   /* Add our register access methods.  */
1419   t->to_fetch_registers = arm_linux_fetch_inferior_registers;
1420   t->to_store_registers = arm_linux_store_inferior_registers;
1421
1422   /* Add our hardware breakpoint and watchpoint implementation.  */
1423   t->to_can_use_hw_breakpoint = arm_linux_can_use_hw_breakpoint;
1424   t->to_insert_hw_breakpoint = arm_linux_insert_hw_breakpoint;
1425   t->to_remove_hw_breakpoint = arm_linux_remove_hw_breakpoint;
1426   t->to_region_ok_for_hw_watchpoint = arm_linux_region_ok_for_hw_watchpoint;
1427   t->to_insert_watchpoint = arm_linux_insert_watchpoint;
1428   t->to_remove_watchpoint = arm_linux_remove_watchpoint;
1429   t->to_stopped_by_watchpoint = arm_linux_stopped_by_watchpoint;
1430   t->to_stopped_data_address = arm_linux_stopped_data_address;
1431   t->to_watchpoint_addr_within_range = arm_linux_watchpoint_addr_within_range;
1432
1433   t->to_read_description = arm_linux_read_description;
1434
1435   /* Register the target.  */
1436   linux_nat_add_target (t);
1437
1438   /* Handle thread creation and exit.  */
1439   linux_nat_set_new_thread (t, arm_linux_new_thread);
1440   linux_nat_set_prepare_to_resume (t, arm_linux_prepare_to_resume);
1441
1442   /* Handle process creation and exit.  */
1443   linux_nat_set_new_fork (t, arm_linux_new_fork);
1444   linux_nat_set_forget_process (t, arm_linux_forget_process);
1445 }